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