]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/core/machine-id-setup.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
10 #include "alloc-util.h"
13 #include "id128-util.h"
15 #include "machine-id-setup.h"
18 #include "mountpoint-util.h"
19 #include "namespace-util.h"
20 #include "path-util.h"
21 #include "process-util.h"
22 #include "stat-util.h"
23 #include "string-util.h"
24 #include "umask-util.h"
28 static int generate_machine_id ( const char * root
, sd_id128_t
* ret
) {
29 const char * dbus_machine_id
;
30 _cleanup_close_
int fd
= - 1 ;
35 /* First, try reading the D-Bus machine id, unless it is a symlink */
36 dbus_machine_id
= prefix_roota ( root
, "/var/lib/dbus/machine-id" );
37 fd
= open ( dbus_machine_id
, O_RDONLY
| O_CLOEXEC
| O_NOCTTY
| O_NOFOLLOW
);
39 if ( id128_read_fd ( fd
, ID128_PLAIN
, ret
) >= 0 ) {
40 log_info ( "Initializing machine ID from D-Bus machine ID." );
47 if ( isempty ( root
) && running_in_chroot () <= 0 ) {
48 /* If that didn't work, see if we are running in a container,
49 * and a machine ID was passed in via $container_uuid the way
50 * libvirt/LXC does it */
52 if ( detect_container () > 0 ) {
53 _cleanup_free_
char * e
= NULL
;
55 if ( getenv_for_pid ( 1 , "container_uuid" , & e
) > 0 &&
56 sd_id128_from_string ( e
, ret
) >= 0 ) {
57 log_info ( "Initializing machine ID from container UUID." );
61 } else if ( detect_vm () == VIRTUALIZATION_KVM
) {
63 /* If we are not running in a container, see if we are
64 * running in qemu/kvm and a machine ID was passed in
65 * via -uuid on the qemu/kvm command line */
67 if ( id128_read ( "/sys/class/dmi/id/product_uuid" , ID128_UUID
, ret
) >= 0 ) {
68 log_info ( "Initializing machine ID from KVM UUID." );
71 /* on POWER, it's exported here instead */
72 if ( id128_read ( "/sys/firmware/devicetree/base/vm,uuid" , ID128_UUID
, ret
) >= 0 ) {
73 log_info ( "Initializing machine ID from KVM UUID." );
79 /* If that didn't work, generate a random machine id */
80 r
= sd_id128_randomize ( ret
);
82 return log_error_errno ( r
, "Failed to generate randomized machine ID: %m" );
84 log_info ( "Initializing machine ID from random generator." );
88 int machine_id_setup ( const char * root
, sd_id128_t machine_id
, sd_id128_t
* ret
) {
89 const char * etc_machine_id
, * run_machine_id
;
90 _cleanup_close_
int fd
= - 1 ;
94 etc_machine_id
= prefix_roota ( root
, "/etc/machine-id" );
96 RUN_WITH_UMASK ( 0000 ) {
97 /* We create this 0444, to indicate that this isn't really
98 * something you should ever modify. Of course, since the file
99 * will be owned by root it doesn't matter much, but maybe
102 ( void ) mkdir_parents ( etc_machine_id
, 0755 );
103 fd
= open ( etc_machine_id
, O_RDWR
| O_CREAT
| O_CLOEXEC
| O_NOCTTY
, 0444 );
105 int old_errno
= errno
;
107 fd
= open ( etc_machine_id
, O_RDONLY
| O_CLOEXEC
| O_NOCTTY
);
109 if ( old_errno
== EROFS
&& errno
== ENOENT
)
110 return log_error_errno ( errno
,
111 "System cannot boot: Missing /etc/machine-id and /etc is mounted read-only. \n "
112 "Booting up is supported only when: \n "
113 "1) /etc/machine-id exists and is populated. \n "
114 "2) /etc/machine-id exists and is empty. \n "
115 "3) /etc/machine-id is missing and /etc is writable. \n " );
117 return log_error_errno ( errno
, "Cannot open %s: %m" , etc_machine_id
);
125 /* A we got a valid machine ID argument, that's what counts */
126 if ( sd_id128_is_null ( machine_id
)) {
128 /* Try to read any existing machine ID */
129 if ( id128_read_fd ( fd
, ID128_PLAIN
, ret
) >= 0 )
132 /* Hmm, so, the id currently stored is not useful, then let's generate one */
133 r
= generate_machine_id ( root
, & machine_id
);
139 if ( lseek ( fd
, 0 , SEEK_SET
) == ( off_t
) - 1 )
140 return log_error_errno ( errno
, "Failed to seek %s: %m" , etc_machine_id
);
142 if ( ftruncate ( fd
, 0 ) < 0 )
143 return log_error_errno ( errno
, "Failed to truncate %s: %m" , etc_machine_id
);
145 if ( id128_write_fd ( fd
, ID128_PLAIN
, machine_id
, true ) >= 0 )
151 /* Hmm, we couldn't write it? So let's write it to /run/machine-id as a replacement */
153 run_machine_id
= prefix_roota ( root
, "/run/machine-id" );
156 r
= id128_write ( run_machine_id
, ID128_PLAIN
, machine_id
, false );
158 ( void ) unlink ( run_machine_id
);
159 return log_error_errno ( r
, "Cannot write %s: %m" , run_machine_id
);
162 /* And now, let's mount it over */
163 if ( mount ( run_machine_id
, etc_machine_id
, NULL
, MS_BIND
, NULL
) < 0 ) {
164 ( void ) unlink_noerrno ( run_machine_id
);
165 return log_error_errno ( errno
, "Failed to mount %s: %m" , etc_machine_id
);
168 log_info ( "Installed transient %s file." , etc_machine_id
);
170 /* Mark the mount read-only */
171 if ( mount ( NULL
, etc_machine_id
, NULL
, MS_BIND
| MS_RDONLY
| MS_REMOUNT
, NULL
) < 0 )
172 log_warning_errno ( errno
, "Failed to make transient %s read-only, ignoring: %m" , etc_machine_id
);
181 int machine_id_commit ( const char * root
) {
182 _cleanup_close_
int fd
= - 1 , initial_mntns_fd
= - 1 ;
183 const char * etc_machine_id
;
187 /* Replaces a tmpfs bind mount of /etc/machine-id by a proper file, atomically. For this, the umount is removed
188 * in a mount namespace, a new file is created at the right place. Afterwards the mount is also removed in the
189 * original mount namespace, thus revealing the file that was just created. */
191 etc_machine_id
= prefix_roota ( root
, "/etc/machine-id" );
193 r
= path_is_mount_point ( etc_machine_id
, NULL
, 0 );
195 return log_error_errno ( r
, "Failed to determine whether %s is a mount point: %m" , etc_machine_id
);
197 log_debug ( "%s is not a mount point. Nothing to do." , etc_machine_id
);
201 /* Read existing machine-id */
202 fd
= open ( etc_machine_id
, O_RDONLY
| O_CLOEXEC
| O_NOCTTY
);
204 return log_error_errno ( errno
, "Cannot open %s: %m" , etc_machine_id
);
206 r
= fd_is_temporary_fs ( fd
);
208 return log_error_errno ( r
, "Failed to determine whether %s is on a temporary file system: %m" , etc_machine_id
);
210 return log_error_errno ( SYNTHETIC_ERRNO ( EROFS
),
211 "%s is not on a temporary file system." ,
214 r
= id128_read_fd ( fd
, ID128_PLAIN
, & id
);
216 return log_error_errno ( r
, "We didn't find a valid machine ID in %s: %m" , etc_machine_id
);
220 /* Store current mount namespace */
221 r
= namespace_open ( 0 , NULL
, & initial_mntns_fd
, NULL
, NULL
, NULL
);
223 return log_error_errno ( r
, "Can't fetch current mount namespace: %m" );
225 /* Switch to a new mount namespace, isolate ourself and unmount etc_machine_id in our new namespace */
226 r
= detach_mount_namespace ();
228 return log_error_errno ( r
, "Failed to set up new mount namespace: %m" );
230 if ( umount ( etc_machine_id
) < 0 )
231 return log_error_errno ( errno
, "Failed to unmount transient %s file in our private namespace: %m" , etc_machine_id
);
233 /* Update a persistent version of etc_machine_id */
234 r
= id128_write ( etc_machine_id
, ID128_PLAIN
, id
, true );
236 return log_error_errno ( r
, "Cannot write %s. This is mandatory to get a persistent machine ID: %m" , etc_machine_id
);
238 /* Return to initial namespace and proceed a lazy tmpfs unmount */
239 r
= namespace_enter (- 1 , initial_mntns_fd
, - 1 , - 1 , - 1 );
241 return log_warning_errno ( r
, "Failed to switch back to initial mount namespace: %m. \n We'll keep transient %s file until next reboot." , etc_machine_id
);
243 if ( umount2 ( etc_machine_id
, MNT_DETACH
) < 0 )
244 return log_warning_errno ( errno
, "Failed to unmount transient %s file: %m. \n We keep that mount until next reboot." , etc_machine_id
);