1 /* SPDX-License-Identifier: LGPL-2.1+ */
9 #include "bus-unit-util.h"
11 #include "device-util.h"
12 #include "dirent-util.h"
17 #include "fstab-util.h"
18 #include "mount-util.h"
20 #include "parse-util.h"
21 #include "path-util.h"
22 #include "spawn-polkit-agent.h"
23 #include "stat-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 int help(void) {
62 _cleanup_free_
char *link
= NULL
;
65 r
= terminal_urlify_man("systemd-mount", "1", &link
);
69 printf("systemd-mount [OPTIONS...] WHAT [WHERE]\n"
70 "systemd-mount [OPTIONS...] --list\n"
71 "%s [OPTIONS...] %sWHAT|WHERE...\n\n"
72 "Establish a mount or auto-mount point transiently.\n\n"
73 " -h --help Show this help\n"
74 " --version Show package version\n"
75 " --no-block Do not wait until operation finished\n"
76 " --no-pager Do not pipe output into a pager\n"
77 " --no-ask-password Do not prompt for password\n"
78 " -q --quiet Suppress information messages during runtime\n"
79 " --user Run as user unit\n"
80 " -H --host=[USER@]HOST Operate on remote host\n"
81 " -M --machine=CONTAINER Operate on local container\n"
82 " --discover Discover mount device metadata\n"
83 " -t --type=TYPE File system type\n"
84 " -o --options=OPTIONS Mount options\n"
85 " --owner=USER Add uid= and gid= options for USER\n"
86 " --fsck=no Don't run file system check before mount\n"
87 " --description=TEXT Description for unit\n"
88 " -p --property=NAME=VALUE Set mount unit property\n"
89 " -A --automount=BOOL Create an auto-mount point\n"
90 " --timeout-idle-sec=SEC Specify automount idle timeout\n"
91 " --automount-property=NAME=VALUE\n"
92 " Set automount unit property\n"
93 " --bind-device Bind automount unit to device\n"
94 " --list List mountable block devices\n"
95 " -u --umount Unmount mount points\n"
96 " -G --collect Unload unit after it stopped, even when failed\n"
97 "\nSee the %s for details.\n"
98 , program_invocation_short_name
99 , streq(program_invocation_short_name
, "systemd-umount") ? "" : "--umount "
106 static int parse_argv(int argc
, char *argv
[]) {
123 ARG_AUTOMOUNT_PROPERTY
,
128 static const struct option options
[] = {
129 { "help", no_argument
, NULL
, 'h' },
130 { "version", no_argument
, NULL
, ARG_VERSION
},
131 { "no-block", no_argument
, NULL
, ARG_NO_BLOCK
},
132 { "no-pager", no_argument
, NULL
, ARG_NO_PAGER
},
133 { "no-ask-password", no_argument
, NULL
, ARG_NO_ASK_PASSWORD
},
134 { "quiet", no_argument
, NULL
, 'q' },
135 { "user", no_argument
, NULL
, ARG_USER
},
136 { "system", no_argument
, NULL
, ARG_SYSTEM
},
137 { "host", required_argument
, NULL
, 'H' },
138 { "machine", required_argument
, NULL
, 'M' },
139 { "discover", no_argument
, NULL
, ARG_DISCOVER
},
140 { "type", required_argument
, NULL
, 't' },
141 { "options", required_argument
, NULL
, 'o' },
142 { "owner", required_argument
, NULL
, ARG_OWNER
},
143 { "fsck", required_argument
, NULL
, ARG_FSCK
},
144 { "description", required_argument
, NULL
, ARG_DESCRIPTION
},
145 { "property", required_argument
, NULL
, 'p' },
146 { "automount", required_argument
, NULL
, ARG_AUTOMOUNT
},
147 { "timeout-idle-sec", required_argument
, NULL
, ARG_TIMEOUT_IDLE
},
148 { "automount-property", required_argument
, NULL
, ARG_AUTOMOUNT_PROPERTY
},
149 { "bind-device", no_argument
, NULL
, ARG_BIND_DEVICE
},
150 { "list", no_argument
, NULL
, ARG_LIST
},
151 { "umount", no_argument
, NULL
, 'u' },
152 { "unmount", no_argument
, NULL
, 'u' },
153 { "collect", no_argument
, NULL
, 'G' },
162 if (strstr(program_invocation_short_name
, "systemd-umount"))
163 arg_action
= ACTION_UMOUNT
;
165 while ((c
= getopt_long(argc
, argv
, "hqH:M:t:o:p:AuG", options
, NULL
)) >= 0)
183 case ARG_NO_ASK_PASSWORD
:
184 arg_ask_password
= false;
200 arg_transport
= BUS_TRANSPORT_REMOTE
;
205 arg_transport
= BUS_TRANSPORT_MACHINE
;
214 if (free_and_strdup(&arg_mount_type
, optarg
) < 0)
219 if (free_and_strdup(&arg_mount_options
, optarg
) < 0)
224 const char *user
= optarg
;
226 r
= get_user_creds(&user
, &arg_uid
, &arg_gid
, NULL
, NULL
, 0);
228 return log_error_errno(r
,
229 r
== -EBADMSG
? "UID or GID of user %s are invalid."
230 : "Cannot use \"%s\" as owner: %m",
236 r
= parse_boolean(optarg
);
238 return log_error_errno(r
, "Failed to parse --fsck= argument: %s", optarg
);
243 case ARG_DESCRIPTION
:
244 if (free_and_strdup(&arg_description
, optarg
) < 0)
249 if (strv_extend(&arg_property
, optarg
) < 0)
255 arg_action
= ACTION_AUTOMOUNT
;
259 r
= parse_boolean(optarg
);
261 return log_error_errno(r
, "--automount= expects a valid boolean parameter: %s", optarg
);
263 arg_action
= r
? ACTION_AUTOMOUNT
: ACTION_MOUNT
;
266 case ARG_TIMEOUT_IDLE
:
267 r
= parse_sec(optarg
, &arg_timeout_idle
);
269 return log_error_errno(r
, "Failed to parse timeout: %s", optarg
);
273 case ARG_AUTOMOUNT_PROPERTY
:
274 if (strv_extend(&arg_automount_property
, optarg
) < 0)
279 case ARG_BIND_DEVICE
:
280 arg_bind_device
= true;
284 arg_action
= ACTION_LIST
;
288 arg_action
= ACTION_UMOUNT
;
292 arg_aggressive_gc
= true;
299 assert_not_reached("Unhandled option");
302 if (arg_user
&& arg_transport
!= BUS_TRANSPORT_LOCAL
) {
303 log_error("Execution in user context is not supported on non-local systems.");
307 if (arg_action
== ACTION_LIST
) {
309 log_error("Too many arguments.");
313 if (arg_transport
!= BUS_TRANSPORT_LOCAL
) {
314 log_error("Listing devices only supported locally.");
317 } else if (arg_action
== ACTION_UMOUNT
) {
318 if (optind
>= argc
) {
319 log_error("At least one argument required.");
323 if (arg_transport
!= BUS_TRANSPORT_LOCAL
) {
326 for (i
= optind
; i
< argc
; i
++)
327 if (!path_is_absolute(argv
[i
]) ) {
328 log_error("Only absolute path is supported: %s", argv
[i
]);
333 if (optind
>= argc
) {
334 log_error("At least one argument required.");
338 if (argc
> optind
+2) {
339 log_error("At most two arguments required.");
343 if (arg_mount_type
&& (fstype_is_api_vfs(arg_mount_type
) || fstype_is_network(arg_mount_type
))) {
344 arg_mount_what
= strdup(argv
[optind
]);
348 } else if (arg_transport
== BUS_TRANSPORT_LOCAL
) {
349 _cleanup_free_
char *u
= NULL
;
351 u
= fstab_node_to_udev_node(argv
[optind
]);
355 r
= chase_symlinks(u
, NULL
, 0, &arg_mount_what
);
357 return log_error_errno(r
, "Failed to make path %s absolute: %m", u
);
359 arg_mount_what
= strdup(argv
[optind
]);
363 path_simplify(arg_mount_what
, false);
365 if (!path_is_absolute(arg_mount_what
)) {
366 log_error("Only absolute path is supported: %s", arg_mount_what
);
371 if (argc
> optind
+1) {
372 if (arg_transport
== BUS_TRANSPORT_LOCAL
) {
373 r
= chase_symlinks(argv
[optind
+1], NULL
, CHASE_NONEXISTENT
, &arg_mount_where
);
375 return log_error_errno(r
, "Failed to make path %s absolute: %m", argv
[optind
+1]);
377 arg_mount_where
= strdup(argv
[optind
+1]);
378 if (!arg_mount_where
)
381 path_simplify(arg_mount_where
, false);
383 if (!path_is_absolute(arg_mount_where
)) {
384 log_error("Only absolute path is supported: %s", arg_mount_where
);
391 if (arg_discover
&& arg_transport
!= BUS_TRANSPORT_LOCAL
) {
392 log_error("Automatic mount location discovery is only supported locally.");
400 static int transient_unit_set_properties(sd_bus_message
*m
, UnitType t
, char **properties
) {
403 if (!isempty(arg_description
)) {
404 r
= sd_bus_message_append(m
, "(sv)", "Description", "s", arg_description
);
409 if (arg_bind_device
&& is_device_path(arg_mount_what
)) {
410 _cleanup_free_
char *device_unit
= NULL
;
412 r
= unit_name_from_path(arg_mount_what
, ".device", &device_unit
);
416 r
= sd_bus_message_append(m
, "(sv)(sv)",
417 "After", "as", 1, device_unit
,
418 "BindsTo", "as", 1, device_unit
);
423 if (arg_aggressive_gc
) {
424 r
= sd_bus_message_append(m
, "(sv)", "CollectMode", "s", "inactive-or-failed");
429 r
= bus_append_unit_property_assignment_many(m
, t
, properties
);
436 static int transient_mount_set_properties(sd_bus_message
*m
) {
437 _cleanup_free_
char *options
= NULL
;
442 r
= transient_unit_set_properties(m
, UNIT_MOUNT
, arg_property
);
446 if (arg_mount_what
) {
447 r
= sd_bus_message_append(m
, "(sv)", "What", "s", arg_mount_what
);
452 if (arg_mount_type
) {
453 r
= sd_bus_message_append(m
, "(sv)", "Type", "s", arg_mount_type
);
458 /* Prepend uid=…,gid=… if arg_uid is set */
459 if (arg_uid
!= UID_INVALID
) {
460 r
= asprintf(&options
,
461 "uid=" UID_FMT
",gid=" GID_FMT
"%s%s",
463 arg_mount_options
? "," : "", strempty(arg_mount_options
));
468 if (options
|| arg_mount_options
) {
469 log_debug("Using mount options: %s", options
?: arg_mount_options
);
471 r
= sd_bus_message_append(m
, "(sv)", "Options", "s", options
?: arg_mount_options
);
475 log_debug("Not using any mount options");
478 _cleanup_free_
char *fsck
= NULL
;
480 r
= unit_name_from_path_instance("systemd-fsck", arg_mount_what
, ".service", &fsck
);
484 r
= sd_bus_message_append(m
,
486 "Requires", "as", 1, fsck
,
487 "After", "as", 1, fsck
);
495 static int transient_automount_set_properties(sd_bus_message
*m
) {
500 r
= transient_unit_set_properties(m
, UNIT_AUTOMOUNT
, arg_automount_property
);
504 if (arg_timeout_idle
!= USEC_INFINITY
) {
505 r
= sd_bus_message_append(m
, "(sv)", "TimeoutIdleUSec", "t", arg_timeout_idle
);
513 static int start_transient_mount(
517 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
, *reply
= NULL
;
518 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
519 _cleanup_(bus_wait_for_jobs_freep
) BusWaitForJobs
*w
= NULL
;
520 _cleanup_free_
char *mount_unit
= NULL
;
524 r
= bus_wait_for_jobs_new(bus
, &w
);
526 return log_error_errno(r
, "Could not watch jobs: %m");
529 r
= unit_name_from_path(arg_mount_where
, ".mount", &mount_unit
);
531 return log_error_errno(r
, "Failed to make mount unit name: %m");
533 r
= sd_bus_message_new_method_call(
536 "org.freedesktop.systemd1",
537 "/org/freedesktop/systemd1",
538 "org.freedesktop.systemd1.Manager",
539 "StartTransientUnit");
541 return bus_log_create_error(r
);
543 r
= sd_bus_message_set_allow_interactive_authorization(m
, arg_ask_password
);
545 return bus_log_create_error(r
);
548 r
= sd_bus_message_append(m
, "ss", mount_unit
, "fail");
550 return bus_log_create_error(r
);
553 r
= sd_bus_message_open_container(m
, 'a', "(sv)");
555 return bus_log_create_error(r
);
557 r
= transient_mount_set_properties(m
);
559 return bus_log_create_error(r
);
561 r
= sd_bus_message_close_container(m
);
563 return bus_log_create_error(r
);
565 /* Auxiliary units */
566 r
= sd_bus_message_append(m
, "a(sa(sv))", 0);
568 return bus_log_create_error(r
);
570 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
572 r
= sd_bus_call(bus
, m
, 0, &error
, &reply
);
574 return log_error_errno(r
, "Failed to start transient mount unit: %s", bus_error_message(&error
, r
));
579 r
= sd_bus_message_read(reply
, "o", &object
);
581 return bus_log_parse_error(r
);
583 r
= bus_wait_for_jobs_one(w
, object
, arg_quiet
);
589 log_info("Started unit %s%s%s for mount point: %s%s%s",
590 ansi_highlight(), mount_unit
, ansi_normal(),
591 ansi_highlight(), arg_mount_where
, ansi_normal());
596 static int start_transient_automount(
600 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
, *reply
= NULL
;
601 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
602 _cleanup_(bus_wait_for_jobs_freep
) BusWaitForJobs
*w
= NULL
;
603 _cleanup_free_
char *automount_unit
= NULL
, *mount_unit
= NULL
;
607 r
= bus_wait_for_jobs_new(bus
, &w
);
609 return log_error_errno(r
, "Could not watch jobs: %m");
612 r
= unit_name_from_path(arg_mount_where
, ".automount", &automount_unit
);
614 return log_error_errno(r
, "Failed to make automount unit name: %m");
616 r
= unit_name_from_path(arg_mount_where
, ".mount", &mount_unit
);
618 return log_error_errno(r
, "Failed to make mount unit name: %m");
620 r
= sd_bus_message_new_method_call(
623 "org.freedesktop.systemd1",
624 "/org/freedesktop/systemd1",
625 "org.freedesktop.systemd1.Manager",
626 "StartTransientUnit");
628 return bus_log_create_error(r
);
630 r
= sd_bus_message_set_allow_interactive_authorization(m
, arg_ask_password
);
632 return bus_log_create_error(r
);
635 r
= sd_bus_message_append(m
, "ss", automount_unit
, "fail");
637 return bus_log_create_error(r
);
640 r
= sd_bus_message_open_container(m
, 'a', "(sv)");
642 return bus_log_create_error(r
);
644 r
= transient_automount_set_properties(m
);
646 return bus_log_create_error(r
);
648 r
= sd_bus_message_close_container(m
);
650 return bus_log_create_error(r
);
652 /* Auxiliary units */
653 r
= sd_bus_message_open_container(m
, 'a', "(sa(sv))");
655 return bus_log_create_error(r
);
657 r
= sd_bus_message_open_container(m
, 'r', "sa(sv)");
659 return bus_log_create_error(r
);
661 r
= sd_bus_message_append(m
, "s", mount_unit
);
663 return bus_log_create_error(r
);
665 r
= sd_bus_message_open_container(m
, 'a', "(sv)");
667 return bus_log_create_error(r
);
669 r
= transient_mount_set_properties(m
);
671 return bus_log_create_error(r
);
673 r
= sd_bus_message_close_container(m
);
675 return bus_log_create_error(r
);
677 r
= sd_bus_message_close_container(m
);
679 return bus_log_create_error(r
);
681 r
= sd_bus_message_close_container(m
);
683 return bus_log_create_error(r
);
685 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
687 r
= sd_bus_call(bus
, m
, 0, &error
, &reply
);
689 return log_error_errno(r
, "Failed to start transient automount unit: %s", bus_error_message(&error
, r
));
694 r
= sd_bus_message_read(reply
, "o", &object
);
696 return bus_log_parse_error(r
);
698 r
= bus_wait_for_jobs_one(w
, object
, arg_quiet
);
704 log_info("Started unit %s%s%s for mount point: %s%s%s",
705 ansi_highlight(), automount_unit
, ansi_normal(),
706 ansi_highlight(), arg_mount_where
, ansi_normal());
711 static int find_mount_points(const char *what
, char ***list
) {
712 _cleanup_fclose_
FILE *proc_self_mountinfo
= NULL
;
713 _cleanup_strv_free_
char **l
= NULL
;
714 size_t bufsize
= 0, n
= 0;
719 /* Returns all mount points obtained from /proc/self/mountinfo in *list,
720 * and the number of mount points as return value. */
722 proc_self_mountinfo
= fopen("/proc/self/mountinfo", "re");
723 if (!proc_self_mountinfo
)
724 return log_error_errno(errno
, "Can't open /proc/self/mountinfo: %m");
727 _cleanup_free_
char *path
= NULL
, *where
= NULL
, *dev
= NULL
;
730 r
= fscanf(proc_self_mountinfo
,
731 "%*s " /* (1) mount id */
732 "%*s " /* (2) parent id */
733 "%*s " /* (3) major:minor */
734 "%*s " /* (4) root */
735 "%ms " /* (5) mount point */
736 "%*s" /* (6) mount options */
737 "%*[^-]" /* (7) optional fields */
738 "- " /* (8) separator */
739 "%*s " /* (9) file system type */
740 "%ms" /* (10) mount source */
741 "%*s" /* (11) mount options 2 */
742 "%*[^\n]", /* some rubbish at the end */
751 if (!streq(what
, dev
))
754 r
= cunescape(path
, UNESCAPE_RELAX
, &where
);
758 /* one extra slot is needed for the terminating NULL */
759 if (!GREEDY_REALLOC(l
, bufsize
, n
+ 2))
762 l
[n
++] = TAKE_PTR(where
);
765 if (!GREEDY_REALLOC(l
, bufsize
, n
+ 1))
774 static int find_loop_device(const char *backing_file
, char **loop_dev
) {
775 _cleanup_closedir_
DIR *d
= NULL
;
777 _cleanup_free_
char *l
= NULL
;
779 assert(backing_file
);
782 d
= opendir("/sys/devices/virtual/block");
786 FOREACH_DIRENT(de
, d
, return -errno
) {
787 _cleanup_free_
char *sys
= NULL
, *fname
= NULL
;
790 dirent_ensure_type(d
, de
);
792 if (de
->d_type
!= DT_DIR
)
795 if (!startswith(de
->d_name
, "loop"))
798 sys
= strjoin("/sys/devices/virtual/block/", de
->d_name
, "/loop/backing_file");
802 r
= read_one_line_file(sys
, &fname
);
804 log_debug_errno(r
, "Failed to read %s, ignoring: %m", sys
);
808 if (files_same(fname
, backing_file
, 0) <= 0)
811 l
= strjoin("/dev/", de
->d_name
);
821 *loop_dev
= TAKE_PTR(l
);
826 static int stop_mount(
829 const char *suffix
) {
831 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
, *reply
= NULL
;
832 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
833 _cleanup_(bus_wait_for_jobs_freep
) BusWaitForJobs
*w
= NULL
;
834 _cleanup_free_
char *mount_unit
= NULL
;
838 r
= bus_wait_for_jobs_new(bus
, &w
);
840 return log_error_errno(r
, "Could not watch jobs: %m");
843 r
= unit_name_from_path(where
, suffix
, &mount_unit
);
845 return log_error_errno(r
, "Failed to make %s unit name from path %s: %m", suffix
+ 1, where
);
847 r
= sd_bus_message_new_method_call(
850 "org.freedesktop.systemd1",
851 "/org/freedesktop/systemd1",
852 "org.freedesktop.systemd1.Manager",
855 return bus_log_create_error(r
);
857 r
= sd_bus_message_set_allow_interactive_authorization(m
, arg_ask_password
);
859 return bus_log_create_error(r
);
862 r
= sd_bus_message_append(m
, "ss", mount_unit
, "fail");
864 return bus_log_create_error(r
);
866 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
868 r
= sd_bus_call(bus
, m
, 0, &error
, &reply
);
870 if (streq(suffix
, ".automount") &&
871 sd_bus_error_has_name(&error
, "org.freedesktop.systemd1.NoSuchUnit"))
873 return log_error_errno(r
, "Failed to stop %s unit: %s", suffix
+ 1, bus_error_message(&error
, r
));
879 r
= sd_bus_message_read(reply
, "o", &object
);
881 return bus_log_parse_error(r
);
883 r
= bus_wait_for_jobs_one(w
, object
, arg_quiet
);
889 log_info("Stopped unit %s%s%s for mount point: %s%s%s",
890 ansi_highlight(), mount_unit
, ansi_normal(),
891 ansi_highlight(), where
, ansi_normal());
896 static int stop_mounts(
902 if (path_equal(where
, "/")) {
903 log_error("Refusing to operate on root directory: %s", where
);
907 if (!path_is_normalized(where
)) {
908 log_error("Path contains non-normalized components: %s", where
);
912 r
= stop_mount(bus
, where
, ".mount");
916 r
= stop_mount(bus
, where
, ".automount");
923 static int umount_by_device(sd_bus
*bus
, const char *what
) {
924 _cleanup_(sd_device_unrefp
) sd_device
*d
= NULL
;
925 _cleanup_strv_free_
char **list
= NULL
;
933 if (stat(what
, &st
) < 0)
934 return log_error_errno(errno
, "Can't stat %s: %m", what
);
936 if (!S_ISBLK(st
.st_mode
)) {
937 log_error("Not a block device: %s", what
);
941 r
= sd_device_new_from_devnum(&d
, 'b', st
.st_rdev
);
943 return log_error_errno(r
, "Failed to get device from device number: %m");
945 r
= sd_device_get_property_value(d
, "ID_FS_USAGE", &v
);
947 return log_error_errno(r
, "Failed to get device property: %m");
949 if (!streq(v
, "filesystem")) {
950 log_error("%s does not contain a known file system.", what
);
954 if (sd_device_get_property_value(d
, "SYSTEMD_MOUNT_WHERE", &v
) >= 0)
955 r2
= stop_mounts(bus
, v
);
957 r
= find_mount_points(what
, &list
);
961 for (l
= list
; *l
; l
++) {
962 r
= stop_mounts(bus
, *l
);
970 static int umount_loop(sd_bus
*bus
, const char *backing_file
) {
971 _cleanup_free_
char *loop_dev
= NULL
;
974 assert(backing_file
);
976 r
= find_loop_device(backing_file
, &loop_dev
);
978 return log_error_errno(r
, r
== -ENXIO
? "File %s is not mounted." : "Can't get loop device for %s: %m", backing_file
);
980 return umount_by_device(bus
, loop_dev
);
983 static int action_umount(
990 if (arg_transport
!= BUS_TRANSPORT_LOCAL
) {
991 for (i
= optind
; i
< argc
; i
++) {
992 _cleanup_free_
char *p
= NULL
;
998 path_simplify(p
, false);
1000 r
= stop_mounts(bus
, p
);
1007 for (i
= optind
; i
< argc
; i
++) {
1008 _cleanup_free_
char *u
= NULL
, *p
= NULL
;
1011 u
= fstab_node_to_udev_node(argv
[i
]);
1015 r
= chase_symlinks(u
, NULL
, 0, &p
);
1017 r2
= log_error_errno(r
, "Failed to make path %s absolute: %m", argv
[i
]);
1021 if (stat(p
, &st
) < 0)
1022 return log_error_errno(errno
, "Can't stat %s (from %s): %m", p
, argv
[i
]);
1024 if (S_ISBLK(st
.st_mode
))
1025 r
= umount_by_device(bus
, p
);
1026 else if (S_ISREG(st
.st_mode
))
1027 r
= umount_loop(bus
, p
);
1028 else if (S_ISDIR(st
.st_mode
))
1029 r
= stop_mounts(bus
, p
);
1031 log_error("Invalid file type: %s (from %s)", p
, argv
[i
]);
1042 static int acquire_mount_type(sd_device
*d
) {
1050 if (sd_device_get_property_value(d
, "ID_FS_TYPE", &v
) < 0)
1053 arg_mount_type
= strdup(v
);
1054 if (!arg_mount_type
)
1057 log_debug("Discovered type=%s", arg_mount_type
);
1061 static int acquire_mount_options(sd_device
*d
) {
1066 if (arg_mount_options
)
1069 if (sd_device_get_property_value(d
, "SYSTEMD_MOUNT_OPTIONS", &v
) < 0)
1072 arg_mount_options
= strdup(v
);
1073 if (!arg_mount_options
)
1076 log_debug("Discovered options=%s", arg_mount_options
);
1080 static const char *get_model(sd_device
*d
) {
1085 if (sd_device_get_property_value(d
, "ID_MODEL_FROM_DATABASE", &model
) >= 0)
1088 if (sd_device_get_property_value(d
, "ID_MODEL", &model
) >= 0)
1094 static const char* get_label(sd_device
*d
) {
1099 if (sd_device_get_property_value(d
, "ID_FS_LABEL", &label
) >= 0)
1102 if (sd_device_get_property_value(d
, "ID_PART_ENTRY_NAME", &label
) >= 0)
1108 static int acquire_mount_where(sd_device
*d
) {
1111 if (arg_mount_where
)
1114 if (sd_device_get_property_value(d
, "SYSTEMD_MOUNT_WHERE", &v
) < 0) {
1115 _cleanup_free_
char *escaped
= NULL
;
1118 name
= get_label(d
);
1120 name
= get_model(d
);
1124 if (sd_device_get_devname(d
, &dn
) < 0)
1127 name
= basename(dn
);
1130 escaped
= xescape(name
, "\\");
1133 if (!filename_is_valid(escaped
))
1136 arg_mount_where
= strjoin("/run/media/system/", escaped
);
1138 arg_mount_where
= strdup(v
);
1140 if (!arg_mount_where
)
1143 log_debug("Discovered where=%s", arg_mount_where
);
1147 static int acquire_mount_where_for_loop_dev(const char *loop_dev
) {
1148 _cleanup_strv_free_
char **list
= NULL
;
1151 if (arg_mount_where
)
1154 r
= find_mount_points(loop_dev
, &list
);
1158 log_error("Can't find mount point of %s. It is expected that %s is already mounted on a place.", loop_dev
, loop_dev
);
1160 } else if (r
>= 2) {
1161 log_error("%s is mounted on %d places. It is expected that %s is mounted on a place.", loop_dev
, r
, loop_dev
);
1165 arg_mount_where
= strdup(list
[0]);
1166 if (!arg_mount_where
)
1169 log_debug("Discovered where=%s", arg_mount_where
);
1173 static int acquire_description(sd_device
*d
) {
1174 const char *model
, *label
;
1176 if (arg_description
)
1179 model
= get_model(d
);
1181 label
= get_label(d
);
1183 (void) sd_device_get_property_value(d
, "ID_PART_ENTRY_NUMBER", &label
);
1186 arg_description
= strjoin(model
, " ", label
);
1188 arg_description
= strdup(label
);
1190 arg_description
= strdup(model
);
1194 if (!arg_description
)
1197 log_debug("Discovered description=%s", arg_description
);
1201 static int acquire_removable(sd_device
*d
) {
1204 /* Shortcut this if there's no reason to check it */
1205 if (arg_action
!= ACTION_DEFAULT
&& arg_timeout_idle_set
&& arg_bind_device
>= 0)
1209 if (sd_device_get_sysattr_value(d
, "removable", &v
) > 0)
1212 if (sd_device_get_parent(d
, &d
) < 0)
1215 if (sd_device_get_subsystem(d
, &v
) < 0 || !streq(v
, "block"))
1219 if (parse_boolean(v
) <= 0)
1222 log_debug("Discovered removable device.");
1224 if (arg_action
== ACTION_DEFAULT
) {
1225 log_debug("Automatically turning on automount.");
1226 arg_action
= ACTION_AUTOMOUNT
;
1229 if (!arg_timeout_idle_set
) {
1230 log_debug("Setting idle timeout to 1s.");
1231 arg_timeout_idle
= USEC_PER_SEC
;
1234 if (arg_bind_device
< 0) {
1235 log_debug("Binding automount unit to device.");
1236 arg_bind_device
= true;
1242 static int discover_loop_backing_file(void) {
1243 _cleanup_(sd_device_unrefp
) sd_device
*d
= NULL
;
1244 _cleanup_free_
char *loop_dev
= NULL
;
1249 r
= find_loop_device(arg_mount_what
, &loop_dev
);
1250 if (r
< 0 && r
!= -ENXIO
)
1251 return log_error_errno(errno
, "Can't get loop device for %s: %m", arg_mount_what
);
1254 _cleanup_free_
char *escaped
= NULL
;
1256 if (arg_mount_where
)
1259 escaped
= xescape(basename(arg_mount_what
), "\\");
1262 if (!filename_is_valid(escaped
)) {
1263 log_error("Escaped name %s is not a valid filename.", escaped
);
1267 arg_mount_where
= strjoin("/run/media/system/", escaped
);
1268 if (!arg_mount_where
)
1271 log_debug("Discovered where=%s", arg_mount_where
);
1275 if (stat(loop_dev
, &st
) < 0)
1276 return log_error_errno(errno
, "Can't stat %s: %m", loop_dev
);
1278 if (!S_ISBLK(st
.st_mode
)) {
1279 log_error("Invalid file type: %s", loop_dev
);
1283 r
= sd_device_new_from_devnum(&d
, 'b', st
.st_rdev
);
1285 return log_error_errno(r
, "Failed to get device from device number: %m");
1287 if (sd_device_get_property_value(d
, "ID_FS_USAGE", &v
) < 0 || !streq(v
, "filesystem")) {
1288 log_error("%s does not contain a known file system.", arg_mount_what
);
1292 r
= acquire_mount_type(d
);
1296 r
= acquire_mount_options(d
);
1300 r
= acquire_mount_where_for_loop_dev(loop_dev
);
1304 r
= acquire_description(d
);
1311 static int discover_device(void) {
1312 _cleanup_(sd_device_unrefp
) sd_device
*d
= NULL
;
1317 if (stat(arg_mount_what
, &st
) < 0)
1318 return log_error_errno(errno
, "Can't stat %s: %m", arg_mount_what
);
1320 if (S_ISREG(st
.st_mode
))
1321 return discover_loop_backing_file();
1323 if (!S_ISBLK(st
.st_mode
)) {
1324 log_error("Invalid file type: %s", arg_mount_what
);
1328 r
= sd_device_new_from_devnum(&d
, 'b', st
.st_rdev
);
1330 return log_error_errno(r
, "Failed to get device from device number: %m");
1332 if (sd_device_get_property_value(d
, "ID_FS_USAGE", &v
) < 0 || !streq(v
, "filesystem")) {
1333 log_error("%s does not contain a known file system.", arg_mount_what
);
1337 r
= acquire_mount_type(d
);
1341 r
= acquire_mount_options(d
);
1345 r
= acquire_mount_where(d
);
1349 r
= acquire_description(d
);
1353 r
= acquire_removable(d
);
1372 char* columns
[_COLUMN_MAX
];
1375 static int compare_item(const struct item
*a
, const struct item
*b
) {
1376 if (a
->columns
[COLUMN_NODE
] == b
->columns
[COLUMN_NODE
])
1378 if (!a
->columns
[COLUMN_NODE
])
1380 if (!b
->columns
[COLUMN_NODE
])
1383 return path_compare(a
->columns
[COLUMN_NODE
], b
->columns
[COLUMN_NODE
]);
1386 static int list_devices(void) {
1388 static const char * const titles
[_COLUMN_MAX
] = {
1389 [COLUMN_NODE
] = "NODE",
1390 [COLUMN_PATH
] = "PATH",
1391 [COLUMN_MODEL
] = "MODEL",
1392 [COLUMN_WWN
] = "WWN",
1393 [COLUMN_FSTYPE
] = "TYPE",
1394 [COLUMN_LABEL
] = "LABEL",
1395 [COLUMN_UUID
] = "UUID"
1398 _cleanup_(sd_device_enumerator_unrefp
) sd_device_enumerator
*e
= NULL
;
1399 size_t n_allocated
= 0, n
= 0, i
;
1400 size_t column_width
[_COLUMN_MAX
];
1401 struct item
*items
= NULL
;
1406 for (c
= 0; c
< _COLUMN_MAX
; c
++)
1407 column_width
[c
] = strlen(titles
[c
]);
1409 r
= sd_device_enumerator_new(&e
);
1413 r
= sd_device_enumerator_add_match_subsystem(e
, "block", true);
1415 return log_error_errno(r
, "Failed to add block match: %m");
1417 r
= sd_device_enumerator_add_match_property(e
, "ID_FS_USAGE", "filesystem");
1419 return log_error_errno(r
, "Failed to add property match: %m");
1421 FOREACH_DEVICE(e
, d
) {
1424 if (!GREEDY_REALLOC0(items
, n_allocated
, n
+1)) {
1431 for (c
= 0; c
< _COLUMN_MAX
; c
++) {
1432 const char *x
= NULL
;
1438 (void) sd_device_get_devname(d
, &x
);
1442 (void) sd_device_get_property_value(d
, "ID_PATH", &x
);
1450 (void) sd_device_get_property_value(d
, "ID_WWN", &x
);
1454 (void) sd_device_get_property_value(d
, "ID_FS_TYPE", &x
);
1462 (void) sd_device_get_property_value(d
, "ID_FS_UUID", &x
);
1469 j
->columns
[c
] = strdup(x
);
1470 if (!j
->columns
[c
]) {
1476 if (k
> column_width
[c
])
1477 column_width
[c
] = k
;
1482 log_info("No devices found.");
1486 typesafe_qsort(items
, n
, compare_item
);
1488 (void) pager_open(arg_no_pager
, false);
1490 fputs(ansi_underline(), stdout
);
1491 for (c
= 0; c
< _COLUMN_MAX
; c
++) {
1495 printf("%-*s", (int) column_width
[c
], titles
[c
]);
1497 fputs(ansi_normal(), stdout
);
1498 fputc('\n', stdout
);
1500 for (i
= 0; i
< n
; i
++) {
1501 for (c
= 0; c
< _COLUMN_MAX
; c
++) {
1505 printf("%-*s", (int) column_width
[c
], strna(items
[i
].columns
[c
]));
1507 fputc('\n', stdout
);
1513 for (i
= 0; i
< n
; i
++)
1514 for (c
= 0; c
< _COLUMN_MAX
; c
++)
1515 free(items
[i
].columns
[c
]);
1521 int main(int argc
, char* argv
[]) {
1525 log_parse_environment();
1528 r
= parse_argv(argc
, argv
);
1532 if (arg_action
== ACTION_LIST
) {
1537 r
= bus_connect_transport_systemd(arg_transport
, arg_host
, arg_user
, &bus
);
1539 log_error_errno(r
, "Failed to create bus connection: %m");
1543 if (arg_action
== ACTION_UMOUNT
) {
1544 r
= action_umount(bus
, argc
, argv
);
1548 if (!path_is_normalized(arg_mount_what
)) {
1549 log_error("Path contains non-normalized components: %s", arg_mount_what
);
1555 r
= discover_device();
1560 if (!arg_mount_where
) {
1561 log_error("Can't figure out where to mount %s.", arg_mount_what
);
1566 if (path_equal(arg_mount_where
, "/")) {
1567 log_error("Refusing to operate on root directory.");
1572 if (!path_is_normalized(arg_mount_where
)) {
1573 log_error("Path contains non-normalized components: %s", arg_mount_where
);
1578 if (streq_ptr(arg_mount_type
, "auto"))
1579 arg_mount_type
= mfree(arg_mount_type
);
1580 if (streq_ptr(arg_mount_options
, "defaults"))
1581 arg_mount_options
= mfree(arg_mount_options
);
1583 if (!is_device_path(arg_mount_what
))
1586 if (arg_fsck
&& arg_mount_type
&& arg_transport
== BUS_TRANSPORT_LOCAL
) {
1587 r
= fsck_exists(arg_mount_type
);
1589 log_warning_errno(r
, "Couldn't determine whether fsck for %s exists, proceeding anyway.", arg_mount_type
);
1591 log_debug("Disabling file system check as fsck for %s doesn't exist.", arg_mount_type
);
1592 arg_fsck
= false; /* fsck doesn't exist, let's not attempt it */
1596 /* The kernel (properly) refuses mounting file systems with unknown uid=,gid= options,
1597 * but not for all filesystem types. Let's try to catch the cases where the option
1598 * would be used if the file system does not support it. It is also possible to
1599 * autodetect the file system, but that's only possible with disk-based file systems
1600 * which incidentally seem to be implemented more carefully and reject unknown options,
1601 * so it's probably OK that we do the check only when the type is specified.
1603 if (arg_mount_type
&&
1604 !streq(arg_mount_type
, "auto") &&
1605 arg_uid
!= UID_INVALID
&&
1606 !fstype_can_uid_gid(arg_mount_type
)) {
1607 log_error("File system type %s is not known to support uid=/gid=, refusing.",
1613 switch (arg_action
) {
1616 case ACTION_DEFAULT
:
1617 r
= start_transient_mount(bus
, argv
+ optind
);
1620 case ACTION_AUTOMOUNT
:
1621 r
= start_transient_automount(bus
, argv
+ optind
);
1625 assert_not_reached("Unexpected action.");
1629 /* make sure we terminate the bus connection first, and then close the
1630 * pager, see issue #3543 for the details. */
1631 bus
= sd_bus_flush_close_unref(bus
);
1634 free(arg_mount_what
);
1635 free(arg_mount_where
);
1636 free(arg_mount_type
);
1637 free(arg_mount_options
);
1638 free(arg_description
);
1639 strv_free(arg_property
);
1640 strv_free(arg_automount_property
);
1642 return r
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
;