1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
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"
18 #include "format-table.h"
19 #include "format-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"
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"
36 #include "terminal-util.h"
38 #include "unit-name.h"
39 #include "user-util.h"
47 } arg_action
= ACTION_DEFAULT
;
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;
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
);
82 static int help(void) {
83 _cleanup_free_
char *link
= NULL
;
86 r
= terminal_urlify_man("systemd-mount", "1", &link
);
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 ",
128 static int parse_argv(int argc
, char *argv
[]) {
146 ARG_AUTOMOUNT_PROPERTY
,
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' },
187 if (invoked_as(argv
, "systemd-umount"))
188 arg_action
= ACTION_UMOUNT
;
190 while ((c
= getopt_long(argc
, argv
, "hqH:M:t:o:p:AuGl", options
, NULL
)) >= 0)
205 arg_pager_flags
|= PAGER_DISABLE
;
216 case ARG_NO_ASK_PASSWORD
:
217 arg_ask_password
= false;
233 arg_transport
= BUS_TRANSPORT_REMOTE
;
238 arg_transport
= BUS_TRANSPORT_MACHINE
;
247 r
= free_and_strdup_warn(&arg_mount_type
, optarg
);
253 r
= free_and_strdup_warn(&arg_mount_options
, optarg
);
259 const char *user
= optarg
;
261 r
= get_user_creds(&user
, &arg_uid
, &arg_gid
, NULL
, NULL
, 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",
271 r
= parse_boolean_argument("--fsck=", optarg
, &arg_fsck
);
276 case ARG_DESCRIPTION
:
277 r
= free_and_strdup_warn(&arg_description
, optarg
);
283 if (strv_extend(&arg_property
, optarg
) < 0)
289 arg_action
= ACTION_AUTOMOUNT
;
293 r
= parse_boolean_argument("--automount=", optarg
, NULL
);
297 arg_action
= r
? ACTION_AUTOMOUNT
: ACTION_MOUNT
;
300 case ARG_TIMEOUT_IDLE
:
301 r
= parse_sec(optarg
, &arg_timeout_idle
);
303 return log_error_errno(r
, "Failed to parse timeout: %s", optarg
);
307 case ARG_AUTOMOUNT_PROPERTY
:
308 if (strv_extend(&arg_automount_property
, optarg
) < 0)
313 case ARG_BIND_DEVICE
:
314 arg_bind_device
= true;
318 arg_action
= ACTION_LIST
;
322 arg_action
= ACTION_UMOUNT
;
326 arg_aggressive_gc
= true;
333 assert_not_reached();
337 arg_ask_password
= false;
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.");
343 if (arg_action
== ACTION_LIST
) {
345 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
346 "Too many arguments.");
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
) {
353 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
354 "At least one argument required.");
356 if (arg_transport
!= BUS_TRANSPORT_LOCAL
) {
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
]);
366 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
367 "At least one argument required.");
370 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
371 "At most two arguments required.");
373 if (arg_mount_type
&& !fstype_is_blockdev_backed(arg_mount_type
)) {
374 arg_mount_what
= strdup(argv
[optind
]);
378 } else if (arg_transport
== BUS_TRANSPORT_LOCAL
) {
379 _cleanup_free_
char *u
= NULL
;
381 u
= fstab_node_to_udev_node(argv
[optind
]);
385 r
= chase_symlinks(u
, NULL
, 0, &arg_mount_what
, NULL
);
387 return log_error_errno(r
, "Failed to make path %s absolute: %m", u
);
389 arg_mount_what
= strdup(argv
[optind
]);
393 path_simplify(arg_mount_what
);
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
);
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
);
404 return log_error_errno(r
, "Failed to make path %s absolute: %m", argv
[optind
+1]);
406 arg_mount_where
= strdup(argv
[optind
+1]);
407 if (!arg_mount_where
)
410 path_simplify(arg_mount_where
);
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
);
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.");
427 static int transient_unit_set_properties(sd_bus_message
*m
, UnitType t
, char **properties
) {
430 if (!isempty(arg_description
)) {
431 r
= sd_bus_message_append(m
, "(sv)", "Description", "s", arg_description
);
436 if (arg_bind_device
&& is_device_path(arg_mount_what
)) {
437 _cleanup_free_
char *device_unit
= NULL
;
439 r
= unit_name_from_path(arg_mount_what
, ".device", &device_unit
);
443 r
= sd_bus_message_append(m
, "(sv)(sv)",
444 "After", "as", 1, device_unit
,
445 "BindsTo", "as", 1, device_unit
);
450 if (arg_aggressive_gc
) {
451 r
= sd_bus_message_append(m
, "(sv)", "CollectMode", "s", "inactive-or-failed");
456 r
= bus_append_unit_property_assignment_many(m
, t
, properties
);
463 static int transient_mount_set_properties(sd_bus_message
*m
) {
464 _cleanup_free_
char *options
= NULL
;
469 r
= transient_unit_set_properties(m
, UNIT_MOUNT
, arg_property
);
473 if (arg_mount_what
) {
474 r
= sd_bus_message_append(m
, "(sv)", "What", "s", arg_mount_what
);
479 if (arg_mount_type
) {
480 r
= sd_bus_message_append(m
, "(sv)", "Type", "s", arg_mount_type
);
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",
490 arg_mount_options
? "," : "", strempty(arg_mount_options
));
495 if (options
|| arg_mount_options
) {
496 log_debug("Using mount options: %s", options
?: arg_mount_options
);
498 r
= sd_bus_message_append(m
, "(sv)", "Options", "s", options
?: arg_mount_options
);
502 log_debug("Not using any mount options");
505 _cleanup_free_
char *fsck
= NULL
;
507 r
= unit_name_from_path_instance("systemd-fsck", arg_mount_what
, ".service", &fsck
);
511 r
= sd_bus_message_append(m
,
513 "Requires", "as", 1, fsck
,
514 "After", "as", 1, fsck
);
522 static int transient_automount_set_properties(sd_bus_message
*m
) {
527 r
= transient_unit_set_properties(m
, UNIT_AUTOMOUNT
, arg_automount_property
);
531 if (arg_timeout_idle
!= USEC_INFINITY
) {
532 r
= sd_bus_message_append(m
, "(sv)", "TimeoutIdleUSec", "t", arg_timeout_idle
);
540 static int start_transient_mount(
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
;
551 r
= bus_wait_for_jobs_new(bus
, &w
);
553 return log_error_errno(r
, "Could not watch jobs: %m");
556 r
= unit_name_from_path(arg_mount_where
, ".mount", &mount_unit
);
558 return log_error_errno(r
, "Failed to make mount unit name: %m");
560 r
= bus_message_new_method_call(bus
, &m
, bus_systemd_mgr
, "StartTransientUnit");
562 return bus_log_create_error(r
);
564 r
= sd_bus_message_set_allow_interactive_authorization(m
, arg_ask_password
);
566 return bus_log_create_error(r
);
569 r
= sd_bus_message_append(m
, "ss", mount_unit
, "fail");
571 return bus_log_create_error(r
);
574 r
= sd_bus_message_open_container(m
, 'a', "(sv)");
576 return bus_log_create_error(r
);
578 r
= transient_mount_set_properties(m
);
580 return bus_log_create_error(r
);
582 r
= sd_bus_message_close_container(m
);
584 return bus_log_create_error(r
);
586 /* Auxiliary units */
587 r
= sd_bus_message_append(m
, "a(sa(sv))", 0);
589 return bus_log_create_error(r
);
591 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
593 r
= sd_bus_call(bus
, m
, 0, &error
, &reply
);
595 return log_error_errno(r
, "Failed to start transient mount unit: %s", bus_error_message(&error
, r
));
600 r
= sd_bus_message_read(reply
, "o", &object
);
602 return bus_log_parse_error(r
);
604 r
= bus_wait_for_jobs_one(w
, object
, 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());
617 static int start_transient_automount(
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
;
628 r
= bus_wait_for_jobs_new(bus
, &w
);
630 return log_error_errno(r
, "Could not watch jobs: %m");
633 r
= unit_name_from_path(arg_mount_where
, ".automount", &automount_unit
);
635 return log_error_errno(r
, "Failed to make automount unit name: %m");
637 r
= unit_name_from_path(arg_mount_where
, ".mount", &mount_unit
);
639 return log_error_errno(r
, "Failed to make mount unit name: %m");
641 r
= bus_message_new_method_call(bus
, &m
, bus_systemd_mgr
, "StartTransientUnit");
643 return bus_log_create_error(r
);
645 r
= sd_bus_message_set_allow_interactive_authorization(m
, arg_ask_password
);
647 return bus_log_create_error(r
);
650 r
= sd_bus_message_append(m
, "ss", automount_unit
, "fail");
652 return bus_log_create_error(r
);
655 r
= sd_bus_message_open_container(m
, 'a', "(sv)");
657 return bus_log_create_error(r
);
659 r
= transient_automount_set_properties(m
);
661 return bus_log_create_error(r
);
663 r
= sd_bus_message_close_container(m
);
665 return bus_log_create_error(r
);
667 /* Auxiliary units */
668 r
= sd_bus_message_open_container(m
, 'a', "(sa(sv))");
670 return bus_log_create_error(r
);
672 r
= sd_bus_message_open_container(m
, 'r', "sa(sv)");
674 return bus_log_create_error(r
);
676 r
= sd_bus_message_append(m
, "s", mount_unit
);
678 return bus_log_create_error(r
);
680 r
= sd_bus_message_open_container(m
, 'a', "(sv)");
682 return bus_log_create_error(r
);
684 r
= transient_mount_set_properties(m
);
686 return bus_log_create_error(r
);
688 r
= sd_bus_message_close_container(m
);
690 return bus_log_create_error(r
);
692 r
= sd_bus_message_close_container(m
);
694 return bus_log_create_error(r
);
696 r
= sd_bus_message_close_container(m
);
698 return bus_log_create_error(r
);
700 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
702 r
= sd_bus_call(bus
, m
, 0, &error
, &reply
);
704 return log_error_errno(r
, "Failed to start transient automount unit: %s", bus_error_message(&error
, r
));
709 r
= sd_bus_message_read(reply
, "o", &object
);
711 return bus_log_parse_error(r
);
713 r
= bus_wait_for_jobs_one(w
, object
, 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());
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
;
736 /* Returns all mount points obtained from /proc/self/mountinfo in *list,
737 * and the number of mount points as return value. */
739 r
= libmount_parse(NULL
, NULL
, &table
, &iter
);
741 return log_error_errno(r
, "Failed to parse /proc/self/mountinfo: %m");
744 struct libmnt_fs
*fs
;
745 const char *source
, *target
;
747 r
= mnt_table_next_fs(table
, iter
, &fs
);
751 return log_error_errno(r
, "Failed to get next entry from /proc/self/mountinfo: %m");
753 source
= mnt_fs_get_source(fs
);
754 target
= mnt_fs_get_target(fs
);
755 if (!source
|| !target
)
758 if (!path_equal(source
, what
))
761 /* one extra slot is needed for the terminating NULL */
762 if (!GREEDY_REALLOC0(l
, n
+ 2))
765 l
[n
] = strdup(target
);
771 if (!GREEDY_REALLOC0(l
, n
+ 1))
778 static int find_loop_device(const char *backing_file
, char **loop_dev
) {
779 _cleanup_closedir_
DIR *d
= NULL
;
781 _cleanup_free_
char *l
= NULL
;
783 assert(backing_file
);
786 d
= opendir("/sys/devices/virtual/block");
790 FOREACH_DIRENT(de
, d
, return -errno
) {
791 _cleanup_free_
char *sys
= NULL
, *fname
= NULL
;
794 if (de
->d_type
!= DT_DIR
)
797 if (!startswith(de
->d_name
, "loop"))
800 sys
= path_join("/sys/devices/virtual/block", de
->d_name
, "loop/backing_file");
804 r
= read_one_line_file(sys
, &fname
);
806 log_debug_errno(r
, "Failed to read %s, ignoring: %m", sys
);
810 if (files_same(fname
, backing_file
, 0) <= 0)
813 l
= path_join("/dev", de
->d_name
);
823 *loop_dev
= TAKE_PTR(l
);
828 static int stop_mount(
831 const char *suffix
) {
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
;
840 r
= bus_wait_for_jobs_new(bus
, &w
);
842 return log_error_errno(r
, "Could not watch jobs: %m");
845 r
= unit_name_from_path(where
, suffix
, &mount_unit
);
847 return log_error_errno(r
, "Failed to make %s unit name from path %s: %m", suffix
+ 1, where
);
849 r
= bus_message_new_method_call(bus
, &m
, bus_systemd_mgr
, "StopUnit");
851 return bus_log_create_error(r
);
853 r
= sd_bus_message_set_allow_interactive_authorization(m
, arg_ask_password
);
855 return bus_log_create_error(r
);
858 r
= sd_bus_message_append(m
, "ss", mount_unit
, "fail");
860 return bus_log_create_error(r
);
862 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
864 r
= sd_bus_call(bus
, m
, 0, &error
, &reply
);
866 if (streq(suffix
, ".automount") &&
867 sd_bus_error_has_name(&error
, "org.freedesktop.systemd1.NoSuchUnit"))
869 return log_error_errno(r
, "Failed to stop %s unit: %s", suffix
+ 1, bus_error_message(&error
, r
));
875 r
= sd_bus_message_read(reply
, "o", &object
);
877 return bus_log_parse_error(r
);
879 r
= bus_wait_for_jobs_one(w
, object
, 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());
892 static int stop_mounts(
898 if (path_equal(where
, "/"))
899 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
900 "Refusing to operate on root directory: %s", where
);
902 if (!path_is_normalized(where
))
903 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
904 "Path contains non-normalized components: %s", where
);
906 r
= stop_mount(bus
, where
, ".mount");
910 r
= stop_mount(bus
, where
, ".automount");
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
;
927 if (stat(what
, &st
) < 0)
928 return log_error_errno(errno
, "Can't stat %s: %m", what
);
930 if (!S_ISBLK(st
.st_mode
))
931 return log_error_errno(SYNTHETIC_ERRNO(ENOTBLK
),
932 "Not a block device: %s", what
);
934 r
= sd_device_new_from_stat_rdev(&d
, &st
);
936 return log_error_errno(r
, "Failed to get device from device number: %m");
938 r
= sd_device_get_property_value(d
, "ID_FS_USAGE", &v
);
940 return log_device_error_errno(d
, r
, "Failed to get device property: %m");
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
);
946 if (sd_device_get_property_value(d
, "SYSTEMD_MOUNT_WHERE", &v
) >= 0)
947 r2
= stop_mounts(bus
, v
);
949 r
= find_mount_points(what
, &list
);
953 for (l
= list
; *l
; l
++) {
954 r
= stop_mounts(bus
, *l
);
962 static int umount_loop(sd_bus
*bus
, const char *backing_file
) {
963 _cleanup_free_
char *loop_dev
= NULL
;
966 assert(backing_file
);
968 r
= find_loop_device(backing_file
, &loop_dev
);
970 return log_error_errno(r
, r
== -ENXIO
? "File %s is not mounted." : "Can't get loop device for %s: %m", backing_file
);
972 return umount_by_device(bus
, loop_dev
);
975 static int action_umount(
982 if (arg_transport
!= BUS_TRANSPORT_LOCAL
) {
983 for (i
= optind
; i
< argc
; i
++) {
984 _cleanup_free_
char *p
= NULL
;
992 r
= stop_mounts(bus
, p
);
999 for (i
= optind
; i
< argc
; i
++) {
1000 _cleanup_free_
char *u
= NULL
, *p
= NULL
;
1003 u
= fstab_node_to_udev_node(argv
[i
]);
1007 r
= chase_symlinks(u
, NULL
, 0, &p
, NULL
);
1009 r2
= log_error_errno(r
, "Failed to make path %s absolute: %m", argv
[i
]);
1013 if (stat(p
, &st
) < 0)
1014 return log_error_errno(errno
, "Can't stat %s (from %s): %m", p
, argv
[i
]);
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
);
1023 log_error("Invalid file type: %s (from %s)", p
, argv
[i
]);
1034 static int acquire_mount_type(sd_device
*d
) {
1042 if (sd_device_get_property_value(d
, "ID_FS_TYPE", &v
) < 0)
1045 arg_mount_type
= strdup(v
);
1046 if (!arg_mount_type
)
1049 log_debug("Discovered type=%s", arg_mount_type
);
1053 static int acquire_mount_options(sd_device
*d
) {
1058 if (arg_mount_options
)
1061 if (sd_device_get_property_value(d
, "SYSTEMD_MOUNT_OPTIONS", &v
) < 0)
1064 arg_mount_options
= strdup(v
);
1065 if (!arg_mount_options
)
1068 log_debug("Discovered options=%s", arg_mount_options
);
1072 static const char *get_model(sd_device
*d
) {
1077 if (sd_device_get_property_value(d
, "ID_MODEL_FROM_DATABASE", &model
) >= 0)
1080 if (sd_device_get_property_value(d
, "ID_MODEL", &model
) >= 0)
1086 static const char* get_label(sd_device
*d
) {
1091 if (sd_device_get_property_value(d
, "ID_FS_LABEL", &label
) >= 0)
1094 if (sd_device_get_property_value(d
, "ID_PART_ENTRY_NAME", &label
) >= 0)
1100 static int acquire_mount_where(sd_device
*d
) {
1103 if (arg_mount_where
)
1106 if (sd_device_get_property_value(d
, "SYSTEMD_MOUNT_WHERE", &v
) < 0) {
1107 _cleanup_free_
char *escaped
= NULL
;
1110 name
= get_label(d
);
1112 name
= get_model(d
);
1116 if (sd_device_get_devname(d
, &dn
) < 0)
1119 name
= basename(dn
);
1122 escaped
= xescape(name
, "\\");
1125 if (!filename_is_valid(escaped
))
1128 arg_mount_where
= path_join("/run/media/system", escaped
);
1130 arg_mount_where
= strdup(v
);
1132 if (!arg_mount_where
)
1135 log_debug("Discovered where=%s", arg_mount_where
);
1139 static int acquire_mount_where_for_loop_dev(const char *loop_dev
) {
1140 _cleanup_strv_free_
char **list
= NULL
;
1143 if (arg_mount_where
)
1146 r
= find_mount_points(loop_dev
, &list
);
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
);
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
);
1158 arg_mount_where
= strdup(list
[0]);
1159 if (!arg_mount_where
)
1162 log_debug("Discovered where=%s", arg_mount_where
);
1166 static int acquire_description(sd_device
*d
) {
1167 const char *model
, *label
;
1169 if (arg_description
)
1172 model
= get_model(d
);
1174 label
= get_label(d
);
1176 (void) sd_device_get_property_value(d
, "ID_PART_ENTRY_NUMBER", &label
);
1179 arg_description
= strjoin(model
, " ", label
);
1181 arg_description
= strdup(label
);
1183 arg_description
= strdup(model
);
1187 if (!arg_description
)
1190 log_debug("Discovered description=%s", arg_description
);
1194 static int acquire_removable(sd_device
*d
) {
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)
1202 if (sd_device_get_sysattr_value(d
, "removable", &v
) >= 0)
1205 if (sd_device_get_parent(d
, &d
) < 0)
1208 if (sd_device_get_subsystem(d
, &v
) < 0 || !streq(v
, "block"))
1212 if (parse_boolean(v
) <= 0)
1215 log_debug("Discovered removable device.");
1217 if (arg_action
== ACTION_DEFAULT
) {
1218 log_debug("Automatically turning on automount.");
1219 arg_action
= ACTION_AUTOMOUNT
;
1222 if (!arg_timeout_idle_set
) {
1223 log_debug("Setting idle timeout to 1s.");
1224 arg_timeout_idle
= USEC_PER_SEC
;
1227 if (arg_bind_device
< 0) {
1228 log_debug("Binding automount unit to device.");
1229 arg_bind_device
= true;
1235 static int discover_loop_backing_file(void) {
1236 _cleanup_(sd_device_unrefp
) sd_device
*d
= NULL
;
1237 _cleanup_free_
char *loop_dev
= NULL
;
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
);
1247 _cleanup_free_
char *escaped
= NULL
;
1249 if (arg_mount_where
)
1252 escaped
= xescape(basename(arg_mount_what
), "\\");
1255 if (!filename_is_valid(escaped
))
1256 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1257 "Escaped name %s is not a valid filename.",
1260 arg_mount_where
= path_join("/run/media/system", escaped
);
1261 if (!arg_mount_where
)
1264 log_debug("Discovered where=%s", arg_mount_where
);
1268 if (stat(loop_dev
, &st
) < 0)
1269 return log_error_errno(errno
, "Can't stat %s: %m", loop_dev
);
1271 if (!S_ISBLK(st
.st_mode
))
1272 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1273 "Invalid file type: %s", loop_dev
);
1275 r
= sd_device_new_from_stat_rdev(&d
, &st
);
1277 return log_error_errno(r
, "Failed to get device from device number: %m");
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
);
1283 r
= acquire_mount_type(d
);
1287 r
= acquire_mount_options(d
);
1291 r
= acquire_mount_where_for_loop_dev(loop_dev
);
1295 r
= acquire_description(d
);
1302 static int discover_device(void) {
1303 _cleanup_(sd_device_unrefp
) sd_device
*d
= NULL
;
1308 if (stat(arg_mount_what
, &st
) < 0)
1309 return log_error_errno(errno
, "Can't stat %s: %m", arg_mount_what
);
1311 if (S_ISREG(st
.st_mode
))
1312 return discover_loop_backing_file();
1314 if (!S_ISBLK(st
.st_mode
))
1315 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1316 "Invalid file type: %s",
1319 r
= sd_device_new_from_stat_rdev(&d
, &st
);
1321 return log_error_errno(r
, "Failed to get device from device number: %m");
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.",
1328 r
= acquire_mount_type(d
);
1332 r
= acquire_mount_options(d
);
1336 r
= acquire_mount_where(d
);
1340 r
= acquire_description(d
);
1344 r
= acquire_removable(d
);
1362 static int list_devices(void) {
1363 _cleanup_(sd_device_enumerator_unrefp
) sd_device_enumerator
*e
= NULL
;
1364 _cleanup_(table_unrefp
) Table
*table
= NULL
;
1369 r
= sd_device_enumerator_new(&e
);
1373 r
= sd_device_enumerator_add_match_subsystem(e
, "block", true);
1375 return log_error_errno(r
, "Failed to add block match: %m");
1377 r
= sd_device_enumerator_add_match_property(e
, "ID_FS_USAGE", "filesystem");
1379 return log_error_errno(r
, "Failed to add property match: %m");
1381 table
= table_new("NODE", "PATH", "MODEL", "WWN", "TYPE", "LABEL", "UUID");
1386 table_set_width(table
, 0);
1388 r
= table_set_sort(table
, (size_t) 0);
1390 return log_error_errno(r
, "Failed to set sort index: %m");
1392 table_set_header(table
, arg_legend
);
1394 FOREACH_DEVICE(e
, d
) {
1395 for (c
= 0; c
< _COLUMN_MAX
; c
++) {
1396 const char *x
= NULL
;
1401 (void) sd_device_get_devname(d
, &x
);
1405 (void) sd_device_get_property_value(d
, "ID_PATH", &x
);
1413 (void) sd_device_get_property_value(d
, "ID_WWN", &x
);
1417 (void) sd_device_get_property_value(d
, "ID_FS_TYPE", &x
);
1425 (void) sd_device_get_property_value(d
, "ID_FS_UUID", &x
);
1429 r
= table_add_cell(table
, NULL
, c
== COLUMN_NODE
? TABLE_PATH
: TABLE_STRING
, strna(x
));
1431 return table_log_add_error(r
);
1435 (void) pager_open(arg_pager_flags
);
1437 r
= table_print(table
, NULL
);
1439 return table_log_print_error(r
);
1444 static int run(int argc
, char* argv
[]) {
1445 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1448 log_show_color(true);
1449 log_parse_environment();
1452 r
= parse_argv(argc
, argv
);
1456 if (arg_action
== ACTION_LIST
)
1457 return list_devices();
1459 r
= bus_connect_transport_systemd(arg_transport
, arg_host
, arg_user
, &bus
);
1461 return bus_log_connect_error(r
, arg_transport
);
1463 if (arg_action
== ACTION_UMOUNT
)
1464 return action_umount(bus
, argc
, argv
);
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",
1473 r
= discover_device();
1478 if (!arg_mount_where
)
1479 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1480 "Can't figure out where to mount %s.",
1483 if (path_equal(arg_mount_where
, "/"))
1484 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1485 "Refusing to operate on root directory.");
1487 if (!path_is_normalized(arg_mount_where
))
1488 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1489 "Path contains non-normalized components: %s",
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
);
1497 if (!is_device_path(arg_mount_what
))
1500 if (arg_fsck
&& arg_mount_type
&& arg_transport
== BUS_TRANSPORT_LOCAL
) {
1501 r
= fsck_exists(arg_mount_type
);
1503 log_warning_errno(r
, "Couldn't determine whether fsck for %s exists, proceeding anyway.", arg_mount_type
);
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 */
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.
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.",
1525 switch (arg_action
) {
1528 case ACTION_DEFAULT
:
1529 r
= start_transient_mount(bus
, argv
+ optind
);
1532 case ACTION_AUTOMOUNT
:
1533 r
= start_transient_automount(bus
, argv
+ optind
);
1537 assert_not_reached();
1543 DEFINE_MAIN_FUNCTION(run
);