MCPcopy Create free account
hub / github.com/cloud-hypervisor/cloud-hypervisor / disk_new

Method disk_new

cloud-hypervisor/tests/integration.rs:9682–9774  ·  view source on GitHub ↗

TODO Cleanup image file explicitly after test, if there's some space issues.

(&self, fs: u8, sz: usize)

Source from the content-addressed store, hash-verified

9680
9681 // TODO Cleanup image file explicitly after test, if there's some space issues.
9682 fn disk_new(&self, fs: u8, sz: usize) -> String {
9683 let mut guard = NEXT_DISK_ID.lock().unwrap();
9684 let id = *guard;
9685 *guard = id + 1;
9686
9687 let img = PathBuf::from(format!("/tmp/test-hotplug-{id}.raw"));
9688 let _ = fs::remove_file(&img);
9689
9690 // Create an image file
9691 let out = Command::new("qemu-img")
9692 .args([
9693 "create",
9694 "-f",
9695 "raw",
9696 img.to_str().unwrap(),
9697 format!("{sz}m").as_str(),
9698 ])
9699 .output()
9700 .expect("qemu-img command failed")
9701 .stdout;
9702 println!("{out:?}");
9703
9704 // Associate image to a loop device
9705 let out = Command::new("losetup")
9706 .args(["--show", "-f", img.to_str().unwrap()])
9707 .output()
9708 .expect("failed to create loop device")
9709 .stdout;
9710 let _tmp = String::from_utf8_lossy(&out);
9711 let loop_dev = _tmp.trim();
9712 println!("{out:?}");
9713
9714 // Create a partition table
9715 // echo 'type=7' | sudo sfdisk "${LOOP}"
9716 let mut child = Command::new("sfdisk")
9717 .args([loop_dev])
9718 .stdin(Stdio::piped())
9719 .spawn()
9720 .unwrap();
9721 let stdin = child.stdin.as_mut().expect("failed to open stdin");
9722 stdin
9723 .write_all("type=7".as_bytes())
9724 .expect("failed to write stdin");
9725 let out = child.wait_with_output().expect("sfdisk failed").stdout;
9726 println!("{out:?}");
9727
9728 // Disengage the loop device
9729 let out = Command::new("losetup")
9730 .args(["-d", loop_dev])
9731 .output()
9732 .expect("loop device not found")
9733 .stdout;
9734 println!("{out:?}");
9735
9736 // Re-associate loop device pointing to the partition only
9737 let out = Command::new("losetup")
9738 .args([
9739 "--show",

Calls 4

newFunction · 0.85
argsMethod · 0.80
spawnMethod · 0.80
as_bytesMethod · 0.80

Tested by

no test coverage detected