1
0
mirror of https://github.com/systemd/systemd.git synced 2024-11-01 09:21:26 +03:00

systemd-mount features and improvements (#5067)

systemd-mount --unmount /some/path
systemd-mount --umount /some/path
systemd-mount -u /some/path
systemd-unmount /some/path
all do the same thing that one could expect from the name.
This commit is contained in:
Lukas Rusak 2017-02-04 07:21:49 -08:00 committed by Zbigniew Jędrzejewski-Szmek
parent b4a8c5ddb1
commit c37fb55bd8

View File

@ -40,6 +40,7 @@ enum {
ACTION_DEFAULT,
ACTION_MOUNT,
ACTION_AUTOMOUNT,
ACTION_UMOUNT,
ACTION_LIST,
} arg_action = ACTION_DEFAULT;
@ -99,6 +100,7 @@ static void help(void) {
" Set automount unit property\n"
" --bind-device Bind automount unit to device\n"
" --list List mountable block devices\n"
" -u --umount Unmount a mount point\n"
, program_invocation_short_name);
}
@ -144,6 +146,8 @@ static int parse_argv(int argc, char *argv[]) {
{ "automount-property", required_argument, NULL, ARG_AUTOMOUNT_PROPERTY },
{ "bind-device", no_argument, NULL, ARG_BIND_DEVICE },
{ "list", no_argument, NULL, ARG_LIST },
{ "umount", no_argument, NULL, 'u' },
{ "unmount", no_argument, NULL, 'u' },
{},
};
@ -152,7 +156,10 @@ static int parse_argv(int argc, char *argv[]) {
assert(argc >= 0);
assert(argv);
while ((c = getopt_long(argc, argv, "hqH:M:t:o:p:A", options, NULL)) >= 0)
if (strstr(program_invocation_short_name, "systemd-umount"))
arg_action = ACTION_UMOUNT;
while ((c = getopt_long(argc, argv, "hqH:M:t:o:p:Au", options, NULL)) >= 0)
switch (c) {
@ -263,6 +270,10 @@ static int parse_argv(int argc, char *argv[]) {
arg_action = ACTION_LIST;
break;
case 'u':
arg_action = ACTION_UMOUNT;
break;
case '?':
return -EINVAL;
@ -607,6 +618,89 @@ static int start_transient_automount(
return 0;
}
static int stop_mount(
sd_bus *bus,
char **argv,
const char *suffix) {
_cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL, *reply = NULL;
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
_cleanup_(bus_wait_for_jobs_freep) BusWaitForJobs *w = NULL;
_cleanup_free_ char *mount_unit = NULL;
int r;
if (!arg_no_block) {
r = bus_wait_for_jobs_new(bus, &w);
if (r < 0)
return log_error_errno(r, "Could not watch jobs: %m");
}
r = unit_name_from_path(arg_mount_where, suffix, &mount_unit);
if (r < 0)
return log_error_errno(r, "Failed to make mount unit name: %m");
r = sd_bus_message_new_method_call(
bus,
&m,
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
"StopUnit");
if (r < 0)
return bus_log_create_error(r);
r = sd_bus_message_set_allow_interactive_authorization(m, arg_ask_password);
if (r < 0)
return bus_log_create_error(r);
/* Name and mode */
r = sd_bus_message_append(m, "ss", mount_unit, "fail");
if (r < 0)
return bus_log_create_error(r);
polkit_agent_open_if_enabled();
r = sd_bus_call(bus, m, 0, &error, &reply);
if (r < 0)
return log_error_errno(r, "Failed to stop mount unit: %s", bus_error_message(&error, r));
if (w) {
const char *object;
r = sd_bus_message_read(reply, "o", &object);
if (r < 0)
return bus_log_parse_error(r);
r = bus_wait_for_jobs_one(w, object, arg_quiet);
if (r < 0)
return r;
}
if (!arg_quiet)
log_info("Stopped unit %s%s%s for mount point: %s%s%s",
ansi_highlight(), mount_unit, ansi_normal(),
ansi_highlight(), arg_mount_where, ansi_normal());
return 0;
}
static int stop_mounts(
sd_bus *bus,
char **argv) {
int r;
r = stop_mount(bus, argv + optind, ".mount");
if (r < 0)
return r;
r = stop_mount(bus, argv + optind, ".automount");
if (r < 0)
return r;
return 0;
}
static int acquire_mount_type(struct udev_device *d) {
const char *v;
@ -1093,6 +1187,10 @@ int main(int argc, char* argv[]) {
r = start_transient_automount(bus, argv + optind);
break;
case ACTION_UMOUNT:
r = stop_mounts(bus, argv + optind);
break;
default:
assert_not_reached("Unexpected action.");
}