1 /* SPDX-License-Identifier: LGPL-2.1+ */
9 #include "bus-unit-util.h"
11 #include "dirent-util.h"
16 #include "fstab-util.h"
17 #include "mount-util.h"
19 #include "parse-util.h"
20 #include "path-util.h"
21 #include "spawn-polkit-agent.h"
22 #include "stat-util.h"
24 #include "udev-util.h"
26 #include "unit-name.h"
27 #include "user-util.h"
28 #include "terminal-util.h"
36 } arg_action
= ACTION_DEFAULT
;
38 static bool arg_no_block
= false;
39 static bool arg_no_pager
= false;
40 static bool arg_ask_password
= true;
41 static bool arg_quiet
= false;
42 static BusTransport arg_transport
= BUS_TRANSPORT_LOCAL
;
43 static bool arg_user
= false;
44 static const char *arg_host
= NULL
;
45 static bool arg_discover
= false;
46 static char *arg_mount_what
= NULL
;
47 static char *arg_mount_where
= NULL
;
48 static char *arg_mount_type
= NULL
;
49 static char *arg_mount_options
= NULL
;
50 static char *arg_description
= NULL
;
51 static char **arg_property
= NULL
;
52 static usec_t arg_timeout_idle
= USEC_INFINITY
;
53 static bool arg_timeout_idle_set
= false;
54 static char **arg_automount_property
= NULL
;
55 static int arg_bind_device
= -1;
56 static uid_t arg_uid
= UID_INVALID
;
57 static gid_t arg_gid
= GID_INVALID
;
58 static bool arg_fsck
= true;
59 static bool arg_aggressive_gc
= false;
61 static void help(void) {
62 printf("systemd-mount [OPTIONS...] WHAT [WHERE]\n"
63 "systemd-mount [OPTIONS...] --list\n"
64 "%s [OPTIONS...] %sWHAT|WHERE...\n\n"
65 "Establish a mount or auto-mount point transiently.\n\n"
66 " -h --help Show this help\n"
67 " --version Show package version\n"
68 " --no-block Do not wait until operation finished\n"
69 " --no-pager Do not pipe output into a pager\n"
70 " --no-ask-password Do not prompt for password\n"
71 " -q --quiet Suppress information messages during runtime\n"
72 " --user Run as user unit\n"
73 " -H --host=[USER@]HOST Operate on remote host\n"
74 " -M --machine=CONTAINER Operate on local container\n"
75 " --discover Discover mount device metadata\n"
76 " -t --type=TYPE File system type\n"
77 " -o --options=OPTIONS Mount options\n"
78 " --owner=USER Add uid= and gid= options for USER\n"
79 " --fsck=no Don't run file system check before mount\n"
80 " --description=TEXT Description for unit\n"
81 " -p --property=NAME=VALUE Set mount unit property\n"
82 " -A --automount=BOOL Create an auto-mount point\n"
83 " --timeout-idle-sec=SEC Specify automount idle timeout\n"
84 " --automount-property=NAME=VALUE\n"
85 " Set automount unit property\n"
86 " --bind-device Bind automount unit to device\n"
87 " --list List mountable block devices\n"
88 " -u --umount Unmount mount points\n"
89 " -G --collect Unload unit after it stopped, even when failed\n",
90 program_invocation_short_name
,
91 streq(program_invocation_short_name
, "systemd-umount") ? "" : "--umount ");
94 static int parse_argv(int argc
, char *argv
[]) {
111 ARG_AUTOMOUNT_PROPERTY
,
116 static const struct option options
[] = {
117 { "help", no_argument
, NULL
, 'h' },
118 { "version", no_argument
, NULL
, ARG_VERSION
},
119 { "no-block", no_argument
, NULL
, ARG_NO_BLOCK
},
120 { "no-pager", no_argument
, NULL
, ARG_NO_PAGER
},
121 { "no-ask-password", no_argument
, NULL
, ARG_NO_ASK_PASSWORD
},
122 { "quiet", no_argument
, NULL
, 'q' },
123 { "user", no_argument
, NULL
, ARG_USER
},
124 { "system", no_argument
, NULL
, ARG_SYSTEM
},
125 { "host", required_argument
, NULL
, 'H' },
126 { "machine", required_argument
, NULL
, 'M' },
127 { "discover", no_argument
, NULL
, ARG_DISCOVER
},
128 { "type", required_argument
, NULL
, 't' },
129 { "options", required_argument
, NULL
, 'o' },
130 { "owner", required_argument
, NULL
, ARG_OWNER
},
131 { "fsck", required_argument
, NULL
, ARG_FSCK
},
132 { "description", required_argument
, NULL
, ARG_DESCRIPTION
},
133 { "property", required_argument
, NULL
, 'p' },
134 { "automount", required_argument
, NULL
, ARG_AUTOMOUNT
},
135 { "timeout-idle-sec", required_argument
, NULL
, ARG_TIMEOUT_IDLE
},
136 { "automount-property", required_argument
, NULL
, ARG_AUTOMOUNT_PROPERTY
},
137 { "bind-device", no_argument
, NULL
, ARG_BIND_DEVICE
},
138 { "list", no_argument
, NULL
, ARG_LIST
},
139 { "umount", no_argument
, NULL
, 'u' },
140 { "unmount", no_argument
, NULL
, 'u' },
141 { "collect", no_argument
, NULL
, 'G' },
150 if (strstr(program_invocation_short_name
, "systemd-umount"))
151 arg_action
= ACTION_UMOUNT
;
153 while ((c
= getopt_long(argc
, argv
, "hqH:M:t:o:p:AuG", options
, NULL
)) >= 0)
172 case ARG_NO_ASK_PASSWORD
:
173 arg_ask_password
= false;
189 arg_transport
= BUS_TRANSPORT_REMOTE
;
194 arg_transport
= BUS_TRANSPORT_MACHINE
;
203 if (free_and_strdup(&arg_mount_type
, optarg
) < 0)
208 if (free_and_strdup(&arg_mount_options
, optarg
) < 0)
213 const char *user
= optarg
;
215 r
= get_user_creds(&user
, &arg_uid
, &arg_gid
, NULL
, NULL
);
217 return log_error_errno(r
,
218 r
== -EBADMSG
? "UID or GID of user %s are invalid."
219 : "Cannot use \"%s\" as owner: %m",
225 r
= parse_boolean(optarg
);
227 return log_error_errno(r
, "Failed to parse --fsck= argument: %s", optarg
);
232 case ARG_DESCRIPTION
:
233 if (free_and_strdup(&arg_description
, optarg
) < 0)
238 if (strv_extend(&arg_property
, optarg
) < 0)
244 arg_action
= ACTION_AUTOMOUNT
;
248 r
= parse_boolean(optarg
);
250 return log_error_errno(r
, "--automount= expects a valid boolean parameter: %s", optarg
);
252 arg_action
= r
? ACTION_AUTOMOUNT
: ACTION_MOUNT
;
255 case ARG_TIMEOUT_IDLE
:
256 r
= parse_sec(optarg
, &arg_timeout_idle
);
258 return log_error_errno(r
, "Failed to parse timeout: %s", optarg
);
262 case ARG_AUTOMOUNT_PROPERTY
:
263 if (strv_extend(&arg_automount_property
, optarg
) < 0)
268 case ARG_BIND_DEVICE
:
269 arg_bind_device
= true;
273 arg_action
= ACTION_LIST
;
277 arg_action
= ACTION_UMOUNT
;
281 arg_aggressive_gc
= true;
288 assert_not_reached("Unhandled option");
291 if (arg_user
&& arg_transport
!= BUS_TRANSPORT_LOCAL
) {
292 log_error("Execution in user context is not supported on non-local systems.");
296 if (arg_action
== ACTION_LIST
) {
298 log_error("Too many arguments.");
302 if (arg_transport
!= BUS_TRANSPORT_LOCAL
) {
303 log_error("Listing devices only supported locally.");
306 } else if (arg_action
== ACTION_UMOUNT
) {
307 if (optind
>= argc
) {
308 log_error("At least one argument required.");
312 if (arg_transport
!= BUS_TRANSPORT_LOCAL
) {
315 for (i
= optind
; i
< argc
; i
++)
316 if (!path_is_absolute(argv
[i
]) ) {
317 log_error("Only absolute path is supported: %s", argv
[i
]);
322 if (optind
>= argc
) {
323 log_error("At least one argument required.");
327 if (argc
> optind
+2) {
328 log_error("At most two arguments required.");
332 if (arg_mount_type
&& (fstype_is_api_vfs(arg_mount_type
) || fstype_is_network(arg_mount_type
))) {
333 arg_mount_what
= strdup(argv
[optind
]);
337 } else if (arg_transport
== BUS_TRANSPORT_LOCAL
) {
338 _cleanup_free_
char *u
= NULL
;
340 u
= fstab_node_to_udev_node(argv
[optind
]);
344 r
= chase_symlinks(u
, NULL
, 0, &arg_mount_what
);
346 return log_error_errno(r
, "Failed to make path %s absolute: %m", u
);
348 arg_mount_what
= strdup(argv
[optind
]);
352 path_simplify(arg_mount_what
, false);
354 if (!path_is_absolute(arg_mount_what
)) {
355 log_error("Only absolute path is supported: %s", arg_mount_what
);
360 if (argc
> optind
+1) {
361 if (arg_transport
== BUS_TRANSPORT_LOCAL
) {
362 r
= chase_symlinks(argv
[optind
+1], NULL
, CHASE_NONEXISTENT
, &arg_mount_where
);
364 return log_error_errno(r
, "Failed to make path %s absolute: %m", argv
[optind
+1]);
366 arg_mount_where
= strdup(argv
[optind
+1]);
367 if (!arg_mount_where
)
370 path_simplify(arg_mount_where
, false);
372 if (!path_is_absolute(arg_mount_where
)) {
373 log_error("Only absolute path is supported: %s", arg_mount_where
);
380 if (arg_discover
&& arg_transport
!= BUS_TRANSPORT_LOCAL
) {
381 log_error("Automatic mount location discovery is only supported locally.");
389 static int transient_unit_set_properties(sd_bus_message
*m
, UnitType t
, char **properties
) {
392 if (!isempty(arg_description
)) {
393 r
= sd_bus_message_append(m
, "(sv)", "Description", "s", arg_description
);
398 if (arg_bind_device
&& is_device_path(arg_mount_what
)) {
399 _cleanup_free_
char *device_unit
= NULL
;
401 r
= unit_name_from_path(arg_mount_what
, ".device", &device_unit
);
405 r
= sd_bus_message_append(m
, "(sv)(sv)",
406 "After", "as", 1, device_unit
,
407 "BindsTo", "as", 1, device_unit
);
412 if (arg_aggressive_gc
) {
413 r
= sd_bus_message_append(m
, "(sv)", "CollectMode", "s", "inactive-or-failed");
418 r
= bus_append_unit_property_assignment_many(m
, t
, properties
);
425 static int transient_mount_set_properties(sd_bus_message
*m
) {
426 _cleanup_free_
char *options
= NULL
;
431 r
= transient_unit_set_properties(m
, UNIT_MOUNT
, arg_property
);
435 if (arg_mount_what
) {
436 r
= sd_bus_message_append(m
, "(sv)", "What", "s", arg_mount_what
);
441 if (arg_mount_type
) {
442 r
= sd_bus_message_append(m
, "(sv)", "Type", "s", arg_mount_type
);
447 /* Prepend uid=…,gid=… if arg_uid is set */
448 if (arg_uid
!= UID_INVALID
) {
449 r
= asprintf(&options
,
450 "uid=" UID_FMT
",gid=" GID_FMT
"%s%s",
452 arg_mount_options
? "," : "", strempty(arg_mount_options
));
457 if (options
|| arg_mount_options
) {
458 log_debug("Using mount options: %s", options
?: arg_mount_options
);
460 r
= sd_bus_message_append(m
, "(sv)", "Options", "s", options
?: arg_mount_options
);
464 log_debug("Not using any mount options");
467 _cleanup_free_
char *fsck
= NULL
;
469 r
= unit_name_from_path_instance("systemd-fsck", arg_mount_what
, ".service", &fsck
);
473 r
= sd_bus_message_append(m
,
475 "Requires", "as", 1, fsck
,
476 "After", "as", 1, fsck
);
484 static int transient_automount_set_properties(sd_bus_message
*m
) {
489 r
= transient_unit_set_properties(m
, UNIT_AUTOMOUNT
, arg_automount_property
);
493 if (arg_timeout_idle
!= USEC_INFINITY
) {
494 r
= sd_bus_message_append(m
, "(sv)", "TimeoutIdleUSec", "t", arg_timeout_idle
);
502 static int start_transient_mount(
506 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
, *reply
= NULL
;
507 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
508 _cleanup_(bus_wait_for_jobs_freep
) BusWaitForJobs
*w
= NULL
;
509 _cleanup_free_
char *mount_unit
= NULL
;
513 r
= bus_wait_for_jobs_new(bus
, &w
);
515 return log_error_errno(r
, "Could not watch jobs: %m");
518 r
= unit_name_from_path(arg_mount_where
, ".mount", &mount_unit
);
520 return log_error_errno(r
, "Failed to make mount unit name: %m");
522 r
= sd_bus_message_new_method_call(
525 "org.freedesktop.systemd1",
526 "/org/freedesktop/systemd1",
527 "org.freedesktop.systemd1.Manager",
528 "StartTransientUnit");
530 return bus_log_create_error(r
);
532 r
= sd_bus_message_set_allow_interactive_authorization(m
, arg_ask_password
);
534 return bus_log_create_error(r
);
537 r
= sd_bus_message_append(m
, "ss", mount_unit
, "fail");
539 return bus_log_create_error(r
);
542 r
= sd_bus_message_open_container(m
, 'a', "(sv)");
544 return bus_log_create_error(r
);
546 r
= transient_mount_set_properties(m
);
548 return bus_log_create_error(r
);
550 r
= sd_bus_message_close_container(m
);
552 return bus_log_create_error(r
);
554 /* Auxiliary units */
555 r
= sd_bus_message_append(m
, "a(sa(sv))", 0);
557 return bus_log_create_error(r
);
559 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
561 r
= sd_bus_call(bus
, m
, 0, &error
, &reply
);
563 return log_error_errno(r
, "Failed to start transient mount unit: %s", bus_error_message(&error
, r
));
568 r
= sd_bus_message_read(reply
, "o", &object
);
570 return bus_log_parse_error(r
);
572 r
= bus_wait_for_jobs_one(w
, object
, arg_quiet
);
578 log_info("Started unit %s%s%s for mount point: %s%s%s",
579 ansi_highlight(), mount_unit
, ansi_normal(),
580 ansi_highlight(), arg_mount_where
, ansi_normal());
585 static int start_transient_automount(
589 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
, *reply
= NULL
;
590 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
591 _cleanup_(bus_wait_for_jobs_freep
) BusWaitForJobs
*w
= NULL
;
592 _cleanup_free_
char *automount_unit
= NULL
, *mount_unit
= NULL
;
596 r
= bus_wait_for_jobs_new(bus
, &w
);
598 return log_error_errno(r
, "Could not watch jobs: %m");
601 r
= unit_name_from_path(arg_mount_where
, ".automount", &automount_unit
);
603 return log_error_errno(r
, "Failed to make automount unit name: %m");
605 r
= unit_name_from_path(arg_mount_where
, ".mount", &mount_unit
);
607 return log_error_errno(r
, "Failed to make mount unit name: %m");
609 r
= sd_bus_message_new_method_call(
612 "org.freedesktop.systemd1",
613 "/org/freedesktop/systemd1",
614 "org.freedesktop.systemd1.Manager",
615 "StartTransientUnit");
617 return bus_log_create_error(r
);
619 r
= sd_bus_message_set_allow_interactive_authorization(m
, arg_ask_password
);
621 return bus_log_create_error(r
);
624 r
= sd_bus_message_append(m
, "ss", automount_unit
, "fail");
626 return bus_log_create_error(r
);
629 r
= sd_bus_message_open_container(m
, 'a', "(sv)");
631 return bus_log_create_error(r
);
633 r
= transient_automount_set_properties(m
);
635 return bus_log_create_error(r
);
637 r
= sd_bus_message_close_container(m
);
639 return bus_log_create_error(r
);
641 /* Auxiliary units */
642 r
= sd_bus_message_open_container(m
, 'a', "(sa(sv))");
644 return bus_log_create_error(r
);
646 r
= sd_bus_message_open_container(m
, 'r', "sa(sv)");
648 return bus_log_create_error(r
);
650 r
= sd_bus_message_append(m
, "s", mount_unit
);
652 return bus_log_create_error(r
);
654 r
= sd_bus_message_open_container(m
, 'a', "(sv)");
656 return bus_log_create_error(r
);
658 r
= transient_mount_set_properties(m
);
660 return bus_log_create_error(r
);
662 r
= sd_bus_message_close_container(m
);
664 return bus_log_create_error(r
);
666 r
= sd_bus_message_close_container(m
);
668 return bus_log_create_error(r
);
670 r
= sd_bus_message_close_container(m
);
672 return bus_log_create_error(r
);
674 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
676 r
= sd_bus_call(bus
, m
, 0, &error
, &reply
);
678 return log_error_errno(r
, "Failed to start transient automount unit: %s", bus_error_message(&error
, r
));
683 r
= sd_bus_message_read(reply
, "o", &object
);
685 return bus_log_parse_error(r
);
687 r
= bus_wait_for_jobs_one(w
, object
, arg_quiet
);
693 log_info("Started unit %s%s%s for mount point: %s%s%s",
694 ansi_highlight(), automount_unit
, ansi_normal(),
695 ansi_highlight(), arg_mount_where
, ansi_normal());
700 static int find_mount_points(const char *what
, char ***list
) {
701 _cleanup_fclose_
FILE *proc_self_mountinfo
= NULL
;
702 _cleanup_strv_free_
char **l
= NULL
;
703 size_t bufsize
= 0, n
= 0;
708 /* Returns all mount points obtained from /proc/self/mountinfo in *list,
709 * and the number of mount points as return value. */
711 proc_self_mountinfo
= fopen("/proc/self/mountinfo", "re");
712 if (!proc_self_mountinfo
)
713 return log_error_errno(errno
, "Can't open /proc/self/mountinfo: %m");
716 _cleanup_free_
char *path
= NULL
, *where
= NULL
, *dev
= NULL
;
719 r
= fscanf(proc_self_mountinfo
,
720 "%*s " /* (1) mount id */
721 "%*s " /* (2) parent id */
722 "%*s " /* (3) major:minor */
723 "%*s " /* (4) root */
724 "%ms " /* (5) mount point */
725 "%*s" /* (6) mount options */
726 "%*[^-]" /* (7) optional fields */
727 "- " /* (8) separator */
728 "%*s " /* (9) file system type */
729 "%ms" /* (10) mount source */
730 "%*s" /* (11) mount options 2 */
731 "%*[^\n]", /* some rubbish at the end */
740 if (!streq(what
, dev
))
743 r
= cunescape(path
, UNESCAPE_RELAX
, &where
);
747 /* one extra slot is needed for the terminating NULL */
748 if (!GREEDY_REALLOC(l
, bufsize
, n
+ 2))
751 l
[n
++] = TAKE_PTR(where
);
754 if (!GREEDY_REALLOC(l
, bufsize
, n
+ 1))
763 static int find_loop_device(const char *backing_file
, char **loop_dev
) {
764 _cleanup_closedir_
DIR *d
= NULL
;
766 _cleanup_free_
char *l
= NULL
;
768 assert(backing_file
);
771 d
= opendir("/sys/devices/virtual/block");
775 FOREACH_DIRENT(de
, d
, return -errno
) {
776 _cleanup_free_
char *sys
= NULL
, *fname
= NULL
;
779 dirent_ensure_type(d
, de
);
781 if (de
->d_type
!= DT_DIR
)
784 if (!startswith(de
->d_name
, "loop"))
787 sys
= strjoin("/sys/devices/virtual/block/", de
->d_name
, "/loop/backing_file");
791 r
= read_one_line_file(sys
, &fname
);
793 log_debug_errno(r
, "Failed to read %s, ignoring: %m", sys
);
797 if (files_same(fname
, backing_file
, 0) <= 0)
800 l
= strjoin("/dev/", de
->d_name
);
810 *loop_dev
= TAKE_PTR(l
);
815 static int stop_mount(
818 const char *suffix
) {
820 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
, *reply
= NULL
;
821 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
822 _cleanup_(bus_wait_for_jobs_freep
) BusWaitForJobs
*w
= NULL
;
823 _cleanup_free_
char *mount_unit
= NULL
;
827 r
= bus_wait_for_jobs_new(bus
, &w
);
829 return log_error_errno(r
, "Could not watch jobs: %m");
832 r
= unit_name_from_path(where
, suffix
, &mount_unit
);
834 return log_error_errno(r
, "Failed to make %s unit name from path %s: %m", suffix
+ 1, where
);
836 r
= sd_bus_message_new_method_call(
839 "org.freedesktop.systemd1",
840 "/org/freedesktop/systemd1",
841 "org.freedesktop.systemd1.Manager",
844 return bus_log_create_error(r
);
846 r
= sd_bus_message_set_allow_interactive_authorization(m
, arg_ask_password
);
848 return bus_log_create_error(r
);
851 r
= sd_bus_message_append(m
, "ss", mount_unit
, "fail");
853 return bus_log_create_error(r
);
855 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
857 r
= sd_bus_call(bus
, m
, 0, &error
, &reply
);
859 if (streq(suffix
, ".automount") &&
860 sd_bus_error_has_name(&error
, "org.freedesktop.systemd1.NoSuchUnit"))
862 return log_error_errno(r
, "Failed to stop %s unit: %s", suffix
+ 1, bus_error_message(&error
, r
));
868 r
= sd_bus_message_read(reply
, "o", &object
);
870 return bus_log_parse_error(r
);
872 r
= bus_wait_for_jobs_one(w
, object
, arg_quiet
);
878 log_info("Stopped unit %s%s%s for mount point: %s%s%s",
879 ansi_highlight(), mount_unit
, ansi_normal(),
880 ansi_highlight(), where
, ansi_normal());
885 static int stop_mounts(
891 if (path_equal(where
, "/")) {
892 log_error("Refusing to operate on root directory: %s", where
);
896 if (!path_is_normalized(where
)) {
897 log_error("Path contains non-normalized components: %s", where
);
901 r
= stop_mount(bus
, where
, ".mount");
905 r
= stop_mount(bus
, where
, ".automount");
912 static int umount_by_device(sd_bus
*bus
, const char *what
) {
913 _cleanup_(udev_device_unrefp
) struct udev_device
*d
= NULL
;
914 _cleanup_(udev_unrefp
) struct udev
*udev
= NULL
;
915 _cleanup_strv_free_
char **list
= NULL
;
923 if (stat(what
, &st
) < 0)
924 return log_error_errno(errno
, "Can't stat %s: %m", what
);
926 if (!S_ISBLK(st
.st_mode
)) {
927 log_error("Not a block device: %s", what
);
935 d
= udev_device_new_from_devnum(udev
, 'b', st
.st_rdev
);
939 v
= udev_device_get_property_value(d
, "ID_FS_USAGE");
940 if (!streq_ptr(v
, "filesystem")) {
941 log_error("%s does not contain a known file system.", what
);
945 v
= udev_device_get_property_value(d
, "SYSTEMD_MOUNT_WHERE");
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(struct udev_device
*d
) {
1042 v
= udev_device_get_property_value(d
, "ID_FS_TYPE");
1046 arg_mount_type
= strdup(v
);
1047 if (!arg_mount_type
)
1050 log_debug("Discovered type=%s", arg_mount_type
);
1054 static int acquire_mount_options(struct udev_device
*d
) {
1057 if (arg_mount_options
)
1060 v
= udev_device_get_property_value(d
, "SYSTEMD_MOUNT_OPTIONS");
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(struct udev_device
*d
) {
1077 model
= udev_device_get_property_value(d
, "ID_MODEL_FROM_DATABASE");
1081 return udev_device_get_property_value(d
, "ID_MODEL");
1084 static const char* get_label(struct udev_device
*d
) {
1089 label
= udev_device_get_property_value(d
, "ID_FS_LABEL");
1093 return udev_device_get_property_value(d
, "ID_PART_ENTRY_NAME");
1096 static int acquire_mount_where(struct udev_device
*d
) {
1099 if (arg_mount_where
)
1102 v
= udev_device_get_property_value(d
, "SYSTEMD_MOUNT_WHERE");
1104 _cleanup_free_
char *escaped
= NULL
;
1107 name
= get_label(d
);
1109 name
= get_model(d
);
1113 dn
= udev_device_get_devnode(d
);
1117 name
= basename(dn
);
1120 escaped
= xescape(name
, "\\");
1123 if (!filename_is_valid(escaped
))
1126 arg_mount_where
= strjoin("/run/media/system/", escaped
);
1128 arg_mount_where
= strdup(v
);
1130 if (!arg_mount_where
)
1133 log_debug("Discovered where=%s", arg_mount_where
);
1137 static int acquire_mount_where_for_loop_dev(const char *loop_dev
) {
1138 _cleanup_strv_free_
char **list
= NULL
;
1141 if (arg_mount_where
)
1144 r
= find_mount_points(loop_dev
, &list
);
1148 log_error("Can't find mount point of %s. It is expected that %s is already mounted on a place.", loop_dev
, loop_dev
);
1150 } else if (r
>= 2) {
1151 log_error("%s is mounted on %d places. It is expected that %s is mounted on a place.", loop_dev
, r
, loop_dev
);
1155 arg_mount_where
= strdup(list
[0]);
1156 if (!arg_mount_where
)
1159 log_debug("Discovered where=%s", arg_mount_where
);
1163 static int acquire_description(struct udev_device
*d
) {
1164 const char *model
, *label
;
1166 if (arg_description
)
1169 model
= get_model(d
);
1171 label
= get_label(d
);
1173 label
= udev_device_get_property_value(d
, "ID_PART_ENTRY_NUMBER");
1176 arg_description
= strjoin(model
, " ", label
);
1178 arg_description
= strdup(label
);
1180 arg_description
= strdup(model
);
1184 if (!arg_description
)
1187 log_debug("Discovered description=%s", arg_description
);
1191 static int acquire_removable(struct udev_device
*d
) {
1194 /* Shortcut this if there's no reason to check it */
1195 if (arg_action
!= ACTION_DEFAULT
&& arg_timeout_idle_set
&& arg_bind_device
>= 0)
1199 v
= udev_device_get_sysattr_value(d
, "removable");
1203 d
= udev_device_get_parent(d
);
1207 if (!streq_ptr(udev_device_get_subsystem(d
), "block"))
1211 if (parse_boolean(v
) <= 0)
1214 log_debug("Discovered removable device.");
1216 if (arg_action
== ACTION_DEFAULT
) {
1217 log_debug("Automatically turning on automount.");
1218 arg_action
= ACTION_AUTOMOUNT
;
1221 if (!arg_timeout_idle_set
) {
1222 log_debug("Setting idle timeout to 1s.");
1223 arg_timeout_idle
= USEC_PER_SEC
;
1226 if (arg_bind_device
< 0) {
1227 log_debug("Binding automount unit to device.");
1228 arg_bind_device
= true;
1234 static int discover_loop_backing_file(void) {
1235 _cleanup_(udev_device_unrefp
) struct udev_device
*d
= NULL
;
1236 _cleanup_(udev_unrefp
) struct udev
*udev
= 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
);
1280 d
= udev_device_new_from_devnum(udev
, 'b', st
.st_rdev
);
1284 v
= udev_device_get_property_value(d
, "ID_FS_USAGE");
1285 if (!streq_ptr(v
, "filesystem")) {
1286 log_error("%s does not contain a known file system.", arg_mount_what
);
1290 r
= acquire_mount_type(d
);
1294 r
= acquire_mount_options(d
);
1298 r
= acquire_mount_where_for_loop_dev(loop_dev
);
1302 r
= acquire_description(d
);
1309 static int discover_device(void) {
1310 _cleanup_(udev_device_unrefp
) struct udev_device
*d
= NULL
;
1311 _cleanup_(udev_unrefp
) struct udev
*udev
= NULL
;
1316 if (stat(arg_mount_what
, &st
) < 0)
1317 return log_error_errno(errno
, "Can't stat %s: %m", arg_mount_what
);
1319 if (S_ISREG(st
.st_mode
))
1320 return discover_loop_backing_file();
1322 if (!S_ISBLK(st
.st_mode
)) {
1323 log_error("Invalid file type: %s", arg_mount_what
);
1331 d
= udev_device_new_from_devnum(udev
, 'b', st
.st_rdev
);
1335 v
= udev_device_get_property_value(d
, "ID_FS_USAGE");
1336 if (!streq_ptr(v
, "filesystem")) {
1337 log_error("%s does not contain a known file system.", arg_mount_what
);
1341 r
= acquire_mount_type(d
);
1345 r
= acquire_mount_options(d
);
1349 r
= acquire_mount_where(d
);
1353 r
= acquire_description(d
);
1357 r
= acquire_removable(d
);
1376 char* columns
[_COLUMN_MAX
];
1379 static int compare_item(const void *a
, const void *b
) {
1380 const struct item
*x
= a
, *y
= b
;
1382 if (x
->columns
[COLUMN_NODE
] == y
->columns
[COLUMN_NODE
])
1384 if (!x
->columns
[COLUMN_NODE
])
1386 if (!y
->columns
[COLUMN_NODE
])
1389 return path_compare(x
->columns
[COLUMN_NODE
], y
->columns
[COLUMN_NODE
]);
1392 static int list_devices(void) {
1394 static const char * const titles
[_COLUMN_MAX
] = {
1395 [COLUMN_NODE
] = "NODE",
1396 [COLUMN_PATH
] = "PATH",
1397 [COLUMN_MODEL
] = "MODEL",
1398 [COLUMN_WWN
] = "WWN",
1399 [COLUMN_FSTYPE
] = "TYPE",
1400 [COLUMN_LABEL
] = "LABEL",
1401 [COLUMN_UUID
] = "UUID"
1404 _cleanup_(udev_enumerate_unrefp
) struct udev_enumerate
*e
= NULL
;
1405 _cleanup_(udev_unrefp
) struct udev
*udev
= NULL
;
1406 struct udev_list_entry
*item
= NULL
, *first
= NULL
;
1407 size_t n_allocated
= 0, n
= 0, i
;
1408 size_t column_width
[_COLUMN_MAX
];
1409 struct item
*items
= NULL
;
1413 for (c
= 0; c
< _COLUMN_MAX
; c
++)
1414 column_width
[c
] = strlen(titles
[c
]);
1420 e
= udev_enumerate_new(udev
);
1424 r
= udev_enumerate_add_match_subsystem(e
, "block");
1426 return log_error_errno(r
, "Failed to add block match: %m");
1428 r
= udev_enumerate_add_match_property(e
, "ID_FS_USAGE", "filesystem");
1430 return log_error_errno(r
, "Failed to add property match: %m");
1432 r
= udev_enumerate_scan_devices(e
);
1434 return log_error_errno(r
, "Failed to scan devices: %m");
1436 first
= udev_enumerate_get_list_entry(e
);
1437 udev_list_entry_foreach(item
, first
) {
1438 _cleanup_(udev_device_unrefp
) struct udev_device
*d
;
1441 d
= udev_device_new_from_syspath(udev
, udev_list_entry_get_name(item
));
1447 if (!GREEDY_REALLOC0(items
, n_allocated
, n
+1)) {
1454 for (c
= 0; c
< _COLUMN_MAX
; c
++) {
1455 const char *x
= NULL
;
1461 x
= udev_device_get_devnode(d
);
1465 x
= udev_device_get_property_value(d
, "ID_PATH");
1473 x
= udev_device_get_property_value(d
, "ID_WWN");
1477 x
= udev_device_get_property_value(d
, "ID_FS_TYPE");
1485 x
= udev_device_get_property_value(d
, "ID_FS_UUID");
1492 j
->columns
[c
] = strdup(x
);
1493 if (!j
->columns
[c
]) {
1499 if (k
> column_width
[c
])
1500 column_width
[c
] = k
;
1505 log_info("No devices found.");
1509 qsort_safe(items
, n
, sizeof(struct item
), compare_item
);
1511 (void) pager_open(arg_no_pager
, false);
1513 fputs(ansi_underline(), stdout
);
1514 for (c
= 0; c
< _COLUMN_MAX
; c
++) {
1518 printf("%-*s", (int) column_width
[c
], titles
[c
]);
1520 fputs(ansi_normal(), stdout
);
1521 fputc('\n', stdout
);
1523 for (i
= 0; i
< n
; i
++) {
1524 for (c
= 0; c
< _COLUMN_MAX
; c
++) {
1528 printf("%-*s", (int) column_width
[c
], strna(items
[i
].columns
[c
]));
1530 fputc('\n', stdout
);
1536 for (i
= 0; i
< n
; i
++)
1537 for (c
= 0; c
< _COLUMN_MAX
; c
++)
1538 free(items
[i
].columns
[c
]);
1544 int main(int argc
, char* argv
[]) {
1548 log_parse_environment();
1551 r
= parse_argv(argc
, argv
);
1555 if (arg_action
== ACTION_LIST
) {
1560 r
= bus_connect_transport_systemd(arg_transport
, arg_host
, arg_user
, &bus
);
1562 log_error_errno(r
, "Failed to create bus connection: %m");
1566 if (arg_action
== ACTION_UMOUNT
) {
1567 r
= action_umount(bus
, argc
, argv
);
1571 if (!path_is_normalized(arg_mount_what
)) {
1572 log_error("Path contains non-normalized components: %s", arg_mount_what
);
1578 r
= discover_device();
1583 if (!arg_mount_where
) {
1584 log_error("Can't figure out where to mount %s.", arg_mount_what
);
1589 if (path_equal(arg_mount_where
, "/")) {
1590 log_error("Refusing to operate on root directory.");
1595 if (!path_is_normalized(arg_mount_where
)) {
1596 log_error("Path contains non-normalized components: %s", arg_mount_where
);
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(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 log_error("File system type %s is not known to support uid=/gid=, refusing.",
1636 switch (arg_action
) {
1639 case ACTION_DEFAULT
:
1640 r
= start_transient_mount(bus
, argv
+ optind
);
1643 case ACTION_AUTOMOUNT
:
1644 r
= start_transient_automount(bus
, argv
+ optind
);
1648 assert_not_reached("Unexpected action.");
1652 /* make sure we terminate the bus connection first, and then close the
1653 * pager, see issue #3543 for the details. */
1654 bus
= sd_bus_flush_close_unref(bus
);
1657 free(arg_mount_what
);
1658 free(arg_mount_where
);
1659 free(arg_mount_type
);
1660 free(arg_mount_options
);
1661 free(arg_description
);
1662 strv_free(arg_property
);
1663 strv_free(arg_automount_property
);
1665 return r
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
;