Skip to content

Commit 9138bf1

Browse files
chore: Code cleanup across GPU passthrough and VM scripts
1 parent a659bcc commit 9138bf1

8 files changed

+44
-476
lines changed

bin/omarchy-gpu-passthrough

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ if ! source "$SCRIPT_DIR/omarchy-gpu-passthrough-utils"; then
1515
fi
1616

1717
# Main Help Function
18-
1918
show_main_help() {
2019
cat <<EOF
2120
GPU Passthrough for Virtual Machines
@@ -83,11 +82,9 @@ EOF
8382
}
8483

8584
# Main Dispatcher
86-
8785
case "${1:-}" in
8886
mode)
8987
shift
90-
# Handle mode --help specially
9188
if [[ "${1:-}" == "--help" || "${1:-}" == "-h" || "${1:-}" == "help" ]]; then
9289
exec "$SCRIPT_DIR/omarchy-gpu-passthrough-bind" mode --help
9390
else
@@ -106,10 +103,8 @@ info)
106103
show_main_help
107104
;;
108105
*)
109-
echo ""
110106
echo "Unknown command: \"$1\""
111107
echo "Run 'omarchy-gpu-passthrough --help' for usage information"
112-
echo ""
113108
exit 1
114109
;;
115110
esac

bin/omarchy-gpu-passthrough-bind

Lines changed: 0 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ check_gpu_processes() {
7373
if [[ -n "$compositor_pids" ]]; then
7474
echo ""
7575
msg_error "SAFETY: Compositor has NVIDIA handle!"
76-
echo ""
7776
msg_error "Compositor holding NVIDIA device - unbind will crash it (blackscreen)!"
7877
echo ""
7978
sudo fuser -v /dev/nvidia* 2>&1 | grep -iE "Hyprland|USER" | head -10
@@ -94,7 +93,6 @@ check_gpu_processes() {
9493
else
9594
echo ""
9695
msg_error "GPU actively in use (${gpu_memory_usage}MiB memory):"
97-
echo ""
9896
GPU_PROCESSES=$(sudo fuser -v /dev/nvidia* 2>&1 | grep -v "USER\|kernel" | head -20)
9997
echo "$GPU_PROCESSES"
10098
echo ""
@@ -116,34 +114,27 @@ check_gpu_processes() {
116114
gpu_memory_usage=$(nvidia-smi --query-gpu=memory.used --format=csv,noheader,nounits 2>/dev/null | awk '{print int($1)}')
117115
if [[ "$gpu_memory_usage" -lt 10 ]]; then
118116
log_success "GPU processes terminated, continuing..."
119-
# Continue with binding (fall through to binding code)
120117
else
121118
echo ""
122119
msg_error "GPU still in use (${gpu_memory_usage}MiB memory)"
123-
echo ""
124120
log_error "Cannot bind: GPU actively in use after kill attempt"
125121
exit 1
126122
fi
127123
else
128124
msg_info "Close GPU apps manually: sudo pkill -9 -f '<name>'"
129-
echo ""
130125
log_error "Cannot bind: GPU actively in use (${gpu_memory_usage}MiB memory used)"
131126
exit 1
132127
fi
133128
else
134129
msg_info "Close GPU apps or kill processes: sudo pkill -9 -f '<name>'"
135-
echo ""
136130
log_error "Cannot bind: GPU actively in use (${gpu_memory_usage}MiB memory used)"
137131
exit 1
138132
fi
139133
fi
140134
elif [[ "$current_driver" == "vfio-pci" ]]; then
141-
# VFIO GPU in use (likely VM running)
142135
echo ""
143136
msg_error "GPU in use by VM or VFIO process"
144137
echo ""
145-
146-
# Show process details (PIDs from gpu_processes variable set at line 36)
147138
local first_pid=""
148139
if [[ -n "$gpu_processes" ]]; then
149140
echo "Processes using GPU:"
@@ -158,7 +149,6 @@ check_gpu_processes() {
158149
fi
159150

160151
msg_warning "GPU is bound to vfio-pci and in use (likely by running VM)"
161-
echo ""
162152
echo "To unbind GPU:"
163153
echo " 1. Stop your VM: omarchy-windows-vm stop"
164154
if [[ -n "$first_pid" ]]; then
@@ -171,10 +161,8 @@ check_gpu_processes() {
171161
log_error "Cannot unbind: GPU in use by VFIO process (VM likely running)"
172162
exit 1
173163
else
174-
# AMD GPU in use
175164
echo ""
176165
msg_error "AMD GPU in use by processes:"
177-
echo ""
178166
GPU_PROCESSES=$(sudo fuser -v /dev/dri/card* /dev/dri/renderD* 2>&1 | grep -v "USER\|kernel" | head -20)
179167
echo "$GPU_PROCESSES"
180168
echo ""
@@ -196,24 +184,20 @@ check_gpu_processes() {
196184
GPU_PROCESSES_AFTER=$(sudo fuser -v /dev/dri/card* /dev/dri/renderD* 2>&1 | grep -v "USER\|kernel" || true)
197185
if [[ -z "$GPU_PROCESSES_AFTER" ]]; then
198186
log_success "GPU processes terminated, continuing..."
199-
# Continue with binding (fall through to binding code)
200187
else
201188
echo ""
202189
msg_error "GPU still in use by processes"
203190
echo "$GPU_PROCESSES_AFTER"
204-
echo ""
205191
log_error "Cannot bind: AMD GPU still in use after kill attempt"
206192
exit 1
207193
fi
208194
else
209195
msg_info "Close GPU apps manually: sudo pkill -9 -f '<name>'"
210-
echo ""
211196
log_error "Cannot bind: AMD GPU in use by processes"
212197
exit 1
213198
fi
214199
else
215200
msg_info "Close GPU apps or kill processes: sudo pkill -9 -f '<name>'"
216-
echo ""
217201
log_error "Cannot bind: AMD GPU in use by processes"
218202
exit 1
219203
fi
@@ -250,7 +234,6 @@ check_display_safety() {
250234
if [[ "$connected_displays" -gt 0 ]]; then
251235
echo ""
252236
msg_error "SAFETY: Monitor connected to dedicated GPU!"
253-
echo ""
254237
msg_error "Detected $connected_displays monitor(s) on $gpu_drm_card - unbind will cause BLACKSCREEN!"
255238
echo ""
256239
msg_warning "Solution:"
@@ -295,7 +278,6 @@ unload_nvidia_modules() {
295278
cmd_bind() {
296279
log_init
297280

298-
# Check configuration first (before printing headers)
299281
if ! load_gpu_config; then
300282
msg_section "GPU Mode: VM"
301283
echo ""
@@ -306,9 +288,6 @@ cmd_bind() {
306288
fi
307289

308290
msg_section "GPU Mode: Switch to VM"
309-
310-
echo ""
311-
echo "Binding GPU to vfio-pci (VM passthrough mode)..."
312291
echo ""
313292

314293
local current_driver=$(lspci -nnk -s "$GPU_PCI_ADDR" | grep "Kernel driver in use:" | awk '{print $5}')
@@ -356,9 +335,6 @@ cmd_bind() {
356335
sudo chgrp kvm /sys/bus/pci/drivers/vfio-pci/{bind,unbind,new_id,remove_id} 2>/dev/null || true
357336
fi
358337

359-
msg_info "Binding to vfio-pci..."
360-
log_info "Binding GPU to vfio-pci..."
361-
362338
echo "$GPU_VENDOR_ID $GPU_DEVICE_ID" | tee /sys/bus/pci/drivers/vfio-pci/new_id >/dev/null 2>&1 || true
363339

364340
if [[ ! -d "/sys/bus/pci/drivers/vfio-pci/0000:$GPU_PCI_ADDR" ]]; then
@@ -388,10 +364,6 @@ cmd_bind() {
388364
echo ""
389365
if [[ "$current_driver" == "vfio-pci" ]]; then
390366
msg_success "GPU bound to vfio-pci (ready for VM)"
391-
echo ""
392-
echo "Next: Start VM (GPU will be used by VM)"
393-
echo "Optional: 'omarchy-gpu-passthrough mode none' to unbind GPU"
394-
echo ""
395367
log_success "=== BIND SUCCESS: GPU ready for VM passthrough ==="
396368

397369
# Update state marker
@@ -408,7 +380,6 @@ cmd_bind() {
408380
cmd_set_none() {
409381
log_init
410382

411-
# Check configuration first (before printing headers)
412383
if ! load_gpu_config; then
413384
msg_section "GPU Mode: None"
414385
echo ""
@@ -419,9 +390,6 @@ cmd_set_none() {
419390
fi
420391

421392
msg_section "GPU Mode: Switch to None"
422-
423-
echo ""
424-
echo "Unbinding GPU from all drivers (inactive state)..."
425393
echo ""
426394

427395
local current_driver=$(lspci -nnk -s "$GPU_PCI_ADDR" | grep "Kernel driver in use:" | awk '{print $5}')
@@ -476,10 +444,6 @@ cmd_set_none() {
476444
echo ""
477445
if [[ -z "$current_driver" ]]; then
478446
msg_success "GPU set to none mode (no driver loaded)"
479-
echo ""
480-
echo "GPU is now inactive (same as boot state)"
481-
echo "To use: 'omarchy-gpu-passthrough mode vm' or 'mode host'"
482-
echo ""
483447
log_success "=== SET NONE SUCCESS: GPU inactive ==="
484448

485449
# Update state marker
@@ -496,7 +460,6 @@ cmd_set_none() {
496460
cmd_unbind() {
497461
log_init
498462

499-
# Check configuration first (before printing headers)
500463
if ! load_gpu_config; then
501464
msg_section "GPU Mode: Host"
502465
echo ""
@@ -507,9 +470,6 @@ cmd_unbind() {
507470
fi
508471

509472
msg_section "GPU Mode: Switch to Host"
510-
511-
echo ""
512-
echo "Restoring GPU to original driver (make available to host)..."
513473
echo ""
514474

515475
local current_driver=$(lspci -nnk -s "$GPU_PCI_ADDR" | grep "Kernel driver in use:" | awk '{print $5}')
@@ -633,17 +593,12 @@ cmd_unbind() {
633593

634594
show_spinner 2 "Loading driver..."
635595

636-
# Verify
637596
current_driver=$(lspci -nnk -s "$GPU_PCI_ADDR" | grep "Kernel driver in use:" | awk '{print $5}')
638597
log_info "Final driver: ${current_driver:-none}"
639598

640599
echo ""
641600
if [[ "$current_driver" == "$GPU_DRIVER_ORIGINAL" ]] || [[ -n "$current_driver" && "$current_driver" != "vfio-pci" ]]; then
642601
msg_success "GPU restored to $GPU_DRIVER_ORIGINAL (available to host)"
643-
echo ""
644-
echo "GPU available for host use (CUDA/compute tasks)"
645-
echo "To rebind for VM: omarchy-gpu-passthrough mode vm"
646-
echo ""
647602
log_success "=== UNBIND SUCCESS: GPU restored to $GPU_DRIVER_ORIGINAL ==="
648603

649604
# Update state marker
@@ -664,7 +619,6 @@ cmd_unbind() {
664619

665620
# Mode Command Functions
666621

667-
# Ensure state marker exists and matches reality (/var/run is tmpfs, marker recreated after reboot)
668622
ensure_state_marker() {
669623
# Use flock for atomic state marker operations (prevent race conditions)
670624
# Use user runtime directory (systemd standard) to avoid permission issues
@@ -751,7 +705,6 @@ cmd_mode_set() {
751705
*)
752706
echo ""
753707
msg_error "Invalid mode: $target"
754-
echo ""
755708
echo "Valid modes: none, vm, host"
756709
echo "Usage: omarchy-gpu-passthrough mode [none|vm|host]"
757710
echo ""
@@ -770,16 +723,13 @@ cmd_mode_get() {
770723
echo ""
771724
msg_error "GPU passthrough not configured"
772725
msg_info "Run: omarchy-gpu-passthrough setup"
773-
echo ""
774726
exit 1
775727
fi
776728

777729
local current_driver=$(lspci -nnk -s "$GPU_PCI_ADDR" | grep "Kernel driver in use:" | awk '{print $5}')
778730

779731
echo ""
780732
echo "GPU: $GPU_NAME ($GPU_PCI_ADDR)"
781-
echo ""
782-
783733
case "$current_driver" in
784734
vfio-pci)
785735
msg_success "Mode: vm"
@@ -845,7 +795,6 @@ cmd_quick_status() {
845795
echo ""
846796
echo "GPU: $GPU_NAME ($GPU_PCI_ADDR)"
847797
echo "Driver: ${current_driver:-none}"
848-
echo ""
849798

850799
if [[ "$current_driver" == "vfio-pci" ]]; then
851800
msg_success "Status: Bound to vfio-pci (ready for VM)"

0 commit comments

Comments
 (0)