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 "umask-util.h"
40 #include "unit-name.h"
41 #include "user-util.h"
49 } arg_action
= ACTION_DEFAULT
;
51 static bool arg_no_block
= false;
52 static PagerFlags arg_pager_flags
= 0;
53 static bool arg_legend
= true;
54 static bool arg_full
= false;
55 static bool arg_ask_password
= true;
56 static bool arg_quiet
= false;
57 static BusTransport arg_transport
= BUS_TRANSPORT_LOCAL
;
58 static RuntimeScope arg_runtime_scope
= RUNTIME_SCOPE_SYSTEM
;
59 static const char *arg_host
= NULL
;
60 static bool arg_discover
= false;
61 static char *arg_mount_what
= NULL
;
62 static char *arg_mount_where
= NULL
;
63 static char *arg_mount_type
= NULL
;
64 static char *arg_mount_options
= NULL
;
65 static char *arg_description
= NULL
;
66 static char **arg_property
= NULL
;
67 static usec_t arg_timeout_idle
= USEC_INFINITY
;
68 static bool arg_timeout_idle_set
= false;
69 static char **arg_automount_property
= NULL
;
70 static int arg_bind_device
= -1;
71 static uid_t arg_uid
= UID_INVALID
;
72 static gid_t arg_gid
= GID_INVALID
;
73 static bool arg_fsck
= true;
74 static bool arg_aggressive_gc
= false;
75 static bool arg_tmpfs
= false;
77 STATIC_DESTRUCTOR_REGISTER(arg_mount_what
, freep
);
78 STATIC_DESTRUCTOR_REGISTER(arg_mount_where
, freep
);
79 STATIC_DESTRUCTOR_REGISTER(arg_mount_type
, freep
);
80 STATIC_DESTRUCTOR_REGISTER(arg_mount_options
, freep
);
81 STATIC_DESTRUCTOR_REGISTER(arg_description
, freep
);
82 STATIC_DESTRUCTOR_REGISTER(arg_property
, strv_freep
);
83 STATIC_DESTRUCTOR_REGISTER(arg_automount_property
, strv_freep
);
85 static int parse_where(const char *input
, char **ret_where
) {
86 _cleanup_free_
char *where
= NULL
;
92 if (arg_transport
== BUS_TRANSPORT_LOCAL
) {
93 r
= chase(input
, NULL
, CHASE_NONEXISTENT
, &where
, NULL
);
95 return log_error_errno(r
, "Failed to make path %s absolute: %m", input
);
97 where
= strdup(input
);
101 path_simplify(where
);
103 if (!path_is_absolute(where
))
104 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
105 "Path must be absolute when operating remotely: %s",
109 *ret_where
= TAKE_PTR(where
);
113 static int help(void) {
114 _cleanup_free_
char *link
= NULL
;
117 r
= terminal_urlify_man("systemd-mount", "1", &link
);
121 printf("systemd-mount [OPTIONS...] WHAT [WHERE]\n"
122 "systemd-mount [OPTIONS...] --tmpfs [NAME] WHERE\n"
123 "systemd-mount [OPTIONS...] --list\n"
124 "%s [OPTIONS...] %sWHAT|WHERE...\n\n"
125 "Establish a mount or auto-mount point transiently.\n\n"
126 " -h --help Show this help\n"
127 " --version Show package version\n"
128 " --no-block Do not wait until operation finished\n"
129 " --no-pager Do not pipe output into a pager\n"
130 " --no-legend Do not show the headers\n"
131 " -l --full Do not ellipsize output\n"
132 " --no-ask-password Do not prompt for password\n"
133 " -q --quiet Suppress information messages during runtime\n"
134 " --user Run as user unit\n"
135 " -H --host=[USER@]HOST Operate on remote host\n"
136 " -M --machine=CONTAINER Operate on local container\n"
137 " --discover Discover mount device metadata\n"
138 " -t --type=TYPE File system type\n"
139 " -o --options=OPTIONS Mount options\n"
140 " --owner=USER Add uid= and gid= options for USER\n"
141 " --fsck=no Don't run file system check before mount\n"
142 " --description=TEXT Description for unit\n"
143 " -p --property=NAME=VALUE Set mount unit property\n"
144 " -A --automount=BOOL Create an auto-mount point\n"
145 " --timeout-idle-sec=SEC Specify automount idle timeout\n"
146 " --automount-property=NAME=VALUE\n"
147 " Set automount unit property\n"
148 " --bind-device Bind automount unit to device\n"
149 " --list List mountable block devices\n"
150 " -u --umount Unmount mount points\n"
151 " -G --collect Unload unit after it stopped, even when failed\n"
152 " -T --tmpfs Create a new tmpfs on the mount point\n"
153 "\nSee the %s for details.\n",
154 program_invocation_short_name
,
155 streq(program_invocation_short_name
, "systemd-umount") ? "" : "--umount ",
161 static int parse_argv(int argc
, char *argv
[]) {
179 ARG_AUTOMOUNT_PROPERTY
,
184 static const struct option options
[] = {
185 { "help", no_argument
, NULL
, 'h' },
186 { "version", no_argument
, NULL
, ARG_VERSION
},
187 { "no-block", no_argument
, NULL
, ARG_NO_BLOCK
},
188 { "no-pager", no_argument
, NULL
, ARG_NO_PAGER
},
189 { "no-legend", no_argument
, NULL
, ARG_NO_LEGEND
},
190 { "full", no_argument
, NULL
, 'l' },
191 { "no-ask-password", no_argument
, NULL
, ARG_NO_ASK_PASSWORD
},
192 { "quiet", no_argument
, NULL
, 'q' },
193 { "user", no_argument
, NULL
, ARG_USER
},
194 { "system", no_argument
, NULL
, ARG_SYSTEM
},
195 { "host", required_argument
, NULL
, 'H' },
196 { "machine", required_argument
, NULL
, 'M' },
197 { "discover", no_argument
, NULL
, ARG_DISCOVER
},
198 { "type", required_argument
, NULL
, 't' },
199 { "options", required_argument
, NULL
, 'o' },
200 { "owner", required_argument
, NULL
, ARG_OWNER
},
201 { "fsck", required_argument
, NULL
, ARG_FSCK
},
202 { "description", required_argument
, NULL
, ARG_DESCRIPTION
},
203 { "property", required_argument
, NULL
, 'p' },
204 { "automount", required_argument
, NULL
, ARG_AUTOMOUNT
},
205 { "timeout-idle-sec", required_argument
, NULL
, ARG_TIMEOUT_IDLE
},
206 { "automount-property", required_argument
, NULL
, ARG_AUTOMOUNT_PROPERTY
},
207 { "bind-device", no_argument
, NULL
, ARG_BIND_DEVICE
},
208 { "list", no_argument
, NULL
, ARG_LIST
},
209 { "umount", no_argument
, NULL
, 'u' },
210 { "unmount", no_argument
, NULL
, 'u' }, /* Compat spelling */
211 { "collect", no_argument
, NULL
, 'G' },
212 { "tmpfs", no_argument
, NULL
, 'T' },
221 if (invoked_as(argv
, "systemd-umount"))
222 arg_action
= ACTION_UMOUNT
;
224 while ((c
= getopt_long(argc
, argv
, "hqH:M:t:o:p:AuGlT", options
, NULL
)) >= 0)
239 arg_pager_flags
|= PAGER_DISABLE
;
250 case ARG_NO_ASK_PASSWORD
:
251 arg_ask_password
= false;
259 arg_runtime_scope
= RUNTIME_SCOPE_USER
;
263 arg_runtime_scope
= RUNTIME_SCOPE_SYSTEM
;
267 arg_transport
= BUS_TRANSPORT_REMOTE
;
272 arg_transport
= BUS_TRANSPORT_MACHINE
;
281 r
= free_and_strdup_warn(&arg_mount_type
, optarg
);
287 r
= free_and_strdup_warn(&arg_mount_options
, optarg
);
293 const char *user
= optarg
;
295 r
= get_user_creds(&user
, &arg_uid
, &arg_gid
, NULL
, NULL
, 0);
297 return log_error_errno(r
,
298 r
== -EBADMSG
? "UID or GID of user %s are invalid."
299 : "Cannot use \"%s\" as owner: %m",
305 r
= parse_boolean_argument("--fsck=", optarg
, &arg_fsck
);
310 case ARG_DESCRIPTION
:
311 r
= free_and_strdup_warn(&arg_description
, optarg
);
317 if (strv_extend(&arg_property
, optarg
) < 0)
323 arg_action
= ACTION_AUTOMOUNT
;
327 r
= parse_boolean_argument("--automount=", optarg
, NULL
);
331 arg_action
= r
? ACTION_AUTOMOUNT
: ACTION_MOUNT
;
334 case ARG_TIMEOUT_IDLE
:
335 r
= parse_sec(optarg
, &arg_timeout_idle
);
337 return log_error_errno(r
, "Failed to parse timeout: %s", optarg
);
341 case ARG_AUTOMOUNT_PROPERTY
:
342 if (strv_extend(&arg_automount_property
, optarg
) < 0)
347 case ARG_BIND_DEVICE
:
348 arg_bind_device
= true;
352 arg_action
= ACTION_LIST
;
356 arg_action
= ACTION_UMOUNT
;
360 arg_aggressive_gc
= true;
371 assert_not_reached();
374 if (arg_runtime_scope
== RUNTIME_SCOPE_USER
) {
375 arg_ask_password
= false;
377 if (arg_transport
!= BUS_TRANSPORT_LOCAL
)
378 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
379 "Execution in user context is not supported on non-local systems.");
382 if (arg_action
== ACTION_LIST
) {
384 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
385 "Too many arguments.");
387 if (arg_transport
!= BUS_TRANSPORT_LOCAL
)
388 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP
),
389 "Listing devices only supported locally.");
390 } else if (arg_action
== ACTION_UMOUNT
) {
392 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
393 "At least one argument required.");
395 if (arg_transport
!= BUS_TRANSPORT_LOCAL
)
396 for (int i
= optind
; i
< argc
; i
++)
397 if (!path_is_absolute(argv
[i
]))
398 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
399 "Path must be absolute when operating remotely: %s",
403 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
404 "At least one argument required.");
407 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
408 "More than two arguments are not allowed.");
411 if (argc
<= optind
+1) {
412 arg_mount_what
= strdup("tmpfs");
416 r
= parse_where(argv
[optind
], &arg_mount_where
);
420 arg_mount_what
= strdup(argv
[optind
]);
425 if (!strv_contains(arg_property
, "Type=tmpfs") &&
426 strv_extend(&arg_property
, "Type=tmpfs") < 0)
429 if (arg_mount_type
&& !fstype_is_blockdev_backed(arg_mount_type
)) {
430 arg_mount_what
= strdup(argv
[optind
]);
434 } else if (arg_transport
== BUS_TRANSPORT_LOCAL
) {
435 _cleanup_free_
char *u
= NULL
;
437 u
= fstab_node_to_udev_node(argv
[optind
]);
441 r
= chase(u
, NULL
, 0, &arg_mount_what
, NULL
);
443 return log_error_errno(r
, "Failed to make path %s absolute: %m", u
);
445 arg_mount_what
= strdup(argv
[optind
]);
449 path_simplify(arg_mount_what
);
451 if (!path_is_absolute(arg_mount_what
))
452 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
453 "Path must be absolute when operating remotely: %s",
458 if (argc
> optind
+1) {
459 r
= parse_where(argv
[optind
+1], &arg_mount_where
);
462 } else if (!arg_tmpfs
)
465 if (arg_discover
&& arg_transport
!= BUS_TRANSPORT_LOCAL
)
466 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP
),
467 "Automatic mount location discovery is only supported locally.");
473 static int transient_unit_set_properties(sd_bus_message
*m
, UnitType t
, char **properties
) {
476 if (!isempty(arg_description
)) {
477 r
= sd_bus_message_append(m
, "(sv)", "Description", "s", arg_description
);
482 if (arg_bind_device
&& is_device_path(arg_mount_what
)) {
483 _cleanup_free_
char *device_unit
= NULL
;
485 r
= unit_name_from_path(arg_mount_what
, ".device", &device_unit
);
489 r
= sd_bus_message_append(m
, "(sv)(sv)",
490 "After", "as", 1, device_unit
,
491 "BindsTo", "as", 1, device_unit
);
496 if (arg_aggressive_gc
) {
497 r
= sd_bus_message_append(m
, "(sv)", "CollectMode", "s", "inactive-or-failed");
502 r
= bus_append_unit_property_assignment_many(m
, t
, properties
);
509 static int transient_mount_set_properties(sd_bus_message
*m
) {
514 r
= transient_unit_set_properties(m
, UNIT_MOUNT
, arg_property
);
518 if (arg_mount_what
) {
519 r
= sd_bus_message_append(m
, "(sv)", "What", "s", arg_mount_what
);
524 if (arg_mount_type
) {
525 r
= sd_bus_message_append(m
, "(sv)", "Type", "s", arg_mount_type
);
530 _cleanup_free_
char *options
= NULL
;
532 /* Prepend uid=…,gid=… if arg_uid is set */
533 if (arg_uid
!= UID_INVALID
) {
534 r
= strextendf_with_separator(&options
, ",",
535 "uid="UID_FMT
",gid="GID_FMT
, arg_uid
, arg_gid
);
540 /* Override the default for tmpfs mounts. The kernel sets the sticky bit on the root directory by
541 * default. This makes sense for the case when the user does 'mount -t tmpfs tmpfs /tmp', but less so
542 * for other directories.
544 * Let's also set some reasonable limits. We use the current umask, to match what a command to create
545 * directory would use, e.g. mkdir. */
549 r
= get_process_umask(0, &mask
);
553 assert((mask
& ~0777) == 0);
554 r
= strextendf_with_separator(&options
, ",",
555 "mode=0%o,nodev,nosuid%s", 0777 & ~mask
, NESTED_TMPFS_LIMITS
);
560 if (arg_mount_options
)
561 if (!strextend_with_separator(&options
, ",", arg_mount_options
))
565 log_debug("Using mount options: %s", options
);
566 r
= sd_bus_message_append(m
, "(sv)", "Options", "s", options
);
570 log_debug("Not using any mount options");
573 _cleanup_free_
char *fsck
= NULL
;
575 r
= unit_name_from_path_instance("systemd-fsck", arg_mount_what
, ".service", &fsck
);
579 r
= sd_bus_message_append(m
,
581 "Requires", "as", 1, fsck
,
582 "After", "as", 1, fsck
);
590 static int transient_automount_set_properties(sd_bus_message
*m
) {
595 r
= transient_unit_set_properties(m
, UNIT_AUTOMOUNT
, arg_automount_property
);
599 if (arg_timeout_idle
!= USEC_INFINITY
) {
600 r
= sd_bus_message_append(m
, "(sv)", "TimeoutIdleUSec", "t", arg_timeout_idle
);
608 static int start_transient_mount(
612 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
, *reply
= NULL
;
613 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
614 _cleanup_(bus_wait_for_jobs_freep
) BusWaitForJobs
*w
= NULL
;
615 _cleanup_free_
char *mount_unit
= NULL
;
619 r
= bus_wait_for_jobs_new(bus
, &w
);
621 return log_error_errno(r
, "Could not watch jobs: %m");
624 r
= unit_name_from_path(arg_mount_where
, ".mount", &mount_unit
);
626 return log_error_errno(r
, "Failed to make mount unit name: %m");
628 r
= bus_message_new_method_call(bus
, &m
, bus_systemd_mgr
, "StartTransientUnit");
630 return bus_log_create_error(r
);
632 r
= sd_bus_message_set_allow_interactive_authorization(m
, arg_ask_password
);
634 return bus_log_create_error(r
);
637 r
= sd_bus_message_append(m
, "ss", mount_unit
, "fail");
639 return bus_log_create_error(r
);
642 r
= sd_bus_message_open_container(m
, 'a', "(sv)");
644 return bus_log_create_error(r
);
646 r
= transient_mount_set_properties(m
);
648 return bus_log_create_error(r
);
650 r
= sd_bus_message_close_container(m
);
652 return bus_log_create_error(r
);
654 /* Auxiliary units */
655 r
= sd_bus_message_append(m
, "a(sa(sv))", 0);
657 return bus_log_create_error(r
);
659 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
661 r
= sd_bus_call(bus
, m
, 0, &error
, &reply
);
663 return log_error_errno(r
, "Failed to start transient mount unit: %s", bus_error_message(&error
, r
));
668 r
= sd_bus_message_read(reply
, "o", &object
);
670 return bus_log_parse_error(r
);
672 r
= bus_wait_for_jobs_one(w
, object
, arg_quiet
, NULL
);
678 log_info("Started unit %s%s%s for mount point: %s%s%s",
679 ansi_highlight(), mount_unit
, ansi_normal(),
680 ansi_highlight(), arg_mount_where
, ansi_normal());
685 static int start_transient_automount(
689 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
, *reply
= NULL
;
690 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
691 _cleanup_(bus_wait_for_jobs_freep
) BusWaitForJobs
*w
= NULL
;
692 _cleanup_free_
char *automount_unit
= NULL
, *mount_unit
= NULL
;
696 r
= bus_wait_for_jobs_new(bus
, &w
);
698 return log_error_errno(r
, "Could not watch jobs: %m");
701 r
= unit_name_from_path(arg_mount_where
, ".automount", &automount_unit
);
703 return log_error_errno(r
, "Failed to make automount unit name: %m");
705 r
= unit_name_from_path(arg_mount_where
, ".mount", &mount_unit
);
707 return log_error_errno(r
, "Failed to make mount unit name: %m");
709 r
= bus_message_new_method_call(bus
, &m
, bus_systemd_mgr
, "StartTransientUnit");
711 return bus_log_create_error(r
);
713 r
= sd_bus_message_set_allow_interactive_authorization(m
, arg_ask_password
);
715 return bus_log_create_error(r
);
718 r
= sd_bus_message_append(m
, "ss", automount_unit
, "fail");
720 return bus_log_create_error(r
);
723 r
= sd_bus_message_open_container(m
, 'a', "(sv)");
725 return bus_log_create_error(r
);
727 r
= transient_automount_set_properties(m
);
729 return bus_log_create_error(r
);
731 r
= sd_bus_message_close_container(m
);
733 return bus_log_create_error(r
);
735 /* Auxiliary units */
736 r
= sd_bus_message_open_container(m
, 'a', "(sa(sv))");
738 return bus_log_create_error(r
);
740 r
= sd_bus_message_open_container(m
, 'r', "sa(sv)");
742 return bus_log_create_error(r
);
744 r
= sd_bus_message_append(m
, "s", mount_unit
);
746 return bus_log_create_error(r
);
748 r
= sd_bus_message_open_container(m
, 'a', "(sv)");
750 return bus_log_create_error(r
);
752 r
= transient_mount_set_properties(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 r
= sd_bus_message_close_container(m
);
766 return bus_log_create_error(r
);
768 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
770 r
= sd_bus_call(bus
, m
, 0, &error
, &reply
);
772 return log_error_errno(r
, "Failed to start transient automount unit: %s", bus_error_message(&error
, r
));
777 r
= sd_bus_message_read(reply
, "o", &object
);
779 return bus_log_parse_error(r
);
781 r
= bus_wait_for_jobs_one(w
, object
, arg_quiet
, NULL
);
787 log_info("Started unit %s%s%s for mount point: %s%s%s",
788 ansi_highlight(), automount_unit
, ansi_normal(),
789 ansi_highlight(), arg_mount_where
, ansi_normal());
794 static int find_mount_points(const char *what
, char ***list
) {
795 _cleanup_(mnt_free_tablep
) struct libmnt_table
*table
= NULL
;
796 _cleanup_(mnt_free_iterp
) struct libmnt_iter
*iter
= NULL
;
797 _cleanup_strv_free_
char **l
= NULL
;
804 /* Returns all mount points obtained from /proc/self/mountinfo in *list,
805 * and the number of mount points as return value. */
807 r
= libmount_parse(NULL
, NULL
, &table
, &iter
);
809 return log_error_errno(r
, "Failed to parse /proc/self/mountinfo: %m");
812 struct libmnt_fs
*fs
;
813 const char *source
, *target
;
815 r
= mnt_table_next_fs(table
, iter
, &fs
);
819 return log_error_errno(r
, "Failed to get next entry from /proc/self/mountinfo: %m");
821 source
= mnt_fs_get_source(fs
);
822 target
= mnt_fs_get_target(fs
);
823 if (!source
|| !target
)
826 if (!path_equal(source
, what
))
829 /* one extra slot is needed for the terminating NULL */
830 if (!GREEDY_REALLOC0(l
, n
+ 2))
833 l
[n
] = strdup(target
);
839 if (!GREEDY_REALLOC0(l
, n
+ 1))
846 static int find_loop_device(const char *backing_file
, sd_device
**ret
) {
847 _cleanup_(sd_device_enumerator_unrefp
) sd_device_enumerator
*e
= NULL
;
850 assert(backing_file
);
853 r
= sd_device_enumerator_new(&e
);
857 r
= sd_device_enumerator_add_match_subsystem(e
, "block", /* match = */ true);
859 return log_error_errno(r
, "Failed to add subsystem match: %m");
861 r
= sd_device_enumerator_add_match_property(e
, "ID_FS_USAGE", "filesystem");
863 return log_error_errno(r
, "Failed to add property match: %m");
865 r
= sd_device_enumerator_add_match_sysname(e
, "loop*");
867 return log_error_errno(r
, "Failed to add sysname match: %m");
869 r
= sd_device_enumerator_add_match_sysattr(e
, "loop/backing_file", /* value = */ NULL
, /* match = */ true);
871 return log_error_errno(r
, "Failed to add sysattr match: %m");
873 FOREACH_DEVICE(e
, dev
) {
876 r
= sd_device_get_sysattr_value(dev
, "loop/backing_file", &s
);
878 log_device_debug_errno(dev
, r
, "Failed to read \"loop/backing_file\" sysattr, ignoring: %m");
882 if (inode_same(s
, backing_file
, 0) <= 0)
885 *ret
= sd_device_ref(dev
);
892 static int stop_mount(
895 const char *suffix
) {
897 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
, *reply
= NULL
;
898 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
899 _cleanup_(bus_wait_for_jobs_freep
) BusWaitForJobs
*w
= NULL
;
900 _cleanup_free_
char *mount_unit
= NULL
;
904 r
= bus_wait_for_jobs_new(bus
, &w
);
906 return log_error_errno(r
, "Could not watch jobs: %m");
909 r
= unit_name_from_path(where
, suffix
, &mount_unit
);
911 return log_error_errno(r
, "Failed to make %s unit name from path %s: %m", suffix
+ 1, where
);
913 r
= bus_message_new_method_call(bus
, &m
, bus_systemd_mgr
, "StopUnit");
915 return bus_log_create_error(r
);
917 r
= sd_bus_message_set_allow_interactive_authorization(m
, arg_ask_password
);
919 return bus_log_create_error(r
);
922 r
= sd_bus_message_append(m
, "ss", mount_unit
, "fail");
924 return bus_log_create_error(r
);
926 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
928 r
= sd_bus_call(bus
, m
, 0, &error
, &reply
);
930 if (streq(suffix
, ".automount") &&
931 sd_bus_error_has_name(&error
, "org.freedesktop.systemd1.NoSuchUnit"))
933 return log_error_errno(r
, "Failed to stop %s unit: %s", suffix
+ 1, bus_error_message(&error
, r
));
939 r
= sd_bus_message_read(reply
, "o", &object
);
941 return bus_log_parse_error(r
);
943 r
= bus_wait_for_jobs_one(w
, object
, arg_quiet
, NULL
);
949 log_info("Stopped unit %s%s%s for mount point: %s%s%s",
950 ansi_highlight(), mount_unit
, ansi_normal(),
951 ansi_highlight(), where
, ansi_normal());
956 static int stop_mounts(
962 if (path_equal(where
, "/"))
963 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
964 "Refusing to operate on root directory: %s", where
);
966 if (!path_is_normalized(where
))
967 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
968 "Path contains non-normalized components: %s", where
);
970 r
= stop_mount(bus
, where
, ".mount");
974 r
= stop_mount(bus
, where
, ".automount");
981 static int umount_by_device(sd_bus
*bus
, sd_device
*dev
) {
982 _cleanup_(sd_device_unrefp
) sd_device
*d
= NULL
;
983 _cleanup_strv_free_
char **list
= NULL
;
990 if (sd_device_get_property_value(d
, "SYSTEMD_MOUNT_WHERE", &v
) >= 0)
991 ret
= stop_mounts(bus
, v
);
993 r
= sd_device_get_devname(dev
, &v
);
997 r
= find_mount_points(v
, &list
);
1001 STRV_FOREACH(l
, list
) {
1002 r
= stop_mounts(bus
, *l
);
1010 static int umount_by_device_node(sd_bus
*bus
, const char *node
) {
1011 _cleanup_(sd_device_unrefp
) sd_device
*dev
= NULL
;
1018 r
= sd_device_new_from_devname(&dev
, node
);
1020 return log_error_errno(r
, "Failed to get device from %s: %m", node
);
1022 r
= sd_device_get_property_value(dev
, "ID_FS_USAGE", &v
);
1024 return log_device_error_errno(dev
, r
, "Failed to get \"ID_FS_USAGE\" device property: %m");
1026 if (!streq(v
, "filesystem"))
1027 return log_device_error_errno(dev
, SYNTHETIC_ERRNO(EINVAL
),
1028 "%s does not contain a known file system.", node
);
1030 return umount_by_device(bus
, dev
);
1033 static int umount_loop(sd_bus
*bus
, const char *backing_file
) {
1034 _cleanup_(sd_device_unrefp
) sd_device
*dev
= NULL
;
1037 assert(backing_file
);
1039 r
= find_loop_device(backing_file
, &dev
);
1041 return log_error_errno(r
, r
== -ENXIO
? "File %s is not mounted." : "Can't get loop device for %s: %m", backing_file
);
1043 return umount_by_device(bus
, dev
);
1046 static int action_umount(
1053 if (arg_transport
!= BUS_TRANSPORT_LOCAL
) {
1054 for (int i
= optind
; i
< argc
; i
++) {
1055 _cleanup_free_
char *p
= NULL
;
1057 p
= strdup(argv
[i
]);
1063 r
= stop_mounts(bus
, p
);
1070 for (int i
= optind
; i
< argc
; i
++) {
1071 _cleanup_free_
char *u
= NULL
, *p
= NULL
;
1074 u
= fstab_node_to_udev_node(argv
[i
]);
1078 r
= chase(u
, NULL
, 0, &p
, NULL
);
1080 r2
= log_error_errno(r
, "Failed to make path %s absolute: %m", argv
[i
]);
1084 if (stat(p
, &st
) < 0)
1085 return log_error_errno(errno
, "Can't stat %s (from %s): %m", p
, argv
[i
]);
1087 if (S_ISBLK(st
.st_mode
))
1088 r
= umount_by_device_node(bus
, p
);
1089 else if (S_ISREG(st
.st_mode
))
1090 r
= umount_loop(bus
, p
);
1091 else if (S_ISDIR(st
.st_mode
))
1092 r
= stop_mounts(bus
, p
);
1094 log_error("Invalid file type: %s (from %s)", p
, argv
[i
]);
1105 static int acquire_mount_type(sd_device
*d
) {
1113 if (sd_device_get_property_value(d
, "ID_FS_TYPE", &v
) < 0)
1116 arg_mount_type
= strdup(v
);
1117 if (!arg_mount_type
)
1120 log_debug("Discovered type=%s", arg_mount_type
);
1124 static int acquire_mount_options(sd_device
*d
) {
1129 if (arg_mount_options
)
1132 if (sd_device_get_property_value(d
, "SYSTEMD_MOUNT_OPTIONS", &v
) < 0)
1135 arg_mount_options
= strdup(v
);
1136 if (!arg_mount_options
)
1139 log_debug("Discovered options=%s", arg_mount_options
);
1143 static const char *get_model(sd_device
*d
) {
1148 if (sd_device_get_property_value(d
, "ID_MODEL_FROM_DATABASE", &model
) >= 0)
1151 if (sd_device_get_property_value(d
, "ID_MODEL", &model
) >= 0)
1157 static const char* get_label(sd_device
*d
) {
1162 if (sd_device_get_property_value(d
, "ID_FS_LABEL", &label
) >= 0)
1165 if (sd_device_get_property_value(d
, "ID_PART_ENTRY_NAME", &label
) >= 0)
1171 static int acquire_mount_where(sd_device
*d
) {
1175 if (arg_mount_where
)
1178 if (sd_device_get_property_value(d
, "SYSTEMD_MOUNT_WHERE", &v
) < 0) {
1179 _cleanup_free_
char *escaped
= NULL
, *devname_bn
= NULL
;
1182 name
= get_label(d
);
1184 name
= get_model(d
);
1188 if (sd_device_get_devname(d
, &dn
) < 0)
1191 r
= path_extract_filename(dn
, &devname_bn
);
1193 return log_error_errno(r
, "Failed to extract file name from '%s': %m", dn
);
1198 escaped
= xescape(name
, "\\");
1201 if (!filename_is_valid(escaped
))
1204 arg_mount_where
= path_join("/run/media/system", escaped
);
1206 arg_mount_where
= strdup(v
);
1208 if (!arg_mount_where
)
1211 log_debug("Discovered where=%s", arg_mount_where
);
1215 static int acquire_mount_where_for_loop_dev(sd_device
*dev
) {
1216 _cleanup_strv_free_
char **list
= NULL
;
1222 if (arg_mount_where
)
1225 r
= sd_device_get_devname(dev
, &node
);
1229 r
= find_mount_points(node
, &list
);
1233 return log_device_error_errno(dev
, SYNTHETIC_ERRNO(EINVAL
),
1234 "Can't find mount point of %s. It is expected that %s is already mounted on a place.",
1237 return log_device_error_errno(dev
, SYNTHETIC_ERRNO(EINVAL
),
1238 "%s is mounted on %d places. It is expected that %s is mounted on a place.",
1241 arg_mount_where
= strdup(list
[0]);
1242 if (!arg_mount_where
)
1245 log_debug("Discovered where=%s", arg_mount_where
);
1249 static int acquire_description(sd_device
*d
) {
1250 const char *model
, *label
;
1252 if (arg_description
)
1255 model
= get_model(d
);
1257 label
= get_label(d
);
1259 (void) sd_device_get_property_value(d
, "ID_PART_ENTRY_NUMBER", &label
);
1262 arg_description
= strjoin(model
, " ", label
);
1264 arg_description
= strdup(label
);
1266 arg_description
= strdup(model
);
1270 if (!arg_description
)
1273 log_debug("Discovered description=%s", arg_description
);
1277 static int acquire_removable(sd_device
*d
) {
1280 /* Shortcut this if there's no reason to check it */
1281 if (arg_action
!= ACTION_DEFAULT
&& arg_timeout_idle_set
&& arg_bind_device
>= 0)
1285 if (sd_device_get_sysattr_value(d
, "removable", &v
) >= 0)
1288 if (sd_device_get_parent(d
, &d
) < 0)
1291 if (sd_device_get_subsystem(d
, &v
) < 0 || !streq(v
, "block"))
1295 if (parse_boolean(v
) <= 0)
1298 log_debug("Discovered removable device.");
1300 if (arg_action
== ACTION_DEFAULT
) {
1301 log_debug("Automatically turning on automount.");
1302 arg_action
= ACTION_AUTOMOUNT
;
1305 if (!arg_timeout_idle_set
) {
1306 log_debug("Setting idle timeout to 1s.");
1307 arg_timeout_idle
= USEC_PER_SEC
;
1310 if (arg_bind_device
< 0) {
1311 log_debug("Binding automount unit to device.");
1312 arg_bind_device
= true;
1318 static int discover_loop_backing_file(void) {
1319 _cleanup_(sd_device_unrefp
) sd_device
*d
= NULL
;
1322 r
= find_loop_device(arg_mount_what
, &d
);
1323 if (r
< 0 && r
!= -ENXIO
)
1324 return log_error_errno(errno
, "Can't get loop device for %s: %m", arg_mount_what
);
1327 _cleanup_free_
char *escaped
= NULL
, *bn
= NULL
;
1329 if (arg_mount_where
)
1332 r
= path_extract_filename(arg_mount_what
, &bn
);
1334 return log_error_errno(r
, "Failed to extract file name from backing file path '%s': %m", arg_mount_what
);
1336 escaped
= xescape(bn
, "\\");
1339 if (!filename_is_valid(escaped
))
1340 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1341 "Escaped name %s is not a valid filename.",
1344 arg_mount_where
= path_join("/run/media/system", escaped
);
1345 if (!arg_mount_where
)
1348 log_debug("Discovered where=%s", arg_mount_where
);
1352 r
= acquire_mount_type(d
);
1356 r
= acquire_mount_options(d
);
1360 r
= acquire_mount_where_for_loop_dev(d
);
1364 r
= acquire_description(d
);
1371 static int discover_device(void) {
1372 _cleanup_(sd_device_unrefp
) sd_device
*d
= NULL
;
1377 if (stat(arg_mount_what
, &st
) < 0)
1378 return log_error_errno(errno
, "Can't stat %s: %m", arg_mount_what
);
1380 if (S_ISREG(st
.st_mode
))
1381 return discover_loop_backing_file();
1383 if (!S_ISBLK(st
.st_mode
))
1384 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1385 "Invalid file type: %s",
1388 r
= sd_device_new_from_stat_rdev(&d
, &st
);
1390 return log_error_errno(r
, "Failed to get device from device number: %m");
1392 if (sd_device_get_property_value(d
, "ID_FS_USAGE", &v
) < 0 || !streq(v
, "filesystem"))
1393 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1394 "%s does not contain a known file system.",
1397 r
= acquire_mount_type(d
);
1401 r
= acquire_mount_options(d
);
1405 r
= acquire_mount_where(d
);
1409 r
= acquire_description(d
);
1413 r
= acquire_removable(d
);
1431 static int list_devices(void) {
1432 _cleanup_(sd_device_enumerator_unrefp
) sd_device_enumerator
*e
= NULL
;
1433 _cleanup_(table_unrefp
) Table
*table
= NULL
;
1436 r
= sd_device_enumerator_new(&e
);
1440 r
= sd_device_enumerator_add_match_subsystem(e
, "block", true);
1442 return log_error_errno(r
, "Failed to add block match: %m");
1444 r
= sd_device_enumerator_add_match_property(e
, "ID_FS_USAGE", "filesystem");
1446 return log_error_errno(r
, "Failed to add property match: %m");
1448 table
= table_new("NODE", "PATH", "MODEL", "WWN", "TYPE", "LABEL", "UUID");
1453 table_set_width(table
, 0);
1455 r
= table_set_sort(table
, (size_t) 0);
1457 return log_error_errno(r
, "Failed to set sort index: %m");
1459 table_set_header(table
, arg_legend
);
1461 FOREACH_DEVICE(e
, d
) {
1462 for (unsigned c
= 0; c
< _COLUMN_MAX
; c
++) {
1463 const char *x
= NULL
;
1468 (void) sd_device_get_devname(d
, &x
);
1472 (void) sd_device_get_property_value(d
, "ID_PATH", &x
);
1480 (void) sd_device_get_property_value(d
, "ID_WWN", &x
);
1484 (void) sd_device_get_property_value(d
, "ID_FS_TYPE", &x
);
1492 (void) sd_device_get_property_value(d
, "ID_FS_UUID", &x
);
1496 r
= table_add_cell(table
, NULL
, c
== COLUMN_NODE
? TABLE_PATH
: TABLE_STRING
, strna(x
));
1498 return table_log_add_error(r
);
1502 pager_open(arg_pager_flags
);
1504 r
= table_print(table
, NULL
);
1506 return table_log_print_error(r
);
1511 static int run(int argc
, char* argv
[]) {
1512 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1515 log_show_color(true);
1516 log_parse_environment();
1519 r
= parse_argv(argc
, argv
);
1523 if (arg_action
== ACTION_LIST
)
1524 return list_devices();
1526 r
= bus_connect_transport_systemd(arg_transport
, arg_host
, arg_runtime_scope
, &bus
);
1528 return bus_log_connect_error(r
, arg_transport
);
1530 if (arg_action
== ACTION_UMOUNT
)
1531 return action_umount(bus
, argc
, argv
);
1533 if ((!arg_mount_type
|| fstype_is_blockdev_backed(arg_mount_type
))
1534 && !path_is_normalized(arg_mount_what
))
1535 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1536 "Path contains non-normalized components: %s",
1540 r
= discover_device();
1545 if (!arg_mount_where
)
1546 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1547 "Can't figure out where to mount %s.",
1550 if (path_equal(arg_mount_where
, "/"))
1551 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1552 "Refusing to operate on root directory.");
1554 if (!path_is_normalized(arg_mount_where
))
1555 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1556 "Path contains non-normalized components: %s",
1559 if (streq_ptr(arg_mount_type
, "auto"))
1560 arg_mount_type
= mfree(arg_mount_type
);
1561 if (streq_ptr(arg_mount_options
, "defaults"))
1562 arg_mount_options
= mfree(arg_mount_options
);
1564 if (!is_device_path(arg_mount_what
))
1567 if (arg_fsck
&& arg_mount_type
&& arg_transport
== BUS_TRANSPORT_LOCAL
) {
1568 r
= fsck_exists_for_fstype(arg_mount_type
);
1570 log_warning_errno(r
, "Couldn't determine whether fsck for %s exists, proceeding anyway.", arg_mount_type
);
1572 log_debug("Disabling file system check as fsck for %s doesn't exist.", arg_mount_type
);
1573 arg_fsck
= false; /* fsck doesn't exist, let's not attempt it */
1577 /* The kernel (properly) refuses mounting file systems with unknown uid=,gid= options,
1578 * but not for all filesystem types. Let's try to catch the cases where the option
1579 * would be used if the file system does not support it. It is also possible to
1580 * autodetect the file system, but that's only possible with disk-based file systems
1581 * which incidentally seem to be implemented more carefully and reject unknown options,
1582 * so it's probably OK that we do the check only when the type is specified.
1584 if (arg_mount_type
&&
1585 !streq(arg_mount_type
, "auto") &&
1586 arg_uid
!= UID_INVALID
&&
1587 !fstype_can_uid_gid(arg_mount_type
))
1588 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP
),
1589 "File system type %s is not known to support uid=/gid=, refusing.",
1592 switch (arg_action
) {
1595 case ACTION_DEFAULT
:
1596 r
= start_transient_mount(bus
, argv
+ optind
);
1599 case ACTION_AUTOMOUNT
:
1600 r
= start_transient_automount(bus
, argv
+ optind
);
1604 assert_not_reached();
1610 DEFINE_MAIN_FUNCTION(run
);