]> git.ipfire.org Git - thirdparty/systemd.git/blobdiff - src/core/machine-id-setup.c
util-lib: move a number of fs operations into fs-util.[ch]
[thirdparty/systemd.git] / src / core / machine-id-setup.c
index 2ffb2a7aa6f6d357f47b011d8594063ef53833d9..921f0710681b8ac3f765f30cb1db60c9a7ed0df8 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 "fs-util.h"
+#include "hexdecoct.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 "mount-util.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;
@@ -107,7 +115,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);
 
@@ -132,8 +140,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);
@@ -145,26 +153,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;
                                         }
                                 }
                         }
@@ -259,7 +265,7 @@ int machine_id_setup(const char *root) {
          * /run/machine-id as a replacement */
 
         RUN_WITH_UMASK(0022) {
-                r = write_string_file(run_machine_id, id);
+                r = write_string_file(run_machine_id, id, WRITE_STRING_FILE_CREATE);
         }
         if (r < 0) {
                 (void) unlink(run_machine_id);
@@ -296,7 +302,7 @@ int machine_id_commit(const char *root) {
                 etc_machine_id = path_kill_slashes(x);
         }
 
-        r = path_is_mount_point(etc_machine_id, false);
+        r = path_is_mount_point(etc_machine_id, 0);
         if (r < 0)
                 return log_error_errno(r, "Failed to determine whether %s is a mount point: %m", etc_machine_id);
         if (r == 0) {
@@ -324,7 +330,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");
 
@@ -350,7 +356,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);