1 /* SPDX-License-Identifier: LGPL-2.1+ */
9 #include "bus-unit-util.h"
11 #include "bus-wait-for-jobs.h"
12 #include "device-util.h"
13 #include "dirent-util.h"
17 #include "format-util.h"
19 #include "fstab-util.h"
20 #include "libmount-util.h"
21 #include "main-func.h"
22 #include "mount-util.h"
23 #include "mountpoint-util.h"
25 #include "parse-util.h"
26 #include "path-util.h"
27 #include "pretty-print.h"
28 #include "sort-util.h"
29 #include "spawn-polkit-agent.h"
30 #include "stat-util.h"
32 #include "terminal-util.h"
34 #include "unit-name.h"
35 #include "user-util.h"
43 } arg_action
= ACTION_DEFAULT
;
45 static bool arg_no_block
= false;
46 static PagerFlags arg_pager_flags
= 0;
47 static bool arg_ask_password
= true;
48 static bool arg_quiet
= false;
49 static BusTransport arg_transport
= BUS_TRANSPORT_LOCAL
;
50 static bool arg_user
= false;
51 static const char *arg_host
= NULL
;
52 static bool arg_discover
= false;
53 static char *arg_mount_what
= NULL
;
54 static char *arg_mount_where
= NULL
;
55 static char *arg_mount_type
= NULL
;
56 static char *arg_mount_options
= NULL
;
57 static char *arg_description
= NULL
;
58 static char **arg_property
= NULL
;
59 static usec_t arg_timeout_idle
= USEC_INFINITY
;
60 static bool arg_timeout_idle_set
= false;
61 static char **arg_automount_property
= NULL
;
62 static int arg_bind_device
= -1;
63 static uid_t arg_uid
= UID_INVALID
;
64 static gid_t arg_gid
= GID_INVALID
;
65 static bool arg_fsck
= true;
66 static bool arg_aggressive_gc
= false;
68 STATIC_DESTRUCTOR_REGISTER(arg_mount_what
, freep
);
69 STATIC_DESTRUCTOR_REGISTER(arg_mount_where
, freep
);
70 STATIC_DESTRUCTOR_REGISTER(arg_mount_type
, freep
);
71 STATIC_DESTRUCTOR_REGISTER(arg_mount_options
, freep
);
72 STATIC_DESTRUCTOR_REGISTER(arg_description
, freep
);
73 STATIC_DESTRUCTOR_REGISTER(arg_property
, strv_freep
);
74 STATIC_DESTRUCTOR_REGISTER(arg_automount_property
, strv_freep
);
76 static int help(void) {
77 _cleanup_free_
char *link
= NULL
;
80 r
= terminal_urlify_man("systemd-mount", "1", &link
);
84 printf("systemd-mount [OPTIONS...] WHAT [WHERE]\n"
85 "systemd-mount [OPTIONS...] --list\n"
86 "%s [OPTIONS...] %sWHAT|WHERE...\n\n"
87 "Establish a mount or auto-mount point transiently.\n\n"
88 " -h --help Show this help\n"
89 " --version Show package version\n"
90 " --no-block Do not wait until operation finished\n"
91 " --no-pager Do not pipe output into a pager\n"
92 " --no-ask-password Do not prompt for password\n"
93 " -q --quiet Suppress information messages during runtime\n"
94 " --user Run as user unit\n"
95 " -H --host=[USER@]HOST Operate on remote host\n"
96 " -M --machine=CONTAINER Operate on local container\n"
97 " --discover Discover mount device metadata\n"
98 " -t --type=TYPE File system type\n"
99 " -o --options=OPTIONS Mount options\n"
100 " --owner=USER Add uid= and gid= options for USER\n"
101 " --fsck=no Don't run file system check before mount\n"
102 " --description=TEXT Description for unit\n"
103 " -p --property=NAME=VALUE Set mount unit property\n"
104 " -A --automount=BOOL Create an auto-mount point\n"
105 " --timeout-idle-sec=SEC Specify automount idle timeout\n"
106 " --automount-property=NAME=VALUE\n"
107 " Set automount unit property\n"
108 " --bind-device Bind automount unit to device\n"
109 " --list List mountable block devices\n"
110 " -u --umount Unmount mount points\n"
111 " -G --collect Unload unit after it stopped, even when failed\n"
112 "\nSee the %s for details.\n"
113 , program_invocation_short_name
114 , streq(program_invocation_short_name
, "systemd-umount") ? "" : "--umount "
121 static int parse_argv(int argc
, char *argv
[]) {
138 ARG_AUTOMOUNT_PROPERTY
,
143 static const struct option options
[] = {
144 { "help", no_argument
, NULL
, 'h' },
145 { "version", no_argument
, NULL
, ARG_VERSION
},
146 { "no-block", no_argument
, NULL
, ARG_NO_BLOCK
},
147 { "no-pager", no_argument
, NULL
, ARG_NO_PAGER
},
148 { "no-ask-password", no_argument
, NULL
, ARG_NO_ASK_PASSWORD
},
149 { "quiet", no_argument
, NULL
, 'q' },
150 { "user", no_argument
, NULL
, ARG_USER
},
151 { "system", no_argument
, NULL
, ARG_SYSTEM
},
152 { "host", required_argument
, NULL
, 'H' },
153 { "machine", required_argument
, NULL
, 'M' },
154 { "discover", no_argument
, NULL
, ARG_DISCOVER
},
155 { "type", required_argument
, NULL
, 't' },
156 { "options", required_argument
, NULL
, 'o' },
157 { "owner", required_argument
, NULL
, ARG_OWNER
},
158 { "fsck", required_argument
, NULL
, ARG_FSCK
},
159 { "description", required_argument
, NULL
, ARG_DESCRIPTION
},
160 { "property", required_argument
, NULL
, 'p' },
161 { "automount", required_argument
, NULL
, ARG_AUTOMOUNT
},
162 { "timeout-idle-sec", required_argument
, NULL
, ARG_TIMEOUT_IDLE
},
163 { "automount-property", required_argument
, NULL
, ARG_AUTOMOUNT_PROPERTY
},
164 { "bind-device", no_argument
, NULL
, ARG_BIND_DEVICE
},
165 { "list", no_argument
, NULL
, ARG_LIST
},
166 { "umount", no_argument
, NULL
, 'u' },
167 { "unmount", no_argument
, NULL
, 'u' },
168 { "collect", no_argument
, NULL
, 'G' },
177 if (strstr(program_invocation_short_name
, "systemd-umount"))
178 arg_action
= ACTION_UMOUNT
;
180 while ((c
= getopt_long(argc
, argv
, "hqH:M:t:o:p:AuG", options
, NULL
)) >= 0)
195 arg_pager_flags
|= PAGER_DISABLE
;
198 case ARG_NO_ASK_PASSWORD
:
199 arg_ask_password
= false;
215 arg_transport
= BUS_TRANSPORT_REMOTE
;
220 arg_transport
= BUS_TRANSPORT_MACHINE
;
229 if (free_and_strdup(&arg_mount_type
, optarg
) < 0)
234 if (free_and_strdup(&arg_mount_options
, optarg
) < 0)
239 const char *user
= optarg
;
241 r
= get_user_creds(&user
, &arg_uid
, &arg_gid
, NULL
, NULL
, 0);
243 return log_error_errno(r
,
244 r
== -EBADMSG
? "UID or GID of user %s are invalid."
245 : "Cannot use \"%s\" as owner: %m",
251 r
= parse_boolean(optarg
);
253 return log_error_errno(r
, "Failed to parse --fsck= argument: %s", optarg
);
258 case ARG_DESCRIPTION
:
259 if (free_and_strdup(&arg_description
, optarg
) < 0)
264 if (strv_extend(&arg_property
, optarg
) < 0)
270 arg_action
= ACTION_AUTOMOUNT
;
274 r
= parse_boolean(optarg
);
276 return log_error_errno(r
, "--automount= expects a valid boolean parameter: %s", optarg
);
278 arg_action
= r
? ACTION_AUTOMOUNT
: ACTION_MOUNT
;
281 case ARG_TIMEOUT_IDLE
:
282 r
= parse_sec(optarg
, &arg_timeout_idle
);
284 return log_error_errno(r
, "Failed to parse timeout: %s", optarg
);
288 case ARG_AUTOMOUNT_PROPERTY
:
289 if (strv_extend(&arg_automount_property
, optarg
) < 0)
294 case ARG_BIND_DEVICE
:
295 arg_bind_device
= true;
299 arg_action
= ACTION_LIST
;
303 arg_action
= ACTION_UMOUNT
;
307 arg_aggressive_gc
= true;
314 assert_not_reached("Unhandled option");
317 if (arg_user
&& arg_transport
!= BUS_TRANSPORT_LOCAL
)
318 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
319 "Execution in user context is not supported on non-local systems.");
321 if (arg_action
== ACTION_LIST
) {
323 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
324 "Too many arguments.");
326 if (arg_transport
!= BUS_TRANSPORT_LOCAL
)
327 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP
),
328 "Listing devices only supported locally.");
329 } else if (arg_action
== ACTION_UMOUNT
) {
331 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
332 "At least one argument required.");
334 if (arg_transport
!= BUS_TRANSPORT_LOCAL
) {
337 for (i
= optind
; i
< argc
; i
++)
338 if (!path_is_absolute(argv
[i
]) )
339 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
340 "Only absolute path is supported: %s", argv
[i
]);
344 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
345 "At least one argument required.");
348 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
349 "At most two arguments required.");
351 if (arg_mount_type
&& (fstype_is_api_vfs(arg_mount_type
) || fstype_is_network(arg_mount_type
))) {
352 arg_mount_what
= strdup(argv
[optind
]);
356 } else if (arg_transport
== BUS_TRANSPORT_LOCAL
) {
357 _cleanup_free_
char *u
= NULL
;
359 u
= fstab_node_to_udev_node(argv
[optind
]);
363 r
= chase_symlinks(u
, NULL
, 0, &arg_mount_what
);
365 return log_error_errno(r
, "Failed to make path %s absolute: %m", u
);
367 arg_mount_what
= strdup(argv
[optind
]);
371 path_simplify(arg_mount_what
, false);
373 if (!path_is_absolute(arg_mount_what
))
374 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
375 "Only absolute path is supported: %s", arg_mount_what
);
378 if (argc
> optind
+1) {
379 if (arg_transport
== BUS_TRANSPORT_LOCAL
) {
380 r
= chase_symlinks(argv
[optind
+1], NULL
, CHASE_NONEXISTENT
, &arg_mount_where
);
382 return log_error_errno(r
, "Failed to make path %s absolute: %m", argv
[optind
+1]);
384 arg_mount_where
= strdup(argv
[optind
+1]);
385 if (!arg_mount_where
)
388 path_simplify(arg_mount_where
, false);
390 if (!path_is_absolute(arg_mount_where
))
391 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
392 "Only absolute path is supported: %s", arg_mount_where
);
397 if (arg_discover
&& arg_transport
!= BUS_TRANSPORT_LOCAL
)
398 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP
),
399 "Automatic mount location discovery is only supported locally.");
405 static int transient_unit_set_properties(sd_bus_message
*m
, UnitType t
, char **properties
) {
408 if (!isempty(arg_description
)) {
409 r
= sd_bus_message_append(m
, "(sv)", "Description", "s", arg_description
);
414 if (arg_bind_device
&& is_device_path(arg_mount_what
)) {
415 _cleanup_free_
char *device_unit
= NULL
;
417 r
= unit_name_from_path(arg_mount_what
, ".device", &device_unit
);
421 r
= sd_bus_message_append(m
, "(sv)(sv)",
422 "After", "as", 1, device_unit
,
423 "BindsTo", "as", 1, device_unit
);
428 if (arg_aggressive_gc
) {
429 r
= sd_bus_message_append(m
, "(sv)", "CollectMode", "s", "inactive-or-failed");
434 r
= bus_append_unit_property_assignment_many(m
, t
, properties
);
441 static int transient_mount_set_properties(sd_bus_message
*m
) {
442 _cleanup_free_
char *options
= NULL
;
447 r
= transient_unit_set_properties(m
, UNIT_MOUNT
, arg_property
);
451 if (arg_mount_what
) {
452 r
= sd_bus_message_append(m
, "(sv)", "What", "s", arg_mount_what
);
457 if (arg_mount_type
) {
458 r
= sd_bus_message_append(m
, "(sv)", "Type", "s", arg_mount_type
);
463 /* Prepend uid=…,gid=… if arg_uid is set */
464 if (arg_uid
!= UID_INVALID
) {
465 r
= asprintf(&options
,
466 "uid=" UID_FMT
",gid=" GID_FMT
"%s%s",
468 arg_mount_options
? "," : "", strempty(arg_mount_options
));
473 if (options
|| arg_mount_options
) {
474 log_debug("Using mount options: %s", options
?: arg_mount_options
);
476 r
= sd_bus_message_append(m
, "(sv)", "Options", "s", options
?: arg_mount_options
);
480 log_debug("Not using any mount options");
483 _cleanup_free_
char *fsck
= NULL
;
485 r
= unit_name_from_path_instance("systemd-fsck", arg_mount_what
, ".service", &fsck
);
489 r
= sd_bus_message_append(m
,
491 "Requires", "as", 1, fsck
,
492 "After", "as", 1, fsck
);
500 static int transient_automount_set_properties(sd_bus_message
*m
) {
505 r
= transient_unit_set_properties(m
, UNIT_AUTOMOUNT
, arg_automount_property
);
509 if (arg_timeout_idle
!= USEC_INFINITY
) {
510 r
= sd_bus_message_append(m
, "(sv)", "TimeoutIdleUSec", "t", arg_timeout_idle
);
518 static int start_transient_mount(
522 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
, *reply
= NULL
;
523 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
524 _cleanup_(bus_wait_for_jobs_freep
) BusWaitForJobs
*w
= NULL
;
525 _cleanup_free_
char *mount_unit
= NULL
;
529 r
= bus_wait_for_jobs_new(bus
, &w
);
531 return log_error_errno(r
, "Could not watch jobs: %m");
534 r
= unit_name_from_path(arg_mount_where
, ".mount", &mount_unit
);
536 return log_error_errno(r
, "Failed to make mount unit name: %m");
538 r
= sd_bus_message_new_method_call(
541 "org.freedesktop.systemd1",
542 "/org/freedesktop/systemd1",
543 "org.freedesktop.systemd1.Manager",
544 "StartTransientUnit");
546 return bus_log_create_error(r
);
548 r
= sd_bus_message_set_allow_interactive_authorization(m
, arg_ask_password
);
550 return bus_log_create_error(r
);
553 r
= sd_bus_message_append(m
, "ss", mount_unit
, "fail");
555 return bus_log_create_error(r
);
558 r
= sd_bus_message_open_container(m
, 'a', "(sv)");
560 return bus_log_create_error(r
);
562 r
= transient_mount_set_properties(m
);
564 return bus_log_create_error(r
);
566 r
= sd_bus_message_close_container(m
);
568 return bus_log_create_error(r
);
570 /* Auxiliary units */
571 r
= sd_bus_message_append(m
, "a(sa(sv))", 0);
573 return bus_log_create_error(r
);
575 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
577 r
= sd_bus_call(bus
, m
, 0, &error
, &reply
);
579 return log_error_errno(r
, "Failed to start transient mount unit: %s", bus_error_message(&error
, r
));
584 r
= sd_bus_message_read(reply
, "o", &object
);
586 return bus_log_parse_error(r
);
588 r
= bus_wait_for_jobs_one(w
, object
, arg_quiet
);
594 log_info("Started unit %s%s%s for mount point: %s%s%s",
595 ansi_highlight(), mount_unit
, ansi_normal(),
596 ansi_highlight(), arg_mount_where
, ansi_normal());
601 static int start_transient_automount(
605 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
, *reply
= NULL
;
606 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
607 _cleanup_(bus_wait_for_jobs_freep
) BusWaitForJobs
*w
= NULL
;
608 _cleanup_free_
char *automount_unit
= NULL
, *mount_unit
= NULL
;
612 r
= bus_wait_for_jobs_new(bus
, &w
);
614 return log_error_errno(r
, "Could not watch jobs: %m");
617 r
= unit_name_from_path(arg_mount_where
, ".automount", &automount_unit
);
619 return log_error_errno(r
, "Failed to make automount unit name: %m");
621 r
= unit_name_from_path(arg_mount_where
, ".mount", &mount_unit
);
623 return log_error_errno(r
, "Failed to make mount unit name: %m");
625 r
= sd_bus_message_new_method_call(
628 "org.freedesktop.systemd1",
629 "/org/freedesktop/systemd1",
630 "org.freedesktop.systemd1.Manager",
631 "StartTransientUnit");
633 return bus_log_create_error(r
);
635 r
= sd_bus_message_set_allow_interactive_authorization(m
, arg_ask_password
);
637 return bus_log_create_error(r
);
640 r
= sd_bus_message_append(m
, "ss", automount_unit
, "fail");
642 return bus_log_create_error(r
);
645 r
= sd_bus_message_open_container(m
, 'a', "(sv)");
647 return bus_log_create_error(r
);
649 r
= transient_automount_set_properties(m
);
651 return bus_log_create_error(r
);
653 r
= sd_bus_message_close_container(m
);
655 return bus_log_create_error(r
);
657 /* Auxiliary units */
658 r
= sd_bus_message_open_container(m
, 'a', "(sa(sv))");
660 return bus_log_create_error(r
);
662 r
= sd_bus_message_open_container(m
, 'r', "sa(sv)");
664 return bus_log_create_error(r
);
666 r
= sd_bus_message_append(m
, "s", mount_unit
);
668 return bus_log_create_error(r
);
670 r
= sd_bus_message_open_container(m
, 'a', "(sv)");
672 return bus_log_create_error(r
);
674 r
= transient_mount_set_properties(m
);
676 return bus_log_create_error(r
);
678 r
= sd_bus_message_close_container(m
);
680 return bus_log_create_error(r
);
682 r
= sd_bus_message_close_container(m
);
684 return bus_log_create_error(r
);
686 r
= sd_bus_message_close_container(m
);
688 return bus_log_create_error(r
);
690 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
692 r
= sd_bus_call(bus
, m
, 0, &error
, &reply
);
694 return log_error_errno(r
, "Failed to start transient automount unit: %s", bus_error_message(&error
, r
));
699 r
= sd_bus_message_read(reply
, "o", &object
);
701 return bus_log_parse_error(r
);
703 r
= bus_wait_for_jobs_one(w
, object
, arg_quiet
);
709 log_info("Started unit %s%s%s for mount point: %s%s%s",
710 ansi_highlight(), automount_unit
, ansi_normal(),
711 ansi_highlight(), arg_mount_where
, ansi_normal());
716 static int find_mount_points(const char *what
, char ***list
) {
717 _cleanup_(mnt_free_tablep
) struct libmnt_table
*table
= NULL
;
718 _cleanup_(mnt_free_iterp
) struct libmnt_iter
*iter
= NULL
;
719 _cleanup_strv_free_
char **l
= NULL
;
720 size_t bufsize
= 0, n
= 0;
726 /* Returns all mount points obtained from /proc/self/mountinfo in *list,
727 * and the number of mount points as return value. */
729 r
= libmount_parse(NULL
, NULL
, &table
, &iter
);
731 return log_error_errno(r
, "Failed to parse /proc/self/mountinfo: %m");
734 struct libmnt_fs
*fs
;
735 const char *source
, *target
;
737 r
= mnt_table_next_fs(table
, iter
, &fs
);
741 return log_error_errno(r
, "Failed to get next entry from /proc/self/mountinfo: %m");
743 source
= mnt_fs_get_source(fs
);
744 target
= mnt_fs_get_target(fs
);
745 if (!source
|| !target
)
748 if (!path_equal(source
, what
))
751 /* one extra slot is needed for the terminating NULL */
752 if (!GREEDY_REALLOC0(l
, bufsize
, n
+ 2))
755 l
[n
] = strdup(target
);
761 if (!GREEDY_REALLOC0(l
, bufsize
, n
+ 1))
768 static int find_loop_device(const char *backing_file
, char **loop_dev
) {
769 _cleanup_closedir_
DIR *d
= NULL
;
771 _cleanup_free_
char *l
= NULL
;
773 assert(backing_file
);
776 d
= opendir("/sys/devices/virtual/block");
780 FOREACH_DIRENT(de
, d
, return -errno
) {
781 _cleanup_free_
char *sys
= NULL
, *fname
= NULL
;
784 dirent_ensure_type(d
, de
);
786 if (de
->d_type
!= DT_DIR
)
789 if (!startswith(de
->d_name
, "loop"))
792 sys
= strjoin("/sys/devices/virtual/block/", de
->d_name
, "/loop/backing_file");
796 r
= read_one_line_file(sys
, &fname
);
798 log_debug_errno(r
, "Failed to read %s, ignoring: %m", sys
);
802 if (files_same(fname
, backing_file
, 0) <= 0)
805 l
= strjoin("/dev/", de
->d_name
);
815 *loop_dev
= TAKE_PTR(l
);
820 static int stop_mount(
823 const char *suffix
) {
825 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
, *reply
= NULL
;
826 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
827 _cleanup_(bus_wait_for_jobs_freep
) BusWaitForJobs
*w
= NULL
;
828 _cleanup_free_
char *mount_unit
= NULL
;
832 r
= bus_wait_for_jobs_new(bus
, &w
);
834 return log_error_errno(r
, "Could not watch jobs: %m");
837 r
= unit_name_from_path(where
, suffix
, &mount_unit
);
839 return log_error_errno(r
, "Failed to make %s unit name from path %s: %m", suffix
+ 1, where
);
841 r
= sd_bus_message_new_method_call(
844 "org.freedesktop.systemd1",
845 "/org/freedesktop/systemd1",
846 "org.freedesktop.systemd1.Manager",
849 return bus_log_create_error(r
);
851 r
= sd_bus_message_set_allow_interactive_authorization(m
, arg_ask_password
);
853 return bus_log_create_error(r
);
856 r
= sd_bus_message_append(m
, "ss", mount_unit
, "fail");
858 return bus_log_create_error(r
);
860 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
862 r
= sd_bus_call(bus
, m
, 0, &error
, &reply
);
864 if (streq(suffix
, ".automount") &&
865 sd_bus_error_has_name(&error
, "org.freedesktop.systemd1.NoSuchUnit"))
867 return log_error_errno(r
, "Failed to stop %s unit: %s", suffix
+ 1, bus_error_message(&error
, r
));
873 r
= sd_bus_message_read(reply
, "o", &object
);
875 return bus_log_parse_error(r
);
877 r
= bus_wait_for_jobs_one(w
, object
, arg_quiet
);
883 log_info("Stopped unit %s%s%s for mount point: %s%s%s",
884 ansi_highlight(), mount_unit
, ansi_normal(),
885 ansi_highlight(), where
, ansi_normal());
890 static int stop_mounts(
896 if (path_equal(where
, "/"))
897 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
898 "Refusing to operate on root directory: %s", where
);
900 if (!path_is_normalized(where
))
901 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
902 "Path contains non-normalized components: %s", where
);
904 r
= stop_mount(bus
, where
, ".mount");
908 r
= stop_mount(bus
, where
, ".automount");
915 static int umount_by_device(sd_bus
*bus
, const char *what
) {
916 _cleanup_(sd_device_unrefp
) sd_device
*d
= NULL
;
917 _cleanup_strv_free_
char **list
= NULL
;
925 if (stat(what
, &st
) < 0)
926 return log_error_errno(errno
, "Can't stat %s: %m", what
);
928 if (!S_ISBLK(st
.st_mode
)) {
929 log_error("Not a block device: %s", what
);
933 r
= sd_device_new_from_devnum(&d
, 'b', st
.st_rdev
);
935 return log_error_errno(r
, "Failed to get device from device number: %m");
937 r
= sd_device_get_property_value(d
, "ID_FS_USAGE", &v
);
939 return log_device_error_errno(d
, r
, "Failed to get device property: %m");
941 if (!streq(v
, "filesystem")) {
942 log_device_error(d
, "%s does not contain a known file system.", what
);
946 if (sd_device_get_property_value(d
, "SYSTEMD_MOUNT_WHERE", &v
) >= 0)
947 r2
= stop_mounts(bus
, v
);
949 r
= find_mount_points(what
, &list
);
953 for (l
= list
; *l
; l
++) {
954 r
= stop_mounts(bus
, *l
);
962 static int umount_loop(sd_bus
*bus
, const char *backing_file
) {
963 _cleanup_free_
char *loop_dev
= NULL
;
966 assert(backing_file
);
968 r
= find_loop_device(backing_file
, &loop_dev
);
970 return log_error_errno(r
, r
== -ENXIO
? "File %s is not mounted." : "Can't get loop device for %s: %m", backing_file
);
972 return umount_by_device(bus
, loop_dev
);
975 static int action_umount(
982 if (arg_transport
!= BUS_TRANSPORT_LOCAL
) {
983 for (i
= optind
; i
< argc
; i
++) {
984 _cleanup_free_
char *p
= NULL
;
990 path_simplify(p
, false);
992 r
= stop_mounts(bus
, p
);
999 for (i
= optind
; i
< argc
; i
++) {
1000 _cleanup_free_
char *u
= NULL
, *p
= NULL
;
1003 u
= fstab_node_to_udev_node(argv
[i
]);
1007 r
= chase_symlinks(u
, NULL
, 0, &p
);
1009 r2
= log_error_errno(r
, "Failed to make path %s absolute: %m", argv
[i
]);
1013 if (stat(p
, &st
) < 0)
1014 return log_error_errno(errno
, "Can't stat %s (from %s): %m", p
, argv
[i
]);
1016 if (S_ISBLK(st
.st_mode
))
1017 r
= umount_by_device(bus
, p
);
1018 else if (S_ISREG(st
.st_mode
))
1019 r
= umount_loop(bus
, p
);
1020 else if (S_ISDIR(st
.st_mode
))
1021 r
= stop_mounts(bus
, p
);
1023 log_error("Invalid file type: %s (from %s)", p
, argv
[i
]);
1034 static int acquire_mount_type(sd_device
*d
) {
1042 if (sd_device_get_property_value(d
, "ID_FS_TYPE", &v
) < 0)
1045 arg_mount_type
= strdup(v
);
1046 if (!arg_mount_type
)
1049 log_debug("Discovered type=%s", arg_mount_type
);
1053 static int acquire_mount_options(sd_device
*d
) {
1058 if (arg_mount_options
)
1061 if (sd_device_get_property_value(d
, "SYSTEMD_MOUNT_OPTIONS", &v
) < 0)
1064 arg_mount_options
= strdup(v
);
1065 if (!arg_mount_options
)
1068 log_debug("Discovered options=%s", arg_mount_options
);
1072 static const char *get_model(sd_device
*d
) {
1077 if (sd_device_get_property_value(d
, "ID_MODEL_FROM_DATABASE", &model
) >= 0)
1080 if (sd_device_get_property_value(d
, "ID_MODEL", &model
) >= 0)
1086 static const char* get_label(sd_device
*d
) {
1091 if (sd_device_get_property_value(d
, "ID_FS_LABEL", &label
) >= 0)
1094 if (sd_device_get_property_value(d
, "ID_PART_ENTRY_NAME", &label
) >= 0)
1100 static int acquire_mount_where(sd_device
*d
) {
1103 if (arg_mount_where
)
1106 if (sd_device_get_property_value(d
, "SYSTEMD_MOUNT_WHERE", &v
) < 0) {
1107 _cleanup_free_
char *escaped
= NULL
;
1110 name
= get_label(d
);
1112 name
= get_model(d
);
1116 if (sd_device_get_devname(d
, &dn
) < 0)
1119 name
= basename(dn
);
1122 escaped
= xescape(name
, "\\");
1125 if (!filename_is_valid(escaped
))
1128 arg_mount_where
= strjoin("/run/media/system/", escaped
);
1130 arg_mount_where
= strdup(v
);
1132 if (!arg_mount_where
)
1135 log_debug("Discovered where=%s", arg_mount_where
);
1139 static int acquire_mount_where_for_loop_dev(const char *loop_dev
) {
1140 _cleanup_strv_free_
char **list
= NULL
;
1143 if (arg_mount_where
)
1146 r
= find_mount_points(loop_dev
, &list
);
1150 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1151 "Can't find mount point of %s. It is expected that %s is already mounted on a place.",
1152 loop_dev
, loop_dev
);
1154 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1155 "%s is mounted on %d places. It is expected that %s is mounted on a place.",
1156 loop_dev
, r
, loop_dev
);
1158 arg_mount_where
= strdup(list
[0]);
1159 if (!arg_mount_where
)
1162 log_debug("Discovered where=%s", arg_mount_where
);
1166 static int acquire_description(sd_device
*d
) {
1167 const char *model
, *label
;
1169 if (arg_description
)
1172 model
= get_model(d
);
1174 label
= get_label(d
);
1176 (void) sd_device_get_property_value(d
, "ID_PART_ENTRY_NUMBER", &label
);
1179 arg_description
= strjoin(model
, " ", label
);
1181 arg_description
= strdup(label
);
1183 arg_description
= strdup(model
);
1187 if (!arg_description
)
1190 log_debug("Discovered description=%s", arg_description
);
1194 static int acquire_removable(sd_device
*d
) {
1197 /* Shortcut this if there's no reason to check it */
1198 if (arg_action
!= ACTION_DEFAULT
&& arg_timeout_idle_set
&& arg_bind_device
>= 0)
1202 if (sd_device_get_sysattr_value(d
, "removable", &v
) > 0)
1205 if (sd_device_get_parent(d
, &d
) < 0)
1208 if (sd_device_get_subsystem(d
, &v
) < 0 || !streq(v
, "block"))
1212 if (parse_boolean(v
) <= 0)
1215 log_debug("Discovered removable device.");
1217 if (arg_action
== ACTION_DEFAULT
) {
1218 log_debug("Automatically turning on automount.");
1219 arg_action
= ACTION_AUTOMOUNT
;
1222 if (!arg_timeout_idle_set
) {
1223 log_debug("Setting idle timeout to 1s.");
1224 arg_timeout_idle
= USEC_PER_SEC
;
1227 if (arg_bind_device
< 0) {
1228 log_debug("Binding automount unit to device.");
1229 arg_bind_device
= true;
1235 static int discover_loop_backing_file(void) {
1236 _cleanup_(sd_device_unrefp
) sd_device
*d
= NULL
;
1237 _cleanup_free_
char *loop_dev
= NULL
;
1242 r
= find_loop_device(arg_mount_what
, &loop_dev
);
1243 if (r
< 0 && r
!= -ENXIO
)
1244 return log_error_errno(errno
, "Can't get loop device for %s: %m", arg_mount_what
);
1247 _cleanup_free_
char *escaped
= NULL
;
1249 if (arg_mount_where
)
1252 escaped
= xescape(basename(arg_mount_what
), "\\");
1255 if (!filename_is_valid(escaped
)) {
1256 log_error("Escaped name %s is not a valid filename.", escaped
);
1260 arg_mount_where
= strjoin("/run/media/system/", escaped
);
1261 if (!arg_mount_where
)
1264 log_debug("Discovered where=%s", arg_mount_where
);
1268 if (stat(loop_dev
, &st
) < 0)
1269 return log_error_errno(errno
, "Can't stat %s: %m", loop_dev
);
1271 if (!S_ISBLK(st
.st_mode
)) {
1272 log_error("Invalid file type: %s", loop_dev
);
1276 r
= sd_device_new_from_devnum(&d
, 'b', st
.st_rdev
);
1278 return log_error_errno(r
, "Failed to get device from device number: %m");
1280 if (sd_device_get_property_value(d
, "ID_FS_USAGE", &v
) < 0 || !streq(v
, "filesystem")) {
1281 log_device_error(d
, "%s does not contain a known file system.", arg_mount_what
);
1285 r
= acquire_mount_type(d
);
1289 r
= acquire_mount_options(d
);
1293 r
= acquire_mount_where_for_loop_dev(loop_dev
);
1297 r
= acquire_description(d
);
1304 static int discover_device(void) {
1305 _cleanup_(sd_device_unrefp
) sd_device
*d
= NULL
;
1310 if (stat(arg_mount_what
, &st
) < 0)
1311 return log_error_errno(errno
, "Can't stat %s: %m", arg_mount_what
);
1313 if (S_ISREG(st
.st_mode
))
1314 return discover_loop_backing_file();
1316 if (!S_ISBLK(st
.st_mode
)) {
1317 log_error("Invalid file type: %s", arg_mount_what
);
1321 r
= sd_device_new_from_devnum(&d
, 'b', st
.st_rdev
);
1323 return log_error_errno(r
, "Failed to get device from device number: %m");
1325 if (sd_device_get_property_value(d
, "ID_FS_USAGE", &v
) < 0 || !streq(v
, "filesystem")) {
1326 log_error("%s does not contain a known file system.", arg_mount_what
);
1330 r
= acquire_mount_type(d
);
1334 r
= acquire_mount_options(d
);
1338 r
= acquire_mount_where(d
);
1342 r
= acquire_description(d
);
1346 r
= acquire_removable(d
);
1365 char* columns
[_COLUMN_MAX
];
1368 static int compare_item(const struct item
*a
, const struct item
*b
) {
1369 if (a
->columns
[COLUMN_NODE
] == b
->columns
[COLUMN_NODE
])
1371 if (!a
->columns
[COLUMN_NODE
])
1373 if (!b
->columns
[COLUMN_NODE
])
1376 return path_compare(a
->columns
[COLUMN_NODE
], b
->columns
[COLUMN_NODE
]);
1379 static int list_devices(void) {
1381 static const char * const titles
[_COLUMN_MAX
] = {
1382 [COLUMN_NODE
] = "NODE",
1383 [COLUMN_PATH
] = "PATH",
1384 [COLUMN_MODEL
] = "MODEL",
1385 [COLUMN_WWN
] = "WWN",
1386 [COLUMN_FSTYPE
] = "TYPE",
1387 [COLUMN_LABEL
] = "LABEL",
1388 [COLUMN_UUID
] = "UUID"
1391 _cleanup_(sd_device_enumerator_unrefp
) sd_device_enumerator
*e
= NULL
;
1392 size_t n_allocated
= 0, n
= 0, i
;
1393 size_t column_width
[_COLUMN_MAX
];
1394 struct item
*items
= NULL
;
1399 for (c
= 0; c
< _COLUMN_MAX
; c
++)
1400 column_width
[c
] = strlen(titles
[c
]);
1402 r
= sd_device_enumerator_new(&e
);
1406 r
= sd_device_enumerator_add_match_subsystem(e
, "block", true);
1408 return log_error_errno(r
, "Failed to add block match: %m");
1410 r
= sd_device_enumerator_add_match_property(e
, "ID_FS_USAGE", "filesystem");
1412 return log_error_errno(r
, "Failed to add property match: %m");
1414 FOREACH_DEVICE(e
, d
) {
1417 if (!GREEDY_REALLOC0(items
, n_allocated
, n
+1)) {
1424 for (c
= 0; c
< _COLUMN_MAX
; c
++) {
1425 const char *x
= NULL
;
1431 (void) sd_device_get_devname(d
, &x
);
1435 (void) sd_device_get_property_value(d
, "ID_PATH", &x
);
1443 (void) sd_device_get_property_value(d
, "ID_WWN", &x
);
1447 (void) sd_device_get_property_value(d
, "ID_FS_TYPE", &x
);
1455 (void) sd_device_get_property_value(d
, "ID_FS_UUID", &x
);
1462 j
->columns
[c
] = strdup(x
);
1463 if (!j
->columns
[c
]) {
1469 if (k
> column_width
[c
])
1470 column_width
[c
] = k
;
1475 log_info("No devices found.");
1479 typesafe_qsort(items
, n
, compare_item
);
1481 (void) pager_open(arg_pager_flags
);
1483 fputs(ansi_underline(), stdout
);
1484 for (c
= 0; c
< _COLUMN_MAX
; c
++) {
1488 printf("%-*s", (int) column_width
[c
], titles
[c
]);
1490 fputs(ansi_normal(), stdout
);
1491 fputc('\n', stdout
);
1493 for (i
= 0; i
< n
; i
++) {
1494 for (c
= 0; c
< _COLUMN_MAX
; c
++) {
1498 printf("%-*s", (int) column_width
[c
], strna(items
[i
].columns
[c
]));
1500 fputc('\n', stdout
);
1506 for (i
= 0; i
< n
; i
++)
1507 for (c
= 0; c
< _COLUMN_MAX
; c
++)
1508 free(items
[i
].columns
[c
]);
1514 static int run(int argc
, char* argv
[]) {
1515 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1518 log_show_color(true);
1519 log_parse_environment();
1522 r
= parse_argv(argc
, argv
);
1526 if (arg_action
== ACTION_LIST
)
1527 return list_devices();
1529 r
= bus_connect_transport_systemd(arg_transport
, arg_host
, arg_user
, &bus
);
1531 return log_error_errno(r
, "Failed to create bus connection: %m");
1533 if (arg_action
== ACTION_UMOUNT
)
1534 return action_umount(bus
, argc
, argv
);
1536 if (!path_is_normalized(arg_mount_what
)) {
1537 log_error("Path contains non-normalized components: %s", arg_mount_what
);
1542 r
= discover_device();
1547 if (!arg_mount_where
) {
1548 log_error("Can't figure out where to mount %s.", arg_mount_what
);
1552 if (path_equal(arg_mount_where
, "/")) {
1553 log_error("Refusing to operate on root directory.");
1557 if (!path_is_normalized(arg_mount_where
)) {
1558 log_error("Path contains non-normalized components: %s", arg_mount_where
);
1562 if (streq_ptr(arg_mount_type
, "auto"))
1563 arg_mount_type
= mfree(arg_mount_type
);
1564 if (streq_ptr(arg_mount_options
, "defaults"))
1565 arg_mount_options
= mfree(arg_mount_options
);
1567 if (!is_device_path(arg_mount_what
))
1570 if (arg_fsck
&& arg_mount_type
&& arg_transport
== BUS_TRANSPORT_LOCAL
) {
1571 r
= fsck_exists(arg_mount_type
);
1573 log_warning_errno(r
, "Couldn't determine whether fsck for %s exists, proceeding anyway.", arg_mount_type
);
1575 log_debug("Disabling file system check as fsck for %s doesn't exist.", arg_mount_type
);
1576 arg_fsck
= false; /* fsck doesn't exist, let's not attempt it */
1580 /* The kernel (properly) refuses mounting file systems with unknown uid=,gid= options,
1581 * but not for all filesystem types. Let's try to catch the cases where the option
1582 * would be used if the file system does not support it. It is also possible to
1583 * autodetect the file system, but that's only possible with disk-based file systems
1584 * which incidentally seem to be implemented more carefully and reject unknown options,
1585 * so it's probably OK that we do the check only when the type is specified.
1587 if (arg_mount_type
&&
1588 !streq(arg_mount_type
, "auto") &&
1589 arg_uid
!= UID_INVALID
&&
1590 !fstype_can_uid_gid(arg_mount_type
)) {
1591 log_error("File system type %s is not known to support uid=/gid=, refusing.",
1596 switch (arg_action
) {
1599 case ACTION_DEFAULT
:
1600 r
= start_transient_mount(bus
, argv
+ optind
);
1603 case ACTION_AUTOMOUNT
:
1604 r
= start_transient_automount(bus
, argv
+ optind
);
1608 assert_not_reached("Unexpected action.");
1614 DEFINE_MAIN_FUNCTION(run
);