]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/mount/mount-tool.c
po: Translated using Weblate (Slovenian)
[thirdparty/systemd.git] / src / mount / mount-tool.c
CommitLineData
db9ecf05 1/* SPDX-License-Identifier: LGPL-2.1-or-later */
450442cf
LP
2
3#include <getopt.h>
4
450442cf 5#include "sd-bus.h"
dcd26523 6#include "sd-device.h"
450442cf 7
d6b4d1c7 8#include "build.h"
450442cf 9#include "bus-error.h"
9b71e4ab 10#include "bus-locator.h"
450442cf 11#include "bus-unit-util.h"
e45c81b8 12#include "bus-wait-for-jobs.h"
f461a28d 13#include "chase.h"
8437c059 14#include "device-util.h"
6f6165bf 15#include "dirent-util.h"
450442cf 16#include "escape.h"
9017f5d8 17#include "fd-util.h"
6f6165bf 18#include "fileio.h"
6ae6ea55 19#include "format-table.h"
ca78ad1d 20#include "format-util.h"
f4938c2e 21#include "fs-util.h"
450442cf 22#include "fstab-util.h"
7d991d48 23#include "libmount-util.h"
f2fae6fb 24#include "main-func.h"
e2be442e 25#include "mount-util.h"
049af8ad 26#include "mountpoint-util.h"
450442cf 27#include "pager.h"
599c7c54 28#include "parse-argument.h"
450442cf
LP
29#include "parse-util.h"
30#include "path-util.h"
294bf0c3 31#include "pretty-print.h"
2306d177 32#include "process-util.h"
760877e9 33#include "sort-util.h"
450442cf 34#include "spawn-polkit-agent.h"
6f6165bf 35#include "stat-util.h"
450442cf 36#include "strv.h"
760877e9 37#include "terminal-util.h"
3cc7a9fd 38#include "udev-util.h"
9269296d 39#include "umask-util.h"
89ada3ba 40#include "unit-def.h"
450442cf 41#include "unit-name.h"
e09fc884 42#include "user-util.h"
450442cf
LP
43
44enum {
45 ACTION_DEFAULT,
46 ACTION_MOUNT,
47 ACTION_AUTOMOUNT,
c37fb55b 48 ACTION_UMOUNT,
450442cf
LP
49 ACTION_LIST,
50} arg_action = ACTION_DEFAULT;
51
52static bool arg_no_block = false;
0221d68a 53static PagerFlags arg_pager_flags = 0;
a5279634 54static bool arg_legend = true;
a362c069 55static bool arg_full = false;
450442cf
LP
56static bool arg_ask_password = true;
57static bool arg_quiet = false;
58static BusTransport arg_transport = BUS_TRANSPORT_LOCAL;
4870133b 59static RuntimeScope arg_runtime_scope = RUNTIME_SCOPE_SYSTEM;
450442cf
LP
60static const char *arg_host = NULL;
61static bool arg_discover = false;
62static char *arg_mount_what = NULL;
63static char *arg_mount_where = NULL;
64static char *arg_mount_type = NULL;
65static char *arg_mount_options = NULL;
66static char *arg_description = NULL;
67static char **arg_property = NULL;
68static usec_t arg_timeout_idle = USEC_INFINITY;
69static bool arg_timeout_idle_set = false;
70static char **arg_automount_property = NULL;
71static int arg_bind_device = -1;
e09fc884
ZJS
72static uid_t arg_uid = UID_INVALID;
73static gid_t arg_gid = GID_INVALID;
450442cf 74static bool arg_fsck = true;
dc336483 75static bool arg_aggressive_gc = false;
2322c6c7 76static bool arg_tmpfs = false;
450442cf 77
f2fae6fb
YW
78STATIC_DESTRUCTOR_REGISTER(arg_mount_what, freep);
79STATIC_DESTRUCTOR_REGISTER(arg_mount_where, freep);
80STATIC_DESTRUCTOR_REGISTER(arg_mount_type, freep);
81STATIC_DESTRUCTOR_REGISTER(arg_mount_options, freep);
82STATIC_DESTRUCTOR_REGISTER(arg_description, freep);
83STATIC_DESTRUCTOR_REGISTER(arg_property, strv_freep);
84STATIC_DESTRUCTOR_REGISTER(arg_automount_property, strv_freep);
85
2322c6c7 86static int parse_where(const char *input, char **ret_where) {
2322c6c7
LB
87 int r;
88
89 assert(input);
90 assert(ret_where);
91
92 if (arg_transport == BUS_TRANSPORT_LOCAL) {
660087dc 93 r = chase(input, NULL, CHASE_NONEXISTENT, ret_where, NULL);
2322c6c7
LB
94 if (r < 0)
95 return log_error_errno(r, "Failed to make path %s absolute: %m", input);
96 } else {
660087dc 97 if (!path_is_absolute(input))
2322c6c7
LB
98 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
99 "Path must be absolute when operating remotely: %s",
660087dc
ZJS
100 input);
101
102 r = path_simplify_alloc(input, ret_where);
103 if (r < 0)
104 return log_error_errno(r, "Failed to simplify path %s: %m", input);
2322c6c7
LB
105 }
106
2322c6c7
LB
107 return 0;
108}
109
37ec0fdd
LP
110static int help(void) {
111 _cleanup_free_ char *link = NULL;
112 int r;
113
114 r = terminal_urlify_man("systemd-mount", "1", &link);
115 if (r < 0)
116 return log_oom();
117
9017f5d8 118 printf("systemd-mount [OPTIONS...] WHAT [WHERE]\n"
2322c6c7 119 "systemd-mount [OPTIONS...] --tmpfs [NAME] WHERE\n"
9017f5d8
YW
120 "systemd-mount [OPTIONS...] --list\n"
121 "%s [OPTIONS...] %sWHAT|WHERE...\n\n"
450442cf
LP
122 "Establish a mount or auto-mount point transiently.\n\n"
123 " -h --help Show this help\n"
124 " --version Show package version\n"
125 " --no-block Do not wait until operation finished\n"
126 " --no-pager Do not pipe output into a pager\n"
a5279634 127 " --no-legend Do not show the headers\n"
a362c069 128 " -l --full Do not ellipsize output\n"
450442cf
LP
129 " --no-ask-password Do not prompt for password\n"
130 " -q --quiet Suppress information messages during runtime\n"
131 " --user Run as user unit\n"
132 " -H --host=[USER@]HOST Operate on remote host\n"
133 " -M --machine=CONTAINER Operate on local container\n"
134 " --discover Discover mount device metadata\n"
135 " -t --type=TYPE File system type\n"
136 " -o --options=OPTIONS Mount options\n"
e09fc884 137 " --owner=USER Add uid= and gid= options for USER\n"
450442cf
LP
138 " --fsck=no Don't run file system check before mount\n"
139 " --description=TEXT Description for unit\n"
140 " -p --property=NAME=VALUE Set mount unit property\n"
141 " -A --automount=BOOL Create an auto-mount point\n"
142 " --timeout-idle-sec=SEC Specify automount idle timeout\n"
143 " --automount-property=NAME=VALUE\n"
144 " Set automount unit property\n"
145 " --bind-device Bind automount unit to device\n"
146 " --list List mountable block devices\n"
dc336483 147 " -u --umount Unmount mount points\n"
37ec0fdd 148 " -G --collect Unload unit after it stopped, even when failed\n"
2322c6c7 149 " -T --tmpfs Create a new tmpfs on the mount point\n"
bc556335
DDM
150 "\nSee the %s for details.\n",
151 program_invocation_short_name,
152 streq(program_invocation_short_name, "systemd-umount") ? "" : "--umount ",
153 link);
37ec0fdd
LP
154
155 return 0;
450442cf
LP
156}
157
158static int parse_argv(int argc, char *argv[]) {
159
160 enum {
161 ARG_VERSION = 0x100,
162 ARG_NO_BLOCK,
163 ARG_NO_PAGER,
a5279634 164 ARG_NO_LEGEND,
450442cf
LP
165 ARG_NO_ASK_PASSWORD,
166 ARG_USER,
167 ARG_SYSTEM,
168 ARG_DISCOVER,
169 ARG_MOUNT_TYPE,
170 ARG_MOUNT_OPTIONS,
e09fc884 171 ARG_OWNER,
450442cf
LP
172 ARG_FSCK,
173 ARG_DESCRIPTION,
174 ARG_TIMEOUT_IDLE,
175 ARG_AUTOMOUNT,
176 ARG_AUTOMOUNT_PROPERTY,
177 ARG_BIND_DEVICE,
178 ARG_LIST,
179 };
180
181 static const struct option options[] = {
182 { "help", no_argument, NULL, 'h' },
183 { "version", no_argument, NULL, ARG_VERSION },
184 { "no-block", no_argument, NULL, ARG_NO_BLOCK },
185 { "no-pager", no_argument, NULL, ARG_NO_PAGER },
a5279634 186 { "no-legend", no_argument, NULL, ARG_NO_LEGEND },
a362c069 187 { "full", no_argument, NULL, 'l' },
450442cf
LP
188 { "no-ask-password", no_argument, NULL, ARG_NO_ASK_PASSWORD },
189 { "quiet", no_argument, NULL, 'q' },
190 { "user", no_argument, NULL, ARG_USER },
191 { "system", no_argument, NULL, ARG_SYSTEM },
192 { "host", required_argument, NULL, 'H' },
193 { "machine", required_argument, NULL, 'M' },
194 { "discover", no_argument, NULL, ARG_DISCOVER },
195 { "type", required_argument, NULL, 't' },
196 { "options", required_argument, NULL, 'o' },
e09fc884 197 { "owner", required_argument, NULL, ARG_OWNER },
0f923832 198 { "fsck", required_argument, NULL, ARG_FSCK },
450442cf
LP
199 { "description", required_argument, NULL, ARG_DESCRIPTION },
200 { "property", required_argument, NULL, 'p' },
201 { "automount", required_argument, NULL, ARG_AUTOMOUNT },
202 { "timeout-idle-sec", required_argument, NULL, ARG_TIMEOUT_IDLE },
203 { "automount-property", required_argument, NULL, ARG_AUTOMOUNT_PROPERTY },
204 { "bind-device", no_argument, NULL, ARG_BIND_DEVICE },
205 { "list", no_argument, NULL, ARG_LIST },
c37fb55b 206 { "umount", no_argument, NULL, 'u' },
52a1e91e 207 { "unmount", no_argument, NULL, 'u' }, /* Compat spelling */
dc336483 208 { "collect", no_argument, NULL, 'G' },
2322c6c7 209 { "tmpfs", no_argument, NULL, 'T' },
450442cf
LP
210 {},
211 };
212
213 int r, c;
214
215 assert(argc >= 0);
216 assert(argv);
217
2306d177
LP
218 if (invoked_as(argv, "systemd-umount"))
219 arg_action = ACTION_UMOUNT;
c37fb55b 220
2322c6c7 221 while ((c = getopt_long(argc, argv, "hqH:M:t:o:p:AuGlT", options, NULL)) >= 0)
450442cf
LP
222
223 switch (c) {
224
225 case 'h':
37ec0fdd 226 return help();
450442cf
LP
227
228 case ARG_VERSION:
229 return version();
230
231 case ARG_NO_BLOCK:
232 arg_no_block = true;
233 break;
234
235 case ARG_NO_PAGER:
0221d68a 236 arg_pager_flags |= PAGER_DISABLE;
450442cf
LP
237 break;
238
a5279634
YW
239 case ARG_NO_LEGEND:
240 arg_legend = false;
241 break;
242
a362c069
YW
243 case 'l':
244 arg_full = true;
245 break;
246
450442cf
LP
247 case ARG_NO_ASK_PASSWORD:
248 arg_ask_password = false;
249 break;
250
251 case 'q':
252 arg_quiet = true;
253 break;
254
255 case ARG_USER:
4870133b 256 arg_runtime_scope = RUNTIME_SCOPE_USER;
450442cf
LP
257 break;
258
259 case ARG_SYSTEM:
4870133b 260 arg_runtime_scope = RUNTIME_SCOPE_SYSTEM;
450442cf
LP
261 break;
262
263 case 'H':
264 arg_transport = BUS_TRANSPORT_REMOTE;
265 arg_host = optarg;
266 break;
267
268 case 'M':
269 arg_transport = BUS_TRANSPORT_MACHINE;
270 arg_host = optarg;
271 break;
272
273 case ARG_DISCOVER:
274 arg_discover = true;
275 break;
276
277 case 't':
b3f9c17a
YW
278 r = free_and_strdup_warn(&arg_mount_type, optarg);
279 if (r < 0)
280 return r;
450442cf
LP
281 break;
282
283 case 'o':
b3f9c17a
YW
284 r = free_and_strdup_warn(&arg_mount_options, optarg);
285 if (r < 0)
286 return r;
450442cf
LP
287 break;
288
e09fc884
ZJS
289 case ARG_OWNER: {
290 const char *user = optarg;
291
fafff8f1 292 r = get_user_creds(&user, &arg_uid, &arg_gid, NULL, NULL, 0);
e09fc884
ZJS
293 if (r < 0)
294 return log_error_errno(r,
295 r == -EBADMSG ? "UID or GID of user %s are invalid."
296 : "Cannot use \"%s\" as owner: %m",
297 optarg);
298 break;
299 }
300
450442cf 301 case ARG_FSCK:
599c7c54 302 r = parse_boolean_argument("--fsck=", optarg, &arg_fsck);
450442cf 303 if (r < 0)
599c7c54 304 return r;
450442cf
LP
305 break;
306
307 case ARG_DESCRIPTION:
b3f9c17a
YW
308 r = free_and_strdup_warn(&arg_description, optarg);
309 if (r < 0)
310 return r;
450442cf
LP
311 break;
312
313 case 'p':
314 if (strv_extend(&arg_property, optarg) < 0)
315 return log_oom();
316
317 break;
318
319 case 'A':
320 arg_action = ACTION_AUTOMOUNT;
321 break;
322
323 case ARG_AUTOMOUNT:
c3470872 324 r = parse_boolean_argument("--automount=", optarg, NULL);
450442cf 325 if (r < 0)
c3470872 326 return r;
450442cf
LP
327
328 arg_action = r ? ACTION_AUTOMOUNT : ACTION_MOUNT;
329 break;
330
331 case ARG_TIMEOUT_IDLE:
332 r = parse_sec(optarg, &arg_timeout_idle);
333 if (r < 0)
334 return log_error_errno(r, "Failed to parse timeout: %s", optarg);
335
336 break;
337
338 case ARG_AUTOMOUNT_PROPERTY:
339 if (strv_extend(&arg_automount_property, optarg) < 0)
340 return log_oom();
341
342 break;
343
344 case ARG_BIND_DEVICE:
345 arg_bind_device = true;
346 break;
347
348 case ARG_LIST:
349 arg_action = ACTION_LIST;
350 break;
351
c37fb55b
LR
352 case 'u':
353 arg_action = ACTION_UMOUNT;
354 break;
355
dc336483
YW
356 case 'G':
357 arg_aggressive_gc = true;
358 break;
359
2322c6c7
LB
360 case 'T':
361 arg_tmpfs = true;
362 break;
363
450442cf
LP
364 case '?':
365 return -EINVAL;
366
367 default:
04499a70 368 assert_not_reached();
450442cf
LP
369 }
370
4870133b 371 if (arg_runtime_scope == RUNTIME_SCOPE_USER) {
966f3a24
LP
372 arg_ask_password = false;
373
4870133b
LP
374 if (arg_transport != BUS_TRANSPORT_LOCAL)
375 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
376 "Execution in user context is not supported on non-local systems.");
377 }
450442cf
LP
378
379 if (arg_action == ACTION_LIST) {
baaa35ad
ZJS
380 if (optind < argc)
381 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
382 "Too many arguments.");
450442cf 383
baaa35ad
ZJS
384 if (arg_transport != BUS_TRANSPORT_LOCAL)
385 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP),
386 "Listing devices only supported locally.");
9017f5d8 387 } else if (arg_action == ACTION_UMOUNT) {
baaa35ad
ZJS
388 if (optind >= argc)
389 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
390 "At least one argument required.");
9017f5d8 391
857127f1
ZJS
392 if (arg_transport != BUS_TRANSPORT_LOCAL)
393 for (int i = optind; i < argc; i++)
394 if (!path_is_absolute(argv[i]))
baaa35ad 395 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
2322c6c7
LB
396 "Path must be absolute when operating remotely: %s",
397 argv[i]);
450442cf 398 } else {
baaa35ad
ZJS
399 if (optind >= argc)
400 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
401 "At least one argument required.");
450442cf 402
baaa35ad
ZJS
403 if (argc > optind+2)
404 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
52a1e91e 405 "More than two arguments are not allowed.");
450442cf 406
2322c6c7
LB
407 if (arg_tmpfs) {
408 if (argc <= optind+1) {
409 arg_mount_what = strdup("tmpfs");
410 if (!arg_mount_what)
411 return log_oom();
e2be442e 412
2322c6c7
LB
413 r = parse_where(argv[optind], &arg_mount_where);
414 if (r < 0)
415 return r;
416 } else {
417 arg_mount_what = strdup(argv[optind]);
418 if (!arg_mount_what)
419 return log_oom();
420 }
450442cf 421
2322c6c7
LB
422 if (!strv_contains(arg_property, "Type=tmpfs") &&
423 strv_extend(&arg_property, "Type=tmpfs") < 0)
afde5b16 424 return log_oom();
afde5b16 425 } else {
2322c6c7
LB
426 if (arg_mount_type && !fstype_is_blockdev_backed(arg_mount_type)) {
427 arg_mount_what = strdup(argv[optind]);
428 if (!arg_mount_what)
429 return log_oom();
afde5b16 430
2322c6c7
LB
431 } else if (arg_transport == BUS_TRANSPORT_LOCAL) {
432 _cleanup_free_ char *u = NULL;
afde5b16 433
2322c6c7
LB
434 u = fstab_node_to_udev_node(argv[optind]);
435 if (!u)
436 return log_oom();
afde5b16 437
2322c6c7 438 r = chase(u, NULL, 0, &arg_mount_what, NULL);
afde5b16 439 if (r < 0)
2322c6c7 440 return log_error_errno(r, "Failed to make path %s absolute: %m", u);
afde5b16 441
660087dc
ZJS
442 } else {
443 if (!path_is_absolute(argv[optind]))
baaa35ad 444 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
2322c6c7 445 "Path must be absolute when operating remotely: %s",
660087dc
ZJS
446 argv[optind]);
447
448 r = path_simplify_alloc(argv[optind], &arg_mount_what);
449 if (r < 0)
450 return log_error_errno(r, "Failed to simplify path: %m");
afde5b16 451 }
2322c6c7
LB
452 }
453
454 if (argc > optind+1) {
455 r = parse_where(argv[optind+1], &arg_mount_where);
456 if (r < 0)
457 return r;
458 } else if (!arg_tmpfs)
450442cf
LP
459 arg_discover = true;
460
baaa35ad
ZJS
461 if (arg_discover && arg_transport != BUS_TRANSPORT_LOCAL)
462 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP),
463 "Automatic mount location discovery is only supported locally.");
450442cf
LP
464 }
465
466 return 1;
467}
468
89ada3ba 469static int transient_unit_set_properties(sd_bus_message *m, UnitType t, char **properties) {
450442cf
LP
470 int r;
471
05b4d3b5
AK
472 if (!isempty(arg_description)) {
473 r = sd_bus_message_append(m, "(sv)", "Description", "s", arg_description);
474 if (r < 0)
475 return r;
476 }
450442cf
LP
477
478 if (arg_bind_device && is_device_path(arg_mount_what)) {
479 _cleanup_free_ char *device_unit = NULL;
480
481 r = unit_name_from_path(arg_mount_what, ".device", &device_unit);
482 if (r < 0)
483 return r;
484
485 r = sd_bus_message_append(m, "(sv)(sv)",
486 "After", "as", 1, device_unit,
487 "BindsTo", "as", 1, device_unit);
488 if (r < 0)
489 return r;
490 }
491
dc336483
YW
492 if (arg_aggressive_gc) {
493 r = sd_bus_message_append(m, "(sv)", "CollectMode", "s", "inactive-or-failed");
494 if (r < 0)
495 return r;
496 }
497
89ada3ba 498 r = bus_append_unit_property_assignment_many(m, t, properties);
450442cf
LP
499 if (r < 0)
500 return r;
501
502 return 0;
503}
504
505static int transient_mount_set_properties(sd_bus_message *m) {
506 int r;
507
508 assert(m);
509
89ada3ba 510 r = transient_unit_set_properties(m, UNIT_MOUNT, arg_property);
450442cf
LP
511 if (r < 0)
512 return r;
513
514 if (arg_mount_what) {
515 r = sd_bus_message_append(m, "(sv)", "What", "s", arg_mount_what);
516 if (r < 0)
517 return r;
518 }
519
520 if (arg_mount_type) {
521 r = sd_bus_message_append(m, "(sv)", "Type", "s", arg_mount_type);
522 if (r < 0)
523 return r;
524 }
525
9269296d
ZJS
526 _cleanup_free_ char *options = NULL;
527
e09fc884
ZJS
528 /* Prepend uid=…,gid=… if arg_uid is set */
529 if (arg_uid != UID_INVALID) {
9269296d
ZJS
530 r = strextendf_with_separator(&options, ",",
531 "uid="UID_FMT",gid="GID_FMT, arg_uid, arg_gid);
450442cf 532 if (r < 0)
9269296d 533 return r;
450442cf
LP
534 }
535
9269296d
ZJS
536 /* Override the default for tmpfs mounts. The kernel sets the sticky bit on the root directory by
537 * default. This makes sense for the case when the user does 'mount -t tmpfs tmpfs /tmp', but less so
538 * for other directories.
539 *
540 * Let's also set some reasonable limits. We use the current umask, to match what a command to create
541 * directory would use, e.g. mkdir. */
542 if (arg_tmpfs) {
543 mode_t mask;
544
545 r = get_process_umask(0, &mask);
546 if (r < 0)
547 return r;
548
549 assert((mask & ~0777) == 0);
550 r = strextendf_with_separator(&options, ",",
551 "mode=0%o,nodev,nosuid%s", 0777 & ~mask, NESTED_TMPFS_LIMITS);
552 if (r < 0)
553 return r;
554 }
555
556 if (arg_mount_options)
557 if (!strextend_with_separator(&options, ",", arg_mount_options))
82007efa 558 return -ENOMEM;
e09fc884 559
9269296d
ZJS
560 if (options) {
561 log_debug("Using mount options: %s", options);
562 r = sd_bus_message_append(m, "(sv)", "Options", "s", options);
e09fc884
ZJS
563 if (r < 0)
564 return r;
565 } else
566 log_debug("Not using any mount options");
567
450442cf
LP
568 if (arg_fsck) {
569 _cleanup_free_ char *fsck = NULL;
570
571 r = unit_name_from_path_instance("systemd-fsck", arg_mount_what, ".service", &fsck);
572 if (r < 0)
573 return r;
574
575 r = sd_bus_message_append(m,
576 "(sv)(sv)",
577 "Requires", "as", 1, fsck,
578 "After", "as", 1, fsck);
579 if (r < 0)
580 return r;
581 }
582
583 return 0;
584}
585
586static int transient_automount_set_properties(sd_bus_message *m) {
587 int r;
588
589 assert(m);
590
89ada3ba 591 r = transient_unit_set_properties(m, UNIT_AUTOMOUNT, arg_automount_property);
450442cf
LP
592 if (r < 0)
593 return r;
594
595 if (arg_timeout_idle != USEC_INFINITY) {
596 r = sd_bus_message_append(m, "(sv)", "TimeoutIdleUSec", "t", arg_timeout_idle);
597 if (r < 0)
598 return r;
599 }
600
601 return 0;
602}
603
604static int start_transient_mount(
605 sd_bus *bus,
606 char **argv) {
607
608 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL, *reply = NULL;
609 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
610 _cleanup_(bus_wait_for_jobs_freep) BusWaitForJobs *w = NULL;
611 _cleanup_free_ char *mount_unit = NULL;
612 int r;
613
614 if (!arg_no_block) {
615 r = bus_wait_for_jobs_new(bus, &w);
616 if (r < 0)
617 return log_error_errno(r, "Could not watch jobs: %m");
618 }
619
620 r = unit_name_from_path(arg_mount_where, ".mount", &mount_unit);
621 if (r < 0)
622 return log_error_errno(r, "Failed to make mount unit name: %m");
623
92cb8ebc 624 r = bus_message_new_method_call(bus, &m, bus_systemd_mgr, "StartTransientUnit");
450442cf
LP
625 if (r < 0)
626 return bus_log_create_error(r);
627
628 r = sd_bus_message_set_allow_interactive_authorization(m, arg_ask_password);
629 if (r < 0)
630 return bus_log_create_error(r);
631
632 /* Name and mode */
633 r = sd_bus_message_append(m, "ss", mount_unit, "fail");
634 if (r < 0)
635 return bus_log_create_error(r);
636
637 /* Properties */
638 r = sd_bus_message_open_container(m, 'a', "(sv)");
639 if (r < 0)
640 return bus_log_create_error(r);
641
642 r = transient_mount_set_properties(m);
643 if (r < 0)
644 return bus_log_create_error(r);
645
646 r = sd_bus_message_close_container(m);
647 if (r < 0)
648 return bus_log_create_error(r);
649
650 /* Auxiliary units */
651 r = sd_bus_message_append(m, "a(sa(sv))", 0);
652 if (r < 0)
653 return bus_log_create_error(r);
654
8a4b13c5 655 polkit_agent_open_if_enabled(arg_transport, arg_ask_password);
450442cf
LP
656
657 r = sd_bus_call(bus, m, 0, &error, &reply);
658 if (r < 0)
659 return log_error_errno(r, "Failed to start transient mount unit: %s", bus_error_message(&error, r));
660
661 if (w) {
662 const char *object;
663
664 r = sd_bus_message_read(reply, "o", &object);
665 if (r < 0)
666 return bus_log_parse_error(r);
667
e22ad53d 668 r = bus_wait_for_jobs_one(w, object, arg_quiet ? 0 : BUS_WAIT_JOBS_LOG_ERROR, NULL);
450442cf
LP
669 if (r < 0)
670 return r;
671 }
672
673 if (!arg_quiet)
674 log_info("Started unit %s%s%s for mount point: %s%s%s",
675 ansi_highlight(), mount_unit, ansi_normal(),
676 ansi_highlight(), arg_mount_where, ansi_normal());
677
678 return 0;
679}
680
681static int start_transient_automount(
682 sd_bus *bus,
683 char **argv) {
684
685 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL, *reply = NULL;
686 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
687 _cleanup_(bus_wait_for_jobs_freep) BusWaitForJobs *w = NULL;
688 _cleanup_free_ char *automount_unit = NULL, *mount_unit = NULL;
689 int r;
690
691 if (!arg_no_block) {
692 r = bus_wait_for_jobs_new(bus, &w);
693 if (r < 0)
694 return log_error_errno(r, "Could not watch jobs: %m");
695 }
696
697 r = unit_name_from_path(arg_mount_where, ".automount", &automount_unit);
698 if (r < 0)
699 return log_error_errno(r, "Failed to make automount unit name: %m");
700
701 r = unit_name_from_path(arg_mount_where, ".mount", &mount_unit);
702 if (r < 0)
703 return log_error_errno(r, "Failed to make mount unit name: %m");
704
92cb8ebc 705 r = bus_message_new_method_call(bus, &m, bus_systemd_mgr, "StartTransientUnit");
450442cf
LP
706 if (r < 0)
707 return bus_log_create_error(r);
708
709 r = sd_bus_message_set_allow_interactive_authorization(m, arg_ask_password);
710 if (r < 0)
711 return bus_log_create_error(r);
712
713 /* Name and mode */
714 r = sd_bus_message_append(m, "ss", automount_unit, "fail");
715 if (r < 0)
716 return bus_log_create_error(r);
717
718 /* Properties */
719 r = sd_bus_message_open_container(m, 'a', "(sv)");
720 if (r < 0)
721 return bus_log_create_error(r);
722
723 r = transient_automount_set_properties(m);
724 if (r < 0)
725 return bus_log_create_error(r);
726
727 r = sd_bus_message_close_container(m);
728 if (r < 0)
729 return bus_log_create_error(r);
730
731 /* Auxiliary units */
732 r = sd_bus_message_open_container(m, 'a', "(sa(sv))");
733 if (r < 0)
734 return bus_log_create_error(r);
735
736 r = sd_bus_message_open_container(m, 'r', "sa(sv)");
737 if (r < 0)
738 return bus_log_create_error(r);
739
740 r = sd_bus_message_append(m, "s", mount_unit);
741 if (r < 0)
742 return bus_log_create_error(r);
743
744 r = sd_bus_message_open_container(m, 'a', "(sv)");
745 if (r < 0)
746 return bus_log_create_error(r);
747
748 r = transient_mount_set_properties(m);
749 if (r < 0)
750 return bus_log_create_error(r);
751
752 r = sd_bus_message_close_container(m);
753 if (r < 0)
754 return bus_log_create_error(r);
755
756 r = sd_bus_message_close_container(m);
757 if (r < 0)
758 return bus_log_create_error(r);
759
760 r = sd_bus_message_close_container(m);
761 if (r < 0)
762 return bus_log_create_error(r);
763
8a4b13c5 764 polkit_agent_open_if_enabled(arg_transport, arg_ask_password);
450442cf
LP
765
766 r = sd_bus_call(bus, m, 0, &error, &reply);
767 if (r < 0)
768 return log_error_errno(r, "Failed to start transient automount unit: %s", bus_error_message(&error, r));
769
770 if (w) {
771 const char *object;
772
773 r = sd_bus_message_read(reply, "o", &object);
774 if (r < 0)
775 return bus_log_parse_error(r);
776
e22ad53d 777 r = bus_wait_for_jobs_one(w, object, arg_quiet ? 0 : BUS_WAIT_JOBS_LOG_ERROR, NULL);
450442cf
LP
778 if (r < 0)
779 return r;
780 }
781
782 if (!arg_quiet)
783 log_info("Started unit %s%s%s for mount point: %s%s%s",
784 ansi_highlight(), automount_unit, ansi_normal(),
785 ansi_highlight(), arg_mount_where, ansi_normal());
786
787 return 0;
788}
789
f0aac575 790static int find_mount_points(const char *what, char ***list) {
7d991d48
ZJS
791 _cleanup_(mnt_free_tablep) struct libmnt_table *table = NULL;
792 _cleanup_(mnt_free_iterp) struct libmnt_iter *iter = NULL;
f0aac575 793 _cleanup_strv_free_ char **l = NULL;
319a4f4b 794 size_t n = 0;
7d991d48 795 int r;
f0aac575
YW
796
797 assert(what);
798 assert(list);
799
800 /* Returns all mount points obtained from /proc/self/mountinfo in *list,
801 * and the number of mount points as return value. */
802
e2857b3d 803 r = libmount_parse(NULL, NULL, &table, &iter);
7d991d48
ZJS
804 if (r < 0)
805 return log_error_errno(r, "Failed to parse /proc/self/mountinfo: %m");
f0aac575
YW
806
807 for (;;) {
7d991d48
ZJS
808 struct libmnt_fs *fs;
809 const char *source, *target;
f0aac575 810
7d991d48
ZJS
811 r = mnt_table_next_fs(table, iter, &fs);
812 if (r == 1)
813 break;
814 if (r < 0)
815 return log_error_errno(r, "Failed to get next entry from /proc/self/mountinfo: %m");
f0aac575 816
7d991d48
ZJS
817 source = mnt_fs_get_source(fs);
818 target = mnt_fs_get_target(fs);
819 if (!source || !target)
f0aac575
YW
820 continue;
821
7d991d48 822 if (!path_equal(source, what))
f0aac575
YW
823 continue;
824
825 /* one extra slot is needed for the terminating NULL */
319a4f4b 826 if (!GREEDY_REALLOC0(l, n + 2))
f0aac575
YW
827 return log_oom();
828
7d991d48
ZJS
829 l[n] = strdup(target);
830 if (!l[n])
831 return log_oom();
832 n++;
f0aac575
YW
833 }
834
319a4f4b 835 if (!GREEDY_REALLOC0(l, n + 1))
290843c3
LP
836 return log_oom();
837
ae2a15bc 838 *list = TAKE_PTR(l);
f0aac575
YW
839 return n;
840}
841
1480c231
YW
842static int find_loop_device(const char *backing_file, sd_device **ret) {
843 _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL;
1480c231 844 int r;
f0aac575
YW
845
846 assert(backing_file);
1480c231 847 assert(ret);
f0aac575 848
1480c231
YW
849 r = sd_device_enumerator_new(&e);
850 if (r < 0)
851 return log_oom();
f0aac575 852
1480c231
YW
853 r = sd_device_enumerator_add_match_subsystem(e, "block", /* match = */ true);
854 if (r < 0)
855 return log_error_errno(r, "Failed to add subsystem match: %m");
f0aac575 856
1480c231
YW
857 r = sd_device_enumerator_add_match_property(e, "ID_FS_USAGE", "filesystem");
858 if (r < 0)
859 return log_error_errno(r, "Failed to add property match: %m");
f0aac575 860
1480c231
YW
861 r = sd_device_enumerator_add_match_sysname(e, "loop*");
862 if (r < 0)
863 return log_error_errno(r, "Failed to add sysname match: %m");
f0aac575 864
1480c231
YW
865 r = sd_device_enumerator_add_match_sysattr(e, "loop/backing_file", /* value = */ NULL, /* match = */ true);
866 if (r < 0)
867 return log_error_errno(r, "Failed to add sysattr match: %m");
868
869 FOREACH_DEVICE(e, dev) {
870 const char *s;
f0aac575 871
1480c231 872 r = sd_device_get_sysattr_value(dev, "loop/backing_file", &s);
a53dceb7 873 if (r < 0) {
1480c231 874 log_device_debug_errno(dev, r, "Failed to read \"loop/backing_file\" sysattr, ignoring: %m");
f0aac575 875 continue;
a53dceb7 876 }
f0aac575 877
563e6846 878 if (inode_same(s, backing_file, 0) <= 0)
f0aac575
YW
879 continue;
880
1480c231
YW
881 *ret = sd_device_ref(dev);
882 return 0;
f0aac575
YW
883 }
884
1480c231 885 return -ENXIO;
f0aac575
YW
886}
887
c37fb55b
LR
888static int stop_mount(
889 sd_bus *bus,
9017f5d8 890 const char *where,
6f6165bf 891 const char *suffix) {
c37fb55b
LR
892
893 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL, *reply = NULL;
894 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
895 _cleanup_(bus_wait_for_jobs_freep) BusWaitForJobs *w = NULL;
896 _cleanup_free_ char *mount_unit = NULL;
897 int r;
898
899 if (!arg_no_block) {
900 r = bus_wait_for_jobs_new(bus, &w);
901 if (r < 0)
902 return log_error_errno(r, "Could not watch jobs: %m");
903 }
904
9017f5d8 905 r = unit_name_from_path(where, suffix, &mount_unit);
c37fb55b 906 if (r < 0)
6442c210 907 return log_error_errno(r, "Failed to make %s unit name from path %s: %m", suffix + 1, where);
c37fb55b 908
92cb8ebc 909 r = bus_message_new_method_call(bus, &m, bus_systemd_mgr, "StopUnit");
c37fb55b
LR
910 if (r < 0)
911 return bus_log_create_error(r);
912
913 r = sd_bus_message_set_allow_interactive_authorization(m, arg_ask_password);
914 if (r < 0)
915 return bus_log_create_error(r);
916
917 /* Name and mode */
918 r = sd_bus_message_append(m, "ss", mount_unit, "fail");
919 if (r < 0)
920 return bus_log_create_error(r);
921
8a4b13c5 922 polkit_agent_open_if_enabled(arg_transport, arg_ask_password);
c37fb55b
LR
923
924 r = sd_bus_call(bus, m, 0, &error, &reply);
6442c210
YW
925 if (r < 0) {
926 if (streq(suffix, ".automount") &&
927 sd_bus_error_has_name(&error, "org.freedesktop.systemd1.NoSuchUnit"))
928 return 0;
929 return log_error_errno(r, "Failed to stop %s unit: %s", suffix + 1, bus_error_message(&error, r));
930 }
c37fb55b
LR
931
932 if (w) {
933 const char *object;
934
935 r = sd_bus_message_read(reply, "o", &object);
936 if (r < 0)
937 return bus_log_parse_error(r);
938
e22ad53d 939 r = bus_wait_for_jobs_one(w, object, arg_quiet ? 0 : BUS_WAIT_JOBS_LOG_ERROR, NULL);
c37fb55b
LR
940 if (r < 0)
941 return r;
942 }
943
944 if (!arg_quiet)
945 log_info("Stopped unit %s%s%s for mount point: %s%s%s",
946 ansi_highlight(), mount_unit, ansi_normal(),
9017f5d8 947 ansi_highlight(), where, ansi_normal());
c37fb55b
LR
948
949 return 0;
950}
951
952static int stop_mounts(
953 sd_bus *bus,
6f6165bf 954 const char *where) {
c37fb55b
LR
955
956 int r;
957
baaa35ad
ZJS
958 if (path_equal(where, "/"))
959 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
960 "Refusing to operate on root directory: %s", where);
9017f5d8 961
baaa35ad
ZJS
962 if (!path_is_normalized(where))
963 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
964 "Path contains non-normalized components: %s", where);
9017f5d8 965
6f6165bf 966 r = stop_mount(bus, where, ".mount");
c37fb55b
LR
967 if (r < 0)
968 return r;
969
6f6165bf 970 r = stop_mount(bus, where, ".automount");
c37fb55b
LR
971 if (r < 0)
972 return r;
973
974 return 0;
975}
976
961d08ca 977static int umount_by_device(sd_bus *bus, sd_device *dev) {
f0aac575 978 _cleanup_strv_free_ char **list = NULL;
9017f5d8 979 const char *v;
961d08ca 980 int r, ret = 0;
9017f5d8 981
961d08ca
YW
982 assert(bus);
983 assert(dev);
9017f5d8 984
6e07f600 985 if (sd_device_get_property_value(dev, "SYSTEMD_MOUNT_WHERE", &v) >= 0)
961d08ca 986 ret = stop_mounts(bus, v);
9017f5d8 987
961d08ca 988 r = sd_device_get_devname(dev, &v);
dcd26523 989 if (r < 0)
961d08ca 990 return r;
9017f5d8 991
961d08ca 992 r = find_mount_points(v, &list);
f0aac575
YW
993 if (r < 0)
994 return r;
995
961d08ca 996 STRV_FOREACH(l, list) {
f0aac575 997 r = stop_mounts(bus, *l);
6f6165bf 998 if (r < 0)
961d08ca 999 ret = r;
9017f5d8
YW
1000 }
1001
961d08ca
YW
1002 return ret;
1003}
1004
1005static int umount_by_device_node(sd_bus *bus, const char *node) {
1006 _cleanup_(sd_device_unrefp) sd_device *dev = NULL;
1007 const char *v;
1008 int r;
1009
1010 assert(bus);
1011 assert(node);
1012
1013 r = sd_device_new_from_devname(&dev, node);
1014 if (r < 0)
1015 return log_error_errno(r, "Failed to get device from %s: %m", node);
1016
1017 r = sd_device_get_property_value(dev, "ID_FS_USAGE", &v);
1018 if (r < 0)
1019 return log_device_error_errno(dev, r, "Failed to get \"ID_FS_USAGE\" device property: %m");
1020
1021 if (!streq(v, "filesystem"))
1022 return log_device_error_errno(dev, SYNTHETIC_ERRNO(EINVAL),
1023 "%s does not contain a known file system.", node);
1024
1025 return umount_by_device(bus, dev);
6f6165bf
YW
1026}
1027
1028static int umount_loop(sd_bus *bus, const char *backing_file) {
1480c231 1029 _cleanup_(sd_device_unrefp) sd_device *dev = NULL;
f0aac575 1030 int r;
6f6165bf
YW
1031
1032 assert(backing_file);
1033
1480c231 1034 r = find_loop_device(backing_file, &dev);
f0aac575 1035 if (r < 0)
aa46fa64 1036 return log_error_errno(r, r == -ENXIO ? "File %s is not mounted." : "Can't get loop device for %s: %m", backing_file);
6f6165bf 1037
1480c231 1038 return umount_by_device(bus, dev);
9017f5d8
YW
1039}
1040
1041static int action_umount(
1042 sd_bus *bus,
1043 int argc,
1044 char **argv) {
1045
857127f1 1046 int r, r2 = 0;
9017f5d8 1047
3747daa2 1048 if (arg_transport != BUS_TRANSPORT_LOCAL) {
857127f1 1049 for (int i = optind; i < argc; i++) {
3747daa2
YW
1050 _cleanup_free_ char *p = NULL;
1051
660087dc
ZJS
1052 r = path_simplify_alloc(argv[i], &p);
1053 if (r < 0)
1054 return r;
3747daa2
YW
1055
1056 r = stop_mounts(bus, p);
1057 if (r < 0)
1058 r2 = r;
1059 }
1060 return r2;
1061 }
1062
857127f1 1063 for (int i = optind; i < argc; i++) {
5bc9f949 1064 _cleanup_free_ char *u = NULL, *p = NULL;
6f6165bf 1065 struct stat st;
9017f5d8
YW
1066
1067 u = fstab_node_to_udev_node(argv[i]);
1068 if (!u)
1069 return log_oom();
9017f5d8 1070
f461a28d 1071 r = chase(u, NULL, 0, &p, NULL);
9017f5d8 1072 if (r < 0) {
02a8bd65 1073 r2 = log_error_errno(r, "Failed to make path %s absolute: %m", argv[i]);
9017f5d8
YW
1074 continue;
1075 }
1076
6f6165bf 1077 if (stat(p, &st) < 0)
02a8bd65 1078 return log_error_errno(errno, "Can't stat %s (from %s): %m", p, argv[i]);
9017f5d8 1079
6f6165bf 1080 if (S_ISBLK(st.st_mode))
961d08ca 1081 r = umount_by_device_node(bus, p);
6f6165bf
YW
1082 else if (S_ISREG(st.st_mode))
1083 r = umount_loop(bus, p);
1084 else if (S_ISDIR(st.st_mode))
1085 r = stop_mounts(bus, p);
1086 else {
02a8bd65 1087 log_error("Invalid file type: %s (from %s)", p, argv[i]);
6f6165bf
YW
1088 r = -EINVAL;
1089 }
1090
1091 if (r < 0)
9017f5d8
YW
1092 r2 = r;
1093 }
1094
1095 return r2;
1096}
1097
dcd26523 1098static int acquire_mount_type(sd_device *d) {
450442cf
LP
1099 const char *v;
1100
1101 assert(d);
1102
1103 if (arg_mount_type)
1104 return 0;
1105
dcd26523 1106 if (sd_device_get_property_value(d, "ID_FS_TYPE", &v) < 0)
450442cf
LP
1107 return 0;
1108
1109 arg_mount_type = strdup(v);
1110 if (!arg_mount_type)
1111 return log_oom();
1112
1113 log_debug("Discovered type=%s", arg_mount_type);
1114 return 1;
1115}
1116
dcd26523 1117static int acquire_mount_options(sd_device *d) {
450442cf
LP
1118 const char *v;
1119
dcd26523
YW
1120 assert(d);
1121
450442cf
LP
1122 if (arg_mount_options)
1123 return 0;
1124
dcd26523 1125 if (sd_device_get_property_value(d, "SYSTEMD_MOUNT_OPTIONS", &v) < 0)
450442cf
LP
1126 return 0;
1127
1128 arg_mount_options = strdup(v);
1129 if (!arg_mount_options)
1130 return log_oom();
1131
1132 log_debug("Discovered options=%s", arg_mount_options);
1133 return 1;
1134}
1135
dcd26523 1136static const char* get_label(sd_device *d) {
450442cf
LP
1137 const char *label;
1138
1139 assert(d);
1140
dcd26523
YW
1141 if (sd_device_get_property_value(d, "ID_FS_LABEL", &label) >= 0)
1142 return label;
1143
1144 if (sd_device_get_property_value(d, "ID_PART_ENTRY_NAME", &label) >= 0)
450442cf
LP
1145 return label;
1146
dcd26523 1147 return NULL;
450442cf
LP
1148}
1149
dcd26523 1150static int acquire_mount_where(sd_device *d) {
450442cf 1151 const char *v;
451f0dba 1152 int r;
450442cf
LP
1153
1154 if (arg_mount_where)
1155 return 0;
1156
dcd26523 1157 if (sd_device_get_property_value(d, "SYSTEMD_MOUNT_WHERE", &v) < 0) {
451f0dba 1158 _cleanup_free_ char *escaped = NULL, *devname_bn = NULL;
450442cf
LP
1159 const char *name;
1160
1161 name = get_label(d);
1162 if (!name)
3cc7a9fd 1163 (void) device_get_model_string(d, &name);
450442cf
LP
1164 if (!name) {
1165 const char *dn;
1166
dcd26523 1167 if (sd_device_get_devname(d, &dn) < 0)
450442cf
LP
1168 return 0;
1169
451f0dba
LP
1170 r = path_extract_filename(dn, &devname_bn);
1171 if (r < 0)
1172 return log_error_errno(r, "Failed to extract file name from '%s': %m", dn);
1173
1174 name = devname_bn;
450442cf
LP
1175 }
1176
1177 escaped = xescape(name, "\\");
f0aac575
YW
1178 if (!escaped)
1179 return log_oom();
450442cf
LP
1180 if (!filename_is_valid(escaped))
1181 return 0;
1182
657ee2d8 1183 arg_mount_where = path_join("/run/media/system", escaped);
450442cf
LP
1184 } else
1185 arg_mount_where = strdup(v);
1186
1187 if (!arg_mount_where)
1188 return log_oom();
1189
1190 log_debug("Discovered where=%s", arg_mount_where);
1191 return 1;
1192}
1193
95a45a87 1194static int acquire_mount_where_for_loop_dev(sd_device *dev) {
f0aac575 1195 _cleanup_strv_free_ char **list = NULL;
95a45a87 1196 const char *node;
f0aac575
YW
1197 int r;
1198
95a45a87
YW
1199 assert(dev);
1200
f0aac575
YW
1201 if (arg_mount_where)
1202 return 0;
1203
95a45a87 1204 r = sd_device_get_devname(dev, &node);
f0aac575
YW
1205 if (r < 0)
1206 return r;
95a45a87
YW
1207
1208 r = find_mount_points(node, &list);
1209 if (r < 0)
1210 return r;
1211 if (r == 0)
1212 return log_device_error_errno(dev, SYNTHETIC_ERRNO(EINVAL),
1213 "Can't find mount point of %s. It is expected that %s is already mounted on a place.",
1214 node, node);
1215 if (r >= 2)
1216 return log_device_error_errno(dev, SYNTHETIC_ERRNO(EINVAL),
1217 "%s is mounted on %d places. It is expected that %s is mounted on a place.",
1218 node, r, node);
f0aac575
YW
1219
1220 arg_mount_where = strdup(list[0]);
1221 if (!arg_mount_where)
1222 return log_oom();
1223
1224 log_debug("Discovered where=%s", arg_mount_where);
1225 return 1;
1226}
1227
dcd26523 1228static int acquire_description(sd_device *d) {
3cc7a9fd 1229 const char *model = NULL, *label;
450442cf
LP
1230
1231 if (arg_description)
1232 return 0;
1233
3cc7a9fd 1234 (void) device_get_model_string(d, &model);
450442cf
LP
1235
1236 label = get_label(d);
1237 if (!label)
dcd26523 1238 (void) sd_device_get_property_value(d, "ID_PART_ENTRY_NUMBER", &label);
450442cf
LP
1239
1240 if (model && label)
605405c6 1241 arg_description = strjoin(model, " ", label);
450442cf
LP
1242 else if (label)
1243 arg_description = strdup(label);
1244 else if (model)
1245 arg_description = strdup(model);
1246 else
05b4d3b5 1247 return 0;
450442cf
LP
1248
1249 if (!arg_description)
1250 return log_oom();
1251
1252 log_debug("Discovered description=%s", arg_description);
1253 return 1;
1254}
1255
dcd26523 1256static int acquire_removable(sd_device *d) {
450442cf
LP
1257 const char *v;
1258
1259 /* Shortcut this if there's no reason to check it */
1260 if (arg_action != ACTION_DEFAULT && arg_timeout_idle_set && arg_bind_device >= 0)
1261 return 0;
1262
1263 for (;;) {
1f22fc38 1264 if (sd_device_get_sysattr_value(d, "removable", &v) >= 0)
450442cf
LP
1265 break;
1266
dcd26523 1267 if (sd_device_get_parent(d, &d) < 0)
450442cf
LP
1268 return 0;
1269
fb53ee0a 1270 if (!device_in_subsystem(d, "block"))
450442cf
LP
1271 return 0;
1272 }
1273
1274 if (parse_boolean(v) <= 0)
1275 return 0;
1276
1277 log_debug("Discovered removable device.");
1278
1279 if (arg_action == ACTION_DEFAULT) {
1280 log_debug("Automatically turning on automount.");
1281 arg_action = ACTION_AUTOMOUNT;
1282 }
1283
1284 if (!arg_timeout_idle_set) {
1285 log_debug("Setting idle timeout to 1s.");
1286 arg_timeout_idle = USEC_PER_SEC;
1287 }
1288
1289 if (arg_bind_device < 0) {
1290 log_debug("Binding automount unit to device.");
1291 arg_bind_device = true;
1292 }
1293
1294 return 1;
1295}
1296
f0aac575 1297static int discover_loop_backing_file(void) {
dcd26523 1298 _cleanup_(sd_device_unrefp) sd_device *d = NULL;
450442cf
LP
1299 int r;
1300
1480c231 1301 r = find_loop_device(arg_mount_what, &d);
aa46fa64 1302 if (r < 0 && r != -ENXIO)
f0aac575
YW
1303 return log_error_errno(errno, "Can't get loop device for %s: %m", arg_mount_what);
1304
aa46fa64 1305 if (r == -ENXIO) {
451f0dba 1306 _cleanup_free_ char *escaped = NULL, *bn = NULL;
f0aac575
YW
1307
1308 if (arg_mount_where)
1309 return 0;
1310
451f0dba
LP
1311 r = path_extract_filename(arg_mount_what, &bn);
1312 if (r < 0)
1313 return log_error_errno(r, "Failed to extract file name from backing file path '%s': %m", arg_mount_what);
1314
1315 escaped = xescape(bn, "\\");
f0aac575
YW
1316 if (!escaped)
1317 return log_oom();
d7a0f1f4
FS
1318 if (!filename_is_valid(escaped))
1319 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1320 "Escaped name %s is not a valid filename.",
1321 escaped);
f0aac575 1322
657ee2d8 1323 arg_mount_where = path_join("/run/media/system", escaped);
f0aac575
YW
1324 if (!arg_mount_where)
1325 return log_oom();
1326
1327 log_debug("Discovered where=%s", arg_mount_where);
450442cf 1328 return 0;
f0aac575
YW
1329 }
1330
f0aac575
YW
1331 r = acquire_mount_type(d);
1332 if (r < 0)
1333 return r;
1334
1335 r = acquire_mount_options(d);
1336 if (r < 0)
1337 return r;
1338
95a45a87 1339 r = acquire_mount_where_for_loop_dev(d);
f0aac575
YW
1340 if (r < 0)
1341 return r;
1342
1343 r = acquire_description(d);
1344 if (r < 0)
1345 return r;
1346
1347 return 0;
1348}
1349
1350static int discover_device(void) {
dcd26523 1351 _cleanup_(sd_device_unrefp) sd_device *d = NULL;
f0aac575
YW
1352 struct stat st;
1353 const char *v;
1354 int r;
1355
450442cf
LP
1356 if (stat(arg_mount_what, &st) < 0)
1357 return log_error_errno(errno, "Can't stat %s: %m", arg_mount_what);
1358
f0aac575
YW
1359 if (S_ISREG(st.st_mode))
1360 return discover_loop_backing_file();
1361
d7a0f1f4
FS
1362 if (!S_ISBLK(st.st_mode))
1363 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1364 "Invalid file type: %s",
1365 arg_mount_what);
450442cf 1366
930aa88f 1367 r = sd_device_new_from_stat_rdev(&d, &st);
dcd26523
YW
1368 if (r < 0)
1369 return log_error_errno(r, "Failed to get device from device number: %m");
450442cf 1370
d7a0f1f4
FS
1371 if (sd_device_get_property_value(d, "ID_FS_USAGE", &v) < 0 || !streq(v, "filesystem"))
1372 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1373 "%s does not contain a known file system.",
1374 arg_mount_what);
450442cf
LP
1375
1376 r = acquire_mount_type(d);
1377 if (r < 0)
1378 return r;
1379
1380 r = acquire_mount_options(d);
1381 if (r < 0)
1382 return r;
1383
1384 r = acquire_mount_where(d);
1385 if (r < 0)
1386 return r;
1387
1388 r = acquire_description(d);
1389 if (r < 0)
1390 return r;
1391
1392 r = acquire_removable(d);
1393 if (r < 0)
1394 return r;
1395
1396 return 0;
1397}
1398
450442cf 1399static int list_devices(void) {
4cbf9d52
LP
1400 enum {
1401 COLUMN_NODE,
1402 COLUMN_PATH,
1403 COLUMN_MODEL,
1404 COLUMN_WWN,
1405 COLUMN_FSTYPE,
1406 COLUMN_LABEL,
1407 COLUMN_UUID,
1408 _COLUMN_MAX,
1409 };
1410
dcd26523 1411 _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL;
6ae6ea55 1412 _cleanup_(table_unrefp) Table *table = NULL;
450442cf
LP
1413 int r;
1414
dcd26523
YW
1415 r = sd_device_enumerator_new(&e);
1416 if (r < 0)
450442cf
LP
1417 return log_oom();
1418
dcd26523 1419 r = sd_device_enumerator_add_match_subsystem(e, "block", true);
450442cf
LP
1420 if (r < 0)
1421 return log_error_errno(r, "Failed to add block match: %m");
1422
dcd26523 1423 r = sd_device_enumerator_add_match_property(e, "ID_FS_USAGE", "filesystem");
450442cf
LP
1424 if (r < 0)
1425 return log_error_errno(r, "Failed to add property match: %m");
1426
b86155d0 1427 table = table_new("NODE", "PATH", "MODEL", "WWN", "FSTYPE", "LABEL", "UUID");
6ae6ea55
YW
1428 if (!table)
1429 return log_oom();
450442cf 1430
a362c069
YW
1431 if (arg_full)
1432 table_set_width(table, 0);
1433
ef1e0b9a 1434 r = table_set_sort(table, (size_t) 0);
6ae6ea55
YW
1435 if (r < 0)
1436 return log_error_errno(r, "Failed to set sort index: %m");
450442cf 1437
a5279634 1438 table_set_header(table, arg_legend);
40961438 1439 table_set_ersatz_string(table, TABLE_ERSATZ_DASH);
a5279634 1440
6ae6ea55 1441 FOREACH_DEVICE(e, d) {
857127f1 1442 for (unsigned c = 0; c < _COLUMN_MAX; c++) {
6fd667e5 1443 const char *x = NULL;
450442cf
LP
1444
1445 switch (c) {
1446
1447 case COLUMN_NODE:
dcd26523 1448 (void) sd_device_get_devname(d, &x);
450442cf
LP
1449 break;
1450
1451 case COLUMN_PATH:
dcd26523 1452 (void) sd_device_get_property_value(d, "ID_PATH", &x);
450442cf
LP
1453 break;
1454
1455 case COLUMN_MODEL:
3cc7a9fd 1456 (void) device_get_model_string(d, &x);
450442cf
LP
1457 break;
1458
1459 case COLUMN_WWN:
dcd26523 1460 (void) sd_device_get_property_value(d, "ID_WWN", &x);
450442cf
LP
1461 break;
1462
1463 case COLUMN_FSTYPE:
dcd26523 1464 (void) sd_device_get_property_value(d, "ID_FS_TYPE", &x);
450442cf
LP
1465 break;
1466
1467 case COLUMN_LABEL:
1468 x = get_label(d);
1469 break;
1470
1471 case COLUMN_UUID:
dcd26523 1472 (void) sd_device_get_property_value(d, "ID_FS_UUID", &x);
450442cf
LP
1473 break;
1474 }
1475
40961438 1476 r = table_add_cell(table, NULL, c == COLUMN_NODE ? TABLE_PATH : TABLE_STRING, x);
6ae6ea55 1477 if (r < 0)
bd17fa8c 1478 return table_log_add_error(r);
450442cf
LP
1479 }
1480 }
1481
384c2c32 1482 pager_open(arg_pager_flags);
450442cf 1483
6ae6ea55
YW
1484 r = table_print(table, NULL);
1485 if (r < 0)
4b6607d9 1486 return table_log_print_error(r);
450442cf 1487
6ae6ea55 1488 return 0;
450442cf
LP
1489}
1490
f2fae6fb
YW
1491static int run(int argc, char* argv[]) {
1492 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
450442cf
LP
1493 int r;
1494
aa976d87 1495 log_setup();
450442cf
LP
1496
1497 r = parse_argv(argc, argv);
1498 if (r <= 0)
f2fae6fb 1499 return r;
450442cf 1500
f2fae6fb
YW
1501 if (arg_action == ACTION_LIST)
1502 return list_devices();
450442cf 1503
4870133b 1504 r = bus_connect_transport_systemd(arg_transport, arg_host, arg_runtime_scope, &bus);
f2fae6fb 1505 if (r < 0)
10a7340a 1506 return bus_log_connect_error(r, arg_transport);
9017f5d8 1507
f2fae6fb
YW
1508 if (arg_action == ACTION_UMOUNT)
1509 return action_umount(bus, argc, argv);
9017f5d8 1510
c15ab81e 1511 if ((!arg_mount_type || fstype_is_blockdev_backed(arg_mount_type))
d7a0f1f4
FS
1512 && !path_is_normalized(arg_mount_what))
1513 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1514 "Path contains non-normalized components: %s",
1515 arg_mount_what);
f0aac575
YW
1516
1517 if (arg_discover) {
1518 r = discover_device();
1519 if (r < 0)
f2fae6fb 1520 return r;
f0aac575
YW
1521 }
1522
d7a0f1f4
FS
1523 if (!arg_mount_where)
1524 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1525 "Can't figure out where to mount %s.",
1526 arg_mount_what);
450442cf 1527
d7a0f1f4
FS
1528 if (path_equal(arg_mount_where, "/"))
1529 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1530 "Refusing to operate on root directory.");
450442cf 1531
d7a0f1f4
FS
1532 if (!path_is_normalized(arg_mount_where))
1533 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1534 "Path contains non-normalized components: %s",
1535 arg_mount_where);
450442cf
LP
1536
1537 if (streq_ptr(arg_mount_type, "auto"))
1538 arg_mount_type = mfree(arg_mount_type);
1539 if (streq_ptr(arg_mount_options, "defaults"))
1540 arg_mount_options = mfree(arg_mount_options);
1541
1542 if (!is_device_path(arg_mount_what))
1543 arg_fsck = false;
1544
1545 if (arg_fsck && arg_mount_type && arg_transport == BUS_TRANSPORT_LOCAL) {
13556724 1546 r = fsck_exists_for_fstype(arg_mount_type);
450442cf
LP
1547 if (r < 0)
1548 log_warning_errno(r, "Couldn't determine whether fsck for %s exists, proceeding anyway.", arg_mount_type);
1549 else if (r == 0) {
1550 log_debug("Disabling file system check as fsck for %s doesn't exist.", arg_mount_type);
1551 arg_fsck = false; /* fsck doesn't exist, let's not attempt it */
1552 }
1553 }
1554
e09fc884
ZJS
1555 /* The kernel (properly) refuses mounting file systems with unknown uid=,gid= options,
1556 * but not for all filesystem types. Let's try to catch the cases where the option
1557 * would be used if the file system does not support it. It is also possible to
1558 * autodetect the file system, but that's only possible with disk-based file systems
1559 * which incidentally seem to be implemented more carefully and reject unknown options,
1560 * so it's probably OK that we do the check only when the type is specified.
1561 */
1562 if (arg_mount_type &&
1563 !streq(arg_mount_type, "auto") &&
1564 arg_uid != UID_INVALID &&
d7a0f1f4
FS
1565 !fstype_can_uid_gid(arg_mount_type))
1566 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP),
1567 "File system type %s is not known to support uid=/gid=, refusing.",
1568 arg_mount_type);
e09fc884 1569
450442cf
LP
1570 switch (arg_action) {
1571
1572 case ACTION_MOUNT:
1573 case ACTION_DEFAULT:
1574 r = start_transient_mount(bus, argv + optind);
1575 break;
1576
1577 case ACTION_AUTOMOUNT:
1578 r = start_transient_automount(bus, argv + optind);
1579 break;
1580
1581 default:
04499a70 1582 assert_not_reached();
450442cf
LP
1583 }
1584
f2fae6fb 1585 return r;
450442cf 1586}
f2fae6fb
YW
1587
1588DEFINE_MAIN_FUNCTION(run);