Skip to content

Commit cfd22dd

Browse files
committed
Update linux-loader to 0.10.0
Update to align to the new Cmdline API and kernel image loader. While at it, update the load_kernel() tests as well. Signed-off-by: Alvise Rigo <a.rigo@virtualopensystems.com> Reviewed-by: Timos Ampelikiotis <t.ampelikiotis@virtualopensystems.com>
1 parent a3772d9 commit cfd22dd

File tree

10 files changed

+61
-29
lines changed

10 files changed

+61
-29
lines changed

Cargo.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,6 @@ lto = true
2121
panic = "abort"
2222

2323
[patch.crates-io]
24-
# TODO: Using this patch until a version > 4.0 gets published.
25-
linux-loader = { git = "https://github.com/rust-vmm/linux-loader.git", rev = "9a9f071" }
24+
# TODO: Update with https://github.com/rust-vmm/linux-loader.git hash of commit
25+
# "add as_string() to the Cmdline crate"
26+
linux-loader = { git = "https://github.com/vitamin-v-software/linux-loader.git", rev = "76aefef" }

src/api/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@ clap = "3.2.17"
1010
vmm = { path = "../vmm" }
1111

1212
[dev-dependencies]
13-
linux-loader = "0.4.0"
13+
linux-loader = "0.10.0"

src/api/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ mod tests {
211211
])
212212
.is_err());
213213

214-
let mut foo_cmdline = Cmdline::new(4096);
214+
let mut foo_cmdline = Cmdline::new(4096).unwrap();
215215
foo_cmdline.insert_str("\"foo=bar bar=foo\"").unwrap();
216216

217217
// OK.

src/devices/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ license = "Apache-2.0 OR BSD-3-Clause"
99
event-manager = { version = "0.3.0", features = ["remote_endpoint"] }
1010
kvm-ioctls = "0.13.0"
1111
libc = "0.2.76"
12-
linux-loader = "0.4.0"
12+
linux-loader = "0.10.0"
1313
log = "*"
1414
vm-memory = "0.13.1"
1515
vm-superio = "0.5.0"

src/devices/src/virtio/block/device.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ mod tests {
191191
assert_eq!(block.device_type(), BLOCK_DEVICE_ID);
192192

193193
assert_eq!(
194-
mock.kernel_cmdline.as_str(),
194+
mock.kernel_cmdline.as_string().unwrap(),
195195
format!(
196196
"virtio_mmio.device=4K@0x{:x}:{} root=/dev/vda ro",
197197
mock.mmio_cfg.range.base().0,

src/devices/src/virtio/mod.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ pub(crate) mod tests {
332332
mmio_mgr: IoManager::new(),
333333
mmio_cfg,
334334
// `4096` seems large enough for testing.
335-
kernel_cmdline: Cmdline::new(4096),
335+
kernel_cmdline: Cmdline::new(4096).unwrap(),
336336
}
337337
}
338338
pub fn env(&mut self) -> Env<MockMem, &mut IoManager> {
@@ -387,7 +387,7 @@ pub(crate) mod tests {
387387
assert_eq!(bus_range.size(), range.size());
388388

389389
assert_eq!(
390-
mock.kernel_cmdline.as_str(),
390+
mock.kernel_cmdline.as_string().unwrap(),
391391
format!(
392392
"virtio_mmio.device=4K@0x{:x}:{}",
393393
range.base().0,
@@ -396,7 +396,11 @@ pub(crate) mod tests {
396396
);
397397

398398
mock.env().insert_cmdline_str("ending_string").unwrap();
399-
assert!(mock.kernel_cmdline.as_str().ends_with("ending_string"));
399+
assert!(mock
400+
.kernel_cmdline
401+
.as_string()
402+
.unwrap()
403+
.ends_with("ending_string"));
400404
}
401405

402406
#[test]

src/vmm/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ event-manager = "0.3.0"
99
kvm-bindings = { version = "0.6.0", features = ["fam-wrappers"] }
1010
kvm-ioctls = "0.13.0"
1111
libc = "0.2.91"
12-
linux-loader = { version = "0.4.0", features = ["bzimage", "elf"] }
12+
linux-loader = { version = "0.10.0", features = ["bzimage", "elf"] }
1313
vm-allocator = "0.1.0"
1414
vm-memory = { version = "0.13.1", features = ["backend-mmap"] }
1515
vm-superio = "0.5.0"

src/vmm/src/config/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ pub struct KernelConfig {
147147
impl KernelConfig {
148148
/// Return the default kernel command line used by the Vmm.
149149
pub fn default_cmdline() -> Cmdline {
150-
let mut cmdline = Cmdline::new(KERNEL_CMDLINE_CAPACITY);
150+
let mut cmdline = Cmdline::new(KERNEL_CMDLINE_CAPACITY).unwrap();
151151

152152
// It's ok to use `unwrap` because the initial capacity of `cmdline` should be
153153
// sufficient to accommodate the default kernel cmdline.
@@ -181,7 +181,7 @@ impl TryFrom<&str> for KernelConfig {
181181
.map_err(ConversionError::new_kernel)?
182182
.unwrap_or_else(|| DEFAULT_KERNEL_CMDLINE.to_string());
183183

184-
let mut cmdline = Cmdline::new(KERNEL_CMDLINE_CAPACITY);
184+
let mut cmdline = Cmdline::new(KERNEL_CMDLINE_CAPACITY).unwrap();
185185
cmdline
186186
.insert_str(cmdline_str)
187187
.map_err(|_| ConversionError::new_kernel("Kernel cmdline capacity error"))?;
@@ -282,7 +282,7 @@ mod tests {
282282
// Check that additional commas in the kernel string do not cause a panic.
283283
let kernel_str = r#"path=/foo/bar,cmdline="foo=bar",kernel_load_addr=42,"#;
284284

285-
let mut foo_cmdline = Cmdline::new(128);
285+
let mut foo_cmdline = Cmdline::new(128).unwrap();
286286
foo_cmdline.insert_str("\"foo=bar\"").unwrap();
287287

288288
let expected_kernel_config = KernelConfig {

src/vmm/src/lib.rs

Lines changed: 41 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -458,12 +458,13 @@ impl Vmm {
458458

459459
// Add the kernel command line to the boot parameters.
460460
bootparams.hdr.cmd_line_ptr = CMDLINE_START as u32;
461-
bootparams.hdr.cmdline_size = self.kernel_cfg.cmdline.as_str().len() as u32 + 1;
461+
bootparams.hdr.cmdline_size =
462+
String::try_from(&self.kernel_cfg.cmdline).unwrap().len() as u32 + 1;
462463

463464
// Load the kernel command line into guest memory.
464-
let mut cmdline = Cmdline::new(4096);
465+
let mut cmdline = Cmdline::new(4096).unwrap();
465466
cmdline
466-
.insert_str(self.kernel_cfg.cmdline.as_str())
467+
.insert_str(String::try_from(&self.kernel_cfg.cmdline).unwrap())
467468
.map_err(Error::Cmdline)?;
468469

469470
load_cmdline(
@@ -724,7 +725,7 @@ impl Vmm {
724725
let cmdline = &self.kernel_cfg.cmdline;
725726
let fdt = self
726727
.fdt_builder
727-
.with_cmdline(cmdline.as_str().to_string())
728+
.with_cmdline(String::try_from(&cmdline).unwrap())
728729
.with_num_vcpus(self.num_vcpus.try_into().unwrap())
729730
.with_mem_size(mem_size)
730731
.create_fdt()
@@ -750,6 +751,7 @@ mod tests {
750751
#[cfg(target_arch = "x86_64")]
751752
use std::path::Path;
752753
use std::path::PathBuf;
754+
use std::fs::write;
753755
#[cfg(target_arch = "x86_64")]
754756
use vm_memory::{
755757
bytes::ByteValued,
@@ -956,20 +958,38 @@ mod tests {
956958
matches!(vmm.load_kernel().unwrap_err(), Error::IO(e) if e.kind() == ErrorKind::NotFound)
957959
);
958960

959-
// Test case: kernel image is invalid.
961+
// Test case: kernel image is invalid. This test has two flavors. In the first
962+
// we try to load an empty file, in the second a file which has all zeros.
960963
let mut vmm_config = default_vmm_config();
961964
let temp_file = TempFile::new().unwrap();
962965
vmm_config.kernel_config.path = PathBuf::from(temp_file.as_path());
963966
let mut vmm = mock_vmm(vmm_config);
964967

965968
let err = vmm.load_kernel().unwrap_err();
969+
#[cfg(target_arch = "x86_64")]
970+
assert!(matches!(
971+
err,
972+
Error::KernelLoad(loader::Error::Elf(loader::elf::Error::ReadElfHeader))
973+
));
974+
#[cfg(target_arch = "aarch64")]
975+
assert!(matches!(
976+
err,
977+
Error::KernelLoad(loader::Error::Pe(loader::pe::Error::ReadImageHeader))
978+
));
979+
980+
let temp_file_path = PathBuf::from(temp_file.as_path());
981+
let buffer: Vec<u8> = vec![0_u8; 1024];
982+
write(temp_file_path, buffer).unwrap();
983+
let err = vmm.load_kernel().unwrap_err();
984+
966985
#[cfg(target_arch = "x86_64")]
967986
assert!(matches!(
968987
err,
969988
Error::KernelLoad(loader::Error::Bzimage(
970989
loader::bzimage::Error::InvalidBzImage
971990
))
972991
));
992+
973993
#[cfg(target_arch = "aarch64")]
974994
assert!(matches!(
975995
err,
@@ -1011,15 +1031,18 @@ mod tests {
10111031
let mut vmm_config = default_vmm_config();
10121032
vmm_config.kernel_config.path = default_elf_path();
10131033
let mut vmm = mock_vmm(vmm_config);
1014-
assert_eq!(vmm.kernel_cfg.cmdline.as_str(), DEFAULT_KERNEL_CMDLINE);
1034+
assert_eq!(
1035+
String::try_from(&vmm.kernel_cfg.cmdline).unwrap(),
1036+
DEFAULT_KERNEL_CMDLINE
1037+
);
10151038
vmm.add_serial_console().unwrap();
10161039
#[cfg(target_arch = "x86_64")]
1017-
assert!(vmm.kernel_cfg.cmdline.as_str().contains("console=ttyS0"));
1040+
assert!(String::try_from(&vmm.kernel_cfg.cmdline)
1041+
.unwrap()
1042+
.contains("console=ttyS0"));
10181043
#[cfg(target_arch = "aarch64")]
1019-
assert!(vmm
1020-
.kernel_cfg
1021-
.cmdline
1022-
.as_str()
1044+
assert!(String::try_from(&vmm.kernel_cfg.cmdline)
1045+
.unwrap()
10231046
.contains("earlycon=uart,mmio"));
10241047
}
10251048

@@ -1118,7 +1141,9 @@ mod tests {
11181141
assert_eq!(vmm.block_devices.len(), 1);
11191142
#[cfg(target_arch = "aarch64")]
11201143
assert_eq!(vmm.fdt_builder.virtio_device_len(), 1);
1121-
assert!(vmm.kernel_cfg.cmdline.as_str().contains("virtio"));
1144+
assert!(String::try_from(&vmm.kernel_cfg.cmdline)
1145+
.unwrap()
1146+
.contains("virtio"));
11221147

11231148
let invalid_block_config = BlockConfig {
11241149
// Let's create the tempfile directly here so that it gets out of scope immediately
@@ -1151,7 +1176,9 @@ mod tests {
11511176
assert_eq!(vmm.net_devices.len(), 1);
11521177
#[cfg(target_arch = "aarch64")]
11531178
assert_eq!(vmm.fdt_builder.virtio_device_len(), 1);
1154-
assert!(vmm.kernel_cfg.cmdline.as_str().contains("virtio"));
1179+
assert!(String::try_from(&vmm.kernel_cfg.cmdline)
1180+
.unwrap()
1181+
.contains("virtio"));
11551182
}
11561183
}
11571184

@@ -1172,7 +1199,7 @@ mod tests {
11721199
let cmdline = &vmm.kernel_cfg.cmdline;
11731200
let fdt = vmm
11741201
.fdt_builder
1175-
.with_cmdline(cmdline.as_str().to_string())
1202+
.with_cmdline(String::try_from(&cmdline).unwrap())
11761203
.with_num_vcpus(vmm.num_vcpus.try_into().unwrap())
11771204
.with_mem_size(mem_size)
11781205
.with_serial_console(0x40000000, 0x1000)

0 commit comments

Comments
 (0)