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