]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
getty-generator: Use device hotplug to instantiate virtualizer consoles
authorDaan De Meyer <daan.j.demeyer@gmail.com>
Mon, 17 Apr 2023 22:46:11 +0000 (00:46 +0200)
committerDaan De Meyer <daan.j.demeyer@gmail.com>
Tue, 18 Apr 2023 07:35:14 +0000 (09:35 +0200)
If getty-generator runs in the initrd, the corresponding tty might not
have been instantiated yet in /dev, which means a serial getty is not
spawned on it. Instead, let's instantiate the serial-getty when the
device appears so that it always gets instantiated.

src/getty-generator/getty-generator.c

index d255e90db52ec722484f8725355d87992d7b8efc..994f2df96fedf5ee0af29b593fed9aafb6138041 100644 (file)
 static const char *arg_dest = NULL;
 static bool arg_enabled = true;
 
-static int add_symlink(const char *fservice, const char *tservice) {
-        char *from, *to;
+static int add_symlink(const char *wants, const char *fservice, const char *tservice) {
+        _cleanup_free_ char *from = NULL, *to = NULL;
         int r;
 
+        assert(wants);
         assert(fservice);
         assert(tservice);
 
-        from = strjoina(SYSTEM_DATA_UNIT_DIR "/", fservice);
-        to = strjoina(arg_dest, "/getty.target.wants/", tservice);
+        from = path_join(SYSTEM_DATA_UNIT_DIR, fservice);
+        if (!from)
+                return -ENOMEM;
+
+        to = path_join(arg_dest, wants, tservice);
+        if (!to)
+                return -ENOMEM;
 
         (void) mkdir_parents_label(to, 0755);
 
@@ -47,7 +53,7 @@ static int add_symlink(const char *fservice, const char *tservice) {
         return 0;
 }
 
-static int add_serial_getty(const char *tty) {
+static int add_serial_getty(const char *wants ,const char *tty) {
         _cleanup_free_ char *n = NULL;
         int r;
 
@@ -59,7 +65,7 @@ static int add_serial_getty(const char *tty) {
         if (r < 0)
                 return log_error_errno(r, "Failed to generate service name: %m");
 
-        return add_symlink("serial-getty@.service", n);
+        return add_symlink(wants, "serial-getty@.service", n);
 }
 
 static int add_container_getty(const char *tty) {
@@ -74,7 +80,7 @@ static int add_container_getty(const char *tty) {
         if (r < 0)
                 return log_error_errno(r, "Failed to generate service name: %m");
 
-        return add_symlink("container-getty@.service", n);
+        return add_symlink("getty.target.wants", "container-getty@.service", n);
 }
 
 static int verify_tty(const char *name) {
@@ -107,7 +113,7 @@ static int run_container(void) {
 
         log_debug("Automatically adding console shell.");
 
-        r = add_symlink("console-getty.service", "console-getty.service");
+        r = add_symlink("getty.target.wants", "console-getty.service", "console-getty.service");
         if (r < 0)
                 return r;
 
@@ -209,7 +215,7 @@ static int run(const char *dest, const char *dest_early, const char *dest_late)
                 if (verify_tty(tty) < 0)
                         continue;
 
-                r = add_serial_getty(tty);
+                r = add_serial_getty("getty.target.wants", tty);
                 if (r < 0)
                         return r;
         }
@@ -224,13 +230,11 @@ static int run(const char *dest, const char *dest_early, const char *dest_late)
                        "3270!tty1") {
                 _cleanup_free_ char *p = NULL;
 
-                p = path_join("/sys/class/tty", j);
+                p = strjoin("dev-", j, ".device.wants");
                 if (!p)
                         return -ENOMEM;
-                if (access(p, F_OK) < 0)
-                        continue;
 
-                r = add_serial_getty(j);
+                r = add_serial_getty(p, j);
                 if (r < 0)
                         return r;
         }