]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
450442cf LP |
2 | |
3 | #include <getopt.h> | |
4 | ||
450442cf | 5 | #include "sd-bus.h" |
dcd26523 | 6 | #include "sd-device.h" |
450442cf LP |
7 | |
8 | #include "bus-error.h" | |
9b71e4ab | 9 | #include "bus-locator.h" |
450442cf | 10 | #include "bus-unit-util.h" |
e45c81b8 | 11 | #include "bus-wait-for-jobs.h" |
8437c059 | 12 | #include "device-util.h" |
6f6165bf | 13 | #include "dirent-util.h" |
450442cf | 14 | #include "escape.h" |
9017f5d8 | 15 | #include "fd-util.h" |
6f6165bf | 16 | #include "fileio.h" |
6ae6ea55 | 17 | #include "format-table.h" |
ca78ad1d | 18 | #include "format-util.h" |
f4938c2e | 19 | #include "fs-util.h" |
450442cf | 20 | #include "fstab-util.h" |
7d991d48 | 21 | #include "libmount-util.h" |
f2fae6fb | 22 | #include "main-func.h" |
e2be442e | 23 | #include "mount-util.h" |
049af8ad | 24 | #include "mountpoint-util.h" |
450442cf LP |
25 | #include "pager.h" |
26 | #include "parse-util.h" | |
27 | #include "path-util.h" | |
294bf0c3 | 28 | #include "pretty-print.h" |
760877e9 | 29 | #include "sort-util.h" |
450442cf | 30 | #include "spawn-polkit-agent.h" |
6f6165bf | 31 | #include "stat-util.h" |
450442cf | 32 | #include "strv.h" |
760877e9 | 33 | #include "terminal-util.h" |
89ada3ba | 34 | #include "unit-def.h" |
450442cf | 35 | #include "unit-name.h" |
e09fc884 | 36 | #include "user-util.h" |
450442cf LP |
37 | |
38 | enum { | |
39 | ACTION_DEFAULT, | |
40 | ACTION_MOUNT, | |
41 | ACTION_AUTOMOUNT, | |
c37fb55b | 42 | ACTION_UMOUNT, |
450442cf LP |
43 | ACTION_LIST, |
44 | } arg_action = ACTION_DEFAULT; | |
45 | ||
46 | static bool arg_no_block = false; | |
0221d68a | 47 | static PagerFlags arg_pager_flags = 0; |
a5279634 | 48 | static bool arg_legend = true; |
a362c069 | 49 | static bool arg_full = false; |
450442cf LP |
50 | static bool arg_ask_password = true; |
51 | static bool arg_quiet = false; | |
52 | static BusTransport arg_transport = BUS_TRANSPORT_LOCAL; | |
53 | static bool arg_user = false; | |
54 | static const char *arg_host = NULL; | |
55 | static bool arg_discover = false; | |
56 | static char *arg_mount_what = NULL; | |
57 | static char *arg_mount_where = NULL; | |
58 | static char *arg_mount_type = NULL; | |
59 | static char *arg_mount_options = NULL; | |
60 | static char *arg_description = NULL; | |
61 | static char **arg_property = NULL; | |
62 | static usec_t arg_timeout_idle = USEC_INFINITY; | |
63 | static bool arg_timeout_idle_set = false; | |
64 | static char **arg_automount_property = NULL; | |
65 | static int arg_bind_device = -1; | |
e09fc884 ZJS |
66 | static uid_t arg_uid = UID_INVALID; |
67 | static gid_t arg_gid = GID_INVALID; | |
450442cf | 68 | static bool arg_fsck = true; |
dc336483 | 69 | static bool arg_aggressive_gc = false; |
450442cf | 70 | |
f2fae6fb YW |
71 | STATIC_DESTRUCTOR_REGISTER(arg_mount_what, freep); |
72 | STATIC_DESTRUCTOR_REGISTER(arg_mount_where, freep); | |
73 | STATIC_DESTRUCTOR_REGISTER(arg_mount_type, freep); | |
74 | STATIC_DESTRUCTOR_REGISTER(arg_mount_options, freep); | |
75 | STATIC_DESTRUCTOR_REGISTER(arg_description, freep); | |
76 | STATIC_DESTRUCTOR_REGISTER(arg_property, strv_freep); | |
77 | STATIC_DESTRUCTOR_REGISTER(arg_automount_property, strv_freep); | |
78 | ||
37ec0fdd LP |
79 | static int help(void) { |
80 | _cleanup_free_ char *link = NULL; | |
81 | int r; | |
82 | ||
83 | r = terminal_urlify_man("systemd-mount", "1", &link); | |
84 | if (r < 0) | |
85 | return log_oom(); | |
86 | ||
9017f5d8 YW |
87 | printf("systemd-mount [OPTIONS...] WHAT [WHERE]\n" |
88 | "systemd-mount [OPTIONS...] --list\n" | |
89 | "%s [OPTIONS...] %sWHAT|WHERE...\n\n" | |
450442cf LP |
90 | "Establish a mount or auto-mount point transiently.\n\n" |
91 | " -h --help Show this help\n" | |
92 | " --version Show package version\n" | |
93 | " --no-block Do not wait until operation finished\n" | |
94 | " --no-pager Do not pipe output into a pager\n" | |
a5279634 | 95 | " --no-legend Do not show the headers\n" |
a362c069 | 96 | " -l --full Do not ellipsize output\n" |
450442cf LP |
97 | " --no-ask-password Do not prompt for password\n" |
98 | " -q --quiet Suppress information messages during runtime\n" | |
99 | " --user Run as user unit\n" | |
100 | " -H --host=[USER@]HOST Operate on remote host\n" | |
101 | " -M --machine=CONTAINER Operate on local container\n" | |
102 | " --discover Discover mount device metadata\n" | |
103 | " -t --type=TYPE File system type\n" | |
104 | " -o --options=OPTIONS Mount options\n" | |
e09fc884 | 105 | " --owner=USER Add uid= and gid= options for USER\n" |
450442cf LP |
106 | " --fsck=no Don't run file system check before mount\n" |
107 | " --description=TEXT Description for unit\n" | |
108 | " -p --property=NAME=VALUE Set mount unit property\n" | |
109 | " -A --automount=BOOL Create an auto-mount point\n" | |
110 | " --timeout-idle-sec=SEC Specify automount idle timeout\n" | |
111 | " --automount-property=NAME=VALUE\n" | |
112 | " Set automount unit property\n" | |
113 | " --bind-device Bind automount unit to device\n" | |
114 | " --list List mountable block devices\n" | |
dc336483 | 115 | " -u --umount Unmount mount points\n" |
37ec0fdd LP |
116 | " -G --collect Unload unit after it stopped, even when failed\n" |
117 | "\nSee the %s for details.\n" | |
118 | , program_invocation_short_name | |
119 | , streq(program_invocation_short_name, "systemd-umount") ? "" : "--umount " | |
120 | , link | |
121 | ); | |
122 | ||
123 | return 0; | |
450442cf LP |
124 | } |
125 | ||
126 | static int parse_argv(int argc, char *argv[]) { | |
127 | ||
128 | enum { | |
129 | ARG_VERSION = 0x100, | |
130 | ARG_NO_BLOCK, | |
131 | ARG_NO_PAGER, | |
a5279634 | 132 | ARG_NO_LEGEND, |
450442cf LP |
133 | ARG_NO_ASK_PASSWORD, |
134 | ARG_USER, | |
135 | ARG_SYSTEM, | |
136 | ARG_DISCOVER, | |
137 | ARG_MOUNT_TYPE, | |
138 | ARG_MOUNT_OPTIONS, | |
e09fc884 | 139 | ARG_OWNER, |
450442cf LP |
140 | ARG_FSCK, |
141 | ARG_DESCRIPTION, | |
142 | ARG_TIMEOUT_IDLE, | |
143 | ARG_AUTOMOUNT, | |
144 | ARG_AUTOMOUNT_PROPERTY, | |
145 | ARG_BIND_DEVICE, | |
146 | ARG_LIST, | |
147 | }; | |
148 | ||
149 | static const struct option options[] = { | |
150 | { "help", no_argument, NULL, 'h' }, | |
151 | { "version", no_argument, NULL, ARG_VERSION }, | |
152 | { "no-block", no_argument, NULL, ARG_NO_BLOCK }, | |
153 | { "no-pager", no_argument, NULL, ARG_NO_PAGER }, | |
a5279634 | 154 | { "no-legend", no_argument, NULL, ARG_NO_LEGEND }, |
a362c069 | 155 | { "full", no_argument, NULL, 'l' }, |
450442cf LP |
156 | { "no-ask-password", no_argument, NULL, ARG_NO_ASK_PASSWORD }, |
157 | { "quiet", no_argument, NULL, 'q' }, | |
158 | { "user", no_argument, NULL, ARG_USER }, | |
159 | { "system", no_argument, NULL, ARG_SYSTEM }, | |
160 | { "host", required_argument, NULL, 'H' }, | |
161 | { "machine", required_argument, NULL, 'M' }, | |
162 | { "discover", no_argument, NULL, ARG_DISCOVER }, | |
163 | { "type", required_argument, NULL, 't' }, | |
164 | { "options", required_argument, NULL, 'o' }, | |
e09fc884 | 165 | { "owner", required_argument, NULL, ARG_OWNER }, |
0f923832 | 166 | { "fsck", required_argument, NULL, ARG_FSCK }, |
450442cf LP |
167 | { "description", required_argument, NULL, ARG_DESCRIPTION }, |
168 | { "property", required_argument, NULL, 'p' }, | |
169 | { "automount", required_argument, NULL, ARG_AUTOMOUNT }, | |
170 | { "timeout-idle-sec", required_argument, NULL, ARG_TIMEOUT_IDLE }, | |
171 | { "automount-property", required_argument, NULL, ARG_AUTOMOUNT_PROPERTY }, | |
172 | { "bind-device", no_argument, NULL, ARG_BIND_DEVICE }, | |
173 | { "list", no_argument, NULL, ARG_LIST }, | |
c37fb55b LR |
174 | { "umount", no_argument, NULL, 'u' }, |
175 | { "unmount", no_argument, NULL, 'u' }, | |
dc336483 | 176 | { "collect", no_argument, NULL, 'G' }, |
450442cf LP |
177 | {}, |
178 | }; | |
179 | ||
180 | int r, c; | |
181 | ||
182 | assert(argc >= 0); | |
183 | assert(argv); | |
184 | ||
c37fb55b LR |
185 | if (strstr(program_invocation_short_name, "systemd-umount")) |
186 | arg_action = ACTION_UMOUNT; | |
187 | ||
a362c069 | 188 | while ((c = getopt_long(argc, argv, "hqH:M:t:o:p:AuGl", options, NULL)) >= 0) |
450442cf LP |
189 | |
190 | switch (c) { | |
191 | ||
192 | case 'h': | |
37ec0fdd | 193 | return help(); |
450442cf LP |
194 | |
195 | case ARG_VERSION: | |
196 | return version(); | |
197 | ||
198 | case ARG_NO_BLOCK: | |
199 | arg_no_block = true; | |
200 | break; | |
201 | ||
202 | case ARG_NO_PAGER: | |
0221d68a | 203 | arg_pager_flags |= PAGER_DISABLE; |
450442cf LP |
204 | break; |
205 | ||
a5279634 YW |
206 | case ARG_NO_LEGEND: |
207 | arg_legend = false; | |
208 | break; | |
209 | ||
a362c069 YW |
210 | case 'l': |
211 | arg_full = true; | |
212 | break; | |
213 | ||
450442cf LP |
214 | case ARG_NO_ASK_PASSWORD: |
215 | arg_ask_password = false; | |
216 | break; | |
217 | ||
218 | case 'q': | |
219 | arg_quiet = true; | |
220 | break; | |
221 | ||
222 | case ARG_USER: | |
223 | arg_user = true; | |
224 | break; | |
225 | ||
226 | case ARG_SYSTEM: | |
227 | arg_user = false; | |
228 | break; | |
229 | ||
230 | case 'H': | |
231 | arg_transport = BUS_TRANSPORT_REMOTE; | |
232 | arg_host = optarg; | |
233 | break; | |
234 | ||
235 | case 'M': | |
236 | arg_transport = BUS_TRANSPORT_MACHINE; | |
237 | arg_host = optarg; | |
238 | break; | |
239 | ||
240 | case ARG_DISCOVER: | |
241 | arg_discover = true; | |
242 | break; | |
243 | ||
244 | case 't': | |
245 | if (free_and_strdup(&arg_mount_type, optarg) < 0) | |
246 | return log_oom(); | |
247 | break; | |
248 | ||
249 | case 'o': | |
250 | if (free_and_strdup(&arg_mount_options, optarg) < 0) | |
251 | return log_oom(); | |
252 | break; | |
253 | ||
e09fc884 ZJS |
254 | case ARG_OWNER: { |
255 | const char *user = optarg; | |
256 | ||
fafff8f1 | 257 | r = get_user_creds(&user, &arg_uid, &arg_gid, NULL, NULL, 0); |
e09fc884 ZJS |
258 | if (r < 0) |
259 | return log_error_errno(r, | |
260 | r == -EBADMSG ? "UID or GID of user %s are invalid." | |
261 | : "Cannot use \"%s\" as owner: %m", | |
262 | optarg); | |
263 | break; | |
264 | } | |
265 | ||
450442cf LP |
266 | case ARG_FSCK: |
267 | r = parse_boolean(optarg); | |
268 | if (r < 0) | |
269 | return log_error_errno(r, "Failed to parse --fsck= argument: %s", optarg); | |
270 | ||
271 | arg_fsck = r; | |
272 | break; | |
273 | ||
274 | case ARG_DESCRIPTION: | |
275 | if (free_and_strdup(&arg_description, optarg) < 0) | |
276 | return log_oom(); | |
277 | break; | |
278 | ||
279 | case 'p': | |
280 | if (strv_extend(&arg_property, optarg) < 0) | |
281 | return log_oom(); | |
282 | ||
283 | break; | |
284 | ||
285 | case 'A': | |
286 | arg_action = ACTION_AUTOMOUNT; | |
287 | break; | |
288 | ||
289 | case ARG_AUTOMOUNT: | |
290 | r = parse_boolean(optarg); | |
291 | if (r < 0) | |
292 | return log_error_errno(r, "--automount= expects a valid boolean parameter: %s", optarg); | |
293 | ||
294 | arg_action = r ? ACTION_AUTOMOUNT : ACTION_MOUNT; | |
295 | break; | |
296 | ||
297 | case ARG_TIMEOUT_IDLE: | |
298 | r = parse_sec(optarg, &arg_timeout_idle); | |
299 | if (r < 0) | |
300 | return log_error_errno(r, "Failed to parse timeout: %s", optarg); | |
301 | ||
302 | break; | |
303 | ||
304 | case ARG_AUTOMOUNT_PROPERTY: | |
305 | if (strv_extend(&arg_automount_property, optarg) < 0) | |
306 | return log_oom(); | |
307 | ||
308 | break; | |
309 | ||
310 | case ARG_BIND_DEVICE: | |
311 | arg_bind_device = true; | |
312 | break; | |
313 | ||
314 | case ARG_LIST: | |
315 | arg_action = ACTION_LIST; | |
316 | break; | |
317 | ||
c37fb55b LR |
318 | case 'u': |
319 | arg_action = ACTION_UMOUNT; | |
320 | break; | |
321 | ||
dc336483 YW |
322 | case 'G': |
323 | arg_aggressive_gc = true; | |
324 | break; | |
325 | ||
450442cf LP |
326 | case '?': |
327 | return -EINVAL; | |
328 | ||
329 | default: | |
330 | assert_not_reached("Unhandled option"); | |
331 | } | |
332 | ||
baaa35ad ZJS |
333 | if (arg_user && arg_transport != BUS_TRANSPORT_LOCAL) |
334 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
335 | "Execution in user context is not supported on non-local systems."); | |
450442cf LP |
336 | |
337 | if (arg_action == ACTION_LIST) { | |
baaa35ad ZJS |
338 | if (optind < argc) |
339 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
340 | "Too many arguments."); | |
450442cf | 341 | |
baaa35ad ZJS |
342 | if (arg_transport != BUS_TRANSPORT_LOCAL) |
343 | return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), | |
344 | "Listing devices only supported locally."); | |
9017f5d8 | 345 | } else if (arg_action == ACTION_UMOUNT) { |
baaa35ad ZJS |
346 | if (optind >= argc) |
347 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
348 | "At least one argument required."); | |
9017f5d8 YW |
349 | |
350 | if (arg_transport != BUS_TRANSPORT_LOCAL) { | |
3747daa2 YW |
351 | int i; |
352 | ||
353 | for (i = optind; i < argc; i++) | |
baaa35ad ZJS |
354 | if (!path_is_absolute(argv[i]) ) |
355 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
356 | "Only absolute path is supported: %s", argv[i]); | |
9017f5d8 | 357 | } |
450442cf | 358 | } else { |
baaa35ad ZJS |
359 | if (optind >= argc) |
360 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
361 | "At least one argument required."); | |
450442cf | 362 | |
baaa35ad ZJS |
363 | if (argc > optind+2) |
364 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
365 | "At most two arguments required."); | |
450442cf | 366 | |
c15ab81e | 367 | if (arg_mount_type && !fstype_is_blockdev_backed(arg_mount_type)) { |
e2be442e YW |
368 | arg_mount_what = strdup(argv[optind]); |
369 | if (!arg_mount_what) | |
370 | return log_oom(); | |
371 | ||
372 | } else if (arg_transport == BUS_TRANSPORT_LOCAL) { | |
5bc9f949 | 373 | _cleanup_free_ char *u = NULL; |
450442cf | 374 | |
afde5b16 YW |
375 | u = fstab_node_to_udev_node(argv[optind]); |
376 | if (!u) | |
377 | return log_oom(); | |
378 | ||
a5648b80 | 379 | r = chase_symlinks(u, NULL, 0, &arg_mount_what, NULL); |
450442cf | 380 | if (r < 0) |
de345d34 | 381 | return log_error_errno(r, "Failed to make path %s absolute: %m", u); |
afde5b16 | 382 | } else { |
4185da7c | 383 | arg_mount_what = strdup(argv[optind]); |
afde5b16 YW |
384 | if (!arg_mount_what) |
385 | return log_oom(); | |
386 | ||
858d36c1 | 387 | path_simplify(arg_mount_what, false); |
afde5b16 | 388 | |
baaa35ad ZJS |
389 | if (!path_is_absolute(arg_mount_what)) |
390 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
391 | "Only absolute path is supported: %s", arg_mount_what); | |
afde5b16 YW |
392 | } |
393 | ||
394 | if (argc > optind+1) { | |
395 | if (arg_transport == BUS_TRANSPORT_LOCAL) { | |
a5648b80 | 396 | r = chase_symlinks(argv[optind+1], NULL, CHASE_NONEXISTENT, &arg_mount_where, NULL); |
afde5b16 | 397 | if (r < 0) |
de345d34 | 398 | return log_error_errno(r, "Failed to make path %s absolute: %m", argv[optind+1]); |
afde5b16 YW |
399 | } else { |
400 | arg_mount_where = strdup(argv[optind+1]); | |
401 | if (!arg_mount_where) | |
402 | return log_oom(); | |
403 | ||
858d36c1 | 404 | path_simplify(arg_mount_where, false); |
afde5b16 | 405 | |
baaa35ad ZJS |
406 | if (!path_is_absolute(arg_mount_where)) |
407 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
408 | "Only absolute path is supported: %s", arg_mount_where); | |
afde5b16 | 409 | } |
450442cf LP |
410 | } else |
411 | arg_discover = true; | |
412 | ||
baaa35ad ZJS |
413 | if (arg_discover && arg_transport != BUS_TRANSPORT_LOCAL) |
414 | return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), | |
415 | "Automatic mount location discovery is only supported locally."); | |
450442cf LP |
416 | } |
417 | ||
418 | return 1; | |
419 | } | |
420 | ||
89ada3ba | 421 | static int transient_unit_set_properties(sd_bus_message *m, UnitType t, char **properties) { |
450442cf LP |
422 | int r; |
423 | ||
05b4d3b5 AK |
424 | if (!isempty(arg_description)) { |
425 | r = sd_bus_message_append(m, "(sv)", "Description", "s", arg_description); | |
426 | if (r < 0) | |
427 | return r; | |
428 | } | |
450442cf LP |
429 | |
430 | if (arg_bind_device && is_device_path(arg_mount_what)) { | |
431 | _cleanup_free_ char *device_unit = NULL; | |
432 | ||
433 | r = unit_name_from_path(arg_mount_what, ".device", &device_unit); | |
434 | if (r < 0) | |
435 | return r; | |
436 | ||
437 | r = sd_bus_message_append(m, "(sv)(sv)", | |
438 | "After", "as", 1, device_unit, | |
439 | "BindsTo", "as", 1, device_unit); | |
440 | if (r < 0) | |
441 | return r; | |
442 | } | |
443 | ||
dc336483 YW |
444 | if (arg_aggressive_gc) { |
445 | r = sd_bus_message_append(m, "(sv)", "CollectMode", "s", "inactive-or-failed"); | |
446 | if (r < 0) | |
447 | return r; | |
448 | } | |
449 | ||
89ada3ba | 450 | r = bus_append_unit_property_assignment_many(m, t, properties); |
450442cf LP |
451 | if (r < 0) |
452 | return r; | |
453 | ||
454 | return 0; | |
455 | } | |
456 | ||
457 | static int transient_mount_set_properties(sd_bus_message *m) { | |
e09fc884 | 458 | _cleanup_free_ char *options = NULL; |
450442cf LP |
459 | int r; |
460 | ||
461 | assert(m); | |
462 | ||
89ada3ba | 463 | r = transient_unit_set_properties(m, UNIT_MOUNT, arg_property); |
450442cf LP |
464 | if (r < 0) |
465 | return r; | |
466 | ||
467 | if (arg_mount_what) { | |
468 | r = sd_bus_message_append(m, "(sv)", "What", "s", arg_mount_what); | |
469 | if (r < 0) | |
470 | return r; | |
471 | } | |
472 | ||
473 | if (arg_mount_type) { | |
474 | r = sd_bus_message_append(m, "(sv)", "Type", "s", arg_mount_type); | |
475 | if (r < 0) | |
476 | return r; | |
477 | } | |
478 | ||
e09fc884 ZJS |
479 | /* Prepend uid=…,gid=… if arg_uid is set */ |
480 | if (arg_uid != UID_INVALID) { | |
481 | r = asprintf(&options, | |
482 | "uid=" UID_FMT ",gid=" GID_FMT "%s%s", | |
483 | arg_uid, arg_gid, | |
980982da | 484 | arg_mount_options ? "," : "", strempty(arg_mount_options)); |
450442cf | 485 | if (r < 0) |
e09fc884 | 486 | return -ENOMEM; |
450442cf LP |
487 | } |
488 | ||
e09fc884 ZJS |
489 | if (options || arg_mount_options) { |
490 | log_debug("Using mount options: %s", options ?: arg_mount_options); | |
491 | ||
492 | r = sd_bus_message_append(m, "(sv)", "Options", "s", options ?: arg_mount_options); | |
493 | if (r < 0) | |
494 | return r; | |
495 | } else | |
496 | log_debug("Not using any mount options"); | |
497 | ||
450442cf LP |
498 | if (arg_fsck) { |
499 | _cleanup_free_ char *fsck = NULL; | |
500 | ||
501 | r = unit_name_from_path_instance("systemd-fsck", arg_mount_what, ".service", &fsck); | |
502 | if (r < 0) | |
503 | return r; | |
504 | ||
505 | r = sd_bus_message_append(m, | |
506 | "(sv)(sv)", | |
507 | "Requires", "as", 1, fsck, | |
508 | "After", "as", 1, fsck); | |
509 | if (r < 0) | |
510 | return r; | |
511 | } | |
512 | ||
513 | return 0; | |
514 | } | |
515 | ||
516 | static int transient_automount_set_properties(sd_bus_message *m) { | |
517 | int r; | |
518 | ||
519 | assert(m); | |
520 | ||
89ada3ba | 521 | r = transient_unit_set_properties(m, UNIT_AUTOMOUNT, arg_automount_property); |
450442cf LP |
522 | if (r < 0) |
523 | return r; | |
524 | ||
525 | if (arg_timeout_idle != USEC_INFINITY) { | |
526 | r = sd_bus_message_append(m, "(sv)", "TimeoutIdleUSec", "t", arg_timeout_idle); | |
527 | if (r < 0) | |
528 | return r; | |
529 | } | |
530 | ||
531 | return 0; | |
532 | } | |
533 | ||
534 | static int start_transient_mount( | |
535 | sd_bus *bus, | |
536 | char **argv) { | |
537 | ||
538 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL, *reply = NULL; | |
539 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
540 | _cleanup_(bus_wait_for_jobs_freep) BusWaitForJobs *w = NULL; | |
541 | _cleanup_free_ char *mount_unit = NULL; | |
542 | int r; | |
543 | ||
544 | if (!arg_no_block) { | |
545 | r = bus_wait_for_jobs_new(bus, &w); | |
546 | if (r < 0) | |
547 | return log_error_errno(r, "Could not watch jobs: %m"); | |
548 | } | |
549 | ||
550 | r = unit_name_from_path(arg_mount_where, ".mount", &mount_unit); | |
551 | if (r < 0) | |
552 | return log_error_errno(r, "Failed to make mount unit name: %m"); | |
553 | ||
92cb8ebc | 554 | r = bus_message_new_method_call(bus, &m, bus_systemd_mgr, "StartTransientUnit"); |
450442cf LP |
555 | if (r < 0) |
556 | return bus_log_create_error(r); | |
557 | ||
558 | r = sd_bus_message_set_allow_interactive_authorization(m, arg_ask_password); | |
559 | if (r < 0) | |
560 | return bus_log_create_error(r); | |
561 | ||
562 | /* Name and mode */ | |
563 | r = sd_bus_message_append(m, "ss", mount_unit, "fail"); | |
564 | if (r < 0) | |
565 | return bus_log_create_error(r); | |
566 | ||
567 | /* Properties */ | |
568 | r = sd_bus_message_open_container(m, 'a', "(sv)"); | |
569 | if (r < 0) | |
570 | return bus_log_create_error(r); | |
571 | ||
572 | r = transient_mount_set_properties(m); | |
573 | if (r < 0) | |
574 | return bus_log_create_error(r); | |
575 | ||
576 | r = sd_bus_message_close_container(m); | |
577 | if (r < 0) | |
578 | return bus_log_create_error(r); | |
579 | ||
580 | /* Auxiliary units */ | |
581 | r = sd_bus_message_append(m, "a(sa(sv))", 0); | |
582 | if (r < 0) | |
583 | return bus_log_create_error(r); | |
584 | ||
8a4b13c5 | 585 | polkit_agent_open_if_enabled(arg_transport, arg_ask_password); |
450442cf LP |
586 | |
587 | r = sd_bus_call(bus, m, 0, &error, &reply); | |
588 | if (r < 0) | |
589 | return log_error_errno(r, "Failed to start transient mount unit: %s", bus_error_message(&error, r)); | |
590 | ||
591 | if (w) { | |
592 | const char *object; | |
593 | ||
594 | r = sd_bus_message_read(reply, "o", &object); | |
595 | if (r < 0) | |
596 | return bus_log_parse_error(r); | |
597 | ||
598 | r = bus_wait_for_jobs_one(w, object, arg_quiet); | |
599 | if (r < 0) | |
600 | return r; | |
601 | } | |
602 | ||
603 | if (!arg_quiet) | |
604 | log_info("Started unit %s%s%s for mount point: %s%s%s", | |
605 | ansi_highlight(), mount_unit, ansi_normal(), | |
606 | ansi_highlight(), arg_mount_where, ansi_normal()); | |
607 | ||
608 | return 0; | |
609 | } | |
610 | ||
611 | static int start_transient_automount( | |
612 | sd_bus *bus, | |
613 | char **argv) { | |
614 | ||
615 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL, *reply = NULL; | |
616 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
617 | _cleanup_(bus_wait_for_jobs_freep) BusWaitForJobs *w = NULL; | |
618 | _cleanup_free_ char *automount_unit = NULL, *mount_unit = NULL; | |
619 | int r; | |
620 | ||
621 | if (!arg_no_block) { | |
622 | r = bus_wait_for_jobs_new(bus, &w); | |
623 | if (r < 0) | |
624 | return log_error_errno(r, "Could not watch jobs: %m"); | |
625 | } | |
626 | ||
627 | r = unit_name_from_path(arg_mount_where, ".automount", &automount_unit); | |
628 | if (r < 0) | |
629 | return log_error_errno(r, "Failed to make automount unit name: %m"); | |
630 | ||
631 | r = unit_name_from_path(arg_mount_where, ".mount", &mount_unit); | |
632 | if (r < 0) | |
633 | return log_error_errno(r, "Failed to make mount unit name: %m"); | |
634 | ||
92cb8ebc | 635 | r = bus_message_new_method_call(bus, &m, bus_systemd_mgr, "StartTransientUnit"); |
450442cf LP |
636 | if (r < 0) |
637 | return bus_log_create_error(r); | |
638 | ||
639 | r = sd_bus_message_set_allow_interactive_authorization(m, arg_ask_password); | |
640 | if (r < 0) | |
641 | return bus_log_create_error(r); | |
642 | ||
643 | /* Name and mode */ | |
644 | r = sd_bus_message_append(m, "ss", automount_unit, "fail"); | |
645 | if (r < 0) | |
646 | return bus_log_create_error(r); | |
647 | ||
648 | /* Properties */ | |
649 | r = sd_bus_message_open_container(m, 'a', "(sv)"); | |
650 | if (r < 0) | |
651 | return bus_log_create_error(r); | |
652 | ||
653 | r = transient_automount_set_properties(m); | |
654 | if (r < 0) | |
655 | return bus_log_create_error(r); | |
656 | ||
657 | r = sd_bus_message_close_container(m); | |
658 | if (r < 0) | |
659 | return bus_log_create_error(r); | |
660 | ||
661 | /* Auxiliary units */ | |
662 | r = sd_bus_message_open_container(m, 'a', "(sa(sv))"); | |
663 | if (r < 0) | |
664 | return bus_log_create_error(r); | |
665 | ||
666 | r = sd_bus_message_open_container(m, 'r', "sa(sv)"); | |
667 | if (r < 0) | |
668 | return bus_log_create_error(r); | |
669 | ||
670 | r = sd_bus_message_append(m, "s", mount_unit); | |
671 | if (r < 0) | |
672 | return bus_log_create_error(r); | |
673 | ||
674 | r = sd_bus_message_open_container(m, 'a', "(sv)"); | |
675 | if (r < 0) | |
676 | return bus_log_create_error(r); | |
677 | ||
678 | r = transient_mount_set_properties(m); | |
679 | if (r < 0) | |
680 | return bus_log_create_error(r); | |
681 | ||
682 | r = sd_bus_message_close_container(m); | |
683 | if (r < 0) | |
684 | return bus_log_create_error(r); | |
685 | ||
686 | r = sd_bus_message_close_container(m); | |
687 | if (r < 0) | |
688 | return bus_log_create_error(r); | |
689 | ||
690 | r = sd_bus_message_close_container(m); | |
691 | if (r < 0) | |
692 | return bus_log_create_error(r); | |
693 | ||
8a4b13c5 | 694 | polkit_agent_open_if_enabled(arg_transport, arg_ask_password); |
450442cf LP |
695 | |
696 | r = sd_bus_call(bus, m, 0, &error, &reply); | |
697 | if (r < 0) | |
698 | return log_error_errno(r, "Failed to start transient automount unit: %s", bus_error_message(&error, r)); | |
699 | ||
700 | if (w) { | |
701 | const char *object; | |
702 | ||
703 | r = sd_bus_message_read(reply, "o", &object); | |
704 | if (r < 0) | |
705 | return bus_log_parse_error(r); | |
706 | ||
707 | r = bus_wait_for_jobs_one(w, object, arg_quiet); | |
708 | if (r < 0) | |
709 | return r; | |
710 | } | |
711 | ||
712 | if (!arg_quiet) | |
713 | log_info("Started unit %s%s%s for mount point: %s%s%s", | |
714 | ansi_highlight(), automount_unit, ansi_normal(), | |
715 | ansi_highlight(), arg_mount_where, ansi_normal()); | |
716 | ||
717 | return 0; | |
718 | } | |
719 | ||
f0aac575 | 720 | static int find_mount_points(const char *what, char ***list) { |
7d991d48 ZJS |
721 | _cleanup_(mnt_free_tablep) struct libmnt_table *table = NULL; |
722 | _cleanup_(mnt_free_iterp) struct libmnt_iter *iter = NULL; | |
f0aac575 YW |
723 | _cleanup_strv_free_ char **l = NULL; |
724 | size_t bufsize = 0, n = 0; | |
7d991d48 | 725 | int r; |
f0aac575 YW |
726 | |
727 | assert(what); | |
728 | assert(list); | |
729 | ||
730 | /* Returns all mount points obtained from /proc/self/mountinfo in *list, | |
731 | * and the number of mount points as return value. */ | |
732 | ||
e2857b3d | 733 | r = libmount_parse(NULL, NULL, &table, &iter); |
7d991d48 ZJS |
734 | if (r < 0) |
735 | return log_error_errno(r, "Failed to parse /proc/self/mountinfo: %m"); | |
f0aac575 YW |
736 | |
737 | for (;;) { | |
7d991d48 ZJS |
738 | struct libmnt_fs *fs; |
739 | const char *source, *target; | |
f0aac575 | 740 | |
7d991d48 ZJS |
741 | r = mnt_table_next_fs(table, iter, &fs); |
742 | if (r == 1) | |
743 | break; | |
744 | if (r < 0) | |
745 | return log_error_errno(r, "Failed to get next entry from /proc/self/mountinfo: %m"); | |
f0aac575 | 746 | |
7d991d48 ZJS |
747 | source = mnt_fs_get_source(fs); |
748 | target = mnt_fs_get_target(fs); | |
749 | if (!source || !target) | |
f0aac575 YW |
750 | continue; |
751 | ||
7d991d48 | 752 | if (!path_equal(source, what)) |
f0aac575 YW |
753 | continue; |
754 | ||
755 | /* one extra slot is needed for the terminating NULL */ | |
7d991d48 | 756 | if (!GREEDY_REALLOC0(l, bufsize, n + 2)) |
f0aac575 YW |
757 | return log_oom(); |
758 | ||
7d991d48 ZJS |
759 | l[n] = strdup(target); |
760 | if (!l[n]) | |
761 | return log_oom(); | |
762 | n++; | |
f0aac575 YW |
763 | } |
764 | ||
7d991d48 | 765 | if (!GREEDY_REALLOC0(l, bufsize, n + 1)) |
290843c3 LP |
766 | return log_oom(); |
767 | ||
ae2a15bc | 768 | *list = TAKE_PTR(l); |
f0aac575 YW |
769 | return n; |
770 | } | |
771 | ||
772 | static int find_loop_device(const char *backing_file, char **loop_dev) { | |
773 | _cleanup_closedir_ DIR *d = NULL; | |
774 | struct dirent *de; | |
775 | _cleanup_free_ char *l = NULL; | |
776 | ||
777 | assert(backing_file); | |
778 | assert(loop_dev); | |
779 | ||
780 | d = opendir("/sys/devices/virtual/block"); | |
5c6803f5 LP |
781 | if (!d) |
782 | return -errno; | |
f0aac575 YW |
783 | |
784 | FOREACH_DIRENT(de, d, return -errno) { | |
785 | _cleanup_free_ char *sys = NULL, *fname = NULL; | |
786 | int r; | |
787 | ||
788 | dirent_ensure_type(d, de); | |
789 | ||
790 | if (de->d_type != DT_DIR) | |
791 | continue; | |
792 | ||
793 | if (!startswith(de->d_name, "loop")) | |
794 | continue; | |
795 | ||
657ee2d8 | 796 | sys = path_join("/sys/devices/virtual/block", de->d_name, "loop/backing_file"); |
f0aac575 | 797 | if (!sys) |
5c6803f5 | 798 | return -ENOMEM; |
f0aac575 YW |
799 | |
800 | r = read_one_line_file(sys, &fname); | |
a53dceb7 LP |
801 | if (r < 0) { |
802 | log_debug_errno(r, "Failed to read %s, ignoring: %m", sys); | |
f0aac575 | 803 | continue; |
a53dceb7 | 804 | } |
f0aac575 YW |
805 | |
806 | if (files_same(fname, backing_file, 0) <= 0) | |
807 | continue; | |
808 | ||
657ee2d8 | 809 | l = path_join("/dev", de->d_name); |
f0aac575 | 810 | if (!l) |
5c6803f5 | 811 | return -ENOMEM; |
f0aac575 YW |
812 | |
813 | break; | |
814 | } | |
815 | ||
816 | if (!l) | |
aa46fa64 | 817 | return -ENXIO; |
f0aac575 | 818 | |
ae2a15bc | 819 | *loop_dev = TAKE_PTR(l); |
f0aac575 YW |
820 | |
821 | return 0; | |
822 | } | |
823 | ||
c37fb55b LR |
824 | static int stop_mount( |
825 | sd_bus *bus, | |
9017f5d8 | 826 | const char *where, |
6f6165bf | 827 | const char *suffix) { |
c37fb55b LR |
828 | |
829 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL, *reply = NULL; | |
830 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
831 | _cleanup_(bus_wait_for_jobs_freep) BusWaitForJobs *w = NULL; | |
832 | _cleanup_free_ char *mount_unit = NULL; | |
833 | int r; | |
834 | ||
835 | if (!arg_no_block) { | |
836 | r = bus_wait_for_jobs_new(bus, &w); | |
837 | if (r < 0) | |
838 | return log_error_errno(r, "Could not watch jobs: %m"); | |
839 | } | |
840 | ||
9017f5d8 | 841 | r = unit_name_from_path(where, suffix, &mount_unit); |
c37fb55b | 842 | if (r < 0) |
6442c210 | 843 | return log_error_errno(r, "Failed to make %s unit name from path %s: %m", suffix + 1, where); |
c37fb55b | 844 | |
92cb8ebc | 845 | r = bus_message_new_method_call(bus, &m, bus_systemd_mgr, "StopUnit"); |
c37fb55b LR |
846 | if (r < 0) |
847 | return bus_log_create_error(r); | |
848 | ||
849 | r = sd_bus_message_set_allow_interactive_authorization(m, arg_ask_password); | |
850 | if (r < 0) | |
851 | return bus_log_create_error(r); | |
852 | ||
853 | /* Name and mode */ | |
854 | r = sd_bus_message_append(m, "ss", mount_unit, "fail"); | |
855 | if (r < 0) | |
856 | return bus_log_create_error(r); | |
857 | ||
8a4b13c5 | 858 | polkit_agent_open_if_enabled(arg_transport, arg_ask_password); |
c37fb55b LR |
859 | |
860 | r = sd_bus_call(bus, m, 0, &error, &reply); | |
6442c210 YW |
861 | if (r < 0) { |
862 | if (streq(suffix, ".automount") && | |
863 | sd_bus_error_has_name(&error, "org.freedesktop.systemd1.NoSuchUnit")) | |
864 | return 0; | |
865 | return log_error_errno(r, "Failed to stop %s unit: %s", suffix + 1, bus_error_message(&error, r)); | |
866 | } | |
c37fb55b LR |
867 | |
868 | if (w) { | |
869 | const char *object; | |
870 | ||
871 | r = sd_bus_message_read(reply, "o", &object); | |
872 | if (r < 0) | |
873 | return bus_log_parse_error(r); | |
874 | ||
875 | r = bus_wait_for_jobs_one(w, object, arg_quiet); | |
876 | if (r < 0) | |
877 | return r; | |
878 | } | |
879 | ||
880 | if (!arg_quiet) | |
881 | log_info("Stopped unit %s%s%s for mount point: %s%s%s", | |
882 | ansi_highlight(), mount_unit, ansi_normal(), | |
9017f5d8 | 883 | ansi_highlight(), where, ansi_normal()); |
c37fb55b LR |
884 | |
885 | return 0; | |
886 | } | |
887 | ||
888 | static int stop_mounts( | |
889 | sd_bus *bus, | |
6f6165bf | 890 | const char *where) { |
c37fb55b LR |
891 | |
892 | int r; | |
893 | ||
baaa35ad ZJS |
894 | if (path_equal(where, "/")) |
895 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
896 | "Refusing to operate on root directory: %s", where); | |
9017f5d8 | 897 | |
baaa35ad ZJS |
898 | if (!path_is_normalized(where)) |
899 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
900 | "Path contains non-normalized components: %s", where); | |
9017f5d8 | 901 | |
6f6165bf | 902 | r = stop_mount(bus, where, ".mount"); |
c37fb55b LR |
903 | if (r < 0) |
904 | return r; | |
905 | ||
6f6165bf | 906 | r = stop_mount(bus, where, ".automount"); |
c37fb55b LR |
907 | if (r < 0) |
908 | return r; | |
909 | ||
910 | return 0; | |
911 | } | |
912 | ||
9017f5d8 | 913 | static int umount_by_device(sd_bus *bus, const char *what) { |
dcd26523 | 914 | _cleanup_(sd_device_unrefp) sd_device *d = NULL; |
f0aac575 | 915 | _cleanup_strv_free_ char **list = NULL; |
9017f5d8 YW |
916 | struct stat st; |
917 | const char *v; | |
f0aac575 | 918 | char **l; |
6f6165bf | 919 | int r, r2 = 0; |
9017f5d8 YW |
920 | |
921 | assert(what); | |
922 | ||
923 | if (stat(what, &st) < 0) | |
924 | return log_error_errno(errno, "Can't stat %s: %m", what); | |
925 | ||
d7a0f1f4 FS |
926 | if (!S_ISBLK(st.st_mode)) |
927 | return log_error_errno(SYNTHETIC_ERRNO(ENOTBLK), | |
928 | "Not a block device: %s", what); | |
9017f5d8 | 929 | |
dcd26523 YW |
930 | r = sd_device_new_from_devnum(&d, 'b', st.st_rdev); |
931 | if (r < 0) | |
932 | return log_error_errno(r, "Failed to get device from device number: %m"); | |
9017f5d8 | 933 | |
dcd26523 YW |
934 | r = sd_device_get_property_value(d, "ID_FS_USAGE", &v); |
935 | if (r < 0) | |
3d0ac9d0 | 936 | return log_device_error_errno(d, r, "Failed to get device property: %m"); |
9017f5d8 | 937 | |
9e791238 ZJS |
938 | if (!streq(v, "filesystem")) |
939 | return log_device_error_errno(d, SYNTHETIC_ERRNO(EINVAL), | |
940 | "%s does not contain a known file system.", what); | |
9017f5d8 | 941 | |
dcd26523 | 942 | if (sd_device_get_property_value(d, "SYSTEMD_MOUNT_WHERE", &v) >= 0) |
f0aac575 | 943 | r2 = stop_mounts(bus, v); |
9017f5d8 | 944 | |
f0aac575 YW |
945 | r = find_mount_points(what, &list); |
946 | if (r < 0) | |
947 | return r; | |
948 | ||
949 | for (l = list; *l; l++) { | |
950 | r = stop_mounts(bus, *l); | |
6f6165bf | 951 | if (r < 0) |
f0aac575 | 952 | r2 = r; |
9017f5d8 YW |
953 | } |
954 | ||
6f6165bf YW |
955 | return r2; |
956 | } | |
957 | ||
958 | static int umount_loop(sd_bus *bus, const char *backing_file) { | |
f0aac575 YW |
959 | _cleanup_free_ char *loop_dev = NULL; |
960 | int r; | |
6f6165bf YW |
961 | |
962 | assert(backing_file); | |
963 | ||
f0aac575 YW |
964 | r = find_loop_device(backing_file, &loop_dev); |
965 | if (r < 0) | |
aa46fa64 | 966 | return log_error_errno(r, r == -ENXIO ? "File %s is not mounted." : "Can't get loop device for %s: %m", backing_file); |
6f6165bf | 967 | |
f0aac575 | 968 | return umount_by_device(bus, loop_dev); |
9017f5d8 YW |
969 | } |
970 | ||
971 | static int action_umount( | |
972 | sd_bus *bus, | |
973 | int argc, | |
974 | char **argv) { | |
975 | ||
976 | int i, r, r2 = 0; | |
977 | ||
3747daa2 YW |
978 | if (arg_transport != BUS_TRANSPORT_LOCAL) { |
979 | for (i = optind; i < argc; i++) { | |
980 | _cleanup_free_ char *p = NULL; | |
981 | ||
982 | p = strdup(argv[i]); | |
983 | if (!p) | |
984 | return log_oom(); | |
985 | ||
858d36c1 | 986 | path_simplify(p, false); |
3747daa2 YW |
987 | |
988 | r = stop_mounts(bus, p); | |
989 | if (r < 0) | |
990 | r2 = r; | |
991 | } | |
992 | return r2; | |
993 | } | |
994 | ||
9017f5d8 | 995 | for (i = optind; i < argc; i++) { |
5bc9f949 | 996 | _cleanup_free_ char *u = NULL, *p = NULL; |
6f6165bf | 997 | struct stat st; |
9017f5d8 YW |
998 | |
999 | u = fstab_node_to_udev_node(argv[i]); | |
1000 | if (!u) | |
1001 | return log_oom(); | |
9017f5d8 | 1002 | |
a5648b80 | 1003 | r = chase_symlinks(u, NULL, 0, &p, NULL); |
9017f5d8 | 1004 | if (r < 0) { |
02a8bd65 | 1005 | r2 = log_error_errno(r, "Failed to make path %s absolute: %m", argv[i]); |
9017f5d8 YW |
1006 | continue; |
1007 | } | |
1008 | ||
6f6165bf | 1009 | if (stat(p, &st) < 0) |
02a8bd65 | 1010 | return log_error_errno(errno, "Can't stat %s (from %s): %m", p, argv[i]); |
9017f5d8 | 1011 | |
6f6165bf YW |
1012 | if (S_ISBLK(st.st_mode)) |
1013 | r = umount_by_device(bus, p); | |
1014 | else if (S_ISREG(st.st_mode)) | |
1015 | r = umount_loop(bus, p); | |
1016 | else if (S_ISDIR(st.st_mode)) | |
1017 | r = stop_mounts(bus, p); | |
1018 | else { | |
02a8bd65 | 1019 | log_error("Invalid file type: %s (from %s)", p, argv[i]); |
6f6165bf YW |
1020 | r = -EINVAL; |
1021 | } | |
1022 | ||
1023 | if (r < 0) | |
9017f5d8 YW |
1024 | r2 = r; |
1025 | } | |
1026 | ||
1027 | return r2; | |
1028 | } | |
1029 | ||
dcd26523 | 1030 | static int acquire_mount_type(sd_device *d) { |
450442cf LP |
1031 | const char *v; |
1032 | ||
1033 | assert(d); | |
1034 | ||
1035 | if (arg_mount_type) | |
1036 | return 0; | |
1037 | ||
dcd26523 | 1038 | if (sd_device_get_property_value(d, "ID_FS_TYPE", &v) < 0) |
450442cf LP |
1039 | return 0; |
1040 | ||
1041 | arg_mount_type = strdup(v); | |
1042 | if (!arg_mount_type) | |
1043 | return log_oom(); | |
1044 | ||
1045 | log_debug("Discovered type=%s", arg_mount_type); | |
1046 | return 1; | |
1047 | } | |
1048 | ||
dcd26523 | 1049 | static int acquire_mount_options(sd_device *d) { |
450442cf LP |
1050 | const char *v; |
1051 | ||
dcd26523 YW |
1052 | assert(d); |
1053 | ||
450442cf LP |
1054 | if (arg_mount_options) |
1055 | return 0; | |
1056 | ||
dcd26523 | 1057 | if (sd_device_get_property_value(d, "SYSTEMD_MOUNT_OPTIONS", &v) < 0) |
450442cf LP |
1058 | return 0; |
1059 | ||
1060 | arg_mount_options = strdup(v); | |
1061 | if (!arg_mount_options) | |
1062 | return log_oom(); | |
1063 | ||
1064 | log_debug("Discovered options=%s", arg_mount_options); | |
1065 | return 1; | |
1066 | } | |
1067 | ||
dcd26523 | 1068 | static const char *get_model(sd_device *d) { |
450442cf LP |
1069 | const char *model; |
1070 | ||
1071 | assert(d); | |
1072 | ||
dcd26523 | 1073 | if (sd_device_get_property_value(d, "ID_MODEL_FROM_DATABASE", &model) >= 0) |
450442cf LP |
1074 | return model; |
1075 | ||
dcd26523 YW |
1076 | if (sd_device_get_property_value(d, "ID_MODEL", &model) >= 0) |
1077 | return model; | |
1078 | ||
1079 | return NULL; | |
450442cf LP |
1080 | } |
1081 | ||
dcd26523 | 1082 | static const char* get_label(sd_device *d) { |
450442cf LP |
1083 | const char *label; |
1084 | ||
1085 | assert(d); | |
1086 | ||
dcd26523 YW |
1087 | if (sd_device_get_property_value(d, "ID_FS_LABEL", &label) >= 0) |
1088 | return label; | |
1089 | ||
1090 | if (sd_device_get_property_value(d, "ID_PART_ENTRY_NAME", &label) >= 0) | |
450442cf LP |
1091 | return label; |
1092 | ||
dcd26523 | 1093 | return NULL; |
450442cf LP |
1094 | } |
1095 | ||
dcd26523 | 1096 | static int acquire_mount_where(sd_device *d) { |
450442cf LP |
1097 | const char *v; |
1098 | ||
1099 | if (arg_mount_where) | |
1100 | return 0; | |
1101 | ||
dcd26523 | 1102 | if (sd_device_get_property_value(d, "SYSTEMD_MOUNT_WHERE", &v) < 0) { |
450442cf LP |
1103 | _cleanup_free_ char *escaped = NULL; |
1104 | const char *name; | |
1105 | ||
1106 | name = get_label(d); | |
1107 | if (!name) | |
1108 | name = get_model(d); | |
1109 | if (!name) { | |
1110 | const char *dn; | |
1111 | ||
dcd26523 | 1112 | if (sd_device_get_devname(d, &dn) < 0) |
450442cf LP |
1113 | return 0; |
1114 | ||
1115 | name = basename(dn); | |
1116 | } | |
1117 | ||
1118 | escaped = xescape(name, "\\"); | |
f0aac575 YW |
1119 | if (!escaped) |
1120 | return log_oom(); | |
450442cf LP |
1121 | if (!filename_is_valid(escaped)) |
1122 | return 0; | |
1123 | ||
657ee2d8 | 1124 | arg_mount_where = path_join("/run/media/system", escaped); |
450442cf LP |
1125 | } else |
1126 | arg_mount_where = strdup(v); | |
1127 | ||
1128 | if (!arg_mount_where) | |
1129 | return log_oom(); | |
1130 | ||
1131 | log_debug("Discovered where=%s", arg_mount_where); | |
1132 | return 1; | |
1133 | } | |
1134 | ||
f0aac575 YW |
1135 | static int acquire_mount_where_for_loop_dev(const char *loop_dev) { |
1136 | _cleanup_strv_free_ char **list = NULL; | |
1137 | int r; | |
1138 | ||
1139 | if (arg_mount_where) | |
1140 | return 0; | |
1141 | ||
1142 | r = find_mount_points(loop_dev, &list); | |
1143 | if (r < 0) | |
1144 | return r; | |
baaa35ad ZJS |
1145 | else if (r == 0) |
1146 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
1147 | "Can't find mount point of %s. It is expected that %s is already mounted on a place.", | |
1148 | loop_dev, loop_dev); | |
1149 | else if (r >= 2) | |
1150 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
1151 | "%s is mounted on %d places. It is expected that %s is mounted on a place.", | |
1152 | loop_dev, r, loop_dev); | |
f0aac575 YW |
1153 | |
1154 | arg_mount_where = strdup(list[0]); | |
1155 | if (!arg_mount_where) | |
1156 | return log_oom(); | |
1157 | ||
1158 | log_debug("Discovered where=%s", arg_mount_where); | |
1159 | return 1; | |
1160 | } | |
1161 | ||
dcd26523 | 1162 | static int acquire_description(sd_device *d) { |
450442cf LP |
1163 | const char *model, *label; |
1164 | ||
1165 | if (arg_description) | |
1166 | return 0; | |
1167 | ||
1168 | model = get_model(d); | |
1169 | ||
1170 | label = get_label(d); | |
1171 | if (!label) | |
dcd26523 | 1172 | (void) sd_device_get_property_value(d, "ID_PART_ENTRY_NUMBER", &label); |
450442cf LP |
1173 | |
1174 | if (model && label) | |
605405c6 | 1175 | arg_description = strjoin(model, " ", label); |
450442cf LP |
1176 | else if (label) |
1177 | arg_description = strdup(label); | |
1178 | else if (model) | |
1179 | arg_description = strdup(model); | |
1180 | else | |
05b4d3b5 | 1181 | return 0; |
450442cf LP |
1182 | |
1183 | if (!arg_description) | |
1184 | return log_oom(); | |
1185 | ||
1186 | log_debug("Discovered description=%s", arg_description); | |
1187 | return 1; | |
1188 | } | |
1189 | ||
dcd26523 | 1190 | static int acquire_removable(sd_device *d) { |
450442cf LP |
1191 | const char *v; |
1192 | ||
1193 | /* Shortcut this if there's no reason to check it */ | |
1194 | if (arg_action != ACTION_DEFAULT && arg_timeout_idle_set && arg_bind_device >= 0) | |
1195 | return 0; | |
1196 | ||
1197 | for (;;) { | |
dcd26523 | 1198 | if (sd_device_get_sysattr_value(d, "removable", &v) > 0) |
450442cf LP |
1199 | break; |
1200 | ||
dcd26523 | 1201 | if (sd_device_get_parent(d, &d) < 0) |
450442cf LP |
1202 | return 0; |
1203 | ||
dcd26523 | 1204 | if (sd_device_get_subsystem(d, &v) < 0 || !streq(v, "block")) |
450442cf LP |
1205 | return 0; |
1206 | } | |
1207 | ||
1208 | if (parse_boolean(v) <= 0) | |
1209 | return 0; | |
1210 | ||
1211 | log_debug("Discovered removable device."); | |
1212 | ||
1213 | if (arg_action == ACTION_DEFAULT) { | |
1214 | log_debug("Automatically turning on automount."); | |
1215 | arg_action = ACTION_AUTOMOUNT; | |
1216 | } | |
1217 | ||
1218 | if (!arg_timeout_idle_set) { | |
1219 | log_debug("Setting idle timeout to 1s."); | |
1220 | arg_timeout_idle = USEC_PER_SEC; | |
1221 | } | |
1222 | ||
1223 | if (arg_bind_device < 0) { | |
1224 | log_debug("Binding automount unit to device."); | |
1225 | arg_bind_device = true; | |
1226 | } | |
1227 | ||
1228 | return 1; | |
1229 | } | |
1230 | ||
f0aac575 | 1231 | static int discover_loop_backing_file(void) { |
dcd26523 | 1232 | _cleanup_(sd_device_unrefp) sd_device *d = NULL; |
f0aac575 | 1233 | _cleanup_free_ char *loop_dev = NULL; |
450442cf LP |
1234 | struct stat st; |
1235 | const char *v; | |
1236 | int r; | |
1237 | ||
f0aac575 | 1238 | r = find_loop_device(arg_mount_what, &loop_dev); |
aa46fa64 | 1239 | if (r < 0 && r != -ENXIO) |
f0aac575 YW |
1240 | return log_error_errno(errno, "Can't get loop device for %s: %m", arg_mount_what); |
1241 | ||
aa46fa64 | 1242 | if (r == -ENXIO) { |
f0aac575 YW |
1243 | _cleanup_free_ char *escaped = NULL; |
1244 | ||
1245 | if (arg_mount_where) | |
1246 | return 0; | |
1247 | ||
1248 | escaped = xescape(basename(arg_mount_what), "\\"); | |
1249 | if (!escaped) | |
1250 | return log_oom(); | |
d7a0f1f4 FS |
1251 | if (!filename_is_valid(escaped)) |
1252 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
1253 | "Escaped name %s is not a valid filename.", | |
1254 | escaped); | |
f0aac575 | 1255 | |
657ee2d8 | 1256 | arg_mount_where = path_join("/run/media/system", escaped); |
f0aac575 YW |
1257 | if (!arg_mount_where) |
1258 | return log_oom(); | |
1259 | ||
1260 | log_debug("Discovered where=%s", arg_mount_where); | |
450442cf | 1261 | return 0; |
f0aac575 YW |
1262 | } |
1263 | ||
1264 | if (stat(loop_dev, &st) < 0) | |
1265 | return log_error_errno(errno, "Can't stat %s: %m", loop_dev); | |
450442cf | 1266 | |
d7a0f1f4 FS |
1267 | if (!S_ISBLK(st.st_mode)) |
1268 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
1269 | "Invalid file type: %s", loop_dev); | |
450442cf | 1270 | |
dcd26523 YW |
1271 | r = sd_device_new_from_devnum(&d, 'b', st.st_rdev); |
1272 | if (r < 0) | |
1273 | return log_error_errno(r, "Failed to get device from device number: %m"); | |
f0aac575 | 1274 | |
9e791238 ZJS |
1275 | if (sd_device_get_property_value(d, "ID_FS_USAGE", &v) < 0 || !streq(v, "filesystem")) |
1276 | return log_device_error_errno(d, SYNTHETIC_ERRNO(EINVAL), | |
1277 | "%s does not contain a known file system.", arg_mount_what); | |
f0aac575 YW |
1278 | |
1279 | r = acquire_mount_type(d); | |
1280 | if (r < 0) | |
1281 | return r; | |
1282 | ||
1283 | r = acquire_mount_options(d); | |
1284 | if (r < 0) | |
1285 | return r; | |
1286 | ||
1287 | r = acquire_mount_where_for_loop_dev(loop_dev); | |
1288 | if (r < 0) | |
1289 | return r; | |
1290 | ||
1291 | r = acquire_description(d); | |
1292 | if (r < 0) | |
1293 | return r; | |
1294 | ||
1295 | return 0; | |
1296 | } | |
1297 | ||
1298 | static int discover_device(void) { | |
dcd26523 | 1299 | _cleanup_(sd_device_unrefp) sd_device *d = NULL; |
f0aac575 YW |
1300 | struct stat st; |
1301 | const char *v; | |
1302 | int r; | |
1303 | ||
450442cf LP |
1304 | if (stat(arg_mount_what, &st) < 0) |
1305 | return log_error_errno(errno, "Can't stat %s: %m", arg_mount_what); | |
1306 | ||
f0aac575 YW |
1307 | if (S_ISREG(st.st_mode)) |
1308 | return discover_loop_backing_file(); | |
1309 | ||
d7a0f1f4 FS |
1310 | if (!S_ISBLK(st.st_mode)) |
1311 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
1312 | "Invalid file type: %s", | |
1313 | arg_mount_what); | |
450442cf | 1314 | |
dcd26523 YW |
1315 | r = sd_device_new_from_devnum(&d, 'b', st.st_rdev); |
1316 | if (r < 0) | |
1317 | return log_error_errno(r, "Failed to get device from device number: %m"); | |
450442cf | 1318 | |
d7a0f1f4 FS |
1319 | if (sd_device_get_property_value(d, "ID_FS_USAGE", &v) < 0 || !streq(v, "filesystem")) |
1320 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
1321 | "%s does not contain a known file system.", | |
1322 | arg_mount_what); | |
450442cf LP |
1323 | |
1324 | r = acquire_mount_type(d); | |
1325 | if (r < 0) | |
1326 | return r; | |
1327 | ||
1328 | r = acquire_mount_options(d); | |
1329 | if (r < 0) | |
1330 | return r; | |
1331 | ||
1332 | r = acquire_mount_where(d); | |
1333 | if (r < 0) | |
1334 | return r; | |
1335 | ||
1336 | r = acquire_description(d); | |
1337 | if (r < 0) | |
1338 | return r; | |
1339 | ||
1340 | r = acquire_removable(d); | |
1341 | if (r < 0) | |
1342 | return r; | |
1343 | ||
1344 | return 0; | |
1345 | } | |
1346 | ||
1347 | enum { | |
1348 | COLUMN_NODE, | |
1349 | COLUMN_PATH, | |
1350 | COLUMN_MODEL, | |
1351 | COLUMN_WWN, | |
1352 | COLUMN_FSTYPE, | |
1353 | COLUMN_LABEL, | |
1354 | COLUMN_UUID, | |
1355 | _COLUMN_MAX, | |
1356 | }; | |
1357 | ||
450442cf | 1358 | static int list_devices(void) { |
dcd26523 | 1359 | _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL; |
6ae6ea55 | 1360 | _cleanup_(table_unrefp) Table *table = NULL; |
dcd26523 | 1361 | sd_device *d; |
450442cf LP |
1362 | unsigned c; |
1363 | int r; | |
1364 | ||
dcd26523 YW |
1365 | r = sd_device_enumerator_new(&e); |
1366 | if (r < 0) | |
450442cf LP |
1367 | return log_oom(); |
1368 | ||
dcd26523 | 1369 | r = sd_device_enumerator_add_match_subsystem(e, "block", true); |
450442cf LP |
1370 | if (r < 0) |
1371 | return log_error_errno(r, "Failed to add block match: %m"); | |
1372 | ||
dcd26523 | 1373 | r = sd_device_enumerator_add_match_property(e, "ID_FS_USAGE", "filesystem"); |
450442cf LP |
1374 | if (r < 0) |
1375 | return log_error_errno(r, "Failed to add property match: %m"); | |
1376 | ||
6ae6ea55 YW |
1377 | table = table_new("NODE", "PATH", "MODEL", "WWN", "TYPE", "LABEL", "UUID"); |
1378 | if (!table) | |
1379 | return log_oom(); | |
450442cf | 1380 | |
a362c069 YW |
1381 | if (arg_full) |
1382 | table_set_width(table, 0); | |
1383 | ||
ad5555b4 | 1384 | r = table_set_sort(table, (size_t) 0, (size_t) SIZE_MAX); |
6ae6ea55 YW |
1385 | if (r < 0) |
1386 | return log_error_errno(r, "Failed to set sort index: %m"); | |
450442cf | 1387 | |
a5279634 YW |
1388 | table_set_header(table, arg_legend); |
1389 | ||
6ae6ea55 | 1390 | FOREACH_DEVICE(e, d) { |
450442cf | 1391 | for (c = 0; c < _COLUMN_MAX; c++) { |
6fd667e5 | 1392 | const char *x = NULL; |
450442cf LP |
1393 | |
1394 | switch (c) { | |
1395 | ||
1396 | case COLUMN_NODE: | |
dcd26523 | 1397 | (void) sd_device_get_devname(d, &x); |
450442cf LP |
1398 | break; |
1399 | ||
1400 | case COLUMN_PATH: | |
dcd26523 | 1401 | (void) sd_device_get_property_value(d, "ID_PATH", &x); |
450442cf LP |
1402 | break; |
1403 | ||
1404 | case COLUMN_MODEL: | |
1405 | x = get_model(d); | |
1406 | break; | |
1407 | ||
1408 | case COLUMN_WWN: | |
dcd26523 | 1409 | (void) sd_device_get_property_value(d, "ID_WWN", &x); |
450442cf LP |
1410 | break; |
1411 | ||
1412 | case COLUMN_FSTYPE: | |
dcd26523 | 1413 | (void) sd_device_get_property_value(d, "ID_FS_TYPE", &x); |
450442cf LP |
1414 | break; |
1415 | ||
1416 | case COLUMN_LABEL: | |
1417 | x = get_label(d); | |
1418 | break; | |
1419 | ||
1420 | case COLUMN_UUID: | |
dcd26523 | 1421 | (void) sd_device_get_property_value(d, "ID_FS_UUID", &x); |
450442cf LP |
1422 | break; |
1423 | } | |
1424 | ||
6ae6ea55 YW |
1425 | r = table_add_cell(table, NULL, c == COLUMN_NODE ? TABLE_PATH : TABLE_STRING, strna(x)); |
1426 | if (r < 0) | |
bd17fa8c | 1427 | return table_log_add_error(r); |
450442cf LP |
1428 | } |
1429 | } | |
1430 | ||
0221d68a | 1431 | (void) pager_open(arg_pager_flags); |
450442cf | 1432 | |
6ae6ea55 YW |
1433 | r = table_print(table, NULL); |
1434 | if (r < 0) | |
4b6607d9 | 1435 | return table_log_print_error(r); |
450442cf | 1436 | |
6ae6ea55 | 1437 | return 0; |
450442cf LP |
1438 | } |
1439 | ||
f2fae6fb YW |
1440 | static int run(int argc, char* argv[]) { |
1441 | _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; | |
450442cf LP |
1442 | int r; |
1443 | ||
1a043959 | 1444 | log_show_color(true); |
450442cf LP |
1445 | log_parse_environment(); |
1446 | log_open(); | |
1447 | ||
1448 | r = parse_argv(argc, argv); | |
1449 | if (r <= 0) | |
f2fae6fb | 1450 | return r; |
450442cf | 1451 | |
f2fae6fb YW |
1452 | if (arg_action == ACTION_LIST) |
1453 | return list_devices(); | |
450442cf | 1454 | |
9017f5d8 | 1455 | r = bus_connect_transport_systemd(arg_transport, arg_host, arg_user, &bus); |
f2fae6fb | 1456 | if (r < 0) |
ddbab78f | 1457 | return bus_log_connect_error(r); |
9017f5d8 | 1458 | |
f2fae6fb YW |
1459 | if (arg_action == ACTION_UMOUNT) |
1460 | return action_umount(bus, argc, argv); | |
9017f5d8 | 1461 | |
c15ab81e | 1462 | if ((!arg_mount_type || fstype_is_blockdev_backed(arg_mount_type)) |
d7a0f1f4 FS |
1463 | && !path_is_normalized(arg_mount_what)) |
1464 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
1465 | "Path contains non-normalized components: %s", | |
1466 | arg_mount_what); | |
f0aac575 YW |
1467 | |
1468 | if (arg_discover) { | |
1469 | r = discover_device(); | |
1470 | if (r < 0) | |
f2fae6fb | 1471 | return r; |
f0aac575 YW |
1472 | } |
1473 | ||
d7a0f1f4 FS |
1474 | if (!arg_mount_where) |
1475 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
1476 | "Can't figure out where to mount %s.", | |
1477 | arg_mount_what); | |
450442cf | 1478 | |
d7a0f1f4 FS |
1479 | if (path_equal(arg_mount_where, "/")) |
1480 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
1481 | "Refusing to operate on root directory."); | |
450442cf | 1482 | |
d7a0f1f4 FS |
1483 | if (!path_is_normalized(arg_mount_where)) |
1484 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
1485 | "Path contains non-normalized components: %s", | |
1486 | arg_mount_where); | |
450442cf LP |
1487 | |
1488 | if (streq_ptr(arg_mount_type, "auto")) | |
1489 | arg_mount_type = mfree(arg_mount_type); | |
1490 | if (streq_ptr(arg_mount_options, "defaults")) | |
1491 | arg_mount_options = mfree(arg_mount_options); | |
1492 | ||
1493 | if (!is_device_path(arg_mount_what)) | |
1494 | arg_fsck = false; | |
1495 | ||
1496 | if (arg_fsck && arg_mount_type && arg_transport == BUS_TRANSPORT_LOCAL) { | |
1497 | r = fsck_exists(arg_mount_type); | |
1498 | if (r < 0) | |
1499 | log_warning_errno(r, "Couldn't determine whether fsck for %s exists, proceeding anyway.", arg_mount_type); | |
1500 | else if (r == 0) { | |
1501 | log_debug("Disabling file system check as fsck for %s doesn't exist.", arg_mount_type); | |
1502 | arg_fsck = false; /* fsck doesn't exist, let's not attempt it */ | |
1503 | } | |
1504 | } | |
1505 | ||
e09fc884 ZJS |
1506 | /* The kernel (properly) refuses mounting file systems with unknown uid=,gid= options, |
1507 | * but not for all filesystem types. Let's try to catch the cases where the option | |
1508 | * would be used if the file system does not support it. It is also possible to | |
1509 | * autodetect the file system, but that's only possible with disk-based file systems | |
1510 | * which incidentally seem to be implemented more carefully and reject unknown options, | |
1511 | * so it's probably OK that we do the check only when the type is specified. | |
1512 | */ | |
1513 | if (arg_mount_type && | |
1514 | !streq(arg_mount_type, "auto") && | |
1515 | arg_uid != UID_INVALID && | |
d7a0f1f4 FS |
1516 | !fstype_can_uid_gid(arg_mount_type)) |
1517 | return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), | |
1518 | "File system type %s is not known to support uid=/gid=, refusing.", | |
1519 | arg_mount_type); | |
e09fc884 | 1520 | |
450442cf LP |
1521 | switch (arg_action) { |
1522 | ||
1523 | case ACTION_MOUNT: | |
1524 | case ACTION_DEFAULT: | |
1525 | r = start_transient_mount(bus, argv + optind); | |
1526 | break; | |
1527 | ||
1528 | case ACTION_AUTOMOUNT: | |
1529 | r = start_transient_automount(bus, argv + optind); | |
1530 | break; | |
1531 | ||
1532 | default: | |
1533 | assert_not_reached("Unexpected action."); | |
1534 | } | |
1535 | ||
f2fae6fb | 1536 | return r; |
450442cf | 1537 | } |
f2fae6fb YW |
1538 | |
1539 | DEFINE_MAIN_FUNCTION(run); |