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