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