1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
10 #include "bus-error.h"
11 #include "bus-locator.h"
12 #include "bus-unit-util.h"
14 #include "bus-wait-for-jobs.h"
16 #include "device-util.h"
17 #include "errno-util.h"
20 #include "format-table.h"
21 #include "format-util.h"
22 #include "fstab-util.h"
23 #include "libmount-util.h"
24 #include "main-func.h"
25 #include "mountpoint-util.h"
27 #include "parse-argument.h"
28 #include "parse-util.h"
29 #include "path-util.h"
30 #include "polkit-agent.h"
31 #include "pretty-print.h"
32 #include "process-util.h"
33 #include "runtime-scope.h"
34 #include "stat-util.h"
35 #include "string-util.h"
37 #include "time-util.h"
38 #include "udev-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;
76 static sd_json_format_flags_t arg_json_format_flags
= SD_JSON_FORMAT_OFF
;
77 static bool arg_canonicalize
= true;
79 STATIC_DESTRUCTOR_REGISTER(arg_mount_what
, freep
);
80 STATIC_DESTRUCTOR_REGISTER(arg_mount_where
, freep
);
81 STATIC_DESTRUCTOR_REGISTER(arg_mount_type
, freep
);
82 STATIC_DESTRUCTOR_REGISTER(arg_mount_options
, freep
);
83 STATIC_DESTRUCTOR_REGISTER(arg_description
, freep
);
84 STATIC_DESTRUCTOR_REGISTER(arg_property
, strv_freep
);
85 STATIC_DESTRUCTOR_REGISTER(arg_automount_property
, strv_freep
);
87 static int parse_where(const char *input
, char **ret_where
) {
93 if (arg_transport
== BUS_TRANSPORT_LOCAL
&& arg_canonicalize
) {
94 r
= chase(input
, /* root= */ NULL
, CHASE_NONEXISTENT
, ret_where
, /* ret_fd= */ NULL
);
96 return log_error_errno(r
, "Failed to make path %s absolute: %m", input
);
98 if (!path_is_absolute(input
))
99 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
100 "Path must be absolute when operating remotely or when canonicalization is turned off: %s",
103 r
= path_simplify_alloc(input
, ret_where
);
105 return log_error_errno(r
, "Failed to simplify path %s: %m", input
);
111 static int help(void) {
112 _cleanup_free_
char *link
= NULL
;
115 r
= terminal_urlify_man("systemd-mount", "1", &link
);
119 printf("systemd-mount [OPTIONS...] WHAT [WHERE]\n"
120 "systemd-mount [OPTIONS...] --tmpfs [NAME] WHERE\n"
121 "systemd-mount [OPTIONS...] --list\n"
122 "%1$s [OPTIONS...] %7$sWHAT|WHERE...\n"
123 "\n%5$sEstablish a mount or auto-mount point transiently.%6$s\n"
124 "\n%3$sOptions:%4$s\n"
125 " -h --help Show this help\n"
126 " --version Show package version\n"
127 " --no-block Do not wait until operation finished\n"
128 " --no-pager Do not pipe output into a pager\n"
129 " --no-legend Do not show the headers\n"
130 " -l --full Do not ellipsize output\n"
131 " --no-ask-password Do not prompt for password\n"
132 " -q --quiet Suppress information messages during runtime\n"
133 " --json=pretty|short|off Generate JSON output\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 " --canonicalize=BOOL Controls whether to canonicalize path before\n"
155 "\nSee the %2$s for details.\n",
156 program_invocation_short_name
,
162 streq(program_invocation_short_name
, "systemd-umount") ? "" : "--umount ");
167 static int parse_argv(int argc
, char *argv
[]) {
185 ARG_AUTOMOUNT_PROPERTY
,
192 static const struct option options
[] = {
193 { "help", no_argument
, NULL
, 'h' },
194 { "version", no_argument
, NULL
, ARG_VERSION
},
195 { "no-block", no_argument
, NULL
, ARG_NO_BLOCK
},
196 { "no-pager", no_argument
, NULL
, ARG_NO_PAGER
},
197 { "no-legend", no_argument
, NULL
, ARG_NO_LEGEND
},
198 { "full", no_argument
, NULL
, 'l' },
199 { "no-ask-password", no_argument
, NULL
, ARG_NO_ASK_PASSWORD
},
200 { "quiet", no_argument
, NULL
, 'q' },
201 { "user", no_argument
, NULL
, ARG_USER
},
202 { "system", no_argument
, NULL
, ARG_SYSTEM
},
203 { "host", required_argument
, NULL
, 'H' },
204 { "machine", required_argument
, NULL
, 'M' },
205 { "discover", no_argument
, NULL
, ARG_DISCOVER
},
206 { "type", required_argument
, NULL
, 't' },
207 { "options", required_argument
, NULL
, 'o' },
208 { "owner", required_argument
, NULL
, ARG_OWNER
},
209 { "fsck", required_argument
, NULL
, ARG_FSCK
},
210 { "description", required_argument
, NULL
, ARG_DESCRIPTION
},
211 { "property", required_argument
, NULL
, 'p' },
212 { "automount", required_argument
, NULL
, ARG_AUTOMOUNT
},
213 { "timeout-idle-sec", required_argument
, NULL
, ARG_TIMEOUT_IDLE
},
214 { "automount-property", required_argument
, NULL
, ARG_AUTOMOUNT_PROPERTY
},
215 { "bind-device", no_argument
, NULL
, ARG_BIND_DEVICE
},
216 { "list", no_argument
, NULL
, ARG_LIST
},
217 { "umount", no_argument
, NULL
, 'u' },
218 { "unmount", no_argument
, NULL
, 'u' }, /* Compat spelling */
219 { "collect", no_argument
, NULL
, 'G' },
220 { "tmpfs", no_argument
, NULL
, 'T' },
221 { "json", required_argument
, NULL
, ARG_JSON
},
222 { "canonicalize", required_argument
, NULL
, ARG_CANONICALIZE
},
231 if (invoked_as(argv
, "systemd-umount"))
232 arg_action
= ACTION_UMOUNT
;
234 while ((c
= getopt_long(argc
, argv
, "hqH:M:t:o:p:AuGlT", options
, NULL
)) >= 0)
249 arg_pager_flags
|= PAGER_DISABLE
;
260 case ARG_NO_ASK_PASSWORD
:
261 arg_ask_password
= false;
269 arg_runtime_scope
= RUNTIME_SCOPE_USER
;
273 arg_runtime_scope
= RUNTIME_SCOPE_SYSTEM
;
277 arg_transport
= BUS_TRANSPORT_REMOTE
;
282 r
= parse_machine_argument(optarg
, &arg_host
, &arg_transport
);
292 r
= free_and_strdup_warn(&arg_mount_type
, optarg
);
298 r
= free_and_strdup_warn(&arg_mount_options
, optarg
);
304 const char *user
= optarg
;
306 r
= get_user_creds(&user
, &arg_uid
, &arg_gid
, NULL
, NULL
, 0);
308 return log_error_errno(r
,
309 r
== -EBADMSG
? "UID or GID of user %s are invalid."
310 : "Cannot use \"%s\" as owner: %m",
316 r
= parse_boolean_argument("--fsck=", optarg
, &arg_fsck
);
321 case ARG_DESCRIPTION
:
322 r
= free_and_strdup_warn(&arg_description
, optarg
);
328 if (strv_extend(&arg_property
, optarg
) < 0)
334 arg_action
= ACTION_AUTOMOUNT
;
338 r
= parse_boolean_argument("--automount=", optarg
, NULL
);
342 arg_action
= r
? ACTION_AUTOMOUNT
: ACTION_MOUNT
;
345 case ARG_TIMEOUT_IDLE
:
346 r
= parse_sec(optarg
, &arg_timeout_idle
);
348 return log_error_errno(r
, "Failed to parse timeout: %s", optarg
);
352 case ARG_AUTOMOUNT_PROPERTY
:
353 if (strv_extend(&arg_automount_property
, optarg
) < 0)
358 case ARG_BIND_DEVICE
:
359 arg_bind_device
= true;
363 arg_action
= ACTION_LIST
;
367 arg_action
= ACTION_UMOUNT
;
371 arg_aggressive_gc
= true;
379 r
= parse_json_argument(optarg
, &arg_json_format_flags
);
385 case ARG_CANONICALIZE
:
386 r
= parse_boolean_argument("--canonicalize=", optarg
, &arg_canonicalize
);
396 assert_not_reached();
399 if (arg_runtime_scope
== RUNTIME_SCOPE_USER
) {
400 arg_ask_password
= false;
402 if (arg_transport
!= BUS_TRANSPORT_LOCAL
)
403 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
404 "Execution in user context is not supported on non-local systems.");
407 if (arg_action
== ACTION_LIST
) {
409 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
410 "Too many arguments.");
412 if (arg_transport
!= BUS_TRANSPORT_LOCAL
)
413 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP
),
414 "Listing devices only supported locally.");
415 } else if (arg_action
== ACTION_UMOUNT
) {
417 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
418 "At least one argument required.");
420 if (arg_transport
!= BUS_TRANSPORT_LOCAL
|| !arg_canonicalize
)
421 for (int i
= optind
; i
< argc
; i
++)
422 if (!path_is_absolute(argv
[i
]))
423 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
424 "Path must be absolute when operating remotely or when canonicalization is turned off: %s",
428 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
429 "At least one argument required.");
432 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
433 "More than two arguments are not allowed.");
437 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
438 "--discover cannot be used in conjunction with --tmpfs.");
440 if (argc
<= optind
+1) {
441 arg_mount_what
= strdup("tmpfs");
445 r
= parse_where(argv
[optind
], &arg_mount_where
);
449 arg_mount_what
= strdup(argv
[optind
]);
454 if (!arg_mount_type
) {
455 arg_mount_type
= strdup("tmpfs");
458 } else if (!streq(arg_mount_type
, "tmpfs"))
459 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
460 "--tmpfs requested with incompatible --type=, refusing: %s",
463 if (arg_mount_type
&& !fstype_is_blockdev_backed(arg_mount_type
)) {
464 arg_mount_what
= strdup(argv
[optind
]);
468 _cleanup_free_
char *u
= NULL
;
469 const char *p
= argv
[optind
];
471 if (arg_canonicalize
) {
472 u
= fstab_node_to_udev_node(p
);
478 if (arg_transport
== BUS_TRANSPORT_LOCAL
&& arg_canonicalize
) {
479 r
= chase(p
, /* root= */ NULL
, /* flags= */ 0, &arg_mount_what
, /* ret_fd= */ NULL
);
481 return log_error_errno(r
, "Failed to chase path '%s': %m", p
);
483 if (!path_is_absolute(p
))
484 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
485 "Path must be absolute when operating remotely or when canonicalization is turned off: %s",
488 r
= path_simplify_alloc(p
, &arg_mount_what
);
495 if (argc
> optind
+1) {
496 r
= parse_where(argv
[optind
+1], &arg_mount_where
);
499 } else if (!arg_tmpfs
)
502 if (arg_discover
&& arg_transport
!= BUS_TRANSPORT_LOCAL
)
503 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP
),
504 "Automatic mount location discovery is only supported locally.");
506 _cleanup_free_
char *dev_bound
= NULL
;
507 r
= fstab_filter_options(arg_mount_options
, "x-systemd.device-bound\0",
508 /* ret_namefound = */ NULL
, &dev_bound
, /* ret_values = */ NULL
, /* ret_filtered = */ NULL
);
510 return log_error_errno(r
, "Failed to parse mount options for x-systemd.device-bound=: %m");
511 if (r
> 0 && !isempty(dev_bound
)) {
512 /* If x-systemd.device-bound=no is explicitly specified, never bind automount unit
513 * to device either. */
514 r
= parse_boolean(dev_bound
);
516 return log_error_errno(r
, "Invalid x-systemd.device-bound= option: %s", dev_bound
);
518 log_full(arg_bind_device
> 0 ? LOG_NOTICE
: LOG_DEBUG
,
519 "x-systemd.device-bound=no set, automatically disabling --bind-device.");
520 arg_bind_device
= false;
528 static int transient_unit_set_properties(sd_bus_message
*m
, UnitType t
, char **properties
) {
532 assert(IN_SET(t
, UNIT_MOUNT
, UNIT_AUTOMOUNT
));
534 if (!isempty(arg_description
)) {
535 r
= sd_bus_message_append(m
, "(sv)", "Description", "s", arg_description
);
540 if (arg_bind_device
> 0 && is_device_path(arg_mount_what
)) {
541 _cleanup_free_
char *device_unit
= NULL
;
543 r
= unit_name_from_path(arg_mount_what
, ".device", &device_unit
);
547 r
= sd_bus_message_append(m
, "(sv)(sv)",
548 "After", "as", 1, device_unit
,
549 "BindsTo", "as", 1, device_unit
);
554 if (arg_aggressive_gc
) {
555 r
= sd_bus_message_append(m
, "(sv)", "CollectMode", "s", "inactive-or-failed");
560 r
= bus_append_unit_property_assignment_many(m
, t
, properties
);
567 static int transient_mount_set_properties(sd_bus_message
*m
) {
572 r
= transient_unit_set_properties(m
, UNIT_MOUNT
, arg_property
);
576 if (arg_mount_what
) {
577 r
= sd_bus_message_append(m
, "(sv)", "What", "s", arg_mount_what
);
582 if (arg_mount_type
) {
583 r
= sd_bus_message_append(m
, "(sv)", "Type", "s", arg_mount_type
);
588 _cleanup_free_
char *options
= NULL
;
590 /* Prepend uid=…,gid=… if arg_uid is set */
591 if (arg_uid
!= UID_INVALID
) {
592 r
= strextendf_with_separator(&options
, ",",
593 "uid="UID_FMT
",gid="GID_FMT
, arg_uid
, arg_gid
);
598 /* Override the default for tmpfs mounts. The kernel sets the sticky bit on the root directory by
599 * default. This makes sense for the case when the user does 'mount -t tmpfs tmpfs /tmp', but less so
600 * for other directories.
602 * Let's also set some reasonable limits. We use the current umask, to match what a command to create
603 * directory would use, e.g. mkdir. */
607 r
= get_process_umask(0, &mask
);
611 assert((mask
& ~0777) == 0);
612 r
= strextendf_with_separator(&options
, ",",
613 "mode=0%o,nodev,nosuid%s", 0777 & ~mask
, NESTED_TMPFS_LIMITS
);
618 if (arg_mount_options
)
619 if (!strextend_with_separator(&options
, ",", arg_mount_options
))
623 log_debug("Using mount options: %s", options
);
624 r
= sd_bus_message_append(m
, "(sv)", "Options", "s", options
);
628 log_debug("Not using any mount options");
631 _cleanup_free_
char *fsck
= NULL
;
633 r
= unit_name_from_path_instance("systemd-fsck", arg_mount_what
, ".service", &fsck
);
637 r
= sd_bus_message_append(m
,
639 "Requires", "as", 1, fsck
,
640 "After", "as", 1, fsck
);
648 static int transient_automount_set_properties(sd_bus_message
*m
) {
653 r
= transient_unit_set_properties(m
, UNIT_AUTOMOUNT
, arg_automount_property
);
657 if (arg_timeout_idle
!= USEC_INFINITY
) {
658 r
= sd_bus_message_append(m
, "(sv)", "TimeoutIdleUSec", "t", arg_timeout_idle
);
666 static int start_transient_mount(sd_bus
*bus
) {
667 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
, *reply
= NULL
;
668 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
669 _cleanup_(bus_wait_for_jobs_freep
) BusWaitForJobs
*w
= NULL
;
670 _cleanup_free_
char *mount_unit
= NULL
;
676 r
= bus_wait_for_jobs_new(bus
, &w
);
678 return log_error_errno(r
, "Could not watch jobs: %m");
681 r
= unit_name_from_path(arg_mount_where
, ".mount", &mount_unit
);
683 return log_error_errno(r
, "Failed to make mount unit name: %m");
685 r
= bus_message_new_method_call(bus
, &m
, bus_systemd_mgr
, "StartTransientUnit");
687 return bus_log_create_error(r
);
690 r
= sd_bus_message_append(m
, "ss", mount_unit
, "fail");
692 return bus_log_create_error(r
);
695 r
= sd_bus_message_open_container(m
, 'a', "(sv)");
697 return bus_log_create_error(r
);
699 r
= transient_mount_set_properties(m
);
701 return bus_log_create_error(r
);
703 r
= sd_bus_message_close_container(m
);
705 return bus_log_create_error(r
);
707 /* No auxiliary units */
708 r
= sd_bus_message_append(m
, "a(sa(sv))", 0);
710 return bus_log_create_error(r
);
712 (void) polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
714 r
= sd_bus_call(bus
, m
, 0, &error
, &reply
);
716 return log_error_errno(r
, "Failed to start transient mount unit: %s", bus_error_message(&error
, r
));
721 r
= sd_bus_message_read(reply
, "o", &object
);
723 return bus_log_parse_error(r
);
725 r
= bus_wait_for_jobs_one(w
, object
, arg_quiet
? 0 : BUS_WAIT_JOBS_LOG_ERROR
, NULL
);
731 log_info("Started unit %s%s%s for mount point: %s%s%s",
732 ansi_highlight(), mount_unit
, ansi_normal(),
733 ansi_highlight(), arg_mount_where
, ansi_normal());
738 static int start_transient_automount(sd_bus
*bus
) {
739 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
, *reply
= NULL
;
740 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
741 _cleanup_(bus_wait_for_jobs_freep
) BusWaitForJobs
*w
= NULL
;
742 _cleanup_free_
char *automount_unit
= NULL
, *mount_unit
= NULL
;
748 r
= bus_wait_for_jobs_new(bus
, &w
);
750 return log_error_errno(r
, "Could not watch jobs: %m");
753 r
= unit_name_from_path(arg_mount_where
, ".automount", &automount_unit
);
755 return log_error_errno(r
, "Failed to make automount unit name: %m");
757 r
= unit_name_from_path(arg_mount_where
, ".mount", &mount_unit
);
759 return log_error_errno(r
, "Failed to make mount unit name: %m");
761 r
= bus_message_new_method_call(bus
, &m
, bus_systemd_mgr
, "StartTransientUnit");
763 return bus_log_create_error(r
);
766 r
= sd_bus_message_append(m
, "ss", automount_unit
, "fail");
768 return bus_log_create_error(r
);
771 r
= sd_bus_message_open_container(m
, 'a', "(sv)");
773 return bus_log_create_error(r
);
775 r
= transient_automount_set_properties(m
);
777 return bus_log_create_error(r
);
779 r
= sd_bus_message_close_container(m
);
781 return bus_log_create_error(r
);
783 /* Auxiliary units */
784 r
= sd_bus_message_open_container(m
, 'a', "(sa(sv))");
786 return bus_log_create_error(r
);
788 r
= sd_bus_message_open_container(m
, 'r', "sa(sv)");
790 return bus_log_create_error(r
);
792 r
= sd_bus_message_append(m
, "s", mount_unit
);
794 return bus_log_create_error(r
);
796 r
= sd_bus_message_open_container(m
, 'a', "(sv)");
798 return bus_log_create_error(r
);
800 r
= transient_mount_set_properties(m
);
802 return bus_log_create_error(r
);
804 r
= sd_bus_message_close_container(m
);
806 return bus_log_create_error(r
);
808 r
= sd_bus_message_close_container(m
);
810 return bus_log_create_error(r
);
812 r
= sd_bus_message_close_container(m
);
814 return bus_log_create_error(r
);
816 (void) polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
818 r
= sd_bus_call(bus
, m
, 0, &error
, &reply
);
820 return log_error_errno(r
, "Failed to start transient automount unit: %s", bus_error_message(&error
, r
));
825 r
= sd_bus_message_read(reply
, "o", &object
);
827 return bus_log_parse_error(r
);
829 r
= bus_wait_for_jobs_one(w
, object
, arg_quiet
? 0 : BUS_WAIT_JOBS_LOG_ERROR
, NULL
);
835 log_info("Started unit %s%s%s for mount point: %s%s%s",
836 ansi_highlight(), automount_unit
, ansi_normal(),
837 ansi_highlight(), arg_mount_where
, ansi_normal());
842 static int find_mount_points_by_source(const char *what
, char ***ret
) {
843 _cleanup_(mnt_free_tablep
) struct libmnt_table
*table
= NULL
;
844 _cleanup_(mnt_free_iterp
) struct libmnt_iter
*iter
= NULL
;
845 _cleanup_strv_free_
char **l
= NULL
;
852 /* Obtain all mount points with source being "what" from /proc/self/mountinfo, return value shows
853 * the total number of them. */
855 r
= libmount_parse_mountinfo(/* source = */ NULL
, &table
, &iter
);
857 return log_error_errno(r
, "Failed to parse /proc/self/mountinfo: %m");
860 struct libmnt_fs
*fs
;
861 const char *source
, *target
;
863 r
= mnt_table_next_fs(table
, iter
, &fs
);
867 return log_error_errno(r
, "Failed to get next entry from /proc/self/mountinfo: %m");
869 source
= mnt_fs_get_source(fs
);
870 target
= mnt_fs_get_target(fs
);
871 if (!source
|| !target
)
874 if (!path_equal(source
, what
))
877 r
= strv_extend_with_size(&l
, &n
, target
);
886 static int find_loop_device(const char *backing_file
, sd_device
**ret
) {
887 _cleanup_(sd_device_enumerator_unrefp
) sd_device_enumerator
*e
= NULL
;
890 assert(backing_file
);
893 r
= sd_device_enumerator_new(&e
);
897 r
= sd_device_enumerator_add_match_subsystem(e
, "block", /* match = */ true);
899 return log_error_errno(r
, "Failed to add subsystem match: %m");
901 r
= sd_device_enumerator_add_match_property(e
, "ID_FS_USAGE", "filesystem");
903 return log_error_errno(r
, "Failed to add property match: %m");
905 r
= sd_device_enumerator_add_match_sysname(e
, "loop*");
907 return log_error_errno(r
, "Failed to add sysname match: %m");
909 r
= sd_device_enumerator_add_match_sysattr(e
, "loop/backing_file", /* value = */ NULL
, /* match = */ true);
911 return log_error_errno(r
, "Failed to add sysattr match: %m");
913 FOREACH_DEVICE(e
, dev
) {
916 r
= sd_device_get_sysattr_value(dev
, "loop/backing_file", &s
);
918 log_device_debug_errno(dev
, r
, "Failed to read \"loop/backing_file\" sysattr, ignoring: %m");
922 if (inode_same(s
, backing_file
, 0) <= 0)
925 *ret
= sd_device_ref(dev
);
932 static int stop_mount(sd_bus
*bus
, const char *where
, const char *suffix
) {
933 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
, *reply
= NULL
;
934 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
935 _cleanup_(bus_wait_for_jobs_freep
) BusWaitForJobs
*w
= NULL
;
936 _cleanup_free_
char *mount_unit
= NULL
;
944 r
= bus_wait_for_jobs_new(bus
, &w
);
946 return log_error_errno(r
, "Could not watch jobs: %m");
949 r
= unit_name_from_path(where
, suffix
, &mount_unit
);
951 return log_error_errno(r
, "Failed to make %s unit name from path '%s': %m", suffix
+ 1, where
);
953 r
= bus_message_new_method_call(bus
, &m
, bus_systemd_mgr
, "StopUnit");
955 return bus_log_create_error(r
);
958 r
= sd_bus_message_append(m
, "ss", mount_unit
, "fail");
960 return bus_log_create_error(r
);
962 (void) polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
964 r
= sd_bus_call(bus
, m
, 0, &error
, &reply
);
966 if (streq(suffix
, ".automount") &&
967 sd_bus_error_has_name(&error
, "org.freedesktop.systemd1.NoSuchUnit"))
969 return log_error_errno(r
, "Failed to stop %s unit: %s", suffix
+ 1, bus_error_message(&error
, r
));
975 r
= sd_bus_message_read(reply
, "o", &object
);
977 return bus_log_parse_error(r
);
979 r
= bus_wait_for_jobs_one(w
, object
, arg_quiet
? 0 : BUS_WAIT_JOBS_LOG_ERROR
, NULL
);
985 log_info("Stopped unit %s%s%s for mount point: %s%s%s",
986 ansi_highlight(), mount_unit
, ansi_normal(),
987 ansi_highlight(), where
, ansi_normal());
992 static int stop_mounts(sd_bus
*bus
, const char *where
) {
998 if (path_equal(where
, "/"))
999 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1000 "Refusing to unmount root directory.");
1002 if (!path_is_normalized(where
))
1003 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1004 "Path contains non-normalized components: %s", where
);
1006 r
= stop_mount(bus
, where
, ".mount");
1010 r
= stop_mount(bus
, where
, ".automount");
1017 static int umount_by_device(sd_bus
*bus
, sd_device
*dev
) {
1018 _cleanup_strv_free_
char **list
= NULL
;
1025 if (sd_device_get_property_value(dev
, "SYSTEMD_MOUNT_WHERE", &v
) >= 0)
1026 ret
= stop_mounts(bus
, v
);
1028 r
= sd_device_get_devname(dev
, &v
);
1032 r
= find_mount_points_by_source(v
, &list
);
1036 STRV_FOREACH(l
, list
)
1037 RET_GATHER(ret
, stop_mounts(bus
, *l
));
1042 static int umount_by_device_node(sd_bus
*bus
, const char *node
) {
1043 _cleanup_(sd_device_unrefp
) sd_device
*dev
= NULL
;
1050 r
= sd_device_new_from_devname(&dev
, node
);
1052 return log_error_errno(r
, "Failed to get device from %s: %m", node
);
1054 r
= sd_device_get_property_value(dev
, "ID_FS_USAGE", &v
);
1056 return log_device_error_errno(dev
, r
, "Failed to get \"ID_FS_USAGE\" device property: %m");
1058 if (!streq(v
, "filesystem"))
1059 return log_device_error_errno(dev
, SYNTHETIC_ERRNO(EINVAL
),
1060 "%s does not contain a known file system.", node
);
1062 return umount_by_device(bus
, dev
);
1065 static int umount_loop(sd_bus
*bus
, const char *backing_file
) {
1066 _cleanup_(sd_device_unrefp
) sd_device
*dev
= NULL
;
1069 assert(backing_file
);
1071 r
= find_loop_device(backing_file
, &dev
);
1073 return log_error_errno(r
, r
== -ENXIO
? "File %s is not mounted." : "Can't get loop device for %s: %m", backing_file
);
1075 return umount_by_device(bus
, dev
);
1078 static int action_umount(sd_bus
*bus
, int argc
, char **argv
) {
1083 assert(argc
> optind
);
1085 if (arg_transport
!= BUS_TRANSPORT_LOCAL
|| !arg_canonicalize
) {
1086 for (int i
= optind
; i
< argc
; i
++) {
1087 _cleanup_free_
char *p
= NULL
;
1089 r
= path_simplify_alloc(argv
[i
], &p
);
1093 RET_GATHER(ret
, stop_mounts(bus
, p
));
1098 for (int i
= optind
; i
< argc
; i
++) {
1099 _cleanup_free_
char *u
= NULL
, *p
= NULL
;
1101 u
= fstab_node_to_udev_node(argv
[i
]);
1105 _cleanup_close_
int fd
= -EBADF
;
1106 r
= chase(u
, /* root= */ NULL
, 0, &p
, &fd
);
1108 RET_GATHER(ret
, log_error_errno(r
, "Failed to chase path '%s': %m", u
));
1113 if (fstat(fd
, &st
) < 0)
1114 return log_error_errno(errno
, "Can't stat '%s' (from %s): %m", p
, argv
[i
]);
1116 r
= is_mount_point_at(fd
, /* filename= */ NULL
, /* flags= */ 0);
1117 fd
= safe_close(fd
); /* before continuing make sure the dir is not keeping anything busy */
1119 RET_GATHER(ret
, stop_mounts(bus
, p
));
1121 /* This can realistically fail on pre-5.8 kernels that do not tell us via statx() if
1122 * something is a mount point, hence handle this gracefully, and go by type as we did
1123 * in pre-v258 times. */
1125 log_warning_errno(r
, "Failed to determine if '%s' is a mount point, ignoring: %m", u
);
1127 if (S_ISDIR(st
.st_mode
))
1128 r
= stop_mounts(bus
, p
);
1129 else if (S_ISBLK(st
.st_mode
))
1130 r
= umount_by_device_node(bus
, p
);
1131 else if (S_ISREG(st
.st_mode
))
1132 r
= umount_loop(bus
, p
);
1134 r
= log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1135 "Unknown file type for unmounting: %s (from %s)",
1144 static int acquire_mount_type(sd_device
*d
) {
1152 if (sd_device_get_property_value(d
, "ID_FS_TYPE", &v
) < 0)
1155 arg_mount_type
= strdup(v
);
1156 if (!arg_mount_type
)
1159 log_debug("Discovered type=%s", arg_mount_type
);
1163 static int acquire_mount_options(sd_device
*d
) {
1168 if (arg_mount_options
)
1171 if (sd_device_get_property_value(d
, "SYSTEMD_MOUNT_OPTIONS", &v
) < 0)
1174 arg_mount_options
= strdup(v
);
1175 if (!arg_mount_options
)
1178 log_debug("Discovered options=%s", arg_mount_options
);
1182 static const char* get_label(sd_device
*d
) {
1187 if (sd_device_get_property_value(d
, "ID_FS_LABEL", &label
) >= 0)
1190 if (sd_device_get_property_value(d
, "ID_PART_ENTRY_NAME", &label
) >= 0)
1196 static int acquire_mount_where(sd_device
*d
) {
1200 if (arg_mount_where
)
1203 if (sd_device_get_property_value(d
, "SYSTEMD_MOUNT_WHERE", &v
) < 0) {
1204 _cleanup_free_
char *escaped
= NULL
, *devname_bn
= NULL
;
1207 name
= get_label(d
);
1209 (void) device_get_model_string(d
, &name
);
1213 if (sd_device_get_devname(d
, &dn
) < 0)
1216 r
= path_extract_filename(dn
, &devname_bn
);
1218 return log_error_errno(r
, "Failed to extract file name from '%s': %m", dn
);
1223 escaped
= xescape(name
, "\\");
1226 if (!filename_is_valid(escaped
))
1229 arg_mount_where
= path_join("/run/media/system", escaped
);
1231 arg_mount_where
= strdup(v
);
1233 if (!arg_mount_where
)
1236 log_debug("Discovered where=%s", arg_mount_where
);
1240 static int acquire_mount_where_for_loop_dev(sd_device
*dev
) {
1241 _cleanup_strv_free_
char **list
= NULL
;
1247 if (arg_mount_where
)
1250 r
= sd_device_get_devname(dev
, &node
);
1254 r
= find_mount_points_by_source(node
, &list
);
1258 return log_device_error_errno(dev
, SYNTHETIC_ERRNO(EINVAL
),
1259 "Can't find mount point of %s. It is expected that %s is already mounted on a place.",
1262 return log_device_error_errno(dev
, SYNTHETIC_ERRNO(EINVAL
),
1263 "%s is mounted on %d places. It is expected that %s is mounted on a place.",
1266 arg_mount_where
= strdup(list
[0]);
1267 if (!arg_mount_where
)
1270 log_debug("Discovered where=%s", arg_mount_where
);
1274 static int acquire_description(sd_device
*d
) {
1275 const char *model
= NULL
, *label
;
1277 if (arg_description
)
1280 (void) device_get_model_string(d
, &model
);
1282 label
= get_label(d
);
1284 (void) sd_device_get_property_value(d
, "ID_PART_ENTRY_NUMBER", &label
);
1287 arg_description
= strjoin(model
, " ", label
);
1289 arg_description
= strdup(label
);
1291 arg_description
= strdup(model
);
1295 if (!arg_description
)
1298 log_debug("Discovered description=%s", arg_description
);
1302 static int acquire_removable(sd_device
*d
) {
1308 /* Shortcut this if there's no reason to check it */
1309 if (arg_action
!= ACTION_DEFAULT
&& arg_timeout_idle_set
&& arg_bind_device
>= 0)
1313 if (sd_device_get_sysattr_value(d
, "removable", &v
) >= 0)
1316 r
= sd_device_get_parent(d
, &d
);
1322 r
= device_in_subsystem(d
, "block");
1329 if (parse_boolean(v
) <= 0)
1332 log_debug("Discovered removable device.");
1334 if (arg_action
== ACTION_DEFAULT
) {
1335 log_debug("Automatically turning on automount.");
1336 arg_action
= ACTION_AUTOMOUNT
;
1339 if (!arg_timeout_idle_set
) {
1340 log_debug("Setting idle timeout to 1s.");
1341 arg_timeout_idle
= USEC_PER_SEC
;
1344 if (arg_bind_device
< 0) {
1345 log_debug("Binding automount unit to device.");
1346 arg_bind_device
= true;
1352 static int discover_loop_backing_file(void) {
1353 _cleanup_(sd_device_unrefp
) sd_device
*d
= NULL
;
1356 r
= find_loop_device(arg_mount_what
, &d
);
1358 _cleanup_free_
char *escaped
= NULL
, *bn
= NULL
;
1360 if (arg_mount_where
)
1363 r
= path_extract_filename(arg_mount_what
, &bn
);
1365 return log_error_errno(r
, "Failed to extract file name from backing file path '%s': %m", arg_mount_what
);
1367 escaped
= xescape(bn
, "\\");
1370 if (!filename_is_valid(escaped
))
1371 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1372 "Escaped name %s is not a valid filename.",
1375 arg_mount_where
= path_join("/run/media/system", escaped
);
1376 if (!arg_mount_where
)
1379 log_debug("Discovered where=%s", arg_mount_where
);
1383 return log_error_errno(r
, "Can't get loop device for %s: %m", arg_mount_what
);
1385 r
= acquire_mount_type(d
);
1389 r
= acquire_mount_options(d
);
1393 r
= acquire_mount_where_for_loop_dev(d
);
1397 r
= acquire_description(d
);
1404 static int discover_device(void) {
1405 _cleanup_(sd_device_unrefp
) sd_device
*d
= NULL
;
1410 if (stat(arg_mount_what
, &st
) < 0)
1411 return log_error_errno(errno
, "Can't stat %s: %m", arg_mount_what
);
1413 if (S_ISREG(st
.st_mode
))
1414 return discover_loop_backing_file();
1416 if (!S_ISBLK(st
.st_mode
))
1417 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1418 "Unsupported mount source type for --discover: %s", arg_mount_what
);
1420 r
= sd_device_new_from_stat_rdev(&d
, &st
);
1422 return log_error_errno(r
, "Failed to get device from device number: %m");
1424 if (sd_device_get_property_value(d
, "ID_FS_USAGE", &v
) < 0 || !streq(v
, "filesystem"))
1425 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1426 "%s does not contain a known file system.",
1429 r
= acquire_mount_type(d
);
1433 r
= acquire_mount_options(d
);
1437 r
= acquire_mount_where(d
);
1441 r
= acquire_description(d
);
1445 r
= acquire_removable(d
);
1452 static int list_devices(void) {
1465 _cleanup_(sd_device_enumerator_unrefp
) sd_device_enumerator
*e
= NULL
;
1466 _cleanup_(table_unrefp
) Table
*table
= NULL
;
1469 r
= sd_device_enumerator_new(&e
);
1473 r
= sd_device_enumerator_add_match_subsystem(e
, "block", true);
1475 return log_error_errno(r
, "Failed to add block match: %m");
1477 r
= sd_device_enumerator_add_match_property(e
, "ID_FS_USAGE", "filesystem");
1479 return log_error_errno(r
, "Failed to add property match: %m");
1481 table
= table_new("node", "diskseq", "path", "model", "wwn", "fstype", "label", "uuid");
1486 table_set_width(table
, 0);
1488 r
= table_set_sort(table
, (size_t) 0);
1490 return log_error_errno(r
, "Failed to set sort index: %m");
1492 table_set_ersatz_string(table
, TABLE_ERSATZ_DASH
);
1494 FOREACH_DEVICE(e
, d
) {
1495 for (unsigned c
= 0; c
< _COLUMN_MAX
; c
++) {
1496 const char *x
= NULL
;
1501 (void) sd_device_get_devname(d
, &x
);
1504 case COLUMN_DISKSEQ
: {
1507 r
= sd_device_get_diskseq(d
, &ds
);
1509 log_debug_errno(r
, "Failed to get diskseq of block device, ignoring: %m");
1510 r
= table_add_cell(table
, NULL
, TABLE_EMPTY
, NULL
);
1512 r
= table_add_cell(table
, NULL
, TABLE_UINT64
, &ds
);
1514 return table_log_add_error(r
);
1520 (void) sd_device_get_property_value(d
, "ID_PATH", &x
);
1524 (void) device_get_model_string(d
, &x
);
1528 (void) sd_device_get_property_value(d
, "ID_WWN", &x
);
1532 (void) sd_device_get_property_value(d
, "ID_FS_TYPE", &x
);
1540 (void) sd_device_get_property_value(d
, "ID_FS_UUID", &x
);
1544 r
= table_add_cell(table
, NULL
, c
== COLUMN_NODE
? TABLE_PATH
: TABLE_STRING
, x
);
1546 return table_log_add_error(r
);
1550 return table_print_with_pager(table
, arg_json_format_flags
, arg_pager_flags
, arg_legend
);
1553 static int run(int argc
, char* argv
[]) {
1554 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1559 r
= parse_argv(argc
, argv
);
1563 if (arg_action
== ACTION_LIST
)
1564 return list_devices();
1566 r
= bus_connect_transport_systemd(arg_transport
, arg_host
, arg_runtime_scope
, &bus
);
1568 return bus_log_connect_error(r
, arg_transport
, arg_runtime_scope
);
1570 (void) sd_bus_set_allow_interactive_authorization(bus
, arg_ask_password
);
1572 if (arg_action
== ACTION_UMOUNT
)
1573 return action_umount(bus
, argc
, argv
);
1575 if ((!arg_mount_type
|| fstype_is_blockdev_backed(arg_mount_type
))
1576 && !path_is_normalized(arg_mount_what
))
1577 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1578 "Path contains non-normalized components: %s",
1582 r
= discover_device();
1587 if (!arg_mount_where
)
1588 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1589 "Can't figure out where to mount %s.",
1592 if (path_equal(arg_mount_where
, "/"))
1593 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1594 "Refusing to operate on root directory.");
1596 if (!path_is_normalized(arg_mount_where
))
1597 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1598 "Path contains non-normalized components: %s",
1601 if (streq_ptr(arg_mount_type
, "auto"))
1602 arg_mount_type
= mfree(arg_mount_type
);
1603 if (streq_ptr(arg_mount_options
, "defaults"))
1604 arg_mount_options
= mfree(arg_mount_options
);
1606 if (!is_device_path(arg_mount_what
))
1609 if (arg_fsck
&& arg_mount_type
&& arg_transport
== BUS_TRANSPORT_LOCAL
) {
1610 r
= fsck_exists_for_fstype(arg_mount_type
);
1612 log_warning_errno(r
, "Couldn't determine whether fsck for %s exists, proceeding anyway.", arg_mount_type
);
1614 log_debug("Disabling file system check as fsck for %s doesn't exist.", arg_mount_type
);
1615 arg_fsck
= false; /* fsck doesn't exist, let's not attempt it */
1619 /* The kernel (properly) refuses mounting file systems with unknown uid=,gid= options,
1620 * but not for all filesystem types. Let's try to catch the cases where the option
1621 * would be used if the file system does not support it. It is also possible to
1622 * autodetect the file system, but that's only possible with disk-based file systems
1623 * which incidentally seem to be implemented more carefully and reject unknown options,
1624 * so it's probably OK that we do the check only when the type is specified.
1626 if (arg_mount_type
&&
1627 !streq(arg_mount_type
, "auto") &&
1628 arg_uid
!= UID_INVALID
&&
1629 !fstype_can_uid_gid(arg_mount_type
))
1630 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP
),
1631 "File system type %s is not known to support uid=/gid=, refusing.",
1634 switch (arg_action
) {
1637 case ACTION_DEFAULT
:
1638 return start_transient_mount(bus
);
1640 case ACTION_AUTOMOUNT
:
1641 return start_transient_automount(bus
);
1644 assert_not_reached();
1648 DEFINE_MAIN_FUNCTION(run
);