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 "main-func.h"
19 #include "mount-util.h"
21 #include "parse-util.h"
22 #include "path-util.h"
23 #include "pretty-print.h"
24 #include "spawn-polkit-agent.h"
25 #include "stat-util.h"
28 #include "unit-name.h"
29 #include "user-util.h"
30 #include "terminal-util.h"
38 } arg_action
= ACTION_DEFAULT
;
40 static bool arg_no_block
= false;
41 static PagerFlags arg_pager_flags
= 0;
42 static bool arg_ask_password
= true;
43 static bool arg_quiet
= false;
44 static BusTransport arg_transport
= BUS_TRANSPORT_LOCAL
;
45 static bool arg_user
= false;
46 static const char *arg_host
= NULL
;
47 static bool arg_discover
= false;
48 static char *arg_mount_what
= NULL
;
49 static char *arg_mount_where
= NULL
;
50 static char *arg_mount_type
= NULL
;
51 static char *arg_mount_options
= NULL
;
52 static char *arg_description
= NULL
;
53 static char **arg_property
= NULL
;
54 static usec_t arg_timeout_idle
= USEC_INFINITY
;
55 static bool arg_timeout_idle_set
= false;
56 static char **arg_automount_property
= NULL
;
57 static int arg_bind_device
= -1;
58 static uid_t arg_uid
= UID_INVALID
;
59 static gid_t arg_gid
= GID_INVALID
;
60 static bool arg_fsck
= true;
61 static bool arg_aggressive_gc
= false;
63 STATIC_DESTRUCTOR_REGISTER(arg_mount_what
, freep
);
64 STATIC_DESTRUCTOR_REGISTER(arg_mount_where
, freep
);
65 STATIC_DESTRUCTOR_REGISTER(arg_mount_type
, freep
);
66 STATIC_DESTRUCTOR_REGISTER(arg_mount_options
, freep
);
67 STATIC_DESTRUCTOR_REGISTER(arg_description
, freep
);
68 STATIC_DESTRUCTOR_REGISTER(arg_property
, strv_freep
);
69 STATIC_DESTRUCTOR_REGISTER(arg_automount_property
, strv_freep
);
71 static int help(void) {
72 _cleanup_free_
char *link
= NULL
;
75 r
= terminal_urlify_man("systemd-mount", "1", &link
);
79 printf("systemd-mount [OPTIONS...] WHAT [WHERE]\n"
80 "systemd-mount [OPTIONS...] --list\n"
81 "%s [OPTIONS...] %sWHAT|WHERE...\n\n"
82 "Establish a mount or auto-mount point transiently.\n\n"
83 " -h --help Show this help\n"
84 " --version Show package version\n"
85 " --no-block Do not wait until operation finished\n"
86 " --no-pager Do not pipe output into a pager\n"
87 " --no-ask-password Do not prompt for password\n"
88 " -q --quiet Suppress information messages during runtime\n"
89 " --user Run as user unit\n"
90 " -H --host=[USER@]HOST Operate on remote host\n"
91 " -M --machine=CONTAINER Operate on local container\n"
92 " --discover Discover mount device metadata\n"
93 " -t --type=TYPE File system type\n"
94 " -o --options=OPTIONS Mount options\n"
95 " --owner=USER Add uid= and gid= options for USER\n"
96 " --fsck=no Don't run file system check before mount\n"
97 " --description=TEXT Description for unit\n"
98 " -p --property=NAME=VALUE Set mount unit property\n"
99 " -A --automount=BOOL Create an auto-mount point\n"
100 " --timeout-idle-sec=SEC Specify automount idle timeout\n"
101 " --automount-property=NAME=VALUE\n"
102 " Set automount unit property\n"
103 " --bind-device Bind automount unit to device\n"
104 " --list List mountable block devices\n"
105 " -u --umount Unmount mount points\n"
106 " -G --collect Unload unit after it stopped, even when failed\n"
107 "\nSee the %s for details.\n"
108 , program_invocation_short_name
109 , streq(program_invocation_short_name
, "systemd-umount") ? "" : "--umount "
116 static int parse_argv(int argc
, char *argv
[]) {
133 ARG_AUTOMOUNT_PROPERTY
,
138 static const struct option options
[] = {
139 { "help", no_argument
, NULL
, 'h' },
140 { "version", no_argument
, NULL
, ARG_VERSION
},
141 { "no-block", no_argument
, NULL
, ARG_NO_BLOCK
},
142 { "no-pager", no_argument
, NULL
, ARG_NO_PAGER
},
143 { "no-ask-password", no_argument
, NULL
, ARG_NO_ASK_PASSWORD
},
144 { "quiet", no_argument
, NULL
, 'q' },
145 { "user", no_argument
, NULL
, ARG_USER
},
146 { "system", no_argument
, NULL
, ARG_SYSTEM
},
147 { "host", required_argument
, NULL
, 'H' },
148 { "machine", required_argument
, NULL
, 'M' },
149 { "discover", no_argument
, NULL
, ARG_DISCOVER
},
150 { "type", required_argument
, NULL
, 't' },
151 { "options", required_argument
, NULL
, 'o' },
152 { "owner", required_argument
, NULL
, ARG_OWNER
},
153 { "fsck", required_argument
, NULL
, ARG_FSCK
},
154 { "description", required_argument
, NULL
, ARG_DESCRIPTION
},
155 { "property", required_argument
, NULL
, 'p' },
156 { "automount", required_argument
, NULL
, ARG_AUTOMOUNT
},
157 { "timeout-idle-sec", required_argument
, NULL
, ARG_TIMEOUT_IDLE
},
158 { "automount-property", required_argument
, NULL
, ARG_AUTOMOUNT_PROPERTY
},
159 { "bind-device", no_argument
, NULL
, ARG_BIND_DEVICE
},
160 { "list", no_argument
, NULL
, ARG_LIST
},
161 { "umount", no_argument
, NULL
, 'u' },
162 { "unmount", no_argument
, NULL
, 'u' },
163 { "collect", no_argument
, NULL
, 'G' },
172 if (strstr(program_invocation_short_name
, "systemd-umount"))
173 arg_action
= ACTION_UMOUNT
;
175 while ((c
= getopt_long(argc
, argv
, "hqH:M:t:o:p:AuG", options
, NULL
)) >= 0)
190 arg_pager_flags
|= PAGER_DISABLE
;
193 case ARG_NO_ASK_PASSWORD
:
194 arg_ask_password
= false;
210 arg_transport
= BUS_TRANSPORT_REMOTE
;
215 arg_transport
= BUS_TRANSPORT_MACHINE
;
224 if (free_and_strdup(&arg_mount_type
, optarg
) < 0)
229 if (free_and_strdup(&arg_mount_options
, optarg
) < 0)
234 const char *user
= optarg
;
236 r
= get_user_creds(&user
, &arg_uid
, &arg_gid
, NULL
, NULL
, 0);
238 return log_error_errno(r
,
239 r
== -EBADMSG
? "UID or GID of user %s are invalid."
240 : "Cannot use \"%s\" as owner: %m",
246 r
= parse_boolean(optarg
);
248 return log_error_errno(r
, "Failed to parse --fsck= argument: %s", optarg
);
253 case ARG_DESCRIPTION
:
254 if (free_and_strdup(&arg_description
, optarg
) < 0)
259 if (strv_extend(&arg_property
, optarg
) < 0)
265 arg_action
= ACTION_AUTOMOUNT
;
269 r
= parse_boolean(optarg
);
271 return log_error_errno(r
, "--automount= expects a valid boolean parameter: %s", optarg
);
273 arg_action
= r
? ACTION_AUTOMOUNT
: ACTION_MOUNT
;
276 case ARG_TIMEOUT_IDLE
:
277 r
= parse_sec(optarg
, &arg_timeout_idle
);
279 return log_error_errno(r
, "Failed to parse timeout: %s", optarg
);
283 case ARG_AUTOMOUNT_PROPERTY
:
284 if (strv_extend(&arg_automount_property
, optarg
) < 0)
289 case ARG_BIND_DEVICE
:
290 arg_bind_device
= true;
294 arg_action
= ACTION_LIST
;
298 arg_action
= ACTION_UMOUNT
;
302 arg_aggressive_gc
= true;
309 assert_not_reached("Unhandled option");
312 if (arg_user
&& arg_transport
!= BUS_TRANSPORT_LOCAL
)
313 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
314 "Execution in user context is not supported on non-local systems.");
316 if (arg_action
== ACTION_LIST
) {
318 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
319 "Too many arguments.");
321 if (arg_transport
!= BUS_TRANSPORT_LOCAL
)
322 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP
),
323 "Listing devices only supported locally.");
324 } else if (arg_action
== ACTION_UMOUNT
) {
326 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
327 "At least one argument required.");
329 if (arg_transport
!= BUS_TRANSPORT_LOCAL
) {
332 for (i
= optind
; i
< argc
; i
++)
333 if (!path_is_absolute(argv
[i
]) )
334 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
335 "Only absolute path is supported: %s", argv
[i
]);
339 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
340 "At least one argument required.");
343 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
344 "At most two arguments required.");
346 if (arg_mount_type
&& (fstype_is_api_vfs(arg_mount_type
) || fstype_is_network(arg_mount_type
))) {
347 arg_mount_what
= strdup(argv
[optind
]);
351 } else if (arg_transport
== BUS_TRANSPORT_LOCAL
) {
352 _cleanup_free_
char *u
= NULL
;
354 u
= fstab_node_to_udev_node(argv
[optind
]);
358 r
= chase_symlinks(u
, NULL
, 0, &arg_mount_what
);
360 return log_error_errno(r
, "Failed to make path %s absolute: %m", u
);
362 arg_mount_what
= strdup(argv
[optind
]);
366 path_simplify(arg_mount_what
, false);
368 if (!path_is_absolute(arg_mount_what
))
369 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
370 "Only absolute path is supported: %s", arg_mount_what
);
373 if (argc
> optind
+1) {
374 if (arg_transport
== BUS_TRANSPORT_LOCAL
) {
375 r
= chase_symlinks(argv
[optind
+1], NULL
, CHASE_NONEXISTENT
, &arg_mount_where
);
377 return log_error_errno(r
, "Failed to make path %s absolute: %m", argv
[optind
+1]);
379 arg_mount_where
= strdup(argv
[optind
+1]);
380 if (!arg_mount_where
)
383 path_simplify(arg_mount_where
, false);
385 if (!path_is_absolute(arg_mount_where
))
386 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
387 "Only absolute path is supported: %s", arg_mount_where
);
392 if (arg_discover
&& arg_transport
!= BUS_TRANSPORT_LOCAL
)
393 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP
),
394 "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 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
904 "Refusing to operate on root directory: %s", where
);
906 if (!path_is_normalized(where
))
907 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
908 "Path contains non-normalized components: %s", where
);
910 r
= stop_mount(bus
, where
, ".mount");
914 r
= stop_mount(bus
, where
, ".automount");
921 static int umount_by_device(sd_bus
*bus
, const char *what
) {
922 _cleanup_(sd_device_unrefp
) sd_device
*d
= NULL
;
923 _cleanup_strv_free_
char **list
= NULL
;
931 if (stat(what
, &st
) < 0)
932 return log_error_errno(errno
, "Can't stat %s: %m", what
);
934 if (!S_ISBLK(st
.st_mode
)) {
935 log_error("Not a block device: %s", what
);
939 r
= sd_device_new_from_devnum(&d
, 'b', st
.st_rdev
);
941 return log_error_errno(r
, "Failed to get device from device number: %m");
943 r
= sd_device_get_property_value(d
, "ID_FS_USAGE", &v
);
945 return log_device_error_errno(d
, r
, "Failed to get device property: %m");
947 if (!streq(v
, "filesystem")) {
948 log_device_error(d
, "%s does not contain a known file system.", what
);
952 if (sd_device_get_property_value(d
, "SYSTEMD_MOUNT_WHERE", &v
) >= 0)
953 r2
= stop_mounts(bus
, v
);
955 r
= find_mount_points(what
, &list
);
959 for (l
= list
; *l
; l
++) {
960 r
= stop_mounts(bus
, *l
);
968 static int umount_loop(sd_bus
*bus
, const char *backing_file
) {
969 _cleanup_free_
char *loop_dev
= NULL
;
972 assert(backing_file
);
974 r
= find_loop_device(backing_file
, &loop_dev
);
976 return log_error_errno(r
, r
== -ENXIO
? "File %s is not mounted." : "Can't get loop device for %s: %m", backing_file
);
978 return umount_by_device(bus
, loop_dev
);
981 static int action_umount(
988 if (arg_transport
!= BUS_TRANSPORT_LOCAL
) {
989 for (i
= optind
; i
< argc
; i
++) {
990 _cleanup_free_
char *p
= NULL
;
996 path_simplify(p
, false);
998 r
= stop_mounts(bus
, p
);
1005 for (i
= optind
; i
< argc
; i
++) {
1006 _cleanup_free_
char *u
= NULL
, *p
= NULL
;
1009 u
= fstab_node_to_udev_node(argv
[i
]);
1013 r
= chase_symlinks(u
, NULL
, 0, &p
);
1015 r2
= log_error_errno(r
, "Failed to make path %s absolute: %m", argv
[i
]);
1019 if (stat(p
, &st
) < 0)
1020 return log_error_errno(errno
, "Can't stat %s (from %s): %m", p
, argv
[i
]);
1022 if (S_ISBLK(st
.st_mode
))
1023 r
= umount_by_device(bus
, p
);
1024 else if (S_ISREG(st
.st_mode
))
1025 r
= umount_loop(bus
, p
);
1026 else if (S_ISDIR(st
.st_mode
))
1027 r
= stop_mounts(bus
, p
);
1029 log_error("Invalid file type: %s (from %s)", p
, argv
[i
]);
1040 static int acquire_mount_type(sd_device
*d
) {
1048 if (sd_device_get_property_value(d
, "ID_FS_TYPE", &v
) < 0)
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(sd_device
*d
) {
1064 if (arg_mount_options
)
1067 if (sd_device_get_property_value(d
, "SYSTEMD_MOUNT_OPTIONS", &v
) < 0)
1070 arg_mount_options
= strdup(v
);
1071 if (!arg_mount_options
)
1074 log_debug("Discovered options=%s", arg_mount_options
);
1078 static const char *get_model(sd_device
*d
) {
1083 if (sd_device_get_property_value(d
, "ID_MODEL_FROM_DATABASE", &model
) >= 0)
1086 if (sd_device_get_property_value(d
, "ID_MODEL", &model
) >= 0)
1092 static const char* get_label(sd_device
*d
) {
1097 if (sd_device_get_property_value(d
, "ID_FS_LABEL", &label
) >= 0)
1100 if (sd_device_get_property_value(d
, "ID_PART_ENTRY_NAME", &label
) >= 0)
1106 static int acquire_mount_where(sd_device
*d
) {
1109 if (arg_mount_where
)
1112 if (sd_device_get_property_value(d
, "SYSTEMD_MOUNT_WHERE", &v
) < 0) {
1113 _cleanup_free_
char *escaped
= NULL
;
1116 name
= get_label(d
);
1118 name
= get_model(d
);
1122 if (sd_device_get_devname(d
, &dn
) < 0)
1125 name
= basename(dn
);
1128 escaped
= xescape(name
, "\\");
1131 if (!filename_is_valid(escaped
))
1134 arg_mount_where
= strjoin("/run/media/system/", escaped
);
1136 arg_mount_where
= strdup(v
);
1138 if (!arg_mount_where
)
1141 log_debug("Discovered where=%s", arg_mount_where
);
1145 static int acquire_mount_where_for_loop_dev(const char *loop_dev
) {
1146 _cleanup_strv_free_
char **list
= NULL
;
1149 if (arg_mount_where
)
1152 r
= find_mount_points(loop_dev
, &list
);
1156 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1157 "Can't find mount point of %s. It is expected that %s is already mounted on a place.",
1158 loop_dev
, loop_dev
);
1160 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1161 "%s is mounted on %d places. It is expected that %s is mounted on a place.",
1162 loop_dev
, r
, loop_dev
);
1164 arg_mount_where
= strdup(list
[0]);
1165 if (!arg_mount_where
)
1168 log_debug("Discovered where=%s", arg_mount_where
);
1172 static int acquire_description(sd_device
*d
) {
1173 const char *model
, *label
;
1175 if (arg_description
)
1178 model
= get_model(d
);
1180 label
= get_label(d
);
1182 (void) sd_device_get_property_value(d
, "ID_PART_ENTRY_NUMBER", &label
);
1185 arg_description
= strjoin(model
, " ", label
);
1187 arg_description
= strdup(label
);
1189 arg_description
= strdup(model
);
1193 if (!arg_description
)
1196 log_debug("Discovered description=%s", arg_description
);
1200 static int acquire_removable(sd_device
*d
) {
1203 /* Shortcut this if there's no reason to check it */
1204 if (arg_action
!= ACTION_DEFAULT
&& arg_timeout_idle_set
&& arg_bind_device
>= 0)
1208 if (sd_device_get_sysattr_value(d
, "removable", &v
) > 0)
1211 if (sd_device_get_parent(d
, &d
) < 0)
1214 if (sd_device_get_subsystem(d
, &v
) < 0 || !streq(v
, "block"))
1218 if (parse_boolean(v
) <= 0)
1221 log_debug("Discovered removable device.");
1223 if (arg_action
== ACTION_DEFAULT
) {
1224 log_debug("Automatically turning on automount.");
1225 arg_action
= ACTION_AUTOMOUNT
;
1228 if (!arg_timeout_idle_set
) {
1229 log_debug("Setting idle timeout to 1s.");
1230 arg_timeout_idle
= USEC_PER_SEC
;
1233 if (arg_bind_device
< 0) {
1234 log_debug("Binding automount unit to device.");
1235 arg_bind_device
= true;
1241 static int discover_loop_backing_file(void) {
1242 _cleanup_(sd_device_unrefp
) sd_device
*d
= NULL
;
1243 _cleanup_free_
char *loop_dev
= NULL
;
1248 r
= find_loop_device(arg_mount_what
, &loop_dev
);
1249 if (r
< 0 && r
!= -ENXIO
)
1250 return log_error_errno(errno
, "Can't get loop device for %s: %m", arg_mount_what
);
1253 _cleanup_free_
char *escaped
= NULL
;
1255 if (arg_mount_where
)
1258 escaped
= xescape(basename(arg_mount_what
), "\\");
1261 if (!filename_is_valid(escaped
)) {
1262 log_error("Escaped name %s is not a valid filename.", escaped
);
1266 arg_mount_where
= strjoin("/run/media/system/", escaped
);
1267 if (!arg_mount_where
)
1270 log_debug("Discovered where=%s", arg_mount_where
);
1274 if (stat(loop_dev
, &st
) < 0)
1275 return log_error_errno(errno
, "Can't stat %s: %m", loop_dev
);
1277 if (!S_ISBLK(st
.st_mode
)) {
1278 log_error("Invalid file type: %s", loop_dev
);
1282 r
= sd_device_new_from_devnum(&d
, 'b', st
.st_rdev
);
1284 return log_error_errno(r
, "Failed to get device from device number: %m");
1286 if (sd_device_get_property_value(d
, "ID_FS_USAGE", &v
) < 0 || !streq(v
, "filesystem")) {
1287 log_device_error(d
, "%s does not contain a known file system.", arg_mount_what
);
1291 r
= acquire_mount_type(d
);
1295 r
= acquire_mount_options(d
);
1299 r
= acquire_mount_where_for_loop_dev(loop_dev
);
1303 r
= acquire_description(d
);
1310 static int discover_device(void) {
1311 _cleanup_(sd_device_unrefp
) sd_device
*d
= NULL
;
1316 if (stat(arg_mount_what
, &st
) < 0)
1317 return log_error_errno(errno
, "Can't stat %s: %m", arg_mount_what
);
1319 if (S_ISREG(st
.st_mode
))
1320 return discover_loop_backing_file();
1322 if (!S_ISBLK(st
.st_mode
)) {
1323 log_error("Invalid file type: %s", arg_mount_what
);
1327 r
= sd_device_new_from_devnum(&d
, 'b', st
.st_rdev
);
1329 return log_error_errno(r
, "Failed to get device from device number: %m");
1331 if (sd_device_get_property_value(d
, "ID_FS_USAGE", &v
) < 0 || !streq(v
, "filesystem")) {
1332 log_error("%s does not contain a known file system.", arg_mount_what
);
1336 r
= acquire_mount_type(d
);
1340 r
= acquire_mount_options(d
);
1344 r
= acquire_mount_where(d
);
1348 r
= acquire_description(d
);
1352 r
= acquire_removable(d
);
1371 char* columns
[_COLUMN_MAX
];
1374 static int compare_item(const struct item
*a
, const struct item
*b
) {
1375 if (a
->columns
[COLUMN_NODE
] == b
->columns
[COLUMN_NODE
])
1377 if (!a
->columns
[COLUMN_NODE
])
1379 if (!b
->columns
[COLUMN_NODE
])
1382 return path_compare(a
->columns
[COLUMN_NODE
], b
->columns
[COLUMN_NODE
]);
1385 static int list_devices(void) {
1387 static const char * const titles
[_COLUMN_MAX
] = {
1388 [COLUMN_NODE
] = "NODE",
1389 [COLUMN_PATH
] = "PATH",
1390 [COLUMN_MODEL
] = "MODEL",
1391 [COLUMN_WWN
] = "WWN",
1392 [COLUMN_FSTYPE
] = "TYPE",
1393 [COLUMN_LABEL
] = "LABEL",
1394 [COLUMN_UUID
] = "UUID"
1397 _cleanup_(sd_device_enumerator_unrefp
) sd_device_enumerator
*e
= NULL
;
1398 size_t n_allocated
= 0, n
= 0, i
;
1399 size_t column_width
[_COLUMN_MAX
];
1400 struct item
*items
= NULL
;
1405 for (c
= 0; c
< _COLUMN_MAX
; c
++)
1406 column_width
[c
] = strlen(titles
[c
]);
1408 r
= sd_device_enumerator_new(&e
);
1412 r
= sd_device_enumerator_add_match_subsystem(e
, "block", true);
1414 return log_error_errno(r
, "Failed to add block match: %m");
1416 r
= sd_device_enumerator_add_match_property(e
, "ID_FS_USAGE", "filesystem");
1418 return log_error_errno(r
, "Failed to add property match: %m");
1420 FOREACH_DEVICE(e
, d
) {
1423 if (!GREEDY_REALLOC0(items
, n_allocated
, n
+1)) {
1430 for (c
= 0; c
< _COLUMN_MAX
; c
++) {
1431 const char *x
= NULL
;
1437 (void) sd_device_get_devname(d
, &x
);
1441 (void) sd_device_get_property_value(d
, "ID_PATH", &x
);
1449 (void) sd_device_get_property_value(d
, "ID_WWN", &x
);
1453 (void) sd_device_get_property_value(d
, "ID_FS_TYPE", &x
);
1461 (void) sd_device_get_property_value(d
, "ID_FS_UUID", &x
);
1468 j
->columns
[c
] = strdup(x
);
1469 if (!j
->columns
[c
]) {
1475 if (k
> column_width
[c
])
1476 column_width
[c
] = k
;
1481 log_info("No devices found.");
1485 typesafe_qsort(items
, n
, compare_item
);
1487 (void) pager_open(arg_pager_flags
);
1489 fputs(ansi_underline(), stdout
);
1490 for (c
= 0; c
< _COLUMN_MAX
; c
++) {
1494 printf("%-*s", (int) column_width
[c
], titles
[c
]);
1496 fputs(ansi_normal(), stdout
);
1497 fputc('\n', stdout
);
1499 for (i
= 0; i
< n
; i
++) {
1500 for (c
= 0; c
< _COLUMN_MAX
; c
++) {
1504 printf("%-*s", (int) column_width
[c
], strna(items
[i
].columns
[c
]));
1506 fputc('\n', stdout
);
1512 for (i
= 0; i
< n
; i
++)
1513 for (c
= 0; c
< _COLUMN_MAX
; c
++)
1514 free(items
[i
].columns
[c
]);
1520 static int run(int argc
, char* argv
[]) {
1521 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1524 log_parse_environment();
1527 r
= parse_argv(argc
, argv
);
1531 if (arg_action
== ACTION_LIST
)
1532 return list_devices();
1534 r
= bus_connect_transport_systemd(arg_transport
, arg_host
, arg_user
, &bus
);
1536 return log_error_errno(r
, "Failed to create bus connection: %m");
1538 if (arg_action
== ACTION_UMOUNT
)
1539 return action_umount(bus
, argc
, argv
);
1541 if (!path_is_normalized(arg_mount_what
)) {
1542 log_error("Path contains non-normalized components: %s", arg_mount_what
);
1547 r
= discover_device();
1552 if (!arg_mount_where
) {
1553 log_error("Can't figure out where to mount %s.", arg_mount_what
);
1557 if (path_equal(arg_mount_where
, "/")) {
1558 log_error("Refusing to operate on root directory.");
1562 if (!path_is_normalized(arg_mount_where
)) {
1563 log_error("Path contains non-normalized components: %s", arg_mount_where
);
1567 if (streq_ptr(arg_mount_type
, "auto"))
1568 arg_mount_type
= mfree(arg_mount_type
);
1569 if (streq_ptr(arg_mount_options
, "defaults"))
1570 arg_mount_options
= mfree(arg_mount_options
);
1572 if (!is_device_path(arg_mount_what
))
1575 if (arg_fsck
&& arg_mount_type
&& arg_transport
== BUS_TRANSPORT_LOCAL
) {
1576 r
= fsck_exists(arg_mount_type
);
1578 log_warning_errno(r
, "Couldn't determine whether fsck for %s exists, proceeding anyway.", arg_mount_type
);
1580 log_debug("Disabling file system check as fsck for %s doesn't exist.", arg_mount_type
);
1581 arg_fsck
= false; /* fsck doesn't exist, let's not attempt it */
1585 /* The kernel (properly) refuses mounting file systems with unknown uid=,gid= options,
1586 * but not for all filesystem types. Let's try to catch the cases where the option
1587 * would be used if the file system does not support it. It is also possible to
1588 * autodetect the file system, but that's only possible with disk-based file systems
1589 * which incidentally seem to be implemented more carefully and reject unknown options,
1590 * so it's probably OK that we do the check only when the type is specified.
1592 if (arg_mount_type
&&
1593 !streq(arg_mount_type
, "auto") &&
1594 arg_uid
!= UID_INVALID
&&
1595 !fstype_can_uid_gid(arg_mount_type
)) {
1596 log_error("File system type %s is not known to support uid=/gid=, refusing.",
1601 switch (arg_action
) {
1604 case ACTION_DEFAULT
:
1605 r
= start_transient_mount(bus
, argv
+ optind
);
1608 case ACTION_AUTOMOUNT
:
1609 r
= start_transient_automount(bus
, argv
+ optind
);
1613 assert_not_reached("Unexpected action.");
1619 DEFINE_MAIN_FUNCTION(run
);