2011-03-04 23:53:19 +03:00
/***
This file is part of systemd .
Copyright 2010 Lennart Poettering
systemd is free software ; you can redistribute it and / or modify it
2012-04-12 02:20:58 +04:00
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation ; either version 2.1 of the License , or
2011-03-04 23:53:19 +03:00
( at your option ) any later version .
systemd is distributed in the hope that it will be useful , but
WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the GNU
2012-04-12 02:20:58 +04:00
Lesser General Public License for more details .
2011-03-04 23:53:19 +03:00
2012-04-12 02:20:58 +04:00
You should have received a copy of the GNU Lesser General Public License
2011-03-04 23:53:19 +03:00
along with systemd ; If not , see < http : //www.gnu.org/licenses/>.
* * */
# include <fcntl.h>
2015-09-30 22:50:22 +03:00
# include <sched.h>
2011-03-04 23:53:19 +03:00
# include <sys/mount.h>
2015-09-30 22:50:22 +03:00
# include <unistd.h>
2011-03-04 23:53:19 +03:00
2015-09-30 22:50:22 +03:00
# include "sd-id128.h"
2012-01-05 19:01:58 +04:00
2015-10-27 05:01:06 +03:00
# include "alloc-util.h"
2015-10-25 15:14:12 +03:00
# include "fd-util.h"
2015-10-26 23:16:26 +03:00
# include "fs-util.h"
2016-07-21 18:57:57 +03:00
# include "id128-util.h"
2015-09-30 22:50:22 +03:00
# include "log.h"
2015-10-25 15:14:12 +03:00
# include "machine-id-setup.h"
2011-03-04 23:53:19 +03:00
# include "macro.h"
2012-04-10 23:54:31 +04:00
# include "mkdir.h"
2015-10-26 20:44:13 +03:00
# include "mount-util.h"
2014-03-14 16:43:21 +04:00
# include "path-util.h"
2015-04-10 20:10:00 +03:00
# include "process-util.h"
2015-10-27 00:01:44 +03:00
# include "stat-util.h"
2015-10-24 23:58:24 +03:00
# include "string-util.h"
2015-10-27 01:20:41 +03:00
# include "umask-util.h"
2015-09-30 22:50:22 +03:00
# include "util.h"
# include "virt.h"
2011-07-25 21:31:07 +04:00
2016-07-22 13:12:27 +03:00
static int generate_machine_id ( const char * root , sd_id128_t * ret ) {
2015-09-07 14:42:47 +03:00
const char * dbus_machine_id ;
2016-07-22 13:12:27 +03:00
_cleanup_close_ int fd = - 1 ;
int r ;
2011-03-04 23:53:19 +03:00
2016-07-22 13:12:27 +03:00
assert ( ret ) ;
2014-03-14 08:43:04 +04:00
2011-03-04 23:53:19 +03:00
/* First, try reading the D-Bus machine id, unless it is a symlink */
2016-07-22 13:12:27 +03:00
dbus_machine_id = prefix_roota ( root , " /var/lib/dbus/machine-id " ) ;
2014-03-14 08:43:04 +04:00
fd = open ( dbus_machine_id , O_RDONLY | O_CLOEXEC | O_NOCTTY | O_NOFOLLOW ) ;
2011-07-25 21:31:07 +04:00
if ( fd > = 0 ) {
2016-07-22 13:12:27 +03:00
if ( id128_read_fd ( fd , ID128_PLAIN , ret ) > = 0 ) {
2015-03-10 20:58:10 +03:00
log_info ( " Initializing machine ID from D-Bus machine ID. " ) ;
return 0 ;
2011-03-04 23:53:19 +03:00
}
2016-07-22 13:12:27 +03:00
fd = safe_close ( fd ) ;
2011-03-04 23:53:19 +03:00
}
2014-08-20 15:49:39 +04:00
if ( isempty ( 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 */
2015-09-07 14:42:47 +03:00
if ( detect_container ( ) > 0 ) {
2014-08-20 15:49:39 +04:00
_cleanup_free_ char * e = NULL ;
2014-04-28 20:11:40 +04:00
2016-07-22 13:12:27 +03:00
if ( getenv_for_pid ( 1 , " container_uuid " , & e ) > 0 & &
sd_id128_from_string ( e , ret ) > = 0 ) {
log_info ( " Initializing machine ID from container UUID. " ) ;
return 0 ;
2014-04-28 20:11:40 +04:00
}
2014-08-20 15:49:39 +04:00
2015-09-07 14:42:47 +03:00
} else if ( detect_vm ( ) = = VIRTUALIZATION_KVM ) {
2014-08-20 15:49:39 +04:00
/* 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 */
2016-07-22 13:12:27 +03:00
if ( id128_read ( " /sys/class/dmi/id/product_uuid " , ID128_UUID , ret ) > = 0 ) {
log_info ( " Initializing machine ID from KVM UUID. " ) ;
return 0 ;
2014-08-20 15:49:39 +04:00
}
2014-04-28 20:11:40 +04:00
}
2012-02-15 22:16:08 +04:00
}
2011-03-04 23:53:19 +03:00
/* If that didn't work, generate a random machine id */
2016-07-22 13:12:27 +03:00
r = sd_id128_randomize ( ret ) ;
2014-11-28 20:23:20 +03:00
if ( r < 0 )
2016-07-22 13:12:27 +03:00
return log_error_errno ( r , " Failed to generate randomized : %m " ) ;
2011-03-04 23:53:19 +03:00
log_info ( " Initializing machine ID from random generator. " ) ;
return 0 ;
}
2016-07-22 13:12:27 +03:00
int machine_id_setup ( const char * root , sd_id128_t machine_id , sd_id128_t * ret ) {
2015-03-10 20:58:10 +03:00
const char * etc_machine_id , * run_machine_id ;
_cleanup_close_ int fd = - 1 ;
2016-07-22 13:12:27 +03:00
bool writable ;
2015-03-10 20:58:10 +03:00
int r ;
2014-11-24 11:40:57 +03:00
2016-04-06 10:20:34 +03:00
etc_machine_id = prefix_roota ( root , " /etc/machine-id " ) ;
2014-11-24 11:40:57 +03:00
2015-03-10 20:58:10 +03:00
RUN_WITH_UMASK ( 0000 ) {
/* We create this 0444, to indicate that this isn't really
* something you should ever modify . Of course , since the file
* will be owned by root it doesn ' t matter much , but maybe
* people look . */
2014-11-24 11:40:57 +03:00
2016-07-22 13:12:27 +03:00
( void ) mkdir_parents ( etc_machine_id , 0755 ) ;
2015-03-10 20:58:10 +03:00
fd = open ( etc_machine_id , O_RDWR | O_CREAT | O_CLOEXEC | O_NOCTTY , 0444 ) ;
if ( fd < 0 ) {
int old_errno = errno ;
fd = open ( etc_machine_id , O_RDONLY | O_CLOEXEC | O_NOCTTY ) ;
if ( fd < 0 ) {
if ( old_errno = = EROFS & & errno = = ENOENT )
log_error_errno ( errno ,
" System cannot boot: Missing /etc/machine-id and /etc is mounted read-only. \n "
" Booting up is supported only when: \n "
" 1) /etc/machine-id exists and is populated. \n "
" 2) /etc/machine-id exists and is empty. \n "
" 3) /etc/machine-id is missing and /etc is writable. \n " ) ;
else
log_error_errno ( errno , " Cannot open %s: %m " , etc_machine_id ) ;
return - errno ;
}
2014-11-24 11:40:57 +03:00
2015-03-10 20:58:10 +03:00
writable = false ;
2016-07-22 13:12:27 +03:00
} else
writable = true ;
2015-03-10 20:58:10 +03:00
}
2016-07-22 13:12:27 +03:00
/* A we got a valid machine ID argument, that's what counts */
if ( sd_id128_is_null ( machine_id ) ) {
2014-11-24 11:40:57 +03:00
2016-07-22 13:12:27 +03:00
/* Try to read any existing machine ID */
if ( id128_read_fd ( fd , ID128_PLAIN , ret ) > = 0 )
return 0 ;
2015-03-10 20:58:10 +03:00
2016-07-22 13:12:27 +03:00
/* Hmm, so, the id currently stored is not useful, then let's generate one */
r = generate_machine_id ( root , & machine_id ) ;
2015-07-06 01:00:59 +03:00
if ( r < 0 )
return r ;
2016-07-22 13:12:27 +03:00
if ( lseek ( fd , 0 , SEEK_SET ) = = ( off_t ) - 1 )
return log_error_errno ( errno , " Failed to seek: %m " ) ;
2015-07-06 01:00:59 +03:00
}
2015-03-10 20:58:10 +03:00
if ( writable )
2016-07-22 13:12:27 +03:00
if ( id128_write_fd ( fd , ID128_PLAIN , machine_id , true ) > = 0 )
goto finish ;
2015-03-10 20:58:10 +03:00
fd = safe_close ( fd ) ;
2016-07-22 13:12:27 +03:00
/* Hmm, we couldn't write it? So let's write it to /run/machine-id as a replacement */
2015-03-10 20:58:10 +03:00
2016-07-22 13:12:27 +03:00
run_machine_id = prefix_roota ( root , " /run/machine-id " ) ;
RUN_WITH_UMASK ( 0022 )
r = id128_write ( run_machine_id , ID128_PLAIN , machine_id , false ) ;
if ( r < 0 ) {
( void ) unlink ( run_machine_id ) ;
return log_error_errno ( r , " Cannot write %s: %m " , run_machine_id ) ;
2015-03-10 20:58:10 +03:00
}
/* And now, let's mount it over */
if ( mount ( run_machine_id , etc_machine_id , NULL , MS_BIND , NULL ) < 0 ) {
( void ) unlink_noerrno ( run_machine_id ) ;
return log_error_errno ( errno , " Failed to mount %s: %m " , etc_machine_id ) ;
}
log_info ( " Installed transient %s file. " , etc_machine_id ) ;
/* Mark the mount read-only */
if ( mount ( NULL , etc_machine_id , NULL , MS_BIND | MS_RDONLY | MS_REMOUNT , NULL ) < 0 )
2016-07-22 13:12:27 +03:00
log_warning_errno ( errno , " Failed to make transient %s read-only, ignoring: %m " , etc_machine_id ) ;
finish :
if ( ret )
* ret = machine_id ;
2015-03-10 20:58:10 +03:00
return 0 ;
2014-11-24 11:40:57 +03:00
}
2014-11-24 11:43:29 +03:00
int machine_id_commit ( const char * root ) {
_cleanup_close_ int fd = - 1 , initial_mntns_fd = - 1 ;
const char * etc_machine_id ;
2016-07-21 20:12:50 +03:00
sd_id128_t id ;
2014-11-24 11:43:29 +03:00
int r ;
2016-07-21 20:12:50 +03:00
/* Replaces a tmpfs bind mount of /etc/machine-id by a proper file, atomically. For this, the umount is removed
* in a mount namespace , a new file is created at the right place . Afterwards the mount is also removed in the
* original mount namespace , thus revealing the file that was just created . */
2016-04-06 10:20:34 +03:00
etc_machine_id = prefix_roota ( root , " /etc/machine-id " ) ;
2014-11-24 11:43:29 +03:00
2015-05-29 18:13:12 +03:00
r = path_is_mount_point ( etc_machine_id , 0 ) ;
2014-11-24 11:43:29 +03:00
if ( r < 0 )
2014-12-29 12:45:58 +03:00
return log_error_errno ( r , " Failed to determine whether %s is a mount point: %m " , etc_machine_id ) ;
2014-11-24 11:43:29 +03:00
if ( r = = 0 ) {
2016-07-10 15:48:23 +03:00
log_debug ( " %s is not a mount point. Nothing to do. " , etc_machine_id ) ;
2014-11-24 11:43:29 +03:00
return 0 ;
}
/* Read existing machine-id */
fd = open ( etc_machine_id , O_RDONLY | O_CLOEXEC | O_NOCTTY ) ;
if ( fd < 0 )
return log_error_errno ( errno , " Cannot open %s: %m " , etc_machine_id ) ;
2015-04-04 12:52:57 +03:00
r = fd_is_temporary_fs ( fd ) ;
2014-11-24 11:43:29 +03:00
if ( r < 0 )
return log_error_errno ( r , " Failed to determine whether %s is on a temporary file system: %m " , etc_machine_id ) ;
if ( r = = 0 ) {
log_error ( " %s is not on a temporary file system. " , etc_machine_id ) ;
return - EROFS ;
}
2016-07-21 20:12:50 +03:00
r = id128_read_fd ( fd , ID128_PLAIN , & id ) ;
if ( r < 0 )
return log_error_errno ( r , " We didn't find a valid machine ID in %s. " , etc_machine_id ) ;
2014-11-24 11:43:29 +03:00
fd = safe_close ( fd ) ;
/* Store current mount namespace */
namespace helpers: Allow entering a UID namespace
To be able to use `systemd-run` or `machinectl login` on a container
that is in a private user namespace, the sub-process must have entered
the user namespace before connecting to the container's D-Bus, otherwise
the UID and GID in the peer credentials are garbage.
So we extend namespace_open and namespace_enter to support UID namespaces,
and we enter the UID namespace in bus_container_connect_{socket,kernel}.
namespace_open will degrade to a no-op if user namespaces are not enabled
in the kernel.
Special handling is required for the setns call in namespace_enter with
a user namespace, since transitioning to your own namespace is forbidden,
as it would result in re-entering your user namespace as root.
Arguably it may be valid to check this at the call site, rather than
inside namespace_enter, but it is less code to do it inside, and if the
intention of calling namespace_enter is to *be* in the target namespace,
rather than to transition to the target namespace, it is a reasonable
approach.
The check for whether the user namespace is the same must happen before
entering namespaces, as we may not be able to access /proc during the
intermediate transition stage.
We can't instead attempt to enter the user namespace and then ignore
the failure from it being the same namespace, since the error code is
not distinct, and we can't compare namespaces while mid-transition.
2015-08-17 11:52:13 +03:00
r = namespace_open ( 0 , NULL , & initial_mntns_fd , NULL , NULL , NULL ) ;
2014-11-24 11:43:29 +03:00
if ( r < 0 )
return log_error_errno ( r , " Can't fetch current mount namespace: %m " ) ;
/* Switch to a new mount namespace, isolate ourself and unmount etc_machine_id in our new namespace */
if ( unshare ( CLONE_NEWNS ) < 0 )
return log_error_errno ( errno , " Failed to enter new namespace: %m " ) ;
if ( mount ( NULL , " / " , NULL , MS_SLAVE | MS_REC , NULL ) < 0 )
return log_error_errno ( errno , " Couldn't make-rslave / mountpoint in our private namespace: %m " ) ;
if ( umount ( etc_machine_id ) < 0 )
return log_error_errno ( errno , " Failed to unmount transient %s file in our private namespace: %m " , etc_machine_id ) ;
/* Update a persistent version of etc_machine_id */
2016-07-21 20:12:50 +03:00
r = id128_write ( etc_machine_id , ID128_PLAIN , id , true ) ;
2014-11-24 11:43:29 +03:00
if ( r < 0 )
2016-07-21 20:12:50 +03:00
return log_error_errno ( r , " Cannot write %s. This is mandatory to get a persistent machine ID: %m " , etc_machine_id ) ;
2014-11-24 11:43:29 +03:00
/* Return to initial namespace and proceed a lazy tmpfs unmount */
namespace helpers: Allow entering a UID namespace
To be able to use `systemd-run` or `machinectl login` on a container
that is in a private user namespace, the sub-process must have entered
the user namespace before connecting to the container's D-Bus, otherwise
the UID and GID in the peer credentials are garbage.
So we extend namespace_open and namespace_enter to support UID namespaces,
and we enter the UID namespace in bus_container_connect_{socket,kernel}.
namespace_open will degrade to a no-op if user namespaces are not enabled
in the kernel.
Special handling is required for the setns call in namespace_enter with
a user namespace, since transitioning to your own namespace is forbidden,
as it would result in re-entering your user namespace as root.
Arguably it may be valid to check this at the call site, rather than
inside namespace_enter, but it is less code to do it inside, and if the
intention of calling namespace_enter is to *be* in the target namespace,
rather than to transition to the target namespace, it is a reasonable
approach.
The check for whether the user namespace is the same must happen before
entering namespaces, as we may not be able to access /proc during the
intermediate transition stage.
We can't instead attempt to enter the user namespace and then ignore
the failure from it being the same namespace, since the error code is
not distinct, and we can't compare namespaces while mid-transition.
2015-08-17 11:52:13 +03:00
r = namespace_enter ( - 1 , initial_mntns_fd , - 1 , - 1 , - 1 ) ;
2014-11-24 11:43:29 +03:00
if ( r < 0 )
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 ) ;
if ( umount2 ( etc_machine_id , MNT_DETACH ) < 0 )
return log_warning_errno ( errno , " Failed to unmount transient %s file: %m. \n We keep that mount until next reboot. " , etc_machine_id ) ;
return 0 ;
}