| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include "sd-bus.h" |
| 4 |
|
| 5 |
#include "alloc-util.h" |
| 6 |
#include "argv-util.h" |
| 7 |
#include "bus-error.h" |
| 8 |
#include "bus-locator.h" |
| 9 |
#include "bus-util.h" |
| 10 |
#include "chase.h" |
| 11 |
#include "fd-util.h" |
| 12 |
#include "initrd-util.h" |
| 13 |
#include "path-util.h" |
| 14 |
#include "proc-cmdline.h" |
| 15 |
#include "signal-util.h" |
| 16 |
#include "stat-util.h" |
| 17 |
#include "string-util.h" |
| 18 |
#include "systemctl.h" |
| 19 |
#include "systemctl-switch-root.h" |
| 20 |
#include "systemctl-util.h" |
| 21 |
|
| 22 |
static int same_file_in_root( |
| 23 |
const char *root, |
| 24 |
const char *a, |
| 25 |
const char *b) { |
| 26 |
|
| 27 |
struct stat sta, stb; |
| 28 |
int r; |
| 29 |
|
| 30 |
r = chase_and_stat(a, root, CHASE_PREFIX_ROOT, NULL, &sta); |
| 31 |
if (r < 0) |
| 32 |
return r; |
| 33 |
|
| 34 |
r = chase_and_stat(b, root, CHASE_PREFIX_ROOT, NULL, &stb); |
| 35 |
if (r < 0) |
| 36 |
return r; |
| 37 |
|
| 38 |
return stat_inode_same(&sta, &stb); |
| 39 |
} |
| 40 |
|
| 41 |
int verb_switch_root(int argc, char *argv[], uintptr_t _data, void *userdata) { |
| 42 |
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
| 43 |
_cleanup_free_ char *cmdline_init = NULL; |
| 44 |
const char *root, *init; |
| 45 |
sd_bus *bus; |
| 46 |
int r; |
| 47 |
|
| 48 |
if (arg_transport != BUS_TRANSPORT_LOCAL) |
| 49 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Cannot switch root remotely."); |
| 50 |
|
| 51 |
if (argc > 3) |
| 52 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Too many arguments."); |
| 53 |
|
| 54 |
if (argc >= 2) { |
| 55 |
root = argv[1]; |
| 56 |
|
| 57 |
if (!path_is_valid(root)) |
| 58 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Invalid root path: %s", root); |
| 59 |
|
| 60 |
if (!path_is_absolute(root)) |
| 61 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Root path is not absolute: %s", root); |
| 62 |
|
| 63 |
r = path_is_root(root); |
| 64 |
if (r < 0) |
| 65 |
return log_error_errno(r, "Failed to check if switch-root directory '%s' is current root: %m", root); |
| 66 |
if (r > 0) |
| 67 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Cannot switch to current root directory: %s", root); |
| 68 |
} else |
| 69 |
root = "/sysroot"; |
| 70 |
|
| 71 |
if (!in_initrd()) |
| 72 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Not in initrd, refusing switch-root operation."); |
| 73 |
|
| 74 |
if (argc >= 3) |
| 75 |
init = argv[2]; |
| 76 |
else { |
| 77 |
r = proc_cmdline_get_key("init", 0, &cmdline_init); |
| 78 |
if (r < 0) |
| 79 |
log_debug_errno(r, "Failed to parse /proc/cmdline: %m"); |
| 80 |
|
| 81 |
init = cmdline_init; |
| 82 |
} |
| 83 |
|
| 84 |
init = empty_to_null(init); |
| 85 |
if (init) { |
| 86 |
if (!path_is_valid(init)) |
| 87 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Invalid path to init binary: %s", init); |
| 88 |
if (!path_is_absolute(init)) |
| 89 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Path to init binary is not absolute: %s", init); |
| 90 |
|
| 91 |
/* If the passed init is actually the same as the systemd binary, then let's suppress it. */ |
| 92 |
if (same_file_in_root(root, SYSTEMD_BINARY_PATH, init) > 0) |
| 93 |
init = NULL; |
| 94 |
} |
| 95 |
|
| 96 |
/* Instruct PID1 to exclude us from its killing spree applied during the transition. Otherwise we |
| 97 |
* would exit with a failure status even though the switch to the new root has succeed. */ |
| 98 |
assert(saved_argv); |
| 99 |
assert(saved_argv[0]); |
| 100 |
saved_argv[0][0] = '@'; |
| 101 |
|
| 102 |
r = acquire_bus(BUS_MANAGER, &bus); |
| 103 |
if (r < 0) |
| 104 |
return r; |
| 105 |
|
| 106 |
/* If we are slow to exit after the root switch, the new systemd instance will send us a signal to |
| 107 |
* terminate. Just ignore it and exit normally. This way the unit does not end up as failed. */ |
| 108 |
r = ignore_signals(SIGTERM); |
| 109 |
if (r < 0) |
| 110 |
log_warning_errno(r, "Failed to change disposition of SIGTERM to ignore: %m"); |
| 111 |
|
| 112 |
log_debug("Switching root - root: %s; init: %s", root, strna(init)); |
| 113 |
|
| 114 |
r = bus_call_method(bus, bus_systemd_mgr, "SwitchRoot", &error, NULL, "ss", root, init); |
| 115 |
if (r < 0) { |
| 116 |
(void) default_signals(SIGTERM); |
| 117 |
|
| 118 |
return log_error_errno(r, "Failed to switch root: %s", bus_error_message(&error, r)); |
| 119 |
} |
| 120 |
|
| 121 |
return 0; |
| 122 |
} |