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