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
) {
91 if (arg_transport
== BUS_TRANSPORT_LOCAL
) {
92 r
= chase(input
, NULL
, CHASE_NONEXISTENT
, ret_where
, NULL
);
94 return log_error_errno(r
, "Failed to make path %s absolute: %m", input
);
96 if (!path_is_absolute(input
))
97 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
98 "Path must be absolute when operating remotely: %s",
101 r
= path_simplify_alloc(input
, ret_where
);
103 return log_error_errno(r
, "Failed to simplify path %s: %m", input
);
109 static int help(void) {
110 _cleanup_free_
char *link
= NULL
;
113 r
= terminal_urlify_man("systemd-mount", "1", &link
);
117 printf("systemd-mount [OPTIONS...] WHAT [WHERE]\n"
118 "systemd-mount [OPTIONS...] --tmpfs [NAME] WHERE\n"
119 "systemd-mount [OPTIONS...] --list\n"
120 "%s [OPTIONS...] %sWHAT|WHERE...\n\n"
121 "Establish a mount or auto-mount point transiently.\n\n"
122 " -h --help Show this help\n"
123 " --version Show package version\n"
124 " --no-block Do not wait until operation finished\n"
125 " --no-pager Do not pipe output into a pager\n"
126 " --no-legend Do not show the headers\n"
127 " -l --full Do not ellipsize output\n"
128 " --no-ask-password Do not prompt for password\n"
129 " -q --quiet Suppress information messages during runtime\n"
130 " --user Run as user unit\n"
131 " -H --host=[USER@]HOST Operate on remote host\n"
132 " -M --machine=CONTAINER Operate on local container\n"
133 " --discover Discover mount device metadata\n"
134 " -t --type=TYPE File system type\n"
135 " -o --options=OPTIONS Mount options\n"
136 " --owner=USER Add uid= and gid= options for USER\n"
137 " --fsck=no Don't run file system check before mount\n"
138 " --description=TEXT Description for unit\n"
139 " -p --property=NAME=VALUE Set mount unit property\n"
140 " -A --automount=BOOL Create an auto-mount point\n"
141 " --timeout-idle-sec=SEC Specify automount idle timeout\n"
142 " --automount-property=NAME=VALUE\n"
143 " Set automount unit property\n"
144 " --bind-device Bind automount unit to device\n"
145 " --list List mountable block devices\n"
146 " -u --umount Unmount mount points\n"
147 " -G --collect Unload unit after it stopped, even when failed\n"
148 " -T --tmpfs Create a new tmpfs on the mount point\n"
149 "\nSee the %s for details.\n",
150 program_invocation_short_name
,
151 streq(program_invocation_short_name
, "systemd-umount") ? "" : "--umount ",
157 static int parse_argv(int argc
, char *argv
[]) {
175 ARG_AUTOMOUNT_PROPERTY
,
180 static const struct option options
[] = {
181 { "help", no_argument
, NULL
, 'h' },
182 { "version", no_argument
, NULL
, ARG_VERSION
},
183 { "no-block", no_argument
, NULL
, ARG_NO_BLOCK
},
184 { "no-pager", no_argument
, NULL
, ARG_NO_PAGER
},
185 { "no-legend", no_argument
, NULL
, ARG_NO_LEGEND
},
186 { "full", no_argument
, NULL
, 'l' },
187 { "no-ask-password", no_argument
, NULL
, ARG_NO_ASK_PASSWORD
},
188 { "quiet", no_argument
, NULL
, 'q' },
189 { "user", no_argument
, NULL
, ARG_USER
},
190 { "system", no_argument
, NULL
, ARG_SYSTEM
},
191 { "host", required_argument
, NULL
, 'H' },
192 { "machine", required_argument
, NULL
, 'M' },
193 { "discover", no_argument
, NULL
, ARG_DISCOVER
},
194 { "type", required_argument
, NULL
, 't' },
195 { "options", required_argument
, NULL
, 'o' },
196 { "owner", required_argument
, NULL
, ARG_OWNER
},
197 { "fsck", required_argument
, NULL
, ARG_FSCK
},
198 { "description", required_argument
, NULL
, ARG_DESCRIPTION
},
199 { "property", required_argument
, NULL
, 'p' },
200 { "automount", required_argument
, NULL
, ARG_AUTOMOUNT
},
201 { "timeout-idle-sec", required_argument
, NULL
, ARG_TIMEOUT_IDLE
},
202 { "automount-property", required_argument
, NULL
, ARG_AUTOMOUNT_PROPERTY
},
203 { "bind-device", no_argument
, NULL
, ARG_BIND_DEVICE
},
204 { "list", no_argument
, NULL
, ARG_LIST
},
205 { "umount", no_argument
, NULL
, 'u' },
206 { "unmount", no_argument
, NULL
, 'u' }, /* Compat spelling */
207 { "collect", no_argument
, NULL
, 'G' },
208 { "tmpfs", no_argument
, NULL
, 'T' },
217 if (invoked_as(argv
, "systemd-umount"))
218 arg_action
= ACTION_UMOUNT
;
220 while ((c
= getopt_long(argc
, argv
, "hqH:M:t:o:p:AuGlT", options
, NULL
)) >= 0)
235 arg_pager_flags
|= PAGER_DISABLE
;
246 case ARG_NO_ASK_PASSWORD
:
247 arg_ask_password
= false;
255 arg_runtime_scope
= RUNTIME_SCOPE_USER
;
259 arg_runtime_scope
= RUNTIME_SCOPE_SYSTEM
;
263 arg_transport
= BUS_TRANSPORT_REMOTE
;
268 arg_transport
= BUS_TRANSPORT_MACHINE
;
277 r
= free_and_strdup_warn(&arg_mount_type
, optarg
);
283 r
= free_and_strdup_warn(&arg_mount_options
, optarg
);
289 const char *user
= optarg
;
291 r
= get_user_creds(&user
, &arg_uid
, &arg_gid
, NULL
, NULL
, 0);
293 return log_error_errno(r
,
294 r
== -EBADMSG
? "UID or GID of user %s are invalid."
295 : "Cannot use \"%s\" as owner: %m",
301 r
= parse_boolean_argument("--fsck=", optarg
, &arg_fsck
);
306 case ARG_DESCRIPTION
:
307 r
= free_and_strdup_warn(&arg_description
, optarg
);
313 if (strv_extend(&arg_property
, optarg
) < 0)
319 arg_action
= ACTION_AUTOMOUNT
;
323 r
= parse_boolean_argument("--automount=", optarg
, NULL
);
327 arg_action
= r
? ACTION_AUTOMOUNT
: ACTION_MOUNT
;
330 case ARG_TIMEOUT_IDLE
:
331 r
= parse_sec(optarg
, &arg_timeout_idle
);
333 return log_error_errno(r
, "Failed to parse timeout: %s", optarg
);
337 case ARG_AUTOMOUNT_PROPERTY
:
338 if (strv_extend(&arg_automount_property
, optarg
) < 0)
343 case ARG_BIND_DEVICE
:
344 arg_bind_device
= true;
348 arg_action
= ACTION_LIST
;
352 arg_action
= ACTION_UMOUNT
;
356 arg_aggressive_gc
= true;
367 assert_not_reached();
370 if (arg_runtime_scope
== RUNTIME_SCOPE_USER
) {
371 arg_ask_password
= false;
373 if (arg_transport
!= BUS_TRANSPORT_LOCAL
)
374 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
375 "Execution in user context is not supported on non-local systems.");
378 if (arg_action
== ACTION_LIST
) {
380 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
381 "Too many arguments.");
383 if (arg_transport
!= BUS_TRANSPORT_LOCAL
)
384 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP
),
385 "Listing devices only supported locally.");
386 } else if (arg_action
== ACTION_UMOUNT
) {
388 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
389 "At least one argument required.");
391 if (arg_transport
!= BUS_TRANSPORT_LOCAL
)
392 for (int i
= optind
; i
< argc
; i
++)
393 if (!path_is_absolute(argv
[i
]))
394 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
395 "Path must be absolute when operating remotely: %s",
399 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
400 "At least one argument required.");
403 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
404 "More than two arguments are not allowed.");
407 if (argc
<= optind
+1) {
408 arg_mount_what
= strdup("tmpfs");
412 r
= parse_where(argv
[optind
], &arg_mount_where
);
416 arg_mount_what
= strdup(argv
[optind
]);
421 if (!strv_contains(arg_property
, "Type=tmpfs") &&
422 strv_extend(&arg_property
, "Type=tmpfs") < 0)
425 if (arg_mount_type
&& !fstype_is_blockdev_backed(arg_mount_type
)) {
426 arg_mount_what
= strdup(argv
[optind
]);
430 } else if (arg_transport
== BUS_TRANSPORT_LOCAL
) {
431 _cleanup_free_
char *u
= NULL
;
433 u
= fstab_node_to_udev_node(argv
[optind
]);
437 r
= chase(u
, NULL
, 0, &arg_mount_what
, NULL
);
439 return log_error_errno(r
, "Failed to make path %s absolute: %m", u
);
442 if (!path_is_absolute(argv
[optind
]))
443 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
444 "Path must be absolute when operating remotely: %s",
447 r
= path_simplify_alloc(argv
[optind
], &arg_mount_what
);
449 return log_error_errno(r
, "Failed to simplify path: %m");
453 if (argc
> optind
+1) {
454 r
= parse_where(argv
[optind
+1], &arg_mount_where
);
457 } else if (!arg_tmpfs
)
460 if (arg_discover
&& arg_transport
!= BUS_TRANSPORT_LOCAL
)
461 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP
),
462 "Automatic mount location discovery is only supported locally.");
468 static int transient_unit_set_properties(sd_bus_message
*m
, UnitType t
, char **properties
) {
471 if (!isempty(arg_description
)) {
472 r
= sd_bus_message_append(m
, "(sv)", "Description", "s", arg_description
);
477 if (arg_bind_device
&& is_device_path(arg_mount_what
)) {
478 _cleanup_free_
char *device_unit
= NULL
;
480 r
= unit_name_from_path(arg_mount_what
, ".device", &device_unit
);
484 r
= sd_bus_message_append(m
, "(sv)(sv)",
485 "After", "as", 1, device_unit
,
486 "BindsTo", "as", 1, device_unit
);
491 if (arg_aggressive_gc
) {
492 r
= sd_bus_message_append(m
, "(sv)", "CollectMode", "s", "inactive-or-failed");
497 r
= bus_append_unit_property_assignment_many(m
, t
, properties
);
504 static int transient_mount_set_properties(sd_bus_message
*m
) {
509 r
= transient_unit_set_properties(m
, UNIT_MOUNT
, arg_property
);
513 if (arg_mount_what
) {
514 r
= sd_bus_message_append(m
, "(sv)", "What", "s", arg_mount_what
);
519 if (arg_mount_type
) {
520 r
= sd_bus_message_append(m
, "(sv)", "Type", "s", arg_mount_type
);
525 _cleanup_free_
char *options
= NULL
;
527 /* Prepend uid=…,gid=… if arg_uid is set */
528 if (arg_uid
!= UID_INVALID
) {
529 r
= strextendf_with_separator(&options
, ",",
530 "uid="UID_FMT
",gid="GID_FMT
, arg_uid
, arg_gid
);
535 /* Override the default for tmpfs mounts. The kernel sets the sticky bit on the root directory by
536 * default. This makes sense for the case when the user does 'mount -t tmpfs tmpfs /tmp', but less so
537 * for other directories.
539 * Let's also set some reasonable limits. We use the current umask, to match what a command to create
540 * directory would use, e.g. mkdir. */
544 r
= get_process_umask(0, &mask
);
548 assert((mask
& ~0777) == 0);
549 r
= strextendf_with_separator(&options
, ",",
550 "mode=0%o,nodev,nosuid%s", 0777 & ~mask
, NESTED_TMPFS_LIMITS
);
555 if (arg_mount_options
)
556 if (!strextend_with_separator(&options
, ",", arg_mount_options
))
560 log_debug("Using mount options: %s", options
);
561 r
= sd_bus_message_append(m
, "(sv)", "Options", "s", options
);
565 log_debug("Not using any mount options");
568 _cleanup_free_
char *fsck
= NULL
;
570 r
= unit_name_from_path_instance("systemd-fsck", arg_mount_what
, ".service", &fsck
);
574 r
= sd_bus_message_append(m
,
576 "Requires", "as", 1, fsck
,
577 "After", "as", 1, fsck
);
585 static int transient_automount_set_properties(sd_bus_message
*m
) {
590 r
= transient_unit_set_properties(m
, UNIT_AUTOMOUNT
, arg_automount_property
);
594 if (arg_timeout_idle
!= USEC_INFINITY
) {
595 r
= sd_bus_message_append(m
, "(sv)", "TimeoutIdleUSec", "t", arg_timeout_idle
);
603 static int start_transient_mount(
607 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
, *reply
= NULL
;
608 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
609 _cleanup_(bus_wait_for_jobs_freep
) BusWaitForJobs
*w
= NULL
;
610 _cleanup_free_
char *mount_unit
= NULL
;
614 r
= bus_wait_for_jobs_new(bus
, &w
);
616 return log_error_errno(r
, "Could not watch jobs: %m");
619 r
= unit_name_from_path(arg_mount_where
, ".mount", &mount_unit
);
621 return log_error_errno(r
, "Failed to make mount unit name: %m");
623 r
= bus_message_new_method_call(bus
, &m
, bus_systemd_mgr
, "StartTransientUnit");
625 return bus_log_create_error(r
);
627 r
= sd_bus_message_set_allow_interactive_authorization(m
, arg_ask_password
);
629 return bus_log_create_error(r
);
632 r
= sd_bus_message_append(m
, "ss", mount_unit
, "fail");
634 return bus_log_create_error(r
);
637 r
= sd_bus_message_open_container(m
, 'a', "(sv)");
639 return bus_log_create_error(r
);
641 r
= transient_mount_set_properties(m
);
643 return bus_log_create_error(r
);
645 r
= sd_bus_message_close_container(m
);
647 return bus_log_create_error(r
);
649 /* Auxiliary units */
650 r
= sd_bus_message_append(m
, "a(sa(sv))", 0);
652 return bus_log_create_error(r
);
654 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
656 r
= sd_bus_call(bus
, m
, 0, &error
, &reply
);
658 return log_error_errno(r
, "Failed to start transient mount unit: %s", bus_error_message(&error
, r
));
663 r
= sd_bus_message_read(reply
, "o", &object
);
665 return bus_log_parse_error(r
);
667 r
= bus_wait_for_jobs_one(w
, object
, arg_quiet
, NULL
);
673 log_info("Started unit %s%s%s for mount point: %s%s%s",
674 ansi_highlight(), mount_unit
, ansi_normal(),
675 ansi_highlight(), arg_mount_where
, ansi_normal());
680 static int start_transient_automount(
684 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
, *reply
= NULL
;
685 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
686 _cleanup_(bus_wait_for_jobs_freep
) BusWaitForJobs
*w
= NULL
;
687 _cleanup_free_
char *automount_unit
= NULL
, *mount_unit
= NULL
;
691 r
= bus_wait_for_jobs_new(bus
, &w
);
693 return log_error_errno(r
, "Could not watch jobs: %m");
696 r
= unit_name_from_path(arg_mount_where
, ".automount", &automount_unit
);
698 return log_error_errno(r
, "Failed to make automount unit name: %m");
700 r
= unit_name_from_path(arg_mount_where
, ".mount", &mount_unit
);
702 return log_error_errno(r
, "Failed to make mount unit name: %m");
704 r
= bus_message_new_method_call(bus
, &m
, bus_systemd_mgr
, "StartTransientUnit");
706 return bus_log_create_error(r
);
708 r
= sd_bus_message_set_allow_interactive_authorization(m
, arg_ask_password
);
710 return bus_log_create_error(r
);
713 r
= sd_bus_message_append(m
, "ss", automount_unit
, "fail");
715 return bus_log_create_error(r
);
718 r
= sd_bus_message_open_container(m
, 'a', "(sv)");
720 return bus_log_create_error(r
);
722 r
= transient_automount_set_properties(m
);
724 return bus_log_create_error(r
);
726 r
= sd_bus_message_close_container(m
);
728 return bus_log_create_error(r
);
730 /* Auxiliary units */
731 r
= sd_bus_message_open_container(m
, 'a', "(sa(sv))");
733 return bus_log_create_error(r
);
735 r
= sd_bus_message_open_container(m
, 'r', "sa(sv)");
737 return bus_log_create_error(r
);
739 r
= sd_bus_message_append(m
, "s", mount_unit
);
741 return bus_log_create_error(r
);
743 r
= sd_bus_message_open_container(m
, 'a', "(sv)");
745 return bus_log_create_error(r
);
747 r
= transient_mount_set_properties(m
);
749 return bus_log_create_error(r
);
751 r
= sd_bus_message_close_container(m
);
753 return bus_log_create_error(r
);
755 r
= sd_bus_message_close_container(m
);
757 return bus_log_create_error(r
);
759 r
= sd_bus_message_close_container(m
);
761 return bus_log_create_error(r
);
763 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
765 r
= sd_bus_call(bus
, m
, 0, &error
, &reply
);
767 return log_error_errno(r
, "Failed to start transient automount unit: %s", bus_error_message(&error
, r
));
772 r
= sd_bus_message_read(reply
, "o", &object
);
774 return bus_log_parse_error(r
);
776 r
= bus_wait_for_jobs_one(w
, object
, arg_quiet
, NULL
);
782 log_info("Started unit %s%s%s for mount point: %s%s%s",
783 ansi_highlight(), automount_unit
, ansi_normal(),
784 ansi_highlight(), arg_mount_where
, ansi_normal());
789 static int find_mount_points(const char *what
, char ***list
) {
790 _cleanup_(mnt_free_tablep
) struct libmnt_table
*table
= NULL
;
791 _cleanup_(mnt_free_iterp
) struct libmnt_iter
*iter
= NULL
;
792 _cleanup_strv_free_
char **l
= NULL
;
799 /* Returns all mount points obtained from /proc/self/mountinfo in *list,
800 * and the number of mount points as return value. */
802 r
= libmount_parse(NULL
, NULL
, &table
, &iter
);
804 return log_error_errno(r
, "Failed to parse /proc/self/mountinfo: %m");
807 struct libmnt_fs
*fs
;
808 const char *source
, *target
;
810 r
= mnt_table_next_fs(table
, iter
, &fs
);
814 return log_error_errno(r
, "Failed to get next entry from /proc/self/mountinfo: %m");
816 source
= mnt_fs_get_source(fs
);
817 target
= mnt_fs_get_target(fs
);
818 if (!source
|| !target
)
821 if (!path_equal(source
, what
))
824 /* one extra slot is needed for the terminating NULL */
825 if (!GREEDY_REALLOC0(l
, n
+ 2))
828 l
[n
] = strdup(target
);
834 if (!GREEDY_REALLOC0(l
, n
+ 1))
841 static int find_loop_device(const char *backing_file
, sd_device
**ret
) {
842 _cleanup_(sd_device_enumerator_unrefp
) sd_device_enumerator
*e
= NULL
;
845 assert(backing_file
);
848 r
= sd_device_enumerator_new(&e
);
852 r
= sd_device_enumerator_add_match_subsystem(e
, "block", /* match = */ true);
854 return log_error_errno(r
, "Failed to add subsystem match: %m");
856 r
= sd_device_enumerator_add_match_property(e
, "ID_FS_USAGE", "filesystem");
858 return log_error_errno(r
, "Failed to add property match: %m");
860 r
= sd_device_enumerator_add_match_sysname(e
, "loop*");
862 return log_error_errno(r
, "Failed to add sysname match: %m");
864 r
= sd_device_enumerator_add_match_sysattr(e
, "loop/backing_file", /* value = */ NULL
, /* match = */ true);
866 return log_error_errno(r
, "Failed to add sysattr match: %m");
868 FOREACH_DEVICE(e
, dev
) {
871 r
= sd_device_get_sysattr_value(dev
, "loop/backing_file", &s
);
873 log_device_debug_errno(dev
, r
, "Failed to read \"loop/backing_file\" sysattr, ignoring: %m");
877 if (inode_same(s
, backing_file
, 0) <= 0)
880 *ret
= sd_device_ref(dev
);
887 static int stop_mount(
890 const char *suffix
) {
892 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
, *reply
= NULL
;
893 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
894 _cleanup_(bus_wait_for_jobs_freep
) BusWaitForJobs
*w
= NULL
;
895 _cleanup_free_
char *mount_unit
= NULL
;
899 r
= bus_wait_for_jobs_new(bus
, &w
);
901 return log_error_errno(r
, "Could not watch jobs: %m");
904 r
= unit_name_from_path(where
, suffix
, &mount_unit
);
906 return log_error_errno(r
, "Failed to make %s unit name from path %s: %m", suffix
+ 1, where
);
908 r
= bus_message_new_method_call(bus
, &m
, bus_systemd_mgr
, "StopUnit");
910 return bus_log_create_error(r
);
912 r
= sd_bus_message_set_allow_interactive_authorization(m
, arg_ask_password
);
914 return bus_log_create_error(r
);
917 r
= sd_bus_message_append(m
, "ss", mount_unit
, "fail");
919 return bus_log_create_error(r
);
921 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
923 r
= sd_bus_call(bus
, m
, 0, &error
, &reply
);
925 if (streq(suffix
, ".automount") &&
926 sd_bus_error_has_name(&error
, "org.freedesktop.systemd1.NoSuchUnit"))
928 return log_error_errno(r
, "Failed to stop %s unit: %s", suffix
+ 1, bus_error_message(&error
, r
));
934 r
= sd_bus_message_read(reply
, "o", &object
);
936 return bus_log_parse_error(r
);
938 r
= bus_wait_for_jobs_one(w
, object
, arg_quiet
, NULL
);
944 log_info("Stopped unit %s%s%s for mount point: %s%s%s",
945 ansi_highlight(), mount_unit
, ansi_normal(),
946 ansi_highlight(), where
, ansi_normal());
951 static int stop_mounts(
957 if (path_equal(where
, "/"))
958 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
959 "Refusing to operate on root directory: %s", where
);
961 if (!path_is_normalized(where
))
962 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
963 "Path contains non-normalized components: %s", where
);
965 r
= stop_mount(bus
, where
, ".mount");
969 r
= stop_mount(bus
, where
, ".automount");
976 static int umount_by_device(sd_bus
*bus
, sd_device
*dev
) {
977 _cleanup_(sd_device_unrefp
) sd_device
*d
= NULL
;
978 _cleanup_strv_free_
char **list
= NULL
;
985 if (sd_device_get_property_value(d
, "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_model(sd_device
*d
) {
1141 if (sd_device_get_property_value(d
, "ID_MODEL_FROM_DATABASE", &model
) >= 0)
1144 if (sd_device_get_property_value(d
, "ID_MODEL", &model
) >= 0)
1150 static const char* get_label(sd_device
*d
) {
1155 if (sd_device_get_property_value(d
, "ID_FS_LABEL", &label
) >= 0)
1158 if (sd_device_get_property_value(d
, "ID_PART_ENTRY_NAME", &label
) >= 0)
1164 static int acquire_mount_where(sd_device
*d
) {
1168 if (arg_mount_where
)
1171 if (sd_device_get_property_value(d
, "SYSTEMD_MOUNT_WHERE", &v
) < 0) {
1172 _cleanup_free_
char *escaped
= NULL
, *devname_bn
= NULL
;
1175 name
= get_label(d
);
1177 name
= get_model(d
);
1181 if (sd_device_get_devname(d
, &dn
) < 0)
1184 r
= path_extract_filename(dn
, &devname_bn
);
1186 return log_error_errno(r
, "Failed to extract file name from '%s': %m", dn
);
1191 escaped
= xescape(name
, "\\");
1194 if (!filename_is_valid(escaped
))
1197 arg_mount_where
= path_join("/run/media/system", escaped
);
1199 arg_mount_where
= strdup(v
);
1201 if (!arg_mount_where
)
1204 log_debug("Discovered where=%s", arg_mount_where
);
1208 static int acquire_mount_where_for_loop_dev(sd_device
*dev
) {
1209 _cleanup_strv_free_
char **list
= NULL
;
1215 if (arg_mount_where
)
1218 r
= sd_device_get_devname(dev
, &node
);
1222 r
= find_mount_points(node
, &list
);
1226 return log_device_error_errno(dev
, SYNTHETIC_ERRNO(EINVAL
),
1227 "Can't find mount point of %s. It is expected that %s is already mounted on a place.",
1230 return log_device_error_errno(dev
, SYNTHETIC_ERRNO(EINVAL
),
1231 "%s is mounted on %d places. It is expected that %s is mounted on a place.",
1234 arg_mount_where
= strdup(list
[0]);
1235 if (!arg_mount_where
)
1238 log_debug("Discovered where=%s", arg_mount_where
);
1242 static int acquire_description(sd_device
*d
) {
1243 const char *model
, *label
;
1245 if (arg_description
)
1248 model
= get_model(d
);
1250 label
= get_label(d
);
1252 (void) sd_device_get_property_value(d
, "ID_PART_ENTRY_NUMBER", &label
);
1255 arg_description
= strjoin(model
, " ", label
);
1257 arg_description
= strdup(label
);
1259 arg_description
= strdup(model
);
1263 if (!arg_description
)
1266 log_debug("Discovered description=%s", arg_description
);
1270 static int acquire_removable(sd_device
*d
) {
1273 /* Shortcut this if there's no reason to check it */
1274 if (arg_action
!= ACTION_DEFAULT
&& arg_timeout_idle_set
&& arg_bind_device
>= 0)
1278 if (sd_device_get_sysattr_value(d
, "removable", &v
) >= 0)
1281 if (sd_device_get_parent(d
, &d
) < 0)
1284 if (sd_device_get_subsystem(d
, &v
) < 0 || !streq(v
, "block"))
1288 if (parse_boolean(v
) <= 0)
1291 log_debug("Discovered removable device.");
1293 if (arg_action
== ACTION_DEFAULT
) {
1294 log_debug("Automatically turning on automount.");
1295 arg_action
= ACTION_AUTOMOUNT
;
1298 if (!arg_timeout_idle_set
) {
1299 log_debug("Setting idle timeout to 1s.");
1300 arg_timeout_idle
= USEC_PER_SEC
;
1303 if (arg_bind_device
< 0) {
1304 log_debug("Binding automount unit to device.");
1305 arg_bind_device
= true;
1311 static int discover_loop_backing_file(void) {
1312 _cleanup_(sd_device_unrefp
) sd_device
*d
= NULL
;
1315 r
= find_loop_device(arg_mount_what
, &d
);
1316 if (r
< 0 && r
!= -ENXIO
)
1317 return log_error_errno(errno
, "Can't get loop device for %s: %m", arg_mount_what
);
1320 _cleanup_free_
char *escaped
= NULL
, *bn
= NULL
;
1322 if (arg_mount_where
)
1325 r
= path_extract_filename(arg_mount_what
, &bn
);
1327 return log_error_errno(r
, "Failed to extract file name from backing file path '%s': %m", arg_mount_what
);
1329 escaped
= xescape(bn
, "\\");
1332 if (!filename_is_valid(escaped
))
1333 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1334 "Escaped name %s is not a valid filename.",
1337 arg_mount_where
= path_join("/run/media/system", escaped
);
1338 if (!arg_mount_where
)
1341 log_debug("Discovered where=%s", arg_mount_where
);
1345 r
= acquire_mount_type(d
);
1349 r
= acquire_mount_options(d
);
1353 r
= acquire_mount_where_for_loop_dev(d
);
1357 r
= acquire_description(d
);
1364 static int discover_device(void) {
1365 _cleanup_(sd_device_unrefp
) sd_device
*d
= NULL
;
1370 if (stat(arg_mount_what
, &st
) < 0)
1371 return log_error_errno(errno
, "Can't stat %s: %m", arg_mount_what
);
1373 if (S_ISREG(st
.st_mode
))
1374 return discover_loop_backing_file();
1376 if (!S_ISBLK(st
.st_mode
))
1377 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1378 "Invalid file type: %s",
1381 r
= sd_device_new_from_stat_rdev(&d
, &st
);
1383 return log_error_errno(r
, "Failed to get device from device number: %m");
1385 if (sd_device_get_property_value(d
, "ID_FS_USAGE", &v
) < 0 || !streq(v
, "filesystem"))
1386 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1387 "%s does not contain a known file system.",
1390 r
= acquire_mount_type(d
);
1394 r
= acquire_mount_options(d
);
1398 r
= acquire_mount_where(d
);
1402 r
= acquire_description(d
);
1406 r
= acquire_removable(d
);
1413 static int list_devices(void) {
1425 _cleanup_(sd_device_enumerator_unrefp
) sd_device_enumerator
*e
= NULL
;
1426 _cleanup_(table_unrefp
) Table
*table
= NULL
;
1429 r
= sd_device_enumerator_new(&e
);
1433 r
= sd_device_enumerator_add_match_subsystem(e
, "block", true);
1435 return log_error_errno(r
, "Failed to add block match: %m");
1437 r
= sd_device_enumerator_add_match_property(e
, "ID_FS_USAGE", "filesystem");
1439 return log_error_errno(r
, "Failed to add property match: %m");
1441 table
= table_new("NODE", "PATH", "MODEL", "WWN", "FSTYPE", "LABEL", "UUID");
1446 table_set_width(table
, 0);
1448 r
= table_set_sort(table
, (size_t) 0);
1450 return log_error_errno(r
, "Failed to set sort index: %m");
1452 table_set_header(table
, arg_legend
);
1453 table_set_ersatz_string(table
, TABLE_ERSATZ_DASH
);
1455 FOREACH_DEVICE(e
, d
) {
1456 for (unsigned c
= 0; c
< _COLUMN_MAX
; c
++) {
1457 const char *x
= NULL
;
1462 (void) sd_device_get_devname(d
, &x
);
1466 (void) sd_device_get_property_value(d
, "ID_PATH", &x
);
1474 (void) sd_device_get_property_value(d
, "ID_WWN", &x
);
1478 (void) sd_device_get_property_value(d
, "ID_FS_TYPE", &x
);
1486 (void) sd_device_get_property_value(d
, "ID_FS_UUID", &x
);
1490 r
= table_add_cell(table
, NULL
, c
== COLUMN_NODE
? TABLE_PATH
: TABLE_STRING
, x
);
1492 return table_log_add_error(r
);
1496 pager_open(arg_pager_flags
);
1498 r
= table_print(table
, NULL
);
1500 return table_log_print_error(r
);
1505 static int run(int argc
, char* argv
[]) {
1506 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1509 log_show_color(true);
1510 log_parse_environment();
1513 r
= parse_argv(argc
, argv
);
1517 if (arg_action
== ACTION_LIST
)
1518 return list_devices();
1520 r
= bus_connect_transport_systemd(arg_transport
, arg_host
, arg_runtime_scope
, &bus
);
1522 return bus_log_connect_error(r
, arg_transport
);
1524 if (arg_action
== ACTION_UMOUNT
)
1525 return action_umount(bus
, argc
, argv
);
1527 if ((!arg_mount_type
|| fstype_is_blockdev_backed(arg_mount_type
))
1528 && !path_is_normalized(arg_mount_what
))
1529 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1530 "Path contains non-normalized components: %s",
1534 r
= discover_device();
1539 if (!arg_mount_where
)
1540 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1541 "Can't figure out where to mount %s.",
1544 if (path_equal(arg_mount_where
, "/"))
1545 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1546 "Refusing to operate on root directory.");
1548 if (!path_is_normalized(arg_mount_where
))
1549 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1550 "Path contains non-normalized components: %s",
1553 if (streq_ptr(arg_mount_type
, "auto"))
1554 arg_mount_type
= mfree(arg_mount_type
);
1555 if (streq_ptr(arg_mount_options
, "defaults"))
1556 arg_mount_options
= mfree(arg_mount_options
);
1558 if (!is_device_path(arg_mount_what
))
1561 if (arg_fsck
&& arg_mount_type
&& arg_transport
== BUS_TRANSPORT_LOCAL
) {
1562 r
= fsck_exists_for_fstype(arg_mount_type
);
1564 log_warning_errno(r
, "Couldn't determine whether fsck for %s exists, proceeding anyway.", arg_mount_type
);
1566 log_debug("Disabling file system check as fsck for %s doesn't exist.", arg_mount_type
);
1567 arg_fsck
= false; /* fsck doesn't exist, let's not attempt it */
1571 /* The kernel (properly) refuses mounting file systems with unknown uid=,gid= options,
1572 * but not for all filesystem types. Let's try to catch the cases where the option
1573 * would be used if the file system does not support it. It is also possible to
1574 * autodetect the file system, but that's only possible with disk-based file systems
1575 * which incidentally seem to be implemented more carefully and reject unknown options,
1576 * so it's probably OK that we do the check only when the type is specified.
1578 if (arg_mount_type
&&
1579 !streq(arg_mount_type
, "auto") &&
1580 arg_uid
!= UID_INVALID
&&
1581 !fstype_can_uid_gid(arg_mount_type
))
1582 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP
),
1583 "File system type %s is not known to support uid=/gid=, refusing.",
1586 switch (arg_action
) {
1589 case ACTION_DEFAULT
:
1590 r
= start_transient_mount(bus
, argv
+ optind
);
1593 case ACTION_AUTOMOUNT
:
1594 r
= start_transient_automount(bus
, argv
+ optind
);
1598 assert_not_reached();
1604 DEFINE_MAIN_FUNCTION(run
);