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