2020-11-09 05:23:58 +01:00
|
|
|
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
2016-08-06 00:32:46 +02:00
|
|
|
|
|
|
|
#include <getopt.h>
|
|
|
|
|
|
|
|
#include "sd-bus.h"
|
2018-08-22 06:36:15 +02:00
|
|
|
#include "sd-device.h"
|
2016-08-06 00:32:46 +02:00
|
|
|
|
|
|
|
#include "bus-error.h"
|
2020-06-28 15:59:37 +02:00
|
|
|
#include "bus-locator.h"
|
2016-08-06 00:32:46 +02:00
|
|
|
#include "bus-unit-util.h"
|
2019-03-06 19:38:45 +01:00
|
|
|
#include "bus-wait-for-jobs.h"
|
2018-08-28 09:05:35 +02:00
|
|
|
#include "device-util.h"
|
2017-06-28 20:19:38 +02:00
|
|
|
#include "dirent-util.h"
|
2016-08-06 00:32:46 +02:00
|
|
|
#include "escape.h"
|
2017-06-26 22:07:07 +02:00
|
|
|
#include "fd-util.h"
|
2017-06-28 20:19:38 +02:00
|
|
|
#include "fileio.h"
|
2020-01-09 06:39:35 +01:00
|
|
|
#include "format-table.h"
|
2019-03-27 11:32:41 +01:00
|
|
|
#include "format-util.h"
|
2017-11-30 17:27:53 +01:00
|
|
|
#include "fs-util.h"
|
2016-08-06 00:32:46 +02:00
|
|
|
#include "fstab-util.h"
|
2019-04-04 13:41:47 +02:00
|
|
|
#include "libmount-util.h"
|
2018-11-20 10:30:13 +01:00
|
|
|
#include "main-func.h"
|
2017-09-04 03:55:51 +02:00
|
|
|
#include "mount-util.h"
|
2018-11-29 10:24:39 +01:00
|
|
|
#include "mountpoint-util.h"
|
2016-08-06 00:32:46 +02:00
|
|
|
#include "pager.h"
|
|
|
|
#include "parse-util.h"
|
|
|
|
#include "path-util.h"
|
2018-11-20 15:42:57 +01:00
|
|
|
#include "pretty-print.h"
|
2019-03-13 12:14:47 +01:00
|
|
|
#include "sort-util.h"
|
2016-08-06 00:32:46 +02:00
|
|
|
#include "spawn-polkit-agent.h"
|
2017-06-28 20:19:38 +02:00
|
|
|
#include "stat-util.h"
|
2016-08-06 00:32:46 +02:00
|
|
|
#include "strv.h"
|
2019-03-13 12:14:47 +01:00
|
|
|
#include "terminal-util.h"
|
2017-12-23 08:59:56 +01:00
|
|
|
#include "unit-def.h"
|
2016-08-06 00:32:46 +02:00
|
|
|
#include "unit-name.h"
|
2017-12-09 10:18:48 +01:00
|
|
|
#include "user-util.h"
|
2016-08-06 00:32:46 +02:00
|
|
|
|
|
|
|
enum {
|
|
|
|
ACTION_DEFAULT,
|
|
|
|
ACTION_MOUNT,
|
|
|
|
ACTION_AUTOMOUNT,
|
2017-02-04 16:21:49 +01:00
|
|
|
ACTION_UMOUNT,
|
2016-08-06 00:32:46 +02:00
|
|
|
ACTION_LIST,
|
|
|
|
} arg_action = ACTION_DEFAULT;
|
|
|
|
|
|
|
|
static bool arg_no_block = false;
|
2018-11-11 12:56:29 +01:00
|
|
|
static PagerFlags arg_pager_flags = 0;
|
2020-01-09 06:49:11 +01:00
|
|
|
static bool arg_legend = true;
|
2020-01-10 04:07:28 +01:00
|
|
|
static bool arg_full = false;
|
2016-08-06 00:32:46 +02:00
|
|
|
static bool arg_ask_password = true;
|
|
|
|
static bool arg_quiet = false;
|
|
|
|
static BusTransport arg_transport = BUS_TRANSPORT_LOCAL;
|
|
|
|
static bool arg_user = false;
|
|
|
|
static const char *arg_host = NULL;
|
|
|
|
static bool arg_discover = false;
|
|
|
|
static char *arg_mount_what = NULL;
|
|
|
|
static char *arg_mount_where = NULL;
|
|
|
|
static char *arg_mount_type = NULL;
|
|
|
|
static char *arg_mount_options = NULL;
|
|
|
|
static char *arg_description = NULL;
|
|
|
|
static char **arg_property = NULL;
|
|
|
|
static usec_t arg_timeout_idle = USEC_INFINITY;
|
|
|
|
static bool arg_timeout_idle_set = false;
|
|
|
|
static char **arg_automount_property = NULL;
|
|
|
|
static int arg_bind_device = -1;
|
2017-12-09 10:18:48 +01:00
|
|
|
static uid_t arg_uid = UID_INVALID;
|
|
|
|
static gid_t arg_gid = GID_INVALID;
|
2016-08-06 00:32:46 +02:00
|
|
|
static bool arg_fsck = true;
|
2017-11-16 07:40:25 +01:00
|
|
|
static bool arg_aggressive_gc = false;
|
2016-08-06 00:32:46 +02:00
|
|
|
|
2018-11-20 10:30:13 +01:00
|
|
|
STATIC_DESTRUCTOR_REGISTER(arg_mount_what, freep);
|
|
|
|
STATIC_DESTRUCTOR_REGISTER(arg_mount_where, freep);
|
|
|
|
STATIC_DESTRUCTOR_REGISTER(arg_mount_type, freep);
|
|
|
|
STATIC_DESTRUCTOR_REGISTER(arg_mount_options, freep);
|
|
|
|
STATIC_DESTRUCTOR_REGISTER(arg_description, freep);
|
|
|
|
STATIC_DESTRUCTOR_REGISTER(arg_property, strv_freep);
|
|
|
|
STATIC_DESTRUCTOR_REGISTER(arg_automount_property, strv_freep);
|
|
|
|
|
2018-08-09 10:32:31 +02:00
|
|
|
static int help(void) {
|
|
|
|
_cleanup_free_ char *link = NULL;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
r = terminal_urlify_man("systemd-mount", "1", &link);
|
|
|
|
if (r < 0)
|
|
|
|
return log_oom();
|
|
|
|
|
2017-06-26 22:07:07 +02:00
|
|
|
printf("systemd-mount [OPTIONS...] WHAT [WHERE]\n"
|
|
|
|
"systemd-mount [OPTIONS...] --list\n"
|
|
|
|
"%s [OPTIONS...] %sWHAT|WHERE...\n\n"
|
2016-08-06 00:32:46 +02:00
|
|
|
"Establish a mount or auto-mount point transiently.\n\n"
|
|
|
|
" -h --help Show this help\n"
|
|
|
|
" --version Show package version\n"
|
|
|
|
" --no-block Do not wait until operation finished\n"
|
|
|
|
" --no-pager Do not pipe output into a pager\n"
|
2020-01-09 06:49:11 +01:00
|
|
|
" --no-legend Do not show the headers\n"
|
2020-01-10 04:07:28 +01:00
|
|
|
" -l --full Do not ellipsize output\n"
|
2016-08-06 00:32:46 +02:00
|
|
|
" --no-ask-password Do not prompt for password\n"
|
|
|
|
" -q --quiet Suppress information messages during runtime\n"
|
|
|
|
" --user Run as user unit\n"
|
|
|
|
" -H --host=[USER@]HOST Operate on remote host\n"
|
|
|
|
" -M --machine=CONTAINER Operate on local container\n"
|
|
|
|
" --discover Discover mount device metadata\n"
|
|
|
|
" -t --type=TYPE File system type\n"
|
|
|
|
" -o --options=OPTIONS Mount options\n"
|
2017-12-09 10:18:48 +01:00
|
|
|
" --owner=USER Add uid= and gid= options for USER\n"
|
2016-08-06 00:32:46 +02:00
|
|
|
" --fsck=no Don't run file system check before mount\n"
|
|
|
|
" --description=TEXT Description for unit\n"
|
|
|
|
" -p --property=NAME=VALUE Set mount unit property\n"
|
|
|
|
" -A --automount=BOOL Create an auto-mount point\n"
|
|
|
|
" --timeout-idle-sec=SEC Specify automount idle timeout\n"
|
|
|
|
" --automount-property=NAME=VALUE\n"
|
|
|
|
" Set automount unit property\n"
|
|
|
|
" --bind-device Bind automount unit to device\n"
|
|
|
|
" --list List mountable block devices\n"
|
2017-11-16 07:40:25 +01:00
|
|
|
" -u --umount Unmount mount points\n"
|
2018-08-09 10:32:31 +02:00
|
|
|
" -G --collect Unload unit after it stopped, even when failed\n"
|
|
|
|
"\nSee the %s for details.\n"
|
|
|
|
, program_invocation_short_name
|
|
|
|
, streq(program_invocation_short_name, "systemd-umount") ? "" : "--umount "
|
|
|
|
, link
|
|
|
|
);
|
|
|
|
|
|
|
|
return 0;
|
2016-08-06 00:32:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int parse_argv(int argc, char *argv[]) {
|
|
|
|
|
|
|
|
enum {
|
|
|
|
ARG_VERSION = 0x100,
|
|
|
|
ARG_NO_BLOCK,
|
|
|
|
ARG_NO_PAGER,
|
2020-01-09 06:49:11 +01:00
|
|
|
ARG_NO_LEGEND,
|
2016-08-06 00:32:46 +02:00
|
|
|
ARG_NO_ASK_PASSWORD,
|
|
|
|
ARG_USER,
|
|
|
|
ARG_SYSTEM,
|
|
|
|
ARG_DISCOVER,
|
|
|
|
ARG_MOUNT_TYPE,
|
|
|
|
ARG_MOUNT_OPTIONS,
|
2017-12-09 10:18:48 +01:00
|
|
|
ARG_OWNER,
|
2016-08-06 00:32:46 +02:00
|
|
|
ARG_FSCK,
|
|
|
|
ARG_DESCRIPTION,
|
|
|
|
ARG_TIMEOUT_IDLE,
|
|
|
|
ARG_AUTOMOUNT,
|
|
|
|
ARG_AUTOMOUNT_PROPERTY,
|
|
|
|
ARG_BIND_DEVICE,
|
|
|
|
ARG_LIST,
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct option options[] = {
|
|
|
|
{ "help", no_argument, NULL, 'h' },
|
|
|
|
{ "version", no_argument, NULL, ARG_VERSION },
|
|
|
|
{ "no-block", no_argument, NULL, ARG_NO_BLOCK },
|
|
|
|
{ "no-pager", no_argument, NULL, ARG_NO_PAGER },
|
2020-01-09 06:49:11 +01:00
|
|
|
{ "no-legend", no_argument, NULL, ARG_NO_LEGEND },
|
2020-01-10 04:07:28 +01:00
|
|
|
{ "full", no_argument, NULL, 'l' },
|
2016-08-06 00:32:46 +02:00
|
|
|
{ "no-ask-password", no_argument, NULL, ARG_NO_ASK_PASSWORD },
|
|
|
|
{ "quiet", no_argument, NULL, 'q' },
|
|
|
|
{ "user", no_argument, NULL, ARG_USER },
|
|
|
|
{ "system", no_argument, NULL, ARG_SYSTEM },
|
|
|
|
{ "host", required_argument, NULL, 'H' },
|
|
|
|
{ "machine", required_argument, NULL, 'M' },
|
|
|
|
{ "discover", no_argument, NULL, ARG_DISCOVER },
|
|
|
|
{ "type", required_argument, NULL, 't' },
|
|
|
|
{ "options", required_argument, NULL, 'o' },
|
2017-12-09 10:18:48 +01:00
|
|
|
{ "owner", required_argument, NULL, ARG_OWNER },
|
2017-02-06 14:25:15 +01:00
|
|
|
{ "fsck", required_argument, NULL, ARG_FSCK },
|
2016-08-06 00:32:46 +02:00
|
|
|
{ "description", required_argument, NULL, ARG_DESCRIPTION },
|
|
|
|
{ "property", required_argument, NULL, 'p' },
|
|
|
|
{ "automount", required_argument, NULL, ARG_AUTOMOUNT },
|
|
|
|
{ "timeout-idle-sec", required_argument, NULL, ARG_TIMEOUT_IDLE },
|
|
|
|
{ "automount-property", required_argument, NULL, ARG_AUTOMOUNT_PROPERTY },
|
|
|
|
{ "bind-device", no_argument, NULL, ARG_BIND_DEVICE },
|
|
|
|
{ "list", no_argument, NULL, ARG_LIST },
|
2017-02-04 16:21:49 +01:00
|
|
|
{ "umount", no_argument, NULL, 'u' },
|
|
|
|
{ "unmount", no_argument, NULL, 'u' },
|
2017-11-16 07:40:25 +01:00
|
|
|
{ "collect", no_argument, NULL, 'G' },
|
2016-08-06 00:32:46 +02:00
|
|
|
{},
|
|
|
|
};
|
|
|
|
|
|
|
|
int r, c;
|
|
|
|
|
|
|
|
assert(argc >= 0);
|
|
|
|
assert(argv);
|
|
|
|
|
2017-02-04 16:21:49 +01:00
|
|
|
if (strstr(program_invocation_short_name, "systemd-umount"))
|
|
|
|
arg_action = ACTION_UMOUNT;
|
|
|
|
|
2020-01-10 04:07:28 +01:00
|
|
|
while ((c = getopt_long(argc, argv, "hqH:M:t:o:p:AuGl", options, NULL)) >= 0)
|
2016-08-06 00:32:46 +02:00
|
|
|
|
|
|
|
switch (c) {
|
|
|
|
|
|
|
|
case 'h':
|
2018-08-09 10:32:31 +02:00
|
|
|
return help();
|
2016-08-06 00:32:46 +02:00
|
|
|
|
|
|
|
case ARG_VERSION:
|
|
|
|
return version();
|
|
|
|
|
|
|
|
case ARG_NO_BLOCK:
|
|
|
|
arg_no_block = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ARG_NO_PAGER:
|
2018-11-11 12:56:29 +01:00
|
|
|
arg_pager_flags |= PAGER_DISABLE;
|
2016-08-06 00:32:46 +02:00
|
|
|
break;
|
|
|
|
|
2020-01-09 06:49:11 +01:00
|
|
|
case ARG_NO_LEGEND:
|
|
|
|
arg_legend = false;
|
|
|
|
break;
|
|
|
|
|
2020-01-10 04:07:28 +01:00
|
|
|
case 'l':
|
|
|
|
arg_full = true;
|
|
|
|
break;
|
|
|
|
|
2016-08-06 00:32:46 +02:00
|
|
|
case ARG_NO_ASK_PASSWORD:
|
|
|
|
arg_ask_password = false;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'q':
|
|
|
|
arg_quiet = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ARG_USER:
|
|
|
|
arg_user = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ARG_SYSTEM:
|
|
|
|
arg_user = false;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'H':
|
|
|
|
arg_transport = BUS_TRANSPORT_REMOTE;
|
|
|
|
arg_host = optarg;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'M':
|
|
|
|
arg_transport = BUS_TRANSPORT_MACHINE;
|
|
|
|
arg_host = optarg;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ARG_DISCOVER:
|
|
|
|
arg_discover = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 't':
|
|
|
|
if (free_and_strdup(&arg_mount_type, optarg) < 0)
|
|
|
|
return log_oom();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'o':
|
|
|
|
if (free_and_strdup(&arg_mount_options, optarg) < 0)
|
|
|
|
return log_oom();
|
|
|
|
break;
|
|
|
|
|
2017-12-09 10:18:48 +01:00
|
|
|
case ARG_OWNER: {
|
|
|
|
const char *user = optarg;
|
|
|
|
|
2018-08-02 18:36:47 +02:00
|
|
|
r = get_user_creds(&user, &arg_uid, &arg_gid, NULL, NULL, 0);
|
2017-12-09 10:18:48 +01:00
|
|
|
if (r < 0)
|
|
|
|
return log_error_errno(r,
|
|
|
|
r == -EBADMSG ? "UID or GID of user %s are invalid."
|
|
|
|
: "Cannot use \"%s\" as owner: %m",
|
|
|
|
optarg);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2016-08-06 00:32:46 +02:00
|
|
|
case ARG_FSCK:
|
|
|
|
r = parse_boolean(optarg);
|
|
|
|
if (r < 0)
|
|
|
|
return log_error_errno(r, "Failed to parse --fsck= argument: %s", optarg);
|
|
|
|
|
|
|
|
arg_fsck = r;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ARG_DESCRIPTION:
|
|
|
|
if (free_and_strdup(&arg_description, optarg) < 0)
|
|
|
|
return log_oom();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'p':
|
|
|
|
if (strv_extend(&arg_property, optarg) < 0)
|
|
|
|
return log_oom();
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'A':
|
|
|
|
arg_action = ACTION_AUTOMOUNT;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ARG_AUTOMOUNT:
|
|
|
|
r = parse_boolean(optarg);
|
|
|
|
if (r < 0)
|
|
|
|
return log_error_errno(r, "--automount= expects a valid boolean parameter: %s", optarg);
|
|
|
|
|
|
|
|
arg_action = r ? ACTION_AUTOMOUNT : ACTION_MOUNT;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ARG_TIMEOUT_IDLE:
|
|
|
|
r = parse_sec(optarg, &arg_timeout_idle);
|
|
|
|
if (r < 0)
|
|
|
|
return log_error_errno(r, "Failed to parse timeout: %s", optarg);
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ARG_AUTOMOUNT_PROPERTY:
|
|
|
|
if (strv_extend(&arg_automount_property, optarg) < 0)
|
|
|
|
return log_oom();
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ARG_BIND_DEVICE:
|
|
|
|
arg_bind_device = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ARG_LIST:
|
|
|
|
arg_action = ACTION_LIST;
|
|
|
|
break;
|
|
|
|
|
2017-02-04 16:21:49 +01:00
|
|
|
case 'u':
|
|
|
|
arg_action = ACTION_UMOUNT;
|
|
|
|
break;
|
|
|
|
|
2017-11-16 07:40:25 +01:00
|
|
|
case 'G':
|
|
|
|
arg_aggressive_gc = true;
|
|
|
|
break;
|
|
|
|
|
2016-08-06 00:32:46 +02:00
|
|
|
case '?':
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
default:
|
|
|
|
assert_not_reached("Unhandled option");
|
|
|
|
}
|
|
|
|
|
2018-11-20 23:40:44 +01:00
|
|
|
if (arg_user && arg_transport != BUS_TRANSPORT_LOCAL)
|
|
|
|
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
|
|
|
|
"Execution in user context is not supported on non-local systems.");
|
2016-08-06 00:32:46 +02:00
|
|
|
|
|
|
|
if (arg_action == ACTION_LIST) {
|
2018-11-20 23:40:44 +01:00
|
|
|
if (optind < argc)
|
|
|
|
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
|
|
|
|
"Too many arguments.");
|
2016-08-06 00:32:46 +02:00
|
|
|
|
2018-11-20 23:40:44 +01:00
|
|
|
if (arg_transport != BUS_TRANSPORT_LOCAL)
|
|
|
|
return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP),
|
|
|
|
"Listing devices only supported locally.");
|
2017-06-26 22:07:07 +02:00
|
|
|
} else if (arg_action == ACTION_UMOUNT) {
|
2018-11-20 23:40:44 +01:00
|
|
|
if (optind >= argc)
|
|
|
|
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
|
|
|
|
"At least one argument required.");
|
2017-06-26 22:07:07 +02:00
|
|
|
|
|
|
|
if (arg_transport != BUS_TRANSPORT_LOCAL) {
|
2017-07-05 14:55:39 +02:00
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = optind; i < argc; i++)
|
2018-11-20 23:40:44 +01:00
|
|
|
if (!path_is_absolute(argv[i]) )
|
|
|
|
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
|
|
|
|
"Only absolute path is supported: %s", argv[i]);
|
2017-06-26 22:07:07 +02:00
|
|
|
}
|
2016-08-06 00:32:46 +02:00
|
|
|
} else {
|
2018-11-20 23:40:44 +01:00
|
|
|
if (optind >= argc)
|
|
|
|
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
|
|
|
|
"At least one argument required.");
|
2016-08-06 00:32:46 +02:00
|
|
|
|
2018-11-20 23:40:44 +01:00
|
|
|
if (argc > optind+2)
|
|
|
|
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
|
|
|
|
"At most two arguments required.");
|
2016-08-06 00:32:46 +02:00
|
|
|
|
2020-06-08 19:25:18 +02:00
|
|
|
if (arg_mount_type && !fstype_is_blockdev_backed(arg_mount_type)) {
|
2017-09-04 03:55:51 +02:00
|
|
|
arg_mount_what = strdup(argv[optind]);
|
|
|
|
if (!arg_mount_what)
|
|
|
|
return log_oom();
|
|
|
|
|
|
|
|
} else if (arg_transport == BUS_TRANSPORT_LOCAL) {
|
2017-11-30 17:28:44 +01:00
|
|
|
_cleanup_free_ char *u = NULL;
|
2016-08-06 00:32:46 +02:00
|
|
|
|
2017-06-29 07:53:49 +02:00
|
|
|
u = fstab_node_to_udev_node(argv[optind]);
|
|
|
|
if (!u)
|
|
|
|
return log_oom();
|
|
|
|
|
2019-10-24 10:33:20 +02:00
|
|
|
r = chase_symlinks(u, NULL, 0, &arg_mount_what, NULL);
|
2016-08-06 00:32:46 +02:00
|
|
|
if (r < 0)
|
2017-11-16 07:55:42 +01:00
|
|
|
return log_error_errno(r, "Failed to make path %s absolute: %m", u);
|
2017-06-29 07:53:49 +02:00
|
|
|
} else {
|
2017-09-01 10:03:01 +02:00
|
|
|
arg_mount_what = strdup(argv[optind]);
|
2017-06-29 07:53:49 +02:00
|
|
|
if (!arg_mount_what)
|
|
|
|
return log_oom();
|
|
|
|
|
2018-05-31 16:39:31 +02:00
|
|
|
path_simplify(arg_mount_what, false);
|
2017-06-29 07:53:49 +02:00
|
|
|
|
2018-11-20 23:40:44 +01:00
|
|
|
if (!path_is_absolute(arg_mount_what))
|
|
|
|
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
|
|
|
|
"Only absolute path is supported: %s", arg_mount_what);
|
2017-06-29 07:53:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (argc > optind+1) {
|
|
|
|
if (arg_transport == BUS_TRANSPORT_LOCAL) {
|
2019-10-24 10:33:20 +02:00
|
|
|
r = chase_symlinks(argv[optind+1], NULL, CHASE_NONEXISTENT, &arg_mount_where, NULL);
|
2017-06-29 07:53:49 +02:00
|
|
|
if (r < 0)
|
2017-11-16 07:55:42 +01:00
|
|
|
return log_error_errno(r, "Failed to make path %s absolute: %m", argv[optind+1]);
|
2017-06-29 07:53:49 +02:00
|
|
|
} else {
|
|
|
|
arg_mount_where = strdup(argv[optind+1]);
|
|
|
|
if (!arg_mount_where)
|
|
|
|
return log_oom();
|
|
|
|
|
2018-05-31 16:39:31 +02:00
|
|
|
path_simplify(arg_mount_where, false);
|
2017-06-29 07:53:49 +02:00
|
|
|
|
2018-11-20 23:40:44 +01:00
|
|
|
if (!path_is_absolute(arg_mount_where))
|
|
|
|
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
|
|
|
|
"Only absolute path is supported: %s", arg_mount_where);
|
2017-06-29 07:53:49 +02:00
|
|
|
}
|
2016-08-06 00:32:46 +02:00
|
|
|
} else
|
|
|
|
arg_discover = true;
|
|
|
|
|
2018-11-20 23:40:44 +01:00
|
|
|
if (arg_discover && arg_transport != BUS_TRANSPORT_LOCAL)
|
|
|
|
return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP),
|
|
|
|
"Automatic mount location discovery is only supported locally.");
|
2016-08-06 00:32:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2017-12-23 08:59:56 +01:00
|
|
|
static int transient_unit_set_properties(sd_bus_message *m, UnitType t, char **properties) {
|
2016-08-06 00:32:46 +02:00
|
|
|
int r;
|
|
|
|
|
2016-08-22 08:14:23 +02:00
|
|
|
if (!isempty(arg_description)) {
|
|
|
|
r = sd_bus_message_append(m, "(sv)", "Description", "s", arg_description);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
}
|
2016-08-06 00:32:46 +02:00
|
|
|
|
|
|
|
if (arg_bind_device && is_device_path(arg_mount_what)) {
|
|
|
|
_cleanup_free_ char *device_unit = NULL;
|
|
|
|
|
|
|
|
r = unit_name_from_path(arg_mount_what, ".device", &device_unit);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
r = sd_bus_message_append(m, "(sv)(sv)",
|
|
|
|
"After", "as", 1, device_unit,
|
|
|
|
"BindsTo", "as", 1, device_unit);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2017-11-16 07:40:25 +01:00
|
|
|
if (arg_aggressive_gc) {
|
|
|
|
r = sd_bus_message_append(m, "(sv)", "CollectMode", "s", "inactive-or-failed");
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2017-12-23 08:59:56 +01:00
|
|
|
r = bus_append_unit_property_assignment_many(m, t, properties);
|
2016-08-06 00:32:46 +02:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int transient_mount_set_properties(sd_bus_message *m) {
|
2017-12-09 10:18:48 +01:00
|
|
|
_cleanup_free_ char *options = NULL;
|
2016-08-06 00:32:46 +02:00
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(m);
|
|
|
|
|
2017-12-23 08:59:56 +01:00
|
|
|
r = transient_unit_set_properties(m, UNIT_MOUNT, arg_property);
|
2016-08-06 00:32:46 +02:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
if (arg_mount_what) {
|
|
|
|
r = sd_bus_message_append(m, "(sv)", "What", "s", arg_mount_what);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (arg_mount_type) {
|
|
|
|
r = sd_bus_message_append(m, "(sv)", "Type", "s", arg_mount_type);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2017-12-09 10:18:48 +01:00
|
|
|
/* Prepend uid=…,gid=… if arg_uid is set */
|
|
|
|
if (arg_uid != UID_INVALID) {
|
|
|
|
r = asprintf(&options,
|
|
|
|
"uid=" UID_FMT ",gid=" GID_FMT "%s%s",
|
|
|
|
arg_uid, arg_gid,
|
2018-06-19 04:00:56 +02:00
|
|
|
arg_mount_options ? "," : "", strempty(arg_mount_options));
|
2016-08-06 00:32:46 +02:00
|
|
|
if (r < 0)
|
2017-12-09 10:18:48 +01:00
|
|
|
return -ENOMEM;
|
2016-08-06 00:32:46 +02:00
|
|
|
}
|
|
|
|
|
2017-12-09 10:18:48 +01:00
|
|
|
if (options || arg_mount_options) {
|
|
|
|
log_debug("Using mount options: %s", options ?: arg_mount_options);
|
|
|
|
|
|
|
|
r = sd_bus_message_append(m, "(sv)", "Options", "s", options ?: arg_mount_options);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
} else
|
|
|
|
log_debug("Not using any mount options");
|
|
|
|
|
2016-08-06 00:32:46 +02:00
|
|
|
if (arg_fsck) {
|
|
|
|
_cleanup_free_ char *fsck = NULL;
|
|
|
|
|
|
|
|
r = unit_name_from_path_instance("systemd-fsck", arg_mount_what, ".service", &fsck);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
r = sd_bus_message_append(m,
|
|
|
|
"(sv)(sv)",
|
|
|
|
"Requires", "as", 1, fsck,
|
|
|
|
"After", "as", 1, fsck);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int transient_automount_set_properties(sd_bus_message *m) {
|
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(m);
|
|
|
|
|
2017-12-23 08:59:56 +01:00
|
|
|
r = transient_unit_set_properties(m, UNIT_AUTOMOUNT, arg_automount_property);
|
2016-08-06 00:32:46 +02:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
if (arg_timeout_idle != USEC_INFINITY) {
|
|
|
|
r = sd_bus_message_append(m, "(sv)", "TimeoutIdleUSec", "t", arg_timeout_idle);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int start_transient_mount(
|
|
|
|
sd_bus *bus,
|
|
|
|
char **argv) {
|
|
|
|
|
|
|
|
_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, ".mount", &mount_unit);
|
|
|
|
if (r < 0)
|
|
|
|
return log_error_errno(r, "Failed to make mount unit name: %m");
|
|
|
|
|
2020-05-03 08:11:33 +02:00
|
|
|
r = bus_message_new_method_call(bus, &m, bus_systemd_mgr, "StartTransientUnit");
|
2016-08-06 00:32:46 +02:00
|
|
|
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);
|
|
|
|
|
|
|
|
/* Properties */
|
|
|
|
r = sd_bus_message_open_container(m, 'a', "(sv)");
|
|
|
|
if (r < 0)
|
|
|
|
return bus_log_create_error(r);
|
|
|
|
|
|
|
|
r = transient_mount_set_properties(m);
|
|
|
|
if (r < 0)
|
|
|
|
return bus_log_create_error(r);
|
|
|
|
|
|
|
|
r = sd_bus_message_close_container(m);
|
|
|
|
if (r < 0)
|
|
|
|
return bus_log_create_error(r);
|
|
|
|
|
|
|
|
/* Auxiliary units */
|
|
|
|
r = sd_bus_message_append(m, "a(sa(sv))", 0);
|
|
|
|
if (r < 0)
|
|
|
|
return bus_log_create_error(r);
|
|
|
|
|
2017-10-30 09:57:53 +01:00
|
|
|
polkit_agent_open_if_enabled(arg_transport, arg_ask_password);
|
2016-08-06 00:32:46 +02:00
|
|
|
|
|
|
|
r = sd_bus_call(bus, m, 0, &error, &reply);
|
|
|
|
if (r < 0)
|
|
|
|
return log_error_errno(r, "Failed to start transient 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("Started 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 start_transient_automount(
|
|
|
|
sd_bus *bus,
|
|
|
|
char **argv) {
|
|
|
|
|
|
|
|
_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 *automount_unit = NULL, *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, ".automount", &automount_unit);
|
|
|
|
if (r < 0)
|
|
|
|
return log_error_errno(r, "Failed to make automount unit name: %m");
|
|
|
|
|
|
|
|
r = unit_name_from_path(arg_mount_where, ".mount", &mount_unit);
|
|
|
|
if (r < 0)
|
|
|
|
return log_error_errno(r, "Failed to make mount unit name: %m");
|
|
|
|
|
2020-05-03 08:11:33 +02:00
|
|
|
r = bus_message_new_method_call(bus, &m, bus_systemd_mgr, "StartTransientUnit");
|
2016-08-06 00:32:46 +02:00
|
|
|
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", automount_unit, "fail");
|
|
|
|
if (r < 0)
|
|
|
|
return bus_log_create_error(r);
|
|
|
|
|
|
|
|
/* Properties */
|
|
|
|
r = sd_bus_message_open_container(m, 'a', "(sv)");
|
|
|
|
if (r < 0)
|
|
|
|
return bus_log_create_error(r);
|
|
|
|
|
|
|
|
r = transient_automount_set_properties(m);
|
|
|
|
if (r < 0)
|
|
|
|
return bus_log_create_error(r);
|
|
|
|
|
|
|
|
r = sd_bus_message_close_container(m);
|
|
|
|
if (r < 0)
|
|
|
|
return bus_log_create_error(r);
|
|
|
|
|
|
|
|
/* Auxiliary units */
|
|
|
|
r = sd_bus_message_open_container(m, 'a', "(sa(sv))");
|
|
|
|
if (r < 0)
|
|
|
|
return bus_log_create_error(r);
|
|
|
|
|
|
|
|
r = sd_bus_message_open_container(m, 'r', "sa(sv)");
|
|
|
|
if (r < 0)
|
|
|
|
return bus_log_create_error(r);
|
|
|
|
|
|
|
|
r = sd_bus_message_append(m, "s", mount_unit);
|
|
|
|
if (r < 0)
|
|
|
|
return bus_log_create_error(r);
|
|
|
|
|
|
|
|
r = sd_bus_message_open_container(m, 'a', "(sv)");
|
|
|
|
if (r < 0)
|
|
|
|
return bus_log_create_error(r);
|
|
|
|
|
|
|
|
r = transient_mount_set_properties(m);
|
|
|
|
if (r < 0)
|
|
|
|
return bus_log_create_error(r);
|
|
|
|
|
|
|
|
r = sd_bus_message_close_container(m);
|
|
|
|
if (r < 0)
|
|
|
|
return bus_log_create_error(r);
|
|
|
|
|
|
|
|
r = sd_bus_message_close_container(m);
|
|
|
|
if (r < 0)
|
|
|
|
return bus_log_create_error(r);
|
|
|
|
|
|
|
|
r = sd_bus_message_close_container(m);
|
|
|
|
if (r < 0)
|
|
|
|
return bus_log_create_error(r);
|
|
|
|
|
2017-10-30 09:57:53 +01:00
|
|
|
polkit_agent_open_if_enabled(arg_transport, arg_ask_password);
|
2016-08-06 00:32:46 +02:00
|
|
|
|
|
|
|
r = sd_bus_call(bus, m, 0, &error, &reply);
|
|
|
|
if (r < 0)
|
|
|
|
return log_error_errno(r, "Failed to start transient automount 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("Started unit %s%s%s for mount point: %s%s%s",
|
|
|
|
ansi_highlight(), automount_unit, ansi_normal(),
|
|
|
|
ansi_highlight(), arg_mount_where, ansi_normal());
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-07-05 14:54:40 +02:00
|
|
|
static int find_mount_points(const char *what, char ***list) {
|
2019-04-04 13:41:47 +02:00
|
|
|
_cleanup_(mnt_free_tablep) struct libmnt_table *table = NULL;
|
|
|
|
_cleanup_(mnt_free_iterp) struct libmnt_iter *iter = NULL;
|
2017-07-05 14:54:40 +02:00
|
|
|
_cleanup_strv_free_ char **l = NULL;
|
|
|
|
size_t bufsize = 0, n = 0;
|
2019-04-04 13:41:47 +02:00
|
|
|
int r;
|
2017-07-05 14:54:40 +02:00
|
|
|
|
|
|
|
assert(what);
|
|
|
|
assert(list);
|
|
|
|
|
|
|
|
/* Returns all mount points obtained from /proc/self/mountinfo in *list,
|
|
|
|
* and the number of mount points as return value. */
|
|
|
|
|
2019-04-05 14:43:06 +02:00
|
|
|
r = libmount_parse(NULL, NULL, &table, &iter);
|
2019-04-04 13:41:47 +02:00
|
|
|
if (r < 0)
|
|
|
|
return log_error_errno(r, "Failed to parse /proc/self/mountinfo: %m");
|
2017-07-05 14:54:40 +02:00
|
|
|
|
|
|
|
for (;;) {
|
2019-04-04 13:41:47 +02:00
|
|
|
struct libmnt_fs *fs;
|
|
|
|
const char *source, *target;
|
2017-07-05 14:54:40 +02:00
|
|
|
|
2019-04-04 13:41:47 +02:00
|
|
|
r = mnt_table_next_fs(table, iter, &fs);
|
|
|
|
if (r == 1)
|
|
|
|
break;
|
|
|
|
if (r < 0)
|
|
|
|
return log_error_errno(r, "Failed to get next entry from /proc/self/mountinfo: %m");
|
2017-07-05 14:54:40 +02:00
|
|
|
|
2019-04-04 13:41:47 +02:00
|
|
|
source = mnt_fs_get_source(fs);
|
|
|
|
target = mnt_fs_get_target(fs);
|
|
|
|
if (!source || !target)
|
2017-07-05 14:54:40 +02:00
|
|
|
continue;
|
|
|
|
|
2019-04-04 13:41:47 +02:00
|
|
|
if (!path_equal(source, what))
|
2017-07-05 14:54:40 +02:00
|
|
|
continue;
|
|
|
|
|
|
|
|
/* one extra slot is needed for the terminating NULL */
|
2019-04-04 13:41:47 +02:00
|
|
|
if (!GREEDY_REALLOC0(l, bufsize, n + 2))
|
2017-07-05 14:54:40 +02:00
|
|
|
return log_oom();
|
|
|
|
|
2019-04-04 13:41:47 +02:00
|
|
|
l[n] = strdup(target);
|
|
|
|
if (!l[n])
|
|
|
|
return log_oom();
|
|
|
|
n++;
|
2017-07-05 14:54:40 +02:00
|
|
|
}
|
|
|
|
|
2019-04-04 13:41:47 +02:00
|
|
|
if (!GREEDY_REALLOC0(l, bufsize, n + 1))
|
2017-07-10 21:36:59 +02:00
|
|
|
return log_oom();
|
|
|
|
|
2018-03-22 16:53:26 +01:00
|
|
|
*list = TAKE_PTR(l);
|
2017-07-05 14:54:40 +02:00
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int find_loop_device(const char *backing_file, char **loop_dev) {
|
|
|
|
_cleanup_closedir_ DIR *d = NULL;
|
|
|
|
struct dirent *de;
|
|
|
|
_cleanup_free_ char *l = NULL;
|
|
|
|
|
|
|
|
assert(backing_file);
|
|
|
|
assert(loop_dev);
|
|
|
|
|
|
|
|
d = opendir("/sys/devices/virtual/block");
|
2017-07-10 21:39:23 +02:00
|
|
|
if (!d)
|
|
|
|
return -errno;
|
2017-07-05 14:54:40 +02:00
|
|
|
|
|
|
|
FOREACH_DIRENT(de, d, return -errno) {
|
|
|
|
_cleanup_free_ char *sys = NULL, *fname = NULL;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
dirent_ensure_type(d, de);
|
|
|
|
|
|
|
|
if (de->d_type != DT_DIR)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!startswith(de->d_name, "loop"))
|
|
|
|
continue;
|
|
|
|
|
2019-06-20 20:07:01 +02:00
|
|
|
sys = path_join("/sys/devices/virtual/block", de->d_name, "loop/backing_file");
|
2017-07-05 14:54:40 +02:00
|
|
|
if (!sys)
|
2017-07-10 21:39:23 +02:00
|
|
|
return -ENOMEM;
|
2017-07-05 14:54:40 +02:00
|
|
|
|
|
|
|
r = read_one_line_file(sys, &fname);
|
2017-07-10 21:41:02 +02:00
|
|
|
if (r < 0) {
|
|
|
|
log_debug_errno(r, "Failed to read %s, ignoring: %m", sys);
|
2017-07-05 14:54:40 +02:00
|
|
|
continue;
|
2017-07-10 21:41:02 +02:00
|
|
|
}
|
2017-07-05 14:54:40 +02:00
|
|
|
|
|
|
|
if (files_same(fname, backing_file, 0) <= 0)
|
|
|
|
continue;
|
|
|
|
|
2019-06-20 20:07:01 +02:00
|
|
|
l = path_join("/dev", de->d_name);
|
2017-07-05 14:54:40 +02:00
|
|
|
if (!l)
|
2017-07-10 21:39:23 +02:00
|
|
|
return -ENOMEM;
|
2017-07-05 14:54:40 +02:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!l)
|
2017-07-10 21:41:14 +02:00
|
|
|
return -ENXIO;
|
2017-07-05 14:54:40 +02:00
|
|
|
|
2018-03-22 16:53:26 +01:00
|
|
|
*loop_dev = TAKE_PTR(l);
|
2017-07-05 14:54:40 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-02-04 16:21:49 +01:00
|
|
|
static int stop_mount(
|
|
|
|
sd_bus *bus,
|
2017-06-26 22:07:07 +02:00
|
|
|
const char *where,
|
2017-06-28 20:19:38 +02:00
|
|
|
const char *suffix) {
|
2017-02-04 16:21:49 +01:00
|
|
|
|
|
|
|
_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");
|
|
|
|
}
|
|
|
|
|
2017-06-26 22:07:07 +02:00
|
|
|
r = unit_name_from_path(where, suffix, &mount_unit);
|
2017-02-04 16:21:49 +01:00
|
|
|
if (r < 0)
|
2017-11-30 09:55:04 +01:00
|
|
|
return log_error_errno(r, "Failed to make %s unit name from path %s: %m", suffix + 1, where);
|
2017-02-04 16:21:49 +01:00
|
|
|
|
2020-05-03 08:11:33 +02:00
|
|
|
r = bus_message_new_method_call(bus, &m, bus_systemd_mgr, "StopUnit");
|
2017-02-04 16:21:49 +01:00
|
|
|
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);
|
|
|
|
|
2017-10-30 09:57:53 +01:00
|
|
|
polkit_agent_open_if_enabled(arg_transport, arg_ask_password);
|
2017-02-04 16:21:49 +01:00
|
|
|
|
|
|
|
r = sd_bus_call(bus, m, 0, &error, &reply);
|
2017-11-30 09:55:04 +01:00
|
|
|
if (r < 0) {
|
|
|
|
if (streq(suffix, ".automount") &&
|
|
|
|
sd_bus_error_has_name(&error, "org.freedesktop.systemd1.NoSuchUnit"))
|
|
|
|
return 0;
|
|
|
|
return log_error_errno(r, "Failed to stop %s unit: %s", suffix + 1, bus_error_message(&error, r));
|
|
|
|
}
|
2017-02-04 16:21:49 +01:00
|
|
|
|
|
|
|
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(),
|
2017-06-26 22:07:07 +02:00
|
|
|
ansi_highlight(), where, ansi_normal());
|
2017-02-04 16:21:49 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int stop_mounts(
|
|
|
|
sd_bus *bus,
|
2017-06-28 20:19:38 +02:00
|
|
|
const char *where) {
|
2017-02-04 16:21:49 +01:00
|
|
|
|
|
|
|
int r;
|
|
|
|
|
2018-11-20 23:40:44 +01:00
|
|
|
if (path_equal(where, "/"))
|
|
|
|
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
|
|
|
|
"Refusing to operate on root directory: %s", where);
|
2017-06-26 22:07:07 +02:00
|
|
|
|
2018-11-20 23:40:44 +01:00
|
|
|
if (!path_is_normalized(where))
|
|
|
|
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
|
|
|
|
"Path contains non-normalized components: %s", where);
|
2017-06-26 22:07:07 +02:00
|
|
|
|
2017-06-28 20:19:38 +02:00
|
|
|
r = stop_mount(bus, where, ".mount");
|
2017-02-04 16:21:49 +01:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
2017-06-28 20:19:38 +02:00
|
|
|
r = stop_mount(bus, where, ".automount");
|
2017-02-04 16:21:49 +01:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-06-26 22:07:07 +02:00
|
|
|
static int umount_by_device(sd_bus *bus, const char *what) {
|
2018-08-22 06:36:15 +02:00
|
|
|
_cleanup_(sd_device_unrefp) sd_device *d = NULL;
|
2017-07-05 14:54:40 +02:00
|
|
|
_cleanup_strv_free_ char **list = NULL;
|
2017-06-26 22:07:07 +02:00
|
|
|
struct stat st;
|
|
|
|
const char *v;
|
2017-07-05 14:54:40 +02:00
|
|
|
char **l;
|
2017-06-28 20:19:38 +02:00
|
|
|
int r, r2 = 0;
|
2017-06-26 22:07:07 +02:00
|
|
|
|
|
|
|
assert(what);
|
|
|
|
|
|
|
|
if (stat(what, &st) < 0)
|
|
|
|
return log_error_errno(errno, "Can't stat %s: %m", what);
|
|
|
|
|
2020-10-09 14:59:44 +02:00
|
|
|
if (!S_ISBLK(st.st_mode))
|
|
|
|
return log_error_errno(SYNTHETIC_ERRNO(ENOTBLK),
|
|
|
|
"Not a block device: %s", what);
|
2017-06-26 22:07:07 +02:00
|
|
|
|
2018-08-22 06:36:15 +02:00
|
|
|
r = sd_device_new_from_devnum(&d, 'b', st.st_rdev);
|
|
|
|
if (r < 0)
|
|
|
|
return log_error_errno(r, "Failed to get device from device number: %m");
|
2017-06-26 22:07:07 +02:00
|
|
|
|
2018-08-22 06:36:15 +02:00
|
|
|
r = sd_device_get_property_value(d, "ID_FS_USAGE", &v);
|
|
|
|
if (r < 0)
|
2018-10-22 05:57:37 +02:00
|
|
|
return log_device_error_errno(d, r, "Failed to get device property: %m");
|
2017-06-26 22:07:07 +02:00
|
|
|
|
2020-07-16 18:04:45 +02:00
|
|
|
if (!streq(v, "filesystem"))
|
|
|
|
return log_device_error_errno(d, SYNTHETIC_ERRNO(EINVAL),
|
|
|
|
"%s does not contain a known file system.", what);
|
2017-06-26 22:07:07 +02:00
|
|
|
|
2018-08-22 06:36:15 +02:00
|
|
|
if (sd_device_get_property_value(d, "SYSTEMD_MOUNT_WHERE", &v) >= 0)
|
2017-07-05 14:54:40 +02:00
|
|
|
r2 = stop_mounts(bus, v);
|
2017-06-26 22:07:07 +02:00
|
|
|
|
2017-07-05 14:54:40 +02:00
|
|
|
r = find_mount_points(what, &list);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
for (l = list; *l; l++) {
|
|
|
|
r = stop_mounts(bus, *l);
|
2017-06-28 20:19:38 +02:00
|
|
|
if (r < 0)
|
2017-07-05 14:54:40 +02:00
|
|
|
r2 = r;
|
2017-06-26 22:07:07 +02:00
|
|
|
}
|
|
|
|
|
2017-06-28 20:19:38 +02:00
|
|
|
return r2;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int umount_loop(sd_bus *bus, const char *backing_file) {
|
2017-07-05 14:54:40 +02:00
|
|
|
_cleanup_free_ char *loop_dev = NULL;
|
|
|
|
int r;
|
2017-06-28 20:19:38 +02:00
|
|
|
|
|
|
|
assert(backing_file);
|
|
|
|
|
2017-07-05 14:54:40 +02:00
|
|
|
r = find_loop_device(backing_file, &loop_dev);
|
|
|
|
if (r < 0)
|
2017-07-10 21:41:14 +02:00
|
|
|
return log_error_errno(r, r == -ENXIO ? "File %s is not mounted." : "Can't get loop device for %s: %m", backing_file);
|
2017-06-28 20:19:38 +02:00
|
|
|
|
2017-07-05 14:54:40 +02:00
|
|
|
return umount_by_device(bus, loop_dev);
|
2017-06-26 22:07:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int action_umount(
|
|
|
|
sd_bus *bus,
|
|
|
|
int argc,
|
|
|
|
char **argv) {
|
|
|
|
|
|
|
|
int i, r, r2 = 0;
|
|
|
|
|
2017-07-05 14:55:39 +02:00
|
|
|
if (arg_transport != BUS_TRANSPORT_LOCAL) {
|
|
|
|
for (i = optind; i < argc; i++) {
|
|
|
|
_cleanup_free_ char *p = NULL;
|
|
|
|
|
|
|
|
p = strdup(argv[i]);
|
|
|
|
if (!p)
|
|
|
|
return log_oom();
|
|
|
|
|
2018-05-31 16:39:31 +02:00
|
|
|
path_simplify(p, false);
|
2017-07-05 14:55:39 +02:00
|
|
|
|
|
|
|
r = stop_mounts(bus, p);
|
|
|
|
if (r < 0)
|
|
|
|
r2 = r;
|
|
|
|
}
|
|
|
|
return r2;
|
|
|
|
}
|
|
|
|
|
2017-06-26 22:07:07 +02:00
|
|
|
for (i = optind; i < argc; i++) {
|
2017-11-30 17:28:44 +01:00
|
|
|
_cleanup_free_ char *u = NULL, *p = NULL;
|
2017-06-28 20:19:38 +02:00
|
|
|
struct stat st;
|
2017-06-26 22:07:07 +02:00
|
|
|
|
|
|
|
u = fstab_node_to_udev_node(argv[i]);
|
|
|
|
if (!u)
|
|
|
|
return log_oom();
|
|
|
|
|
2019-10-24 10:33:20 +02:00
|
|
|
r = chase_symlinks(u, NULL, 0, &p, NULL);
|
2017-06-26 22:07:07 +02:00
|
|
|
if (r < 0) {
|
2017-09-07 21:16:19 +02:00
|
|
|
r2 = log_error_errno(r, "Failed to make path %s absolute: %m", argv[i]);
|
2017-06-26 22:07:07 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-06-28 20:19:38 +02:00
|
|
|
if (stat(p, &st) < 0)
|
2017-09-07 21:16:19 +02:00
|
|
|
return log_error_errno(errno, "Can't stat %s (from %s): %m", p, argv[i]);
|
2017-06-26 22:07:07 +02:00
|
|
|
|
2017-06-28 20:19:38 +02:00
|
|
|
if (S_ISBLK(st.st_mode))
|
|
|
|
r = umount_by_device(bus, p);
|
|
|
|
else if (S_ISREG(st.st_mode))
|
|
|
|
r = umount_loop(bus, p);
|
|
|
|
else if (S_ISDIR(st.st_mode))
|
|
|
|
r = stop_mounts(bus, p);
|
|
|
|
else {
|
2017-09-07 21:16:19 +02:00
|
|
|
log_error("Invalid file type: %s (from %s)", p, argv[i]);
|
2017-06-28 20:19:38 +02:00
|
|
|
r = -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (r < 0)
|
2017-06-26 22:07:07 +02:00
|
|
|
r2 = r;
|
|
|
|
}
|
|
|
|
|
|
|
|
return r2;
|
|
|
|
}
|
|
|
|
|
2018-08-22 06:36:15 +02:00
|
|
|
static int acquire_mount_type(sd_device *d) {
|
2016-08-06 00:32:46 +02:00
|
|
|
const char *v;
|
|
|
|
|
|
|
|
assert(d);
|
|
|
|
|
|
|
|
if (arg_mount_type)
|
|
|
|
return 0;
|
|
|
|
|
2018-08-22 06:36:15 +02:00
|
|
|
if (sd_device_get_property_value(d, "ID_FS_TYPE", &v) < 0)
|
2016-08-06 00:32:46 +02:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
arg_mount_type = strdup(v);
|
|
|
|
if (!arg_mount_type)
|
|
|
|
return log_oom();
|
|
|
|
|
|
|
|
log_debug("Discovered type=%s", arg_mount_type);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2018-08-22 06:36:15 +02:00
|
|
|
static int acquire_mount_options(sd_device *d) {
|
2016-08-06 00:32:46 +02:00
|
|
|
const char *v;
|
|
|
|
|
2018-08-22 06:36:15 +02:00
|
|
|
assert(d);
|
|
|
|
|
2016-08-06 00:32:46 +02:00
|
|
|
if (arg_mount_options)
|
|
|
|
return 0;
|
|
|
|
|
2018-08-22 06:36:15 +02:00
|
|
|
if (sd_device_get_property_value(d, "SYSTEMD_MOUNT_OPTIONS", &v) < 0)
|
2016-08-06 00:32:46 +02:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
arg_mount_options = strdup(v);
|
|
|
|
if (!arg_mount_options)
|
|
|
|
return log_oom();
|
|
|
|
|
|
|
|
log_debug("Discovered options=%s", arg_mount_options);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2018-08-22 06:36:15 +02:00
|
|
|
static const char *get_model(sd_device *d) {
|
2016-08-06 00:32:46 +02:00
|
|
|
const char *model;
|
|
|
|
|
|
|
|
assert(d);
|
|
|
|
|
2018-08-22 06:36:15 +02:00
|
|
|
if (sd_device_get_property_value(d, "ID_MODEL_FROM_DATABASE", &model) >= 0)
|
2016-08-06 00:32:46 +02:00
|
|
|
return model;
|
|
|
|
|
2018-08-22 06:36:15 +02:00
|
|
|
if (sd_device_get_property_value(d, "ID_MODEL", &model) >= 0)
|
|
|
|
return model;
|
|
|
|
|
|
|
|
return NULL;
|
2016-08-06 00:32:46 +02:00
|
|
|
}
|
|
|
|
|
2018-08-22 06:36:15 +02:00
|
|
|
static const char* get_label(sd_device *d) {
|
2016-08-06 00:32:46 +02:00
|
|
|
const char *label;
|
|
|
|
|
|
|
|
assert(d);
|
|
|
|
|
2018-08-22 06:36:15 +02:00
|
|
|
if (sd_device_get_property_value(d, "ID_FS_LABEL", &label) >= 0)
|
|
|
|
return label;
|
|
|
|
|
|
|
|
if (sd_device_get_property_value(d, "ID_PART_ENTRY_NAME", &label) >= 0)
|
2016-08-06 00:32:46 +02:00
|
|
|
return label;
|
|
|
|
|
2018-08-22 06:36:15 +02:00
|
|
|
return NULL;
|
2016-08-06 00:32:46 +02:00
|
|
|
}
|
|
|
|
|
2018-08-22 06:36:15 +02:00
|
|
|
static int acquire_mount_where(sd_device *d) {
|
2016-08-06 00:32:46 +02:00
|
|
|
const char *v;
|
|
|
|
|
|
|
|
if (arg_mount_where)
|
|
|
|
return 0;
|
|
|
|
|
2018-08-22 06:36:15 +02:00
|
|
|
if (sd_device_get_property_value(d, "SYSTEMD_MOUNT_WHERE", &v) < 0) {
|
2016-08-06 00:32:46 +02:00
|
|
|
_cleanup_free_ char *escaped = NULL;
|
|
|
|
const char *name;
|
|
|
|
|
|
|
|
name = get_label(d);
|
|
|
|
if (!name)
|
|
|
|
name = get_model(d);
|
|
|
|
if (!name) {
|
|
|
|
const char *dn;
|
|
|
|
|
2018-08-22 06:36:15 +02:00
|
|
|
if (sd_device_get_devname(d, &dn) < 0)
|
2016-08-06 00:32:46 +02:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
name = basename(dn);
|
|
|
|
}
|
|
|
|
|
|
|
|
escaped = xescape(name, "\\");
|
2017-07-05 14:54:40 +02:00
|
|
|
if (!escaped)
|
|
|
|
return log_oom();
|
2016-08-06 00:32:46 +02:00
|
|
|
if (!filename_is_valid(escaped))
|
|
|
|
return 0;
|
|
|
|
|
2019-06-20 20:07:01 +02:00
|
|
|
arg_mount_where = path_join("/run/media/system", escaped);
|
2016-08-06 00:32:46 +02:00
|
|
|
} else
|
|
|
|
arg_mount_where = strdup(v);
|
|
|
|
|
|
|
|
if (!arg_mount_where)
|
|
|
|
return log_oom();
|
|
|
|
|
|
|
|
log_debug("Discovered where=%s", arg_mount_where);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2017-07-05 14:54:40 +02:00
|
|
|
static int acquire_mount_where_for_loop_dev(const char *loop_dev) {
|
|
|
|
_cleanup_strv_free_ char **list = NULL;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
if (arg_mount_where)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
r = find_mount_points(loop_dev, &list);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2018-11-20 23:40:44 +01:00
|
|
|
else if (r == 0)
|
|
|
|
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
|
|
|
|
"Can't find mount point of %s. It is expected that %s is already mounted on a place.",
|
|
|
|
loop_dev, loop_dev);
|
|
|
|
else if (r >= 2)
|
|
|
|
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
|
|
|
|
"%s is mounted on %d places. It is expected that %s is mounted on a place.",
|
|
|
|
loop_dev, r, loop_dev);
|
2017-07-05 14:54:40 +02:00
|
|
|
|
|
|
|
arg_mount_where = strdup(list[0]);
|
|
|
|
if (!arg_mount_where)
|
|
|
|
return log_oom();
|
|
|
|
|
|
|
|
log_debug("Discovered where=%s", arg_mount_where);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2018-08-22 06:36:15 +02:00
|
|
|
static int acquire_description(sd_device *d) {
|
2016-08-06 00:32:46 +02:00
|
|
|
const char *model, *label;
|
|
|
|
|
|
|
|
if (arg_description)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
model = get_model(d);
|
|
|
|
|
|
|
|
label = get_label(d);
|
|
|
|
if (!label)
|
2018-08-22 06:36:15 +02:00
|
|
|
(void) sd_device_get_property_value(d, "ID_PART_ENTRY_NUMBER", &label);
|
2016-08-06 00:32:46 +02:00
|
|
|
|
|
|
|
if (model && label)
|
2016-10-23 17:43:27 +02:00
|
|
|
arg_description = strjoin(model, " ", label);
|
2016-08-06 00:32:46 +02:00
|
|
|
else if (label)
|
|
|
|
arg_description = strdup(label);
|
|
|
|
else if (model)
|
|
|
|
arg_description = strdup(model);
|
|
|
|
else
|
2016-08-22 08:14:23 +02:00
|
|
|
return 0;
|
2016-08-06 00:32:46 +02:00
|
|
|
|
|
|
|
if (!arg_description)
|
|
|
|
return log_oom();
|
|
|
|
|
|
|
|
log_debug("Discovered description=%s", arg_description);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2018-08-22 06:36:15 +02:00
|
|
|
static int acquire_removable(sd_device *d) {
|
2016-08-06 00:32:46 +02:00
|
|
|
const char *v;
|
|
|
|
|
|
|
|
/* Shortcut this if there's no reason to check it */
|
|
|
|
if (arg_action != ACTION_DEFAULT && arg_timeout_idle_set && arg_bind_device >= 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
for (;;) {
|
2018-08-22 06:36:15 +02:00
|
|
|
if (sd_device_get_sysattr_value(d, "removable", &v) > 0)
|
2016-08-06 00:32:46 +02:00
|
|
|
break;
|
|
|
|
|
2018-08-22 06:36:15 +02:00
|
|
|
if (sd_device_get_parent(d, &d) < 0)
|
2016-08-06 00:32:46 +02:00
|
|
|
return 0;
|
|
|
|
|
2018-08-22 06:36:15 +02:00
|
|
|
if (sd_device_get_subsystem(d, &v) < 0 || !streq(v, "block"))
|
2016-08-06 00:32:46 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (parse_boolean(v) <= 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
log_debug("Discovered removable device.");
|
|
|
|
|
|
|
|
if (arg_action == ACTION_DEFAULT) {
|
|
|
|
log_debug("Automatically turning on automount.");
|
|
|
|
arg_action = ACTION_AUTOMOUNT;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!arg_timeout_idle_set) {
|
|
|
|
log_debug("Setting idle timeout to 1s.");
|
|
|
|
arg_timeout_idle = USEC_PER_SEC;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (arg_bind_device < 0) {
|
|
|
|
log_debug("Binding automount unit to device.");
|
|
|
|
arg_bind_device = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2017-07-05 14:54:40 +02:00
|
|
|
static int discover_loop_backing_file(void) {
|
2018-08-22 06:36:15 +02:00
|
|
|
_cleanup_(sd_device_unrefp) sd_device *d = NULL;
|
2017-07-05 14:54:40 +02:00
|
|
|
_cleanup_free_ char *loop_dev = NULL;
|
2016-08-06 00:32:46 +02:00
|
|
|
struct stat st;
|
|
|
|
const char *v;
|
|
|
|
int r;
|
|
|
|
|
2017-07-05 14:54:40 +02:00
|
|
|
r = find_loop_device(arg_mount_what, &loop_dev);
|
2017-07-10 21:41:14 +02:00
|
|
|
if (r < 0 && r != -ENXIO)
|
2017-07-05 14:54:40 +02:00
|
|
|
return log_error_errno(errno, "Can't get loop device for %s: %m", arg_mount_what);
|
|
|
|
|
2017-07-10 21:41:14 +02:00
|
|
|
if (r == -ENXIO) {
|
2017-07-05 14:54:40 +02:00
|
|
|
_cleanup_free_ char *escaped = NULL;
|
|
|
|
|
|
|
|
if (arg_mount_where)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
escaped = xescape(basename(arg_mount_what), "\\");
|
|
|
|
if (!escaped)
|
|
|
|
return log_oom();
|
2020-10-09 14:59:44 +02:00
|
|
|
if (!filename_is_valid(escaped))
|
|
|
|
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
|
|
|
|
"Escaped name %s is not a valid filename.",
|
|
|
|
escaped);
|
2017-07-05 14:54:40 +02:00
|
|
|
|
2019-06-20 20:07:01 +02:00
|
|
|
arg_mount_where = path_join("/run/media/system", escaped);
|
2017-07-05 14:54:40 +02:00
|
|
|
if (!arg_mount_where)
|
|
|
|
return log_oom();
|
|
|
|
|
|
|
|
log_debug("Discovered where=%s", arg_mount_where);
|
2016-08-06 00:32:46 +02:00
|
|
|
return 0;
|
2017-07-05 14:54:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (stat(loop_dev, &st) < 0)
|
|
|
|
return log_error_errno(errno, "Can't stat %s: %m", loop_dev);
|
2016-08-06 00:32:46 +02:00
|
|
|
|
2020-10-09 14:59:44 +02:00
|
|
|
if (!S_ISBLK(st.st_mode))
|
|
|
|
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
|
|
|
|
"Invalid file type: %s", loop_dev);
|
2016-08-06 00:32:46 +02:00
|
|
|
|
2018-08-22 06:36:15 +02:00
|
|
|
r = sd_device_new_from_devnum(&d, 'b', st.st_rdev);
|
|
|
|
if (r < 0)
|
|
|
|
return log_error_errno(r, "Failed to get device from device number: %m");
|
2017-07-05 14:54:40 +02:00
|
|
|
|
2020-07-16 18:04:45 +02:00
|
|
|
if (sd_device_get_property_value(d, "ID_FS_USAGE", &v) < 0 || !streq(v, "filesystem"))
|
|
|
|
return log_device_error_errno(d, SYNTHETIC_ERRNO(EINVAL),
|
|
|
|
"%s does not contain a known file system.", arg_mount_what);
|
2017-07-05 14:54:40 +02:00
|
|
|
|
|
|
|
r = acquire_mount_type(d);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
r = acquire_mount_options(d);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
r = acquire_mount_where_for_loop_dev(loop_dev);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
r = acquire_description(d);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int discover_device(void) {
|
2018-08-22 06:36:15 +02:00
|
|
|
_cleanup_(sd_device_unrefp) sd_device *d = NULL;
|
2017-07-05 14:54:40 +02:00
|
|
|
struct stat st;
|
|
|
|
const char *v;
|
|
|
|
int r;
|
|
|
|
|
2016-08-06 00:32:46 +02:00
|
|
|
if (stat(arg_mount_what, &st) < 0)
|
|
|
|
return log_error_errno(errno, "Can't stat %s: %m", arg_mount_what);
|
|
|
|
|
2017-07-05 14:54:40 +02:00
|
|
|
if (S_ISREG(st.st_mode))
|
|
|
|
return discover_loop_backing_file();
|
|
|
|
|
2020-10-09 14:59:44 +02:00
|
|
|
if (!S_ISBLK(st.st_mode))
|
|
|
|
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
|
|
|
|
"Invalid file type: %s",
|
|
|
|
arg_mount_what);
|
2016-08-06 00:32:46 +02:00
|
|
|
|
2018-08-22 06:36:15 +02:00
|
|
|
r = sd_device_new_from_devnum(&d, 'b', st.st_rdev);
|
|
|
|
if (r < 0)
|
|
|
|
return log_error_errno(r, "Failed to get device from device number: %m");
|
2016-08-06 00:32:46 +02:00
|
|
|
|
2020-10-09 14:59:44 +02:00
|
|
|
if (sd_device_get_property_value(d, "ID_FS_USAGE", &v) < 0 || !streq(v, "filesystem"))
|
|
|
|
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
|
|
|
|
"%s does not contain a known file system.",
|
|
|
|
arg_mount_what);
|
2016-08-06 00:32:46 +02:00
|
|
|
|
|
|
|
r = acquire_mount_type(d);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
r = acquire_mount_options(d);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
r = acquire_mount_where(d);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
r = acquire_description(d);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
r = acquire_removable(d);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
enum {
|
|
|
|
COLUMN_NODE,
|
|
|
|
COLUMN_PATH,
|
|
|
|
COLUMN_MODEL,
|
|
|
|
COLUMN_WWN,
|
|
|
|
COLUMN_FSTYPE,
|
|
|
|
COLUMN_LABEL,
|
|
|
|
COLUMN_UUID,
|
|
|
|
_COLUMN_MAX,
|
|
|
|
};
|
|
|
|
|
|
|
|
static int list_devices(void) {
|
2018-08-22 06:36:15 +02:00
|
|
|
_cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL;
|
2020-01-09 06:39:35 +01:00
|
|
|
_cleanup_(table_unrefp) Table *table = NULL;
|
2018-08-22 06:36:15 +02:00
|
|
|
sd_device *d;
|
2016-08-06 00:32:46 +02:00
|
|
|
unsigned c;
|
|
|
|
int r;
|
|
|
|
|
2018-08-22 06:36:15 +02:00
|
|
|
r = sd_device_enumerator_new(&e);
|
|
|
|
if (r < 0)
|
2016-08-06 00:32:46 +02:00
|
|
|
return log_oom();
|
|
|
|
|
2018-08-22 06:36:15 +02:00
|
|
|
r = sd_device_enumerator_add_match_subsystem(e, "block", true);
|
2016-08-06 00:32:46 +02:00
|
|
|
if (r < 0)
|
|
|
|
return log_error_errno(r, "Failed to add block match: %m");
|
|
|
|
|
2018-08-22 06:36:15 +02:00
|
|
|
r = sd_device_enumerator_add_match_property(e, "ID_FS_USAGE", "filesystem");
|
2016-08-06 00:32:46 +02:00
|
|
|
if (r < 0)
|
|
|
|
return log_error_errno(r, "Failed to add property match: %m");
|
|
|
|
|
2020-01-09 06:39:35 +01:00
|
|
|
table = table_new("NODE", "PATH", "MODEL", "WWN", "TYPE", "LABEL", "UUID");
|
|
|
|
if (!table)
|
|
|
|
return log_oom();
|
2016-08-06 00:32:46 +02:00
|
|
|
|
2020-01-10 04:07:28 +01:00
|
|
|
if (arg_full)
|
|
|
|
table_set_width(table, 0);
|
|
|
|
|
2020-02-14 09:33:43 +01:00
|
|
|
r = table_set_sort(table, (size_t) 0, (size_t) SIZE_MAX);
|
2020-01-09 06:39:35 +01:00
|
|
|
if (r < 0)
|
|
|
|
return log_error_errno(r, "Failed to set sort index: %m");
|
2016-08-06 00:32:46 +02:00
|
|
|
|
2020-01-09 06:49:11 +01:00
|
|
|
table_set_header(table, arg_legend);
|
|
|
|
|
2020-01-09 06:39:35 +01:00
|
|
|
FOREACH_DEVICE(e, d) {
|
2016-08-06 00:32:46 +02:00
|
|
|
for (c = 0; c < _COLUMN_MAX; c++) {
|
2017-02-21 16:59:48 +01:00
|
|
|
const char *x = NULL;
|
2016-08-06 00:32:46 +02:00
|
|
|
|
|
|
|
switch (c) {
|
|
|
|
|
|
|
|
case COLUMN_NODE:
|
2018-08-22 06:36:15 +02:00
|
|
|
(void) sd_device_get_devname(d, &x);
|
2016-08-06 00:32:46 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case COLUMN_PATH:
|
2018-08-22 06:36:15 +02:00
|
|
|
(void) sd_device_get_property_value(d, "ID_PATH", &x);
|
2016-08-06 00:32:46 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case COLUMN_MODEL:
|
|
|
|
x = get_model(d);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case COLUMN_WWN:
|
2018-08-22 06:36:15 +02:00
|
|
|
(void) sd_device_get_property_value(d, "ID_WWN", &x);
|
2016-08-06 00:32:46 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case COLUMN_FSTYPE:
|
2018-08-22 06:36:15 +02:00
|
|
|
(void) sd_device_get_property_value(d, "ID_FS_TYPE", &x);
|
2016-08-06 00:32:46 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case COLUMN_LABEL:
|
|
|
|
x = get_label(d);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case COLUMN_UUID:
|
2018-08-22 06:36:15 +02:00
|
|
|
(void) sd_device_get_property_value(d, "ID_FS_UUID", &x);
|
2016-08-06 00:32:46 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2020-01-09 06:39:35 +01:00
|
|
|
r = table_add_cell(table, NULL, c == COLUMN_NODE ? TABLE_PATH : TABLE_STRING, strna(x));
|
|
|
|
if (r < 0)
|
2020-01-10 10:23:24 +01:00
|
|
|
return table_log_add_error(r);
|
2016-08-06 00:32:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-11 12:56:29 +01:00
|
|
|
(void) pager_open(arg_pager_flags);
|
2016-08-06 00:32:46 +02:00
|
|
|
|
2020-01-09 06:39:35 +01:00
|
|
|
r = table_print(table, NULL);
|
|
|
|
if (r < 0)
|
2020-07-08 09:16:52 +02:00
|
|
|
return table_log_print_error(r);
|
2016-08-06 00:32:46 +02:00
|
|
|
|
2020-01-09 06:39:35 +01:00
|
|
|
return 0;
|
2016-08-06 00:32:46 +02:00
|
|
|
}
|
|
|
|
|
2018-11-20 10:30:13 +01:00
|
|
|
static int run(int argc, char* argv[]) {
|
|
|
|
_cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
|
2016-08-06 00:32:46 +02:00
|
|
|
int r;
|
|
|
|
|
2019-04-26 12:28:25 +02:00
|
|
|
log_show_color(true);
|
2016-08-06 00:32:46 +02:00
|
|
|
log_parse_environment();
|
|
|
|
log_open();
|
|
|
|
|
|
|
|
r = parse_argv(argc, argv);
|
|
|
|
if (r <= 0)
|
2018-11-20 10:30:13 +01:00
|
|
|
return r;
|
2016-08-06 00:32:46 +02:00
|
|
|
|
2018-11-20 10:30:13 +01:00
|
|
|
if (arg_action == ACTION_LIST)
|
|
|
|
return list_devices();
|
2016-08-06 00:32:46 +02:00
|
|
|
|
2017-06-26 22:07:07 +02:00
|
|
|
r = bus_connect_transport_systemd(arg_transport, arg_host, arg_user, &bus);
|
2018-11-20 10:30:13 +01:00
|
|
|
if (r < 0)
|
2020-07-20 13:20:52 +02:00
|
|
|
return bus_log_connect_error(r);
|
2017-06-26 22:07:07 +02:00
|
|
|
|
2018-11-20 10:30:13 +01:00
|
|
|
if (arg_action == ACTION_UMOUNT)
|
|
|
|
return action_umount(bus, argc, argv);
|
2017-06-26 22:07:07 +02:00
|
|
|
|
2020-06-08 19:25:18 +02:00
|
|
|
if ((!arg_mount_type || fstype_is_blockdev_backed(arg_mount_type))
|
2020-10-09 14:59:44 +02:00
|
|
|
&& !path_is_normalized(arg_mount_what))
|
|
|
|
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
|
|
|
|
"Path contains non-normalized components: %s",
|
|
|
|
arg_mount_what);
|
2017-07-05 14:54:40 +02:00
|
|
|
|
|
|
|
if (arg_discover) {
|
|
|
|
r = discover_device();
|
|
|
|
if (r < 0)
|
2018-11-20 10:30:13 +01:00
|
|
|
return r;
|
2017-07-05 14:54:40 +02:00
|
|
|
}
|
|
|
|
|
2020-10-09 14:59:44 +02:00
|
|
|
if (!arg_mount_where)
|
|
|
|
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
|
|
|
|
"Can't figure out where to mount %s.",
|
|
|
|
arg_mount_what);
|
2016-08-06 00:32:46 +02:00
|
|
|
|
2020-10-09 14:59:44 +02:00
|
|
|
if (path_equal(arg_mount_where, "/"))
|
|
|
|
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
|
|
|
|
"Refusing to operate on root directory.");
|
2016-08-06 00:32:46 +02:00
|
|
|
|
2020-10-09 14:59:44 +02:00
|
|
|
if (!path_is_normalized(arg_mount_where))
|
|
|
|
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
|
|
|
|
"Path contains non-normalized components: %s",
|
|
|
|
arg_mount_where);
|
2016-08-06 00:32:46 +02:00
|
|
|
|
|
|
|
if (streq_ptr(arg_mount_type, "auto"))
|
|
|
|
arg_mount_type = mfree(arg_mount_type);
|
|
|
|
if (streq_ptr(arg_mount_options, "defaults"))
|
|
|
|
arg_mount_options = mfree(arg_mount_options);
|
|
|
|
|
|
|
|
if (!is_device_path(arg_mount_what))
|
|
|
|
arg_fsck = false;
|
|
|
|
|
|
|
|
if (arg_fsck && arg_mount_type && arg_transport == BUS_TRANSPORT_LOCAL) {
|
|
|
|
r = fsck_exists(arg_mount_type);
|
|
|
|
if (r < 0)
|
|
|
|
log_warning_errno(r, "Couldn't determine whether fsck for %s exists, proceeding anyway.", arg_mount_type);
|
|
|
|
else if (r == 0) {
|
|
|
|
log_debug("Disabling file system check as fsck for %s doesn't exist.", arg_mount_type);
|
|
|
|
arg_fsck = false; /* fsck doesn't exist, let's not attempt it */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-09 10:18:48 +01:00
|
|
|
/* The kernel (properly) refuses mounting file systems with unknown uid=,gid= options,
|
|
|
|
* but not for all filesystem types. Let's try to catch the cases where the option
|
|
|
|
* would be used if the file system does not support it. It is also possible to
|
|
|
|
* autodetect the file system, but that's only possible with disk-based file systems
|
|
|
|
* which incidentally seem to be implemented more carefully and reject unknown options,
|
|
|
|
* so it's probably OK that we do the check only when the type is specified.
|
|
|
|
*/
|
|
|
|
if (arg_mount_type &&
|
|
|
|
!streq(arg_mount_type, "auto") &&
|
|
|
|
arg_uid != UID_INVALID &&
|
2020-10-09 14:59:44 +02:00
|
|
|
!fstype_can_uid_gid(arg_mount_type))
|
|
|
|
return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP),
|
|
|
|
"File system type %s is not known to support uid=/gid=, refusing.",
|
|
|
|
arg_mount_type);
|
2017-12-09 10:18:48 +01:00
|
|
|
|
2016-08-06 00:32:46 +02:00
|
|
|
switch (arg_action) {
|
|
|
|
|
|
|
|
case ACTION_MOUNT:
|
|
|
|
case ACTION_DEFAULT:
|
|
|
|
r = start_transient_mount(bus, argv + optind);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ACTION_AUTOMOUNT:
|
|
|
|
r = start_transient_automount(bus, argv + optind);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
assert_not_reached("Unexpected action.");
|
|
|
|
}
|
|
|
|
|
2018-11-20 10:30:13 +01:00
|
|
|
return r;
|
2016-08-06 00:32:46 +02:00
|
|
|
}
|
2018-11-20 10:30:13 +01:00
|
|
|
|
|
|
|
DEFINE_MAIN_FUNCTION(run);
|