]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/machine-id-setup.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
10 #include "alloc-util.h"
12 #include "creds-util.h"
14 #include "id128-util.h"
17 #include "machine-id-setup.h"
20 #include "mount-util.h"
21 #include "mountpoint-util.h"
22 #include "namespace-util.h"
23 #include "path-util.h"
24 #include "process-util.h"
25 #include "stat-util.h"
26 #include "string-util.h"
27 #include "sync-util.h"
28 #include "umask-util.h"
31 static int acquire_machine_id_from_credential ( sd_id128_t
* ret
) {
32 _cleanup_free_
char * buf
= NULL
;
35 r
= read_credential_with_decryption ( "system.machine_id" , ( void **) & buf
, /* ret_size= */ NULL
);
37 return log_warning_errno ( r
, "Failed to read system.machine_id credential, ignoring: %m" );
38 if ( r
== 0 ) /* not found */
41 r
= sd_id128_from_string ( buf
, ret
);
43 return log_warning_errno ( r
, "Failed to parse system.machine_id credential, ignoring: %m" );
45 log_info ( "Initializing machine ID from credential." );
49 static int generate_machine_id ( const char * root
, sd_id128_t
* ret
) {
50 _cleanup_close_
int fd
= - EBADF
;
55 /* First, try reading the D-Bus machine id, unless it is a symlink */
56 fd
= chase_and_open ( "/var/lib/dbus/machine-id" , root
, CHASE_PREFIX_ROOT
| CHASE_NOFOLLOW
, O_RDONLY
| O_CLOEXEC
| O_NOCTTY
, NULL
);
57 if ( fd
>= 0 && id128_read_fd ( fd
, ID128_FORMAT_PLAIN
| ID128_REFUSE_NULL
, ret
) >= 0 ) {
58 log_info ( "Initializing machine ID from D-Bus machine ID." );
62 if ( isempty ( root
) && running_in_chroot () <= 0 ) {
63 /* Let's use a system credential for the machine ID if we can */
64 r
= acquire_machine_id_from_credential ( ret
);
68 /* If that didn't work, see if we are running in a container,
69 * and a machine ID was passed in via $container_uuid the way
70 * libvirt/LXC does it */
72 if ( detect_container () > 0 ) {
73 _cleanup_free_
char * e
= NULL
;
75 if ( getenv_for_pid ( 1 , "container_uuid" , & e
) > 0 &&
76 sd_id128_from_string ( e
, ret
) >= 0 ) {
77 log_info ( "Initializing machine ID from container UUID." );
81 } else if ( IN_SET ( detect_vm (), VIRTUALIZATION_KVM
, VIRTUALIZATION_AMAZON
, VIRTUALIZATION_QEMU
)) {
83 /* If we are not running in a container, see if we are running in a VM that provides
84 * a system UUID via the SMBIOS/DMI interfaces. Such environments include QEMU/KVM
85 * with the -uuid on the qemu command line or the Amazon EC2 Nitro hypervisor. */
87 if ( id128_get_product ( ret
) >= 0 ) {
88 log_info ( "Initializing machine ID from VM UUID." );
94 /* If that didn't work, generate a random machine id */
95 r
= sd_id128_randomize ( ret
);
97 return log_error_errno ( r
, "Failed to generate randomized machine ID: %m" );
99 log_info ( "Initializing machine ID from random generator." );
103 int machine_id_setup ( const char * root
, bool force_transient
, sd_id128_t machine_id
, sd_id128_t
* ret
) {
104 const char * etc_machine_id
, * run_machine_id
;
105 _cleanup_close_
int fd
= - EBADF
;
109 etc_machine_id
= prefix_roota ( root
, "/etc/machine-id" );
112 /* We create this 0444, to indicate that this isn't really
113 * something you should ever modify. Of course, since the file
114 * will be owned by root it doesn't matter much, but maybe
117 ( void ) mkdir_parents ( etc_machine_id
, 0755 );
118 fd
= open ( etc_machine_id
, O_RDWR
| O_CREAT
| O_CLOEXEC
| O_NOCTTY
, 0444 );
120 int old_errno
= errno
;
122 fd
= open ( etc_machine_id
, O_RDONLY
| O_CLOEXEC
| O_NOCTTY
);
124 if ( old_errno
== EROFS
&& errno
== ENOENT
)
125 return log_error_errno ( errno
,
126 "System cannot boot: Missing /etc/machine-id and /etc is mounted read-only. \n "
127 "Booting up is supported only when: \n "
128 "1) /etc/machine-id exists and is populated. \n "
129 "2) /etc/machine-id exists and is empty. \n "
130 "3) /etc/machine-id is missing and /etc is writable. \n " );
132 return log_error_errno ( errno
, "Cannot open %s: %m" , etc_machine_id
);
140 /* A we got a valid machine ID argument, that's what counts */
141 if ( sd_id128_is_null ( machine_id
)) {
143 /* Try to read any existing machine ID */
144 if ( id128_read_fd ( fd
, ID128_FORMAT_PLAIN
, ret
) >= 0 )
147 /* Hmm, so, the id currently stored is not useful, then let's generate one */
148 r
= generate_machine_id ( root
, & machine_id
);
154 if ( lseek ( fd
, 0 , SEEK_SET
) < 0 )
155 return log_error_errno ( errno
, "Failed to seek %s: %m" , etc_machine_id
);
157 if ( ftruncate ( fd
, 0 ) < 0 )
158 return log_error_errno ( errno
, "Failed to truncate %s: %m" , etc_machine_id
);
160 /* If the caller requested a transient machine-id, write the string "uninitialized\n" to
161 * disk and overmount it with a transient file.
163 * Otherwise write the machine-id directly to disk. */
164 if ( force_transient
) {
165 r
= loop_write ( fd
, "uninitialized \n " , SIZE_MAX
);
167 return log_error_errno ( r
, "Failed to write uninitialized %s: %m" , etc_machine_id
);
171 return log_error_errno ( r
, "Failed to sync %s: %m" , etc_machine_id
);
173 r
= id128_write_fd ( fd
, ID128_FORMAT_PLAIN
| ID128_SYNC_ON_WRITE
, machine_id
);
175 return log_error_errno ( r
, "Failed to write %s: %m" , etc_machine_id
);
183 /* Hmm, we couldn't or shouldn't write the machine-id to /etc?
184 * So let's write it to /run/machine-id as a replacement */
186 run_machine_id
= prefix_roota ( root
, "/run/machine-id" );
189 r
= id128_write ( run_machine_id
, ID128_FORMAT_PLAIN
, machine_id
);
191 ( void ) unlink ( run_machine_id
);
192 return log_error_errno ( r
, "Cannot write %s: %m" , run_machine_id
);
195 /* And now, let's mount it over */
196 r
= mount_follow_verbose ( LOG_ERR
, run_machine_id
, etc_machine_id
, NULL
, MS_BIND
, NULL
);
198 ( void ) unlink ( run_machine_id
);
202 log_full ( force_transient
? LOG_DEBUG
: LOG_INFO
, "Installed transient %s file." , etc_machine_id
);
204 /* Mark the mount read-only */
205 r
= mount_follow_verbose ( LOG_WARNING
, NULL
, etc_machine_id
, NULL
, MS_BIND
| MS_RDONLY
| MS_REMOUNT
, NULL
);
216 int machine_id_commit ( const char * root
) {
217 _cleanup_close_
int fd
= - EBADF
, initial_mntns_fd
= - EBADF
;
218 const char * etc_machine_id
;
222 /* Before doing anything, sync everything to ensure any changes by first-boot units are persisted.
224 * First, explicitly sync the file systems we care about and check if it worked. */
225 FOREACH_STRING ( sync_path
, "/etc/" , "/var/" ) {
226 r
= syncfs_path ( AT_FDCWD
, sync_path
);
228 return log_error_errno ( r
, "Cannot sync %s: %m" , sync_path
);
231 /* Afterwards, sync() the rest too, but we can't check the return value for these. */
234 /* Replaces a tmpfs bind mount of /etc/machine-id by a proper file, atomically. For this, the umount is removed
235 * in a mount namespace, a new file is created at the right place. Afterwards the mount is also removed in the
236 * original mount namespace, thus revealing the file that was just created. */
238 etc_machine_id
= prefix_roota ( root
, "/etc/machine-id" );
240 r
= path_is_mount_point ( etc_machine_id
, NULL
, 0 );
242 return log_error_errno ( r
, "Failed to determine whether %s is a mount point: %m" , etc_machine_id
);
244 log_debug ( "%s is not a mount point. Nothing to do." , etc_machine_id
);
248 /* Read existing machine-id */
249 fd
= open ( etc_machine_id
, O_RDONLY
| O_CLOEXEC
| O_NOCTTY
);
251 return log_error_errno ( errno
, "Cannot open %s: %m" , etc_machine_id
);
253 r
= fd_is_temporary_fs ( fd
);
255 return log_error_errno ( r
, "Failed to determine whether %s is on a temporary file system: %m" , etc_machine_id
);
257 return log_error_errno ( SYNTHETIC_ERRNO ( EROFS
),
258 "%s is not on a temporary file system." ,
261 r
= id128_read_fd ( fd
, ID128_FORMAT_PLAIN
, & id
);
263 return log_error_errno ( r
, "We didn't find a valid machine ID in %s: %m" , etc_machine_id
);
267 /* Store current mount namespace */
268 r
= namespace_open ( 0 , NULL
, & initial_mntns_fd
, NULL
, NULL
, NULL
);
270 return log_error_errno ( r
, "Can't fetch current mount namespace: %m" );
272 /* Switch to a new mount namespace, isolate ourself and unmount etc_machine_id in our new namespace */
273 r
= detach_mount_namespace ();
275 return log_error_errno ( r
, "Failed to set up new mount namespace: %m" );
277 r
= umount_verbose ( LOG_ERR
, etc_machine_id
, 0 );
281 /* Update a persistent version of etc_machine_id */
282 r
= id128_write ( etc_machine_id
, ID128_FORMAT_PLAIN
| ID128_SYNC_ON_WRITE
, id
);
284 return log_error_errno ( r
, "Cannot write %s. This is mandatory to get a persistent machine ID: %m" , etc_machine_id
);
286 /* Return to initial namespace and proceed a lazy tmpfs unmount */
287 r
= namespace_enter (- 1 , initial_mntns_fd
, - 1 , - 1 , - 1 );
289 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
);
291 if ( umount2 ( etc_machine_id
, MNT_DETACH
) < 0 )
292 return log_warning_errno ( errno
, "Failed to unmount transient %s file: %m. \n We keep that mount until next reboot." , etc_machine_id
);