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