Skip to content

Commit eae835f

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 09866dc commit eae835f

File tree

10 files changed

+50
-26
lines changed

10 files changed

+50
-26
lines changed

Cargo.lock

Lines changed: 1 addition & 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 = { version = "0.10.0", path = "../linux-loader" }

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: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -458,12 +458,12 @@ 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 = self.kernel_cfg.cmdline.as_string().unwrap().len() as u32 + 1;
462462

463463
// Load the kernel command line into guest memory.
464-
let mut cmdline = Cmdline::new(4096);
464+
let mut cmdline = Cmdline::new(4096).unwrap();
465465
cmdline
466-
.insert_str(self.kernel_cfg.cmdline.as_str())
466+
.insert_str(self.kernel_cfg.cmdline.as_string().unwrap())
467467
.map_err(Error::Cmdline)?;
468468

469469
load_cmdline(
@@ -724,7 +724,7 @@ impl Vmm {
724724
let cmdline = &self.kernel_cfg.cmdline;
725725
let fdt = self
726726
.fdt_builder
727-
.with_cmdline(cmdline.as_str().to_string())
727+
.with_cmdline(cmdline.as_string().unwrap())
728728
.with_num_vcpus(self.num_vcpus.try_into().unwrap())
729729
.with_mem_size(mem_size)
730730
.create_fdt()
@@ -750,6 +750,7 @@ mod tests {
750750
#[cfg(target_arch = "x86_64")]
751751
use std::path::Path;
752752
use std::path::PathBuf;
753+
use std::fs::write;
753754
#[cfg(target_arch = "x86_64")]
754755
use vm_memory::{
755756
bytes::ByteValued,
@@ -956,20 +957,38 @@ mod tests {
956957
matches!(vmm.load_kernel().unwrap_err(), Error::IO(e) if e.kind() == ErrorKind::NotFound)
957958
);
958959

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

965967
let err = vmm.load_kernel().unwrap_err();
968+
#[cfg(target_arch = "x86_64")]
969+
assert!(matches!(
970+
err,
971+
Error::KernelLoad(loader::Error::Elf(loader::elf::Error::ReadElfHeader))
972+
));
973+
#[cfg(target_arch = "aarch64")]
974+
assert!(matches!(
975+
err,
976+
Error::KernelLoad(loader::Error::Pe(loader::pe::Error::ReadImageHeader))
977+
));
978+
979+
let temp_file_path = PathBuf::from(temp_file.as_path());
980+
let buffer: Vec<u8> = vec![0_u8; 1024];
981+
write(temp_file_path, buffer).unwrap();
982+
let err = vmm.load_kernel().unwrap_err();
983+
966984
#[cfg(target_arch = "x86_64")]
967985
assert!(matches!(
968986
err,
969987
Error::KernelLoad(loader::Error::Bzimage(
970988
loader::bzimage::Error::InvalidBzImage
971989
))
972990
));
991+
973992
#[cfg(target_arch = "aarch64")]
974993
assert!(matches!(
975994
err,
@@ -1011,15 +1030,16 @@ mod tests {
10111030
let mut vmm_config = default_vmm_config();
10121031
vmm_config.kernel_config.path = default_elf_path();
10131032
let mut vmm = mock_vmm(vmm_config);
1014-
assert_eq!(vmm.kernel_cfg.cmdline.as_str(), DEFAULT_KERNEL_CMDLINE);
1033+
assert_eq!(vmm.kernel_cfg.cmdline.as_string().unwrap(), DEFAULT_KERNEL_CMDLINE);
10151034
vmm.add_serial_console().unwrap();
10161035
#[cfg(target_arch = "x86_64")]
1017-
assert!(vmm.kernel_cfg.cmdline.as_str().contains("console=ttyS0"));
1036+
assert!(vmm.kernel_cfg.cmdline.as_string().unwrap().contains("console=ttyS0"));
10181037
#[cfg(target_arch = "aarch64")]
10191038
assert!(vmm
10201039
.kernel_cfg
10211040
.cmdline
1022-
.as_str()
1041+
.as_string()
1042+
.unwrap()
10231043
.contains("earlycon=uart,mmio"));
10241044
}
10251045

@@ -1118,7 +1138,7 @@ mod tests {
11181138
assert_eq!(vmm.block_devices.len(), 1);
11191139
#[cfg(target_arch = "aarch64")]
11201140
assert_eq!(vmm.fdt_builder.virtio_device_len(), 1);
1121-
assert!(vmm.kernel_cfg.cmdline.as_str().contains("virtio"));
1141+
assert!(vmm.kernel_cfg.cmdline.as_string().unwrap().contains("virtio"));
11221142

11231143
let invalid_block_config = BlockConfig {
11241144
// Let's create the tempfile directly here so that it gets out of scope immediately
@@ -1151,7 +1171,7 @@ mod tests {
11511171
assert_eq!(vmm.net_devices.len(), 1);
11521172
#[cfg(target_arch = "aarch64")]
11531173
assert_eq!(vmm.fdt_builder.virtio_device_len(), 1);
1154-
assert!(vmm.kernel_cfg.cmdline.as_str().contains("virtio"));
1174+
assert!(vmm.kernel_cfg.cmdline.as_string().unwrap().contains("virtio"));
11551175
}
11561176
}
11571177

@@ -1172,7 +1192,7 @@ mod tests {
11721192
let cmdline = &vmm.kernel_cfg.cmdline;
11731193
let fdt = vmm
11741194
.fdt_builder
1175-
.with_cmdline(cmdline.as_str().to_string())
1195+
.with_cmdline(cmdline.as_string().unwrap().to_string())
11761196
.with_num_vcpus(vmm.num_vcpus.try_into().unwrap())
11771197
.with_mem_size(mem_size)
11781198
.with_serial_console(0x40000000, 0x1000)

0 commit comments

Comments
 (0)