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