]> git.ipfire.org Git - thirdparty/systemd.git/blobdiff - src/core/machine-id-setup.c
util-lib: split out IO related calls to io-util.[ch]
[thirdparty/systemd.git] / src / core / machine-id-setup.c
index 8e26362546ba675510336b53743504a2a585c054..c72892b3438e3d355ff3558d0a4f15a1e5018f98 100644 (file)
   along with systemd; If not, see <http://www.gnu.org/licenses/>.
 ***/
 
-#include <unistd.h>
-#include <stdio.h>
 #include <errno.h>
-#include <string.h>
 #include <fcntl.h>
+#include <sched.h>
+#include <stdio.h>
+#include <string.h>
 #include <sys/mount.h>
+#include <unistd.h>
 
-#include "systemd/sd-id128.h"
+#include "sd-id128.h"
 
+#include "fd-util.h"
+#include "fileio.h"
+#include "io-util.h"
+#include "log.h"
 #include "machine-id-setup.h"
 #include "macro.h"
-#include "util.h"
 #include "mkdir.h"
-#include "log.h"
-#include "virt.h"
-#include "fileio.h"
 #include "path-util.h"
 #include "process-util.h"
+#include "string-util.h"
+#include "util.h"
+#include "virt.h"
 
 static int shorten_uuid(char destination[34], const char source[36]) {
         unsigned i, j;
@@ -108,7 +112,7 @@ static int generate_machine_id(char id[34], const char *root) {
         unsigned char *p;
         sd_id128_t buf;
         char  *q;
-        const char *vm_id, *dbus_machine_id;
+        const char *dbus_machine_id;
 
         assert(id);
 
@@ -133,8 +137,8 @@ static int generate_machine_id(char id[34], const char *root) {
                 /* If that didn't work, 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) {
+
+                if (detect_container() > 0) {
                         _cleanup_free_ char *e = NULL;
 
                         r = getenv_for_pid(1, "container_uuid", &e);
@@ -146,26 +150,24 @@ static int generate_machine_id(char id[34], const char *root) {
                                 }
                         }
 
-                } else {
+                } else if (detect_vm() == VIRTUALIZATION_KVM) {
+
                         /* If we are not running in a container, see if we are
                          * running in qemu/kvm and a machine ID was passed in
                          * via -uuid on the qemu/kvm command line */
 
-                        r = detect_vm(&vm_id);
-                        if (r > 0 && streq(vm_id, "kvm")) {
-                                char uuid[36];
+                        char uuid[36];
 
-                                fd = open("/sys/class/dmi/id/product_uuid", O_RDONLY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW);
-                                if (fd >= 0) {
-                                        r = loop_read_exact(fd, uuid, 36, false);
-                                        safe_close(fd);
+                        fd = open("/sys/class/dmi/id/product_uuid", O_RDONLY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW);
+                        if (fd >= 0) {
+                                r = loop_read_exact(fd, uuid, 36, false);
+                                safe_close(fd);
 
+                                if (r >= 0) {
+                                        r = shorten_uuid(id, uuid);
                                         if (r >= 0) {
-                                                r = shorten_uuid(id, uuid);
-                                                if (r >= 0) {
-                                                        log_info("Initializing machine ID from KVM UUID.");
-                                                        return 0;
-                                                }
+                                                log_info("Initializing machine ID from KVM UUID.");
+                                                return 0;
                                         }
                                 }
                         }
@@ -325,7 +327,7 @@ int machine_id_commit(const char *root) {
         fd = safe_close(fd);
 
         /* Store current mount namespace */
-        r = namespace_open(0, NULL, &initial_mntns_fd, NULL, NULL);
+        r = namespace_open(0, NULL, &initial_mntns_fd, NULL, NULL, NULL);
         if (r < 0)
                 return log_error_errno(r, "Can't fetch current mount namespace: %m");
 
@@ -351,7 +353,7 @@ int machine_id_commit(const char *root) {
         fd = safe_close(fd);
 
         /* Return to initial namespace and proceed a lazy tmpfs unmount */
-        r = namespace_enter(-1, initial_mntns_fd, -1, -1);
+        r = namespace_enter(-1, initial_mntns_fd, -1, -1, -1);
         if (r < 0)
                 return log_warning_errno(r, "Failed to switch back to initial mount namespace: %m.\nWe'll keep transient %s file until next reboot.", etc_machine_id);