]> git.ipfire.org Git - pakfire.git/commitdiff
Move populating /dev to libpakfire
authorMichael Tremer <michael.tremer@ipfire.org>
Sat, 13 Feb 2021 12:41:06 +0000 (12:41 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sat, 13 Feb 2021 13:07:48 +0000 (13:07 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/libpakfire/pakfire.c
src/pakfire/builder.py

index 349efe8edd4907569bb515fbcedc94a0dfb1255c..b11dae96958693a96aa512ac6edbb3fc6bc0184a 100644 (file)
@@ -26,6 +26,7 @@
 #include <stdlib.h>
 #include <sys/mount.h>
 #include <sys/stat.h>
+#include <sys/sysmacros.h>
 #include <sys/types.h>
 #include <syslog.h>
 #include <unistd.h>
@@ -362,6 +363,76 @@ ERROR:
        return r;
 }
 
+static const struct pakfire_devnode {
+       const char* path;
+       int major;
+       int minor;
+       mode_t mode;
+} devnodes[] = {
+       { "/dev/null",      1,  3, S_IFCHR|S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH, },
+       { "/dev/zero",      1,  5, S_IFCHR|S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH, },
+       { "/dev/full",      1,  7, S_IFCHR|S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH, },
+       { "/dev/random",    1,  8, S_IFCHR|S_IRUSR|S_IRGRP|S_IROTH, },
+       { "/dev/urandom",   1,  9, S_IFCHR|S_IRUSR|S_IRGRP|S_IROTH, },
+       { "/dev/kmsg",      1, 11, S_IFCHR|S_IRUSR|S_IRGRP|S_IROTH, },
+       { "/dev/tty",       5,  0, S_IFCHR|S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH, },
+       { "/dev/console",   5,  1, S_IFCHR|S_IRUSR|S_IWUSR, },
+       { "/dev/ptmx",      5,  2, S_IFCHR|S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH, },
+       { "/dev/rtc0",    252,  0, S_IFCHR|S_IRUSR|S_IWUSR, },
+       { NULL },
+};
+
+static const struct pakfire_symlink {
+       const char* target;
+       const char* path;
+} symlinks[] = {
+       { "/proc/self/fd",   "/dev/fd", },
+       { "/proc/self/fd/0", "/dev/stdin" },
+       { "/proc/self/fd/1", "/dev/stdout" },
+       { "/proc/self/fd/2", "/dev/stderr" },
+       { "/proc/kcore",     "/dev/core" },
+       { NULL },
+};
+
+static int pakfire_populate_dev(Pakfire pakfire) {
+       // Create device nodes
+       for (const struct pakfire_devnode* devnode = devnodes; devnode->path; devnode++) {
+               DEBUG(pakfire, "Creating device node %s\n", devnode->path);
+
+               char* path = pakfire_make_path(pakfire, devnode->path);
+               dev_t dev = makedev(devnode->major, devnode->minor);
+
+               int r = mknod(path, devnode->mode, dev);
+               if (r) {
+                       ERROR(pakfire, "Could not create %s: %s\n", devnode->path, strerror(errno));
+                       free(path);
+
+                       return r;
+               }
+
+               free(path);
+       }
+
+       // Create symlinks
+       for (const struct pakfire_symlink* s = symlinks; s->target; s++) {
+               DEBUG(pakfire, "Creating symlink %s -> %s\n", s->path, s->target);
+
+               char* path = pakfire_make_path(pakfire, s->path);
+
+               int r = symlink(s->target, path);
+               if (r) {
+                       ERROR(pakfire, "Could not create symlink %s: %s\n", s->path, strerror(errno));
+                       free(path);
+
+                       return r;
+               }
+
+               free(path);
+       }
+
+       return 0;
+}
+
 PAKFIRE_EXPORT int pakfire_activate(Pakfire pakfire) {
        if (strcmp(pakfire->path, "/") == 0)
                return 0;
@@ -378,6 +449,11 @@ PAKFIRE_EXPORT int pakfire_activate(Pakfire pakfire) {
                return r;
        }
 
+       // Populate /dev
+       r = pakfire_populate_dev(pakfire);
+       if (r)
+               return r;
+
        return 0;
 }
 
index 2220943dc16c9633734857325d0b9388a009c50c..c4f851fa06024e18097f564ea299ef6049fbf97c 100644 (file)
@@ -73,7 +73,6 @@ class Builder(object):
 
                # Settings array.
                self.settings = {
-                       "enable_loop_devices" : self.config.get_bool("builder", "use_loop_devices", True),
                        "enable_ccache"       : self.config.get_bool("builder", "use_ccache", True),
                        "buildroot_tmpfs"     : self.config.get_bool("builder", "use_tmpfs", False),
                }
@@ -123,9 +122,6 @@ class Builder(object):
                # Lock the build environment
                self.lock()
 
-               # Populate /dev
-               self.populate_dev()
-
                # Setup domain name resolution in chroot
                self.setup_dns()
 
@@ -301,42 +297,6 @@ class Builder(object):
 
                shutil.copy2(file_in, file_out)
 
-       def populate_dev(self):
-               nodes = [
-                       "/dev/null",
-                       "/dev/zero",
-                       "/dev/full",
-                       "/dev/random",
-                       "/dev/urandom",
-                       "/dev/tty",
-                       "/dev/ptmx",
-                       "/dev/kmsg",
-                       "/dev/rtc0",
-                       "/dev/console",
-               ]
-
-               # If we need loop devices (which are optional) we create them here.
-               if self.settings["enable_loop_devices"]:
-                       for i in range(0, 7):
-                               nodes.append("/dev/loop%d" % i)
-
-               for node in nodes:
-                       # Stat the original node of the host system and copy it to
-                       # the build chroot.
-                       try:
-                               node_stat = os.stat(node)
-
-                       # If it cannot be found, just go on.
-                       except OSError:
-                               continue
-
-                       self._create_node(node, node_stat.st_mode, node_stat.st_rdev)
-
-               os.symlink("/proc/self/fd/0", self.chrootPath("dev", "stdin"))
-               os.symlink("/proc/self/fd/1", self.chrootPath("dev", "stdout"))
-               os.symlink("/proc/self/fd/2", self.chrootPath("dev", "stderr"))
-               os.symlink("/proc/self/fd",   self.chrootPath("dev", "fd"))
-
        def chrootPath(self, *args):
                # Remove all leading slashes
                _args = []
@@ -361,18 +321,6 @@ class Builder(object):
                for i in ("/etc/resolv.conf", "/etc/hosts"):
                        self.copyin(i, i)
 
-       def _create_node(self, filename, mode, device):
-               self.log.debug("Create node: %s (%s)" % (filename, mode))
-
-               filename = self.chrootPath(filename)
-
-               # Create parent directory if it is missing.
-               dirname = os.path.dirname(filename)
-               if not os.path.exists(dirname):
-                       os.makedirs(dirname)
-
-               os.mknod(filename, mode, device)
-
        def execute_root(self, command, **kwargs):
                """
                        Executes the given command outside the build chroot.