]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/mount/mount-tool.c
man/systemd.mount: tmpfs automatically gains After=swap.target dep
[thirdparty/systemd.git] / src / mount / mount-tool.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <getopt.h>
4
5 #include "sd-bus.h"
6 #include "sd-device.h"
7
8 #include "build.h"
9 #include "bus-error.h"
10 #include "bus-locator.h"
11 #include "bus-unit-util.h"
12 #include "bus-wait-for-jobs.h"
13 #include "chase.h"
14 #include "device-util.h"
15 #include "dirent-util.h"
16 #include "escape.h"
17 #include "fd-util.h"
18 #include "fileio.h"
19 #include "format-table.h"
20 #include "format-util.h"
21 #include "fs-util.h"
22 #include "fstab-util.h"
23 #include "libmount-util.h"
24 #include "main-func.h"
25 #include "mount-util.h"
26 #include "mountpoint-util.h"
27 #include "pager.h"
28 #include "parse-argument.h"
29 #include "parse-util.h"
30 #include "path-util.h"
31 #include "pretty-print.h"
32 #include "process-util.h"
33 #include "sort-util.h"
34 #include "spawn-polkit-agent.h"
35 #include "stat-util.h"
36 #include "strv.h"
37 #include "terminal-util.h"
38 #include "udev-util.h"
39 #include "umask-util.h"
40 #include "unit-def.h"
41 #include "unit-name.h"
42 #include "user-util.h"
43
44 enum {
45 ACTION_DEFAULT,
46 ACTION_MOUNT,
47 ACTION_AUTOMOUNT,
48 ACTION_UMOUNT,
49 ACTION_LIST,
50 } arg_action = ACTION_DEFAULT;
51
52 static bool arg_no_block = false;
53 static PagerFlags arg_pager_flags = 0;
54 static bool arg_legend = true;
55 static bool arg_full = false;
56 static bool arg_ask_password = true;
57 static bool arg_quiet = false;
58 static BusTransport arg_transport = BUS_TRANSPORT_LOCAL;
59 static RuntimeScope arg_runtime_scope = RUNTIME_SCOPE_SYSTEM;
60 static const char *arg_host = NULL;
61 static bool arg_discover = false;
62 static char *arg_mount_what = NULL;
63 static char *arg_mount_where = NULL;
64 static char *arg_mount_type = NULL;
65 static char *arg_mount_options = NULL;
66 static char *arg_description = NULL;
67 static char **arg_property = NULL;
68 static usec_t arg_timeout_idle = USEC_INFINITY;
69 static bool arg_timeout_idle_set = false;
70 static char **arg_automount_property = NULL;
71 static int arg_bind_device = -1;
72 static uid_t arg_uid = UID_INVALID;
73 static gid_t arg_gid = GID_INVALID;
74 static bool arg_fsck = true;
75 static bool arg_aggressive_gc = false;
76 static bool arg_tmpfs = false;
77
78 STATIC_DESTRUCTOR_REGISTER(arg_mount_what, freep);
79 STATIC_DESTRUCTOR_REGISTER(arg_mount_where, freep);
80 STATIC_DESTRUCTOR_REGISTER(arg_mount_type, freep);
81 STATIC_DESTRUCTOR_REGISTER(arg_mount_options, freep);
82 STATIC_DESTRUCTOR_REGISTER(arg_description, freep);
83 STATIC_DESTRUCTOR_REGISTER(arg_property, strv_freep);
84 STATIC_DESTRUCTOR_REGISTER(arg_automount_property, strv_freep);
85
86 static int parse_where(const char *input, char **ret_where) {
87 int r;
88
89 assert(input);
90 assert(ret_where);
91
92 if (arg_transport == BUS_TRANSPORT_LOCAL) {
93 r = chase(input, NULL, CHASE_NONEXISTENT, ret_where, NULL);
94 if (r < 0)
95 return log_error_errno(r, "Failed to make path %s absolute: %m", input);
96 } else {
97 if (!path_is_absolute(input))
98 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
99 "Path must be absolute when operating remotely: %s",
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);
105 }
106
107 return 0;
108 }
109
110 static 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
118 printf("systemd-mount [OPTIONS...] WHAT [WHERE]\n"
119 "systemd-mount [OPTIONS...] --tmpfs [NAME] WHERE\n"
120 "systemd-mount [OPTIONS...] --list\n"
121 "%s [OPTIONS...] %sWHAT|WHERE...\n\n"
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"
127 " --no-legend Do not show the headers\n"
128 " -l --full Do not ellipsize output\n"
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"
137 " --owner=USER Add uid= and gid= options for USER\n"
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"
147 " -u --umount Unmount mount points\n"
148 " -G --collect Unload unit after it stopped, even when failed\n"
149 " -T --tmpfs Create a new tmpfs on the mount point\n"
150 "\nSee the %s for details.\n",
151 program_invocation_short_name,
152 streq(program_invocation_short_name, "systemd-umount") ? "" : "--umount ",
153 link);
154
155 return 0;
156 }
157
158 static int parse_argv(int argc, char *argv[]) {
159
160 enum {
161 ARG_VERSION = 0x100,
162 ARG_NO_BLOCK,
163 ARG_NO_PAGER,
164 ARG_NO_LEGEND,
165 ARG_NO_ASK_PASSWORD,
166 ARG_USER,
167 ARG_SYSTEM,
168 ARG_DISCOVER,
169 ARG_MOUNT_TYPE,
170 ARG_MOUNT_OPTIONS,
171 ARG_OWNER,
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 },
186 { "no-legend", no_argument, NULL, ARG_NO_LEGEND },
187 { "full", no_argument, NULL, 'l' },
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' },
197 { "owner", required_argument, NULL, ARG_OWNER },
198 { "fsck", required_argument, NULL, ARG_FSCK },
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 },
206 { "umount", no_argument, NULL, 'u' },
207 { "unmount", no_argument, NULL, 'u' }, /* Compat spelling */
208 { "collect", no_argument, NULL, 'G' },
209 { "tmpfs", no_argument, NULL, 'T' },
210 {},
211 };
212
213 int r, c;
214
215 assert(argc >= 0);
216 assert(argv);
217
218 if (invoked_as(argv, "systemd-umount"))
219 arg_action = ACTION_UMOUNT;
220
221 while ((c = getopt_long(argc, argv, "hqH:M:t:o:p:AuGlT", options, NULL)) >= 0)
222
223 switch (c) {
224
225 case 'h':
226 return help();
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:
236 arg_pager_flags |= PAGER_DISABLE;
237 break;
238
239 case ARG_NO_LEGEND:
240 arg_legend = false;
241 break;
242
243 case 'l':
244 arg_full = true;
245 break;
246
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:
256 arg_runtime_scope = RUNTIME_SCOPE_USER;
257 break;
258
259 case ARG_SYSTEM:
260 arg_runtime_scope = RUNTIME_SCOPE_SYSTEM;
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':
278 r = free_and_strdup_warn(&arg_mount_type, optarg);
279 if (r < 0)
280 return r;
281 break;
282
283 case 'o':
284 r = free_and_strdup_warn(&arg_mount_options, optarg);
285 if (r < 0)
286 return r;
287 break;
288
289 case ARG_OWNER: {
290 const char *user = optarg;
291
292 r = get_user_creds(&user, &arg_uid, &arg_gid, NULL, NULL, 0);
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
301 case ARG_FSCK:
302 r = parse_boolean_argument("--fsck=", optarg, &arg_fsck);
303 if (r < 0)
304 return r;
305 break;
306
307 case ARG_DESCRIPTION:
308 r = free_and_strdup_warn(&arg_description, optarg);
309 if (r < 0)
310 return r;
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:
324 r = parse_boolean_argument("--automount=", optarg, NULL);
325 if (r < 0)
326 return r;
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
352 case 'u':
353 arg_action = ACTION_UMOUNT;
354 break;
355
356 case 'G':
357 arg_aggressive_gc = true;
358 break;
359
360 case 'T':
361 arg_tmpfs = true;
362 break;
363
364 case '?':
365 return -EINVAL;
366
367 default:
368 assert_not_reached();
369 }
370
371 if (arg_runtime_scope == RUNTIME_SCOPE_USER) {
372 arg_ask_password = false;
373
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 }
378
379 if (arg_action == ACTION_LIST) {
380 if (optind < argc)
381 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
382 "Too many arguments.");
383
384 if (arg_transport != BUS_TRANSPORT_LOCAL)
385 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP),
386 "Listing devices only supported locally.");
387 } else if (arg_action == ACTION_UMOUNT) {
388 if (optind >= argc)
389 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
390 "At least one argument required.");
391
392 if (arg_transport != BUS_TRANSPORT_LOCAL)
393 for (int i = optind; i < argc; i++)
394 if (!path_is_absolute(argv[i]))
395 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
396 "Path must be absolute when operating remotely: %s",
397 argv[i]);
398 } else {
399 if (optind >= argc)
400 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
401 "At least one argument required.");
402
403 if (argc > optind+2)
404 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
405 "More than two arguments are not allowed.");
406
407 if (arg_tmpfs) {
408 if (argc <= optind+1) {
409 arg_mount_what = strdup("tmpfs");
410 if (!arg_mount_what)
411 return log_oom();
412
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 }
421
422 if (!strv_contains(arg_property, "Type=tmpfs") &&
423 strv_extend(&arg_property, "Type=tmpfs") < 0)
424 return log_oom();
425 } else {
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();
430
431 } else if (arg_transport == BUS_TRANSPORT_LOCAL) {
432 _cleanup_free_ char *u = NULL;
433
434 u = fstab_node_to_udev_node(argv[optind]);
435 if (!u)
436 return log_oom();
437
438 r = chase(u, NULL, 0, &arg_mount_what, NULL);
439 if (r < 0)
440 return log_error_errno(r, "Failed to make path %s absolute: %m", u);
441
442 } else {
443 if (!path_is_absolute(argv[optind]))
444 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
445 "Path must be absolute when operating remotely: %s",
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");
451 }
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)
459 arg_discover = true;
460
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.");
464 }
465
466 return 1;
467 }
468
469 static int transient_unit_set_properties(sd_bus_message *m, UnitType t, char **properties) {
470 int r;
471
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 }
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
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
498 r = bus_append_unit_property_assignment_many(m, t, properties);
499 if (r < 0)
500 return r;
501
502 return 0;
503 }
504
505 static int transient_mount_set_properties(sd_bus_message *m) {
506 int r;
507
508 assert(m);
509
510 r = transient_unit_set_properties(m, UNIT_MOUNT, arg_property);
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
526 _cleanup_free_ char *options = NULL;
527
528 /* Prepend uid=…,gid=… if arg_uid is set */
529 if (arg_uid != UID_INVALID) {
530 r = strextendf_with_separator(&options, ",",
531 "uid="UID_FMT",gid="GID_FMT, arg_uid, arg_gid);
532 if (r < 0)
533 return r;
534 }
535
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))
558 return -ENOMEM;
559
560 if (options) {
561 log_debug("Using mount options: %s", options);
562 r = sd_bus_message_append(m, "(sv)", "Options", "s", options);
563 if (r < 0)
564 return r;
565 } else
566 log_debug("Not using any mount options");
567
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
586 static int transient_automount_set_properties(sd_bus_message *m) {
587 int r;
588
589 assert(m);
590
591 r = transient_unit_set_properties(m, UNIT_AUTOMOUNT, arg_automount_property);
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
604 static 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
624 r = bus_message_new_method_call(bus, &m, bus_systemd_mgr, "StartTransientUnit");
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
655 polkit_agent_open_if_enabled(arg_transport, arg_ask_password);
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
668 r = bus_wait_for_jobs_one(w, object, arg_quiet, NULL);
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
681 static 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
705 r = bus_message_new_method_call(bus, &m, bus_systemd_mgr, "StartTransientUnit");
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
764 polkit_agent_open_if_enabled(arg_transport, arg_ask_password);
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
777 r = bus_wait_for_jobs_one(w, object, arg_quiet, NULL);
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
790 static int find_mount_points(const char *what, char ***list) {
791 _cleanup_(mnt_free_tablep) struct libmnt_table *table = NULL;
792 _cleanup_(mnt_free_iterp) struct libmnt_iter *iter = NULL;
793 _cleanup_strv_free_ char **l = NULL;
794 size_t n = 0;
795 int r;
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
803 r = libmount_parse(NULL, NULL, &table, &iter);
804 if (r < 0)
805 return log_error_errno(r, "Failed to parse /proc/self/mountinfo: %m");
806
807 for (;;) {
808 struct libmnt_fs *fs;
809 const char *source, *target;
810
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");
816
817 source = mnt_fs_get_source(fs);
818 target = mnt_fs_get_target(fs);
819 if (!source || !target)
820 continue;
821
822 if (!path_equal(source, what))
823 continue;
824
825 /* one extra slot is needed for the terminating NULL */
826 if (!GREEDY_REALLOC0(l, n + 2))
827 return log_oom();
828
829 l[n] = strdup(target);
830 if (!l[n])
831 return log_oom();
832 n++;
833 }
834
835 if (!GREEDY_REALLOC0(l, n + 1))
836 return log_oom();
837
838 *list = TAKE_PTR(l);
839 return n;
840 }
841
842 static int find_loop_device(const char *backing_file, sd_device **ret) {
843 _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL;
844 int r;
845
846 assert(backing_file);
847 assert(ret);
848
849 r = sd_device_enumerator_new(&e);
850 if (r < 0)
851 return log_oom();
852
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");
856
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");
860
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");
864
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;
871
872 r = sd_device_get_sysattr_value(dev, "loop/backing_file", &s);
873 if (r < 0) {
874 log_device_debug_errno(dev, r, "Failed to read \"loop/backing_file\" sysattr, ignoring: %m");
875 continue;
876 }
877
878 if (inode_same(s, backing_file, 0) <= 0)
879 continue;
880
881 *ret = sd_device_ref(dev);
882 return 0;
883 }
884
885 return -ENXIO;
886 }
887
888 static int stop_mount(
889 sd_bus *bus,
890 const char *where,
891 const char *suffix) {
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
905 r = unit_name_from_path(where, suffix, &mount_unit);
906 if (r < 0)
907 return log_error_errno(r, "Failed to make %s unit name from path %s: %m", suffix + 1, where);
908
909 r = bus_message_new_method_call(bus, &m, bus_systemd_mgr, "StopUnit");
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
922 polkit_agent_open_if_enabled(arg_transport, arg_ask_password);
923
924 r = sd_bus_call(bus, m, 0, &error, &reply);
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 }
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
939 r = bus_wait_for_jobs_one(w, object, arg_quiet, NULL);
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(),
947 ansi_highlight(), where, ansi_normal());
948
949 return 0;
950 }
951
952 static int stop_mounts(
953 sd_bus *bus,
954 const char *where) {
955
956 int r;
957
958 if (path_equal(where, "/"))
959 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
960 "Refusing to operate on root directory: %s", where);
961
962 if (!path_is_normalized(where))
963 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
964 "Path contains non-normalized components: %s", where);
965
966 r = stop_mount(bus, where, ".mount");
967 if (r < 0)
968 return r;
969
970 r = stop_mount(bus, where, ".automount");
971 if (r < 0)
972 return r;
973
974 return 0;
975 }
976
977 static int umount_by_device(sd_bus *bus, sd_device *dev) {
978 _cleanup_strv_free_ char **list = NULL;
979 const char *v;
980 int r, ret = 0;
981
982 assert(bus);
983 assert(dev);
984
985 if (sd_device_get_property_value(dev, "SYSTEMD_MOUNT_WHERE", &v) >= 0)
986 ret = stop_mounts(bus, v);
987
988 r = sd_device_get_devname(dev, &v);
989 if (r < 0)
990 return r;
991
992 r = find_mount_points(v, &list);
993 if (r < 0)
994 return r;
995
996 STRV_FOREACH(l, list) {
997 r = stop_mounts(bus, *l);
998 if (r < 0)
999 ret = r;
1000 }
1001
1002 return ret;
1003 }
1004
1005 static 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);
1026 }
1027
1028 static int umount_loop(sd_bus *bus, const char *backing_file) {
1029 _cleanup_(sd_device_unrefp) sd_device *dev = NULL;
1030 int r;
1031
1032 assert(backing_file);
1033
1034 r = find_loop_device(backing_file, &dev);
1035 if (r < 0)
1036 return log_error_errno(r, r == -ENXIO ? "File %s is not mounted." : "Can't get loop device for %s: %m", backing_file);
1037
1038 return umount_by_device(bus, dev);
1039 }
1040
1041 static int action_umount(
1042 sd_bus *bus,
1043 int argc,
1044 char **argv) {
1045
1046 int r, r2 = 0;
1047
1048 if (arg_transport != BUS_TRANSPORT_LOCAL) {
1049 for (int i = optind; i < argc; i++) {
1050 _cleanup_free_ char *p = NULL;
1051
1052 r = path_simplify_alloc(argv[i], &p);
1053 if (r < 0)
1054 return r;
1055
1056 r = stop_mounts(bus, p);
1057 if (r < 0)
1058 r2 = r;
1059 }
1060 return r2;
1061 }
1062
1063 for (int i = optind; i < argc; i++) {
1064 _cleanup_free_ char *u = NULL, *p = NULL;
1065 struct stat st;
1066
1067 u = fstab_node_to_udev_node(argv[i]);
1068 if (!u)
1069 return log_oom();
1070
1071 r = chase(u, NULL, 0, &p, NULL);
1072 if (r < 0) {
1073 r2 = log_error_errno(r, "Failed to make path %s absolute: %m", argv[i]);
1074 continue;
1075 }
1076
1077 if (stat(p, &st) < 0)
1078 return log_error_errno(errno, "Can't stat %s (from %s): %m", p, argv[i]);
1079
1080 if (S_ISBLK(st.st_mode))
1081 r = umount_by_device_node(bus, p);
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 {
1087 log_error("Invalid file type: %s (from %s)", p, argv[i]);
1088 r = -EINVAL;
1089 }
1090
1091 if (r < 0)
1092 r2 = r;
1093 }
1094
1095 return r2;
1096 }
1097
1098 static int acquire_mount_type(sd_device *d) {
1099 const char *v;
1100
1101 assert(d);
1102
1103 if (arg_mount_type)
1104 return 0;
1105
1106 if (sd_device_get_property_value(d, "ID_FS_TYPE", &v) < 0)
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
1117 static int acquire_mount_options(sd_device *d) {
1118 const char *v;
1119
1120 assert(d);
1121
1122 if (arg_mount_options)
1123 return 0;
1124
1125 if (sd_device_get_property_value(d, "SYSTEMD_MOUNT_OPTIONS", &v) < 0)
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
1136 static const char* get_label(sd_device *d) {
1137 const char *label;
1138
1139 assert(d);
1140
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)
1145 return label;
1146
1147 return NULL;
1148 }
1149
1150 static int acquire_mount_where(sd_device *d) {
1151 const char *v;
1152 int r;
1153
1154 if (arg_mount_where)
1155 return 0;
1156
1157 if (sd_device_get_property_value(d, "SYSTEMD_MOUNT_WHERE", &v) < 0) {
1158 _cleanup_free_ char *escaped = NULL, *devname_bn = NULL;
1159 const char *name;
1160
1161 name = get_label(d);
1162 if (!name)
1163 (void) device_get_model_string(d, &name);
1164 if (!name) {
1165 const char *dn;
1166
1167 if (sd_device_get_devname(d, &dn) < 0)
1168 return 0;
1169
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;
1175 }
1176
1177 escaped = xescape(name, "\\");
1178 if (!escaped)
1179 return log_oom();
1180 if (!filename_is_valid(escaped))
1181 return 0;
1182
1183 arg_mount_where = path_join("/run/media/system", escaped);
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
1194 static int acquire_mount_where_for_loop_dev(sd_device *dev) {
1195 _cleanup_strv_free_ char **list = NULL;
1196 const char *node;
1197 int r;
1198
1199 assert(dev);
1200
1201 if (arg_mount_where)
1202 return 0;
1203
1204 r = sd_device_get_devname(dev, &node);
1205 if (r < 0)
1206 return r;
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);
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
1228 static int acquire_description(sd_device *d) {
1229 const char *model = NULL, *label;
1230
1231 if (arg_description)
1232 return 0;
1233
1234 (void) device_get_model_string(d, &model);
1235
1236 label = get_label(d);
1237 if (!label)
1238 (void) sd_device_get_property_value(d, "ID_PART_ENTRY_NUMBER", &label);
1239
1240 if (model && label)
1241 arg_description = strjoin(model, " ", label);
1242 else if (label)
1243 arg_description = strdup(label);
1244 else if (model)
1245 arg_description = strdup(model);
1246 else
1247 return 0;
1248
1249 if (!arg_description)
1250 return log_oom();
1251
1252 log_debug("Discovered description=%s", arg_description);
1253 return 1;
1254 }
1255
1256 static int acquire_removable(sd_device *d) {
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 (;;) {
1264 if (sd_device_get_sysattr_value(d, "removable", &v) >= 0)
1265 break;
1266
1267 if (sd_device_get_parent(d, &d) < 0)
1268 return 0;
1269
1270 if (sd_device_get_subsystem(d, &v) < 0 || !streq(v, "block"))
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
1297 static int discover_loop_backing_file(void) {
1298 _cleanup_(sd_device_unrefp) sd_device *d = NULL;
1299 int r;
1300
1301 r = find_loop_device(arg_mount_what, &d);
1302 if (r < 0 && r != -ENXIO)
1303 return log_error_errno(errno, "Can't get loop device for %s: %m", arg_mount_what);
1304
1305 if (r == -ENXIO) {
1306 _cleanup_free_ char *escaped = NULL, *bn = NULL;
1307
1308 if (arg_mount_where)
1309 return 0;
1310
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, "\\");
1316 if (!escaped)
1317 return log_oom();
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);
1322
1323 arg_mount_where = path_join("/run/media/system", escaped);
1324 if (!arg_mount_where)
1325 return log_oom();
1326
1327 log_debug("Discovered where=%s", arg_mount_where);
1328 return 0;
1329 }
1330
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
1339 r = acquire_mount_where_for_loop_dev(d);
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
1350 static int discover_device(void) {
1351 _cleanup_(sd_device_unrefp) sd_device *d = NULL;
1352 struct stat st;
1353 const char *v;
1354 int r;
1355
1356 if (stat(arg_mount_what, &st) < 0)
1357 return log_error_errno(errno, "Can't stat %s: %m", arg_mount_what);
1358
1359 if (S_ISREG(st.st_mode))
1360 return discover_loop_backing_file();
1361
1362 if (!S_ISBLK(st.st_mode))
1363 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1364 "Invalid file type: %s",
1365 arg_mount_what);
1366
1367 r = sd_device_new_from_stat_rdev(&d, &st);
1368 if (r < 0)
1369 return log_error_errno(r, "Failed to get device from device number: %m");
1370
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);
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
1399 static int list_devices(void) {
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
1411 _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL;
1412 _cleanup_(table_unrefp) Table *table = NULL;
1413 int r;
1414
1415 r = sd_device_enumerator_new(&e);
1416 if (r < 0)
1417 return log_oom();
1418
1419 r = sd_device_enumerator_add_match_subsystem(e, "block", true);
1420 if (r < 0)
1421 return log_error_errno(r, "Failed to add block match: %m");
1422
1423 r = sd_device_enumerator_add_match_property(e, "ID_FS_USAGE", "filesystem");
1424 if (r < 0)
1425 return log_error_errno(r, "Failed to add property match: %m");
1426
1427 table = table_new("NODE", "PATH", "MODEL", "WWN", "FSTYPE", "LABEL", "UUID");
1428 if (!table)
1429 return log_oom();
1430
1431 if (arg_full)
1432 table_set_width(table, 0);
1433
1434 r = table_set_sort(table, (size_t) 0);
1435 if (r < 0)
1436 return log_error_errno(r, "Failed to set sort index: %m");
1437
1438 table_set_header(table, arg_legend);
1439 table_set_ersatz_string(table, TABLE_ERSATZ_DASH);
1440
1441 FOREACH_DEVICE(e, d) {
1442 for (unsigned c = 0; c < _COLUMN_MAX; c++) {
1443 const char *x = NULL;
1444
1445 switch (c) {
1446
1447 case COLUMN_NODE:
1448 (void) sd_device_get_devname(d, &x);
1449 break;
1450
1451 case COLUMN_PATH:
1452 (void) sd_device_get_property_value(d, "ID_PATH", &x);
1453 break;
1454
1455 case COLUMN_MODEL:
1456 (void) device_get_model_string(d, &x);
1457 break;
1458
1459 case COLUMN_WWN:
1460 (void) sd_device_get_property_value(d, "ID_WWN", &x);
1461 break;
1462
1463 case COLUMN_FSTYPE:
1464 (void) sd_device_get_property_value(d, "ID_FS_TYPE", &x);
1465 break;
1466
1467 case COLUMN_LABEL:
1468 x = get_label(d);
1469 break;
1470
1471 case COLUMN_UUID:
1472 (void) sd_device_get_property_value(d, "ID_FS_UUID", &x);
1473 break;
1474 }
1475
1476 r = table_add_cell(table, NULL, c == COLUMN_NODE ? TABLE_PATH : TABLE_STRING, x);
1477 if (r < 0)
1478 return table_log_add_error(r);
1479 }
1480 }
1481
1482 pager_open(arg_pager_flags);
1483
1484 r = table_print(table, NULL);
1485 if (r < 0)
1486 return table_log_print_error(r);
1487
1488 return 0;
1489 }
1490
1491 static int run(int argc, char* argv[]) {
1492 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
1493 int r;
1494
1495 log_show_color(true);
1496 log_parse_environment();
1497 log_open();
1498
1499 r = parse_argv(argc, argv);
1500 if (r <= 0)
1501 return r;
1502
1503 if (arg_action == ACTION_LIST)
1504 return list_devices();
1505
1506 r = bus_connect_transport_systemd(arg_transport, arg_host, arg_runtime_scope, &bus);
1507 if (r < 0)
1508 return bus_log_connect_error(r, arg_transport);
1509
1510 if (arg_action == ACTION_UMOUNT)
1511 return action_umount(bus, argc, argv);
1512
1513 if ((!arg_mount_type || fstype_is_blockdev_backed(arg_mount_type))
1514 && !path_is_normalized(arg_mount_what))
1515 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1516 "Path contains non-normalized components: %s",
1517 arg_mount_what);
1518
1519 if (arg_discover) {
1520 r = discover_device();
1521 if (r < 0)
1522 return r;
1523 }
1524
1525 if (!arg_mount_where)
1526 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1527 "Can't figure out where to mount %s.",
1528 arg_mount_what);
1529
1530 if (path_equal(arg_mount_where, "/"))
1531 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1532 "Refusing to operate on root directory.");
1533
1534 if (!path_is_normalized(arg_mount_where))
1535 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1536 "Path contains non-normalized components: %s",
1537 arg_mount_where);
1538
1539 if (streq_ptr(arg_mount_type, "auto"))
1540 arg_mount_type = mfree(arg_mount_type);
1541 if (streq_ptr(arg_mount_options, "defaults"))
1542 arg_mount_options = mfree(arg_mount_options);
1543
1544 if (!is_device_path(arg_mount_what))
1545 arg_fsck = false;
1546
1547 if (arg_fsck && arg_mount_type && arg_transport == BUS_TRANSPORT_LOCAL) {
1548 r = fsck_exists_for_fstype(arg_mount_type);
1549 if (r < 0)
1550 log_warning_errno(r, "Couldn't determine whether fsck for %s exists, proceeding anyway.", arg_mount_type);
1551 else if (r == 0) {
1552 log_debug("Disabling file system check as fsck for %s doesn't exist.", arg_mount_type);
1553 arg_fsck = false; /* fsck doesn't exist, let's not attempt it */
1554 }
1555 }
1556
1557 /* The kernel (properly) refuses mounting file systems with unknown uid=,gid= options,
1558 * but not for all filesystem types. Let's try to catch the cases where the option
1559 * would be used if the file system does not support it. It is also possible to
1560 * autodetect the file system, but that's only possible with disk-based file systems
1561 * which incidentally seem to be implemented more carefully and reject unknown options,
1562 * so it's probably OK that we do the check only when the type is specified.
1563 */
1564 if (arg_mount_type &&
1565 !streq(arg_mount_type, "auto") &&
1566 arg_uid != UID_INVALID &&
1567 !fstype_can_uid_gid(arg_mount_type))
1568 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP),
1569 "File system type %s is not known to support uid=/gid=, refusing.",
1570 arg_mount_type);
1571
1572 switch (arg_action) {
1573
1574 case ACTION_MOUNT:
1575 case ACTION_DEFAULT:
1576 r = start_transient_mount(bus, argv + optind);
1577 break;
1578
1579 case ACTION_AUTOMOUNT:
1580 r = start_transient_automount(bus, argv + optind);
1581 break;
1582
1583 default:
1584 assert_not_reached();
1585 }
1586
1587 return r;
1588 }
1589
1590 DEFINE_MAIN_FUNCTION(run);