[systemd-commits] 3 commits - TODO man/systemd-nspawn.xml src/core src/nspawn src/shared

Lennart Poettering lennart at kemper.freedesktop.org
Sun Apr 22 06:08:01 PDT 2012


 TODO                        |    6 ---
 man/systemd-nspawn.xml      |   11 ++++++
 src/core/machine-id-setup.c |   47 ++++++--------------------
 src/nspawn/nspawn.c         |   24 +++++++++++--
 src/shared/util.c           |   62 +++++++++++++++++++++++++++++++++++
 src/shared/util.h           |    2 +
 src/shared/virt.c           |   77 ++++++++++++--------------------------------
 7 files changed, 130 insertions(+), 99 deletions(-)

New commits:
commit 8e47b1d2ebefc4ca02c681eba87bb290c5af02d2
Author: Lennart Poettering <lennart at poettering.net>
Date:   Sun Apr 22 15:07:35 2012 +0200

    machine-id: fix spelling

diff --git a/src/core/machine-id-setup.c b/src/core/machine-id-setup.c
index 636519c..c6fd77a 100644
--- a/src/core/machine-id-setup.c
+++ b/src/core/machine-id-setup.c
@@ -100,7 +100,7 @@ static int generate(char id[34]) {
                         if (k >= 36) {
                                 r = shorten_uuid(id, uuid);
                                 if (r >= 0) {
-                                        log_info("Initializing machine ID from KVM UUID");
+                                        log_info("Initializing machine ID from KVM UUID.");
                                         return 0;
                                 }
                         }
@@ -119,7 +119,7 @@ static int generate(char id[34]) {
                         if (strlen(e) >= 36) {
                                 r = shorten_uuid(id, e);
                                 if (r >= 0) {
-                                        log_info("Initializing machine ID from container UUID");
+                                        log_info("Initializing machine ID from container UUID.");
                                         free(e);
                                         return 0;
                                 }

commit ab94af9201496ea3aa59bbf2a01eb750fbd1c08a
Author: Lennart Poettering <lennart at poettering.net>
Date:   Sun Apr 22 14:48:46 2012 +0200

    util: unify getenv() logic for other PID

diff --git a/src/core/machine-id-setup.c b/src/core/machine-id-setup.c
index 9e84ac0..636519c 100644
--- a/src/core/machine-id-setup.c
+++ b/src/core/machine-id-setup.c
@@ -110,45 +110,22 @@ static int generate(char id[34]) {
         /* If that didn't work either, see if we are running in a
          * container, and a machine ID was passed in via
          * $container_uuid the way libvirt/LXC does it */
-
         r = detect_container(NULL);
         if (r > 0) {
-                FILE *f;
-
-                f = fopen("/proc/1/environ", "re");
-                if (f) {
-                        bool done = false;
-
-                        do {
-                                char line[LINE_MAX];
-                                unsigned i;
-
-                                for (i = 0; i < sizeof(line)-1; i++) {
-                                        int c;
-
-                                        c = getc(f);
-                                        if (_unlikely_(c == EOF)) {
-                                                done = true;
-                                                break;
-                                        } else if (c == 0)
-                                                break;
+                char *e;
 
-                                        line[i] = c;
-                                }
-                                line[i] = 0;
-
-                                if (startswith(line, "container_uuid=") &&
-                                    strlen(line + 15) >= 36) {
-                                        r = shorten_uuid(id, line + 15);
-                                        if (r >= 0) {
-                                                log_info("Initializing machine ID from container UUID");
-                                                return 0;
-                                        }
+                r = getenv_for_pid(1, "container_uuid", &e);
+                if (r > 0) {
+                        if (strlen(e) >= 36) {
+                                r = shorten_uuid(id, e);
+                                if (r >= 0) {
+                                        log_info("Initializing machine ID from container UUID");
+                                        free(e);
+                                        return 0;
                                 }
+                        }
 
-                        } while (!done);
-
-                        fclose(f);
+                        free(e);
                 }
         }
 
diff --git a/src/shared/util.c b/src/shared/util.c
index 317abb8..ba24562 100644
--- a/src/shared/util.c
+++ b/src/shared/util.c
@@ -6168,3 +6168,65 @@ int path_is_read_only_fs(const char *path) {
 
         return !!(st.f_flag & ST_RDONLY);
 }
+
+int getenv_for_pid(pid_t pid, const char *field, char **_value) {
+        char path[sizeof("/proc/")-1+10+sizeof("/environ")], *value = NULL;
+        int r;
+        FILE *f;
+        bool done = false;
+        size_t l;
+
+        assert(field);
+        assert(_value);
+
+        if (pid == 0)
+                pid = getpid();
+
+        snprintf(path, sizeof(path), "/proc/%lu/environ", (unsigned long) pid);
+        char_array_0(path);
+
+        f = fopen(path, "re");
+        if (!f)
+                return -errno;
+
+        l = strlen(field);
+        r = 0;
+
+        do {
+                char line[LINE_MAX];
+                unsigned i;
+
+                for (i = 0; i < sizeof(line)-1; i++) {
+                        int c;
+
+                        c = getc(f);
+                        if (_unlikely_(c == EOF)) {
+                                done = true;
+                                break;
+                        } else if (c == 0)
+                                break;
+
+                        line[i] = c;
+                }
+                line[i] = 0;
+
+                if (memcmp(line, field, l) == 0 && line[l] == '=') {
+                        value = strdup(line + l + 1);
+                        if (!value) {
+                                r = -ENOMEM;
+                                break;
+                        }
+
+                        r = 1;
+                        break;
+                }
+
+        } while (!done);
+
+        fclose(f);
+
+        if (r >= 0)
+                *_value = value;
+
+        return r;
+}
diff --git a/src/shared/util.h b/src/shared/util.h
index a26c1d9..17ffd19 100644
--- a/src/shared/util.h
+++ b/src/shared/util.h
@@ -540,4 +540,6 @@ int fork_agent(pid_t *pid, const int except[], unsigned n_except, const char *pa
 
 int setrlimit_closest(int resource, const struct rlimit *rlim);
 
+int getenv_for_pid(pid_t pid, const char *field, char **_value);
+
 #endif
diff --git a/src/shared/virt.c b/src/shared/virt.c
index b74c513..6e44794 100644
--- a/src/shared/virt.c
+++ b/src/shared/virt.c
@@ -153,7 +153,8 @@ int detect_vm(const char **id) {
 }
 
 int detect_container(const char **id) {
-        FILE *f;
+        char *e = NULL;
+        int r;
 
         /* Unfortunately many of these operations require root access
          * in one way or another */
@@ -180,63 +181,29 @@ int detect_container(const char **id) {
                 return 1;
         }
 
-        f = fopen("/proc/1/environ", "re");
-        if (f) {
-                bool done = false;
-
-                do {
-                        char line[LINE_MAX];
-                        unsigned i;
-
-                        for (i = 0; i < sizeof(line)-1; i++) {
-                                int c;
-
-                                c = getc(f);
-                                if (_unlikely_(c == EOF)) {
-                                        done = true;
-                                        break;
-                                } else if (c == 0)
-                                        break;
-
-                                line[i] = c;
-                        }
-                        line[i] = 0;
-
-                        if (streq(line, "container=lxc")) {
-                                fclose(f);
-
-                                if (id)
-                                        *id = "lxc";
-                                return 1;
-
-                        } else if (streq(line, "container=lxc-libvirt")) {
-                                fclose(f);
-
-                                if (id)
-                                        *id = "lxc-libvirt";
-                                return 1;
+        r = getenv_for_pid(1, "container", &e);
+        if (r <= 0)
+                return r;
 
-                        } else if (streq(line, "container=systemd-nspawn")) {
-                                fclose(f);
-
-                                if (id)
-                                        *id = "systemd-nspawn";
-                                return 1;
-
-                        } else if (startswith(line, "container=")) {
-                                fclose(f);
-
-                                if (id)
-                                        *id = "other";
-                                return 1;
-                        }
-
-                } while (!done);
-
-                fclose(f);
+        /* We only recognize a selected few here, since we want to
+         * enforce a redacted namespace */
+        if (streq(e, "lxc")) {
+                if (id)
+                        *id = "lxc";
+        } else if (streq(e, "lxc-libvirt")) {
+                if (id)
+                        *id = "lxc-libvirt";
+        } else if (streq(e, "systemd-nspawn")) {
+                if (id)
+                        *id = "systemd-nspawn";
+        } else {
+                if (id)
+                        *id = "other";
         }
 
-        return 0;
+        free(e);
+
+        return r;
 }
 
 /* Returns a short identifier for the various VM/container implementations */

commit 144f0fc0c8a5e2f6b72179e2b5fb992474da24ad
Author: Lennart Poettering <lennart at poettering.net>
Date:   Sun Apr 22 14:48:21 2012 +0200

    nspawn: add --uuid= switch to allow setting the machine id for the container

diff --git a/TODO b/TODO
index 2569b41..e38c110 100644
--- a/TODO
+++ b/TODO
@@ -43,8 +43,6 @@ Features:
 
 * cg_shorten_controllers() misuses alloca()
 
-* suspend/hibernate/hybrid support, auto-suspend logic with idle hint
-
 * udev systemd unify:
   - strpcpy(), strpcpyl(), strscpy(), strscpyl()
   - utf8 validator code
@@ -80,8 +78,6 @@ Features:
 
 * add man page documenting all kernel cmdline options, including stuff like fsck.mode=
 
-* show getty in container mode, not sulogin
-
 * support container_ttys=
 
 * journald: make configurable "store-on-var", "store-on-run", "dont-store", "auto"
@@ -109,7 +105,7 @@ Features:
 
 * add command to systemctl to plot dependency graph as tree (see rhbz 795365)
 
-* make logind reserve tty10 or so for text logins, so that gdm never picks it up
+* make logind reserve tty9 or so for text logins, so that gdm never picks it up
 
 * add option to sockets to avoid activation. Instead just drop packets/connections, see http://cyberelk.net/tim/2012/02/15/portreserve-systemd-solution/
 
diff --git a/man/systemd-nspawn.xml b/man/systemd-nspawn.xml
index 28e5035..5bf43e8 100644
--- a/man/systemd-nspawn.xml
+++ b/man/systemd-nspawn.xml
@@ -165,6 +165,17 @@
                         </varlistentry>
 
                         <varlistentry>
+                                <term><option>--uuid=</option></term>
+
+                                <listitem><para>Set the specified uuid
+                                for the container. The init system
+                                will initialize
+                                <filename>/etc/machine-id</filename>
+                                from this if this file is not set yet.
+                                </para></listitem>
+                        </varlistentry>
+
+                        <varlistentry>
                                 <term><option>--controllers=</option></term>
                                 <term><option>-C</option></term>
 
diff --git a/src/nspawn/nspawn.c b/src/nspawn/nspawn.c
index 7050c05..bf3a844 100644
--- a/src/nspawn/nspawn.c
+++ b/src/nspawn/nspawn.c
@@ -55,6 +55,7 @@
 static char *arg_directory = NULL;
 static char *arg_user = NULL;
 static char **arg_controllers = NULL;
+static char *arg_uuid = NULL;
 static bool arg_private_network = false;
 static bool arg_boot = false;
 
@@ -67,6 +68,7 @@ static int help(void) {
                "  -b --boot             Boot up full system (i.e. invoke init)\n"
                "  -u --user=USER        Run the command under specified user or uid\n"
                "  -C --controllers=LIST Put the container in specified comma-separated cgroup hierarchies\n"
+               "     --uuid=UUID        Set a specific machine UUID for the container\n"
                "     --private-network  Disable network in container\n",
                program_invocation_short_name);
 
@@ -76,7 +78,8 @@ static int help(void) {
 static int parse_argv(int argc, char *argv[]) {
 
         enum {
-                ARG_PRIVATE_NETWORK = 0x100
+                ARG_PRIVATE_NETWORK = 0x100,
+                ARG_UUID
         };
 
         static const struct option options[] = {
@@ -86,6 +89,7 @@ static int parse_argv(int argc, char *argv[]) {
                 { "controllers",     required_argument, NULL, 'C'                 },
                 { "private-network", no_argument,       NULL, ARG_PRIVATE_NETWORK },
                 { "boot",            no_argument,       NULL, 'b'                 },
+                { "uuid",            required_argument, NULL, ARG_UUID            },
                 { NULL,              0,                 NULL, 0                   }
         };
 
@@ -140,6 +144,10 @@ static int parse_argv(int argc, char *argv[]) {
                         arg_boot = true;
                         break;
 
+                case ARG_UUID:
+                        arg_uuid = optarg;
+                        break;
+
                 case '?':
                         return -EINVAL;
 
@@ -912,6 +920,7 @@ int main(int argc, char *argv[]) {
                         NULL, /* HOME */
                         NULL, /* USER */
                         NULL, /* LOGNAME */
+                        NULL, /* container_uuid */
                         NULL
                 };
 
@@ -1022,13 +1031,20 @@ int main(int argc, char *argv[]) {
                         }
                 }
 
-                if ((asprintf((char**)(envp + 3), "HOME=%s", home? home: "/root") < 0) ||
-                    (asprintf((char**)(envp + 4), "USER=%s", arg_user? arg_user : "root") < 0) ||
-                    (asprintf((char**)(envp + 5), "LOGNAME=%s", arg_user? arg_user : "root") < 0)) {
+                if ((asprintf((char**)(envp + 3), "HOME=%s", home ? home: "/root") < 0) ||
+                    (asprintf((char**)(envp + 4), "USER=%s", arg_user ? arg_user : "root") < 0) ||
+                    (asprintf((char**)(envp + 5), "LOGNAME=%s", arg_user ? arg_user : "root") < 0)) {
                     log_error("Out of memory");
                     goto child_fail;
                 }
 
+                if (arg_uuid) {
+                        if (asprintf((char**)(envp + 6), "container_uuid=%s", arg_uuid) < 0) {
+                                log_error("Out of memory");
+                                goto child_fail;
+                        }
+                }
+
                 setup_hostname();
 
                 if (arg_boot) {



More information about the systemd-commits mailing list