2022-09-13 17:55:17 +00:00
|
|
|
// Copyright 2022 The ChromiumOS Authors
|
2022-08-09 19:37:16 +00:00
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
|
|
|
//! Testing virtio-block.
|
|
|
|
|
|
|
|
pub mod fixture;
|
|
|
|
|
2022-10-07 21:10:47 +00:00
|
|
|
use std::env;
|
2022-08-09 19:37:16 +00:00
|
|
|
use std::process::Command;
|
|
|
|
|
|
|
|
use fixture::Config;
|
|
|
|
use fixture::TestVm;
|
|
|
|
use tempfile::NamedTempFile;
|
|
|
|
|
2022-10-14 02:25:30 +00:00
|
|
|
const DEFAULT_BLOCK_SIZE: u64 = 1024 * 1024;
|
|
|
|
|
2022-08-09 19:37:16 +00:00
|
|
|
fn prepare_disk_img() -> NamedTempFile {
|
|
|
|
let mut disk = NamedTempFile::new().unwrap();
|
2022-10-14 02:25:30 +00:00
|
|
|
disk.as_file_mut().set_len(DEFAULT_BLOCK_SIZE).unwrap();
|
2022-08-09 19:37:16 +00:00
|
|
|
|
2022-10-07 21:10:47 +00:00
|
|
|
// Add /sbin and /usr/sbin to PATH since some distributions put mkfs.ext4 in one of those
|
|
|
|
// directories but don't add them to non-root PATH.
|
|
|
|
let path = env::var("PATH").unwrap();
|
|
|
|
let path = [&path, "/sbin", "/usr/sbin"].join(":");
|
|
|
|
|
2022-08-09 19:37:16 +00:00
|
|
|
// TODO(b/243127910): Use `mkfs.ext4 -d` to include test data.
|
2022-10-07 21:10:47 +00:00
|
|
|
Command::new("mkfs.ext4")
|
2022-08-09 19:37:16 +00:00
|
|
|
.arg(disk.path().to_str().unwrap())
|
2022-10-07 21:10:47 +00:00
|
|
|
.env("PATH", path)
|
2022-08-09 19:37:16 +00:00
|
|
|
.output()
|
|
|
|
.expect("failed to execute process");
|
|
|
|
disk
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(b/243127498): Add tests for write and sync operations.
|
|
|
|
#[test]
|
|
|
|
fn mount_block() {
|
|
|
|
let disk = prepare_disk_img();
|
2022-10-26 09:13:38 +00:00
|
|
|
let disk_path = disk.path().to_str().unwrap();
|
2022-08-09 19:37:16 +00:00
|
|
|
println!("disk={disk_path}");
|
|
|
|
|
2022-10-26 09:13:38 +00:00
|
|
|
let config = Config::new().extra_args(vec!["--block".to_string(), format!("{},ro", disk_path)]);
|
2022-08-09 19:37:16 +00:00
|
|
|
let mut vm = TestVm::new(config).unwrap();
|
|
|
|
assert_eq!(
|
|
|
|
vm.exec_in_guest("mount -t ext4 /dev/vdb /mnt && echo 42")
|
|
|
|
.unwrap()
|
|
|
|
.trim(),
|
|
|
|
"42"
|
|
|
|
);
|
|
|
|
}
|
2022-10-14 02:25:30 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn resize() {
|
|
|
|
let disk = prepare_disk_img();
|
|
|
|
let disk_path = disk.path().to_str().unwrap().to_string();
|
|
|
|
println!("disk={disk_path}");
|
|
|
|
|
2022-10-26 09:13:38 +00:00
|
|
|
let config = Config::new().extra_args(vec!["--block".to_string(), disk_path]);
|
2022-10-14 02:25:30 +00:00
|
|
|
let mut vm = TestVm::new(config).unwrap();
|
|
|
|
|
|
|
|
// Check the initial block device size.
|
|
|
|
assert_eq!(
|
|
|
|
vm.exec_in_guest("blockdev --getsize64 /dev/vdb")
|
|
|
|
.unwrap()
|
|
|
|
.trim()
|
|
|
|
.parse::<u64>()
|
|
|
|
.unwrap(),
|
|
|
|
DEFAULT_BLOCK_SIZE
|
|
|
|
);
|
|
|
|
|
|
|
|
let new_size = DEFAULT_BLOCK_SIZE * 2;
|
|
|
|
|
|
|
|
// The index of the disk to resize.
|
|
|
|
let disk_index = 1;
|
|
|
|
|
|
|
|
vm.disk(vec![
|
|
|
|
"resize".to_string(),
|
|
|
|
disk_index.to_string(),
|
|
|
|
new_size.to_string(),
|
|
|
|
])
|
|
|
|
.expect("Disk resizing command failed");
|
|
|
|
|
|
|
|
// Check the new block device size.
|
|
|
|
assert_eq!(
|
|
|
|
vm.exec_in_guest("blockdev --getsize64 /dev/vdb")
|
|
|
|
.unwrap()
|
|
|
|
.trim()
|
|
|
|
.parse::<u64>()
|
|
|
|
.unwrap(),
|
|
|
|
new_size
|
|
|
|
);
|
|
|
|
}
|