1 /* SPDX-License-Identifier: LGPL-2.1+ */
9 #include "bus-unit-util.h"
11 #include "bus-wait-for-jobs.h"
12 #include "device-util.h"
13 #include "dirent-util.h"
17 #include "format-util.h"
19 #include "fstab-util.h"
20 #include "main-func.h"
21 #include "mount-util.h"
22 #include "mountpoint-util.h"
24 #include "parse-util.h"
25 #include "path-util.h"
26 #include "pretty-print.h"
27 #include "sort-util.h"
28 #include "spawn-polkit-agent.h"
29 #include "stat-util.h"
31 #include "terminal-util.h"
33 #include "unit-name.h"
34 #include "user-util.h"
42 } arg_action
= ACTION_DEFAULT
;
44 static bool arg_no_block
= false;
45 static PagerFlags arg_pager_flags
= 0;
46 static bool arg_ask_password
= true;
47 static bool arg_quiet
= false;
48 static BusTransport arg_transport
= BUS_TRANSPORT_LOCAL
;
49 static bool arg_user
= false;
50 static const char *arg_host
= NULL
;
51 static bool arg_discover
= false;
52 static char *arg_mount_what
= NULL
;
53 static char *arg_mount_where
= NULL
;
54 static char *arg_mount_type
= NULL
;
55 static char *arg_mount_options
= NULL
;
56 static char *arg_description
= NULL
;
57 static char **arg_property
= NULL
;
58 static usec_t arg_timeout_idle
= USEC_INFINITY
;
59 static bool arg_timeout_idle_set
= false;
60 static char **arg_automount_property
= NULL
;
61 static int arg_bind_device
= -1;
62 static uid_t arg_uid
= UID_INVALID
;
63 static gid_t arg_gid
= GID_INVALID
;
64 static bool arg_fsck
= true;
65 static bool arg_aggressive_gc
= false;
67 STATIC_DESTRUCTOR_REGISTER(arg_mount_what
, freep
);
68 STATIC_DESTRUCTOR_REGISTER(arg_mount_where
, freep
);
69 STATIC_DESTRUCTOR_REGISTER(arg_mount_type
, freep
);
70 STATIC_DESTRUCTOR_REGISTER(arg_mount_options
, freep
);
71 STATIC_DESTRUCTOR_REGISTER(arg_description
, freep
);
72 STATIC_DESTRUCTOR_REGISTER(arg_property
, strv_freep
);
73 STATIC_DESTRUCTOR_REGISTER(arg_automount_property
, strv_freep
);
75 static int help(void) {
76 _cleanup_free_
char *link
= NULL
;
79 r
= terminal_urlify_man("systemd-mount", "1", &link
);
83 printf("systemd-mount [OPTIONS...] WHAT [WHERE]\n"
84 "systemd-mount [OPTIONS...] --list\n"
85 "%s [OPTIONS...] %sWHAT|WHERE...\n\n"
86 "Establish a mount or auto-mount point transiently.\n\n"
87 " -h --help Show this help\n"
88 " --version Show package version\n"
89 " --no-block Do not wait until operation finished\n"
90 " --no-pager Do not pipe output into a pager\n"
91 " --no-ask-password Do not prompt for password\n"
92 " -q --quiet Suppress information messages during runtime\n"
93 " --user Run as user unit\n"
94 " -H --host=[USER@]HOST Operate on remote host\n"
95 " -M --machine=CONTAINER Operate on local container\n"
96 " --discover Discover mount device metadata\n"
97 " -t --type=TYPE File system type\n"
98 " -o --options=OPTIONS Mount options\n"
99 " --owner=USER Add uid= and gid= options for USER\n"
100 " --fsck=no Don't run file system check before mount\n"
101 " --description=TEXT Description for unit\n"
102 " -p --property=NAME=VALUE Set mount unit property\n"
103 " -A --automount=BOOL Create an auto-mount point\n"
104 " --timeout-idle-sec=SEC Specify automount idle timeout\n"
105 " --automount-property=NAME=VALUE\n"
106 " Set automount unit property\n"
107 " --bind-device Bind automount unit to device\n"
108 " --list List mountable block devices\n"
109 " -u --umount Unmount mount points\n"
110 " -G --collect Unload unit after it stopped, even when failed\n"
111 "\nSee the %s for details.\n"
112 , program_invocation_short_name
113 , streq(program_invocation_short_name
, "systemd-umount") ? "" : "--umount "
120 static int parse_argv(int argc
, char *argv
[]) {
137 ARG_AUTOMOUNT_PROPERTY
,
142 static const struct option options
[] = {
143 { "help", no_argument
, NULL
, 'h' },
144 { "version", no_argument
, NULL
, ARG_VERSION
},
145 { "no-block", no_argument
, NULL
, ARG_NO_BLOCK
},
146 { "no-pager", no_argument
, NULL
, ARG_NO_PAGER
},
147 { "no-ask-password", no_argument
, NULL
, ARG_NO_ASK_PASSWORD
},
148 { "quiet", no_argument
, NULL
, 'q' },
149 { "user", no_argument
, NULL
, ARG_USER
},
150 { "system", no_argument
, NULL
, ARG_SYSTEM
},
151 { "host", required_argument
, NULL
, 'H' },
152 { "machine", required_argument
, NULL
, 'M' },
153 { "discover", no_argument
, NULL
, ARG_DISCOVER
},
154 { "type", required_argument
, NULL
, 't' },
155 { "options", required_argument
, NULL
, 'o' },
156 { "owner", required_argument
, NULL
, ARG_OWNER
},
157 { "fsck", required_argument
, NULL
, ARG_FSCK
},
158 { "description", required_argument
, NULL
, ARG_DESCRIPTION
},
159 { "property", required_argument
, NULL
, 'p' },
160 { "automount", required_argument
, NULL
, ARG_AUTOMOUNT
},
161 { "timeout-idle-sec", required_argument
, NULL
, ARG_TIMEOUT_IDLE
},
162 { "automount-property", required_argument
, NULL
, ARG_AUTOMOUNT_PROPERTY
},
163 { "bind-device", no_argument
, NULL
, ARG_BIND_DEVICE
},
164 { "list", no_argument
, NULL
, ARG_LIST
},
165 { "umount", no_argument
, NULL
, 'u' },
166 { "unmount", no_argument
, NULL
, 'u' },
167 { "collect", no_argument
, NULL
, 'G' },
176 if (strstr(program_invocation_short_name
, "systemd-umount"))
177 arg_action
= ACTION_UMOUNT
;
179 while ((c
= getopt_long(argc
, argv
, "hqH:M:t:o:p:AuG", options
, NULL
)) >= 0)
194 arg_pager_flags
|= PAGER_DISABLE
;
197 case ARG_NO_ASK_PASSWORD
:
198 arg_ask_password
= false;
214 arg_transport
= BUS_TRANSPORT_REMOTE
;
219 arg_transport
= BUS_TRANSPORT_MACHINE
;
228 if (free_and_strdup(&arg_mount_type
, optarg
) < 0)
233 if (free_and_strdup(&arg_mount_options
, optarg
) < 0)
238 const char *user
= optarg
;
240 r
= get_user_creds(&user
, &arg_uid
, &arg_gid
, NULL
, NULL
, 0);
242 return log_error_errno(r
,
243 r
== -EBADMSG
? "UID or GID of user %s are invalid."
244 : "Cannot use \"%s\" as owner: %m",
250 r
= parse_boolean(optarg
);
252 return log_error_errno(r
, "Failed to parse --fsck= argument: %s", optarg
);
257 case ARG_DESCRIPTION
:
258 if (free_and_strdup(&arg_description
, optarg
) < 0)
263 if (strv_extend(&arg_property
, optarg
) < 0)
269 arg_action
= ACTION_AUTOMOUNT
;
273 r
= parse_boolean(optarg
);
275 return log_error_errno(r
, "--automount= expects a valid boolean parameter: %s", optarg
);
277 arg_action
= r
? ACTION_AUTOMOUNT
: ACTION_MOUNT
;
280 case ARG_TIMEOUT_IDLE
:
281 r
= parse_sec(optarg
, &arg_timeout_idle
);
283 return log_error_errno(r
, "Failed to parse timeout: %s", optarg
);
287 case ARG_AUTOMOUNT_PROPERTY
:
288 if (strv_extend(&arg_automount_property
, optarg
) < 0)
293 case ARG_BIND_DEVICE
:
294 arg_bind_device
= true;
298 arg_action
= ACTION_LIST
;
302 arg_action
= ACTION_UMOUNT
;
306 arg_aggressive_gc
= true;
313 assert_not_reached("Unhandled option");
316 if (arg_user
&& arg_transport
!= BUS_TRANSPORT_LOCAL
)
317 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
318 "Execution in user context is not supported on non-local systems.");
320 if (arg_action
== ACTION_LIST
) {
322 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
323 "Too many arguments.");
325 if (arg_transport
!= BUS_TRANSPORT_LOCAL
)
326 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP
),
327 "Listing devices only supported locally.");
328 } else if (arg_action
== ACTION_UMOUNT
) {
330 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
331 "At least one argument required.");
333 if (arg_transport
!= BUS_TRANSPORT_LOCAL
) {
336 for (i
= optind
; i
< argc
; i
++)
337 if (!path_is_absolute(argv
[i
]) )
338 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
339 "Only absolute path is supported: %s", argv
[i
]);
343 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
344 "At least one argument required.");
347 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
348 "At most two arguments required.");
350 if (arg_mount_type
&& (fstype_is_api_vfs(arg_mount_type
) || fstype_is_network(arg_mount_type
))) {
351 arg_mount_what
= strdup(argv
[optind
]);
355 } else if (arg_transport
== BUS_TRANSPORT_LOCAL
) {
356 _cleanup_free_
char *u
= NULL
;
358 u
= fstab_node_to_udev_node(argv
[optind
]);
362 r
= chase_symlinks(u
, NULL
, 0, &arg_mount_what
);
364 return log_error_errno(r
, "Failed to make path %s absolute: %m", u
);
366 arg_mount_what
= strdup(argv
[optind
]);
370 path_simplify(arg_mount_what
, false);
372 if (!path_is_absolute(arg_mount_what
))
373 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
374 "Only absolute path is supported: %s", arg_mount_what
);
377 if (argc
> optind
+1) {
378 if (arg_transport
== BUS_TRANSPORT_LOCAL
) {
379 r
= chase_symlinks(argv
[optind
+1], NULL
, CHASE_NONEXISTENT
, &arg_mount_where
);
381 return log_error_errno(r
, "Failed to make path %s absolute: %m", argv
[optind
+1]);
383 arg_mount_where
= strdup(argv
[optind
+1]);
384 if (!arg_mount_where
)
387 path_simplify(arg_mount_where
, false);
389 if (!path_is_absolute(arg_mount_where
))
390 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
391 "Only absolute path is supported: %s", arg_mount_where
);
396 if (arg_discover
&& arg_transport
!= BUS_TRANSPORT_LOCAL
)
397 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP
),
398 "Automatic mount location discovery is only supported locally.");
404 static int transient_unit_set_properties(sd_bus_message
*m
, UnitType t
, char **properties
) {
407 if (!isempty(arg_description
)) {
408 r
= sd_bus_message_append(m
, "(sv)", "Description", "s", arg_description
);
413 if (arg_bind_device
&& is_device_path(arg_mount_what
)) {
414 _cleanup_free_
char *device_unit
= NULL
;
416 r
= unit_name_from_path(arg_mount_what
, ".device", &device_unit
);
420 r
= sd_bus_message_append(m
, "(sv)(sv)",
421 "After", "as", 1, device_unit
,
422 "BindsTo", "as", 1, device_unit
);
427 if (arg_aggressive_gc
) {
428 r
= sd_bus_message_append(m
, "(sv)", "CollectMode", "s", "inactive-or-failed");
433 r
= bus_append_unit_property_assignment_many(m
, t
, properties
);
440 static int transient_mount_set_properties(sd_bus_message
*m
) {
441 _cleanup_free_
char *options
= NULL
;
446 r
= transient_unit_set_properties(m
, UNIT_MOUNT
, arg_property
);
450 if (arg_mount_what
) {
451 r
= sd_bus_message_append(m
, "(sv)", "What", "s", arg_mount_what
);
456 if (arg_mount_type
) {
457 r
= sd_bus_message_append(m
, "(sv)", "Type", "s", arg_mount_type
);
462 /* Prepend uid=…,gid=… if arg_uid is set */
463 if (arg_uid
!= UID_INVALID
) {
464 r
= asprintf(&options
,
465 "uid=" UID_FMT
",gid=" GID_FMT
"%s%s",
467 arg_mount_options
? "," : "", strempty(arg_mount_options
));
472 if (options
|| arg_mount_options
) {
473 log_debug("Using mount options: %s", options
?: arg_mount_options
);
475 r
= sd_bus_message_append(m
, "(sv)", "Options", "s", options
?: arg_mount_options
);
479 log_debug("Not using any mount options");
482 _cleanup_free_
char *fsck
= NULL
;
484 r
= unit_name_from_path_instance("systemd-fsck", arg_mount_what
, ".service", &fsck
);
488 r
= sd_bus_message_append(m
,
490 "Requires", "as", 1, fsck
,
491 "After", "as", 1, fsck
);
499 static int transient_automount_set_properties(sd_bus_message
*m
) {
504 r
= transient_unit_set_properties(m
, UNIT_AUTOMOUNT
, arg_automount_property
);
508 if (arg_timeout_idle
!= USEC_INFINITY
) {
509 r
= sd_bus_message_append(m
, "(sv)", "TimeoutIdleUSec", "t", arg_timeout_idle
);
517 static int start_transient_mount(
521 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
, *reply
= NULL
;
522 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
523 _cleanup_(bus_wait_for_jobs_freep
) BusWaitForJobs
*w
= NULL
;
524 _cleanup_free_
char *mount_unit
= NULL
;
528 r
= bus_wait_for_jobs_new(bus
, &w
);
530 return log_error_errno(r
, "Could not watch jobs: %m");
533 r
= unit_name_from_path(arg_mount_where
, ".mount", &mount_unit
);
535 return log_error_errno(r
, "Failed to make mount unit name: %m");
537 r
= sd_bus_message_new_method_call(
540 "org.freedesktop.systemd1",
541 "/org/freedesktop/systemd1",
542 "org.freedesktop.systemd1.Manager",
543 "StartTransientUnit");
545 return bus_log_create_error(r
);
547 r
= sd_bus_message_set_allow_interactive_authorization(m
, arg_ask_password
);
549 return bus_log_create_error(r
);
552 r
= sd_bus_message_append(m
, "ss", mount_unit
, "fail");
554 return bus_log_create_error(r
);
557 r
= sd_bus_message_open_container(m
, 'a', "(sv)");
559 return bus_log_create_error(r
);
561 r
= transient_mount_set_properties(m
);
563 return bus_log_create_error(r
);
565 r
= sd_bus_message_close_container(m
);
567 return bus_log_create_error(r
);
569 /* Auxiliary units */
570 r
= sd_bus_message_append(m
, "a(sa(sv))", 0);
572 return bus_log_create_error(r
);
574 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
576 r
= sd_bus_call(bus
, m
, 0, &error
, &reply
);
578 return log_error_errno(r
, "Failed to start transient mount unit: %s", bus_error_message(&error
, r
));
583 r
= sd_bus_message_read(reply
, "o", &object
);
585 return bus_log_parse_error(r
);
587 r
= bus_wait_for_jobs_one(w
, object
, arg_quiet
);
593 log_info("Started unit %s%s%s for mount point: %s%s%s",
594 ansi_highlight(), mount_unit
, ansi_normal(),
595 ansi_highlight(), arg_mount_where
, ansi_normal());
600 static int start_transient_automount(
604 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
, *reply
= NULL
;
605 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
606 _cleanup_(bus_wait_for_jobs_freep
) BusWaitForJobs
*w
= NULL
;
607 _cleanup_free_
char *automount_unit
= NULL
, *mount_unit
= NULL
;
611 r
= bus_wait_for_jobs_new(bus
, &w
);
613 return log_error_errno(r
, "Could not watch jobs: %m");
616 r
= unit_name_from_path(arg_mount_where
, ".automount", &automount_unit
);
618 return log_error_errno(r
, "Failed to make automount unit name: %m");
620 r
= unit_name_from_path(arg_mount_where
, ".mount", &mount_unit
);
622 return log_error_errno(r
, "Failed to make mount unit name: %m");
624 r
= sd_bus_message_new_method_call(
627 "org.freedesktop.systemd1",
628 "/org/freedesktop/systemd1",
629 "org.freedesktop.systemd1.Manager",
630 "StartTransientUnit");
632 return bus_log_create_error(r
);
634 r
= sd_bus_message_set_allow_interactive_authorization(m
, arg_ask_password
);
636 return bus_log_create_error(r
);
639 r
= sd_bus_message_append(m
, "ss", automount_unit
, "fail");
641 return bus_log_create_error(r
);
644 r
= sd_bus_message_open_container(m
, 'a', "(sv)");
646 return bus_log_create_error(r
);
648 r
= transient_automount_set_properties(m
);
650 return bus_log_create_error(r
);
652 r
= sd_bus_message_close_container(m
);
654 return bus_log_create_error(r
);
656 /* Auxiliary units */
657 r
= sd_bus_message_open_container(m
, 'a', "(sa(sv))");
659 return bus_log_create_error(r
);
661 r
= sd_bus_message_open_container(m
, 'r', "sa(sv)");
663 return bus_log_create_error(r
);
665 r
= sd_bus_message_append(m
, "s", mount_unit
);
667 return bus_log_create_error(r
);
669 r
= sd_bus_message_open_container(m
, 'a', "(sv)");
671 return bus_log_create_error(r
);
673 r
= transient_mount_set_properties(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 r
= sd_bus_message_close_container(m
);
687 return bus_log_create_error(r
);
689 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
691 r
= sd_bus_call(bus
, m
, 0, &error
, &reply
);
693 return log_error_errno(r
, "Failed to start transient automount unit: %s", bus_error_message(&error
, r
));
698 r
= sd_bus_message_read(reply
, "o", &object
);
700 return bus_log_parse_error(r
);
702 r
= bus_wait_for_jobs_one(w
, object
, arg_quiet
);
708 log_info("Started unit %s%s%s for mount point: %s%s%s",
709 ansi_highlight(), automount_unit
, ansi_normal(),
710 ansi_highlight(), arg_mount_where
, ansi_normal());
715 static int find_mount_points(const char *what
, char ***list
) {
716 _cleanup_fclose_
FILE *proc_self_mountinfo
= NULL
;
717 _cleanup_strv_free_
char **l
= NULL
;
718 size_t bufsize
= 0, n
= 0;
723 /* Returns all mount points obtained from /proc/self/mountinfo in *list,
724 * and the number of mount points as return value. */
726 proc_self_mountinfo
= fopen("/proc/self/mountinfo", "re");
727 if (!proc_self_mountinfo
)
728 return log_error_errno(errno
, "Can't open /proc/self/mountinfo: %m");
731 _cleanup_free_
char *path
= NULL
, *where
= NULL
, *dev
= NULL
;
734 r
= fscanf(proc_self_mountinfo
,
735 "%*s " /* (1) mount id */
736 "%*s " /* (2) parent id */
737 "%*s " /* (3) major:minor */
738 "%*s " /* (4) root */
739 "%ms " /* (5) mount point */
740 "%*s" /* (6) mount options */
741 "%*[^-]" /* (7) optional fields */
742 "- " /* (8) separator */
743 "%*s " /* (9) file system type */
744 "%ms" /* (10) mount source */
745 "%*s" /* (11) mount options 2 */
746 "%*[^\n]", /* some rubbish at the end */
755 if (!streq(what
, dev
))
758 r
= cunescape(path
, UNESCAPE_RELAX
, &where
);
762 /* one extra slot is needed for the terminating NULL */
763 if (!GREEDY_REALLOC(l
, bufsize
, n
+ 2))
766 l
[n
++] = TAKE_PTR(where
);
769 if (!GREEDY_REALLOC(l
, bufsize
, n
+ 1))
778 static int find_loop_device(const char *backing_file
, char **loop_dev
) {
779 _cleanup_closedir_
DIR *d
= NULL
;
781 _cleanup_free_
char *l
= NULL
;
783 assert(backing_file
);
786 d
= opendir("/sys/devices/virtual/block");
790 FOREACH_DIRENT(de
, d
, return -errno
) {
791 _cleanup_free_
char *sys
= NULL
, *fname
= NULL
;
794 dirent_ensure_type(d
, de
);
796 if (de
->d_type
!= DT_DIR
)
799 if (!startswith(de
->d_name
, "loop"))
802 sys
= strjoin("/sys/devices/virtual/block/", de
->d_name
, "/loop/backing_file");
806 r
= read_one_line_file(sys
, &fname
);
808 log_debug_errno(r
, "Failed to read %s, ignoring: %m", sys
);
812 if (files_same(fname
, backing_file
, 0) <= 0)
815 l
= strjoin("/dev/", de
->d_name
);
825 *loop_dev
= TAKE_PTR(l
);
830 static int stop_mount(
833 const char *suffix
) {
835 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
, *reply
= NULL
;
836 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
837 _cleanup_(bus_wait_for_jobs_freep
) BusWaitForJobs
*w
= NULL
;
838 _cleanup_free_
char *mount_unit
= NULL
;
842 r
= bus_wait_for_jobs_new(bus
, &w
);
844 return log_error_errno(r
, "Could not watch jobs: %m");
847 r
= unit_name_from_path(where
, suffix
, &mount_unit
);
849 return log_error_errno(r
, "Failed to make %s unit name from path %s: %m", suffix
+ 1, where
);
851 r
= sd_bus_message_new_method_call(
854 "org.freedesktop.systemd1",
855 "/org/freedesktop/systemd1",
856 "org.freedesktop.systemd1.Manager",
859 return bus_log_create_error(r
);
861 r
= sd_bus_message_set_allow_interactive_authorization(m
, arg_ask_password
);
863 return bus_log_create_error(r
);
866 r
= sd_bus_message_append(m
, "ss", mount_unit
, "fail");
868 return bus_log_create_error(r
);
870 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
872 r
= sd_bus_call(bus
, m
, 0, &error
, &reply
);
874 if (streq(suffix
, ".automount") &&
875 sd_bus_error_has_name(&error
, "org.freedesktop.systemd1.NoSuchUnit"))
877 return log_error_errno(r
, "Failed to stop %s unit: %s", suffix
+ 1, bus_error_message(&error
, r
));
883 r
= sd_bus_message_read(reply
, "o", &object
);
885 return bus_log_parse_error(r
);
887 r
= bus_wait_for_jobs_one(w
, object
, arg_quiet
);
893 log_info("Stopped unit %s%s%s for mount point: %s%s%s",
894 ansi_highlight(), mount_unit
, ansi_normal(),
895 ansi_highlight(), where
, ansi_normal());
900 static int stop_mounts(
906 if (path_equal(where
, "/"))
907 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
908 "Refusing to operate on root directory: %s", where
);
910 if (!path_is_normalized(where
))
911 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
912 "Path contains non-normalized components: %s", where
);
914 r
= stop_mount(bus
, where
, ".mount");
918 r
= stop_mount(bus
, where
, ".automount");
925 static int umount_by_device(sd_bus
*bus
, const char *what
) {
926 _cleanup_(sd_device_unrefp
) sd_device
*d
= NULL
;
927 _cleanup_strv_free_
char **list
= NULL
;
935 if (stat(what
, &st
) < 0)
936 return log_error_errno(errno
, "Can't stat %s: %m", what
);
938 if (!S_ISBLK(st
.st_mode
)) {
939 log_error("Not a block device: %s", what
);
943 r
= sd_device_new_from_devnum(&d
, 'b', st
.st_rdev
);
945 return log_error_errno(r
, "Failed to get device from device number: %m");
947 r
= sd_device_get_property_value(d
, "ID_FS_USAGE", &v
);
949 return log_device_error_errno(d
, r
, "Failed to get device property: %m");
951 if (!streq(v
, "filesystem")) {
952 log_device_error(d
, "%s does not contain a known file system.", what
);
956 if (sd_device_get_property_value(d
, "SYSTEMD_MOUNT_WHERE", &v
) >= 0)
957 r2
= stop_mounts(bus
, v
);
959 r
= find_mount_points(what
, &list
);
963 for (l
= list
; *l
; l
++) {
964 r
= stop_mounts(bus
, *l
);
972 static int umount_loop(sd_bus
*bus
, const char *backing_file
) {
973 _cleanup_free_
char *loop_dev
= NULL
;
976 assert(backing_file
);
978 r
= find_loop_device(backing_file
, &loop_dev
);
980 return log_error_errno(r
, r
== -ENXIO
? "File %s is not mounted." : "Can't get loop device for %s: %m", backing_file
);
982 return umount_by_device(bus
, loop_dev
);
985 static int action_umount(
992 if (arg_transport
!= BUS_TRANSPORT_LOCAL
) {
993 for (i
= optind
; i
< argc
; i
++) {
994 _cleanup_free_
char *p
= NULL
;
1000 path_simplify(p
, false);
1002 r
= stop_mounts(bus
, p
);
1009 for (i
= optind
; i
< argc
; i
++) {
1010 _cleanup_free_
char *u
= NULL
, *p
= NULL
;
1013 u
= fstab_node_to_udev_node(argv
[i
]);
1017 r
= chase_symlinks(u
, NULL
, 0, &p
);
1019 r2
= log_error_errno(r
, "Failed to make path %s absolute: %m", argv
[i
]);
1023 if (stat(p
, &st
) < 0)
1024 return log_error_errno(errno
, "Can't stat %s (from %s): %m", p
, argv
[i
]);
1026 if (S_ISBLK(st
.st_mode
))
1027 r
= umount_by_device(bus
, p
);
1028 else if (S_ISREG(st
.st_mode
))
1029 r
= umount_loop(bus
, p
);
1030 else if (S_ISDIR(st
.st_mode
))
1031 r
= stop_mounts(bus
, p
);
1033 log_error("Invalid file type: %s (from %s)", p
, argv
[i
]);
1044 static int acquire_mount_type(sd_device
*d
) {
1052 if (sd_device_get_property_value(d
, "ID_FS_TYPE", &v
) < 0)
1055 arg_mount_type
= strdup(v
);
1056 if (!arg_mount_type
)
1059 log_debug("Discovered type=%s", arg_mount_type
);
1063 static int acquire_mount_options(sd_device
*d
) {
1068 if (arg_mount_options
)
1071 if (sd_device_get_property_value(d
, "SYSTEMD_MOUNT_OPTIONS", &v
) < 0)
1074 arg_mount_options
= strdup(v
);
1075 if (!arg_mount_options
)
1078 log_debug("Discovered options=%s", arg_mount_options
);
1082 static const char *get_model(sd_device
*d
) {
1087 if (sd_device_get_property_value(d
, "ID_MODEL_FROM_DATABASE", &model
) >= 0)
1090 if (sd_device_get_property_value(d
, "ID_MODEL", &model
) >= 0)
1096 static const char* get_label(sd_device
*d
) {
1101 if (sd_device_get_property_value(d
, "ID_FS_LABEL", &label
) >= 0)
1104 if (sd_device_get_property_value(d
, "ID_PART_ENTRY_NAME", &label
) >= 0)
1110 static int acquire_mount_where(sd_device
*d
) {
1113 if (arg_mount_where
)
1116 if (sd_device_get_property_value(d
, "SYSTEMD_MOUNT_WHERE", &v
) < 0) {
1117 _cleanup_free_
char *escaped
= NULL
;
1120 name
= get_label(d
);
1122 name
= get_model(d
);
1126 if (sd_device_get_devname(d
, &dn
) < 0)
1129 name
= basename(dn
);
1132 escaped
= xescape(name
, "\\");
1135 if (!filename_is_valid(escaped
))
1138 arg_mount_where
= strjoin("/run/media/system/", escaped
);
1140 arg_mount_where
= strdup(v
);
1142 if (!arg_mount_where
)
1145 log_debug("Discovered where=%s", arg_mount_where
);
1149 static int acquire_mount_where_for_loop_dev(const char *loop_dev
) {
1150 _cleanup_strv_free_
char **list
= NULL
;
1153 if (arg_mount_where
)
1156 r
= find_mount_points(loop_dev
, &list
);
1160 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1161 "Can't find mount point of %s. It is expected that %s is already mounted on a place.",
1162 loop_dev
, loop_dev
);
1164 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1165 "%s is mounted on %d places. It is expected that %s is mounted on a place.",
1166 loop_dev
, r
, loop_dev
);
1168 arg_mount_where
= strdup(list
[0]);
1169 if (!arg_mount_where
)
1172 log_debug("Discovered where=%s", arg_mount_where
);
1176 static int acquire_description(sd_device
*d
) {
1177 const char *model
, *label
;
1179 if (arg_description
)
1182 model
= get_model(d
);
1184 label
= get_label(d
);
1186 (void) sd_device_get_property_value(d
, "ID_PART_ENTRY_NUMBER", &label
);
1189 arg_description
= strjoin(model
, " ", label
);
1191 arg_description
= strdup(label
);
1193 arg_description
= strdup(model
);
1197 if (!arg_description
)
1200 log_debug("Discovered description=%s", arg_description
);
1204 static int acquire_removable(sd_device
*d
) {
1207 /* Shortcut this if there's no reason to check it */
1208 if (arg_action
!= ACTION_DEFAULT
&& arg_timeout_idle_set
&& arg_bind_device
>= 0)
1212 if (sd_device_get_sysattr_value(d
, "removable", &v
) > 0)
1215 if (sd_device_get_parent(d
, &d
) < 0)
1218 if (sd_device_get_subsystem(d
, &v
) < 0 || !streq(v
, "block"))
1222 if (parse_boolean(v
) <= 0)
1225 log_debug("Discovered removable device.");
1227 if (arg_action
== ACTION_DEFAULT
) {
1228 log_debug("Automatically turning on automount.");
1229 arg_action
= ACTION_AUTOMOUNT
;
1232 if (!arg_timeout_idle_set
) {
1233 log_debug("Setting idle timeout to 1s.");
1234 arg_timeout_idle
= USEC_PER_SEC
;
1237 if (arg_bind_device
< 0) {
1238 log_debug("Binding automount unit to device.");
1239 arg_bind_device
= true;
1245 static int discover_loop_backing_file(void) {
1246 _cleanup_(sd_device_unrefp
) sd_device
*d
= NULL
;
1247 _cleanup_free_
char *loop_dev
= NULL
;
1252 r
= find_loop_device(arg_mount_what
, &loop_dev
);
1253 if (r
< 0 && r
!= -ENXIO
)
1254 return log_error_errno(errno
, "Can't get loop device for %s: %m", arg_mount_what
);
1257 _cleanup_free_
char *escaped
= NULL
;
1259 if (arg_mount_where
)
1262 escaped
= xescape(basename(arg_mount_what
), "\\");
1265 if (!filename_is_valid(escaped
)) {
1266 log_error("Escaped name %s is not a valid filename.", escaped
);
1270 arg_mount_where
= strjoin("/run/media/system/", escaped
);
1271 if (!arg_mount_where
)
1274 log_debug("Discovered where=%s", arg_mount_where
);
1278 if (stat(loop_dev
, &st
) < 0)
1279 return log_error_errno(errno
, "Can't stat %s: %m", loop_dev
);
1281 if (!S_ISBLK(st
.st_mode
)) {
1282 log_error("Invalid file type: %s", loop_dev
);
1286 r
= sd_device_new_from_devnum(&d
, 'b', st
.st_rdev
);
1288 return log_error_errno(r
, "Failed to get device from device number: %m");
1290 if (sd_device_get_property_value(d
, "ID_FS_USAGE", &v
) < 0 || !streq(v
, "filesystem")) {
1291 log_device_error(d
, "%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_(sd_device_unrefp
) sd_device
*d
= NULL
;
1320 if (stat(arg_mount_what
, &st
) < 0)
1321 return log_error_errno(errno
, "Can't stat %s: %m", arg_mount_what
);
1323 if (S_ISREG(st
.st_mode
))
1324 return discover_loop_backing_file();
1326 if (!S_ISBLK(st
.st_mode
)) {
1327 log_error("Invalid file type: %s", arg_mount_what
);
1331 r
= sd_device_new_from_devnum(&d
, 'b', st
.st_rdev
);
1333 return log_error_errno(r
, "Failed to get device from device number: %m");
1335 if (sd_device_get_property_value(d
, "ID_FS_USAGE", &v
) < 0 || !streq(v
, "filesystem")) {
1336 log_error("%s does not contain a known file system.", arg_mount_what
);
1340 r
= acquire_mount_type(d
);
1344 r
= acquire_mount_options(d
);
1348 r
= acquire_mount_where(d
);
1352 r
= acquire_description(d
);
1356 r
= acquire_removable(d
);
1375 char* columns
[_COLUMN_MAX
];
1378 static int compare_item(const struct item
*a
, const struct item
*b
) {
1379 if (a
->columns
[COLUMN_NODE
] == b
->columns
[COLUMN_NODE
])
1381 if (!a
->columns
[COLUMN_NODE
])
1383 if (!b
->columns
[COLUMN_NODE
])
1386 return path_compare(a
->columns
[COLUMN_NODE
], b
->columns
[COLUMN_NODE
]);
1389 static int list_devices(void) {
1391 static const char * const titles
[_COLUMN_MAX
] = {
1392 [COLUMN_NODE
] = "NODE",
1393 [COLUMN_PATH
] = "PATH",
1394 [COLUMN_MODEL
] = "MODEL",
1395 [COLUMN_WWN
] = "WWN",
1396 [COLUMN_FSTYPE
] = "TYPE",
1397 [COLUMN_LABEL
] = "LABEL",
1398 [COLUMN_UUID
] = "UUID"
1401 _cleanup_(sd_device_enumerator_unrefp
) sd_device_enumerator
*e
= NULL
;
1402 size_t n_allocated
= 0, n
= 0, i
;
1403 size_t column_width
[_COLUMN_MAX
];
1404 struct item
*items
= NULL
;
1409 for (c
= 0; c
< _COLUMN_MAX
; c
++)
1410 column_width
[c
] = strlen(titles
[c
]);
1412 r
= sd_device_enumerator_new(&e
);
1416 r
= sd_device_enumerator_add_match_subsystem(e
, "block", true);
1418 return log_error_errno(r
, "Failed to add block match: %m");
1420 r
= sd_device_enumerator_add_match_property(e
, "ID_FS_USAGE", "filesystem");
1422 return log_error_errno(r
, "Failed to add property match: %m");
1424 FOREACH_DEVICE(e
, d
) {
1427 if (!GREEDY_REALLOC0(items
, n_allocated
, n
+1)) {
1434 for (c
= 0; c
< _COLUMN_MAX
; c
++) {
1435 const char *x
= NULL
;
1441 (void) sd_device_get_devname(d
, &x
);
1445 (void) sd_device_get_property_value(d
, "ID_PATH", &x
);
1453 (void) sd_device_get_property_value(d
, "ID_WWN", &x
);
1457 (void) sd_device_get_property_value(d
, "ID_FS_TYPE", &x
);
1465 (void) sd_device_get_property_value(d
, "ID_FS_UUID", &x
);
1472 j
->columns
[c
] = strdup(x
);
1473 if (!j
->columns
[c
]) {
1479 if (k
> column_width
[c
])
1480 column_width
[c
] = k
;
1485 log_info("No devices found.");
1489 typesafe_qsort(items
, n
, compare_item
);
1491 (void) pager_open(arg_pager_flags
);
1493 fputs(ansi_underline(), stdout
);
1494 for (c
= 0; c
< _COLUMN_MAX
; c
++) {
1498 printf("%-*s", (int) column_width
[c
], titles
[c
]);
1500 fputs(ansi_normal(), stdout
);
1501 fputc('\n', stdout
);
1503 for (i
= 0; i
< n
; i
++) {
1504 for (c
= 0; c
< _COLUMN_MAX
; c
++) {
1508 printf("%-*s", (int) column_width
[c
], strna(items
[i
].columns
[c
]));
1510 fputc('\n', stdout
);
1516 for (i
= 0; i
< n
; i
++)
1517 for (c
= 0; c
< _COLUMN_MAX
; c
++)
1518 free(items
[i
].columns
[c
]);
1524 static int run(int argc
, char* argv
[]) {
1525 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1528 log_parse_environment();
1531 r
= parse_argv(argc
, argv
);
1535 if (arg_action
== ACTION_LIST
)
1536 return list_devices();
1538 r
= bus_connect_transport_systemd(arg_transport
, arg_host
, arg_user
, &bus
);
1540 return log_error_errno(r
, "Failed to create bus connection: %m");
1542 if (arg_action
== ACTION_UMOUNT
)
1543 return action_umount(bus
, argc
, argv
);
1545 if (!path_is_normalized(arg_mount_what
)) {
1546 log_error("Path contains non-normalized components: %s", arg_mount_what
);
1551 r
= discover_device();
1556 if (!arg_mount_where
) {
1557 log_error("Can't figure out where to mount %s.", arg_mount_what
);
1561 if (path_equal(arg_mount_where
, "/")) {
1562 log_error("Refusing to operate on root directory.");
1566 if (!path_is_normalized(arg_mount_where
)) {
1567 log_error("Path contains non-normalized components: %s", arg_mount_where
);
1571 if (streq_ptr(arg_mount_type
, "auto"))
1572 arg_mount_type
= mfree(arg_mount_type
);
1573 if (streq_ptr(arg_mount_options
, "defaults"))
1574 arg_mount_options
= mfree(arg_mount_options
);
1576 if (!is_device_path(arg_mount_what
))
1579 if (arg_fsck
&& arg_mount_type
&& arg_transport
== BUS_TRANSPORT_LOCAL
) {
1580 r
= fsck_exists(arg_mount_type
);
1582 log_warning_errno(r
, "Couldn't determine whether fsck for %s exists, proceeding anyway.", arg_mount_type
);
1584 log_debug("Disabling file system check as fsck for %s doesn't exist.", arg_mount_type
);
1585 arg_fsck
= false; /* fsck doesn't exist, let's not attempt it */
1589 /* The kernel (properly) refuses mounting file systems with unknown uid=,gid= options,
1590 * but not for all filesystem types. Let's try to catch the cases where the option
1591 * would be used if the file system does not support it. It is also possible to
1592 * autodetect the file system, but that's only possible with disk-based file systems
1593 * which incidentally seem to be implemented more carefully and reject unknown options,
1594 * so it's probably OK that we do the check only when the type is specified.
1596 if (arg_mount_type
&&
1597 !streq(arg_mount_type
, "auto") &&
1598 arg_uid
!= UID_INVALID
&&
1599 !fstype_can_uid_gid(arg_mount_type
)) {
1600 log_error("File system type %s is not known to support uid=/gid=, refusing.",
1605 switch (arg_action
) {
1608 case ACTION_DEFAULT
:
1609 r
= start_transient_mount(bus
, argv
+ optind
);
1612 case ACTION_AUTOMOUNT
:
1613 r
= start_transient_automount(bus
, argv
+ optind
);
1617 assert_not_reached("Unexpected action.");
1623 DEFINE_MAIN_FUNCTION(run
);