diff options
author | Lennart Poettering <lennart@poettering.net> | 2012-03-14 14:06:42 +0100 |
---|---|---|
committer | Lennart Poettering <lennart@poettering.net> | 2012-03-14 14:06:42 +0100 |
commit | 09b967eaa51a39dabb7f238927f67bd682466dbc (patch) | |
tree | 96fef6956a89eb27703c84270a4100fbc3f6bfba /src | |
parent | 2d44fc7ba5aaf700672baa7b0697caefddafdc53 (diff) |
machine-id: initialize from $container_uuid if not set otherwise
This is a result of the discussions on
https://bugs.freedesktop.org/show_bug.cgi?id=46894
Diffstat (limited to 'src')
-rw-r--r-- | src/machine-id-setup.c | 81 |
1 files changed, 67 insertions, 14 deletions
diff --git a/src/machine-id-setup.c b/src/machine-id-setup.c index d584181bed..0f97433804 100644 --- a/src/machine-id-setup.c +++ b/src/machine-id-setup.c @@ -35,6 +35,28 @@ #include "log.h" #include "virt.h" +static int shorten_uuid(char destination[36], const char *source) { + unsigned i, j; + + for (i = 0, j = 0; i < 36 && j < 32; i++) { + int t; + + t = unhexchar(source[i]); + if (t < 0) + continue; + + destination[j++] = hexchar(t); + } + + if (i == 36 && j == 32) { + destination[32] = '\n'; + destination[33] = 0; + return 0; + } + + return -EINVAL; +} + static int generate(char id[34]) { int fd, r; unsigned char *p; @@ -75,26 +97,57 @@ static int generate(char id[34]) { close_nointr_nofail(fd); if (k >= 36) { - unsigned i, j; + r = shorten_uuid(id, uuid); + if (r >= 0) { + log_info("Initializing machine ID from KVM UUID"); + return 0; + } + } + } + } - for (i = 0, j = 0; i < 36 && j < 32; i++) { - int t; + /* 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 */ - t = unhexchar(uuid[i]); - if (t < 0) - continue; + r = detect_container(NULL); + if (r > 0) { + FILE *f; - id[j++] = hexchar(t); - } + f = fopen("/proc/1/environ", "re"); + if (f) { + bool done = false; - if (i == 36 && j == 32) { - id[32] = '\n'; - id[33] = 0; + do { + char line[LINE_MAX]; + unsigned i; - log_info("Initializing machine ID from KVM UUID"); - return 0; + 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 (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; + } + } + + } while (!done); + + fclose(f); } } |