1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
10 #include "bus-locator.h"
11 #include "bus-unit-util.h"
12 #include "bus-wait-for-jobs.h"
14 #include "device-util.h"
15 #include "dirent-util.h"
19 #include "format-table.h"
20 #include "format-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"
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"
37 #include "terminal-util.h"
38 #include "udev-util.h"
39 #include "umask-util.h"
41 #include "unit-name.h"
42 #include "user-util.h"
50 } arg_action
= ACTION_DEFAULT
;
52 static bool arg_no_block
= false;
53 static PagerFlags arg_pager_flags
= 0;
54 static bool arg_legend
= true;
55 static bool arg_full
= false;
56 static bool arg_ask_password
= true;
57 static bool arg_quiet
= false;
58 static BusTransport arg_transport
= BUS_TRANSPORT_LOCAL
;
59 static RuntimeScope arg_runtime_scope
= RUNTIME_SCOPE_SYSTEM
;
60 static const char *arg_host
= NULL
;
61 static bool arg_discover
= false;
62 static char *arg_mount_what
= NULL
;
63 static char *arg_mount_where
= NULL
;
64 static char *arg_mount_type
= NULL
;
65 static char *arg_mount_options
= NULL
;
66 static char *arg_description
= NULL
;
67 static char **arg_property
= NULL
;
68 static usec_t arg_timeout_idle
= USEC_INFINITY
;
69 static bool arg_timeout_idle_set
= false;
70 static char **arg_automount_property
= NULL
;
71 static int arg_bind_device
= -1;
72 static uid_t arg_uid
= UID_INVALID
;
73 static gid_t arg_gid
= GID_INVALID
;
74 static bool arg_fsck
= true;
75 static bool arg_aggressive_gc
= false;
76 static bool arg_tmpfs
= false;
78 STATIC_DESTRUCTOR_REGISTER(arg_mount_what
, freep
);
79 STATIC_DESTRUCTOR_REGISTER(arg_mount_where
, freep
);
80 STATIC_DESTRUCTOR_REGISTER(arg_mount_type
, freep
);
81 STATIC_DESTRUCTOR_REGISTER(arg_mount_options
, freep
);
82 STATIC_DESTRUCTOR_REGISTER(arg_description
, freep
);
83 STATIC_DESTRUCTOR_REGISTER(arg_property
, strv_freep
);
84 STATIC_DESTRUCTOR_REGISTER(arg_automount_property
, strv_freep
);
86 static int parse_where(const char *input
, char **ret_where
) {
92 if (arg_transport
== BUS_TRANSPORT_LOCAL
) {
93 r
= chase(input
, NULL
, CHASE_NONEXISTENT
, ret_where
, NULL
);
95 return log_error_errno(r
, "Failed to make path %s absolute: %m", input
);
97 if (!path_is_absolute(input
))
98 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
99 "Path must be absolute when operating remotely: %s",
102 r
= path_simplify_alloc(input
, ret_where
);
104 return log_error_errno(r
, "Failed to simplify path %s: %m", input
);
110 static int help(void) {
111 _cleanup_free_
char *link
= NULL
;
114 r
= terminal_urlify_man("systemd-mount", "1", &link
);
118 printf("systemd-mount [OPTIONS...] WHAT [WHERE]\n"
119 "systemd-mount [OPTIONS...] --tmpfs [NAME] WHERE\n"
120 "systemd-mount [OPTIONS...] --list\n"
121 "%s [OPTIONS...] %sWHAT|WHERE...\n\n"
122 "Establish a mount or auto-mount point transiently.\n\n"
123 " -h --help Show this help\n"
124 " --version Show package version\n"
125 " --no-block Do not wait until operation finished\n"
126 " --no-pager Do not pipe output into a pager\n"
127 " --no-legend Do not show the headers\n"
128 " -l --full Do not ellipsize output\n"
129 " --no-ask-password Do not prompt for password\n"
130 " -q --quiet Suppress information messages during runtime\n"
131 " --user Run as user unit\n"
132 " -H --host=[USER@]HOST Operate on remote host\n"
133 " -M --machine=CONTAINER Operate on local container\n"
134 " --discover Discover mount device metadata\n"
135 " -t --type=TYPE File system type\n"
136 " -o --options=OPTIONS Mount options\n"
137 " --owner=USER Add uid= and gid= options for USER\n"
138 " --fsck=no Don't run file system check before mount\n"
139 " --description=TEXT Description for unit\n"
140 " -p --property=NAME=VALUE Set mount unit property\n"
141 " -A --automount=BOOL Create an auto-mount point\n"
142 " --timeout-idle-sec=SEC Specify automount idle timeout\n"
143 " --automount-property=NAME=VALUE\n"
144 " Set automount unit property\n"
145 " --bind-device Bind automount unit to device\n"
146 " --list List mountable block devices\n"
147 " -u --umount Unmount mount points\n"
148 " -G --collect Unload unit after it stopped, even when failed\n"
149 " -T --tmpfs Create a new tmpfs on the mount point\n"
150 "\nSee the %s for details.\n",
151 program_invocation_short_name
,
152 streq(program_invocation_short_name
, "systemd-umount") ? "" : "--umount ",
158 static int parse_argv(int argc
, char *argv
[]) {
176 ARG_AUTOMOUNT_PROPERTY
,
181 static const struct option options
[] = {
182 { "help", no_argument
, NULL
, 'h' },
183 { "version", no_argument
, NULL
, ARG_VERSION
},
184 { "no-block", no_argument
, NULL
, ARG_NO_BLOCK
},
185 { "no-pager", no_argument
, NULL
, ARG_NO_PAGER
},
186 { "no-legend", no_argument
, NULL
, ARG_NO_LEGEND
},
187 { "full", no_argument
, NULL
, 'l' },
188 { "no-ask-password", no_argument
, NULL
, ARG_NO_ASK_PASSWORD
},
189 { "quiet", no_argument
, NULL
, 'q' },
190 { "user", no_argument
, NULL
, ARG_USER
},
191 { "system", no_argument
, NULL
, ARG_SYSTEM
},
192 { "host", required_argument
, NULL
, 'H' },
193 { "machine", required_argument
, NULL
, 'M' },
194 { "discover", no_argument
, NULL
, ARG_DISCOVER
},
195 { "type", required_argument
, NULL
, 't' },
196 { "options", required_argument
, NULL
, 'o' },
197 { "owner", required_argument
, NULL
, ARG_OWNER
},
198 { "fsck", required_argument
, NULL
, ARG_FSCK
},
199 { "description", required_argument
, NULL
, ARG_DESCRIPTION
},
200 { "property", required_argument
, NULL
, 'p' },
201 { "automount", required_argument
, NULL
, ARG_AUTOMOUNT
},
202 { "timeout-idle-sec", required_argument
, NULL
, ARG_TIMEOUT_IDLE
},
203 { "automount-property", required_argument
, NULL
, ARG_AUTOMOUNT_PROPERTY
},
204 { "bind-device", no_argument
, NULL
, ARG_BIND_DEVICE
},
205 { "list", no_argument
, NULL
, ARG_LIST
},
206 { "umount", no_argument
, NULL
, 'u' },
207 { "unmount", no_argument
, NULL
, 'u' }, /* Compat spelling */
208 { "collect", no_argument
, NULL
, 'G' },
209 { "tmpfs", no_argument
, NULL
, 'T' },
218 if (invoked_as(argv
, "systemd-umount"))
219 arg_action
= ACTION_UMOUNT
;
221 while ((c
= getopt_long(argc
, argv
, "hqH:M:t:o:p:AuGlT", options
, NULL
)) >= 0)
236 arg_pager_flags
|= PAGER_DISABLE
;
247 case ARG_NO_ASK_PASSWORD
:
248 arg_ask_password
= false;
256 arg_runtime_scope
= RUNTIME_SCOPE_USER
;
260 arg_runtime_scope
= RUNTIME_SCOPE_SYSTEM
;
264 arg_transport
= BUS_TRANSPORT_REMOTE
;
269 arg_transport
= BUS_TRANSPORT_MACHINE
;
278 r
= free_and_strdup_warn(&arg_mount_type
, optarg
);
284 r
= free_and_strdup_warn(&arg_mount_options
, optarg
);
290 const char *user
= optarg
;
292 r
= get_user_creds(&user
, &arg_uid
, &arg_gid
, NULL
, NULL
, 0);
294 return log_error_errno(r
,
295 r
== -EBADMSG
? "UID or GID of user %s are invalid."
296 : "Cannot use \"%s\" as owner: %m",
302 r
= parse_boolean_argument("--fsck=", optarg
, &arg_fsck
);
307 case ARG_DESCRIPTION
:
308 r
= free_and_strdup_warn(&arg_description
, optarg
);
314 if (strv_extend(&arg_property
, optarg
) < 0)
320 arg_action
= ACTION_AUTOMOUNT
;
324 r
= parse_boolean_argument("--automount=", optarg
, NULL
);
328 arg_action
= r
? ACTION_AUTOMOUNT
: ACTION_MOUNT
;
331 case ARG_TIMEOUT_IDLE
:
332 r
= parse_sec(optarg
, &arg_timeout_idle
);
334 return log_error_errno(r
, "Failed to parse timeout: %s", optarg
);
338 case ARG_AUTOMOUNT_PROPERTY
:
339 if (strv_extend(&arg_automount_property
, optarg
) < 0)
344 case ARG_BIND_DEVICE
:
345 arg_bind_device
= true;
349 arg_action
= ACTION_LIST
;
353 arg_action
= ACTION_UMOUNT
;
357 arg_aggressive_gc
= true;
368 assert_not_reached();
371 if (arg_runtime_scope
== RUNTIME_SCOPE_USER
) {
372 arg_ask_password
= false;
374 if (arg_transport
!= BUS_TRANSPORT_LOCAL
)
375 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
376 "Execution in user context is not supported on non-local systems.");
379 if (arg_action
== ACTION_LIST
) {
381 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
382 "Too many arguments.");
384 if (arg_transport
!= BUS_TRANSPORT_LOCAL
)
385 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP
),
386 "Listing devices only supported locally.");
387 } else if (arg_action
== ACTION_UMOUNT
) {
389 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
390 "At least one argument required.");
392 if (arg_transport
!= BUS_TRANSPORT_LOCAL
)
393 for (int i
= optind
; i
< argc
; i
++)
394 if (!path_is_absolute(argv
[i
]))
395 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
396 "Path must be absolute when operating remotely: %s",
400 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
401 "At least one argument required.");
404 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
405 "More than two arguments are not allowed.");
408 if (argc
<= optind
+1) {
409 arg_mount_what
= strdup("tmpfs");
413 r
= parse_where(argv
[optind
], &arg_mount_where
);
417 arg_mount_what
= strdup(argv
[optind
]);
422 if (!strv_contains(arg_property
, "Type=tmpfs") &&
423 strv_extend(&arg_property
, "Type=tmpfs") < 0)
426 if (arg_mount_type
&& !fstype_is_blockdev_backed(arg_mount_type
)) {
427 arg_mount_what
= strdup(argv
[optind
]);
431 } else if (arg_transport
== BUS_TRANSPORT_LOCAL
) {
432 _cleanup_free_
char *u
= NULL
;
434 u
= fstab_node_to_udev_node(argv
[optind
]);
438 r
= chase(u
, NULL
, 0, &arg_mount_what
, NULL
);
440 return log_error_errno(r
, "Failed to make path %s absolute: %m", u
);
443 if (!path_is_absolute(argv
[optind
]))
444 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
445 "Path must be absolute when operating remotely: %s",
448 r
= path_simplify_alloc(argv
[optind
], &arg_mount_what
);
450 return log_error_errno(r
, "Failed to simplify path: %m");
454 if (argc
> optind
+1) {
455 r
= parse_where(argv
[optind
+1], &arg_mount_where
);
458 } else if (!arg_tmpfs
)
461 if (arg_discover
&& arg_transport
!= BUS_TRANSPORT_LOCAL
)
462 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP
),
463 "Automatic mount location discovery is only supported locally.");
469 static int transient_unit_set_properties(sd_bus_message
*m
, UnitType t
, char **properties
) {
472 if (!isempty(arg_description
)) {
473 r
= sd_bus_message_append(m
, "(sv)", "Description", "s", arg_description
);
478 if (arg_bind_device
&& is_device_path(arg_mount_what
)) {
479 _cleanup_free_
char *device_unit
= NULL
;
481 r
= unit_name_from_path(arg_mount_what
, ".device", &device_unit
);
485 r
= sd_bus_message_append(m
, "(sv)(sv)",
486 "After", "as", 1, device_unit
,
487 "BindsTo", "as", 1, device_unit
);
492 if (arg_aggressive_gc
) {
493 r
= sd_bus_message_append(m
, "(sv)", "CollectMode", "s", "inactive-or-failed");
498 r
= bus_append_unit_property_assignment_many(m
, t
, properties
);
505 static int transient_mount_set_properties(sd_bus_message
*m
) {
510 r
= transient_unit_set_properties(m
, UNIT_MOUNT
, arg_property
);
514 if (arg_mount_what
) {
515 r
= sd_bus_message_append(m
, "(sv)", "What", "s", arg_mount_what
);
520 if (arg_mount_type
) {
521 r
= sd_bus_message_append(m
, "(sv)", "Type", "s", arg_mount_type
);
526 _cleanup_free_
char *options
= NULL
;
528 /* Prepend uid=…,gid=… if arg_uid is set */
529 if (arg_uid
!= UID_INVALID
) {
530 r
= strextendf_with_separator(&options
, ",",
531 "uid="UID_FMT
",gid="GID_FMT
, arg_uid
, arg_gid
);
536 /* Override the default for tmpfs mounts. The kernel sets the sticky bit on the root directory by
537 * default. This makes sense for the case when the user does 'mount -t tmpfs tmpfs /tmp', but less so
538 * for other directories.
540 * Let's also set some reasonable limits. We use the current umask, to match what a command to create
541 * directory would use, e.g. mkdir. */
545 r
= get_process_umask(0, &mask
);
549 assert((mask
& ~0777) == 0);
550 r
= strextendf_with_separator(&options
, ",",
551 "mode=0%o,nodev,nosuid%s", 0777 & ~mask
, NESTED_TMPFS_LIMITS
);
556 if (arg_mount_options
)
557 if (!strextend_with_separator(&options
, ",", arg_mount_options
))
561 log_debug("Using mount options: %s", options
);
562 r
= sd_bus_message_append(m
, "(sv)", "Options", "s", options
);
566 log_debug("Not using any mount options");
569 _cleanup_free_
char *fsck
= NULL
;
571 r
= unit_name_from_path_instance("systemd-fsck", arg_mount_what
, ".service", &fsck
);
575 r
= sd_bus_message_append(m
,
577 "Requires", "as", 1, fsck
,
578 "After", "as", 1, fsck
);
586 static int transient_automount_set_properties(sd_bus_message
*m
) {
591 r
= transient_unit_set_properties(m
, UNIT_AUTOMOUNT
, arg_automount_property
);
595 if (arg_timeout_idle
!= USEC_INFINITY
) {
596 r
= sd_bus_message_append(m
, "(sv)", "TimeoutIdleUSec", "t", arg_timeout_idle
);
604 static int start_transient_mount(
608 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
, *reply
= NULL
;
609 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
610 _cleanup_(bus_wait_for_jobs_freep
) BusWaitForJobs
*w
= NULL
;
611 _cleanup_free_
char *mount_unit
= NULL
;
615 r
= bus_wait_for_jobs_new(bus
, &w
);
617 return log_error_errno(r
, "Could not watch jobs: %m");
620 r
= unit_name_from_path(arg_mount_where
, ".mount", &mount_unit
);
622 return log_error_errno(r
, "Failed to make mount unit name: %m");
624 r
= bus_message_new_method_call(bus
, &m
, bus_systemd_mgr
, "StartTransientUnit");
626 return bus_log_create_error(r
);
628 r
= sd_bus_message_set_allow_interactive_authorization(m
, arg_ask_password
);
630 return bus_log_create_error(r
);
633 r
= sd_bus_message_append(m
, "ss", mount_unit
, "fail");
635 return bus_log_create_error(r
);
638 r
= sd_bus_message_open_container(m
, 'a', "(sv)");
640 return bus_log_create_error(r
);
642 r
= transient_mount_set_properties(m
);
644 return bus_log_create_error(r
);
646 r
= sd_bus_message_close_container(m
);
648 return bus_log_create_error(r
);
650 /* Auxiliary units */
651 r
= sd_bus_message_append(m
, "a(sa(sv))", 0);
653 return bus_log_create_error(r
);
655 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
657 r
= sd_bus_call(bus
, m
, 0, &error
, &reply
);
659 return log_error_errno(r
, "Failed to start transient mount unit: %s", bus_error_message(&error
, r
));
664 r
= sd_bus_message_read(reply
, "o", &object
);
666 return bus_log_parse_error(r
);
668 r
= bus_wait_for_jobs_one(w
, object
, arg_quiet
? 0 : BUS_WAIT_JOBS_LOG_ERROR
, NULL
);
674 log_info("Started unit %s%s%s for mount point: %s%s%s",
675 ansi_highlight(), mount_unit
, ansi_normal(),
676 ansi_highlight(), arg_mount_where
, ansi_normal());
681 static int start_transient_automount(
685 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
, *reply
= NULL
;
686 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
687 _cleanup_(bus_wait_for_jobs_freep
) BusWaitForJobs
*w
= NULL
;
688 _cleanup_free_
char *automount_unit
= NULL
, *mount_unit
= NULL
;
692 r
= bus_wait_for_jobs_new(bus
, &w
);
694 return log_error_errno(r
, "Could not watch jobs: %m");
697 r
= unit_name_from_path(arg_mount_where
, ".automount", &automount_unit
);
699 return log_error_errno(r
, "Failed to make automount unit name: %m");
701 r
= unit_name_from_path(arg_mount_where
, ".mount", &mount_unit
);
703 return log_error_errno(r
, "Failed to make mount unit name: %m");
705 r
= bus_message_new_method_call(bus
, &m
, bus_systemd_mgr
, "StartTransientUnit");
707 return bus_log_create_error(r
);
709 r
= sd_bus_message_set_allow_interactive_authorization(m
, arg_ask_password
);
711 return bus_log_create_error(r
);
714 r
= sd_bus_message_append(m
, "ss", automount_unit
, "fail");
716 return bus_log_create_error(r
);
719 r
= sd_bus_message_open_container(m
, 'a', "(sv)");
721 return bus_log_create_error(r
);
723 r
= transient_automount_set_properties(m
);
725 return bus_log_create_error(r
);
727 r
= sd_bus_message_close_container(m
);
729 return bus_log_create_error(r
);
731 /* Auxiliary units */
732 r
= sd_bus_message_open_container(m
, 'a', "(sa(sv))");
734 return bus_log_create_error(r
);
736 r
= sd_bus_message_open_container(m
, 'r', "sa(sv)");
738 return bus_log_create_error(r
);
740 r
= sd_bus_message_append(m
, "s", mount_unit
);
742 return bus_log_create_error(r
);
744 r
= sd_bus_message_open_container(m
, 'a', "(sv)");
746 return bus_log_create_error(r
);
748 r
= transient_mount_set_properties(m
);
750 return bus_log_create_error(r
);
752 r
= sd_bus_message_close_container(m
);
754 return bus_log_create_error(r
);
756 r
= sd_bus_message_close_container(m
);
758 return bus_log_create_error(r
);
760 r
= sd_bus_message_close_container(m
);
762 return bus_log_create_error(r
);
764 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
766 r
= sd_bus_call(bus
, m
, 0, &error
, &reply
);
768 return log_error_errno(r
, "Failed to start transient automount unit: %s", bus_error_message(&error
, r
));
773 r
= sd_bus_message_read(reply
, "o", &object
);
775 return bus_log_parse_error(r
);
777 r
= bus_wait_for_jobs_one(w
, object
, arg_quiet
? 0 : BUS_WAIT_JOBS_LOG_ERROR
, NULL
);
783 log_info("Started unit %s%s%s for mount point: %s%s%s",
784 ansi_highlight(), automount_unit
, ansi_normal(),
785 ansi_highlight(), arg_mount_where
, ansi_normal());
790 static int find_mount_points(const char *what
, char ***list
) {
791 _cleanup_(mnt_free_tablep
) struct libmnt_table
*table
= NULL
;
792 _cleanup_(mnt_free_iterp
) struct libmnt_iter
*iter
= NULL
;
793 _cleanup_strv_free_
char **l
= NULL
;
800 /* Returns all mount points obtained from /proc/self/mountinfo in *list,
801 * and the number of mount points as return value. */
803 r
= libmount_parse(NULL
, NULL
, &table
, &iter
);
805 return log_error_errno(r
, "Failed to parse /proc/self/mountinfo: %m");
808 struct libmnt_fs
*fs
;
809 const char *source
, *target
;
811 r
= mnt_table_next_fs(table
, iter
, &fs
);
815 return log_error_errno(r
, "Failed to get next entry from /proc/self/mountinfo: %m");
817 source
= mnt_fs_get_source(fs
);
818 target
= mnt_fs_get_target(fs
);
819 if (!source
|| !target
)
822 if (!path_equal(source
, what
))
825 /* one extra slot is needed for the terminating NULL */
826 if (!GREEDY_REALLOC0(l
, n
+ 2))
829 l
[n
] = strdup(target
);
835 if (!GREEDY_REALLOC0(l
, n
+ 1))
842 static int find_loop_device(const char *backing_file
, sd_device
**ret
) {
843 _cleanup_(sd_device_enumerator_unrefp
) sd_device_enumerator
*e
= NULL
;
846 assert(backing_file
);
849 r
= sd_device_enumerator_new(&e
);
853 r
= sd_device_enumerator_add_match_subsystem(e
, "block", /* match = */ true);
855 return log_error_errno(r
, "Failed to add subsystem match: %m");
857 r
= sd_device_enumerator_add_match_property(e
, "ID_FS_USAGE", "filesystem");
859 return log_error_errno(r
, "Failed to add property match: %m");
861 r
= sd_device_enumerator_add_match_sysname(e
, "loop*");
863 return log_error_errno(r
, "Failed to add sysname match: %m");
865 r
= sd_device_enumerator_add_match_sysattr(e
, "loop/backing_file", /* value = */ NULL
, /* match = */ true);
867 return log_error_errno(r
, "Failed to add sysattr match: %m");
869 FOREACH_DEVICE(e
, dev
) {
872 r
= sd_device_get_sysattr_value(dev
, "loop/backing_file", &s
);
874 log_device_debug_errno(dev
, r
, "Failed to read \"loop/backing_file\" sysattr, ignoring: %m");
878 if (inode_same(s
, backing_file
, 0) <= 0)
881 *ret
= sd_device_ref(dev
);
888 static int stop_mount(
891 const char *suffix
) {
893 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
, *reply
= NULL
;
894 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
895 _cleanup_(bus_wait_for_jobs_freep
) BusWaitForJobs
*w
= NULL
;
896 _cleanup_free_
char *mount_unit
= NULL
;
900 r
= bus_wait_for_jobs_new(bus
, &w
);
902 return log_error_errno(r
, "Could not watch jobs: %m");
905 r
= unit_name_from_path(where
, suffix
, &mount_unit
);
907 return log_error_errno(r
, "Failed to make %s unit name from path %s: %m", suffix
+ 1, where
);
909 r
= bus_message_new_method_call(bus
, &m
, bus_systemd_mgr
, "StopUnit");
911 return bus_log_create_error(r
);
913 r
= sd_bus_message_set_allow_interactive_authorization(m
, arg_ask_password
);
915 return bus_log_create_error(r
);
918 r
= sd_bus_message_append(m
, "ss", mount_unit
, "fail");
920 return bus_log_create_error(r
);
922 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
924 r
= sd_bus_call(bus
, m
, 0, &error
, &reply
);
926 if (streq(suffix
, ".automount") &&
927 sd_bus_error_has_name(&error
, "org.freedesktop.systemd1.NoSuchUnit"))
929 return log_error_errno(r
, "Failed to stop %s unit: %s", suffix
+ 1, bus_error_message(&error
, r
));
935 r
= sd_bus_message_read(reply
, "o", &object
);
937 return bus_log_parse_error(r
);
939 r
= bus_wait_for_jobs_one(w
, object
, arg_quiet
? 0 : BUS_WAIT_JOBS_LOG_ERROR
, NULL
);
945 log_info("Stopped unit %s%s%s for mount point: %s%s%s",
946 ansi_highlight(), mount_unit
, ansi_normal(),
947 ansi_highlight(), where
, ansi_normal());
952 static int stop_mounts(
958 if (path_equal(where
, "/"))
959 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
960 "Refusing to operate on root directory: %s", where
);
962 if (!path_is_normalized(where
))
963 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
964 "Path contains non-normalized components: %s", where
);
966 r
= stop_mount(bus
, where
, ".mount");
970 r
= stop_mount(bus
, where
, ".automount");
977 static int umount_by_device(sd_bus
*bus
, sd_device
*dev
) {
978 _cleanup_strv_free_
char **list
= NULL
;
985 if (sd_device_get_property_value(dev
, "SYSTEMD_MOUNT_WHERE", &v
) >= 0)
986 ret
= stop_mounts(bus
, v
);
988 r
= sd_device_get_devname(dev
, &v
);
992 r
= find_mount_points(v
, &list
);
996 STRV_FOREACH(l
, list
) {
997 r
= stop_mounts(bus
, *l
);
1005 static int umount_by_device_node(sd_bus
*bus
, const char *node
) {
1006 _cleanup_(sd_device_unrefp
) sd_device
*dev
= NULL
;
1013 r
= sd_device_new_from_devname(&dev
, node
);
1015 return log_error_errno(r
, "Failed to get device from %s: %m", node
);
1017 r
= sd_device_get_property_value(dev
, "ID_FS_USAGE", &v
);
1019 return log_device_error_errno(dev
, r
, "Failed to get \"ID_FS_USAGE\" device property: %m");
1021 if (!streq(v
, "filesystem"))
1022 return log_device_error_errno(dev
, SYNTHETIC_ERRNO(EINVAL
),
1023 "%s does not contain a known file system.", node
);
1025 return umount_by_device(bus
, dev
);
1028 static int umount_loop(sd_bus
*bus
, const char *backing_file
) {
1029 _cleanup_(sd_device_unrefp
) sd_device
*dev
= NULL
;
1032 assert(backing_file
);
1034 r
= find_loop_device(backing_file
, &dev
);
1036 return log_error_errno(r
, r
== -ENXIO
? "File %s is not mounted." : "Can't get loop device for %s: %m", backing_file
);
1038 return umount_by_device(bus
, dev
);
1041 static int action_umount(
1048 if (arg_transport
!= BUS_TRANSPORT_LOCAL
) {
1049 for (int i
= optind
; i
< argc
; i
++) {
1050 _cleanup_free_
char *p
= NULL
;
1052 r
= path_simplify_alloc(argv
[i
], &p
);
1056 r
= stop_mounts(bus
, p
);
1063 for (int i
= optind
; i
< argc
; i
++) {
1064 _cleanup_free_
char *u
= NULL
, *p
= NULL
;
1067 u
= fstab_node_to_udev_node(argv
[i
]);
1071 r
= chase(u
, NULL
, 0, &p
, NULL
);
1073 r2
= log_error_errno(r
, "Failed to make path %s absolute: %m", argv
[i
]);
1077 if (stat(p
, &st
) < 0)
1078 return log_error_errno(errno
, "Can't stat %s (from %s): %m", p
, argv
[i
]);
1080 if (S_ISBLK(st
.st_mode
))
1081 r
= umount_by_device_node(bus
, p
);
1082 else if (S_ISREG(st
.st_mode
))
1083 r
= umount_loop(bus
, p
);
1084 else if (S_ISDIR(st
.st_mode
))
1085 r
= stop_mounts(bus
, p
);
1087 log_error("Invalid file type: %s (from %s)", p
, argv
[i
]);
1098 static int acquire_mount_type(sd_device
*d
) {
1106 if (sd_device_get_property_value(d
, "ID_FS_TYPE", &v
) < 0)
1109 arg_mount_type
= strdup(v
);
1110 if (!arg_mount_type
)
1113 log_debug("Discovered type=%s", arg_mount_type
);
1117 static int acquire_mount_options(sd_device
*d
) {
1122 if (arg_mount_options
)
1125 if (sd_device_get_property_value(d
, "SYSTEMD_MOUNT_OPTIONS", &v
) < 0)
1128 arg_mount_options
= strdup(v
);
1129 if (!arg_mount_options
)
1132 log_debug("Discovered options=%s", arg_mount_options
);
1136 static const char* get_label(sd_device
*d
) {
1141 if (sd_device_get_property_value(d
, "ID_FS_LABEL", &label
) >= 0)
1144 if (sd_device_get_property_value(d
, "ID_PART_ENTRY_NAME", &label
) >= 0)
1150 static int acquire_mount_where(sd_device
*d
) {
1154 if (arg_mount_where
)
1157 if (sd_device_get_property_value(d
, "SYSTEMD_MOUNT_WHERE", &v
) < 0) {
1158 _cleanup_free_
char *escaped
= NULL
, *devname_bn
= NULL
;
1161 name
= get_label(d
);
1163 (void) device_get_model_string(d
, &name
);
1167 if (sd_device_get_devname(d
, &dn
) < 0)
1170 r
= path_extract_filename(dn
, &devname_bn
);
1172 return log_error_errno(r
, "Failed to extract file name from '%s': %m", dn
);
1177 escaped
= xescape(name
, "\\");
1180 if (!filename_is_valid(escaped
))
1183 arg_mount_where
= path_join("/run/media/system", escaped
);
1185 arg_mount_where
= strdup(v
);
1187 if (!arg_mount_where
)
1190 log_debug("Discovered where=%s", arg_mount_where
);
1194 static int acquire_mount_where_for_loop_dev(sd_device
*dev
) {
1195 _cleanup_strv_free_
char **list
= NULL
;
1201 if (arg_mount_where
)
1204 r
= sd_device_get_devname(dev
, &node
);
1208 r
= find_mount_points(node
, &list
);
1212 return log_device_error_errno(dev
, SYNTHETIC_ERRNO(EINVAL
),
1213 "Can't find mount point of %s. It is expected that %s is already mounted on a place.",
1216 return log_device_error_errno(dev
, SYNTHETIC_ERRNO(EINVAL
),
1217 "%s is mounted on %d places. It is expected that %s is mounted on a place.",
1220 arg_mount_where
= strdup(list
[0]);
1221 if (!arg_mount_where
)
1224 log_debug("Discovered where=%s", arg_mount_where
);
1228 static int acquire_description(sd_device
*d
) {
1229 const char *model
= NULL
, *label
;
1231 if (arg_description
)
1234 (void) device_get_model_string(d
, &model
);
1236 label
= get_label(d
);
1238 (void) sd_device_get_property_value(d
, "ID_PART_ENTRY_NUMBER", &label
);
1241 arg_description
= strjoin(model
, " ", label
);
1243 arg_description
= strdup(label
);
1245 arg_description
= strdup(model
);
1249 if (!arg_description
)
1252 log_debug("Discovered description=%s", arg_description
);
1256 static int acquire_removable(sd_device
*d
) {
1259 /* Shortcut this if there's no reason to check it */
1260 if (arg_action
!= ACTION_DEFAULT
&& arg_timeout_idle_set
&& arg_bind_device
>= 0)
1264 if (sd_device_get_sysattr_value(d
, "removable", &v
) >= 0)
1267 if (sd_device_get_parent(d
, &d
) < 0)
1270 if (sd_device_get_subsystem(d
, &v
) < 0 || !streq(v
, "block"))
1274 if (parse_boolean(v
) <= 0)
1277 log_debug("Discovered removable device.");
1279 if (arg_action
== ACTION_DEFAULT
) {
1280 log_debug("Automatically turning on automount.");
1281 arg_action
= ACTION_AUTOMOUNT
;
1284 if (!arg_timeout_idle_set
) {
1285 log_debug("Setting idle timeout to 1s.");
1286 arg_timeout_idle
= USEC_PER_SEC
;
1289 if (arg_bind_device
< 0) {
1290 log_debug("Binding automount unit to device.");
1291 arg_bind_device
= true;
1297 static int discover_loop_backing_file(void) {
1298 _cleanup_(sd_device_unrefp
) sd_device
*d
= NULL
;
1301 r
= find_loop_device(arg_mount_what
, &d
);
1302 if (r
< 0 && r
!= -ENXIO
)
1303 return log_error_errno(errno
, "Can't get loop device for %s: %m", arg_mount_what
);
1306 _cleanup_free_
char *escaped
= NULL
, *bn
= NULL
;
1308 if (arg_mount_where
)
1311 r
= path_extract_filename(arg_mount_what
, &bn
);
1313 return log_error_errno(r
, "Failed to extract file name from backing file path '%s': %m", arg_mount_what
);
1315 escaped
= xescape(bn
, "\\");
1318 if (!filename_is_valid(escaped
))
1319 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1320 "Escaped name %s is not a valid filename.",
1323 arg_mount_where
= path_join("/run/media/system", escaped
);
1324 if (!arg_mount_where
)
1327 log_debug("Discovered where=%s", arg_mount_where
);
1331 r
= acquire_mount_type(d
);
1335 r
= acquire_mount_options(d
);
1339 r
= acquire_mount_where_for_loop_dev(d
);
1343 r
= acquire_description(d
);
1350 static int discover_device(void) {
1351 _cleanup_(sd_device_unrefp
) sd_device
*d
= NULL
;
1356 if (stat(arg_mount_what
, &st
) < 0)
1357 return log_error_errno(errno
, "Can't stat %s: %m", arg_mount_what
);
1359 if (S_ISREG(st
.st_mode
))
1360 return discover_loop_backing_file();
1362 if (!S_ISBLK(st
.st_mode
))
1363 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1364 "Invalid file type: %s",
1367 r
= sd_device_new_from_stat_rdev(&d
, &st
);
1369 return log_error_errno(r
, "Failed to get device from device number: %m");
1371 if (sd_device_get_property_value(d
, "ID_FS_USAGE", &v
) < 0 || !streq(v
, "filesystem"))
1372 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1373 "%s does not contain a known file system.",
1376 r
= acquire_mount_type(d
);
1380 r
= acquire_mount_options(d
);
1384 r
= acquire_mount_where(d
);
1388 r
= acquire_description(d
);
1392 r
= acquire_removable(d
);
1399 static int list_devices(void) {
1411 _cleanup_(sd_device_enumerator_unrefp
) sd_device_enumerator
*e
= NULL
;
1412 _cleanup_(table_unrefp
) Table
*table
= NULL
;
1415 r
= sd_device_enumerator_new(&e
);
1419 r
= sd_device_enumerator_add_match_subsystem(e
, "block", true);
1421 return log_error_errno(r
, "Failed to add block match: %m");
1423 r
= sd_device_enumerator_add_match_property(e
, "ID_FS_USAGE", "filesystem");
1425 return log_error_errno(r
, "Failed to add property match: %m");
1427 table
= table_new("NODE", "PATH", "MODEL", "WWN", "FSTYPE", "LABEL", "UUID");
1432 table_set_width(table
, 0);
1434 r
= table_set_sort(table
, (size_t) 0);
1436 return log_error_errno(r
, "Failed to set sort index: %m");
1438 table_set_header(table
, arg_legend
);
1439 table_set_ersatz_string(table
, TABLE_ERSATZ_DASH
);
1441 FOREACH_DEVICE(e
, d
) {
1442 for (unsigned c
= 0; c
< _COLUMN_MAX
; c
++) {
1443 const char *x
= NULL
;
1448 (void) sd_device_get_devname(d
, &x
);
1452 (void) sd_device_get_property_value(d
, "ID_PATH", &x
);
1456 (void) device_get_model_string(d
, &x
);
1460 (void) sd_device_get_property_value(d
, "ID_WWN", &x
);
1464 (void) sd_device_get_property_value(d
, "ID_FS_TYPE", &x
);
1472 (void) sd_device_get_property_value(d
, "ID_FS_UUID", &x
);
1476 r
= table_add_cell(table
, NULL
, c
== COLUMN_NODE
? TABLE_PATH
: TABLE_STRING
, x
);
1478 return table_log_add_error(r
);
1482 pager_open(arg_pager_flags
);
1484 r
= table_print(table
, NULL
);
1486 return table_log_print_error(r
);
1491 static int run(int argc
, char* argv
[]) {
1492 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1495 log_show_color(true);
1496 log_parse_environment();
1499 r
= parse_argv(argc
, argv
);
1503 if (arg_action
== ACTION_LIST
)
1504 return list_devices();
1506 r
= bus_connect_transport_systemd(arg_transport
, arg_host
, arg_runtime_scope
, &bus
);
1508 return bus_log_connect_error(r
, arg_transport
);
1510 if (arg_action
== ACTION_UMOUNT
)
1511 return action_umount(bus
, argc
, argv
);
1513 if ((!arg_mount_type
|| fstype_is_blockdev_backed(arg_mount_type
))
1514 && !path_is_normalized(arg_mount_what
))
1515 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1516 "Path contains non-normalized components: %s",
1520 r
= discover_device();
1525 if (!arg_mount_where
)
1526 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1527 "Can't figure out where to mount %s.",
1530 if (path_equal(arg_mount_where
, "/"))
1531 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1532 "Refusing to operate on root directory.");
1534 if (!path_is_normalized(arg_mount_where
))
1535 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1536 "Path contains non-normalized components: %s",
1539 if (streq_ptr(arg_mount_type
, "auto"))
1540 arg_mount_type
= mfree(arg_mount_type
);
1541 if (streq_ptr(arg_mount_options
, "defaults"))
1542 arg_mount_options
= mfree(arg_mount_options
);
1544 if (!is_device_path(arg_mount_what
))
1547 if (arg_fsck
&& arg_mount_type
&& arg_transport
== BUS_TRANSPORT_LOCAL
) {
1548 r
= fsck_exists_for_fstype(arg_mount_type
);
1550 log_warning_errno(r
, "Couldn't determine whether fsck for %s exists, proceeding anyway.", arg_mount_type
);
1552 log_debug("Disabling file system check as fsck for %s doesn't exist.", arg_mount_type
);
1553 arg_fsck
= false; /* fsck doesn't exist, let's not attempt it */
1557 /* The kernel (properly) refuses mounting file systems with unknown uid=,gid= options,
1558 * but not for all filesystem types. Let's try to catch the cases where the option
1559 * would be used if the file system does not support it. It is also possible to
1560 * autodetect the file system, but that's only possible with disk-based file systems
1561 * which incidentally seem to be implemented more carefully and reject unknown options,
1562 * so it's probably OK that we do the check only when the type is specified.
1564 if (arg_mount_type
&&
1565 !streq(arg_mount_type
, "auto") &&
1566 arg_uid
!= UID_INVALID
&&
1567 !fstype_can_uid_gid(arg_mount_type
))
1568 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP
),
1569 "File system type %s is not known to support uid=/gid=, refusing.",
1572 switch (arg_action
) {
1575 case ACTION_DEFAULT
:
1576 r
= start_transient_mount(bus
, argv
+ optind
);
1579 case ACTION_AUTOMOUNT
:
1580 r
= start_transient_automount(bus
, argv
+ optind
);
1584 assert_not_reached();
1590 DEFINE_MAIN_FUNCTION(run
);