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