| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include "sd-bus.h" |
| 4 |
|
| 5 |
#include "alloc-util.h" |
| 6 |
#include "bus-error.h" |
| 7 |
#include "bus-locator.h" |
| 8 |
#include "bus-util.h" |
| 9 |
#include "extract-word.h" |
| 10 |
#include "gpt.h" |
| 11 |
#include "log.h" |
| 12 |
#include "systemctl.h" |
| 13 |
#include "systemctl-mount.h" |
| 14 |
#include "systemctl-util.h" |
| 15 |
#include "unit-name.h" |
| 16 |
|
| 17 |
int verb_bind(int argc, char *argv[], uintptr_t _data, void *userdata) { |
| 18 |
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
| 19 |
_cleanup_free_ char *n = NULL; |
| 20 |
sd_bus *bus; |
| 21 |
int r; |
| 22 |
|
| 23 |
r = acquire_bus(BUS_MANAGER, &bus); |
| 24 |
if (r < 0) |
| 25 |
return r; |
| 26 |
|
| 27 |
polkit_agent_open_maybe(); |
| 28 |
|
| 29 |
r = unit_name_mangle(argv[1], arg_quiet ? 0 : UNIT_NAME_MANGLE_WARN, &n); |
| 30 |
if (r < 0) |
| 31 |
return log_error_errno(r, "Failed to mangle unit name: %m"); |
| 32 |
|
| 33 |
r = bus_call_method( |
| 34 |
bus, |
| 35 |
bus_systemd_mgr, |
| 36 |
"BindMountUnit", |
| 37 |
&error, |
| 38 |
NULL, |
| 39 |
"sssbb", |
| 40 |
n, |
| 41 |
argv[2], |
| 42 |
argv[3], |
| 43 |
arg_read_only, |
| 44 |
arg_mkdir); |
| 45 |
if (r < 0) |
| 46 |
return log_error_errno(r, "Failed to bind mount: %s", bus_error_message(&error, r)); |
| 47 |
|
| 48 |
return 0; |
| 49 |
} |
| 50 |
|
| 51 |
int verb_mount_image(int argc, char *argv[], uintptr_t _data, void *userdata) { |
| 52 |
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
| 53 |
const char *unit = argv[1], *src = argv[2], *dest = argv[3]; |
| 54 |
_cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL; |
| 55 |
_cleanup_free_ char *n = NULL; |
| 56 |
sd_bus *bus; |
| 57 |
int r; |
| 58 |
|
| 59 |
r = acquire_bus(BUS_MANAGER, &bus); |
| 60 |
if (r < 0) |
| 61 |
return r; |
| 62 |
|
| 63 |
polkit_agent_open_maybe(); |
| 64 |
|
| 65 |
r = unit_name_mangle(unit, arg_quiet ? 0 : UNIT_NAME_MANGLE_WARN, &n); |
| 66 |
if (r < 0) |
| 67 |
return log_error_errno(r, "Failed to mangle unit name: %m"); |
| 68 |
|
| 69 |
r = bus_message_new_method_call( |
| 70 |
bus, |
| 71 |
&m, |
| 72 |
bus_systemd_mgr, |
| 73 |
"MountImageUnit"); |
| 74 |
if (r < 0) |
| 75 |
return bus_log_create_error(r); |
| 76 |
|
| 77 |
r = sd_bus_message_append( |
| 78 |
m, |
| 79 |
"sssbb", |
| 80 |
n, |
| 81 |
src, |
| 82 |
dest, |
| 83 |
arg_read_only, |
| 84 |
arg_mkdir); |
| 85 |
if (r < 0) |
| 86 |
return bus_log_create_error(r); |
| 87 |
|
| 88 |
r = sd_bus_message_open_container(m, 'a', "(ss)"); |
| 89 |
if (r < 0) |
| 90 |
return bus_log_create_error(r); |
| 91 |
|
| 92 |
if (argc > 4) { |
| 93 |
_cleanup_free_ char *partition = NULL, *mount_options = NULL; |
| 94 |
const char *options = argv[4]; |
| 95 |
|
| 96 |
r = extract_many_words(&options, ":", EXTRACT_CUNESCAPE|EXTRACT_UNESCAPE_SEPARATORS, &partition, &mount_options); |
| 97 |
if (r < 0) |
| 98 |
return r; |
| 99 |
/* Single set of options, applying to the root partition/single filesystem */ |
| 100 |
if (r == 1) { |
| 101 |
r = sd_bus_message_append(m, "(ss)", "root", partition); |
| 102 |
if (r < 0) |
| 103 |
return bus_log_create_error(r); |
| 104 |
} else if (r > 1) { |
| 105 |
if (partition_designator_from_string(partition) < 0) |
| 106 |
return bus_log_create_error(-EINVAL); |
| 107 |
|
| 108 |
r = sd_bus_message_append(m, "(ss)", partition, mount_options); |
| 109 |
if (r < 0) |
| 110 |
return bus_log_create_error(r); |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
r = sd_bus_message_close_container(m); |
| 115 |
if (r < 0) |
| 116 |
return bus_log_create_error(r); |
| 117 |
|
| 118 |
r = sd_bus_call(bus, m, -1, &error, NULL); |
| 119 |
if (r < 0) |
| 120 |
return log_error_errno(r, "Failed to mount image: %s", bus_error_message(&error, r)); |
| 121 |
|
| 122 |
return 0; |
| 123 |
} |