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"
13 #include "chase-symlinks.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"
39 #include "unit-name.h"
40 #include "user-util.h"
48 } arg_action
= ACTION_DEFAULT
;
50 static bool arg_no_block
= false;
51 static PagerFlags arg_pager_flags
= 0;
52 static bool arg_legend
= true;
53 static bool arg_full
= false;
54 static bool arg_ask_password
= true;
55 static bool arg_quiet
= false;
56 static BusTransport arg_transport
= BUS_TRANSPORT_LOCAL
;
57 static RuntimeScope arg_runtime_scope
= RUNTIME_SCOPE_SYSTEM
;
58 static const char *arg_host
= NULL
;
59 static bool arg_discover
= false;
60 static char *arg_mount_what
= NULL
;
61 static char *arg_mount_where
= NULL
;
62 static char *arg_mount_type
= NULL
;
63 static char *arg_mount_options
= NULL
;
64 static char *arg_description
= NULL
;
65 static char **arg_property
= NULL
;
66 static usec_t arg_timeout_idle
= USEC_INFINITY
;
67 static bool arg_timeout_idle_set
= false;
68 static char **arg_automount_property
= NULL
;
69 static int arg_bind_device
= -1;
70 static uid_t arg_uid
= UID_INVALID
;
71 static gid_t arg_gid
= GID_INVALID
;
72 static bool arg_fsck
= true;
73 static bool arg_aggressive_gc
= false;
75 STATIC_DESTRUCTOR_REGISTER(arg_mount_what
, freep
);
76 STATIC_DESTRUCTOR_REGISTER(arg_mount_where
, freep
);
77 STATIC_DESTRUCTOR_REGISTER(arg_mount_type
, freep
);
78 STATIC_DESTRUCTOR_REGISTER(arg_mount_options
, freep
);
79 STATIC_DESTRUCTOR_REGISTER(arg_description
, freep
);
80 STATIC_DESTRUCTOR_REGISTER(arg_property
, strv_freep
);
81 STATIC_DESTRUCTOR_REGISTER(arg_automount_property
, strv_freep
);
83 static int help(void) {
84 _cleanup_free_
char *link
= NULL
;
87 r
= terminal_urlify_man("systemd-mount", "1", &link
);
91 printf("systemd-mount [OPTIONS...] WHAT [WHERE]\n"
92 "systemd-mount [OPTIONS...] --list\n"
93 "%s [OPTIONS...] %sWHAT|WHERE...\n\n"
94 "Establish a mount or auto-mount point transiently.\n\n"
95 " -h --help Show this help\n"
96 " --version Show package version\n"
97 " --no-block Do not wait until operation finished\n"
98 " --no-pager Do not pipe output into a pager\n"
99 " --no-legend Do not show the headers\n"
100 " -l --full Do not ellipsize output\n"
101 " --no-ask-password Do not prompt for password\n"
102 " -q --quiet Suppress information messages during runtime\n"
103 " --user Run as user unit\n"
104 " -H --host=[USER@]HOST Operate on remote host\n"
105 " -M --machine=CONTAINER Operate on local container\n"
106 " --discover Discover mount device metadata\n"
107 " -t --type=TYPE File system type\n"
108 " -o --options=OPTIONS Mount options\n"
109 " --owner=USER Add uid= and gid= options for USER\n"
110 " --fsck=no Don't run file system check before mount\n"
111 " --description=TEXT Description for unit\n"
112 " -p --property=NAME=VALUE Set mount unit property\n"
113 " -A --automount=BOOL Create an auto-mount point\n"
114 " --timeout-idle-sec=SEC Specify automount idle timeout\n"
115 " --automount-property=NAME=VALUE\n"
116 " Set automount unit property\n"
117 " --bind-device Bind automount unit to device\n"
118 " --list List mountable block devices\n"
119 " -u --umount Unmount mount points\n"
120 " -G --collect Unload unit after it stopped, even when failed\n"
121 "\nSee the %s for details.\n",
122 program_invocation_short_name
,
123 streq(program_invocation_short_name
, "systemd-umount") ? "" : "--umount ",
129 static int parse_argv(int argc
, char *argv
[]) {
147 ARG_AUTOMOUNT_PROPERTY
,
152 static const struct option options
[] = {
153 { "help", no_argument
, NULL
, 'h' },
154 { "version", no_argument
, NULL
, ARG_VERSION
},
155 { "no-block", no_argument
, NULL
, ARG_NO_BLOCK
},
156 { "no-pager", no_argument
, NULL
, ARG_NO_PAGER
},
157 { "no-legend", no_argument
, NULL
, ARG_NO_LEGEND
},
158 { "full", no_argument
, NULL
, 'l' },
159 { "no-ask-password", no_argument
, NULL
, ARG_NO_ASK_PASSWORD
},
160 { "quiet", no_argument
, NULL
, 'q' },
161 { "user", no_argument
, NULL
, ARG_USER
},
162 { "system", no_argument
, NULL
, ARG_SYSTEM
},
163 { "host", required_argument
, NULL
, 'H' },
164 { "machine", required_argument
, NULL
, 'M' },
165 { "discover", no_argument
, NULL
, ARG_DISCOVER
},
166 { "type", required_argument
, NULL
, 't' },
167 { "options", required_argument
, NULL
, 'o' },
168 { "owner", required_argument
, NULL
, ARG_OWNER
},
169 { "fsck", required_argument
, NULL
, ARG_FSCK
},
170 { "description", required_argument
, NULL
, ARG_DESCRIPTION
},
171 { "property", required_argument
, NULL
, 'p' },
172 { "automount", required_argument
, NULL
, ARG_AUTOMOUNT
},
173 { "timeout-idle-sec", required_argument
, NULL
, ARG_TIMEOUT_IDLE
},
174 { "automount-property", required_argument
, NULL
, ARG_AUTOMOUNT_PROPERTY
},
175 { "bind-device", no_argument
, NULL
, ARG_BIND_DEVICE
},
176 { "list", no_argument
, NULL
, ARG_LIST
},
177 { "umount", no_argument
, NULL
, 'u' },
178 { "unmount", no_argument
, NULL
, 'u' },
179 { "collect", no_argument
, NULL
, 'G' },
188 if (invoked_as(argv
, "systemd-umount"))
189 arg_action
= ACTION_UMOUNT
;
191 while ((c
= getopt_long(argc
, argv
, "hqH:M:t:o:p:AuGl", options
, NULL
)) >= 0)
206 arg_pager_flags
|= PAGER_DISABLE
;
217 case ARG_NO_ASK_PASSWORD
:
218 arg_ask_password
= false;
226 arg_runtime_scope
= RUNTIME_SCOPE_USER
;
230 arg_runtime_scope
= RUNTIME_SCOPE_SYSTEM
;
234 arg_transport
= BUS_TRANSPORT_REMOTE
;
239 arg_transport
= BUS_TRANSPORT_MACHINE
;
248 r
= free_and_strdup_warn(&arg_mount_type
, optarg
);
254 r
= free_and_strdup_warn(&arg_mount_options
, optarg
);
260 const char *user
= optarg
;
262 r
= get_user_creds(&user
, &arg_uid
, &arg_gid
, NULL
, NULL
, 0);
264 return log_error_errno(r
,
265 r
== -EBADMSG
? "UID or GID of user %s are invalid."
266 : "Cannot use \"%s\" as owner: %m",
272 r
= parse_boolean_argument("--fsck=", optarg
, &arg_fsck
);
277 case ARG_DESCRIPTION
:
278 r
= free_and_strdup_warn(&arg_description
, optarg
);
284 if (strv_extend(&arg_property
, optarg
) < 0)
290 arg_action
= ACTION_AUTOMOUNT
;
294 r
= parse_boolean_argument("--automount=", optarg
, NULL
);
298 arg_action
= r
? ACTION_AUTOMOUNT
: ACTION_MOUNT
;
301 case ARG_TIMEOUT_IDLE
:
302 r
= parse_sec(optarg
, &arg_timeout_idle
);
304 return log_error_errno(r
, "Failed to parse timeout: %s", optarg
);
308 case ARG_AUTOMOUNT_PROPERTY
:
309 if (strv_extend(&arg_automount_property
, optarg
) < 0)
314 case ARG_BIND_DEVICE
:
315 arg_bind_device
= true;
319 arg_action
= ACTION_LIST
;
323 arg_action
= ACTION_UMOUNT
;
327 arg_aggressive_gc
= true;
334 assert_not_reached();
337 if (arg_runtime_scope
== RUNTIME_SCOPE_USER
) {
338 arg_ask_password
= false;
340 if (arg_transport
!= BUS_TRANSPORT_LOCAL
)
341 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
342 "Execution in user context is not supported on non-local systems.");
345 if (arg_action
== ACTION_LIST
) {
347 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
348 "Too many arguments.");
350 if (arg_transport
!= BUS_TRANSPORT_LOCAL
)
351 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP
),
352 "Listing devices only supported locally.");
353 } else if (arg_action
== ACTION_UMOUNT
) {
355 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
356 "At least one argument required.");
358 if (arg_transport
!= BUS_TRANSPORT_LOCAL
) {
361 for (i
= optind
; i
< argc
; i
++)
362 if (!path_is_absolute(argv
[i
]) )
363 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
364 "Only absolute path is supported: %s", argv
[i
]);
368 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
369 "At least one argument required.");
372 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
373 "At most two arguments required.");
375 if (arg_mount_type
&& !fstype_is_blockdev_backed(arg_mount_type
)) {
376 arg_mount_what
= strdup(argv
[optind
]);
380 } else if (arg_transport
== BUS_TRANSPORT_LOCAL
) {
381 _cleanup_free_
char *u
= NULL
;
383 u
= fstab_node_to_udev_node(argv
[optind
]);
387 r
= chase_symlinks(u
, NULL
, 0, &arg_mount_what
, NULL
);
389 return log_error_errno(r
, "Failed to make path %s absolute: %m", u
);
391 arg_mount_what
= strdup(argv
[optind
]);
395 path_simplify(arg_mount_what
);
397 if (!path_is_absolute(arg_mount_what
))
398 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
399 "Only absolute path is supported: %s", arg_mount_what
);
402 if (argc
> optind
+1) {
403 if (arg_transport
== BUS_TRANSPORT_LOCAL
) {
404 r
= chase_symlinks(argv
[optind
+1], NULL
, CHASE_NONEXISTENT
, &arg_mount_where
, NULL
);
406 return log_error_errno(r
, "Failed to make path %s absolute: %m", argv
[optind
+1]);
408 arg_mount_where
= strdup(argv
[optind
+1]);
409 if (!arg_mount_where
)
412 path_simplify(arg_mount_where
);
414 if (!path_is_absolute(arg_mount_where
))
415 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
416 "Only absolute path is supported: %s", arg_mount_where
);
421 if (arg_discover
&& arg_transport
!= BUS_TRANSPORT_LOCAL
)
422 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP
),
423 "Automatic mount location discovery is only supported locally.");
429 static int transient_unit_set_properties(sd_bus_message
*m
, UnitType t
, char **properties
) {
432 if (!isempty(arg_description
)) {
433 r
= sd_bus_message_append(m
, "(sv)", "Description", "s", arg_description
);
438 if (arg_bind_device
&& is_device_path(arg_mount_what
)) {
439 _cleanup_free_
char *device_unit
= NULL
;
441 r
= unit_name_from_path(arg_mount_what
, ".device", &device_unit
);
445 r
= sd_bus_message_append(m
, "(sv)(sv)",
446 "After", "as", 1, device_unit
,
447 "BindsTo", "as", 1, device_unit
);
452 if (arg_aggressive_gc
) {
453 r
= sd_bus_message_append(m
, "(sv)", "CollectMode", "s", "inactive-or-failed");
458 r
= bus_append_unit_property_assignment_many(m
, t
, properties
);
465 static int transient_mount_set_properties(sd_bus_message
*m
) {
466 _cleanup_free_
char *options
= NULL
;
471 r
= transient_unit_set_properties(m
, UNIT_MOUNT
, arg_property
);
475 if (arg_mount_what
) {
476 r
= sd_bus_message_append(m
, "(sv)", "What", "s", arg_mount_what
);
481 if (arg_mount_type
) {
482 r
= sd_bus_message_append(m
, "(sv)", "Type", "s", arg_mount_type
);
487 /* Prepend uid=…,gid=… if arg_uid is set */
488 if (arg_uid
!= UID_INVALID
) {
489 r
= asprintf(&options
,
490 "uid=" UID_FMT
",gid=" GID_FMT
"%s%s",
492 arg_mount_options
? "," : "", strempty(arg_mount_options
));
497 if (options
|| arg_mount_options
) {
498 log_debug("Using mount options: %s", options
?: arg_mount_options
);
500 r
= sd_bus_message_append(m
, "(sv)", "Options", "s", options
?: arg_mount_options
);
504 log_debug("Not using any mount options");
507 _cleanup_free_
char *fsck
= NULL
;
509 r
= unit_name_from_path_instance("systemd-fsck", arg_mount_what
, ".service", &fsck
);
513 r
= sd_bus_message_append(m
,
515 "Requires", "as", 1, fsck
,
516 "After", "as", 1, fsck
);
524 static int transient_automount_set_properties(sd_bus_message
*m
) {
529 r
= transient_unit_set_properties(m
, UNIT_AUTOMOUNT
, arg_automount_property
);
533 if (arg_timeout_idle
!= USEC_INFINITY
) {
534 r
= sd_bus_message_append(m
, "(sv)", "TimeoutIdleUSec", "t", arg_timeout_idle
);
542 static int start_transient_mount(
546 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
, *reply
= NULL
;
547 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
548 _cleanup_(bus_wait_for_jobs_freep
) BusWaitForJobs
*w
= NULL
;
549 _cleanup_free_
char *mount_unit
= NULL
;
553 r
= bus_wait_for_jobs_new(bus
, &w
);
555 return log_error_errno(r
, "Could not watch jobs: %m");
558 r
= unit_name_from_path(arg_mount_where
, ".mount", &mount_unit
);
560 return log_error_errno(r
, "Failed to make mount unit name: %m");
562 r
= bus_message_new_method_call(bus
, &m
, bus_systemd_mgr
, "StartTransientUnit");
564 return bus_log_create_error(r
);
566 r
= sd_bus_message_set_allow_interactive_authorization(m
, arg_ask_password
);
568 return bus_log_create_error(r
);
571 r
= sd_bus_message_append(m
, "ss", mount_unit
, "fail");
573 return bus_log_create_error(r
);
576 r
= sd_bus_message_open_container(m
, 'a', "(sv)");
578 return bus_log_create_error(r
);
580 r
= transient_mount_set_properties(m
);
582 return bus_log_create_error(r
);
584 r
= sd_bus_message_close_container(m
);
586 return bus_log_create_error(r
);
588 /* Auxiliary units */
589 r
= sd_bus_message_append(m
, "a(sa(sv))", 0);
591 return bus_log_create_error(r
);
593 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
595 r
= sd_bus_call(bus
, m
, 0, &error
, &reply
);
597 return log_error_errno(r
, "Failed to start transient mount unit: %s", bus_error_message(&error
, r
));
602 r
= sd_bus_message_read(reply
, "o", &object
);
604 return bus_log_parse_error(r
);
606 r
= bus_wait_for_jobs_one(w
, object
, arg_quiet
, NULL
);
612 log_info("Started unit %s%s%s for mount point: %s%s%s",
613 ansi_highlight(), mount_unit
, ansi_normal(),
614 ansi_highlight(), arg_mount_where
, ansi_normal());
619 static int start_transient_automount(
623 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
, *reply
= NULL
;
624 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
625 _cleanup_(bus_wait_for_jobs_freep
) BusWaitForJobs
*w
= NULL
;
626 _cleanup_free_
char *automount_unit
= NULL
, *mount_unit
= NULL
;
630 r
= bus_wait_for_jobs_new(bus
, &w
);
632 return log_error_errno(r
, "Could not watch jobs: %m");
635 r
= unit_name_from_path(arg_mount_where
, ".automount", &automount_unit
);
637 return log_error_errno(r
, "Failed to make automount unit name: %m");
639 r
= unit_name_from_path(arg_mount_where
, ".mount", &mount_unit
);
641 return log_error_errno(r
, "Failed to make mount unit name: %m");
643 r
= bus_message_new_method_call(bus
, &m
, bus_systemd_mgr
, "StartTransientUnit");
645 return bus_log_create_error(r
);
647 r
= sd_bus_message_set_allow_interactive_authorization(m
, arg_ask_password
);
649 return bus_log_create_error(r
);
652 r
= sd_bus_message_append(m
, "ss", automount_unit
, "fail");
654 return bus_log_create_error(r
);
657 r
= sd_bus_message_open_container(m
, 'a', "(sv)");
659 return bus_log_create_error(r
);
661 r
= transient_automount_set_properties(m
);
663 return bus_log_create_error(r
);
665 r
= sd_bus_message_close_container(m
);
667 return bus_log_create_error(r
);
669 /* Auxiliary units */
670 r
= sd_bus_message_open_container(m
, 'a', "(sa(sv))");
672 return bus_log_create_error(r
);
674 r
= sd_bus_message_open_container(m
, 'r', "sa(sv)");
676 return bus_log_create_error(r
);
678 r
= sd_bus_message_append(m
, "s", mount_unit
);
680 return bus_log_create_error(r
);
682 r
= sd_bus_message_open_container(m
, 'a', "(sv)");
684 return bus_log_create_error(r
);
686 r
= transient_mount_set_properties(m
);
688 return bus_log_create_error(r
);
690 r
= sd_bus_message_close_container(m
);
692 return bus_log_create_error(r
);
694 r
= sd_bus_message_close_container(m
);
696 return bus_log_create_error(r
);
698 r
= sd_bus_message_close_container(m
);
700 return bus_log_create_error(r
);
702 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
704 r
= sd_bus_call(bus
, m
, 0, &error
, &reply
);
706 return log_error_errno(r
, "Failed to start transient automount unit: %s", bus_error_message(&error
, r
));
711 r
= sd_bus_message_read(reply
, "o", &object
);
713 return bus_log_parse_error(r
);
715 r
= bus_wait_for_jobs_one(w
, object
, arg_quiet
, NULL
);
721 log_info("Started unit %s%s%s for mount point: %s%s%s",
722 ansi_highlight(), automount_unit
, ansi_normal(),
723 ansi_highlight(), arg_mount_where
, ansi_normal());
728 static int find_mount_points(const char *what
, char ***list
) {
729 _cleanup_(mnt_free_tablep
) struct libmnt_table
*table
= NULL
;
730 _cleanup_(mnt_free_iterp
) struct libmnt_iter
*iter
= NULL
;
731 _cleanup_strv_free_
char **l
= NULL
;
738 /* Returns all mount points obtained from /proc/self/mountinfo in *list,
739 * and the number of mount points as return value. */
741 r
= libmount_parse(NULL
, NULL
, &table
, &iter
);
743 return log_error_errno(r
, "Failed to parse /proc/self/mountinfo: %m");
746 struct libmnt_fs
*fs
;
747 const char *source
, *target
;
749 r
= mnt_table_next_fs(table
, iter
, &fs
);
753 return log_error_errno(r
, "Failed to get next entry from /proc/self/mountinfo: %m");
755 source
= mnt_fs_get_source(fs
);
756 target
= mnt_fs_get_target(fs
);
757 if (!source
|| !target
)
760 if (!path_equal(source
, what
))
763 /* one extra slot is needed for the terminating NULL */
764 if (!GREEDY_REALLOC0(l
, n
+ 2))
767 l
[n
] = strdup(target
);
773 if (!GREEDY_REALLOC0(l
, n
+ 1))
780 static int find_loop_device(const char *backing_file
, sd_device
**ret
) {
781 _cleanup_(sd_device_enumerator_unrefp
) sd_device_enumerator
*e
= NULL
;
785 assert(backing_file
);
788 r
= sd_device_enumerator_new(&e
);
792 r
= sd_device_enumerator_add_match_subsystem(e
, "block", /* match = */ true);
794 return log_error_errno(r
, "Failed to add subsystem match: %m");
796 r
= sd_device_enumerator_add_match_property(e
, "ID_FS_USAGE", "filesystem");
798 return log_error_errno(r
, "Failed to add property match: %m");
800 r
= sd_device_enumerator_add_match_sysname(e
, "loop*");
802 return log_error_errno(r
, "Failed to add sysname match: %m");
804 r
= sd_device_enumerator_add_match_sysattr(e
, "loop/backing_file", /* value = */ NULL
, /* match = */ true);
806 return log_error_errno(r
, "Failed to add sysattr match: %m");
808 FOREACH_DEVICE(e
, dev
) {
811 r
= sd_device_get_sysattr_value(dev
, "loop/backing_file", &s
);
813 log_device_debug_errno(dev
, r
, "Failed to read \"loop/backing_file\" sysattr, ignoring: %m");
817 if (files_same(s
, backing_file
, 0) <= 0)
820 *ret
= sd_device_ref(dev
);
827 static int stop_mount(
830 const char *suffix
) {
832 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
, *reply
= NULL
;
833 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
834 _cleanup_(bus_wait_for_jobs_freep
) BusWaitForJobs
*w
= NULL
;
835 _cleanup_free_
char *mount_unit
= NULL
;
839 r
= bus_wait_for_jobs_new(bus
, &w
);
841 return log_error_errno(r
, "Could not watch jobs: %m");
844 r
= unit_name_from_path(where
, suffix
, &mount_unit
);
846 return log_error_errno(r
, "Failed to make %s unit name from path %s: %m", suffix
+ 1, where
);
848 r
= bus_message_new_method_call(bus
, &m
, bus_systemd_mgr
, "StopUnit");
850 return bus_log_create_error(r
);
852 r
= sd_bus_message_set_allow_interactive_authorization(m
, arg_ask_password
);
854 return bus_log_create_error(r
);
857 r
= sd_bus_message_append(m
, "ss", mount_unit
, "fail");
859 return bus_log_create_error(r
);
861 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
863 r
= sd_bus_call(bus
, m
, 0, &error
, &reply
);
865 if (streq(suffix
, ".automount") &&
866 sd_bus_error_has_name(&error
, "org.freedesktop.systemd1.NoSuchUnit"))
868 return log_error_errno(r
, "Failed to stop %s unit: %s", suffix
+ 1, bus_error_message(&error
, r
));
874 r
= sd_bus_message_read(reply
, "o", &object
);
876 return bus_log_parse_error(r
);
878 r
= bus_wait_for_jobs_one(w
, object
, arg_quiet
, NULL
);
884 log_info("Stopped unit %s%s%s for mount point: %s%s%s",
885 ansi_highlight(), mount_unit
, ansi_normal(),
886 ansi_highlight(), where
, ansi_normal());
891 static int stop_mounts(
897 if (path_equal(where
, "/"))
898 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
899 "Refusing to operate on root directory: %s", where
);
901 if (!path_is_normalized(where
))
902 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
903 "Path contains non-normalized components: %s", where
);
905 r
= stop_mount(bus
, where
, ".mount");
909 r
= stop_mount(bus
, where
, ".automount");
916 static int umount_by_device(sd_bus
*bus
, sd_device
*dev
) {
917 _cleanup_(sd_device_unrefp
) sd_device
*d
= NULL
;
918 _cleanup_strv_free_
char **list
= NULL
;
925 if (sd_device_get_property_value(d
, "SYSTEMD_MOUNT_WHERE", &v
) >= 0)
926 ret
= stop_mounts(bus
, v
);
928 r
= sd_device_get_devname(dev
, &v
);
932 r
= find_mount_points(v
, &list
);
936 STRV_FOREACH(l
, list
) {
937 r
= stop_mounts(bus
, *l
);
945 static int umount_by_device_node(sd_bus
*bus
, const char *node
) {
946 _cleanup_(sd_device_unrefp
) sd_device
*dev
= NULL
;
953 r
= sd_device_new_from_devname(&dev
, node
);
955 return log_error_errno(r
, "Failed to get device from %s: %m", node
);
957 r
= sd_device_get_property_value(dev
, "ID_FS_USAGE", &v
);
959 return log_device_error_errno(dev
, r
, "Failed to get \"ID_FS_USAGE\" device property: %m");
961 if (!streq(v
, "filesystem"))
962 return log_device_error_errno(dev
, SYNTHETIC_ERRNO(EINVAL
),
963 "%s does not contain a known file system.", node
);
965 return umount_by_device(bus
, dev
);
968 static int umount_loop(sd_bus
*bus
, const char *backing_file
) {
969 _cleanup_(sd_device_unrefp
) sd_device
*dev
= NULL
;
972 assert(backing_file
);
974 r
= find_loop_device(backing_file
, &dev
);
976 return log_error_errno(r
, r
== -ENXIO
? "File %s is not mounted." : "Can't get loop device for %s: %m", backing_file
);
978 return umount_by_device(bus
, dev
);
981 static int action_umount(
988 if (arg_transport
!= BUS_TRANSPORT_LOCAL
) {
989 for (i
= optind
; i
< argc
; i
++) {
990 _cleanup_free_
char *p
= NULL
;
998 r
= stop_mounts(bus
, p
);
1005 for (i
= optind
; i
< argc
; i
++) {
1006 _cleanup_free_
char *u
= NULL
, *p
= NULL
;
1009 u
= fstab_node_to_udev_node(argv
[i
]);
1013 r
= chase_symlinks(u
, NULL
, 0, &p
, NULL
);
1015 r2
= log_error_errno(r
, "Failed to make path %s absolute: %m", argv
[i
]);
1019 if (stat(p
, &st
) < 0)
1020 return log_error_errno(errno
, "Can't stat %s (from %s): %m", p
, argv
[i
]);
1022 if (S_ISBLK(st
.st_mode
))
1023 r
= umount_by_device_node(bus
, p
);
1024 else if (S_ISREG(st
.st_mode
))
1025 r
= umount_loop(bus
, p
);
1026 else if (S_ISDIR(st
.st_mode
))
1027 r
= stop_mounts(bus
, p
);
1029 log_error("Invalid file type: %s (from %s)", p
, argv
[i
]);
1040 static int acquire_mount_type(sd_device
*d
) {
1048 if (sd_device_get_property_value(d
, "ID_FS_TYPE", &v
) < 0)
1051 arg_mount_type
= strdup(v
);
1052 if (!arg_mount_type
)
1055 log_debug("Discovered type=%s", arg_mount_type
);
1059 static int acquire_mount_options(sd_device
*d
) {
1064 if (arg_mount_options
)
1067 if (sd_device_get_property_value(d
, "SYSTEMD_MOUNT_OPTIONS", &v
) < 0)
1070 arg_mount_options
= strdup(v
);
1071 if (!arg_mount_options
)
1074 log_debug("Discovered options=%s", arg_mount_options
);
1078 static const char *get_model(sd_device
*d
) {
1083 if (sd_device_get_property_value(d
, "ID_MODEL_FROM_DATABASE", &model
) >= 0)
1086 if (sd_device_get_property_value(d
, "ID_MODEL", &model
) >= 0)
1092 static const char* get_label(sd_device
*d
) {
1097 if (sd_device_get_property_value(d
, "ID_FS_LABEL", &label
) >= 0)
1100 if (sd_device_get_property_value(d
, "ID_PART_ENTRY_NAME", &label
) >= 0)
1106 static int acquire_mount_where(sd_device
*d
) {
1110 if (arg_mount_where
)
1113 if (sd_device_get_property_value(d
, "SYSTEMD_MOUNT_WHERE", &v
) < 0) {
1114 _cleanup_free_
char *escaped
= NULL
, *devname_bn
= NULL
;
1117 name
= get_label(d
);
1119 name
= get_model(d
);
1123 if (sd_device_get_devname(d
, &dn
) < 0)
1126 r
= path_extract_filename(dn
, &devname_bn
);
1128 return log_error_errno(r
, "Failed to extract file name from '%s': %m", dn
);
1133 escaped
= xescape(name
, "\\");
1136 if (!filename_is_valid(escaped
))
1139 arg_mount_where
= path_join("/run/media/system", escaped
);
1141 arg_mount_where
= strdup(v
);
1143 if (!arg_mount_where
)
1146 log_debug("Discovered where=%s", arg_mount_where
);
1150 static int acquire_mount_where_for_loop_dev(sd_device
*dev
) {
1151 _cleanup_strv_free_
char **list
= NULL
;
1157 if (arg_mount_where
)
1160 r
= sd_device_get_devname(dev
, &node
);
1164 r
= find_mount_points(node
, &list
);
1168 return log_device_error_errno(dev
, SYNTHETIC_ERRNO(EINVAL
),
1169 "Can't find mount point of %s. It is expected that %s is already mounted on a place.",
1172 return log_device_error_errno(dev
, SYNTHETIC_ERRNO(EINVAL
),
1173 "%s is mounted on %d places. It is expected that %s is mounted on a place.",
1176 arg_mount_where
= strdup(list
[0]);
1177 if (!arg_mount_where
)
1180 log_debug("Discovered where=%s", arg_mount_where
);
1184 static int acquire_description(sd_device
*d
) {
1185 const char *model
, *label
;
1187 if (arg_description
)
1190 model
= get_model(d
);
1192 label
= get_label(d
);
1194 (void) sd_device_get_property_value(d
, "ID_PART_ENTRY_NUMBER", &label
);
1197 arg_description
= strjoin(model
, " ", label
);
1199 arg_description
= strdup(label
);
1201 arg_description
= strdup(model
);
1205 if (!arg_description
)
1208 log_debug("Discovered description=%s", arg_description
);
1212 static int acquire_removable(sd_device
*d
) {
1215 /* Shortcut this if there's no reason to check it */
1216 if (arg_action
!= ACTION_DEFAULT
&& arg_timeout_idle_set
&& arg_bind_device
>= 0)
1220 if (sd_device_get_sysattr_value(d
, "removable", &v
) >= 0)
1223 if (sd_device_get_parent(d
, &d
) < 0)
1226 if (sd_device_get_subsystem(d
, &v
) < 0 || !streq(v
, "block"))
1230 if (parse_boolean(v
) <= 0)
1233 log_debug("Discovered removable device.");
1235 if (arg_action
== ACTION_DEFAULT
) {
1236 log_debug("Automatically turning on automount.");
1237 arg_action
= ACTION_AUTOMOUNT
;
1240 if (!arg_timeout_idle_set
) {
1241 log_debug("Setting idle timeout to 1s.");
1242 arg_timeout_idle
= USEC_PER_SEC
;
1245 if (arg_bind_device
< 0) {
1246 log_debug("Binding automount unit to device.");
1247 arg_bind_device
= true;
1253 static int discover_loop_backing_file(void) {
1254 _cleanup_(sd_device_unrefp
) sd_device
*d
= NULL
;
1257 r
= find_loop_device(arg_mount_what
, &d
);
1258 if (r
< 0 && r
!= -ENXIO
)
1259 return log_error_errno(errno
, "Can't get loop device for %s: %m", arg_mount_what
);
1262 _cleanup_free_
char *escaped
= NULL
, *bn
= NULL
;
1264 if (arg_mount_where
)
1267 r
= path_extract_filename(arg_mount_what
, &bn
);
1269 return log_error_errno(r
, "Failed to extract file name from backing file path '%s': %m", arg_mount_what
);
1271 escaped
= xescape(bn
, "\\");
1274 if (!filename_is_valid(escaped
))
1275 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1276 "Escaped name %s is not a valid filename.",
1279 arg_mount_where
= path_join("/run/media/system", escaped
);
1280 if (!arg_mount_where
)
1283 log_debug("Discovered where=%s", arg_mount_where
);
1287 r
= acquire_mount_type(d
);
1291 r
= acquire_mount_options(d
);
1295 r
= acquire_mount_where_for_loop_dev(d
);
1299 r
= acquire_description(d
);
1306 static int discover_device(void) {
1307 _cleanup_(sd_device_unrefp
) sd_device
*d
= NULL
;
1312 if (stat(arg_mount_what
, &st
) < 0)
1313 return log_error_errno(errno
, "Can't stat %s: %m", arg_mount_what
);
1315 if (S_ISREG(st
.st_mode
))
1316 return discover_loop_backing_file();
1318 if (!S_ISBLK(st
.st_mode
))
1319 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1320 "Invalid file type: %s",
1323 r
= sd_device_new_from_stat_rdev(&d
, &st
);
1325 return log_error_errno(r
, "Failed to get device from device number: %m");
1327 if (sd_device_get_property_value(d
, "ID_FS_USAGE", &v
) < 0 || !streq(v
, "filesystem"))
1328 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1329 "%s does not contain a known file system.",
1332 r
= acquire_mount_type(d
);
1336 r
= acquire_mount_options(d
);
1340 r
= acquire_mount_where(d
);
1344 r
= acquire_description(d
);
1348 r
= acquire_removable(d
);
1366 static int list_devices(void) {
1367 _cleanup_(sd_device_enumerator_unrefp
) sd_device_enumerator
*e
= NULL
;
1368 _cleanup_(table_unrefp
) Table
*table
= NULL
;
1373 r
= sd_device_enumerator_new(&e
);
1377 r
= sd_device_enumerator_add_match_subsystem(e
, "block", true);
1379 return log_error_errno(r
, "Failed to add block match: %m");
1381 r
= sd_device_enumerator_add_match_property(e
, "ID_FS_USAGE", "filesystem");
1383 return log_error_errno(r
, "Failed to add property match: %m");
1385 table
= table_new("NODE", "PATH", "MODEL", "WWN", "TYPE", "LABEL", "UUID");
1390 table_set_width(table
, 0);
1392 r
= table_set_sort(table
, (size_t) 0);
1394 return log_error_errno(r
, "Failed to set sort index: %m");
1396 table_set_header(table
, arg_legend
);
1398 FOREACH_DEVICE(e
, d
) {
1399 for (c
= 0; c
< _COLUMN_MAX
; c
++) {
1400 const char *x
= NULL
;
1405 (void) sd_device_get_devname(d
, &x
);
1409 (void) sd_device_get_property_value(d
, "ID_PATH", &x
);
1417 (void) sd_device_get_property_value(d
, "ID_WWN", &x
);
1421 (void) sd_device_get_property_value(d
, "ID_FS_TYPE", &x
);
1429 (void) sd_device_get_property_value(d
, "ID_FS_UUID", &x
);
1433 r
= table_add_cell(table
, NULL
, c
== COLUMN_NODE
? TABLE_PATH
: TABLE_STRING
, strna(x
));
1435 return table_log_add_error(r
);
1439 pager_open(arg_pager_flags
);
1441 r
= table_print(table
, NULL
);
1443 return table_log_print_error(r
);
1448 static int run(int argc
, char* argv
[]) {
1449 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1452 log_show_color(true);
1453 log_parse_environment();
1456 r
= parse_argv(argc
, argv
);
1460 if (arg_action
== ACTION_LIST
)
1461 return list_devices();
1463 r
= bus_connect_transport_systemd(arg_transport
, arg_host
, arg_runtime_scope
, &bus
);
1465 return bus_log_connect_error(r
, arg_transport
);
1467 if (arg_action
== ACTION_UMOUNT
)
1468 return action_umount(bus
, argc
, argv
);
1470 if ((!arg_mount_type
|| fstype_is_blockdev_backed(arg_mount_type
))
1471 && !path_is_normalized(arg_mount_what
))
1472 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1473 "Path contains non-normalized components: %s",
1477 r
= discover_device();
1482 if (!arg_mount_where
)
1483 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1484 "Can't figure out where to mount %s.",
1487 if (path_equal(arg_mount_where
, "/"))
1488 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1489 "Refusing to operate on root directory.");
1491 if (!path_is_normalized(arg_mount_where
))
1492 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1493 "Path contains non-normalized components: %s",
1496 if (streq_ptr(arg_mount_type
, "auto"))
1497 arg_mount_type
= mfree(arg_mount_type
);
1498 if (streq_ptr(arg_mount_options
, "defaults"))
1499 arg_mount_options
= mfree(arg_mount_options
);
1501 if (!is_device_path(arg_mount_what
))
1504 if (arg_fsck
&& arg_mount_type
&& arg_transport
== BUS_TRANSPORT_LOCAL
) {
1505 r
= fsck_exists_for_fstype(arg_mount_type
);
1507 log_warning_errno(r
, "Couldn't determine whether fsck for %s exists, proceeding anyway.", arg_mount_type
);
1509 log_debug("Disabling file system check as fsck for %s doesn't exist.", arg_mount_type
);
1510 arg_fsck
= false; /* fsck doesn't exist, let's not attempt it */
1514 /* The kernel (properly) refuses mounting file systems with unknown uid=,gid= options,
1515 * but not for all filesystem types. Let's try to catch the cases where the option
1516 * would be used if the file system does not support it. It is also possible to
1517 * autodetect the file system, but that's only possible with disk-based file systems
1518 * which incidentally seem to be implemented more carefully and reject unknown options,
1519 * so it's probably OK that we do the check only when the type is specified.
1521 if (arg_mount_type
&&
1522 !streq(arg_mount_type
, "auto") &&
1523 arg_uid
!= UID_INVALID
&&
1524 !fstype_can_uid_gid(arg_mount_type
))
1525 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP
),
1526 "File system type %s is not known to support uid=/gid=, refusing.",
1529 switch (arg_action
) {
1532 case ACTION_DEFAULT
:
1533 r
= start_transient_mount(bus
, argv
+ optind
);
1536 case ACTION_AUTOMOUNT
:
1537 r
= start_transient_automount(bus
, argv
+ optind
);
1541 assert_not_reached();
1547 DEFINE_MAIN_FUNCTION(run
);