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