vm-curator 0.4.5

A TUI application to manage QEMU VM library
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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
//! PCI Passthrough Screen
//!
//! Displays PCI devices for passthrough selection with special handling for GPUs.
//! Shows IOMMU groups, driver bindings, and prerequisite status.

use ratatui::{
    layout::{Alignment, Constraint, Direction, Layout, Rect},
    prelude::*,
    style::{Color, Modifier, Style},
    widgets::{Block, Borders, Clear, List, ListItem, ListState, Paragraph, Wrap},
};

use crate::app::App;
use crate::hardware::PciDevice;

/// Render the PCI passthrough screen
pub fn render(app: &App, frame: &mut Frame) {
    let area = frame.area();

    // Calculate dialog size - larger to accommodate device list
    let dialog_width = 85.min(area.width.saturating_sub(4));
    let dialog_height = 28.min(area.height.saturating_sub(4));

    let dialog_area = centered_rect(dialog_width, dialog_height, area);

    // Clear the background
    frame.render_widget(Clear, dialog_area);

    let selected_count = app.selected_pci_devices.len();
    let title = if app.config.single_gpu_enabled {
        // Single GPU mode - don't show GPU count (boot VGA is passed through differently)
        format!(" PCI Passthrough ({} selected) ", selected_count)
    } else if app.config.enable_multi_gpu_passthrough {
        let gpu_count = app.pci_devices.iter().filter(|d| d.is_gpu() && !d.is_boot_vga).count();
        format!(
            " PCI Passthrough ({} selected, {} GPU{} available) ",
            selected_count,
            gpu_count,
            if gpu_count == 1 { "" } else { "s" }
        )
    } else {
        format!(" PCI Passthrough ({} selected) ", selected_count)
    };

    let block = Block::default()
        .title(title)
        .borders(Borders::ALL)
        .border_style(Style::default().fg(Color::Cyan))
        .style(Style::default().bg(Color::Black));

    let inner = block.inner(dialog_area);
    frame.render_widget(block, dialog_area);

    // Layout: status bar, device list, help
    let chunks = Layout::default()
        .direction(Direction::Vertical)
        .constraints([
            Constraint::Length(3), // Status bar
            Constraint::Min(8),    // Device list
            Constraint::Length(2), // Help text
        ])
        .split(inner);

    // Render status bar
    render_status_bar(app, frame, chunks[0]);

    // Add horizontal margins for device list
    let h_chunks = Layout::default()
        .direction(Direction::Horizontal)
        .constraints([
            Constraint::Length(1),  // Left margin
            Constraint::Min(1),     // Content
            Constraint::Length(1),  // Right margin
        ])
        .split(chunks[1]);

    // Render device list
    render_device_list(app, frame, h_chunks[1]);

    // Help text - show GPU options only when multi-GPU passthrough is enabled (not single GPU)
    let help_text = if app.config.enable_multi_gpu_passthrough && !app.config.single_gpu_enabled {
        "[Space/Enter] Toggle  [g] Auto-select GPU  [s] Save  [p] Prerequisites  [Esc] Back"
    } else {
        "[Space/Enter] Toggle  [s] Save  [Esc] Back"
    };
    let help = Paragraph::new(help_text)
        .style(Style::default().fg(Color::DarkGray))
        .alignment(Alignment::Center);
    frame.render_widget(help, chunks[2]);
}

/// Render the status bar showing prerequisite status
fn render_status_bar(app: &App, frame: &mut Frame, area: Rect) {
    // Handle different GPU passthrough modes
    let spans = if app.config.single_gpu_enabled {
        // Single GPU passthrough mode
        vec![
            Span::styled(" Mode: ", Style::default().fg(Color::White)),
            Span::styled("Single GPU Passthrough", Style::default().fg(Color::Cyan)),
            Span::styled("  (GPU selection managed via Single GPU Setup)", Style::default().fg(Color::DarkGray)),
        ]
    } else if app.config.enable_multi_gpu_passthrough {
        // Multi-GPU passthrough mode - show full status
        let status = app.multi_gpu_status.as_ref();
        let (status_text, status_color) = if let Some(status) = status {
            if status.is_ready() {
                (status.summary(), Color::Green)
            } else {
                (status.summary(), Color::Yellow)
            }
        } else {
            ("Status unknown".to_string(), Color::DarkGray)
        };

        let mut spans = vec![
            Span::styled(" Status: ", Style::default().fg(Color::White)),
            Span::styled(status_text, Style::default().fg(status_color)),
        ];

        // Add quick indicators
        if let Some(status) = status {
            spans.push(Span::raw("  |  "));
            spans.push(Span::styled(
                "IOMMU",
                Style::default().fg(if status.iommu_enabled { Color::Green } else { Color::Red }),
            ));
            spans.push(Span::raw(" "));
            spans.push(Span::styled(
                "VFIO",
                Style::default().fg(if status.vfio_loaded { Color::Green } else { Color::Red }),
            ));
        }
        spans
    } else {
        // GPU passthrough disabled - show simple message
        vec![
            Span::styled(" Select PCI devices to pass through to the VM", Style::default().fg(Color::DarkGray)),
        ]
    };

    let status_para = Paragraph::new(Line::from(spans))
        .block(Block::default().borders(Borders::BOTTOM).border_style(Style::default().fg(Color::DarkGray)));

    frame.render_widget(status_para, area);
}

/// Render the device list
fn render_device_list(app: &App, frame: &mut Frame, area: Rect) {
    if app.pci_devices.is_empty() {
        let msg = Paragraph::new("No PCI devices found.\n\nEnsure you have permission to read /sys/bus/pci/devices.")
            .style(Style::default().fg(Color::DarkGray))
            .alignment(Alignment::Center);
        frame.render_widget(msg, area);
        return;
    }

    // Filter devices to show useful passthrough candidates
    let relevant_devices: Vec<(usize, &PciDevice)> = app
        .pci_devices
        .iter()
        .enumerate()
        .filter(|(_, d)| {
            // Always show useful passthrough candidates (USB, network, storage, audio)
            if d.is_passthrough_candidate() {
                return true;
            }
            // When GPU passthrough is enabled, also show GPUs and GPU-related devices
            if app.config.enable_multi_gpu_passthrough {
                d.is_gpu()
                    || d.is_audio()
                    || d.iommu_group.is_some() && is_device_in_gpu_group(d, &app.pci_devices)
            } else {
                false
            }
        })
        .collect();

    let items: Vec<ListItem> = relevant_devices
        .iter()
        .map(|(original_idx, device)| {
            let selected = app.selected_pci_devices.contains(original_idx);
            let is_current = *original_idx == app.selected_menu_item;

            // Build the display line
            let checkbox = if selected { "[X]" } else { "[ ]" };

            // Determine device color based on type
            let device_color = if device.is_boot_vga {
                Color::Red // Boot VGA - cannot select
            } else if device.is_gpu() {
                Color::Cyan // GPU - highlight
            } else if device.is_audio() {
                Color::Magenta // Audio device
            } else {
                Color::White
            };

            // Build device info string
            let mut info_parts = vec![device.short_vendor().to_string()];

            if !device.device_name.is_empty() {
                info_parts.push(device.device_name.clone());
            } else {
                info_parts.push(device.class_description().to_string());
            }

            let device_info = info_parts.join(" ");

            // Driver binding
            let driver_info = device
                .driver
                .as_ref()
                .map(|d| format!("[{}]", d))
                .unwrap_or_else(|| "[no driver]".to_string());

            let driver_color = if device.is_vfio_bound() {
                Color::Green
            } else {
                Color::DarkGray
            };

            // IOMMU group
            let iommu_info = device
                .iommu_group
                .map(|g| format!("IOMMU:{}", g))
                .unwrap_or_else(|| "no IOMMU".to_string());

            // Build the line with proper formatting
            let line = Line::from(vec![
                Span::styled(
                    format!("{} ", checkbox),
                    if is_current {
                        Style::default().fg(Color::Yellow).add_modifier(Modifier::BOLD)
                    } else if selected {
                        Style::default().fg(Color::Green)
                    } else {
                        Style::default().fg(Color::White)
                    },
                ),
                Span::styled(
                    format!("{:<12} ", device.address),
                    Style::default().fg(Color::White),
                ),
                Span::styled(
                    format!("{:<40} ", truncate_str(&device_info, 40)),
                    Style::default().fg(device_color),
                ),
                Span::styled(
                    format!("{:<12} ", driver_info),
                    Style::default().fg(driver_color),
                ),
                Span::styled(
                    iommu_info,
                    Style::default().fg(Color::DarkGray),
                ),
            ]);

            // Add boot VGA warning
            let mut lines = vec![line];
            if device.is_boot_vga {
                lines.push(Line::styled(
                    "     (Boot VGA - cannot be passed through)",
                    Style::default().fg(Color::Red).add_modifier(Modifier::ITALIC),
                ));
            }

            ListItem::new(lines)
        })
        .collect();

    // Map selected_menu_item to the filtered list index
    let list_selected = relevant_devices
        .iter()
        .position(|(idx, _)| *idx == app.selected_menu_item);

    let mut state = ListState::default();
    state.select(list_selected);

    let list = List::new(items)
        .highlight_symbol("> ")
        .highlight_style(Style::default().add_modifier(Modifier::BOLD));

    frame.render_stateful_widget(list, area, &mut state);
}

/// Render the prerequisites screen
#[allow(dead_code)]
pub fn render_prerequisites(app: &App, frame: &mut Frame) {
    let area = frame.area();

    let dialog_width = 70.min(area.width.saturating_sub(4));
    let dialog_height = 22.min(area.height.saturating_sub(4));

    let dialog_area = centered_rect(dialog_width, dialog_height, area);
    frame.render_widget(Clear, dialog_area);

    let block = Block::default()
        .title(" GPU Passthrough Prerequisites ")
        .borders(Borders::ALL)
        .border_style(Style::default().fg(Color::Cyan))
        .style(Style::default().bg(Color::Black));

    let inner = block.inner(dialog_area);
    frame.render_widget(block, dialog_area);

    let status = app.multi_gpu_status.as_ref();

    let mut lines: Vec<Line> = Vec::new();

    lines.push(Line::styled(
        "GPU Passthrough Prerequisites",
        Style::default().fg(Color::White).add_modifier(Modifier::BOLD),
    ));
    lines.push(Line::raw(""));

    if let Some(status) = status {
        // IOMMU check
        let iommu_icon = if status.iommu_enabled { " OK " } else { "FAIL" };
        let iommu_color = if status.iommu_enabled { Color::Green } else { Color::Red };
        lines.push(Line::from(vec![
            Span::styled(format!("[{}] ", iommu_icon), Style::default().fg(iommu_color)),
            Span::raw("IOMMU enabled in kernel"),
        ]));
        if !status.iommu_enabled {
            lines.push(Line::styled(
                "    Add intel_iommu=on or amd_iommu=on to kernel parameters",
                Style::default().fg(Color::Yellow),
            ));
        }

        // VFIO check
        let vfio_icon = if status.vfio_loaded { " OK " } else { "FAIL" };
        let vfio_color = if status.vfio_loaded { Color::Green } else { Color::Red };
        lines.push(Line::from(vec![
            Span::styled(format!("[{}] ", vfio_icon), Style::default().fg(vfio_color)),
            Span::raw("VFIO modules loaded"),
        ]));
        if !status.vfio_loaded {
            lines.push(Line::styled(
                "    Run: sudo modprobe vfio-pci",
                Style::default().fg(Color::Yellow),
            ));
        }

        // GPU availability
        let gpu_available = !status.passthrough_gpus.is_empty();
        let gpu_icon = if gpu_available { " OK " } else { "FAIL" };
        let gpu_color = if gpu_available { Color::Green } else { Color::Red };
        lines.push(Line::from(vec![
            Span::styled(format!("[{}] ", gpu_icon), Style::default().fg(gpu_color)),
            Span::raw(format!(
                "Secondary GPU available ({} found)",
                status.passthrough_gpus.len()
            )),
        ]));

        if let Some(ref boot_vga) = status.boot_vga {
            lines.push(Line::styled(
                format!("    Boot VGA: {} (cannot be passed through)", boot_vga.display_name()),
                Style::default().fg(Color::DarkGray),
            ));
        }

        for gpu in &status.passthrough_gpus {
            let driver_status = if gpu.is_vfio_bound() {
                "vfio-pci"
            } else {
                gpu.driver.as_deref().unwrap_or("no driver")
            };
            lines.push(Line::styled(
                format!("    {} [{}]", gpu.display_name(), driver_status),
                Style::default().fg(Color::Cyan),
            ));
        }

        // Warnings
        if !status.warnings.is_empty() {
            lines.push(Line::raw(""));
            lines.push(Line::styled("Warnings:", Style::default().fg(Color::Yellow)));
            for warning in &status.warnings {
                lines.push(Line::styled(format!("  - {}", warning), Style::default().fg(Color::Yellow)));
            }
        }

        // Looking Glass info
        lines.push(Line::raw(""));
        lines.push(Line::styled(
            "For Looking Glass:",
            Style::default().fg(Color::White).add_modifier(Modifier::BOLD),
        ));
        lines.push(Line::raw("  - Install looking-glass-client on host"));
        lines.push(Line::raw("  - Install Looking Glass Host in guest VM"));
        lines.push(Line::raw("  - Connect HDMI/DP dummy plug to guest GPU"));
    } else {
        lines.push(Line::styled(
            "Unable to check prerequisites",
            Style::default().fg(Color::Red),
        ));
    }

    lines.push(Line::raw(""));
    lines.push(Line::styled(
        "[Press any key to close]",
        Style::default().fg(Color::DarkGray),
    ));

    let para = Paragraph::new(lines)
        .wrap(Wrap { trim: false });
    frame.render_widget(para, inner);
}

/// Check if a device is in the same IOMMU group as any GPU
fn is_device_in_gpu_group(device: &PciDevice, all_devices: &[PciDevice]) -> bool {
    if let Some(group) = device.iommu_group {
        for d in all_devices {
            if d.is_gpu() && d.iommu_group == Some(group) && d.address != device.address {
                return true;
            }
        }
    }
    false
}

/// Truncate a string to max_len characters, adding "..." if truncated
fn truncate_str(s: &str, max_len: usize) -> String {
    if s.len() <= max_len {
        s.to_string()
    } else {
        format!("{}...", &s[..max_len.saturating_sub(3)])
    }
}

fn centered_rect(width: u16, height: u16, area: Rect) -> Rect {
    let x = area.x + (area.width.saturating_sub(width)) / 2;
    let y = area.y + (area.height.saturating_sub(height)) / 2;
    Rect::new(x, y, width, height)
}

/// Handle key input for PCI passthrough screen
pub fn handle_key(app: &mut App, key: crossterm::event::KeyEvent) -> anyhow::Result<()> {
    use crossterm::event::KeyCode;

    let gpu_enabled = app.config.enable_multi_gpu_passthrough;

    // Get relevant devices for navigation (must match render filter)
    let relevant_indices: Vec<usize> = app
        .pci_devices
        .iter()
        .enumerate()
        .filter(|(_, d)| {
            // Always include useful passthrough candidates
            if d.is_passthrough_candidate() {
                return true;
            }
            // When GPU passthrough is enabled, also include GPUs and GPU-related devices
            if gpu_enabled {
                d.is_gpu()
                    || d.is_audio()
                    || d.iommu_group.is_some() && is_device_in_gpu_group(d, &app.pci_devices)
            } else {
                false
            }
        })
        .map(|(i, _)| i)
        .collect();

    match key.code {
        KeyCode::Esc => {
            app.selected_menu_item = 0; // Reset for management menu
            app.pop_screen();
        }
        KeyCode::Char('j') | KeyCode::Down => {
            // Find current position in relevant devices
            if let Some(current_pos) = relevant_indices.iter().position(|&i| i == app.selected_menu_item) {
                if current_pos < relevant_indices.len().saturating_sub(1) {
                    app.selected_menu_item = relevant_indices[current_pos + 1];
                }
            } else if !relevant_indices.is_empty() {
                app.selected_menu_item = relevant_indices[0];
            }
        }
        KeyCode::Char('k') | KeyCode::Up => {
            if let Some(current_pos) = relevant_indices.iter().position(|&i| i == app.selected_menu_item) {
                if current_pos > 0 {
                    app.selected_menu_item = relevant_indices[current_pos - 1];
                }
            } else if !relevant_indices.is_empty() {
                app.selected_menu_item = relevant_indices[0];
            }
        }
        KeyCode::Char(' ') | KeyCode::Enter => {
            app.toggle_pci_device(app.selected_menu_item);
        }
        KeyCode::Char('g') | KeyCode::Char('G') if gpu_enabled => {
            // Auto-select current GPU and its audio pair
            if let Some(device) = app.pci_devices.get(app.selected_menu_item) {
                if device.is_gpu() && !device.is_boot_vga {
                    app.auto_select_gpu(app.selected_menu_item);
                }
            }
        }
        KeyCode::Char('s') | KeyCode::Char('S') => {
            // Save PCI passthrough configuration
            let save_result = save_pci_passthrough(app);
            match save_result {
                Ok(count) => {
                    let mut status_msg = if count > 0 {
                        format!("Saved {} PCI device(s) to launch.sh", count)
                    } else {
                        "Cleared PCI passthrough from launch.sh".to_string()
                    };
                    // Reload script
                    app.reload_selected_vm_script();

                    // Regenerate single-GPU scripts if they exist
                    if let Some(vm) = app.selected_vm() {
                        if crate::hardware::scripts_exist(&vm.path) {
                            // Try with in-memory config first, fall back to saved config
                            let regen_result = if let Some(config) = app.single_gpu_config.as_ref() {
                                crate::vm::single_gpu_scripts::regenerate_if_exists(vm, config)
                            } else {
                                crate::vm::single_gpu_scripts::regenerate_from_saved_config(vm)
                            };

                            match regen_result {
                                Ok(true) => {
                                    status_msg.push_str("; single-GPU scripts regenerated");
                                }
                                Ok(false) => {} // Scripts don't exist, nothing to regenerate
                                Err(e) => {
                                    status_msg.push_str(&format!("; warning: failed to regenerate single-GPU scripts: {}", e));
                                }
                            }
                        }
                    }

                    app.set_status(status_msg);
                }
                Err(e) => {
                    app.set_status(format!("Error saving PCI config: {}", e));
                }
            }
        }
        KeyCode::Char('p') | KeyCode::Char('P') if gpu_enabled => {
            // Refresh and show GPU prerequisites
            app.multi_gpu_status = Some(crate::hardware::check_multi_gpu_passthrough_status());
            // We'll need a separate screen state for prerequisites
            // For now, just refresh the status
            app.set_status("GPU prerequisites refreshed");
        }
        _ => {}
    }

    Ok(())
}

/// Save PCI passthrough configuration to the VM's launch.sh
fn save_pci_passthrough(app: &App) -> anyhow::Result<usize> {
    let vm = app
        .selected_vm()
        .ok_or_else(|| anyhow::anyhow!("No VM selected"))?;

    let devices: Vec<&PciDevice> = app
        .selected_pci_devices
        .iter()
        .filter_map(|&i| app.pci_devices.get(i))
        .collect();

    let count = devices.len();

    // Read current launch.sh
    let script_path = &vm.launch_script;
    let content = std::fs::read_to_string(script_path)?;

    // Remove existing PCI passthrough section
    let content = remove_pci_section(&content);

    // Generate new PCI passthrough section
    let pci_section = generate_pci_section(&devices);

    // Insert the new section
    let new_content = insert_pci_section(&content, &pci_section);

    // Write back
    std::fs::write(script_path, new_content)?;

    Ok(count)
}

// Markers for PCI passthrough section in launch.sh
const PCI_MARKER_START: &str = "# >>> PCI Passthrough (managed by vm-curator) >>>";
const PCI_MARKER_END: &str = "# <<< PCI Passthrough <<<";

fn remove_pci_section(content: &str) -> String {
    let mut result = String::new();
    let mut in_pci_section = false;

    for line in content.lines() {
        if line.trim() == PCI_MARKER_START {
            in_pci_section = true;
            continue;
        }
        if line.trim() == PCI_MARKER_END {
            in_pci_section = false;
            continue;
        }
        if !in_pci_section {
            // Also remove any $PCI_PASSTHROUGH_ARGS references
            let cleaned_line = line
                .replace(" $PCI_PASSTHROUGH_ARGS", "")
                .replace("$PCI_PASSTHROUGH_ARGS ", "")
                .replace("$PCI_PASSTHROUGH_ARGS", "");
            result.push_str(&cleaned_line);
            result.push('\n');
        }
    }

    while result.ends_with("\n\n") {
        result.pop();
    }

    result
}

fn generate_pci_section(devices: &[&PciDevice]) -> String {
    if devices.is_empty() {
        return String::new();
    }

    let mut section = String::new();
    section.push_str(PCI_MARKER_START);
    section.push('\n');

    // Generate the passthrough args
    let args = crate::hardware::generate_passthrough_args(
        &devices.iter().map(|d| (*d).clone()).collect::<Vec<_>>()
    );

    section.push_str("PCI_PASSTHROUGH_ARGS=\"");
    section.push_str(&args.join(" "));
    section.push_str("\"\n");
    section.push_str(PCI_MARKER_END);
    section.push('\n');

    section
}

fn insert_pci_section(content: &str, pci_section: &str) -> String {
    crate::vm::lifecycle::insert_args_section(content, pci_section, "$PCI_PASSTHROUGH_ARGS")
}