forked from amoghmunikote/cmpunlocker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·438 lines (389 loc) · 15.7 KB
/
Copy pathinstall.sh
File metadata and controls
executable file
·438 lines (389 loc) · 15.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
#!/bin/bash
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
mapfile -t SUPPORTED_VERSIONS < <(grep -E '^[0-9]+\.[0-9]+\.[0-9]+$' "${SCRIPT_DIR}/driver/VERSION")
SUPPORTED_VERSIONS_CSV="$(IFS=', '; echo "${SUPPORTED_VERSIONS[*]}")"
LOG_DIR="${SCRIPT_DIR}/logs"
mkdir -p "${LOG_DIR}"
LOG_FILE="${LOG_DIR}/install_$(date +%Y%m%d_%H%M%S).log"
PROFILE_OVERRIDE=""
CONFIGURE_IOMMU=1
CONFIGURE_GEN2_SERVICE=1
CONFIGURE_PASSTHROUGH=1
for arg in "$@"; do
case "${arg}" in
--profile=8gb|--profile=8GB) PROFILE_OVERRIDE="8gb" ;;
--profile=10gb|--profile=10GB) PROFILE_OVERRIDE="10gb" ;;
--no-iommu) CONFIGURE_IOMMU=0 ;;
--no-gen2-service) CONFIGURE_GEN2_SERVICE=0 ;;
--no-passthrough) CONFIGURE_PASSTHROUGH=0 ;;
-h|--help)
cat <<'EOF'
Usage: sudo ./install.sh [--profile=8gb|10gb] [--no-iommu] [--no-gen2-service]
[--no-passthrough]
--profile=8gb Force 8GB metadata label (geometry is still chosen per PCI ID)
--profile=10gb Force 10GB metadata label (geometry is still chosen per PCI ID)
--no-iommu Do not touch the kernel command line (leave IOMMU settings alone)
--no-gen2-service
Do not install the early-boot PCIe Gen2 retrain service
--no-passthrough
Do not set the cards up for VM passthrough. By default the
unlock is made to survive being handed to vfio-pci, so a VM
sees an unlocked card with only a stock NVIDIA driver in it
By default the installer appends intel_iommu=on / amd_iommu=on plus iommu=pt to
the kernel command line so the IOMMU runs in passthrough mode. This takes effect
on the next reboot.
Without --profile, each unlockable GPU is classified by PCI device ID:
10de:20c2 → 8gb / 64GB unlock
10de:2082 → 10gb / 40GB unlock
Multi-GPU and mixed 8GB+10GB systems are supported in one install.
EOF
exit 0
;;
*)
echo "Unknown argument: ${arg}" >&2
echo "Try: sudo ./install.sh --help" >&2
exit 1
;;
esac
done
exec > >(tee -a "${LOG_FILE}") 2>&1
source "${SCRIPT_DIR}/common/lib.sh"
banner
step_init 7
step "Verifying root privileges"
[[ "${EUID}" -eq 0 ]] || die "Run as root: sudo ./install.sh"
ok "Running as root"
step "Detecting CMP 170HX GPU(s)"
mapfile -t PCI_LINES < <(lspci -nn 2>/dev/null | grep -iE '10de:20b0|10de:20c2|10de:2082' || true)
[[ ${#PCI_LINES[@]} -gt 0 ]] || die "No CMP 170HX GPU found (10de:20b0 / 10de:20c2 / 10de:2082)"
SMI_MEM_CACHE=""
if command -v nvidia-smi &>/dev/null; then
SMI_MEM_CACHE="$(nvidia-smi --query-gpu=pci.bus_id,memory.total --format=csv,noheader,nounits 2>/dev/null || true)"
fi
GPU_BDFS=()
GPU_DEVIDS=()
GPU_PROFILES=()
GPU_EXPECTED=()
GPU_CURRENT=()
COUNT_8GB=0
COUNT_10GB=0
COUNT_UNSUPPORTED=0
for PCI_LINE in "${PCI_LINES[@]}"; do
PCI="$(echo "${PCI_LINE}" | awk '{print $1}')"
PCI_FULL="$(normalize_bus_id "${PCI}")"
DEVID="$(echo "${PCI_LINE}" | grep -oE '10de:[0-9a-fA-F]{4}' | head -1 | cut -d: -f2 | tr '[:upper:]' '[:lower:]')"
PROF="$(profile_from_devid "${DEVID}")"
CUR_MEM="$(smi_memory_for_bus "${PCI_FULL}" || true)"
[[ -n "${CUR_MEM}" ]] || CUR_MEM="?"
if [[ "${PROF}" == "unsupported" ]]; then
COUNT_UNSUPPORTED=$((COUNT_UNSUPPORTED + 1))
warn "GPU ${PCI_FULL} (10de:${DEVID}) — unlock path not gated for this ID; skipping"
continue
fi
EXP="$(expected_mib_for_profile "${PROF}")"
GPU_BDFS+=("${PCI_FULL}")
GPU_DEVIDS+=("${DEVID}")
GPU_PROFILES+=("${PROF}")
GPU_EXPECTED+=("${EXP}")
GPU_CURRENT+=("${CUR_MEM}")
if [[ "${PROF}" == "8gb" ]]; then
COUNT_8GB=$((COUNT_8GB + 1))
else
COUNT_10GB=$((COUNT_10GB + 1))
fi
if [[ "${CUR_MEM}" != "?" ]]; then
ok "GPU ${PCI_FULL} (10de:${DEVID}) → ${PROF} (current ${CUR_MEM} MiB, expect ~${EXP} MiB unlocked)"
else
ok "GPU ${PCI_FULL} (10de:${DEVID}) → ${PROF} (expect ~${EXP} MiB unlocked)"
fi
done
[[ ${#GPU_BDFS[@]} -gt 0 ]] || die "No unlockable CMP 170HX GPUs found (need 10de:20c2 and/or 10de:2082)"
if (( COUNT_UNSUPPORTED > 0 )); then
info "Inventory: ${#GPU_BDFS[@]} unlockable (${COUNT_8GB}× 8gb, ${COUNT_10GB}× 10gb), ${COUNT_UNSUPPORTED} unsupported"
else
info "Inventory: ${#GPU_BDFS[@]} unlockable (${COUNT_8GB}× 8gb, ${COUNT_10GB}× 10gb)"
fi
step "Selecting card memory profile"
CARD_PROFILE=""
if (( COUNT_8GB > 0 && COUNT_10GB > 0 )); then
CARD_PROFILE="mixed"
ok "Mixed variants detected → profile mixed (runtime geometry by PCI ID)"
if [[ -n "${PROFILE_OVERRIDE}" ]]; then
warn "--profile=${PROFILE_OVERRIDE} ignored for mixed inventory; card_profile stays mixed (each card unlocks by PCI ID)"
fi
elif (( COUNT_8GB > 0 )); then
CARD_PROFILE="8gb"
elif (( COUNT_10GB > 0 )); then
CARD_PROFILE="10gb"
else
die "Internal error: no unlockable profiles counted"
fi
if [[ -n "${PROFILE_OVERRIDE}" && "${CARD_PROFILE}" != "mixed" ]]; then
if [[ "${PROFILE_OVERRIDE}" != "${CARD_PROFILE}" ]]; then
warn "Inventory is ${CARD_PROFILE} but --profile=${PROFILE_OVERRIDE} was forced (metadata only; geometry follows PCI ID)"
else
ok "Profile forced via --profile=${CARD_PROFILE}"
fi
CARD_PROFILE="${PROFILE_OVERRIDE}"
fi
case "${CARD_PROFILE}" in
8gb)
info "Unlock geometry: 64GB per card (CFG1=0x02779000 LMR=0x0000020B)"
;;
10gb)
info "Unlock geometry: 40GB per card (CFG1=0x02669000 LMR=0x0000028A)"
;;
mixed)
info "Unlock geometry: 64GB for 20c2 / 40GB for 2082"
;;
*)
die "Internal error: bad profile ${CARD_PROFILE}"
;;
esac
GPU_INVENTORY_LINES=()
for i in "${!GPU_BDFS[@]}"; do
GPU_INVENTORY_LINES+=("${GPU_BDFS[$i]} ${GPU_DEVIDS[$i]} ${GPU_PROFILES[$i]} ${GPU_EXPECTED[$i]}")
done
export CMPUNLOCKER_CARD_PROFILE="${CARD_PROFILE}"
CMPUNLOCKER_GPU_INVENTORY="$(printf '%s\n' "${GPU_INVENTORY_LINES[@]}")"
export CMPUNLOCKER_GPU_INVENTORY
step "Verifying nvidia-open (${SUPPORTED_VERSIONS_CSV})"
[[ ${#SUPPORTED_VERSIONS[@]} -gt 0 ]] || die "No supported versions listed in driver/VERSION"
if [[ -d /sys/firmware/efi ]] && command -v mokutil &>/dev/null; then
if mokutil --sb-state 2>/dev/null | grep -qi 'SecureBoot enabled'; then
die "Secure Boot is enabled. Disable it before installing unsigned patched modules."
fi
fi
version_supported() {
local v="$1"
local s
for s in "${SUPPORTED_VERSIONS[@]}"; do
[[ "${v}" == "${s}" ]] && return 0
done
return 1
}
detected=""
if [[ -r /proc/driver/nvidia/version ]]; then
detected="$(grep -oE '[0-9]+\.[0-9]+\.[0-9]+' /proc/driver/nvidia/version | head -1 || true)"
fi
if [[ -z "${detected}" ]] && command -v nvidia-smi &>/dev/null; then
smi_version="$(nvidia-smi --query-gpu=driver_version --format=csv,noheader 2>/dev/null | head -1 | tr -d '[:space:]' || true)"
[[ "${smi_version}" =~ ^[0-9]+(\.[0-9]+)+$ ]] && detected="${smi_version}"
fi
if [[ -z "${detected}" ]]; then
for cand in "${SUPPORTED_VERSIONS[@]}"; do
if [[ -d "/lib/firmware/nvidia/${cand}" ]]; then
detected="${cand}"
break
fi
done
if [[ -z "${detected}" ]]; then
fw="$(ls -d /lib/firmware/nvidia/*/ 2>/dev/null | sed 's|.*/nvidia/||;s|/||' | sort -rV | head -1 || true)"
detected="${fw}"
fi
fi
[[ -n "${detected}" ]] || die "Could not detect an installed NVIDIA driver. Install nvidia-open ${SUPPORTED_VERSIONS_CSV} first."
version_supported "${detected}" || die "Installed driver is ${detected}, but cmpunlocker requires one of: ${SUPPORTED_VERSIONS_CSV}."
ok "NVIDIA driver ${detected} is supported"
[[ -d "/lib/modules/$(uname -r)/build" ]] || die "Kernel headers missing for $(uname -r). Install linux-headers-$(uname -r) or kernel-devel."
ok "Kernel headers present for $(uname -r)"
info "Removing conflicting NVIDIA DKMS modules (not all systems have any)"
for ver in "${SUPPORTED_VERSIONS[@]}"; do
dkms remove nvidia/"${ver}" --all 2>/dev/null || true
done
depmod -a "$(uname -r)"
ok "DKMS conflicting modules resolution complete"
step "Building and installing patched modules"
chmod +x "${SCRIPT_DIR}/driver/build.sh"
CMPUNLOCKER_DRIVER_VERSION="${detected}" \
CMPUNLOCKER_CARD_PROFILE="${CARD_PROFILE}" \
CMPUNLOCKER_GPU_INVENTORY="${CMPUNLOCKER_GPU_INVENTORY}" \
"${SCRIPT_DIR}/driver/build.sh"
ok "Patched modules installed (profile ${CARD_PROFILE})"
step "Setting up VM passthrough"
PASSTHROUGH_STATUS="skipped"
if (( CONFIGURE_PASSTHROUGH == 1 )); then
chmod +x "${SCRIPT_DIR}/tools/passthrough-setup.sh"
if CMPUNLOCKER_KVER="$(uname -r)" "${SCRIPT_DIR}/tools/passthrough-setup.sh"; then
PASSTHROUGH_STATUS="armed"
else
PASSTHROUGH_STATUS="failed"
warn "passthrough setup failed; the unlock still works on this host"
fi
else
warn "--no-passthrough given; cards are not prepared for VM passthrough"
fi
info "Configuring PCIe Gen2"
cat > /etc/modprobe.d/cmp-pcie-gen2.conf <<'EOF'
options nvidia NVreg_RegistryDwords="RmForceEnableGen2=1;RMPcieLinkSpeed=0x1"
EOF
ok "Wrote /etc/modprobe.d/cmp-pcie-gen2.conf"
for legacy_unit in cmpretrain.service cmp-gen2-retrain.service; do
systemctl disable --now "${legacy_unit}" 2>/dev/null || true
systemctl reset-failed "${legacy_unit}" 2>/dev/null || true
done
rm -f /etc/systemd/system/cmpretrain.service \
/etc/systemd/system/cmp-gen2-retrain.service \
/usr/local/sbin/retrain.sh \
/usr/local/sbin/cmp-gen2-retrain.sh
systemctl daemon-reload
ok "Removed legacy PCIe retrain helpers"
if (( CONFIGURE_GEN2_SERVICE == 1 )); then
chmod +x "${SCRIPT_DIR}/tools/hammer.sh" \
"${SCRIPT_DIR}/tools/service.sh"
"${SCRIPT_DIR}/tools/service.sh" install
ok "Early-boot Gen2 retrain service armed (not started in this session)"
else
warn "--no-gen2-service given; early-boot PCIe retraining is not installed"
fi
info "Configuring IOMMU (passthrough)"
IOMMU_STATUS="skipped"
IOMMU_PARAMS=""
iommu_params_for_cpu() {
local vendor=""
vendor="$(awk -F': ' '/^vendor_id/{print $2; exit}' /proc/cpuinfo 2>/dev/null || true)"
case "${vendor}" in
GenuineIntel) echo "intel_iommu=on iommu=pt" ;;
AuthenticAMD) echo "amd_iommu=on iommu=pt" ;;
*) echo "" ;;
esac
}
cmdline_merge() {
local current="$1"
local token out=()
for token in ${current}; do
case "${token}" in
intel_iommu=*|amd_iommu=*|iommu=*) continue ;;
*) out+=("${token}") ;;
esac
done
for token in ${IOMMU_PARAMS}; do
out+=("${token}")
done
echo "${out[*]}"
}
configure_iommu_grub() {
local grub_file="/etc/default/grub"
local key="GRUB_CMDLINE_LINUX_DEFAULT"
local current merged
grep -q "^${key}=" "${grub_file}" || key="GRUB_CMDLINE_LINUX"
if grep -q "^${key}=" "${grub_file}"; then
current="$(sed -n "s/^${key}=\"\(.*\)\"$/\1/p" "${grub_file}" | head -1)"
else
current=""
fi
merged="$(cmdline_merge "${current}")"
if [[ "${current}" == "${merged}" ]]; then
ok "GRUB already has ${IOMMU_PARAMS} (${key})"
IOMMU_STATUS="already-set"
return 0
fi
cp -a "${grub_file}" "${grub_file}.cmpunlocker.bak"
if grep -q "^${key}=" "${grub_file}"; then
local escaped="${merged//\//\\/}"
sed -i "s/^${key}=.*/${key}=\"${escaped}\"/" "${grub_file}"
else
printf '%s="%s"\n' "${key}" "${merged}" >> "${grub_file}"
fi
ok "Set ${key}=\"${merged}\" (backup: ${grub_file}.cmpunlocker.bak)"
if command -v update-grub &>/dev/null; then
update-grub
elif command -v grub2-mkconfig &>/dev/null; then
local cfg="/boot/grub2/grub.cfg"
local efi_cfg
efi_cfg="$(ls /boot/efi/EFI/*/grub.cfg 2>/dev/null | head -1 || true)"
[[ -n "${efi_cfg}" ]] && cfg="${efi_cfg}"
grub2-mkconfig -o "${cfg}"
elif command -v grub-mkconfig &>/dev/null; then
grub-mkconfig -o /boot/grub/grub.cfg
else
warn "No grub config generator found — regenerate grub.cfg manually"
IOMMU_STATUS="needs-grub-regen"
return 0
fi
ok "Regenerated GRUB config"
IOMMU_STATUS="configured"
}
configure_iommu_kernel_cmdline() {
local file="/etc/kernel/cmdline"
local current merged
current="$(tr -d '\n' < "${file}")"
merged="$(cmdline_merge "${current}")"
if [[ "${current}" == "${merged}" ]]; then
ok "${file} already has ${IOMMU_PARAMS}"
IOMMU_STATUS="already-set"
return 0
fi
cp -a "${file}" "${file}.cmpunlocker.bak"
printf '%s\n' "${merged}" > "${file}"
ok "Set ${file} to \"${merged}\" (backup: ${file}.cmpunlocker.bak)"
if command -v kernel-install &>/dev/null && [[ -d /boot/loader/entries ]]; then
for kdir in /lib/modules/*/; do
kver="$(basename "${kdir}")"
[[ -f "${kdir}/vmlinuz" ]] || continue
kernel-install add "${kver}" "${kdir}/vmlinuz" 2>/dev/null || true
done
ok "Refreshed systemd-boot entries"
IOMMU_STATUS="configured"
else
warn "Update your boot entries so ${file} takes effect"
IOMMU_STATUS="needs-boot-refresh"
fi
}
if (( CONFIGURE_IOMMU == 0 )); then
warn "--no-iommu given; leaving kernel command line untouched"
else
IOMMU_PARAMS="$(iommu_params_for_cpu)"
if [[ -z "${IOMMU_PARAMS}" ]]; then
warn "Unrecognized CPU vendor — cannot pick IOMMU kernel parameters; skipping"
elif [[ -f /etc/default/grub ]]; then
info "Target: ${IOMMU_PARAMS} (GRUB)"
configure_iommu_grub
elif [[ -f /etc/kernel/cmdline ]]; then
info "Target: ${IOMMU_PARAMS} (systemd-boot)"
configure_iommu_kernel_cmdline
else
warn "No /etc/default/grub or /etc/kernel/cmdline found"
warn "Add these to your kernel command line manually: ${IOMMU_PARAMS}"
IOMMU_STATUS="manual"
fi
if grep -qw iommu=pt /proc/cmdline 2>/dev/null && [[ -d /sys/class/iommu ]] && [[ -n "$(ls -A /sys/class/iommu 2>/dev/null)" ]]; then
ok "IOMMU is already active in passthrough mode on the running kernel"
elif [[ "${IOMMU_STATUS}" != "skipped" ]]; then
info "IOMMU passthrough takes effect after the next reboot"
warn "IOMMU must also be enabled in BIOS/UEFI (VT-d / AMD-Vi / SVM)"
fi
fi
step "Done"
banner
echo "cmpunlocker install finished!"
echo "Profile: ${CARD_PROFILE} | ${#GPU_BDFS[@]} GPU(s): ${COUNT_8GB}× 8gb, ${COUNT_10GB}× 10gb"
echo "Passthrough: ${PASSTHROUGH_STATUS}"
if [[ -n "${IOMMU_PARAMS}" && "${IOMMU_STATUS}" != "skipped" ]]; then
echo "IOMMU: ${IOMMU_PARAMS} (${IOMMU_STATUS})"
else
echo "IOMMU: not configured"
fi
echo ""
echo "Per-GPU expectations after unlock:"
printf " %-16s %-8s %-8s %s\n" "BDF" "PCI ID" "Variant" "Expect MiB"
for i in "${!GPU_BDFS[@]}"; do
printf " %-16s %-8s %-8s ~%s\n" "${GPU_BDFS[$i]}" "${GPU_DEVIDS[$i]}" "${GPU_PROFILES[$i]}" "${GPU_EXPECTED[$i]}"
done
echo ""
echo "Next:"
echo -e " 1. Cold reboot recommended: ${CYAN}sudo shutdown -h now${NC} (then power on)"
echo -e " 2. Verify all GPUs: ${CYAN}sudo ./verify.sh${NC}"
echo -e " 3. Verify PCIe Gen2: ${CYAN}nvidia-smi --query-gpu=pcie.link.gen.current,pcie.link.gen.max --format=csv${NC} (expect 2,2)"
echo -e " 4. Or check manually: ${CYAN}nvidia-smi${NC}"
echo -e " 5. Unlock logs: ${CYAN}sudo dmesg | grep SEC2_DEBUG${NC}"
echo -e " 6. Verify IOMMU after reboot: ${CYAN}cat /proc/cmdline${NC} and ${CYAN}ls /sys/class/iommu${NC}"
if (( CONFIGURE_GEN2_SERVICE == 1 )); then
echo -e " 7. Verify negotiated Gen2: ${CYAN}sudo ./tools/service.sh verify${NC}"
echo -e " Recovery boot option: ${CYAN}systemd.mask=gen2.service${NC}"
fi
echo ""
echo "This script removed the nvidia DKMS kernel modules. You will need to re-run this script after each kernel upgrade"
echo "Log saved to: ${LOG_FILE}"
echo ""