]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/core/machine-id-setup.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
27 #include <sys/mount.h>
32 #include "alloc-util.h"
36 #include "hexdecoct.h"
39 #include "machine-id-setup.h"
42 #include "mount-util.h"
43 #include "path-util.h"
44 #include "process-util.h"
45 #include "stat-util.h"
46 #include "string-util.h"
47 #include "umask-util.h"
51 static int shorten_uuid ( char destination
[ 34 ], const char source
[ 36 ]) {
57 /* Converts a UUID into a machine ID, by lowercasing it and
58 * removing dashes. Validates everything. */
60 for ( i
= 0 , j
= 0 ; i
< 36 && j
< 32 ; i
++) {
63 t
= unhexchar ( source
[ i
]);
67 destination
[ j
++] = hexchar ( t
);
70 if ( i
!= 36 || j
!= 32 )
73 destination
[ 32 ] = ' \n ' ;
78 static int read_machine_id ( int fd
, char id
[ 34 ]) {
79 char id_to_validate
[ 34 ];
85 /* Reads a machine ID from a file, validates it, and returns
86 * it. The returned ID ends in a newline. */
88 r
= loop_read_exact ( fd
, id_to_validate
, 33 , false );
92 if ( id_to_validate
[ 32 ] != ' \n ' )
95 id_to_validate
[ 32 ] = 0 ;
97 if (! id128_is_valid ( id_to_validate
))
100 memcpy ( id
, id_to_validate
, 32 );
106 static int write_machine_id ( int fd
, char id
[ 34 ]) {
110 if ( lseek ( fd
, 0 , SEEK_SET
) < 0 )
113 return loop_write ( fd
, id
, 33 , false );
116 static int generate_machine_id ( char id
[ 34 ], const char * root
) {
121 const char * dbus_machine_id
;
126 dbus_machine_id
= "/var/lib/dbus/machine-id" ;
128 dbus_machine_id
= strjoina ( root
, "/var/lib/dbus/machine-id" );
130 /* First, try reading the D-Bus machine id, unless it is a symlink */
131 fd
= open ( dbus_machine_id
, O_RDONLY
| O_CLOEXEC
| O_NOCTTY
| O_NOFOLLOW
);
133 r
= read_machine_id ( fd
, id
);
137 log_info ( "Initializing machine ID from D-Bus machine ID." );
143 /* If that didn't work, see if we are running in a container,
144 * and a machine ID was passed in via $container_uuid the way
145 * libvirt/LXC does it */
147 if ( detect_container () > 0 ) {
148 _cleanup_free_
char * e
= NULL
;
150 r
= getenv_for_pid ( 1 , "container_uuid" , & e
);
152 r
= shorten_uuid ( id
, e
);
154 log_info ( "Initializing machine ID from container UUID." );
159 } else if ( detect_vm () == VIRTUALIZATION_KVM
) {
161 /* If we are not running in a container, see if we are
162 * running in qemu/kvm and a machine ID was passed in
163 * via -uuid on the qemu/kvm command line */
167 fd
= open ( "/sys/class/dmi/id/product_uuid" , O_RDONLY
| O_CLOEXEC
| O_NOCTTY
| O_NOFOLLOW
);
169 r
= loop_read_exact ( fd
, uuid
, 36 , false );
173 r
= shorten_uuid ( id
, uuid
);
175 log_info ( "Initializing machine ID from KVM UUID." );
183 /* If that didn't work, generate a random machine id */
184 r
= sd_id128_randomize (& buf
);
186 return log_error_errno ( r
, "Failed to open /dev/urandom: %m" );
188 for ( p
= buf
. bytes
, q
= id
; p
< buf
. bytes
+ sizeof ( buf
); p
++, q
+= 2 ) {
189 q
[ 0 ] = hexchar (* p
>> 4 );
190 q
[ 1 ] = hexchar (* p
& 15 );
196 log_info ( "Initializing machine ID from random generator." );
201 int machine_id_setup ( const char * root
, sd_id128_t machine_id
) {
202 const char * etc_machine_id
, * run_machine_id
;
203 _cleanup_close_
int fd
= - 1 ;
204 bool writable
= true ;
205 char id
[ 34 ]; /* 32 + \n + \0 */
209 etc_machine_id
= "/etc/machine-id" ;
210 run_machine_id
= "/run/machine-id" ;
214 x
= strjoina ( root
, "/etc/machine-id" );
215 etc_machine_id
= path_kill_slashes ( x
);
217 x
= strjoina ( root
, "/run/machine-id" );
218 run_machine_id
= path_kill_slashes ( x
);
221 RUN_WITH_UMASK ( 0000 ) {
222 /* We create this 0444, to indicate that this isn't really
223 * something you should ever modify. Of course, since the file
224 * will be owned by root it doesn't matter much, but maybe
227 mkdir_parents ( etc_machine_id
, 0755 );
228 fd
= open ( etc_machine_id
, O_RDWR
| O_CREAT
| O_CLOEXEC
| O_NOCTTY
, 0444 );
230 int old_errno
= errno
;
232 fd
= open ( etc_machine_id
, O_RDONLY
| O_CLOEXEC
| O_NOCTTY
);
234 if ( old_errno
== EROFS
&& errno
== ENOENT
)
235 log_error_errno ( errno
,
236 "System cannot boot: Missing /etc/machine-id and /etc is mounted read-only. \n "
237 "Booting up is supported only when: \n "
238 "1) /etc/machine-id exists and is populated. \n "
239 "2) /etc/machine-id exists and is empty. \n "
240 "3) /etc/machine-id is missing and /etc is writable. \n " );
242 log_error_errno ( errno
, "Cannot open %s: %m" , etc_machine_id
);
251 /* A machine id argument overrides all other machined-ids */
252 if (! sd_id128_is_null ( machine_id
)) {
253 sd_id128_to_string ( machine_id
, id
);
257 if ( read_machine_id ( fd
, id
) >= 0 )
260 /* Hmm, so, the id currently stored is not useful, then let's
263 r
= generate_machine_id ( id
, root
);
269 if ( write_machine_id ( fd
, id
) >= 0 )
274 /* Hmm, we couldn't write it? So let's write it to
275 * /run/machine-id as a replacement */
277 RUN_WITH_UMASK ( 0022 ) {
278 r
= write_string_file ( run_machine_id
, id
, WRITE_STRING_FILE_CREATE
);
281 ( void ) unlink ( run_machine_id
);
282 return log_error_errno ( r
, "Cannot write %s: %m" , run_machine_id
);
285 /* And now, let's mount it over */
286 if ( mount ( run_machine_id
, etc_machine_id
, NULL
, MS_BIND
, NULL
) < 0 ) {
287 ( void ) unlink_noerrno ( run_machine_id
);
288 return log_error_errno ( errno
, "Failed to mount %s: %m" , etc_machine_id
);
291 log_info ( "Installed transient %s file." , etc_machine_id
);
293 /* Mark the mount read-only */
294 if ( mount ( NULL
, etc_machine_id
, NULL
, MS_BIND
| MS_RDONLY
| MS_REMOUNT
, NULL
) < 0 )
295 log_warning_errno ( errno
, "Failed to make transient %s read-only: %m" , etc_machine_id
);
300 int machine_id_commit ( const char * root
) {
301 _cleanup_close_
int fd
= - 1 , initial_mntns_fd
= - 1 ;
302 const char * etc_machine_id
;
303 char id
[ 34 ]; /* 32 + \n + \0 */
307 etc_machine_id
= "/etc/machine-id" ;
311 x
= strjoina ( root
, "/etc/machine-id" );
312 etc_machine_id
= path_kill_slashes ( x
);
315 r
= path_is_mount_point ( etc_machine_id
, 0 );
317 return log_error_errno ( r
, "Failed to determine whether %s is a mount point: %m" , etc_machine_id
);
319 log_debug ( "%s is is not a mount point. Nothing to do." , etc_machine_id
);
323 /* Read existing machine-id */
324 fd
= open ( etc_machine_id
, O_RDONLY
| O_CLOEXEC
| O_NOCTTY
);
326 return log_error_errno ( errno
, "Cannot open %s: %m" , etc_machine_id
);
328 r
= read_machine_id ( fd
, id
);
330 return log_error_errno ( r
, "We didn't find a valid machine ID in %s." , etc_machine_id
);
332 r
= fd_is_temporary_fs ( fd
);
334 return log_error_errno ( r
, "Failed to determine whether %s is on a temporary file system: %m" , etc_machine_id
);
336 log_error ( "%s is not on a temporary file system." , etc_machine_id
);
342 /* Store current mount namespace */
343 r
= namespace_open ( 0 , NULL
, & initial_mntns_fd
, NULL
, NULL
, NULL
);
345 return log_error_errno ( r
, "Can't fetch current mount namespace: %m" );
347 /* Switch to a new mount namespace, isolate ourself and unmount etc_machine_id in our new namespace */
348 if ( unshare ( CLONE_NEWNS
) < 0 )
349 return log_error_errno ( errno
, "Failed to enter new namespace: %m" );
351 if ( mount ( NULL
, "/" , NULL
, MS_SLAVE
| MS_REC
, NULL
) < 0 )
352 return log_error_errno ( errno
, "Couldn't make-rslave / mountpoint in our private namespace: %m" );
354 if ( umount ( etc_machine_id
) < 0 )
355 return log_error_errno ( errno
, "Failed to unmount transient %s file in our private namespace: %m" , etc_machine_id
);
357 /* Update a persistent version of etc_machine_id */
358 fd
= open ( etc_machine_id
, O_RDWR
| O_CREAT
| O_CLOEXEC
| O_NOCTTY
, 0444 );
360 return log_error_errno ( errno
, "Cannot open for writing %s. This is mandatory to get a persistent machine-id: %m" , etc_machine_id
);
362 r
= write_machine_id ( fd
, id
);
364 return log_error_errno ( r
, "Cannot write %s: %m" , etc_machine_id
);
368 /* Return to initial namespace and proceed a lazy tmpfs unmount */
369 r
= namespace_enter (- 1 , initial_mntns_fd
, - 1 , - 1 , - 1 );
371 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
);
373 if ( umount2 ( etc_machine_id
, MNT_DETACH
) < 0 )
374 return log_warning_errno ( errno
, "Failed to unmount transient %s file: %m. \n We keep that mount until next reboot." , etc_machine_id
);