1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2016 Lennart Poettering
13 #include "bus-error.h"
14 #include "bus-unit-util.h"
16 #include "dirent-util.h"
21 #include "fstab-util.h"
22 #include "mount-util.h"
24 #include "parse-util.h"
25 #include "path-util.h"
26 #include "spawn-polkit-agent.h"
27 #include "stat-util.h"
29 #include "udev-util.h"
31 #include "unit-name.h"
32 #include "user-util.h"
33 #include "terminal-util.h"
41 } arg_action
= ACTION_DEFAULT
;
43 static bool arg_no_block
= false;
44 static bool arg_no_pager
= false;
45 static bool arg_ask_password
= true;
46 static bool arg_quiet
= false;
47 static BusTransport arg_transport
= BUS_TRANSPORT_LOCAL
;
48 static bool arg_user
= false;
49 static const char *arg_host
= NULL
;
50 static bool arg_discover
= false;
51 static char *arg_mount_what
= NULL
;
52 static char *arg_mount_where
= NULL
;
53 static char *arg_mount_type
= NULL
;
54 static char *arg_mount_options
= NULL
;
55 static char *arg_description
= NULL
;
56 static char **arg_property
= NULL
;
57 static usec_t arg_timeout_idle
= USEC_INFINITY
;
58 static bool arg_timeout_idle_set
= false;
59 static char **arg_automount_property
= NULL
;
60 static int arg_bind_device
= -1;
61 static uid_t arg_uid
= UID_INVALID
;
62 static gid_t arg_gid
= GID_INVALID
;
63 static bool arg_fsck
= true;
64 static bool arg_aggressive_gc
= false;
66 static void help(void) {
67 printf("systemd-mount [OPTIONS...] WHAT [WHERE]\n"
68 "systemd-mount [OPTIONS...] --list\n"
69 "%s [OPTIONS...] %sWHAT|WHERE...\n\n"
70 "Establish a mount or auto-mount point transiently.\n\n"
71 " -h --help Show this help\n"
72 " --version Show package version\n"
73 " --no-block Do not wait until operation finished\n"
74 " --no-pager Do not pipe output into a pager\n"
75 " --no-ask-password Do not prompt for password\n"
76 " -q --quiet Suppress information messages during runtime\n"
77 " --user Run as user unit\n"
78 " -H --host=[USER@]HOST Operate on remote host\n"
79 " -M --machine=CONTAINER Operate on local container\n"
80 " --discover Discover mount device metadata\n"
81 " -t --type=TYPE File system type\n"
82 " -o --options=OPTIONS Mount options\n"
83 " --owner=USER Add uid= and gid= options for USER\n"
84 " --fsck=no Don't run file system check before mount\n"
85 " --description=TEXT Description for unit\n"
86 " -p --property=NAME=VALUE Set mount unit property\n"
87 " -A --automount=BOOL Create an auto-mount point\n"
88 " --timeout-idle-sec=SEC Specify automount idle timeout\n"
89 " --automount-property=NAME=VALUE\n"
90 " Set automount unit property\n"
91 " --bind-device Bind automount unit to device\n"
92 " --list List mountable block devices\n"
93 " -u --umount Unmount mount points\n"
94 " -G --collect Unload unit after it stopped, even when failed\n",
95 program_invocation_short_name
,
96 streq(program_invocation_short_name
, "systemd-umount") ? "" : "--umount ");
99 static int parse_argv(int argc
, char *argv
[]) {
116 ARG_AUTOMOUNT_PROPERTY
,
121 static const struct option options
[] = {
122 { "help", no_argument
, NULL
, 'h' },
123 { "version", no_argument
, NULL
, ARG_VERSION
},
124 { "no-block", no_argument
, NULL
, ARG_NO_BLOCK
},
125 { "no-pager", no_argument
, NULL
, ARG_NO_PAGER
},
126 { "no-ask-password", no_argument
, NULL
, ARG_NO_ASK_PASSWORD
},
127 { "quiet", no_argument
, NULL
, 'q' },
128 { "user", no_argument
, NULL
, ARG_USER
},
129 { "system", no_argument
, NULL
, ARG_SYSTEM
},
130 { "host", required_argument
, NULL
, 'H' },
131 { "machine", required_argument
, NULL
, 'M' },
132 { "discover", no_argument
, NULL
, ARG_DISCOVER
},
133 { "type", required_argument
, NULL
, 't' },
134 { "options", required_argument
, NULL
, 'o' },
135 { "owner", required_argument
, NULL
, ARG_OWNER
},
136 { "fsck", required_argument
, NULL
, ARG_FSCK
},
137 { "description", required_argument
, NULL
, ARG_DESCRIPTION
},
138 { "property", required_argument
, NULL
, 'p' },
139 { "automount", required_argument
, NULL
, ARG_AUTOMOUNT
},
140 { "timeout-idle-sec", required_argument
, NULL
, ARG_TIMEOUT_IDLE
},
141 { "automount-property", required_argument
, NULL
, ARG_AUTOMOUNT_PROPERTY
},
142 { "bind-device", no_argument
, NULL
, ARG_BIND_DEVICE
},
143 { "list", no_argument
, NULL
, ARG_LIST
},
144 { "umount", no_argument
, NULL
, 'u' },
145 { "unmount", no_argument
, NULL
, 'u' },
146 { "collect", no_argument
, NULL
, 'G' },
155 if (strstr(program_invocation_short_name
, "systemd-umount"))
156 arg_action
= ACTION_UMOUNT
;
158 while ((c
= getopt_long(argc
, argv
, "hqH:M:t:o:p:AuG", options
, NULL
)) >= 0)
177 case ARG_NO_ASK_PASSWORD
:
178 arg_ask_password
= false;
194 arg_transport
= BUS_TRANSPORT_REMOTE
;
199 arg_transport
= BUS_TRANSPORT_MACHINE
;
208 if (free_and_strdup(&arg_mount_type
, optarg
) < 0)
213 if (free_and_strdup(&arg_mount_options
, optarg
) < 0)
218 const char *user
= optarg
;
220 r
= get_user_creds(&user
, &arg_uid
, &arg_gid
, NULL
, NULL
);
222 return log_error_errno(r
,
223 r
== -EBADMSG
? "UID or GID of user %s are invalid."
224 : "Cannot use \"%s\" as owner: %m",
230 r
= parse_boolean(optarg
);
232 return log_error_errno(r
, "Failed to parse --fsck= argument: %s", optarg
);
237 case ARG_DESCRIPTION
:
238 if (free_and_strdup(&arg_description
, optarg
) < 0)
243 if (strv_extend(&arg_property
, optarg
) < 0)
249 arg_action
= ACTION_AUTOMOUNT
;
253 r
= parse_boolean(optarg
);
255 return log_error_errno(r
, "--automount= expects a valid boolean parameter: %s", optarg
);
257 arg_action
= r
? ACTION_AUTOMOUNT
: ACTION_MOUNT
;
260 case ARG_TIMEOUT_IDLE
:
261 r
= parse_sec(optarg
, &arg_timeout_idle
);
263 return log_error_errno(r
, "Failed to parse timeout: %s", optarg
);
267 case ARG_AUTOMOUNT_PROPERTY
:
268 if (strv_extend(&arg_automount_property
, optarg
) < 0)
273 case ARG_BIND_DEVICE
:
274 arg_bind_device
= true;
278 arg_action
= ACTION_LIST
;
282 arg_action
= ACTION_UMOUNT
;
286 arg_aggressive_gc
= true;
293 assert_not_reached("Unhandled option");
296 if (arg_user
&& arg_transport
!= BUS_TRANSPORT_LOCAL
) {
297 log_error("Execution in user context is not supported on non-local systems.");
301 if (arg_action
== ACTION_LIST
) {
303 log_error("Too many arguments.");
307 if (arg_transport
!= BUS_TRANSPORT_LOCAL
) {
308 log_error("Listing devices only supported locally.");
311 } else if (arg_action
== ACTION_UMOUNT
) {
312 if (optind
>= argc
) {
313 log_error("At least one argument required.");
317 if (arg_transport
!= BUS_TRANSPORT_LOCAL
) {
320 for (i
= optind
; i
< argc
; i
++)
321 if (!path_is_absolute(argv
[i
]) ) {
322 log_error("Only absolute path is supported: %s", argv
[i
]);
327 if (optind
>= argc
) {
328 log_error("At least one argument required.");
332 if (argc
> optind
+2) {
333 log_error("At most two arguments required.");
337 if (arg_mount_type
&& (fstype_is_api_vfs(arg_mount_type
) || fstype_is_network(arg_mount_type
))) {
338 arg_mount_what
= strdup(argv
[optind
]);
342 } else if (arg_transport
== BUS_TRANSPORT_LOCAL
) {
343 _cleanup_free_
char *u
= NULL
;
345 u
= fstab_node_to_udev_node(argv
[optind
]);
349 r
= chase_symlinks(u
, NULL
, 0, &arg_mount_what
);
351 return log_error_errno(r
, "Failed to make path %s absolute: %m", u
);
353 arg_mount_what
= strdup(argv
[optind
]);
357 path_simplify(arg_mount_what
, false);
359 if (!path_is_absolute(arg_mount_what
)) {
360 log_error("Only absolute path is supported: %s", arg_mount_what
);
365 if (argc
> optind
+1) {
366 if (arg_transport
== BUS_TRANSPORT_LOCAL
) {
367 r
= chase_symlinks(argv
[optind
+1], NULL
, CHASE_NONEXISTENT
, &arg_mount_where
);
369 return log_error_errno(r
, "Failed to make path %s absolute: %m", argv
[optind
+1]);
371 arg_mount_where
= strdup(argv
[optind
+1]);
372 if (!arg_mount_where
)
375 path_simplify(arg_mount_where
, false);
377 if (!path_is_absolute(arg_mount_where
)) {
378 log_error("Only absolute path is supported: %s", arg_mount_where
);
385 if (arg_discover
&& arg_transport
!= BUS_TRANSPORT_LOCAL
) {
386 log_error("Automatic mount location discovery is only supported locally.");
394 static int transient_unit_set_properties(sd_bus_message
*m
, UnitType t
, char **properties
) {
397 if (!isempty(arg_description
)) {
398 r
= sd_bus_message_append(m
, "(sv)", "Description", "s", arg_description
);
403 if (arg_bind_device
&& is_device_path(arg_mount_what
)) {
404 _cleanup_free_
char *device_unit
= NULL
;
406 r
= unit_name_from_path(arg_mount_what
, ".device", &device_unit
);
410 r
= sd_bus_message_append(m
, "(sv)(sv)",
411 "After", "as", 1, device_unit
,
412 "BindsTo", "as", 1, device_unit
);
417 if (arg_aggressive_gc
) {
418 r
= sd_bus_message_append(m
, "(sv)", "CollectMode", "s", "inactive-or-failed");
423 r
= bus_append_unit_property_assignment_many(m
, t
, properties
);
430 static int transient_mount_set_properties(sd_bus_message
*m
) {
431 _cleanup_free_
char *options
= NULL
;
436 r
= transient_unit_set_properties(m
, UNIT_MOUNT
, arg_property
);
440 if (arg_mount_what
) {
441 r
= sd_bus_message_append(m
, "(sv)", "What", "s", arg_mount_what
);
446 if (arg_mount_type
) {
447 r
= sd_bus_message_append(m
, "(sv)", "Type", "s", arg_mount_type
);
452 /* Prepend uid=…,gid=… if arg_uid is set */
453 if (arg_uid
!= UID_INVALID
) {
454 r
= asprintf(&options
,
455 "uid=" UID_FMT
",gid=" GID_FMT
"%s%s",
457 arg_mount_options
? "," : "", arg_mount_options
);
462 if (options
|| arg_mount_options
) {
463 log_debug("Using mount options: %s", options
?: arg_mount_options
);
465 r
= sd_bus_message_append(m
, "(sv)", "Options", "s", options
?: arg_mount_options
);
469 log_debug("Not using any mount options");
472 _cleanup_free_
char *fsck
= NULL
;
474 r
= unit_name_from_path_instance("systemd-fsck", arg_mount_what
, ".service", &fsck
);
478 r
= sd_bus_message_append(m
,
480 "Requires", "as", 1, fsck
,
481 "After", "as", 1, fsck
);
489 static int transient_automount_set_properties(sd_bus_message
*m
) {
494 r
= transient_unit_set_properties(m
, UNIT_AUTOMOUNT
, arg_automount_property
);
498 if (arg_timeout_idle
!= USEC_INFINITY
) {
499 r
= sd_bus_message_append(m
, "(sv)", "TimeoutIdleUSec", "t", arg_timeout_idle
);
507 static int start_transient_mount(
511 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
, *reply
= NULL
;
512 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
513 _cleanup_(bus_wait_for_jobs_freep
) BusWaitForJobs
*w
= NULL
;
514 _cleanup_free_
char *mount_unit
= NULL
;
518 r
= bus_wait_for_jobs_new(bus
, &w
);
520 return log_error_errno(r
, "Could not watch jobs: %m");
523 r
= unit_name_from_path(arg_mount_where
, ".mount", &mount_unit
);
525 return log_error_errno(r
, "Failed to make mount unit name: %m");
527 r
= sd_bus_message_new_method_call(
530 "org.freedesktop.systemd1",
531 "/org/freedesktop/systemd1",
532 "org.freedesktop.systemd1.Manager",
533 "StartTransientUnit");
535 return bus_log_create_error(r
);
537 r
= sd_bus_message_set_allow_interactive_authorization(m
, arg_ask_password
);
539 return bus_log_create_error(r
);
542 r
= sd_bus_message_append(m
, "ss", mount_unit
, "fail");
544 return bus_log_create_error(r
);
547 r
= sd_bus_message_open_container(m
, 'a', "(sv)");
549 return bus_log_create_error(r
);
551 r
= transient_mount_set_properties(m
);
553 return bus_log_create_error(r
);
555 r
= sd_bus_message_close_container(m
);
557 return bus_log_create_error(r
);
559 /* Auxiliary units */
560 r
= sd_bus_message_append(m
, "a(sa(sv))", 0);
562 return bus_log_create_error(r
);
564 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
566 r
= sd_bus_call(bus
, m
, 0, &error
, &reply
);
568 return log_error_errno(r
, "Failed to start transient mount unit: %s", bus_error_message(&error
, r
));
573 r
= sd_bus_message_read(reply
, "o", &object
);
575 return bus_log_parse_error(r
);
577 r
= bus_wait_for_jobs_one(w
, object
, arg_quiet
);
583 log_info("Started unit %s%s%s for mount point: %s%s%s",
584 ansi_highlight(), mount_unit
, ansi_normal(),
585 ansi_highlight(), arg_mount_where
, ansi_normal());
590 static int start_transient_automount(
594 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
, *reply
= NULL
;
595 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
596 _cleanup_(bus_wait_for_jobs_freep
) BusWaitForJobs
*w
= NULL
;
597 _cleanup_free_
char *automount_unit
= NULL
, *mount_unit
= NULL
;
601 r
= bus_wait_for_jobs_new(bus
, &w
);
603 return log_error_errno(r
, "Could not watch jobs: %m");
606 r
= unit_name_from_path(arg_mount_where
, ".automount", &automount_unit
);
608 return log_error_errno(r
, "Failed to make automount unit name: %m");
610 r
= unit_name_from_path(arg_mount_where
, ".mount", &mount_unit
);
612 return log_error_errno(r
, "Failed to make mount unit name: %m");
614 r
= sd_bus_message_new_method_call(
617 "org.freedesktop.systemd1",
618 "/org/freedesktop/systemd1",
619 "org.freedesktop.systemd1.Manager",
620 "StartTransientUnit");
622 return bus_log_create_error(r
);
624 r
= sd_bus_message_set_allow_interactive_authorization(m
, arg_ask_password
);
626 return bus_log_create_error(r
);
629 r
= sd_bus_message_append(m
, "ss", automount_unit
, "fail");
631 return bus_log_create_error(r
);
634 r
= sd_bus_message_open_container(m
, 'a', "(sv)");
636 return bus_log_create_error(r
);
638 r
= transient_automount_set_properties(m
);
640 return bus_log_create_error(r
);
642 r
= sd_bus_message_close_container(m
);
644 return bus_log_create_error(r
);
646 /* Auxiliary units */
647 r
= sd_bus_message_open_container(m
, 'a', "(sa(sv))");
649 return bus_log_create_error(r
);
651 r
= sd_bus_message_open_container(m
, 'r', "sa(sv)");
653 return bus_log_create_error(r
);
655 r
= sd_bus_message_append(m
, "s", mount_unit
);
657 return bus_log_create_error(r
);
659 r
= sd_bus_message_open_container(m
, 'a', "(sv)");
661 return bus_log_create_error(r
);
663 r
= transient_mount_set_properties(m
);
665 return bus_log_create_error(r
);
667 r
= sd_bus_message_close_container(m
);
669 return bus_log_create_error(r
);
671 r
= sd_bus_message_close_container(m
);
673 return bus_log_create_error(r
);
675 r
= sd_bus_message_close_container(m
);
677 return bus_log_create_error(r
);
679 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
681 r
= sd_bus_call(bus
, m
, 0, &error
, &reply
);
683 return log_error_errno(r
, "Failed to start transient automount unit: %s", bus_error_message(&error
, r
));
688 r
= sd_bus_message_read(reply
, "o", &object
);
690 return bus_log_parse_error(r
);
692 r
= bus_wait_for_jobs_one(w
, object
, arg_quiet
);
698 log_info("Started unit %s%s%s for mount point: %s%s%s",
699 ansi_highlight(), automount_unit
, ansi_normal(),
700 ansi_highlight(), arg_mount_where
, ansi_normal());
705 static int find_mount_points(const char *what
, char ***list
) {
706 _cleanup_fclose_
FILE *proc_self_mountinfo
= NULL
;
707 _cleanup_strv_free_
char **l
= NULL
;
708 size_t bufsize
= 0, n
= 0;
713 /* Returns all mount points obtained from /proc/self/mountinfo in *list,
714 * and the number of mount points as return value. */
716 proc_self_mountinfo
= fopen("/proc/self/mountinfo", "re");
717 if (!proc_self_mountinfo
)
718 return log_error_errno(errno
, "Can't open /proc/self/mountinfo: %m");
721 _cleanup_free_
char *path
= NULL
, *where
= NULL
, *dev
= NULL
;
724 r
= fscanf(proc_self_mountinfo
,
725 "%*s " /* (1) mount id */
726 "%*s " /* (2) parent id */
727 "%*s " /* (3) major:minor */
728 "%*s " /* (4) root */
729 "%ms " /* (5) mount point */
730 "%*s" /* (6) mount options */
731 "%*[^-]" /* (7) optional fields */
732 "- " /* (8) separator */
733 "%*s " /* (9) file system type */
734 "%ms" /* (10) mount source */
735 "%*s" /* (11) mount options 2 */
736 "%*[^\n]", /* some rubbish at the end */
745 if (!streq(what
, dev
))
748 r
= cunescape(path
, UNESCAPE_RELAX
, &where
);
752 /* one extra slot is needed for the terminating NULL */
753 if (!GREEDY_REALLOC(l
, bufsize
, n
+ 2))
756 l
[n
++] = TAKE_PTR(where
);
759 if (!GREEDY_REALLOC(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 log_error("Refusing to operate on root directory: %s", where
);
901 if (!path_is_normalized(where
)) {
902 log_error("Path contains non-normalized components: %s", where
);
906 r
= stop_mount(bus
, where
, ".mount");
910 r
= stop_mount(bus
, where
, ".automount");
917 static int umount_by_device(sd_bus
*bus
, const char *what
) {
918 _cleanup_(udev_device_unrefp
) struct udev_device
*d
= NULL
;
919 _cleanup_(udev_unrefp
) struct udev
*udev
= NULL
;
920 _cleanup_strv_free_
char **list
= NULL
;
928 if (stat(what
, &st
) < 0)
929 return log_error_errno(errno
, "Can't stat %s: %m", what
);
931 if (!S_ISBLK(st
.st_mode
)) {
932 log_error("Not a block device: %s", what
);
940 d
= udev_device_new_from_devnum(udev
, 'b', st
.st_rdev
);
944 v
= udev_device_get_property_value(d
, "ID_FS_USAGE");
945 if (!streq_ptr(v
, "filesystem")) {
946 log_error("%s does not contain a known file system.", what
);
950 v
= udev_device_get_property_value(d
, "SYSTEMD_MOUNT_WHERE");
952 r2
= stop_mounts(bus
, v
);
954 r
= find_mount_points(what
, &list
);
958 for (l
= list
; *l
; l
++) {
959 r
= stop_mounts(bus
, *l
);
967 static int umount_loop(sd_bus
*bus
, const char *backing_file
) {
968 _cleanup_free_
char *loop_dev
= NULL
;
971 assert(backing_file
);
973 r
= find_loop_device(backing_file
, &loop_dev
);
975 return log_error_errno(r
, r
== -ENXIO
? "File %s is not mounted." : "Can't get loop device for %s: %m", backing_file
);
977 return umount_by_device(bus
, loop_dev
);
980 static int action_umount(
987 if (arg_transport
!= BUS_TRANSPORT_LOCAL
) {
988 for (i
= optind
; i
< argc
; i
++) {
989 _cleanup_free_
char *p
= NULL
;
995 path_simplify(p
, false);
997 r
= stop_mounts(bus
, p
);
1004 for (i
= optind
; i
< argc
; i
++) {
1005 _cleanup_free_
char *u
= NULL
, *p
= NULL
;
1008 u
= fstab_node_to_udev_node(argv
[i
]);
1012 r
= chase_symlinks(u
, NULL
, 0, &p
);
1014 r2
= log_error_errno(r
, "Failed to make path %s absolute: %m", argv
[i
]);
1018 if (stat(p
, &st
) < 0)
1019 return log_error_errno(errno
, "Can't stat %s (from %s): %m", p
, argv
[i
]);
1021 if (S_ISBLK(st
.st_mode
))
1022 r
= umount_by_device(bus
, p
);
1023 else if (S_ISREG(st
.st_mode
))
1024 r
= umount_loop(bus
, p
);
1025 else if (S_ISDIR(st
.st_mode
))
1026 r
= stop_mounts(bus
, p
);
1028 log_error("Invalid file type: %s (from %s)", p
, argv
[i
]);
1039 static int acquire_mount_type(struct udev_device
*d
) {
1047 v
= udev_device_get_property_value(d
, "ID_FS_TYPE");
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(struct udev_device
*d
) {
1062 if (arg_mount_options
)
1065 v
= udev_device_get_property_value(d
, "SYSTEMD_MOUNT_OPTIONS");
1069 arg_mount_options
= strdup(v
);
1070 if (!arg_mount_options
)
1073 log_debug("Discovered options=%s", arg_mount_options
);
1077 static const char *get_model(struct udev_device
*d
) {
1082 model
= udev_device_get_property_value(d
, "ID_MODEL_FROM_DATABASE");
1086 return udev_device_get_property_value(d
, "ID_MODEL");
1089 static const char* get_label(struct udev_device
*d
) {
1094 label
= udev_device_get_property_value(d
, "ID_FS_LABEL");
1098 return udev_device_get_property_value(d
, "ID_PART_ENTRY_NAME");
1101 static int acquire_mount_where(struct udev_device
*d
) {
1104 if (arg_mount_where
)
1107 v
= udev_device_get_property_value(d
, "SYSTEMD_MOUNT_WHERE");
1109 _cleanup_free_
char *escaped
= NULL
;
1112 name
= get_label(d
);
1114 name
= get_model(d
);
1118 dn
= udev_device_get_devnode(d
);
1122 name
= basename(dn
);
1125 escaped
= xescape(name
, "\\");
1128 if (!filename_is_valid(escaped
))
1131 arg_mount_where
= strjoin("/run/media/system/", escaped
);
1133 arg_mount_where
= strdup(v
);
1135 if (!arg_mount_where
)
1138 log_debug("Discovered where=%s", arg_mount_where
);
1142 static int acquire_mount_where_for_loop_dev(const char *loop_dev
) {
1143 _cleanup_strv_free_
char **list
= NULL
;
1146 if (arg_mount_where
)
1149 r
= find_mount_points(loop_dev
, &list
);
1153 log_error("Can't find mount point of %s. It is expected that %s is already mounted on a place.", loop_dev
, loop_dev
);
1155 } else if (r
>= 2) {
1156 log_error("%s is mounted on %d places. It is expected that %s is mounted on a place.", loop_dev
, r
, loop_dev
);
1160 arg_mount_where
= strdup(list
[0]);
1161 if (!arg_mount_where
)
1164 log_debug("Discovered where=%s", arg_mount_where
);
1168 static int acquire_description(struct udev_device
*d
) {
1169 const char *model
, *label
;
1171 if (arg_description
)
1174 model
= get_model(d
);
1176 label
= get_label(d
);
1178 label
= udev_device_get_property_value(d
, "ID_PART_ENTRY_NUMBER");
1181 arg_description
= strjoin(model
, " ", label
);
1183 arg_description
= strdup(label
);
1185 arg_description
= strdup(model
);
1189 if (!arg_description
)
1192 log_debug("Discovered description=%s", arg_description
);
1196 static int acquire_removable(struct udev_device
*d
) {
1199 /* Shortcut this if there's no reason to check it */
1200 if (arg_action
!= ACTION_DEFAULT
&& arg_timeout_idle_set
&& arg_bind_device
>= 0)
1204 v
= udev_device_get_sysattr_value(d
, "removable");
1208 d
= udev_device_get_parent(d
);
1212 if (!streq_ptr(udev_device_get_subsystem(d
), "block"))
1216 if (parse_boolean(v
) <= 0)
1219 log_debug("Discovered removable device.");
1221 if (arg_action
== ACTION_DEFAULT
) {
1222 log_debug("Automatically turning on automount.");
1223 arg_action
= ACTION_AUTOMOUNT
;
1226 if (!arg_timeout_idle_set
) {
1227 log_debug("Setting idle timeout to 1s.");
1228 arg_timeout_idle
= USEC_PER_SEC
;
1231 if (arg_bind_device
< 0) {
1232 log_debug("Binding automount unit to device.");
1233 arg_bind_device
= true;
1239 static int discover_loop_backing_file(void) {
1240 _cleanup_(udev_device_unrefp
) struct udev_device
*d
= NULL
;
1241 _cleanup_(udev_unrefp
) struct udev
*udev
= NULL
;
1242 _cleanup_free_
char *loop_dev
= NULL
;
1247 r
= find_loop_device(arg_mount_what
, &loop_dev
);
1248 if (r
< 0 && r
!= -ENXIO
)
1249 return log_error_errno(errno
, "Can't get loop device for %s: %m", arg_mount_what
);
1252 _cleanup_free_
char *escaped
= NULL
;
1254 if (arg_mount_where
)
1257 escaped
= xescape(basename(arg_mount_what
), "\\");
1260 if (!filename_is_valid(escaped
)) {
1261 log_error("Escaped name %s is not a valid filename.", escaped
);
1265 arg_mount_where
= strjoin("/run/media/system/", escaped
);
1266 if (!arg_mount_where
)
1269 log_debug("Discovered where=%s", arg_mount_where
);
1273 if (stat(loop_dev
, &st
) < 0)
1274 return log_error_errno(errno
, "Can't stat %s: %m", loop_dev
);
1276 if (!S_ISBLK(st
.st_mode
)) {
1277 log_error("Invalid file type: %s", loop_dev
);
1285 d
= udev_device_new_from_devnum(udev
, 'b', st
.st_rdev
);
1289 v
= udev_device_get_property_value(d
, "ID_FS_USAGE");
1290 if (!streq_ptr(v
, "filesystem")) {
1291 log_error("%s does not contain a known file system.", arg_mount_what
);
1295 r
= acquire_mount_type(d
);
1299 r
= acquire_mount_options(d
);
1303 r
= acquire_mount_where_for_loop_dev(loop_dev
);
1307 r
= acquire_description(d
);
1314 static int discover_device(void) {
1315 _cleanup_(udev_device_unrefp
) struct udev_device
*d
= NULL
;
1316 _cleanup_(udev_unrefp
) struct udev
*udev
= NULL
;
1321 if (stat(arg_mount_what
, &st
) < 0)
1322 return log_error_errno(errno
, "Can't stat %s: %m", arg_mount_what
);
1324 if (S_ISREG(st
.st_mode
))
1325 return discover_loop_backing_file();
1327 if (!S_ISBLK(st
.st_mode
)) {
1328 log_error("Invalid file type: %s", arg_mount_what
);
1336 d
= udev_device_new_from_devnum(udev
, 'b', st
.st_rdev
);
1340 v
= udev_device_get_property_value(d
, "ID_FS_USAGE");
1341 if (!streq_ptr(v
, "filesystem")) {
1342 log_error("%s does not contain a known file system.", arg_mount_what
);
1346 r
= acquire_mount_type(d
);
1350 r
= acquire_mount_options(d
);
1354 r
= acquire_mount_where(d
);
1358 r
= acquire_description(d
);
1362 r
= acquire_removable(d
);
1381 char* columns
[_COLUMN_MAX
];
1384 static int compare_item(const void *a
, const void *b
) {
1385 const struct item
*x
= a
, *y
= b
;
1387 if (x
->columns
[COLUMN_NODE
] == y
->columns
[COLUMN_NODE
])
1389 if (!x
->columns
[COLUMN_NODE
])
1391 if (!y
->columns
[COLUMN_NODE
])
1394 return path_compare(x
->columns
[COLUMN_NODE
], y
->columns
[COLUMN_NODE
]);
1397 static int list_devices(void) {
1399 static const char * const titles
[_COLUMN_MAX
] = {
1400 [COLUMN_NODE
] = "NODE",
1401 [COLUMN_PATH
] = "PATH",
1402 [COLUMN_MODEL
] = "MODEL",
1403 [COLUMN_WWN
] = "WWN",
1404 [COLUMN_FSTYPE
] = "TYPE",
1405 [COLUMN_LABEL
] = "LABEL",
1406 [COLUMN_UUID
] = "UUID"
1409 _cleanup_(udev_enumerate_unrefp
) struct udev_enumerate
*e
= NULL
;
1410 _cleanup_(udev_unrefp
) struct udev
*udev
= NULL
;
1411 struct udev_list_entry
*item
= NULL
, *first
= NULL
;
1412 size_t n_allocated
= 0, n
= 0, i
;
1413 size_t column_width
[_COLUMN_MAX
];
1414 struct item
*items
= NULL
;
1418 for (c
= 0; c
< _COLUMN_MAX
; c
++)
1419 column_width
[c
] = strlen(titles
[c
]);
1425 e
= udev_enumerate_new(udev
);
1429 r
= udev_enumerate_add_match_subsystem(e
, "block");
1431 return log_error_errno(r
, "Failed to add block match: %m");
1433 r
= udev_enumerate_add_match_property(e
, "ID_FS_USAGE", "filesystem");
1435 return log_error_errno(r
, "Failed to add property match: %m");
1437 r
= udev_enumerate_scan_devices(e
);
1439 return log_error_errno(r
, "Failed to scan devices: %m");
1441 first
= udev_enumerate_get_list_entry(e
);
1442 udev_list_entry_foreach(item
, first
) {
1443 _cleanup_(udev_device_unrefp
) struct udev_device
*d
;
1446 d
= udev_device_new_from_syspath(udev
, udev_list_entry_get_name(item
));
1452 if (!GREEDY_REALLOC0(items
, n_allocated
, n
+1)) {
1459 for (c
= 0; c
< _COLUMN_MAX
; c
++) {
1460 const char *x
= NULL
;
1466 x
= udev_device_get_devnode(d
);
1470 x
= udev_device_get_property_value(d
, "ID_PATH");
1478 x
= udev_device_get_property_value(d
, "ID_WWN");
1482 x
= udev_device_get_property_value(d
, "ID_FS_TYPE");
1490 x
= udev_device_get_property_value(d
, "ID_FS_UUID");
1497 j
->columns
[c
] = strdup(x
);
1498 if (!j
->columns
[c
]) {
1504 if (k
> column_width
[c
])
1505 column_width
[c
] = k
;
1510 log_info("No devices found.");
1514 qsort_safe(items
, n
, sizeof(struct item
), compare_item
);
1516 (void) pager_open(arg_no_pager
, false);
1518 fputs(ansi_underline(), stdout
);
1519 for (c
= 0; c
< _COLUMN_MAX
; c
++) {
1523 printf("%-*s", (int) column_width
[c
], titles
[c
]);
1525 fputs(ansi_normal(), stdout
);
1526 fputc('\n', stdout
);
1528 for (i
= 0; i
< n
; i
++) {
1529 for (c
= 0; c
< _COLUMN_MAX
; c
++) {
1533 printf("%-*s", (int) column_width
[c
], strna(items
[i
].columns
[c
]));
1535 fputc('\n', stdout
);
1541 for (i
= 0; i
< n
; i
++)
1542 for (c
= 0; c
< _COLUMN_MAX
; c
++)
1543 free(items
[i
].columns
[c
]);
1549 int main(int argc
, char* argv
[]) {
1553 log_parse_environment();
1556 r
= parse_argv(argc
, argv
);
1560 if (arg_action
== ACTION_LIST
) {
1565 r
= bus_connect_transport_systemd(arg_transport
, arg_host
, arg_user
, &bus
);
1567 log_error_errno(r
, "Failed to create bus connection: %m");
1571 if (arg_action
== ACTION_UMOUNT
) {
1572 r
= action_umount(bus
, argc
, argv
);
1576 if (!path_is_normalized(arg_mount_what
)) {
1577 log_error("Path contains non-normalized components: %s", arg_mount_what
);
1583 r
= discover_device();
1588 if (!arg_mount_where
) {
1589 log_error("Can't figure out where to mount %s.", arg_mount_what
);
1594 if (path_equal(arg_mount_where
, "/")) {
1595 log_error("Refusing to operate on root directory.");
1600 if (!path_is_normalized(arg_mount_where
)) {
1601 log_error("Path contains non-normalized components: %s", arg_mount_where
);
1606 if (streq_ptr(arg_mount_type
, "auto"))
1607 arg_mount_type
= mfree(arg_mount_type
);
1608 if (streq_ptr(arg_mount_options
, "defaults"))
1609 arg_mount_options
= mfree(arg_mount_options
);
1611 if (!is_device_path(arg_mount_what
))
1614 if (arg_fsck
&& arg_mount_type
&& arg_transport
== BUS_TRANSPORT_LOCAL
) {
1615 r
= fsck_exists(arg_mount_type
);
1617 log_warning_errno(r
, "Couldn't determine whether fsck for %s exists, proceeding anyway.", arg_mount_type
);
1619 log_debug("Disabling file system check as fsck for %s doesn't exist.", arg_mount_type
);
1620 arg_fsck
= false; /* fsck doesn't exist, let's not attempt it */
1624 /* The kernel (properly) refuses mounting file systems with unknown uid=,gid= options,
1625 * but not for all filesystem types. Let's try to catch the cases where the option
1626 * would be used if the file system does not support it. It is also possible to
1627 * autodetect the file system, but that's only possible with disk-based file systems
1628 * which incidentally seem to be implemented more carefully and reject unknown options,
1629 * so it's probably OK that we do the check only when the type is specified.
1631 if (arg_mount_type
&&
1632 !streq(arg_mount_type
, "auto") &&
1633 arg_uid
!= UID_INVALID
&&
1634 !fstype_can_uid_gid(arg_mount_type
)) {
1635 log_error("File system type %s is not known to support uid=/gid=, refusing.",
1641 switch (arg_action
) {
1644 case ACTION_DEFAULT
:
1645 r
= start_transient_mount(bus
, argv
+ optind
);
1648 case ACTION_AUTOMOUNT
:
1649 r
= start_transient_automount(bus
, argv
+ optind
);
1653 assert_not_reached("Unexpected action.");
1657 /* make sure we terminate the bus connection first, and then close the
1658 * pager, see issue #3543 for the details. */
1659 bus
= sd_bus_flush_close_unref(bus
);
1662 free(arg_mount_what
);
1663 free(arg_mount_where
);
1664 free(arg_mount_type
);
1665 free(arg_mount_options
);
1666 free(arg_description
);
1667 strv_free(arg_property
);
1668 strv_free(arg_automount_property
);
1670 return r
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
;