I’m writing a new nbdkit plugin called linuxdisk. nbdkit is our flexible, plugin-based NBD server, and this new plugin lets you create a complete Linux-compatible virtual disk from a host directory on the fly.
One of the many uses for this is booting minimal VMs very quickly. Here’s an example you can set up in a few seconds. It boots to an interactive busybox shell:
$ mkdir /tmp/root /tmp/root/sbin /tmp/root/bin /tmp/root/dev $ sudo mknod /tmp/root/dev/console c 5 1 $ cp /sbin/busybox /tmp/root/sbin/ $ ln /tmp/root/sbin/busybox /tmp/root/bin/sh $ ln /tmp/root/sbin/busybox /tmp/root/bin/ls $ ln /tmp/root/sbin/busybox /tmp/root/sbin/init $ nbdkit -U - linuxdisk /tmp/root \ --run 'qemu-kvm -display none -kernel /boot/vmlinuz-4.20.8-200.fc29.x86_64 -drive file=nbd:unix:$unixsocket,snapshot=on -append "console=ttyS0 root=/dev/sda1 rw" -serial stdio'
If you need any extra files in the VM just drop them straight into /tmp/root
before booting it.
Edit: How the heck does /dev
get populated in this VM?
What if you edit the files while the VM is running? Will those appear on the guest system?
No.
Longer reply now that I’m not using a phone …
It works by creating an ext2 filesystem from scratch each time (which only takes a fraction of a second). The code is here: https://github.com/rwmjones/nbdkit/blob/master/plugins/linuxdisk/filesystem.c
If you want to share a filesystem with the host you must use NFS or virtio-fs or similar.
Pingback: nbdkit 1.12 | Richard WM Jones