def get_bind_mounts(chrootdir, bindmounts, mountparent=True):
        chrootmounts = []
        if bindmounts in ("", None):
            bindmounts = ""

        for mount in bindmounts.split(";"):
            if not mount:
                continue

            srcdst = mount.split(":")
            srcdst[0] = os.path.abspath(os.path.expanduser(srcdst[0]))
            if len(srcdst) == 1:
                srcdst.append("none")

            # if some bindmount is not existed, but it's created inside
            # chroot, this is not expected
            if not os.path.exists(srcdst[0]):
                os.makedirs(srcdst[0])

            if not os.path.isdir(srcdst[0]):
                continue

            if srcdst[0] in BIND_MOUNTS or srcdst[0] == '/':
                msger.verbose("%s will be mounted by default." % srcdst[0])
                continue

            if srcdst[1] == "" or srcdst[1] == "none":
                srcdst[1] = None
            else:
                srcdst[1] = os.path.abspath(os.path.expanduser(srcdst[1]))
                if os.path.isdir(chrootdir + "/" + srcdst[1]):
                    msger.warning("%s has existed in %s , skip it."\
                                  % (srcdst[1], chrootdir))
                    continue

            chrootmounts.append(
                fs_related.BindChrootMount(srcdst[0], chrootdir, srcdst[1]))
        """Default bind mounts"""
        for pt in BIND_MOUNTS:
            if not os.path.exists(pt):
                continue
            chrootmounts.append(fs_related.BindChrootMount(
                pt, chrootdir, None))

        if mountparent:
            chrootmounts.append(
                fs_related.BindChrootMount("/", chrootdir, "/parentroot",
                                           "ro"))

        for kernel in os.listdir("/lib/modules"):
            chrootmounts.append(
                fs_related.BindChrootMount("/lib/modules/" + kernel, chrootdir,
                                           None, "ro"))

        return chrootmounts
示例#2
0
    def mount(self, base_on=None, cachedir=None):
        """Setup the target filesystem in preparation for an install.

        This function sets up the filesystem which the ImageCreator will
        install into and configure. The ImageCreator class merely creates an
        install root directory, bind mounts some system directories (e.g. /dev)
        and writes out /etc/fstab. Other subclasses may also e.g. create a
        sparse file, format it and loopback mount it to the install root.

        base_on -- a previous install on which to base this install; defaults
                   to None, causing a new image to be created

        cachedir -- a directory in which to store the Yum cache; defaults to
                    None, causing a new cache to be created; by setting this
                    to another directory, the same cache can be reused across
                    multiple installs.

        """
        self.__setup_tmpdir()
        self.__ensure_builddir()

        # prevent popup dialog in Ubuntu(s)
        misc.hide_loopdev_presentation()

        fs.makedirs(self._instroot)
        fs.makedirs(self._outdir)

        self._mount_instroot(base_on)

        for d in ("/dev/pts", "/etc", "/boot", "/var/log", "/sys", "/proc",
                  "/usr/bin"):
            fs.makedirs(self._instroot + d)

        if self.target_arch and self.target_arch.startswith("arm"):
            self.qemu_emulator = misc.setup_qemu_emulator(
                self._instroot, self.target_arch)

        self.get_cachedir(cachedir)

        # bind mount system directories into _instroot
        for (f, dest) in [("/sys", None), ("/proc", None),
                          ("/proc/sys/fs/binfmt_misc", None),
                          ("/dev/pts", None)]:
            self.__bindmounts.append(
                fs.BindChrootMount(f, self._instroot, dest))

        self._do_bindmounts()

        self.__create_minimal_dev()

        if os.path.exists(self._instroot + "/etc/mtab"):
            os.unlink(self._instroot + "/etc/mtab")
        os.symlink("../proc/mounts", self._instroot + "/etc/mtab")

        self.__write_fstab()

        # get size of available space in 'instroot' fs
        self._root_fs_avail = misc.get_filesystem_avail(self._instroot)
示例#3
0
def get_bindmounts(chrootdir, bindmounts=None):
    """ calculate all bind mount entries for global usage """
    # bindmounts should be a string like '/dev:/dev'
    # FIXME: refine the bindmounts from string to dict
    global chroot_bindmounts

    def totuple(string):
        """ convert string contained ':' to a tuple """
        if ':' in string:
            src, dst = string.split(':', 1)
        else:
            src = string
            dst = None

        return (src or None, dst or None)

    if chroot_bindmounts:
        return chroot_bindmounts

    chroot_bindmounts = []
    bindmounts = bindmounts or ""
    mountlist = []

    for mount in bindmounts.split(";"):
        if not mount:
            continue

        (src, dst) = totuple(mount)

        if src in BIND_MOUNTS or src == '/':
            continue

        if not os.path.exists(src):
            os.makedirs(src)

        if dst and os.path.isdir("%s/%s" % (chrootdir, dst)):
            msger.warning("%s existed in %s , skip it." % (dst, chrootdir))
            continue

        mountlist.append(totuple(mount))

    for mntpoint in BIND_MOUNTS:
        if os.path.isdir(mntpoint):
            mountlist.append(tuple((mntpoint, None)))

    for pair in mountlist:
        if pair[0] == "/lib/modules":
            opt = "ro"
        else:
            opt = None
        bmount = fs_related.BindChrootMount(pair[0], chrootdir, pair[1], opt)
        chroot_bindmounts.append(bmount)

    return chroot_bindmounts