Skip to main content

vm_ch/
bootloader.rs

1use std::cell::RefCell;
2
3/// Stores the kernel, initrd, and command-line paths for booting a Linux guest.
4/// On macOS this wraps VZLinuxBootLoader; here it is a plain data holder
5/// consumed by `VirtualMachine::new()`.
6///
7/// Uses `RefCell` for interior mutability to match Darwin`s `&self` setter API.
8pub struct LinuxBootLoader {
9    pub(crate) kernel_path: String,
10    pub(crate) initrd_path: RefCell<Option<String>>,
11    pub(crate) command_line: RefCell<Option<String>>,
12}
13
14impl LinuxBootLoader {
15    pub fn new(kernel_path: &str, initrd_path: &str, command_line: &str) -> Self {
16        let bl = Self::new_with_kernel(kernel_path);
17        bl.set_initrd(initrd_path);
18        bl.set_command_line(command_line);
19        bl
20    }
21
22    pub fn new_with_kernel(kernel_path: &str) -> Self {
23        LinuxBootLoader {
24            kernel_path: kernel_path.to_string(),
25            initrd_path: RefCell::new(None),
26            command_line: RefCell::new(None),
27        }
28    }
29
30    pub fn set_initrd(&self, initrd_path: &str) {
31        *self.initrd_path.borrow_mut() = Some(initrd_path.to_string());
32    }
33
34    /// cloud-hypervisor attaches a virtio-console, so `console=hvc0` from the
35    /// caller is kept as-is.
36    pub fn set_command_line(&self, command_line: &str) {
37        *self.command_line.borrow_mut() = Some(command_line.to_string());
38    }
39}