vm-curator 0.4.2

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
//! Single GPU Passthrough Setup Screen
//!
//! Provides UI for configuring single GPU passthrough, including:
//! - GPU and IOMMU group information display
//! - Script generation controls
//! - System support status
//!
//! Note: Looking Glass is NOT used for single-GPU passthrough because the display
//! goes directly to physical monitors connected to the passed-through GPU.

use crossterm::event::{KeyCode, KeyEvent};
use ratatui::{
    layout::{Alignment, Constraint, Direction, Layout, Rect},
    prelude::*,
    style::{Color, Modifier, Style},
    widgets::{Block, Borders, Clear, Paragraph, Wrap},
};

use crate::app::{App, Screen};
use crate::hardware::single_gpu::{check_single_gpu_support, is_running_from_tty};
use crate::hardware::{scripts_exist, SingleGpuConfig};
use crate::vm::single_gpu_scripts;

/// Fields that can be focused in the setup screen
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SetupField {
    GenerateScripts,
    DeleteScripts,
}

impl SetupField {
    fn all() -> &'static [SetupField] {
        &[
            SetupField::GenerateScripts,
            SetupField::DeleteScripts,
        ]
    }
}

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

    // Calculate dialog size
    let dialog_width = 72.min(area.width.saturating_sub(4));
    let dialog_height = 26.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(" Single GPU Passthrough Setup ")
        .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: System support, GPU info, separator, scripts, help
    let chunks = Layout::default()
        .direction(Direction::Vertical)
        .margin(1)
        .constraints([
            Constraint::Length(3),  // System support status
            Constraint::Length(6),  // GPU info
            Constraint::Length(1),  // Separator
            Constraint::Length(6),  // Scripts info
            Constraint::Min(1),     // Spacer
            Constraint::Length(2),  // Help
        ])
        .split(inner);

    // Render system support status
    render_system_support(app, frame, chunks[0]);

    // Render GPU info
    render_gpu_info(app, frame, chunks[1]);

    // Separator
    let sep1 = Paragraph::new("─".repeat(chunks[2].width as usize))
        .style(Style::default().fg(Color::DarkGray));
    frame.render_widget(sep1, chunks[2]);

    // Scripts info
    render_scripts_info(app, frame, chunks[3]);

    // Help
    render_help(app, frame, chunks[5]);
}

/// Render system support status
fn render_system_support(_app: &App, frame: &mut Frame, area: Rect) {
    let support = check_single_gpu_support();

    let mut lines = Vec::new();

    // Status line
    let (status_text, status_color) = if support.is_supported() {
        ("System Ready", Color::Green)
    } else {
        ("System Not Ready", Color::Red)
    };

    lines.push(Line::from(vec![
        Span::styled("Status: ", Style::default().fg(Color::White)),
        Span::styled(status_text, Style::default().fg(status_color).add_modifier(Modifier::BOLD)),
    ]));

    // Details
    let mut details = Vec::new();
    details.push(format!(
        "IOMMU: {}",
        if support.iommu_enabled { "Enabled" } else { "Disabled" }
    ));
    details.push(format!(
        "VFIO: {}",
        if support.vfio_available { "Available" } else { "Not Available" }
    ));

    lines.push(Line::styled(
        details.join("  |  "),
        Style::default().fg(Color::DarkGray),
    ));

    // Warning if running from graphical terminal
    if !is_running_from_tty() {
        lines.push(Line::styled(
            "Note: Scripts must be run from TTY, not graphical terminal",
            Style::default().fg(Color::Yellow),
        ));
    }

    let para = Paragraph::new(lines);
    frame.render_widget(para, area);
}

/// Render GPU information section
fn render_gpu_info(app: &App, frame: &mut Frame, area: Rect) {
    let single_gpu_config = app.single_gpu_config.as_ref();

    let mut lines = Vec::new();

    if let Some(config) = single_gpu_config {
        // GPU name
        let gpu_name = if !config.gpu.device_name.is_empty() {
            format!("{} {}", config.gpu.short_vendor(), config.gpu.device_name)
        } else {
            format!("{} GPU", config.gpu.short_vendor())
        };
        lines.push(Line::from(vec![
            Span::styled("GPU: ", Style::default().fg(Color::White)),
            Span::styled(
                format!("{} [{}]", gpu_name, config.gpu.address),
                Style::default().fg(Color::Cyan),
            ),
        ]));

        // Audio device (if present)
        if let Some(ref audio) = config.audio {
            lines.push(Line::from(vec![
                Span::styled("Audio: ", Style::default().fg(Color::White)),
                Span::styled(
                    format!("{} [{}]", audio.device_name, audio.address),
                    Style::default().fg(Color::Magenta),
                ),
            ]));
        } else {
            lines.push(Line::from(vec![
                Span::styled("Audio: ", Style::default().fg(Color::White)),
                Span::styled("None detected", Style::default().fg(Color::DarkGray)),
            ]));
        }

        // IOMMU group - use the stored iommu_group_devices
        let iommu_info = config
            .gpu
            .iommu_group
            .map(|g| {
                let device_count = config.iommu_group_devices.len();
                format!(
                    "{} ({} device{})",
                    g,
                    device_count,
                    if device_count == 1 { "" } else { "s" }
                )
            })
            .unwrap_or_else(|| "None".to_string());
        lines.push(Line::from(vec![
            Span::styled("IOMMU Group: ", Style::default().fg(Color::White)),
            Span::styled(iommu_info, Style::default().fg(Color::Yellow)),
        ]));

        // Passthrough addresses
        let addrs = config.all_passthrough_addresses();
        lines.push(Line::from(vec![
            Span::styled("Passthrough: ", Style::default().fg(Color::White)),
            Span::styled(addrs.join(", "), Style::default().fg(Color::White)),
        ]));

        // Display manager
        lines.push(Line::from(vec![
            Span::styled("Display Manager: ", Style::default().fg(Color::White)),
            Span::styled(
                format!("{} (auto-detected)", config.display_manager.display_name()),
                Style::default().fg(Color::White),
            ),
        ]));
    } else {
        lines.push(Line::styled(
            "No GPU selected for passthrough",
            Style::default().fg(Color::Yellow),
        ));
        lines.push(Line::styled(
            "Select a boot VGA device from PCI Passthrough screen",
            Style::default().fg(Color::DarkGray),
        ));
    }

    let para = Paragraph::new(lines);
    frame.render_widget(para, area);
}

/// Render scripts information
fn render_scripts_info(app: &App, frame: &mut Frame, area: Rect) {
    let vm = app.selected_vm();
    let selected_field = app.single_gpu_selected_field;

    let scripts_present = vm.map(|v| scripts_exist(&v.path)).unwrap_or(false);

    let vm_path = vm
        .map(|v| v.path.display().to_string())
        .unwrap_or_else(|| "~/vm-space/<vm>/".to_string());

    let mut lines = vec![
        Line::styled(
            "Scripts location:",
            Style::default().fg(Color::White),
        ),
        Line::styled(format!("  {}", vm_path), Style::default().fg(Color::DarkGray)),
    ];

    if scripts_present {
        lines.push(Line::styled(
            "  Scripts exist (will be overwritten on generate)",
            Style::default().fg(Color::Green),
        ));
    } else {
        lines.push(Line::styled(
            "  No scripts generated yet",
            Style::default().fg(Color::DarkGray),
        ));
    }

    // Action buttons
    let generate_style = if selected_field == SetupField::GenerateScripts as usize {
        Style::default()
            .fg(Color::Yellow)
            .add_modifier(Modifier::BOLD)
    } else {
        Style::default().fg(Color::Green)
    };

    let delete_style = if selected_field == SetupField::DeleteScripts as usize {
        Style::default()
            .fg(Color::Yellow)
            .add_modifier(Modifier::BOLD)
    } else if scripts_present {
        Style::default().fg(Color::Red)
    } else {
        Style::default().fg(Color::DarkGray)
    };

    lines.push(Line::from(vec![
        Span::styled("[g] ", generate_style),
        Span::styled("Generate", generate_style),
        Span::raw("  "),
        Span::styled("[d] ", delete_style),
        Span::styled("Delete", delete_style),
    ]));

    let para = Paragraph::new(lines);
    frame.render_widget(para, area);
}

/// Render help text
fn render_help(_app: &App, frame: &mut Frame, area: Rect) {
    let help = Paragraph::new(
        "[g] Generate Scripts  [d] Delete Scripts  [Esc] Back",
    )
    .style(Style::default().fg(Color::DarkGray))
    .alignment(Alignment::Center);
    frame.render_widget(help, area);
}

/// Render instructions dialog (shown after generating scripts)
pub fn render_instructions(app: &App, frame: &mut Frame) {
    let area = frame.area();

    let dialog_width = 64.min(area.width.saturating_sub(4));
    let dialog_height = 20.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(" Single GPU Passthrough Launch ")
        .borders(Borders::ALL)
        .border_style(Style::default().fg(Color::Green))
        .style(Style::default().bg(Color::Black));

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

    let vm_path = app
        .selected_vm()
        .map(|v| v.path.display().to_string())
        .unwrap_or_else(|| "~/vm-space/<vm>".to_string());

    // Check if running from TTY
    let tty_warning = if !is_running_from_tty() {
        Line::styled(
            "WARNING: You are in a graphical session!",
            Style::default().fg(Color::Red).add_modifier(Modifier::BOLD),
        )
    } else {
        Line::styled(
            "You are running from TTY (good!)",
            Style::default().fg(Color::Green),
        )
    };

    let lines = vec![
        tty_warning,
        Line::raw(""),
        Line::styled(
            "Single GPU passthrough requires running from TTY.",
            Style::default().fg(Color::Yellow),
        ),
        Line::raw(""),
        Line::styled(
            "Instructions:",
            Style::default()
                .fg(Color::White)
                .add_modifier(Modifier::BOLD),
        ),
        Line::raw("1. Press Ctrl+Alt+F3 to switch to TTY3"),
        Line::raw("2. Log in with your username"),
        Line::raw("3. Run the following command:"),
        Line::raw(""),
        Line::styled(
            format!("   sudo {}/single-gpu-start.sh", vm_path),
            Style::default().fg(Color::Cyan),
        ),
        Line::raw(""),
        Line::raw("After the VM exits, your display will be restored."),
        Line::raw(""),
        Line::styled(
            "If something goes wrong, SSH in and run:",
            Style::default().fg(Color::Yellow),
        ),
        Line::styled(
            format!("   sudo {}/single-gpu-restore.sh", vm_path),
            Style::default().fg(Color::Cyan),
        ),
        Line::raw(""),
        Line::styled("[Enter/Esc] Close", Style::default().fg(Color::DarkGray)),
    ];

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

/// Handle key input for the single GPU setup screen
pub fn handle_key(app: &mut App, key: KeyEvent) -> anyhow::Result<()> {
    let field_count = SetupField::all().len();

    match key.code {
        KeyCode::Esc => {
            app.selected_menu_item = 0; // Reset for management menu
            app.pop_screen();
        }
        KeyCode::Up | KeyCode::Char('k') => {
            if app.single_gpu_selected_field > 0 {
                app.single_gpu_selected_field -= 1;
            }
        }
        KeyCode::Down | KeyCode::Char('j') => {
            if app.single_gpu_selected_field < field_count - 1 {
                app.single_gpu_selected_field += 1;
            }
        }
        KeyCode::Enter | KeyCode::Char(' ') => {
            handle_field_action(app)?;
        }
        KeyCode::Char('g') | KeyCode::Char('G') => {
            generate_scripts(app)?;
        }
        KeyCode::Char('d') | KeyCode::Char('D') => {
            delete_scripts(app)?;
        }
        _ => {}
    }

    Ok(())
}

/// Handle action for the currently selected field
fn handle_field_action(app: &mut App) -> anyhow::Result<()> {
    let field = SetupField::all().get(app.single_gpu_selected_field);

    match field {
        Some(SetupField::GenerateScripts) => {
            generate_scripts(app)?;
        }
        Some(SetupField::DeleteScripts) => {
            delete_scripts(app)?;
        }
        None => {}
    }

    Ok(())
}

/// Generate single GPU passthrough scripts
fn generate_scripts(app: &mut App) -> anyhow::Result<()> {
    let vm = app.selected_vm().cloned();
    let config = app.single_gpu_config.clone();

    // Pre-flight check
    let support = check_single_gpu_support();
    if !support.is_supported() {
        app.set_status(format!("Cannot generate: {}", support.summary()));
        return Ok(());
    }

    match (vm, config) {
        (Some(vm), Some(config)) => {
            match crate::vm::generate_single_gpu_scripts(&vm, &config) {
                Ok(scripts) => {
                    // Log generated script paths
                    let dir = scripts.start_script.parent().unwrap();
                    app.set_status(format!(
                        "Generated: {}, {} in {}",
                        scripts.start_script.file_name().unwrap().to_string_lossy(),
                        scripts.restore_script.file_name().unwrap().to_string_lossy(),
                        dir.display()
                    ));
                    // Show instructions dialog
                    app.single_gpu_show_instructions = true;
                    app.push_screen(Screen::SingleGpuInstructions);
                }
                Err(e) => {
                    app.set_status(format!("Error generating scripts: {}", e));
                }
            }
        }
        (None, _) => {
            app.set_status("No VM selected");
        }
        (_, None) => {
            app.set_status("No GPU configured for passthrough");
        }
    }

    Ok(())
}

/// Delete single GPU passthrough scripts
fn delete_scripts(app: &mut App) -> anyhow::Result<()> {
    let vm = app.selected_vm();

    if let Some(vm) = vm {
        if !scripts_exist(&vm.path) {
            app.set_status("No scripts to delete");
            return Ok(());
        }

        match single_gpu_scripts::delete_scripts(&vm.path) {
            Ok(()) => {
                app.set_status("Scripts deleted");
            }
            Err(e) => {
                app.set_status(format!("Error deleting scripts: {}", e));
            }
        }
    } else {
        app.set_status("No VM selected");
    }

    Ok(())
}

/// Initialize single GPU config for the currently selected GPU
pub fn init_single_gpu_config(app: &mut App) {
    // Check if single GPU passthrough is supported
    let support = check_single_gpu_support();

    if !support.is_supported() {
        app.set_status(support.summary());
    }

    // Find the boot VGA device
    let boot_vga = app.pci_devices.iter().find(|d| d.can_single_gpu_passthrough());

    if let Some(gpu) = boot_vga {
        let config = SingleGpuConfig::new(gpu.clone(), &app.pci_devices);
        app.single_gpu_config = Some(config);
    } else if let Some(gpu) = app.pci_devices.iter().find(|d| d.is_boot_vga) {
        // Fallback to boot VGA even if it doesn't have IOMMU
        let config = SingleGpuConfig::new(gpu.clone(), &app.pci_devices);
        app.single_gpu_config = Some(config);
    }
}

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)
}