Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 36 additions & 20 deletions crates/kit/src/libvirt/domain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ pub struct DomainBuilder {
disk_path: Option<String>,
transient_disk: bool, // Use transient disk with temporary overlay
network: Option<String>,
vnc_port: Option<u16>,
graphical_console: bool,
kernel_args: Option<String>,
metadata: HashMap<String, String>,
qemu_args: Vec<String>,
Expand Down Expand Up @@ -76,7 +76,7 @@ impl DomainBuilder {
disk_path: None,
transient_disk: false,
network: None,
vnc_port: None,
graphical_console: false,
kernel_args: None,
metadata: HashMap::new(),
qemu_args: Vec::new(),
Expand Down Expand Up @@ -129,10 +129,9 @@ impl DomainBuilder {
self
}

/// Enable VNC on specified port
#[allow(dead_code)]
pub fn with_vnc(mut self, port: u16) -> Self {
self.vnc_port = Some(port);
/// Enable graphical console (SPICE) for virt-manager access
pub fn with_graphical_console(mut self) -> Self {
self.graphical_console = true;
self
}

Expand Down Expand Up @@ -474,19 +473,23 @@ impl DomainBuilder {
}
}

// VNC graphics if enabled
if let Some(vnc_port) = self.vnc_port {
// Graphical console (SPICE) for virt-manager access
if self.graphical_console {
writer.start_element("graphics", &[("type", "spice"), ("autoport", "yes")])?;
writer.write_empty_element("listen", &[("type", "address")])?;
writer.end_element("graphics")?;
writer.start_element("video", &[])?;
writer.write_empty_element(
"graphics",
&[
("type", "vnc"),
("port", &vnc_port.to_string()),
("listen", "127.0.0.1"),
],
"model",
&[("type", "virtio"), ("heads", "1"), ("primary", "yes")],
)?;
writer.start_element("video", &[])?;
writer.write_empty_element("model", &[("type", "vga")])?;
writer.end_element("video")?;
writer.start_element("channel", &[("type", "spicevmc")])?;
writer.write_empty_element(
"target",
&[("type", "virtio"), ("name", "com.redhat.spice.0")],
)?;
writer.end_element("channel")?;
}

// Virtiofs filesystems
Expand Down Expand Up @@ -633,15 +636,28 @@ mod tests {
}

#[test]
fn test_vnc_configuration() {
fn test_graphical_console_configuration() {
// Test with graphical console enabled
let xml = DomainBuilder::new()
.with_name("test")
.with_vnc(5901)
.with_graphical_console()
.build_xml()
.unwrap();

assert!(xml.contains("graphics type=\"spice\" autoport=\"yes\""));
assert!(xml.contains("model type=\"virtio\" heads=\"1\" primary=\"yes\""));
assert!(xml.contains("channel type=\"spicevmc\""));
assert!(xml.contains("target type=\"virtio\" name=\"com.redhat.spice.0\""));

// Test without graphical console (default)
let xml_no_graphics = DomainBuilder::new()
.with_name("test-no-graphics")
.build_xml()
.unwrap();

assert!(xml.contains("graphics type=\"vnc\" port=\"5901\""));
assert!(xml.contains("model type=\"vga\""));
assert!(!xml_no_graphics.contains("<graphics"));
assert!(!xml_no_graphics.contains("<video"));
assert!(!xml_no_graphics.contains("spicevmc"));
}

#[test]
Expand Down
13 changes: 13 additions & 0 deletions crates/kit/src/libvirt/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,10 @@ pub struct LibvirtRunOpts {
#[clap(long)]
pub label: Vec<String>,

/// Enable graphical console (SPICE) for virt-manager access
#[clap(long)]
pub graphical_console: bool,

/// Create a transient VM that disappears on shutdown/reboot
#[clap(long)]
pub transient: bool,
Expand Down Expand Up @@ -473,6 +477,12 @@ pub fn run(global_opts: &crate::libvirt::LibvirtOptions, mut opts: LibvirtRunOpt
opts.install.target_transport = Some(UPDATE_FROM_HOST_TRANSPORT.to_owned());
}

// Add console=tty1 kernel argument for graphical console support
if opts.graphical_console {
opts.install.karg.push("console=tty1".to_string());
debug!("Added console=tty1 kernel argument for graphical console");
}

// Add Ignition kernel argument to install options if Ignition config is specified
// This ensures the kernel arg is baked into the installed system's GRUB configuration
if opts.ignition_config.is_some() {
Expand Down Expand Up @@ -1193,6 +1203,9 @@ fn create_libvirt_domain_from_disk(
domain_builder =
domain_builder.with_firmware_log(crate::libvirt::domain::FirmwareLogOutput::Console);
}
if opts.graphical_console {
domain_builder = domain_builder.with_graphical_console();
}
domain_builder = domain_builder
.with_metadata("bootc:source-image", &opts.image)
.with_metadata("bootc:memory-mb", &memory.to_string())
Expand Down
Loading