From: Michael Tremer Date: Sun, 21 Mar 2021 13:44:56 +0000 (+0000) Subject: builder: Add function to bind-mount ccache X-Git-Tag: 0.9.28~1285^2~497 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=69e754ab648351a42b40b3e8dc5e548dbc33f42e;p=pakfire.git builder: Add function to bind-mount ccache Signed-off-by: Michael Tremer --- diff --git a/src/_pakfire/pakfire.c b/src/_pakfire/pakfire.c index 13b83a24c..e00bc66c0 100644 --- a/src/_pakfire/pakfire.c +++ b/src/_pakfire/pakfire.c @@ -694,7 +694,29 @@ static int Pakfire_set_offline(PakfireObject* self, PyObject* value) { return 0; } +static PyObject* Pakfire_bind(PakfireObject* self, PyObject* args) { + const char* src = NULL; + const char* dst = NULL; + + if (!PyArg_ParseTuple(args, "s|z", &src, &dst)) + return NULL; + + int r = pakfire_bind(self->pakfire, src, dst, 0); + if (r) { + PyErr_SetFromErrno(PyExc_OSError); + return NULL; + } + + Py_RETURN_NONE; +} + static struct PyMethodDef Pakfire_methods[] = { + { + "bind", + (PyCFunction)Pakfire_bind, + METH_VARARGS, + NULL, + }, { "dist", (PyCFunction)Pakfire_dist, diff --git a/src/libpakfire/include/pakfire/pakfire.h b/src/libpakfire/include/pakfire/pakfire.h index 4468ae9c4..091135c16 100644 --- a/src/libpakfire/include/pakfire/pakfire.h +++ b/src/libpakfire/include/pakfire/pakfire.h @@ -39,6 +39,7 @@ char* pakfire_make_path(Pakfire pakfire, const char* path); int pakfire_activate(Pakfire pakfire); int pakfire_deactivate(Pakfire pakfire); +int pakfire_bind(Pakfire pakfire, const char* src, const char* dst, int flags); const char* pakfire_get_arch(Pakfire pakfire); diff --git a/src/libpakfire/libpakfire.sym b/src/libpakfire/libpakfire.sym index ff5208f1f..3bbd4cca7 100644 --- a/src/libpakfire/libpakfire.sym +++ b/src/libpakfire/libpakfire.sym @@ -22,6 +22,7 @@ LIBPAKFIRE_0 { global: # pakfire pakfire_activate; + pakfire_bind; pakfire_count_packages; pakfire_create; pakfire_deactivate; diff --git a/src/libpakfire/pakfire.c b/src/libpakfire/pakfire.c index 1fc0f31e3..98edd75f8 100644 --- a/src/libpakfire/pakfire.c +++ b/src/libpakfire/pakfire.c @@ -303,6 +303,29 @@ PAKFIRE_EXPORT char* pakfire_make_path(Pakfire pakfire, const char* path) { return pakfire_path_join(pakfire->path, path); } +PAKFIRE_EXPORT int pakfire_bind(Pakfire pakfire, const char* src, const char* dst, int flags) { + if (!dst) + dst = src; + + char* mountpoint = pakfire_make_path(pakfire, dst); + if (!mountpoint) + return 1; + + DEBUG(pakfire, "Mounting %s to %s\n", src, mountpoint); + + // Make sure the directory exists + int r = pakfire_mkdir(pakfire, mountpoint, 0); + if (r) + goto ERROR; + + // Perform mount + r = mount(src, mountpoint, NULL, flags|MS_BIND, NULL); + +ERROR: + free(mountpoint); + return r; +} + static const struct pakfire_mountpoint { const char* source; const char* target; diff --git a/src/pakfire/base.py b/src/pakfire/base.py index 095aa11a2..435dfe569 100644 --- a/src/pakfire/base.py +++ b/src/pakfire/base.py @@ -40,7 +40,7 @@ from .i18n import _ class Pakfire(_pakfire.Pakfire): __version__ = PAKFIRE_VERSION - def __init__(self, path="/", config=None, arch=None, distro=None, offline=False): + def __init__(self, path=None, config=None, arch=None, distro=None, offline=False): _pakfire.Pakfire.__init__(self, path, arch, offline=offline) # Initialise logging system diff --git a/src/pakfire/builder.py b/src/pakfire/builder.py index 114550b22..711c57561 100644 --- a/src/pakfire/builder.py +++ b/src/pakfire/builder.py @@ -93,9 +93,6 @@ class Builder(object): # Setup logging self.log = self.setup_logging(logfile) - # Path - self.path = os.path.join(BUILD_ROOT, self.build_id) - # Architecture to build for self.arch = arch or _pakfire.native_arch() @@ -107,18 +104,15 @@ class Builder(object): self.cgroup = self._make_cgroup() def __enter__(self): - self.log.debug("Entering %s" % self.path) - - # Mount the build environment - self._mount() + self.log.debug("Entering %s" % self) # Setup domain name resolution in chroot - self.setup_dns() + #self.setup_dns() return BuilderContext(self) def __exit__(self, type, value, traceback): - self.log.debug("Leaving %s" % self.path) + self.log.debug("Leaving %s" % self) # Kill all remaining processes in the build environment self.cgroup.killall() @@ -127,12 +121,6 @@ class Builder(object): self.cgroup.destroy() self.cgroup = None - # Umount the build environment - self._umount() - - # Delete everything - self._destroy() - def setup_logging(self, logfile): l = log.getChild(self.build_id) l.setLevel(logging.DEBUG) @@ -171,39 +159,6 @@ class Builder(object): return cgroup - def _destroy(self): - self.log.debug("Destroying environment %s" % self.path) - - if os.path.exists(self.path): - util.rm(self.path) - - def _mount(self): - """ - Mounts the build environment - """ - os.makedirs(self.path) - - # Bind-mount the ccache if enabled - if self.settings.get("enable_ccache"): - if not os.path.exists(CCACHE_CACHE_DIR): - os.makedirs(CCACHE_CACHE_DIR) - - os.makedirs("%s/var/cache/ccache" % self.path) - - _pakfire.mount(CCACHE_CACHE_DIR, "%s/var/cache/ccache" % self.path, - flags=_pakfire.MS_BIND) - - def _umount(self): - """ - Umounts the build environment - """ - mountpoints = ( - "%s/var/cache/ccache" % self.path, - ) - - for mp in mountpoints: - _pakfire.umount(mp) - def copyin(self, file_out, file_in): if file_in.startswith("/"): file_in = file_in[1:] @@ -272,12 +227,14 @@ class BuilderContext(object): # Initialise Pakfire instance self.pakfire = base.Pakfire( - path=self.builder.path, config=self.builder.config, distro=self.builder.config.distro, arch=self.builder.arch, ) + # Mount ccache + self.pakfire.bind(CCACHE_CACHE_DIR, "/var/cache/ccache") + @property def environ(self): # Build a minimal environment for executing, but try to inherit TERM and LANG