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