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