LCOV - code coverage report
Current view: top level - shared - switch-root.c (source / functions) Hit Total Coverage
Test: systemd_full.info Lines: 0 52 0.0 %
Date: 2019-08-23 13:36:53 Functions: 0 1 0.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 0 73 0.0 %

           Branch data     Line data    Source code
       1                 :            : /* SPDX-License-Identifier: LGPL-2.1+ */
       2                 :            : 
       3                 :            : #include <errno.h>
       4                 :            : #include <fcntl.h>
       5                 :            : #include <limits.h>
       6                 :            : #include <stdbool.h>
       7                 :            : #include <stdio.h>
       8                 :            : #include <sys/mount.h>
       9                 :            : #include <sys/stat.h>
      10                 :            : #include <unistd.h>
      11                 :            : 
      12                 :            : #include "base-filesystem.h"
      13                 :            : #include "fd-util.h"
      14                 :            : #include "fs-util.h"
      15                 :            : #include "log.h"
      16                 :            : #include "missing.h"
      17                 :            : #include "mkdir.h"
      18                 :            : #include "mount-util.h"
      19                 :            : #include "mountpoint-util.h"
      20                 :            : #include "path-util.h"
      21                 :            : #include "rm-rf.h"
      22                 :            : #include "stdio-util.h"
      23                 :            : #include "string-util.h"
      24                 :            : #include "strv.h"
      25                 :            : #include "switch-root.h"
      26                 :            : #include "user-util.h"
      27                 :            : #include "util.h"
      28                 :            : 
      29                 :          0 : int switch_root(const char *new_root,
      30                 :            :                 const char *old_root_after, /* path below the new root, where to place the old root after the transition */
      31                 :            :                 bool unmount_old_root,
      32                 :            :                 unsigned long mount_flags) {  /* MS_MOVE or MS_BIND */
      33                 :            : 
      34                 :          0 :         _cleanup_free_ char *resolved_old_root_after = NULL;
      35                 :          0 :         _cleanup_close_ int old_root_fd = -1;
      36                 :            :         bool old_root_remove;
      37                 :            :         const char *i;
      38                 :            :         int r;
      39                 :            : 
      40         [ #  # ]:          0 :         assert(new_root);
      41         [ #  # ]:          0 :         assert(old_root_after);
      42                 :            : 
      43         [ #  # ]:          0 :         if (path_equal(new_root, "/"))
      44                 :          0 :                 return 0;
      45                 :            : 
      46                 :            :         /* Check if we shall remove the contents of the old root */
      47                 :          0 :         old_root_remove = in_initrd();
      48         [ #  # ]:          0 :         if (old_root_remove) {
      49                 :          0 :                 old_root_fd = open("/", O_RDONLY|O_NONBLOCK|O_CLOEXEC|O_NOCTTY|O_DIRECTORY);
      50         [ #  # ]:          0 :                 if (old_root_fd < 0)
      51         [ #  # ]:          0 :                         return log_error_errno(errno, "Failed to open root directory: %m");
      52                 :            :         }
      53                 :            : 
      54                 :            :         /* Determine where we shall place the old root after the transition */
      55                 :          0 :         r = chase_symlinks(old_root_after, new_root, CHASE_PREFIX_ROOT|CHASE_NONEXISTENT, &resolved_old_root_after);
      56         [ #  # ]:          0 :         if (r < 0)
      57         [ #  # ]:          0 :                 return log_error_errno(r, "Failed to resolve %s/%s: %m", new_root, old_root_after);
      58         [ #  # ]:          0 :         if (r == 0) /* Doesn't exist yet. Let's create it */
      59                 :          0 :                 (void) mkdir_p_label(resolved_old_root_after, 0755);
      60                 :            : 
      61                 :            :         /* Work-around for kernel design: the kernel refuses MS_MOVE if any file systems are mounted MS_SHARED. Hence
      62                 :            :          * remount them MS_PRIVATE here as a work-around.
      63                 :            :          *
      64                 :            :          * https://bugzilla.redhat.com/show_bug.cgi?id=847418 */
      65         [ #  # ]:          0 :         if (mount(NULL, "/", NULL, MS_REC|MS_PRIVATE, NULL) < 0)
      66         [ #  # ]:          0 :                 return log_error_errno(errno, "Failed to set \"/\" mount propagation to private: %m");
      67                 :            : 
      68         [ #  # ]:          0 :         FOREACH_STRING(i, "/sys", "/dev", "/run", "/proc") {
      69      [ #  #  # ]:          0 :                 _cleanup_free_ char *chased = NULL;
      70                 :            : 
      71                 :          0 :                 r = chase_symlinks(i, new_root, CHASE_PREFIX_ROOT|CHASE_NONEXISTENT, &chased);
      72         [ #  # ]:          0 :                 if (r < 0)
      73         [ #  # ]:          0 :                         return log_error_errno(r, "Failed to resolve %s/%s: %m", new_root, i);
      74         [ #  # ]:          0 :                 if (r > 0) {
      75                 :            :                         /* Already exists. Let's see if it is a mount point already. */
      76                 :          0 :                         r = path_is_mount_point(chased, NULL, 0);
      77         [ #  # ]:          0 :                         if (r < 0)
      78         [ #  # ]:          0 :                                 return log_error_errno(r, "Failed to determine whether %s is a mount point: %m", chased);
      79         [ #  # ]:          0 :                         if (r > 0) /* If it is already mounted, then do nothing */
      80                 :          0 :                                 continue;
      81                 :            :                 } else
      82                 :            :                          /* Doesn't exist yet? */
      83                 :          0 :                         (void) mkdir_p_label(chased, 0755);
      84                 :            : 
      85         [ #  # ]:          0 :                 if (mount(i, chased, NULL, mount_flags, NULL) < 0)
      86         [ #  # ]:          0 :                         return log_error_errno(errno, "Failed to mount %s to %s: %m", i, chased);
      87                 :            :         }
      88                 :            : 
      89                 :            :         /* Do not fail if base_filesystem_create() fails. Not all switch roots are like base_filesystem_create() wants
      90                 :            :          * them to look like. They might even boot, if they are RO and don't have the FS layout. Just ignore the error
      91                 :            :          * and switch_root() nevertheless. */
      92                 :          0 :         (void) base_filesystem_create(new_root, UID_INVALID, GID_INVALID);
      93                 :            : 
      94         [ #  # ]:          0 :         if (chdir(new_root) < 0)
      95         [ #  # ]:          0 :                 return log_error_errno(errno, "Failed to change directory to %s: %m", new_root);
      96                 :            : 
      97                 :            :         /* We first try a pivot_root() so that we can umount the old root dir. In many cases (i.e. where rootfs is /),
      98                 :            :          * that's not possible however, and hence we simply overmount root */
      99         [ #  # ]:          0 :         if (pivot_root(new_root, resolved_old_root_after) >= 0) {
     100                 :            : 
     101                 :            :                 /* Immediately get rid of the old root, if detach_oldroot is set.
     102                 :            :                  * Since we are running off it we need to do this lazily. */
     103         [ #  # ]:          0 :                 if (unmount_old_root) {
     104                 :          0 :                         r = umount_recursive(old_root_after, MNT_DETACH);
     105         [ #  # ]:          0 :                         if (r < 0)
     106         [ #  # ]:          0 :                                 log_warning_errno(r, "Failed to unmount old root directory tree, ignoring: %m");
     107                 :            :                 }
     108                 :            : 
     109         [ #  # ]:          0 :         } else if (mount(new_root, "/", NULL, MS_MOVE, NULL) < 0)
     110         [ #  # ]:          0 :                 return log_error_errno(errno, "Failed to move %s to /: %m", new_root);
     111                 :            : 
     112         [ #  # ]:          0 :         if (chroot(".") < 0)
     113         [ #  # ]:          0 :                 return log_error_errno(errno, "Failed to change root: %m");
     114                 :            : 
     115         [ #  # ]:          0 :         if (chdir("/") < 0)
     116         [ #  # ]:          0 :                 return log_error_errno(errno, "Failed to change directory: %m");
     117                 :            : 
     118         [ #  # ]:          0 :         if (old_root_fd >= 0) {
     119                 :            :                 struct stat rb;
     120                 :            : 
     121         [ #  # ]:          0 :                 if (fstat(old_root_fd, &rb) < 0)
     122         [ #  # ]:          0 :                         log_warning_errno(errno, "Failed to stat old root directory, leaving: %m");
     123                 :            :                 else
     124                 :          0 :                         (void) rm_rf_children(TAKE_FD(old_root_fd), 0, &rb); /* takes possession of the dir fd, even on failure */
     125                 :            :         }
     126                 :            : 
     127                 :          0 :         return 0;
     128                 :            : }

Generated by: LCOV version 1.14