Skip to content

Commit

Permalink
Merge pull request kuasar-io#48 from Vanient/main
Browse files Browse the repository at this point in the history
vmm:support running stratovirt sandbox in x86
  • Loading branch information
Burning1020 authored Aug 1, 2023
2 parents 052022d + 29c82fc commit 7c854c8
Show file tree
Hide file tree
Showing 6 changed files with 3,745 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ install-vmm:

ifeq ($(HYPERVISOR), stratovirt)
@install -p -m 640 bin/kuasar.initrd ${DEST_DIR}${INSTALL_DIR}/kuasar.initrd
@install -p -m 640 vmm/sandbox/config_stratovirt.toml ${DEST_DIR}${INSTALL_DIR}/config_stratovirt.toml
@install -p -m 640 vmm/sandbox/config_stratovirt_${ARCH}.toml ${DEST_DIR}${INSTALL_DIR}/config_stratovirt.toml
else
@install -p -m 640 bin/kuasar.img ${DEST_DIR}${INSTALL_DIR}/kuasar.img
@install -p -m 640 vmm/sandbox/config_clh.toml ${DEST_DIR}${INSTALL_DIR}/config_clh.toml
Expand Down
File renamed without changes.
19 changes: 19 additions & 0 deletions vmm/sandbox/config_stratovirt_x86_64.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[sandbox]
log_level = "info"

[hypervisor]
path = "/usr/bin/stratovirt"
machine_type = "q35,mem-share=on"
kernel_path = "/var/lib/kuasar/vmlinux.bin"
image_path = ""
initrd_path = "/var/lib/kuasar/kuasar.initrd"
kernel_params = "task.log_level=debug task.sharefs_type=virtiofs"
firmware= "/usr/share/edk2/ovmf/OVMF_CODE.fd"
vcpus = 1
memory_in_mb = 1024
block_device_driver = "virtio-blk"
debug = true
enable_mem_prealloc = false

[hypervisor.virtiofsd_conf]
path = "/usr/bin/vhost_user_fs"
41 changes: 35 additions & 6 deletions vmm/sandbox/src/stratovirt/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ const DEFAULT_STRATOVIRT_PATH: &str = "/usr/bin/stratovirt";
const DEFAULT_KERNEL_PARAMS: &str =
"console=hvc0 console=hvc1 iommu=off debug panic=1 pcie_ports=native";

#[cfg(target_arch = "x86_64")]
const ROOTFS_KERNEL_PARAMS: &str = " root=/dev/vda ro rootfstype=ext4";
#[cfg(target_arch = "aarch64")]
const ROOTFS_KERNEL_PARAMS: &str = " root=/dev/vda1 ro rootfstype=ext4";

#[derive(Clone, Debug, Deserialize)]
pub struct StratoVirtVMConfig {
pub path: String,
Expand Down Expand Up @@ -123,10 +128,7 @@ impl StratoVirtVMConfig {
}

if !self.common.image_path.is_empty() {
result
.kernel
.kernel_params
.push_str(" root=/dev/vda1 ro rootfstype=ext4");
result.kernel.kernel_params.push_str(ROOTFS_KERNEL_PARAMS);
}

result.global_params = vec![Global {
Expand All @@ -139,6 +141,13 @@ impl StratoVirtVMConfig {
prealloc: self.common.enable_mem_prealloc,
};

if !self.common.firmware.is_empty() {
result.firmware = Some(Firmware {
param_key: "drive".to_string(),
file: self.common.firmware.to_string(),
});
}

Ok(result)
}
}
Expand Down Expand Up @@ -208,6 +217,21 @@ pub struct Knobs {
pub prealloc: bool,
}

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct Firmware {
pub param_key: String,
pub file: String,
}

impl ToCmdLineParams for Firmware {
fn to_cmdline_params(&self, hyphen: &str) -> Vec<String> {
vec![
format!("{}{}", hyphen, self.param_key),
format!("file={},if=pflash,format=raw,unit=0,readonly=on", self.file),
]
}
}

#[derive(CmdLineParamSet, Default, Clone, Serialize, Deserialize)]
pub struct StratoVirtConfig {
pub name: String,
Expand All @@ -226,13 +250,14 @@ pub struct StratoVirtConfig {
#[param(key = "global")]
pub global_params: Vec<Global>,
pub knobs: Knobs,
pub firmware: Option<Firmware>,
}

#[cfg(test)]
mod tests {
use crate::{
param::ToCmdLineParams,
stratovirt::config::{QmpSocket, StratoVirtVMConfig},
stratovirt::config::{QmpSocket, StratoVirtVMConfig, ROOTFS_KERNEL_PARAMS},
};

#[tokio::test]
Expand Down Expand Up @@ -318,6 +343,10 @@ mod tests {
let params = stratovirt_config.to_cmdline_params("-");

println!("params: {:?}", params);
let append_params = format!(
"console=hvc0 console=hvc1 iommu=off debug panic=1 pcie_ports=native{}",
ROOTFS_KERNEL_PARAMS
);
let expected_params = vec![
"-name",
"sandbox-1",
Expand All @@ -330,7 +359,7 @@ mod tests {
"-kernel",
"/var/lib/kuasar/vmlinux.bin",
"-append",
"console=hvc0 console=hvc1 iommu=off debug panic=1 pcie_ports=native root=/dev/vda1 ro rootfstype=ext4",
&append_params,
"-smp",
"cpus=1",
"-m",
Expand Down
3 changes: 3 additions & 0 deletions vmm/sandbox/src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ pub struct HypervisorCommonConfig {
#[serde(default)]
pub kernel_params: String,
#[serde(default)]
pub firmware: String,
#[serde(default)]
pub enable_mem_prealloc: bool,
}

Expand All @@ -125,6 +127,7 @@ impl Default for HypervisorCommonConfig {
image_path: "".to_string(),
initrd_path: "".to_string(),
kernel_params: "".to_string(),
firmware: "".to_string(),
enable_mem_prealloc: false,
}
}
Expand Down
Loading

0 comments on commit 7c854c8

Please sign in to comment.