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"
20 #include "mountpoint-util.h"
22 #include "parse-util.h"
23 #include "path-util.h"
24 #include "pretty-print.h"
25 #include "spawn-polkit-agent.h"
26 #include "stat-util.h"
29 #include "unit-name.h"
30 #include "user-util.h"
31 #include "terminal-util.h"
39 } arg_action
= ACTION_DEFAULT
;
41 static bool arg_no_block
= false;
42 static PagerFlags arg_pager_flags
= 0;
43 static bool arg_ask_password
= true;
44 static bool arg_quiet
= false;
45 static BusTransport arg_transport
= BUS_TRANSPORT_LOCAL
;
46 static bool arg_user
= false;
47 static const char *arg_host
= NULL
;
48 static bool arg_discover
= false;
49 static char *arg_mount_what
= NULL
;
50 static char *arg_mount_where
= NULL
;
51 static char *arg_mount_type
= NULL
;
52 static char *arg_mount_options
= NULL
;
53 static char *arg_description
= NULL
;
54 static char **arg_property
= NULL
;
55 static usec_t arg_timeout_idle
= USEC_INFINITY
;
56 static bool arg_timeout_idle_set
= false;
57 static char **arg_automount_property
= NULL
;
58 static int arg_bind_device
= -1;
59 static uid_t arg_uid
= UID_INVALID
;
60 static gid_t arg_gid
= GID_INVALID
;
61 static bool arg_fsck
= true;
62 static bool arg_aggressive_gc
= false;
64 STATIC_DESTRUCTOR_REGISTER(arg_mount_what
, freep
);
65 STATIC_DESTRUCTOR_REGISTER(arg_mount_where
, freep
);
66 STATIC_DESTRUCTOR_REGISTER(arg_mount_type
, freep
);
67 STATIC_DESTRUCTOR_REGISTER(arg_mount_options
, freep
);
68 STATIC_DESTRUCTOR_REGISTER(arg_description
, freep
);
69 STATIC_DESTRUCTOR_REGISTER(arg_property
, strv_freep
);
70 STATIC_DESTRUCTOR_REGISTER(arg_automount_property
, strv_freep
);
72 static int help(void) {
73 _cleanup_free_
char *link
= NULL
;
76 r
= terminal_urlify_man("systemd-mount", "1", &link
);
80 printf("systemd-mount [OPTIONS...] WHAT [WHERE]\n"
81 "systemd-mount [OPTIONS...] --list\n"
82 "%s [OPTIONS...] %sWHAT|WHERE...\n\n"
83 "Establish a mount or auto-mount point transiently.\n\n"
84 " -h --help Show this help\n"
85 " --version Show package version\n"
86 " --no-block Do not wait until operation finished\n"
87 " --no-pager Do not pipe output into a pager\n"
88 " --no-ask-password Do not prompt for password\n"
89 " -q --quiet Suppress information messages during runtime\n"
90 " --user Run as user unit\n"
91 " -H --host=[USER@]HOST Operate on remote host\n"
92 " -M --machine=CONTAINER Operate on local container\n"
93 " --discover Discover mount device metadata\n"
94 " -t --type=TYPE File system type\n"
95 " -o --options=OPTIONS Mount options\n"
96 " --owner=USER Add uid= and gid= options for USER\n"
97 " --fsck=no Don't run file system check before mount\n"
98 " --description=TEXT Description for unit\n"
99 " -p --property=NAME=VALUE Set mount unit property\n"
100 " -A --automount=BOOL Create an auto-mount point\n"
101 " --timeout-idle-sec=SEC Specify automount idle timeout\n"
102 " --automount-property=NAME=VALUE\n"
103 " Set automount unit property\n"
104 " --bind-device Bind automount unit to device\n"
105 " --list List mountable block devices\n"
106 " -u --umount Unmount mount points\n"
107 " -G --collect Unload unit after it stopped, even when failed\n"
108 "\nSee the %s for details.\n"
109 , program_invocation_short_name
110 , streq(program_invocation_short_name
, "systemd-umount") ? "" : "--umount "
117 static int parse_argv(int argc
, char *argv
[]) {
134 ARG_AUTOMOUNT_PROPERTY
,
139 static const struct option options
[] = {
140 { "help", no_argument
, NULL
, 'h' },
141 { "version", no_argument
, NULL
, ARG_VERSION
},
142 { "no-block", no_argument
, NULL
, ARG_NO_BLOCK
},
143 { "no-pager", no_argument
, NULL
, ARG_NO_PAGER
},
144 { "no-ask-password", no_argument
, NULL
, ARG_NO_ASK_PASSWORD
},
145 { "quiet", no_argument
, NULL
, 'q' },
146 { "user", no_argument
, NULL
, ARG_USER
},
147 { "system", no_argument
, NULL
, ARG_SYSTEM
},
148 { "host", required_argument
, NULL
, 'H' },
149 { "machine", required_argument
, NULL
, 'M' },
150 { "discover", no_argument
, NULL
, ARG_DISCOVER
},
151 { "type", required_argument
, NULL
, 't' },
152 { "options", required_argument
, NULL
, 'o' },
153 { "owner", required_argument
, NULL
, ARG_OWNER
},
154 { "fsck", required_argument
, NULL
, ARG_FSCK
},
155 { "description", required_argument
, NULL
, ARG_DESCRIPTION
},
156 { "property", required_argument
, NULL
, 'p' },
157 { "automount", required_argument
, NULL
, ARG_AUTOMOUNT
},
158 { "timeout-idle-sec", required_argument
, NULL
, ARG_TIMEOUT_IDLE
},
159 { "automount-property", required_argument
, NULL
, ARG_AUTOMOUNT_PROPERTY
},
160 { "bind-device", no_argument
, NULL
, ARG_BIND_DEVICE
},
161 { "list", no_argument
, NULL
, ARG_LIST
},
162 { "umount", no_argument
, NULL
, 'u' },
163 { "unmount", no_argument
, NULL
, 'u' },
164 { "collect", no_argument
, NULL
, 'G' },
173 if (strstr(program_invocation_short_name
, "systemd-umount"))
174 arg_action
= ACTION_UMOUNT
;
176 while ((c
= getopt_long(argc
, argv
, "hqH:M:t:o:p:AuG", options
, NULL
)) >= 0)
191 arg_pager_flags
|= PAGER_DISABLE
;
194 case ARG_NO_ASK_PASSWORD
:
195 arg_ask_password
= false;
211 arg_transport
= BUS_TRANSPORT_REMOTE
;
216 arg_transport
= BUS_TRANSPORT_MACHINE
;
225 if (free_and_strdup(&arg_mount_type
, optarg
) < 0)
230 if (free_and_strdup(&arg_mount_options
, optarg
) < 0)
235 const char *user
= optarg
;
237 r
= get_user_creds(&user
, &arg_uid
, &arg_gid
, NULL
, NULL
, 0);
239 return log_error_errno(r
,
240 r
== -EBADMSG
? "UID or GID of user %s are invalid."
241 : "Cannot use \"%s\" as owner: %m",
247 r
= parse_boolean(optarg
);
249 return log_error_errno(r
, "Failed to parse --fsck= argument: %s", optarg
);
254 case ARG_DESCRIPTION
:
255 if (free_and_strdup(&arg_description
, optarg
) < 0)
260 if (strv_extend(&arg_property
, optarg
) < 0)
266 arg_action
= ACTION_AUTOMOUNT
;
270 r
= parse_boolean(optarg
);
272 return log_error_errno(r
, "--automount= expects a valid boolean parameter: %s", optarg
);
274 arg_action
= r
? ACTION_AUTOMOUNT
: ACTION_MOUNT
;
277 case ARG_TIMEOUT_IDLE
:
278 r
= parse_sec(optarg
, &arg_timeout_idle
);
280 return log_error_errno(r
, "Failed to parse timeout: %s", optarg
);
284 case ARG_AUTOMOUNT_PROPERTY
:
285 if (strv_extend(&arg_automount_property
, optarg
) < 0)
290 case ARG_BIND_DEVICE
:
291 arg_bind_device
= true;
295 arg_action
= ACTION_LIST
;
299 arg_action
= ACTION_UMOUNT
;
303 arg_aggressive_gc
= true;
310 assert_not_reached("Unhandled option");
313 if (arg_user
&& arg_transport
!= BUS_TRANSPORT_LOCAL
)
314 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
315 "Execution in user context is not supported on non-local systems.");
317 if (arg_action
== ACTION_LIST
) {
319 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
320 "Too many arguments.");
322 if (arg_transport
!= BUS_TRANSPORT_LOCAL
)
323 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP
),
324 "Listing devices only supported locally.");
325 } else if (arg_action
== ACTION_UMOUNT
) {
327 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
328 "At least one argument required.");
330 if (arg_transport
!= BUS_TRANSPORT_LOCAL
) {
333 for (i
= optind
; i
< argc
; i
++)
334 if (!path_is_absolute(argv
[i
]) )
335 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
336 "Only absolute path is supported: %s", argv
[i
]);
340 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
341 "At least one argument required.");
344 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
345 "At most two arguments required.");
347 if (arg_mount_type
&& (fstype_is_api_vfs(arg_mount_type
) || fstype_is_network(arg_mount_type
))) {
348 arg_mount_what
= strdup(argv
[optind
]);
352 } else if (arg_transport
== BUS_TRANSPORT_LOCAL
) {
353 _cleanup_free_
char *u
= NULL
;
355 u
= fstab_node_to_udev_node(argv
[optind
]);
359 r
= chase_symlinks(u
, NULL
, 0, &arg_mount_what
);
361 return log_error_errno(r
, "Failed to make path %s absolute: %m", u
);
363 arg_mount_what
= strdup(argv
[optind
]);
367 path_simplify(arg_mount_what
, false);
369 if (!path_is_absolute(arg_mount_what
))
370 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
371 "Only absolute path is supported: %s", arg_mount_what
);
374 if (argc
> optind
+1) {
375 if (arg_transport
== BUS_TRANSPORT_LOCAL
) {
376 r
= chase_symlinks(argv
[optind
+1], NULL
, CHASE_NONEXISTENT
, &arg_mount_where
);
378 return log_error_errno(r
, "Failed to make path %s absolute: %m", argv
[optind
+1]);
380 arg_mount_where
= strdup(argv
[optind
+1]);
381 if (!arg_mount_where
)
384 path_simplify(arg_mount_where
, false);
386 if (!path_is_absolute(arg_mount_where
))
387 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
388 "Only absolute path is supported: %s", arg_mount_where
);
393 if (arg_discover
&& arg_transport
!= BUS_TRANSPORT_LOCAL
)
394 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP
),
395 "Automatic mount location discovery is only supported locally.");
401 static int transient_unit_set_properties(sd_bus_message
*m
, UnitType t
, char **properties
) {
404 if (!isempty(arg_description
)) {
405 r
= sd_bus_message_append(m
, "(sv)", "Description", "s", arg_description
);
410 if (arg_bind_device
&& is_device_path(arg_mount_what
)) {
411 _cleanup_free_
char *device_unit
= NULL
;
413 r
= unit_name_from_path(arg_mount_what
, ".device", &device_unit
);
417 r
= sd_bus_message_append(m
, "(sv)(sv)",
418 "After", "as", 1, device_unit
,
419 "BindsTo", "as", 1, device_unit
);
424 if (arg_aggressive_gc
) {
425 r
= sd_bus_message_append(m
, "(sv)", "CollectMode", "s", "inactive-or-failed");
430 r
= bus_append_unit_property_assignment_many(m
, t
, properties
);
437 static int transient_mount_set_properties(sd_bus_message
*m
) {
438 _cleanup_free_
char *options
= NULL
;
443 r
= transient_unit_set_properties(m
, UNIT_MOUNT
, arg_property
);
447 if (arg_mount_what
) {
448 r
= sd_bus_message_append(m
, "(sv)", "What", "s", arg_mount_what
);
453 if (arg_mount_type
) {
454 r
= sd_bus_message_append(m
, "(sv)", "Type", "s", arg_mount_type
);
459 /* Prepend uid=…,gid=… if arg_uid is set */
460 if (arg_uid
!= UID_INVALID
) {
461 r
= asprintf(&options
,
462 "uid=" UID_FMT
",gid=" GID_FMT
"%s%s",
464 arg_mount_options
? "," : "", strempty(arg_mount_options
));
469 if (options
|| arg_mount_options
) {
470 log_debug("Using mount options: %s", options
?: arg_mount_options
);
472 r
= sd_bus_message_append(m
, "(sv)", "Options", "s", options
?: arg_mount_options
);
476 log_debug("Not using any mount options");
479 _cleanup_free_
char *fsck
= NULL
;
481 r
= unit_name_from_path_instance("systemd-fsck", arg_mount_what
, ".service", &fsck
);
485 r
= sd_bus_message_append(m
,
487 "Requires", "as", 1, fsck
,
488 "After", "as", 1, fsck
);
496 static int transient_automount_set_properties(sd_bus_message
*m
) {
501 r
= transient_unit_set_properties(m
, UNIT_AUTOMOUNT
, arg_automount_property
);
505 if (arg_timeout_idle
!= USEC_INFINITY
) {
506 r
= sd_bus_message_append(m
, "(sv)", "TimeoutIdleUSec", "t", arg_timeout_idle
);
514 static int start_transient_mount(
518 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
, *reply
= NULL
;
519 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
520 _cleanup_(bus_wait_for_jobs_freep
) BusWaitForJobs
*w
= NULL
;
521 _cleanup_free_
char *mount_unit
= NULL
;
525 r
= bus_wait_for_jobs_new(bus
, &w
);
527 return log_error_errno(r
, "Could not watch jobs: %m");
530 r
= unit_name_from_path(arg_mount_where
, ".mount", &mount_unit
);
532 return log_error_errno(r
, "Failed to make mount unit name: %m");
534 r
= sd_bus_message_new_method_call(
537 "org.freedesktop.systemd1",
538 "/org/freedesktop/systemd1",
539 "org.freedesktop.systemd1.Manager",
540 "StartTransientUnit");
542 return bus_log_create_error(r
);
544 r
= sd_bus_message_set_allow_interactive_authorization(m
, arg_ask_password
);
546 return bus_log_create_error(r
);
549 r
= sd_bus_message_append(m
, "ss", mount_unit
, "fail");
551 return bus_log_create_error(r
);
554 r
= sd_bus_message_open_container(m
, 'a', "(sv)");
556 return bus_log_create_error(r
);
558 r
= transient_mount_set_properties(m
);
560 return bus_log_create_error(r
);
562 r
= sd_bus_message_close_container(m
);
564 return bus_log_create_error(r
);
566 /* Auxiliary units */
567 r
= sd_bus_message_append(m
, "a(sa(sv))", 0);
569 return bus_log_create_error(r
);
571 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
573 r
= sd_bus_call(bus
, m
, 0, &error
, &reply
);
575 return log_error_errno(r
, "Failed to start transient mount unit: %s", bus_error_message(&error
, r
));
580 r
= sd_bus_message_read(reply
, "o", &object
);
582 return bus_log_parse_error(r
);
584 r
= bus_wait_for_jobs_one(w
, object
, arg_quiet
);
590 log_info("Started unit %s%s%s for mount point: %s%s%s",
591 ansi_highlight(), mount_unit
, ansi_normal(),
592 ansi_highlight(), arg_mount_where
, ansi_normal());
597 static int start_transient_automount(
601 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
, *reply
= NULL
;
602 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
603 _cleanup_(bus_wait_for_jobs_freep
) BusWaitForJobs
*w
= NULL
;
604 _cleanup_free_
char *automount_unit
= NULL
, *mount_unit
= NULL
;
608 r
= bus_wait_for_jobs_new(bus
, &w
);
610 return log_error_errno(r
, "Could not watch jobs: %m");
613 r
= unit_name_from_path(arg_mount_where
, ".automount", &automount_unit
);
615 return log_error_errno(r
, "Failed to make automount unit name: %m");
617 r
= unit_name_from_path(arg_mount_where
, ".mount", &mount_unit
);
619 return log_error_errno(r
, "Failed to make mount unit name: %m");
621 r
= sd_bus_message_new_method_call(
624 "org.freedesktop.systemd1",
625 "/org/freedesktop/systemd1",
626 "org.freedesktop.systemd1.Manager",
627 "StartTransientUnit");
629 return bus_log_create_error(r
);
631 r
= sd_bus_message_set_allow_interactive_authorization(m
, arg_ask_password
);
633 return bus_log_create_error(r
);
636 r
= sd_bus_message_append(m
, "ss", automount_unit
, "fail");
638 return bus_log_create_error(r
);
641 r
= sd_bus_message_open_container(m
, 'a', "(sv)");
643 return bus_log_create_error(r
);
645 r
= transient_automount_set_properties(m
);
647 return bus_log_create_error(r
);
649 r
= sd_bus_message_close_container(m
);
651 return bus_log_create_error(r
);
653 /* Auxiliary units */
654 r
= sd_bus_message_open_container(m
, 'a', "(sa(sv))");
656 return bus_log_create_error(r
);
658 r
= sd_bus_message_open_container(m
, 'r', "sa(sv)");
660 return bus_log_create_error(r
);
662 r
= sd_bus_message_append(m
, "s", mount_unit
);
664 return bus_log_create_error(r
);
666 r
= sd_bus_message_open_container(m
, 'a', "(sv)");
668 return bus_log_create_error(r
);
670 r
= transient_mount_set_properties(m
);
672 return bus_log_create_error(r
);
674 r
= sd_bus_message_close_container(m
);
676 return bus_log_create_error(r
);
678 r
= sd_bus_message_close_container(m
);
680 return bus_log_create_error(r
);
682 r
= sd_bus_message_close_container(m
);
684 return bus_log_create_error(r
);
686 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
688 r
= sd_bus_call(bus
, m
, 0, &error
, &reply
);
690 return log_error_errno(r
, "Failed to start transient automount unit: %s", bus_error_message(&error
, r
));
695 r
= sd_bus_message_read(reply
, "o", &object
);
697 return bus_log_parse_error(r
);
699 r
= bus_wait_for_jobs_one(w
, object
, arg_quiet
);
705 log_info("Started unit %s%s%s for mount point: %s%s%s",
706 ansi_highlight(), automount_unit
, ansi_normal(),
707 ansi_highlight(), arg_mount_where
, ansi_normal());
712 static int find_mount_points(const char *what
, char ***list
) {
713 _cleanup_fclose_
FILE *proc_self_mountinfo
= NULL
;
714 _cleanup_strv_free_
char **l
= NULL
;
715 size_t bufsize
= 0, n
= 0;
720 /* Returns all mount points obtained from /proc/self/mountinfo in *list,
721 * and the number of mount points as return value. */
723 proc_self_mountinfo
= fopen("/proc/self/mountinfo", "re");
724 if (!proc_self_mountinfo
)
725 return log_error_errno(errno
, "Can't open /proc/self/mountinfo: %m");
728 _cleanup_free_
char *path
= NULL
, *where
= NULL
, *dev
= NULL
;
731 r
= fscanf(proc_self_mountinfo
,
732 "%*s " /* (1) mount id */
733 "%*s " /* (2) parent id */
734 "%*s " /* (3) major:minor */
735 "%*s " /* (4) root */
736 "%ms " /* (5) mount point */
737 "%*s" /* (6) mount options */
738 "%*[^-]" /* (7) optional fields */
739 "- " /* (8) separator */
740 "%*s " /* (9) file system type */
741 "%ms" /* (10) mount source */
742 "%*s" /* (11) mount options 2 */
743 "%*[^\n]", /* some rubbish at the end */
752 if (!streq(what
, dev
))
755 r
= cunescape(path
, UNESCAPE_RELAX
, &where
);
759 /* one extra slot is needed for the terminating NULL */
760 if (!GREEDY_REALLOC(l
, bufsize
, n
+ 2))
763 l
[n
++] = TAKE_PTR(where
);
766 if (!GREEDY_REALLOC(l
, bufsize
, n
+ 1))
775 static int find_loop_device(const char *backing_file
, char **loop_dev
) {
776 _cleanup_closedir_
DIR *d
= NULL
;
778 _cleanup_free_
char *l
= NULL
;
780 assert(backing_file
);
783 d
= opendir("/sys/devices/virtual/block");
787 FOREACH_DIRENT(de
, d
, return -errno
) {
788 _cleanup_free_
char *sys
= NULL
, *fname
= NULL
;
791 dirent_ensure_type(d
, de
);
793 if (de
->d_type
!= DT_DIR
)
796 if (!startswith(de
->d_name
, "loop"))
799 sys
= strjoin("/sys/devices/virtual/block/", de
->d_name
, "/loop/backing_file");
803 r
= read_one_line_file(sys
, &fname
);
805 log_debug_errno(r
, "Failed to read %s, ignoring: %m", sys
);
809 if (files_same(fname
, backing_file
, 0) <= 0)
812 l
= strjoin("/dev/", de
->d_name
);
822 *loop_dev
= TAKE_PTR(l
);
827 static int stop_mount(
830 const char *suffix
) {
832 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
, *reply
= NULL
;
833 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
834 _cleanup_(bus_wait_for_jobs_freep
) BusWaitForJobs
*w
= NULL
;
835 _cleanup_free_
char *mount_unit
= NULL
;
839 r
= bus_wait_for_jobs_new(bus
, &w
);
841 return log_error_errno(r
, "Could not watch jobs: %m");
844 r
= unit_name_from_path(where
, suffix
, &mount_unit
);
846 return log_error_errno(r
, "Failed to make %s unit name from path %s: %m", suffix
+ 1, where
);
848 r
= sd_bus_message_new_method_call(
851 "org.freedesktop.systemd1",
852 "/org/freedesktop/systemd1",
853 "org.freedesktop.systemd1.Manager",
856 return bus_log_create_error(r
);
858 r
= sd_bus_message_set_allow_interactive_authorization(m
, arg_ask_password
);
860 return bus_log_create_error(r
);
863 r
= sd_bus_message_append(m
, "ss", mount_unit
, "fail");
865 return bus_log_create_error(r
);
867 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
869 r
= sd_bus_call(bus
, m
, 0, &error
, &reply
);
871 if (streq(suffix
, ".automount") &&
872 sd_bus_error_has_name(&error
, "org.freedesktop.systemd1.NoSuchUnit"))
874 return log_error_errno(r
, "Failed to stop %s unit: %s", suffix
+ 1, bus_error_message(&error
, r
));
880 r
= sd_bus_message_read(reply
, "o", &object
);
882 return bus_log_parse_error(r
);
884 r
= bus_wait_for_jobs_one(w
, object
, arg_quiet
);
890 log_info("Stopped unit %s%s%s for mount point: %s%s%s",
891 ansi_highlight(), mount_unit
, ansi_normal(),
892 ansi_highlight(), where
, ansi_normal());
897 static int stop_mounts(
903 if (path_equal(where
, "/"))
904 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
905 "Refusing to operate on root directory: %s", where
);
907 if (!path_is_normalized(where
))
908 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
909 "Path contains non-normalized components: %s", where
);
911 r
= stop_mount(bus
, where
, ".mount");
915 r
= stop_mount(bus
, where
, ".automount");
922 static int umount_by_device(sd_bus
*bus
, const char *what
) {
923 _cleanup_(sd_device_unrefp
) sd_device
*d
= NULL
;
924 _cleanup_strv_free_
char **list
= NULL
;
932 if (stat(what
, &st
) < 0)
933 return log_error_errno(errno
, "Can't stat %s: %m", what
);
935 if (!S_ISBLK(st
.st_mode
)) {
936 log_error("Not a block device: %s", what
);
940 r
= sd_device_new_from_devnum(&d
, 'b', st
.st_rdev
);
942 return log_error_errno(r
, "Failed to get device from device number: %m");
944 r
= sd_device_get_property_value(d
, "ID_FS_USAGE", &v
);
946 return log_device_error_errno(d
, r
, "Failed to get device property: %m");
948 if (!streq(v
, "filesystem")) {
949 log_device_error(d
, "%s does not contain a known file system.", what
);
953 if (sd_device_get_property_value(d
, "SYSTEMD_MOUNT_WHERE", &v
) >= 0)
954 r2
= stop_mounts(bus
, v
);
956 r
= find_mount_points(what
, &list
);
960 for (l
= list
; *l
; l
++) {
961 r
= stop_mounts(bus
, *l
);
969 static int umount_loop(sd_bus
*bus
, const char *backing_file
) {
970 _cleanup_free_
char *loop_dev
= NULL
;
973 assert(backing_file
);
975 r
= find_loop_device(backing_file
, &loop_dev
);
977 return log_error_errno(r
, r
== -ENXIO
? "File %s is not mounted." : "Can't get loop device for %s: %m", backing_file
);
979 return umount_by_device(bus
, loop_dev
);
982 static int action_umount(
989 if (arg_transport
!= BUS_TRANSPORT_LOCAL
) {
990 for (i
= optind
; i
< argc
; i
++) {
991 _cleanup_free_
char *p
= NULL
;
997 path_simplify(p
, false);
999 r
= stop_mounts(bus
, p
);
1006 for (i
= optind
; i
< argc
; i
++) {
1007 _cleanup_free_
char *u
= NULL
, *p
= NULL
;
1010 u
= fstab_node_to_udev_node(argv
[i
]);
1014 r
= chase_symlinks(u
, NULL
, 0, &p
);
1016 r2
= log_error_errno(r
, "Failed to make path %s absolute: %m", argv
[i
]);
1020 if (stat(p
, &st
) < 0)
1021 return log_error_errno(errno
, "Can't stat %s (from %s): %m", p
, argv
[i
]);
1023 if (S_ISBLK(st
.st_mode
))
1024 r
= umount_by_device(bus
, p
);
1025 else if (S_ISREG(st
.st_mode
))
1026 r
= umount_loop(bus
, p
);
1027 else if (S_ISDIR(st
.st_mode
))
1028 r
= stop_mounts(bus
, p
);
1030 log_error("Invalid file type: %s (from %s)", p
, argv
[i
]);
1041 static int acquire_mount_type(sd_device
*d
) {
1049 if (sd_device_get_property_value(d
, "ID_FS_TYPE", &v
) < 0)
1052 arg_mount_type
= strdup(v
);
1053 if (!arg_mount_type
)
1056 log_debug("Discovered type=%s", arg_mount_type
);
1060 static int acquire_mount_options(sd_device
*d
) {
1065 if (arg_mount_options
)
1068 if (sd_device_get_property_value(d
, "SYSTEMD_MOUNT_OPTIONS", &v
) < 0)
1071 arg_mount_options
= strdup(v
);
1072 if (!arg_mount_options
)
1075 log_debug("Discovered options=%s", arg_mount_options
);
1079 static const char *get_model(sd_device
*d
) {
1084 if (sd_device_get_property_value(d
, "ID_MODEL_FROM_DATABASE", &model
) >= 0)
1087 if (sd_device_get_property_value(d
, "ID_MODEL", &model
) >= 0)
1093 static const char* get_label(sd_device
*d
) {
1098 if (sd_device_get_property_value(d
, "ID_FS_LABEL", &label
) >= 0)
1101 if (sd_device_get_property_value(d
, "ID_PART_ENTRY_NAME", &label
) >= 0)
1107 static int acquire_mount_where(sd_device
*d
) {
1110 if (arg_mount_where
)
1113 if (sd_device_get_property_value(d
, "SYSTEMD_MOUNT_WHERE", &v
) < 0) {
1114 _cleanup_free_
char *escaped
= NULL
;
1117 name
= get_label(d
);
1119 name
= get_model(d
);
1123 if (sd_device_get_devname(d
, &dn
) < 0)
1126 name
= basename(dn
);
1129 escaped
= xescape(name
, "\\");
1132 if (!filename_is_valid(escaped
))
1135 arg_mount_where
= strjoin("/run/media/system/", escaped
);
1137 arg_mount_where
= strdup(v
);
1139 if (!arg_mount_where
)
1142 log_debug("Discovered where=%s", arg_mount_where
);
1146 static int acquire_mount_where_for_loop_dev(const char *loop_dev
) {
1147 _cleanup_strv_free_
char **list
= NULL
;
1150 if (arg_mount_where
)
1153 r
= find_mount_points(loop_dev
, &list
);
1157 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1158 "Can't find mount point of %s. It is expected that %s is already mounted on a place.",
1159 loop_dev
, loop_dev
);
1161 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1162 "%s is mounted on %d places. It is expected that %s is mounted on a place.",
1163 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_device_error(d
, "%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_pager_flags
);
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 static int run(int argc
, char* argv
[]) {
1522 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1525 log_parse_environment();
1528 r
= parse_argv(argc
, argv
);
1532 if (arg_action
== ACTION_LIST
)
1533 return list_devices();
1535 r
= bus_connect_transport_systemd(arg_transport
, arg_host
, arg_user
, &bus
);
1537 return log_error_errno(r
, "Failed to create bus connection: %m");
1539 if (arg_action
== ACTION_UMOUNT
)
1540 return action_umount(bus
, argc
, argv
);
1542 if (!path_is_normalized(arg_mount_what
)) {
1543 log_error("Path contains non-normalized components: %s", arg_mount_what
);
1548 r
= discover_device();
1553 if (!arg_mount_where
) {
1554 log_error("Can't figure out where to mount %s.", arg_mount_what
);
1558 if (path_equal(arg_mount_where
, "/")) {
1559 log_error("Refusing to operate on root directory.");
1563 if (!path_is_normalized(arg_mount_where
)) {
1564 log_error("Path contains non-normalized components: %s", arg_mount_where
);
1568 if (streq_ptr(arg_mount_type
, "auto"))
1569 arg_mount_type
= mfree(arg_mount_type
);
1570 if (streq_ptr(arg_mount_options
, "defaults"))
1571 arg_mount_options
= mfree(arg_mount_options
);
1573 if (!is_device_path(arg_mount_what
))
1576 if (arg_fsck
&& arg_mount_type
&& arg_transport
== BUS_TRANSPORT_LOCAL
) {
1577 r
= fsck_exists(arg_mount_type
);
1579 log_warning_errno(r
, "Couldn't determine whether fsck for %s exists, proceeding anyway.", arg_mount_type
);
1581 log_debug("Disabling file system check as fsck for %s doesn't exist.", arg_mount_type
);
1582 arg_fsck
= false; /* fsck doesn't exist, let's not attempt it */
1586 /* The kernel (properly) refuses mounting file systems with unknown uid=,gid= options,
1587 * but not for all filesystem types. Let's try to catch the cases where the option
1588 * would be used if the file system does not support it. It is also possible to
1589 * autodetect the file system, but that's only possible with disk-based file systems
1590 * which incidentally seem to be implemented more carefully and reject unknown options,
1591 * so it's probably OK that we do the check only when the type is specified.
1593 if (arg_mount_type
&&
1594 !streq(arg_mount_type
, "auto") &&
1595 arg_uid
!= UID_INVALID
&&
1596 !fstype_can_uid_gid(arg_mount_type
)) {
1597 log_error("File system type %s is not known to support uid=/gid=, refusing.",
1602 switch (arg_action
) {
1605 case ACTION_DEFAULT
:
1606 r
= start_transient_mount(bus
, argv
+ optind
);
1609 case ACTION_AUTOMOUNT
:
1610 r
= start_transient_automount(bus
, argv
+ optind
);
1614 assert_not_reached("Unexpected action.");
1620 DEFINE_MAIN_FUNCTION(run
);