[libvirt] [sandbox 07/10] qemu: use mounts targeting / as root

Daniel P. Berrange berrange at redhat.com
Fri Jun 26 16:42:28 UTC 2015


On Thu, Jun 25, 2015 at 06:49:44PM +0200, Cédric Bosdonnat wrote:
> So far a mount with / as target doesn't change anything: the host / is
> still the one mounted as /. libvirt-sandbox-init-qemu now detects the
> presence of a / target in mounts.cfg and mounts it instead of
> sandbox:root.
> ---
>  libvirt-sandbox/libvirt-sandbox-init-qemu.c | 69 ++++++++++++++++++++++++++++-
>  1 file changed, 67 insertions(+), 2 deletions(-)
> 
> diff --git a/libvirt-sandbox/libvirt-sandbox-init-qemu.c b/libvirt-sandbox/libvirt-sandbox-init-qemu.c
> index 9acea5f..02fb980 100644
> --- a/libvirt-sandbox/libvirt-sandbox-init-qemu.c
> +++ b/libvirt-sandbox/libvirt-sandbox-init-qemu.c
> @@ -264,6 +264,70 @@ mount_entry(const char *source,
>      }
>  }
>  
> +static void
> +mount_root(const char *path)
> +{
> +    int foundRoot = 0;
> +
> +    /* Loop over mounts.cfg to see if we have a candidate for / */
> +    mount_mkdir(SANDBOXCONFIGDIR, 0755);
> +    mount_9pfs("sandbox:config", SANDBOXCONFIGDIR, 0755, 1);
> +
> +    FILE *fp = fopen(SANDBOXCONFIGDIR "/mounts.cfg", "r");
> +    while (fgets(line, sizeof line, fp) && !foundRoot) {
> +        char *source = line;
> +        char *target = strchr(source, '\t');
> +        *target = '\0';
> +        target++;
> +        char *type = strchr(target, '\t');
> +        *type = '\0';
> +        type++;
> +        char *opts = strchr(type, '\t');
> +        *opts = '\0';
> +        opts++;
> +        char *tmp = strchr(opts, '\n');
> +        *tmp = '\0';
> +
> +        if (STREQ(target, "/")) {
> +            int needsDev = strncmp(source, "/dev/", 5) == 0;
> +
> +            if (debug)
> +                fprintf(stderr, "libvirt-sandbox-init-qemu: found root from %s\n",
> +                        source);
> +
> +            /* In this case, we need to have a /dev before the chroot */
> +            if (needsDev) {
> +                mount_other("/proc", "proc", 0755);
> +                mount_other("/dev", "tmpfs", 0755);

Should this be 'devtmpfs' instead of just 'tmpfs' now ?

> +            }
> +
> +            mount_entry(source, path, type, opts);
> +
> +            if (needsDev && umount("/dev") < 0) {
> +                fprintf(stderr,
> +                        "libvirt-sandbox-init-qemu: %s: "
> +                        "cannot unmount temporary /dev: %s\n",
> +                        __func__, strerror(errno));
> +                exit_poweroff();
> +            }

Do we need to unmount the temporary /proc too, to just avoid
polluting /proc/mounts with multiple instances of it

> +            foundRoot = 1;
> +        }
> +    }
> +    fclose(fp);
> +
> +    if (umount(SANDBOXCONFIGDIR) < 0) {
> +        fprintf(stderr,
> +                "libvirt-sandbox-init-qemu: %s: "
> +                "cannot unmount temporary %s: %s\n",
> +                __func__, SANDBOXCONFIGDIR, strerror(errno));
> +        exit_poweroff();
> +    }
> +
> +    /* If we couldn't get a / in the mounts, then use the host one */
> +    if (!foundRoot)
> +        mount_9pfs("sandbox:root", path, 0755, 1);
> +}
> +
>  int
>  main(int argc ATTR_UNUSED, char **argv ATTR_UNUSED)
>  {
> @@ -308,7 +372,7 @@ main(int argc ATTR_UNUSED, char **argv ATTR_UNUSED)
>      if (debug)
>          fprintf(stderr, "libvirt-sandbox-init-qemu: mounting new root on /tmproot\n");
>  
> -    mount_9pfs("sandbox:root", "/tmproot", 0755, 1);
> +    mount_root("/tmproot");
>  
>      /* Note that pivot_root won't work.  See the note in
>       * Documentation/filesystems/ramfs-rootfs-initramfs.txt
> @@ -412,7 +476,8 @@ main(int argc ATTR_UNUSED, char **argv ATTR_UNUSED)
>              fprintf(stderr, "libvirt-sandbox-init-qemu: %s: %s -> %s (%s, %s)\n",
>                      __func__, source, target, type, opts);
>  
> -        mount_entry(source, target, type, opts);
> +        if (STREQ(target, "/"))

Shouldn't this be inverted, ie  STRNEQ instead of STREQ - ie we
need to skip '/'

> +            mount_entry(source, target, type, opts);
>      }
>      fclose(fp);
>  


Regards,
Daniel
-- 
|: http://berrange.com      -o-    http://www.flickr.com/photos/dberrange/ :|
|: http://libvirt.org              -o-             http://virt-manager.org :|
|: http://autobuild.org       -o-         http://search.cpan.org/~danberr/ :|
|: http://entangle-photo.org       -o-       http://live.gnome.org/gtk-vnc :|




More information about the libvir-list mailing list