Debian Codegraph @

src/mount/mount-tool.c

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