From: Michael Tremer Date: Sun, 25 Sep 2011 10:36:53 +0000 (+0200) Subject: Change the way the buildroot is mounted. X-Git-Tag: 0.9.10~25 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=8930b228887fe8605298edbb225763c6078197a5;p=pakfire.git Change the way the buildroot is mounted. We do mount some more directories that are needed (e.g. /run) and do that once for the whole time the buildroot is existant. --- diff --git a/pakfire/base.py b/pakfire/base.py index 50bd5c4d0..813ed979b 100644 --- a/pakfire/base.py +++ b/pakfire/base.py @@ -422,8 +422,11 @@ class Pakfire(object): resultdirs.append(p.repos.local_build.path) try: - b.prepare() - b.extract() + # Start to prepare the build environment by mounting + # the filesystems and extracting files. + b.start() + + # Build the package. b.build(install_test=install_test) # Copy-out all resultfiles @@ -440,7 +443,7 @@ class Pakfire(object): raise finally: - b.destroy() + b.stop() def _build(self, pkg, resultdir, nodeps=False, **kwargs): b = builder.Builder(self, pkg, resultdir, **kwargs) @@ -457,11 +460,10 @@ class Pakfire(object): b = builder.BuildEnviron(pkg, **kwargs) try: - b.prepare() - b.extract() + b.start() b.shell() finally: - b.destroy() + b.stop() def dist(self, pkgs, resultdirs=None): if not resultdirs: diff --git a/pakfire/builder.py b/pakfire/builder.py index 0149e797f..26c128d1f 100644 --- a/pakfire/builder.py +++ b/pakfire/builder.py @@ -142,6 +142,29 @@ class BuildEnviron(object): # Save the build time. self.build_time = int(time.time()) + def start(self): + # Mount the directories. + self._mountall() + + # Create all devnodes and other dirs we need. + self.prepare() + + # Extract all needed packages. + self.extract() + + def stop(self): + # Kill all still running processes. + util.orphans_kill(self.path) + + # Close pakfire instance. + del self.pakfire + + # Umount the build environment. + self._umountall() + + # Remove all files. + self.destroy() + @property def arch(self): """ @@ -407,8 +430,6 @@ class BuildEnviron(object): os.mknod(filename, mode, device) def destroy(self): - util.orphans_kill(self.path) - logging.debug("Destroying environment %s" % self.path) if os.path.exists(self.path): @@ -430,36 +451,67 @@ class BuildEnviron(object): def _mountall(self): self.log.debug("Mounting environment") - for cmd, mountpoint in self.mountpoints: - cmd = "%s %s" % (cmd, self.chrootPath(mountpoint)) + for src, dest, fs, options in self.mountpoints: + mountpoint = self.chrootPath(dest) + if options: + options = "-o %s" % options + + # Eventually create mountpoint directory + if not os.path.exists(mountpoint): + os.makedirs(mountpoint) + + cmd = "mount -n -t %s %s %s %s" % \ + (fs, options, src, mountpoint) chroot.do(cmd, shell=True) def _umountall(self): self.log.debug("Umounting environment") - for cmd, mountpoint in self.mountpoints: - cmd = "umount -n %s" % self.chrootPath(mountpoint) - chroot.do(cmd, raiseExc=0, shell=True) + + mountpoints = [] + for src, dest, fs, options in reversed(self.mountpoints): + if not dest in mountpoints: + mountpoints.append(dest) + + for dest in mountpoints: + mountpoint = self.chrootPath(dest) + + chroot.do("umount -n %s" % mountpoint, raiseExc=0, shell=True) @property def mountpoints(self): - ret = [ - ("mount -n -t proc pakfire_chroot_proc", "proc"), - ("mount -n -t sysfs pakfire_chroot_sysfs", "sys"), + mountpoints = [] + + # Make root as a tmpfs. + #mountpoints += [ + # ("pakfire_root", "/", "tmpfs", "defaults"), + #] + + mountpoints += [ + # src, dest, fs, options + ("pakfire_proc", "/proc", "proc", "nosuid,noexec,nodev"), + ("/proc/sys", "/proc/sys", "bind", "bind"), + ("/proc/sys", "/proc/sys", "bind", "bind,ro,remount"), + ("/sys", "/sys", "bind", "bind"), + ("/sys", "/sys", "bind", "bind,ro,remount"), + ("pakfire_tmpfs", "/dev", "tmpfs", "mode=755,nosuid"), + ("/dev/pts", "/dev/pts", "bind", "bind"), + ("pakfire_tmpfs", "/run", "tmpfs", "mode=755,nosuid,nodev"), ] - mountopt = "gid=%d,mode=0620,ptmxmode=0666" % grp.getgrnam("tty").gr_gid - if self.kernel_version >= "2.6.29": - mountopt += ",newinstance" - - ret.extend([ - ("mount -n -t devpts -o %s pakfire_chroot_devpts" % mountopt, "dev/pts"), - ("mount -n -t tmpfs pakfire_chroot_shmfs", "dev/shm"), - ]) + # If selinux is enabled. + if os.path.exists("/sys/fs/selinux"): + mountpoints += [ + ("/sys/fs/selinux", "/sys/fs/selinux", "bind", "bind"), + ("/sys/fs/selinux", "/sys/fs/selinux", "bind", "bind,ro,remount"), + ] + # If ccache support is requested, we bind mount the cache. if self.settings.get("enable_ccache"): - ret.append(("mount -n --bind %s" % CCACHE_CACHE_DIR, "var/cache/ccache")) + mountpoints += [ + (CCACHE_CACHE_DIR, "/var/cache/ccache", "bind", "bind"), + ] - return ret + return mountpoints @property def environ(self): @@ -494,43 +546,38 @@ class BuildEnviron(object): def do(self, command, shell=True, personality=None, logger=None, *args, **kwargs): ret = None - try: - # Environment variables - env = self.environ - if kwargs.has_key("env"): - env.update(kwargs.pop("env")) - - logging.debug("Environment:") - for k, v in sorted(env.items()): - logging.debug(" %s=%s" % (k, v)) + # Environment variables + env = self.environ - # Update personality it none was set - if not personality: - personality = self.distro.personality + if kwargs.has_key("env"): + env.update(kwargs.pop("env")) - # Make every shell to a login shell because we set a lot of - # environment things there. - if shell: - command = ["bash", "--login", "-c", command] + logging.debug("Environment:") + for k, v in sorted(env.items()): + logging.debug(" %s=%s" % (k, v)) - self._mountall() + # Update personality it none was set + if not personality: + personality = self.distro.personality - if not kwargs.has_key("chrootPath"): - kwargs["chrootPath"] = self.chrootPath() + # Make every shell to a login shell because we set a lot of + # environment things there. + if shell: + command = ["bash", "--login", "-c", command] - ret = chroot.do( - command, - personality=personality, - shell=False, - env=env, - logger=logger, - *args, - **kwargs - ) + if not kwargs.has_key("chrootPath"): + kwargs["chrootPath"] = self.chrootPath() - finally: - self._umountall() + ret = chroot.do( + command, + personality=personality, + shell=False, + env=env, + logger=logger, + *args, + **kwargs + ) return ret @@ -581,14 +628,8 @@ class BuildEnviron(object): logging.debug("Shell command: %s" % command) - try: - self._mountall() - - shell = os.system(command) - return os.WEXITSTATUS(shell) - - finally: - self._umountall() + shell = os.system(command) + return os.WEXITSTATUS(shell) class Builder(object): diff --git a/po/pakfire.pot b/po/pakfire.pot index 63fe0751d..2204c3aa4 100644 --- a/po/pakfire.pot +++ b/po/pakfire.pot @@ -8,7 +8,7 @@ msgid "" msgstr "" "Project-Id-Version: PACKAGE VERSION\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2011-09-24 17:09+0200\n" +"POT-Creation-Date: 2011-09-24 23:54+0200\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME \n" "Language-Team: LANGUAGE \n" @@ -69,11 +69,11 @@ msgstr "" msgid "There are no packages to install." msgstr "" -#: ../pakfire/base.py:451 +#: ../pakfire/base.py:454 msgid "Build command has failed." msgstr "" -#: ../pakfire/base.py:535 +#: ../pakfire/base.py:537 msgid "Everything is fine." msgstr "" @@ -82,22 +82,22 @@ msgid "Package information:" msgstr "" #. Copy the makefile and load source tarballs. -#: ../pakfire/builder.py:261 +#: ../pakfire/builder.py:284 msgid "Extracting" msgstr "" -#: ../pakfire/builder.py:551 +#: ../pakfire/builder.py:605 msgid "The build command failed. See logfile for details." msgstr "" #. Package the result. #. Make all these little package from the build environment. -#: ../pakfire/builder.py:700 +#: ../pakfire/builder.py:748 msgid "Creating packages:" msgstr "" #. Execute the buildscript of this stage. -#: ../pakfire/builder.py:720 +#: ../pakfire/builder.py:768 #, python-format msgid "Running stage %s:" msgstr ""