[Libguestfs] [PATCH nbdkit v3 4/4] Add linuxdisk plugin.
Eric Blake
eblake at redhat.com
Fri Feb 22 15:42:38 UTC 2019
On 2/22/19 3:07 AM, Richard W.M. Jones wrote:
> From: "Richard W.M. Jones" <rjones at redhat.com>
>
> This plugin allows you to create a complete ext2, ext3 or ext4
> filesystem in a GPT partitioned disk image. This can be attached as a
> disk to a Linux virtual machine. It is implemented using e2fsprogs
> mke2fs ‘-d’ option thus allowing the implementation to be very small
> and simple, with all the hard work done by mke2fs.
>
> Although there is some overlap with nbdkit-iso-plugin and
> nbdkit-floppy-plugin, the implementations and use cases of all three
> +static int64_t
> +estimate_size (void)
> +{
>
> + r = pclose (fp);
> + if (r == -1) {
> + nbdkit_error ("pclose: %m");
> + free (line);
> + return -1;
> + }
> + if (WIFEXITED (r) && WEXITSTATUS (r) != 0) {
> + nbdkit_error ("du command failed with exit code %d", WEXITSTATUS (r));
> + return -1;
> + }
> +
Doesn't cover if the process died due to signal.
> +static int
> +mke2fs (const char *filename)
> +{
> + /* Run the command. */
> + nbdkit_debug ("%s", command);
> + r = system (command);
> + free (command);
> +
> + if (WIFEXITED (r) && WEXITSTATUS (r) != 0) {
> + nbdkit_error ("mke2fs command failed with exit code %d", WEXITSTATUS (r));
> + return -1;
> + }
> + else if (WIFSIGNALED (r)) {
> + nbdkit_error ("mke2fs command was killed by signal %d", WTERMSIG (r));
> + return -1;
> + }
> + else if (WIFSTOPPED (r)) {
> + nbdkit_error ("mke2fs command was stopped by signal %d", WSTOPSIG (r));
> + return -1;
> + }
> +
WIFSTOPPED() branch is unreachable.
> +=item nbdkit --filter=cow linuxdisk /path/to/directory
> +
> +Add a writable overlay (see L<nbdkit-cow-filter(1)>, allowing the disk
> +to be written by the client. B<Multiple clients must not be allowed
> +to connect at the same time> (even if they all mount it read-only) as
> +this will cause disk corruption.
Not a cause for changing any of the wording, but just to make sure I
understand: even when a filesystem is mounted read-only, the kernel may
still write to the underlying block device (such as fsck or journal
updates), and THESE writes must only be done by a single writer, hence
allowing multiple writers to a cow wrapper around the read-only file
system indeed risks races where one writer corrupts another.
> +=item Create a minimal virtual appliance
> +
> +This creates and boots a minimal L<busybox(1)>-based virtual
> +appliance. This assumes that your kernel (F</boot/vmlinuz>) contains
> +the ext2 or ext4 driver compiled in, but most Linux distro kernels
> +have that.
> +
> + mkdir root root/bin root/dev root/proc root/sbin root/sys
> + mkdir root/usr root/usr/bin root/usr/sbin
Worth 'mkdir -p' in this example?
> + sudo mknod root/dev/console c 5 1
> + cp /sbin/busybox root/sbin/
> + ln root/sbin/busybox root/sbin/init
> + ln root/sbin/busybox root/bin/ls
> + ln root/sbin/busybox root/bin/sh
> + nbdkit -U - linuxdisk root --run '
> + qemu-kvm -display none -kernel /boot/vmlinuz -drive file=nbd:unix:$unixsocket,snapshot=on -append "console=ttyS0 root=/dev/sda1 rw" -serial stdio
> + '
> +
> +You can drop any extra files you need into the F<root/> directory and
> +they will be copied into the appliance before boot. After booting
> +type these commands to complete the environmental setup:
> +
> + /sbin/busybox --install
> + mount -t proc proc /proc
> + mount -t sysfs sys /sys
Pretty cool!
I think you're ready to go, with minor tweaks to child process status
handling.
--
Eric Blake, Principal Software Engineer
Red Hat, Inc. +1-919-301-3226
Virtualization: qemu.org | libvirt.org
More information about the Libguestfs
mailing list