]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/shutdown/umount.c
Merge pull request #13936 from keszybz/format-table-uninhibited
[thirdparty/systemd.git] / src / shutdown / umount.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 Copyright © 2010 ProFUSION embedded systems
4 ***/
5
6 #include <errno.h>
7 #include <fcntl.h>
8 #include <linux/dm-ioctl.h>
9 #include <linux/loop.h>
10 #include <sys/mount.h>
11 #include <sys/swap.h>
12 #include <sys/stat.h>
13 #include <sys/types.h>
14 #include <unistd.h>
15
16 #include "sd-device.h"
17
18 #include "alloc-util.h"
19 #include "blockdev-util.h"
20 #include "def.h"
21 #include "device-util.h"
22 #include "escape.h"
23 #include "fd-util.h"
24 #include "fstab-util.h"
25 #include "libmount-util.h"
26 #include "mount-setup.h"
27 #include "mount-util.h"
28 #include "mountpoint-util.h"
29 #include "path-util.h"
30 #include "process-util.h"
31 #include "signal-util.h"
32 #include "string-util.h"
33 #include "strv.h"
34 #include "umount.h"
35 #include "util.h"
36 #include "virt.h"
37
38 static void mount_point_free(MountPoint **head, MountPoint *m) {
39 assert(head);
40 assert(m);
41
42 LIST_REMOVE(mount_point, *head, m);
43
44 free(m->path);
45 free(m->remount_options);
46 free(m);
47 }
48
49 void mount_points_list_free(MountPoint **head) {
50 assert(head);
51
52 while (*head)
53 mount_point_free(head, *head);
54 }
55
56 int mount_points_list_get(const char *mountinfo, MountPoint **head) {
57 _cleanup_(mnt_free_tablep) struct libmnt_table *table = NULL;
58 _cleanup_(mnt_free_iterp) struct libmnt_iter *iter = NULL;
59 int r;
60
61 assert(head);
62
63 r = libmount_parse(mountinfo, NULL, &table, &iter);
64 if (r < 0)
65 return log_error_errno(r, "Failed to parse %s: %m", mountinfo);
66
67 for (;;) {
68 struct libmnt_fs *fs;
69 const char *path, *fstype;
70 _cleanup_free_ char *options = NULL;
71 unsigned long remount_flags = 0u;
72 _cleanup_free_ char *remount_options = NULL;
73 bool try_remount_ro;
74 _cleanup_free_ MountPoint *m = NULL;
75
76 r = mnt_table_next_fs(table, iter, &fs);
77 if (r == 1)
78 break;
79 if (r < 0)
80 return log_error_errno(r, "Failed to get next entry from %s: %m", mountinfo);
81
82 path = mnt_fs_get_target(fs);
83 if (!path)
84 continue;
85
86 fstype = mnt_fs_get_fstype(fs);
87
88 /* Combine the generic VFS options with the FS-specific
89 * options. Duplicates are not a problem here, because the only
90 * options that should come up twice are typically ro/rw, which
91 * are turned into MS_RDONLY or the inversion of it.
92 *
93 * Even if there are duplicates later in mount_option_mangle()
94 * they shouldn't hurt anyways as they override each other.
95 */
96 if (!strextend_with_separator(&options, ",",
97 mnt_fs_get_vfs_options(fs),
98 NULL))
99 return log_oom();
100 if (!strextend_with_separator(&options, ",",
101 mnt_fs_get_fs_options(fs),
102 NULL))
103 return log_oom();
104
105 /* Ignore mount points we can't unmount because they
106 * are API or because we are keeping them open (like
107 * /dev/console). Also, ignore all mounts below API
108 * file systems, since they are likely virtual too,
109 * and hence not worth spending time on. Also, in
110 * unprivileged containers we might lack the rights to
111 * unmount these things, hence don't bother. */
112 if (mount_point_is_api(path) ||
113 mount_point_ignore(path) ||
114 PATH_STARTSWITH_SET(path, "/dev", "/sys", "/proc"))
115 continue;
116
117 /* If we are in a container, don't attempt to
118 * read-only mount anything as that brings no real
119 * benefits, but might confuse the host, as we remount
120 * the superblock here, not the bind mount.
121 *
122 * If the filesystem is a network fs, also skip the
123 * remount. It brings no value (we cannot leave
124 * a "dirty fs") and could hang if the network is down.
125 * Note that umount2() is more careful and will not
126 * hang because of the network being down. */
127 try_remount_ro = detect_container() <= 0 &&
128 !fstype_is_network(fstype) &&
129 !fstype_is_api_vfs(fstype) &&
130 !fstype_is_ro(fstype) &&
131 !fstab_test_yes_no_option(options, "ro\0rw\0");
132
133 if (try_remount_ro) {
134 /* mount(2) states that mount flags and options need to be exactly the same
135 * as they were when the filesystem was mounted, except for the desired
136 * changes. So we reconstruct both here and adjust them for the later
137 * remount call too. */
138
139 r = mnt_fs_get_propagation(fs, &remount_flags);
140 if (r < 0) {
141 log_warning_errno(r, "mnt_fs_get_propagation() failed for %s, ignoring: %m", path);
142 continue;
143 }
144
145 r = mount_option_mangle(options, remount_flags, &remount_flags, &remount_options);
146 if (r < 0) {
147 log_warning_errno(r, "mount_option_mangle failed for %s, ignoring: %m", path);
148 continue;
149 }
150
151 /* MS_BIND is special. If it is provided it will only make the mount-point
152 * read-only. If left out, the super block itself is remounted, which we want. */
153 remount_flags = (remount_flags|MS_REMOUNT|MS_RDONLY) & ~MS_BIND;
154 }
155
156 m = new0(MountPoint, 1);
157 if (!m)
158 return log_oom();
159
160 m->path = strdup(path);
161 if (!m->path)
162 return log_oom();
163
164 m->remount_options = TAKE_PTR(remount_options);
165 m->remount_flags = remount_flags;
166 m->try_remount_ro = try_remount_ro;
167
168 LIST_PREPEND(mount_point, *head, TAKE_PTR(m));
169 }
170
171 return 0;
172 }
173
174 int swap_list_get(const char *swaps, MountPoint **head) {
175 _cleanup_(mnt_free_tablep) struct libmnt_table *t = NULL;
176 _cleanup_(mnt_free_iterp) struct libmnt_iter *i = NULL;
177 int r;
178
179 assert(head);
180
181 t = mnt_new_table();
182 i = mnt_new_iter(MNT_ITER_FORWARD);
183 if (!t || !i)
184 return log_oom();
185
186 r = mnt_table_parse_swaps(t, swaps);
187 if (r < 0)
188 return log_error_errno(r, "Failed to parse %s: %m", swaps);
189
190 for (;;) {
191 struct libmnt_fs *fs;
192 _cleanup_free_ MountPoint *swap = NULL;
193 const char *source;
194
195 r = mnt_table_next_fs(t, i, &fs);
196 if (r == 1)
197 break;
198 if (r < 0)
199 return log_error_errno(r, "Failed to get next entry from %s: %m", swaps);
200
201 source = mnt_fs_get_source(fs);
202 if (!source)
203 continue;
204
205 swap = new0(MountPoint, 1);
206 if (!swap)
207 return -ENOMEM;
208
209 swap->path = strdup(source);
210 if (!swap->path)
211 return -ENOMEM;
212
213 LIST_PREPEND(mount_point, *head, TAKE_PTR(swap));
214 }
215
216 return 0;
217 }
218
219 static int loopback_list_get(MountPoint **head) {
220 _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL;
221 sd_device *d;
222 int r;
223
224 assert(head);
225
226 r = sd_device_enumerator_new(&e);
227 if (r < 0)
228 return r;
229
230 r = sd_device_enumerator_allow_uninitialized(e);
231 if (r < 0)
232 return r;
233
234 r = sd_device_enumerator_add_match_subsystem(e, "block", true);
235 if (r < 0)
236 return r;
237
238 r = sd_device_enumerator_add_match_sysname(e, "loop*");
239 if (r < 0)
240 return r;
241
242 r = sd_device_enumerator_add_match_sysattr(e, "loop/backing_file", NULL, true);
243 if (r < 0)
244 return r;
245
246 FOREACH_DEVICE(e, d) {
247 _cleanup_free_ char *p = NULL;
248 const char *dn;
249 MountPoint *lb;
250
251 if (sd_device_get_devname(d, &dn) < 0)
252 continue;
253
254 p = strdup(dn);
255 if (!p)
256 return -ENOMEM;
257
258 lb = new(MountPoint, 1);
259 if (!lb)
260 return -ENOMEM;
261
262 *lb = (MountPoint) {
263 .path = TAKE_PTR(p),
264 };
265
266 LIST_PREPEND(mount_point, *head, lb);
267 }
268
269 return 0;
270 }
271
272 static int dm_list_get(MountPoint **head) {
273 _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL;
274 sd_device *d;
275 int r;
276
277 assert(head);
278
279 r = sd_device_enumerator_new(&e);
280 if (r < 0)
281 return r;
282
283 r = sd_device_enumerator_allow_uninitialized(e);
284 if (r < 0)
285 return r;
286
287 r = sd_device_enumerator_add_match_subsystem(e, "block", true);
288 if (r < 0)
289 return r;
290
291 r = sd_device_enumerator_add_match_sysname(e, "dm-*");
292 if (r < 0)
293 return r;
294
295 FOREACH_DEVICE(e, d) {
296 _cleanup_free_ char *p = NULL;
297 const char *dn;
298 MountPoint *m;
299 dev_t devnum;
300
301 if (sd_device_get_devnum(d, &devnum) < 0 ||
302 sd_device_get_devname(d, &dn) < 0)
303 continue;
304
305 p = strdup(dn);
306 if (!p)
307 return -ENOMEM;
308
309 m = new(MountPoint, 1);
310 if (!m)
311 return -ENOMEM;
312
313 *m = (MountPoint) {
314 .path = TAKE_PTR(p),
315 .devnum = devnum,
316 };
317
318 LIST_PREPEND(mount_point, *head, m);
319 }
320
321 return 0;
322 }
323
324 static int delete_loopback(const char *device) {
325 _cleanup_close_ int fd = -1;
326 int r;
327
328 assert(device);
329
330 fd = open(device, O_RDONLY|O_CLOEXEC);
331 if (fd < 0)
332 return errno == ENOENT ? 0 : -errno;
333
334 r = ioctl(fd, LOOP_CLR_FD, 0);
335 if (r >= 0)
336 return 1;
337
338 /* ENXIO: not bound, so no error */
339 if (errno == ENXIO)
340 return 0;
341
342 return -errno;
343 }
344
345 static int delete_dm(dev_t devnum) {
346
347 struct dm_ioctl dm = {
348 .version = {
349 DM_VERSION_MAJOR,
350 DM_VERSION_MINOR,
351 DM_VERSION_PATCHLEVEL
352 },
353 .data_size = sizeof(dm),
354 .dev = devnum,
355 };
356
357 _cleanup_close_ int fd = -1;
358
359 assert(major(devnum) != 0);
360
361 fd = open("/dev/mapper/control", O_RDWR|O_CLOEXEC);
362 if (fd < 0)
363 return -errno;
364
365 if (ioctl(fd, DM_DEV_REMOVE, &dm) < 0)
366 return -errno;
367
368 return 0;
369 }
370
371 static bool nonunmountable_path(const char *path) {
372 return path_equal(path, "/")
373 #if ! HAVE_SPLIT_USR
374 || path_equal(path, "/usr")
375 #endif
376 || path_startswith(path, "/run/initramfs");
377 }
378
379 static int remount_with_timeout(MountPoint *m, int umount_log_level) {
380 pid_t pid;
381 int r;
382
383 BLOCK_SIGNALS(SIGCHLD);
384
385 assert(m);
386
387 /* Due to the possibility of a remount operation hanging, we
388 * fork a child process and set a timeout. If the timeout
389 * lapses, the assumption is that that particular remount
390 * failed. */
391 r = safe_fork("(sd-remount)", FORK_RESET_SIGNALS|FORK_CLOSE_ALL_FDS|FORK_LOG|FORK_REOPEN_LOG, &pid);
392 if (r < 0)
393 return r;
394 if (r == 0) {
395 log_info("Remounting '%s' read-only in with options '%s'.", m->path, m->remount_options);
396
397 /* Start the mount operation here in the child */
398 r = mount(NULL, m->path, NULL, m->remount_flags, m->remount_options);
399 if (r < 0)
400 log_full_errno(umount_log_level, errno, "Failed to remount '%s' read-only: %m", m->path);
401
402 _exit(r < 0 ? EXIT_FAILURE : EXIT_SUCCESS);
403 }
404
405 r = wait_for_terminate_with_timeout(pid, DEFAULT_TIMEOUT_USEC);
406 if (r == -ETIMEDOUT) {
407 log_error_errno(r, "Remounting '%s' timed out, issuing SIGKILL to PID " PID_FMT ".", m->path, pid);
408 (void) kill(pid, SIGKILL);
409 } else if (r == -EPROTO)
410 log_debug_errno(r, "Remounting '%s' failed abnormally, child process " PID_FMT " aborted or exited non-zero.", m->path, pid);
411 else if (r < 0)
412 log_error_errno(r, "Remounting '%s' failed unexpectedly, couldn't wait for child process " PID_FMT ": %m", m->path, pid);
413
414 return r;
415 }
416
417 static int umount_with_timeout(MountPoint *m, int umount_log_level) {
418 pid_t pid;
419 int r;
420
421 BLOCK_SIGNALS(SIGCHLD);
422
423 assert(m);
424
425 /* Due to the possibility of a umount operation hanging, we
426 * fork a child process and set a timeout. If the timeout
427 * lapses, the assumption is that that particular umount
428 * failed. */
429 r = safe_fork("(sd-umount)", FORK_RESET_SIGNALS|FORK_CLOSE_ALL_FDS|FORK_LOG|FORK_REOPEN_LOG, &pid);
430 if (r < 0)
431 return r;
432 if (r == 0) {
433 log_info("Unmounting '%s'.", m->path);
434
435 /* Start the mount operation here in the child Using MNT_FORCE
436 * causes some filesystems (e.g. FUSE and NFS and other network
437 * filesystems) to abort any pending requests and return -EIO
438 * rather than blocking indefinitely. If the filesysten is
439 * "busy", this may allow processes to die, thus making the
440 * filesystem less busy so the unmount might succeed (rather
441 * then return EBUSY).*/
442 r = umount2(m->path, MNT_FORCE);
443 if (r < 0)
444 log_full_errno(umount_log_level, errno, "Failed to unmount %s: %m", m->path);
445
446 _exit(r < 0 ? EXIT_FAILURE : EXIT_SUCCESS);
447 }
448
449 r = wait_for_terminate_with_timeout(pid, DEFAULT_TIMEOUT_USEC);
450 if (r == -ETIMEDOUT) {
451 log_error_errno(r, "Unmounting '%s' timed out, issuing SIGKILL to PID " PID_FMT ".", m->path, pid);
452 (void) kill(pid, SIGKILL);
453 } else if (r == -EPROTO)
454 log_debug_errno(r, "Unmounting '%s' failed abnormally, child process " PID_FMT " aborted or exited non-zero.", m->path, pid);
455 else if (r < 0)
456 log_error_errno(r, "Unmounting '%s' failed unexpectedly, couldn't wait for child process " PID_FMT ": %m", m->path, pid);
457
458 return r;
459 }
460
461 /* This includes remounting readonly, which changes the kernel mount options.
462 * Therefore the list passed to this function is invalidated, and should not be reused. */
463 static int mount_points_list_umount(MountPoint **head, bool *changed, int umount_log_level) {
464 MountPoint *m;
465 int n_failed = 0;
466
467 assert(head);
468 assert(changed);
469
470 LIST_FOREACH(mount_point, m, *head) {
471 if (m->try_remount_ro) {
472 /* We always try to remount directories
473 * read-only first, before we go on and umount
474 * them.
475 *
476 * Mount points can be stacked. If a mount
477 * point is stacked below / or /usr, we
478 * cannot umount or remount it directly,
479 * since there is no way to refer to the
480 * underlying mount. There's nothing we can do
481 * about it for the general case, but we can
482 * do something about it if it is aliased
483 * somewhere else via a bind mount. If we
484 * explicitly remount the super block of that
485 * alias read-only we hence should be
486 * relatively safe regarding keeping a dirty fs
487 * we cannot otherwise see.
488 *
489 * Since the remount can hang in the instance of
490 * remote filesystems, we remount asynchronously
491 * and skip the subsequent umount if it fails. */
492 if (remount_with_timeout(m, umount_log_level) < 0) {
493 /* Remount failed, but try unmounting anyway,
494 * unless this is a mount point we want to skip. */
495 if (nonunmountable_path(m->path)) {
496 n_failed++;
497 continue;
498 }
499 }
500 }
501
502 /* Skip / and /usr since we cannot unmount that
503 * anyway, since we are running from it. They have
504 * already been remounted ro. */
505 if (nonunmountable_path(m->path))
506 continue;
507
508 /* Trying to umount */
509 if (umount_with_timeout(m, umount_log_level) < 0)
510 n_failed++;
511 else
512 *changed = true;
513 }
514
515 return n_failed;
516 }
517
518 static int swap_points_list_off(MountPoint **head, bool *changed) {
519 MountPoint *m, *n;
520 int n_failed = 0;
521
522 assert(head);
523 assert(changed);
524
525 LIST_FOREACH_SAFE(mount_point, m, n, *head) {
526 log_info("Deactivating swap %s.", m->path);
527 if (swapoff(m->path) == 0) {
528 *changed = true;
529 mount_point_free(head, m);
530 } else {
531 log_warning_errno(errno, "Could not deactivate swap %s: %m", m->path);
532 n_failed++;
533 }
534 }
535
536 return n_failed;
537 }
538
539 static int loopback_points_list_detach(MountPoint **head, bool *changed, int umount_log_level) {
540 MountPoint *m, *n;
541 int n_failed = 0, k;
542 struct stat root_st;
543
544 assert(head);
545 assert(changed);
546
547 k = lstat("/", &root_st);
548
549 LIST_FOREACH_SAFE(mount_point, m, n, *head) {
550 int r;
551 struct stat loopback_st;
552
553 if (k >= 0 &&
554 major(root_st.st_dev) != 0 &&
555 lstat(m->path, &loopback_st) >= 0 &&
556 root_st.st_dev == loopback_st.st_rdev) {
557 n_failed++;
558 continue;
559 }
560
561 log_info("Detaching loopback %s.", m->path);
562 r = delete_loopback(m->path);
563 if (r >= 0) {
564 if (r > 0)
565 *changed = true;
566
567 mount_point_free(head, m);
568 } else {
569 log_full_errno(umount_log_level, errno, "Could not detach loopback %s: %m", m->path);
570 n_failed++;
571 }
572 }
573
574 return n_failed;
575 }
576
577 static int dm_points_list_detach(MountPoint **head, bool *changed, int umount_log_level) {
578 MountPoint *m, *n;
579 int n_failed = 0, r;
580 dev_t rootdev;
581
582 assert(head);
583 assert(changed);
584
585 r = get_block_device("/", &rootdev);
586 if (r <= 0)
587 rootdev = 0;
588
589 LIST_FOREACH_SAFE(mount_point, m, n, *head) {
590
591 if (major(rootdev) != 0 && rootdev == m->devnum) {
592 n_failed ++;
593 continue;
594 }
595
596 log_info("Detaching DM %u:%u.", major(m->devnum), minor(m->devnum));
597 r = delete_dm(m->devnum);
598 if (r >= 0) {
599 *changed = true;
600 mount_point_free(head, m);
601 } else {
602 log_full_errno(umount_log_level, errno, "Could not detach DM %s: %m", m->path);
603 n_failed++;
604 }
605 }
606
607 return n_failed;
608 }
609
610 static int umount_all_once(bool *changed, int umount_log_level) {
611 int r;
612 _cleanup_(mount_points_list_free) LIST_HEAD(MountPoint, mp_list_head);
613
614 assert(changed);
615
616 LIST_HEAD_INIT(mp_list_head);
617 r = mount_points_list_get(NULL, &mp_list_head);
618 if (r < 0)
619 return r;
620
621 return mount_points_list_umount(&mp_list_head, changed, umount_log_level);
622 }
623
624 int umount_all(bool *changed, int umount_log_level) {
625 bool umount_changed;
626 int r;
627
628 assert(changed);
629
630 /* Retry umount, until nothing can be umounted anymore. Mounts are
631 * processed in order, newest first. The retries are needed when
632 * an old mount has been moved, to a path inside a newer mount. */
633 do {
634 umount_changed = false;
635
636 r = umount_all_once(&umount_changed, umount_log_level);
637 if (umount_changed)
638 *changed = true;
639 } while (umount_changed);
640
641 return r;
642 }
643
644 int swapoff_all(bool *changed) {
645 _cleanup_(mount_points_list_free) LIST_HEAD(MountPoint, swap_list_head);
646 int r;
647
648 assert(changed);
649
650 LIST_HEAD_INIT(swap_list_head);
651
652 r = swap_list_get(NULL, &swap_list_head);
653 if (r < 0)
654 return r;
655
656 return swap_points_list_off(&swap_list_head, changed);
657 }
658
659 int loopback_detach_all(bool *changed, int umount_log_level) {
660 _cleanup_(mount_points_list_free) LIST_HEAD(MountPoint, loopback_list_head);
661 int r;
662
663 assert(changed);
664
665 LIST_HEAD_INIT(loopback_list_head);
666
667 r = loopback_list_get(&loopback_list_head);
668 if (r < 0)
669 return r;
670
671 return loopback_points_list_detach(&loopback_list_head, changed, umount_log_level);
672 }
673
674 int dm_detach_all(bool *changed, int umount_log_level) {
675 _cleanup_(mount_points_list_free) LIST_HEAD(MountPoint, dm_list_head);
676 int r;
677
678 assert(changed);
679
680 LIST_HEAD_INIT(dm_list_head);
681
682 r = dm_list_get(&dm_list_head);
683 if (r < 0)
684 return r;
685
686 return dm_points_list_detach(&dm_list_head, changed, umount_log_level);
687 }