patches and low-level development discussion
 help / color / mirror / code / Atom feed
From: Alyssa Ross <hi@alyssa.is>
To: devel@spectrum-os.org
Subject: [PATCH v2 1/6] host/start-vm: support multiple block devices
Date: Mon, 10 Oct 2022 23:29:04 +0000	[thread overview]
Message-ID: <20221010232909.1953738-25-hi@alyssa.is> (raw)
In-Reply-To: <20221010232909.1953738-1-hi@alyssa.is>

This is the first step to being able to do things like having a static
base image for an application VM, and another image containing the
application to run.

When we actually use multiple images we'll need to change how we boot
so we know the kernel is booting from the right image — probably by
PARTUUID or PARTLABEL — because we can't guarantee how the kernel
orders disks inside the VM.

Signed-off-by: Alyssa Ross <hi@alyssa.is>
---
 Documentation/creating-vms.adoc         |  8 +++---
 host/start-vm/lib.rs                    | 36 ++++++++++++++++++++-----
 host/start-vm/tests/vm_command-basic.rs |  4 +--
 vm/app/catgirl/Makefile                 | 12 ++++-----
 vm/app/lynx/Makefile                    | 12 ++++-----
 vm/sys/net/Makefile                     | 12 ++++-----
 6 files changed, 55 insertions(+), 29 deletions(-)

diff --git a/Documentation/creating-vms.adoc b/Documentation/creating-vms.adoc
index d967098..6d4fde0 100644
--- a/Documentation/creating-vms.adoc
+++ b/Documentation/creating-vms.adoc
@@ -15,8 +15,9 @@ The directory can contain the following files:
 vmlinux:: An uncompressed Linux kernel image for the VM to boot.
 *Required.*
 
-rootfs.ext4:: A disk image that will be provided to the guest as a
-virtio-blk device.  *Required.*
+blk:: A directory containing disk images (with file names ending in
+".img") that will be provided to the guest as a virtio-blk device.
+Order is not guaranteed.  At least one image is *required*.
 
 providers/net:: A directory containing a file named for each VM that
 should provide networking to this VM.  The contents of these files are
@@ -33,7 +34,8 @@ appvm-lynx
 ├── providers/
 │   └── net/
 │       └── netvm
-├── rootfs.ext4
+├── blk/
+│   └── root.img
 └── vmlinux*
 ----
 
diff --git a/host/start-vm/lib.rs b/host/start-vm/lib.rs
index 1230a6e..5d43a3e 100644
--- a/host/start-vm/lib.rs
+++ b/host/start-vm/lib.rs
@@ -81,12 +81,36 @@ pub fn vm_command(dir: PathBuf, config_root: &Path) -> Result<Command, String> {
         Err(e) => return Err(format!("reading directory {:?}: {}", net_providers_dir, e)),
     }
 
-    command.arg("--disk").arg({
-        let mut disk = OsString::from("path=/ext/svc/data/");
-        disk.push(&vm_name);
-        disk.push("/rootfs.ext4,readonly=on");
-        disk
-    });
+    command.arg("--disk");
+
+    let blk_dir = config_dir.join("blk");
+    match blk_dir.read_dir() {
+        Ok(entries) => {
+            for result in entries {
+                let entry = result
+                    .map_err(|e| format!("examining directory entry: {}", e))?
+                    .path();
+
+                if entry.extension() != Some(OsStr::new("img")) {
+                    continue;
+                }
+
+                if entry.as_os_str().as_bytes().contains(&b',') {
+                    return Err(format!("illegal ',' character in path {:?}", entry));
+                }
+
+                let mut arg = OsString::from("path=");
+                arg.push(entry);
+                arg.push(",readonly=on");
+                command.arg(arg);
+            }
+        }
+        Err(e) => return Err(format!("reading directory {:?}: {}", blk_dir, e)),
+    }
+
+    if command.get_args().last() == Some(OsStr::new("--disk")) {
+        return Err("no block devices specified".to_string());
+    }
 
     command.arg("--serial").arg({
         let mut serial = OsString::from("file=/run/");
diff --git a/host/start-vm/tests/vm_command-basic.rs b/host/start-vm/tests/vm_command-basic.rs
index b2edb7c..a577a71 100644
--- a/host/start-vm/tests/vm_command-basic.rs
+++ b/host/start-vm/tests/vm_command-basic.rs
@@ -14,7 +14,7 @@ fn main() -> std::io::Result<()> {
     create_dir(&service_dir)?;
 
     let kernel_path = tmp_dir.path().join("svc/data/testvm/vmlinux");
-    let image_path = tmp_dir.path().join("svc/data/testvm/rootfs.ext4");
+    let image_path = tmp_dir.path().join("svc/data/testvm/blk/root.img");
 
     create_dir_all(kernel_path.parent().unwrap())?;
     create_dir_all(image_path.parent().unwrap())?;
@@ -43,7 +43,7 @@ fn main() -> std::io::Result<()> {
         OsStr::new("--kernel"),
         kernel_path.as_os_str(),
         OsStr::new("--disk"),
-        OsStr::new("path=/ext/svc/data/testvm/rootfs.ext4,readonly=on"),
+        &expected_disk_arg,
         OsStr::new("--serial"),
         OsStr::new("file=/run/testvm.log"),
     ];
diff --git a/vm/app/catgirl/Makefile b/vm/app/catgirl/Makefile
index a32826b..9016745 100644
--- a/vm/app/catgirl/Makefile
+++ b/vm/app/catgirl/Makefile
@@ -12,7 +12,7 @@ VMM = qemu
 HOST_FILES = host/data/appvm-catgirl/providers/net/netvm
 
 HOST_BUILD_FILES = \
-	build/host/data/appvm-catgirl/rootfs.ext4 \
+	build/host/data/appvm-catgirl/blk/root.img \
 	build/host/data/appvm-catgirl/vmlinux
 
 # We produce a directory, but that doesn't play nice with Make,
@@ -35,7 +35,7 @@ build/host/data/appvm-catgirl/vmlinux: $(VMLINUX)
 
 # tar2ext4 will leave half a filesystem behind if it's interrupted
 # half way through.
-build/host/data/appvm-catgirl/rootfs.ext4: build/rootfs.tar
+build/host/data/appvm-catgirl/blk/root.img: build/rootfs.tar
 	mkdir -p $$(dirname $@)
 	tar2ext4 -i build/rootfs.tar -o $@.tmp
 	mv $@.tmp $@
@@ -92,9 +92,9 @@ build/etc/s6-rc: $(VM_S6_RC_FILES)
 	    s6-rc-compile $@ $$dir; \
 	    exit=$$?; rm -r $$dir; exit $$exit
 
-run-qemu: build/host/data/appvm-catgirl/rootfs.ext4
+run-qemu: build/host/data/appvm-catgirl/blk/root.img
 	$(QEMU_KVM) -m 128 -cpu host -machine q35,kernel=$(KERNEL) -vga none \
-	  -drive file=build/host/data/appvm-catgirl/rootfs.ext4,if=virtio,format=raw,readonly=on \
+	  -drive file=build/host/data/appvm-catgirl/blk/root.img,if=virtio,format=raw,readonly=on \
 	  -append "console=ttyS0 root=/dev/vda" \
 	  -netdev user,id=net0 \
 	  -device virtio-net,netdev=net0,mac=0A:B3:EC:00:00:00 \
@@ -103,11 +103,11 @@ run-qemu: build/host/data/appvm-catgirl/rootfs.ext4
 	  -device virtconsole,chardev=virtiocon0
 .PHONY: run-qemu
 
-run-cloud-hypervisor: build/host/data/appvm-catgirl/rootfs.ext4
+run-cloud-hypervisor: build/host/data/appvm-catgirl/blk/root.img
 	$(CLOUD_HYPERVISOR) \
 	    --api-socket path=vmm.sock \
 	    --memory size=128M \
-	    --disk path=build/host/data/appvm-catgirl/rootfs.ext4,readonly=on \
+	    --disk path=build/host/data/appvm-catgirl/blk/root.img,readonly=on \
 	    --net tap=tap0,mac=0A:B3:EC:00:00:00 \
 	    --kernel $(KERNEL) \
 	    --cmdline "console=ttyS0 root=/dev/vda" \
diff --git a/vm/app/lynx/Makefile b/vm/app/lynx/Makefile
index c25c9ab..ff87cb8 100644
--- a/vm/app/lynx/Makefile
+++ b/vm/app/lynx/Makefile
@@ -12,7 +12,7 @@ VMM = qemu
 HOST_FILES = host/data/appvm-lynx/providers/net/netvm
 
 HOST_BUILD_FILES = \
-	build/host/data/appvm-lynx/rootfs.ext4 \
+	build/host/data/appvm-lynx/blk/root.img \
 	build/host/data/appvm-lynx/vmlinux
 
 # We produce a directory, but that doesn't play nice with Make,
@@ -35,7 +35,7 @@ build/host/data/appvm-lynx/vmlinux: $(VMLINUX)
 
 # tar2ext4 will leave half a filesystem behind if it's interrupted
 # half way through.
-build/host/data/appvm-lynx/rootfs.ext4: build/rootfs.tar
+build/host/data/appvm-lynx/blk/root.img: build/rootfs.tar
 	mkdir -p $$(dirname $@)
 	tar2ext4 -i build/rootfs.tar -o $@.tmp
 	mv $@.tmp $@
@@ -91,9 +91,9 @@ build/etc/s6-rc: $(VM_S6_RC_FILES)
 	    s6-rc-compile $@ $$dir; \
 	    exit=$$?; rm -r $$dir; exit $$exit
 
-run-qemu: build/host/data/appvm-lynx/rootfs.ext4
+run-qemu: build/host/data/appvm-lynx/blk/root.img
 	$(QEMU_KVM) -m 128 -cpu host -machine q35,kernel=$(KERNEL) -vga none \
-	  -drive file=build/host/data/appvm-lynx/rootfs.ext4,if=virtio,format=raw,readonly=on \
+	  -drive file=build/host/data/appvm-lynx/blk/root.img,if=virtio,format=raw,readonly=on \
 	  -append "console=ttyS0 root=/dev/vda" \
 	  -netdev user,id=net0 \
 	  -device virtio-net,netdev=net0,mac=0A:B3:EC:00:00:00 \
@@ -102,11 +102,11 @@ run-qemu: build/host/data/appvm-lynx/rootfs.ext4
 	  -device virtconsole,chardev=virtiocon0
 .PHONY: run-qemu
 
-run-cloud-hypervisor: build/host/data/appvm-lynx/rootfs.ext4
+run-cloud-hypervisor: build/host/data/appvm-lynx/blk/root.img
 	$(CLOUD_HYPERVISOR) \
 	    --api-socket path=vmm.sock \
 	    --memory size=128M \
-	    --disk path=build/host/data/appvm-lynx/rootfs.ext4,readonly=on \
+	    --disk path=build/host/data/appvm-lynx/blk/root.img,readonly=on \
 	    --net tap=tap0,mac=0A:B3:EC:00:00:00 \
 	    --kernel $(KERNEL) \
 	    --cmdline "console=ttyS0 root=/dev/vda" \
diff --git a/vm/sys/net/Makefile b/vm/sys/net/Makefile
index 7cb7a5f..0b5fe8f 100644
--- a/vm/sys/net/Makefile
+++ b/vm/sys/net/Makefile
@@ -10,7 +10,7 @@ CLOUD_HYPERVISOR = cloud-hypervisor
 VMM = qemu
 
 HOST_BUILD_FILES = \
-	build/host/data/netvm/rootfs.ext4 \
+	build/host/data/netvm/blk/root.img \
 	build/host/data/netvm/vmlinux
 
 # We produce a directory, but that doesn't play nice with Make,
@@ -32,7 +32,7 @@ build/host/data/netvm/vmlinux: $(VMLINUX)
 
 # tar2ext4 will leave half a filesystem behind if it's interrupted
 # half way through.
-build/host/data/netvm/rootfs.ext4: build/rootfs.tar
+build/host/data/netvm/blk/root.img: build/rootfs.tar
 	mkdir -p $$(dirname $@)
 	tar2ext4 -i build/rootfs.tar -o $@.tmp
 	mv $@.tmp $@
@@ -97,9 +97,9 @@ build/etc/s6-rc: $(VM_S6_RC_FILES)
 	    s6-rc-compile $@ $$dir; \
 	    exit=$$?; rm -r $$dir; exit $$exit
 
-run-qemu: build/host/data/netvm/rootfs.ext4
+run-qemu: build/host/data/netvm/blk/root.img
 	$(QEMU_KVM) -m 128 -cpu host -machine q35,kernel=$(KERNEL) -vga none \
-	  -drive file=build/host/data/netvm/rootfs.ext4,if=virtio,format=raw,readonly=on \
+	  -drive file=build/host/data/netvm/blk/root.img,if=virtio,format=raw,readonly=on \
 	  -append "console=ttyS0 root=/dev/vda" \
 	  -netdev user,id=net0 \
 	  -device e1000e,netdev=net0 \
@@ -110,11 +110,11 @@ run-qemu: build/host/data/netvm/rootfs.ext4
 	  -device virtconsole,chardev=virtiocon0
 .PHONY: run-qemu
 
-run-cloud-hypervisor: build/host/data/netvm/rootfs.ext4
+run-cloud-hypervisor: build/host/data/netvm/blk/root.img
 	$(CLOUD_HYPERVISOR) \
 	    --api-socket path=vmm.sock \
 	    --memory size=128M \
-	    --disk path=build/host/data/netvm/rootfs.ext4,readonly=on \
+	    --disk path=build/host/data/netvm/blk/root.img,readonly=on \
 	    --net tap=tap0 tap=tap1,mac=0A:B3:EC:80:00:00 \
 	    --kernel $(KERNEL) \
 	    --cmdline "console=ttyS0 root=/dev/vda" \
-- 
2.37.1



  parent reply	other threads:[~2022-10-10 23:33 UTC|newest]

Thread overview: 47+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-10-10 23:28 [PATCH 00/22] Implement managing VMs with Nix Alyssa Ross
2022-10-10 23:28 ` [PATCH 01/22] host/start-vm: use MAP_SHARED memory for VMs Alyssa Ross
2023-02-26 19:17   ` Alyssa Ross
2022-10-10 23:28 ` [PATCH 02/22] host/start-vm: implement shared directories Alyssa Ross
2023-02-26 19:17   ` Alyssa Ross
2022-10-10 23:28 ` [PATCH 03/22] host/rootfs: generate virtiofsd services Alyssa Ross
2023-02-26 19:17   ` Alyssa Ross
2022-10-10 23:28 ` [PATCH 04/22] Documentation: explain VM shared directories Alyssa Ross
2023-02-26 19:17   ` Alyssa Ross
2022-10-10 23:28 ` [PATCH 05/22] vm-lib/make-vm.nix: support " Alyssa Ross
2023-02-26 19:17   ` Alyssa Ross
2022-10-10 23:28 ` [PATCH 06/22] img/app: add support for testing virtiofs Alyssa Ross
2023-02-26 19:17   ` Alyssa Ross
2022-10-10 23:28 ` [PATCH 07/22] img/app: don't block app startup on network online Alyssa Ross
2023-02-26 19:17   ` Alyssa Ross
2022-10-10 23:28 ` [PATCH 08/22] img/app: auto-mount virtiofs0 filesystem Alyssa Ross
2023-02-26 19:17   ` Alyssa Ross
2022-10-10 23:28 ` [PATCH 09/22] vm/app/mg.nix: init Alyssa Ross
2023-02-26 19:17   ` Alyssa Ross
2022-10-10 23:28 ` [PATCH 10/22] vm/app/mg.nix: open virtio filesystem in dired Alyssa Ross
2023-02-26 19:17   ` Alyssa Ross
2022-10-10 23:28 ` [PATCH 11/22] host/rootfs: move ext mounting to s6-rc service Alyssa Ross
2022-11-14  1:14   ` Alyssa Ross
2022-10-10 23:28 ` [PATCH 12/22] host/rootfs: automatically grow user partition Alyssa Ross
2022-11-14  1:14   ` Alyssa Ross
2022-10-10 23:28 ` [PATCH 13/22] host/rootfs: use a bigger test ext partition Alyssa Ross
2022-11-14  1:14   ` Alyssa Ross
2022-10-10 23:28 ` [PATCH 14/22] host/initramfs/extfs.nix: tar2ext4 -> mkfs.ext4 -d Alyssa Ross
2022-11-14  1:14   ` Alyssa Ross
2022-10-10 23:28 ` [PATCH 15/22] host/start-vm: resolve VM symlinks with /ext root Alyssa Ross
2022-10-10 23:28 ` [PATCH 16/22] host/rootfs: " Alyssa Ross
2022-10-10 23:28 ` [PATCH 17/22] Documentation: explain /ext symlink resolution Alyssa Ross
2022-10-10 23:28 ` [PATCH 18/22] host/start-vm: increase memory size to 512M Alyssa Ross
2022-10-10 23:28 ` [PATCH 19/22] vm/app/nix: add Alyssa Ross
2022-10-10 23:29 ` [PATCH 20/22] vm-lib/make-vms.nix: add Alyssa Ross
2022-10-10 23:29 ` [PATCH 21/22] host/initramfs/extfs.nix: add example Nix-built VM Alyssa Ross
2022-10-10 23:29 ` [PATCH 22/22] Documentation: add how-to guide for Nix-built VMs Alyssa Ross
2022-10-10 23:29 ` [PATCH v2 0/6] Introduce a shared base for application VMs Alyssa Ross
2022-10-10 23:37   ` Alyssa Ross
2022-10-10 23:29 ` Alyssa Ross [this message]
2022-10-10 23:29 ` [PATCH v2 2/6] scripts/make-gpt.sh: add support for labels Alyssa Ross
2022-10-10 23:29 ` [PATCH v2 3/6] vm: build GPT images Alyssa Ross
2022-10-10 23:29 ` [PATCH v2 4/6] host/start-vm: boot using partition label Alyssa Ross
2022-10-10 23:29 ` [PATCH v2 5/6] release: rename from "img" Alyssa Ross
2022-10-10 23:29 ` [PATCH v2 6/6] img/app: extract from appvm-{lynx,catgirl} Alyssa Ross
  -- strict thread matches above, loose matches on Subject: below --
2022-10-09 11:40 [PATCH v2 0/6] Introduce a shared base for application VMs Alyssa Ross
2022-10-09 11:40 ` [PATCH v2 1/6] host/start-vm: support multiple block devices Alyssa Ross
2022-11-14  1:14   ` Alyssa Ross

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20221010232909.1953738-25-hi@alyssa.is \
    --to=hi@alyssa.is \
    --cc=devel@spectrum-os.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://spectrum-os.org/git/crosvm
	https://spectrum-os.org/git/doc
	https://spectrum-os.org/git/mktuntap
	https://spectrum-os.org/git/nixpkgs
	https://spectrum-os.org/git/spectrum
	https://spectrum-os.org/git/ucspi-vsock
	https://spectrum-os.org/git/www

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).