1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2010 ProFUSION embedded systems
10 #include <linux/loop.h>
12 #include <sys/mount.h>
15 /* This needs to be after sys/mount.h :( */
20 #include "alloc-util.h"
21 #include "blockdev-util.h"
25 #include "fstab-util.h"
26 #include "linux-3.13/dm-ioctl.h"
27 #include "mount-setup.h"
28 #include "mount-util.h"
29 #include "path-util.h"
30 #include "process-util.h"
31 #include "signal-util.h"
32 #include "string-util.h"
33 #include "udev-util.h"
38 DEFINE_TRIVIAL_CLEANUP_FUNC(struct libmnt_table
*, mnt_free_table
);
39 DEFINE_TRIVIAL_CLEANUP_FUNC(struct libmnt_iter
*, mnt_free_iter
);
41 static void mount_point_free(MountPoint
**head
, MountPoint
*m
) {
45 LIST_REMOVE(mount_point
, *head
, m
);
48 free(m
->remount_options
);
52 void mount_points_list_free(MountPoint
**head
) {
56 mount_point_free(head
, *head
);
59 int mount_points_list_get(const char *mountinfo
, MountPoint
**head
) {
60 _cleanup_(mnt_free_tablep
) struct libmnt_table
*t
= NULL
;
61 _cleanup_(mnt_free_iterp
) struct libmnt_iter
*i
= NULL
;
67 i
= mnt_new_iter(MNT_ITER_FORWARD
);
71 r
= mnt_table_parse_mtab(t
, mountinfo
);
73 return log_error_errno(r
, "Failed to parse %s: %m", mountinfo
);
77 const char *path
, *options
, *fstype
;
78 _cleanup_free_
char *p
= NULL
;
79 unsigned long remount_flags
= 0u;
80 _cleanup_free_
char *remount_options
= NULL
;
84 r
= mnt_table_next_fs(t
, i
, &fs
);
88 return log_error_errno(r
, "Failed to get next entry from %s: %m", mountinfo
);
90 path
= mnt_fs_get_target(fs
);
94 if (cunescape(path
, UNESCAPE_RELAX
, &p
) < 0)
97 options
= mnt_fs_get_options(fs
);
98 fstype
= mnt_fs_get_fstype(fs
);
100 /* Ignore mount points we can't unmount because they
101 * are API or because we are keeping them open (like
102 * /dev/console). Also, ignore all mounts below API
103 * file systems, since they are likely virtual too,
104 * and hence not worth spending time on. Also, in
105 * unprivileged containers we might lack the rights to
106 * unmount these things, hence don't bother. */
107 if (mount_point_is_api(p
) ||
108 mount_point_ignore(p
) ||
109 path_startswith(p
, "/dev") ||
110 path_startswith(p
, "/sys") ||
111 path_startswith(p
, "/proc"))
114 /* If we are in a container, don't attempt to
115 * read-only mount anything as that brings no real
116 * benefits, but might confuse the host, as we remount
117 * the superblock here, not the bind mount.
119 * If the filesystem is a network fs, also skip the
120 * remount. It brings no value (we cannot leave
121 * a "dirty fs") and could hang if the network is down.
122 * Note that umount2() is more careful and will not
123 * hang because of the network being down. */
124 try_remount_ro
= detect_container() <= 0 &&
125 !fstype_is_network(fstype
) &&
126 !fstype_is_api_vfs(fstype
) &&
127 !fstype_is_ro(fstype
) &&
128 !fstab_test_yes_no_option(options
, "ro\0rw\0");
130 if (try_remount_ro
) {
131 /* mount(2) states that mount flags and options need to be exactly the same
132 * as they were when the filesystem was mounted, except for the desired
133 * changes. So we reconstruct both here and adjust them for the later
134 * remount call too. */
136 r
= mnt_fs_get_propagation(fs
, &remount_flags
);
138 log_warning_errno(r
, "mnt_fs_get_propagation() failed for %s, ignoring: %m", path
);
142 r
= mount_option_mangle(options
, remount_flags
, &remount_flags
, &remount_options
);
144 log_warning_errno(r
, "mount_option_mangle failed for %s, ignoring: %m", path
);
148 /* MS_BIND is special. If it is provided it will only make the mount-point
149 * read-only. If left out, the super block itself is remounted, which we want. */
150 remount_flags
= (remount_flags
|MS_REMOUNT
|MS_RDONLY
) & ~MS_BIND
;
153 m
= new0(MountPoint
, 1);
157 free_and_replace(m
->path
, p
);
158 free_and_replace(m
->remount_options
, remount_options
);
159 m
->remount_flags
= remount_flags
;
160 m
->try_remount_ro
= try_remount_ro
;
162 LIST_PREPEND(mount_point
, *head
, m
);
168 int swap_list_get(const char *swaps
, MountPoint
**head
) {
169 _cleanup_(mnt_free_tablep
) struct libmnt_table
*t
= NULL
;
170 _cleanup_(mnt_free_iterp
) struct libmnt_iter
*i
= NULL
;
176 i
= mnt_new_iter(MNT_ITER_FORWARD
);
180 r
= mnt_table_parse_swaps(t
, swaps
);
182 return log_error_errno(r
, "Failed to parse %s: %m", swaps
);
185 struct libmnt_fs
*fs
;
189 _cleanup_free_
char *d
= NULL
;
191 r
= mnt_table_next_fs(t
, i
, &fs
);
195 return log_error_errno(r
, "Failed to get next entry from %s: %m", swaps
);
197 source
= mnt_fs_get_source(fs
);
201 r
= cunescape(source
, UNESCAPE_RELAX
, &d
);
205 swap
= new0(MountPoint
, 1);
209 free_and_replace(swap
->path
, d
);
210 LIST_PREPEND(mount_point
, *head
, swap
);
216 static int loopback_list_get(MountPoint
**head
) {
217 _cleanup_(udev_enumerate_unrefp
) struct udev_enumerate
*e
= NULL
;
218 struct udev_list_entry
*item
= NULL
, *first
= NULL
;
219 _cleanup_(udev_unrefp
) struct udev
*udev
= NULL
;
228 e
= udev_enumerate_new(udev
);
232 r
= udev_enumerate_add_match_subsystem(e
, "block");
236 r
= udev_enumerate_add_match_sysname(e
, "loop*");
240 r
= udev_enumerate_add_match_sysattr(e
, "loop/backing_file", NULL
);
244 r
= udev_enumerate_scan_devices(e
);
248 first
= udev_enumerate_get_list_entry(e
);
249 udev_list_entry_foreach(item
, first
) {
250 _cleanup_(udev_device_unrefp
) struct udev_device
*d
;
252 _cleanup_free_ MountPoint
*lb
= NULL
;
254 d
= udev_device_new_from_syspath(udev
, udev_list_entry_get_name(item
));
258 dn
= udev_device_get_devnode(d
);
262 lb
= new0(MountPoint
, 1);
266 r
= free_and_strdup(&lb
->path
, dn
);
270 LIST_PREPEND(mount_point
, *head
, lb
);
277 static int dm_list_get(MountPoint
**head
) {
278 _cleanup_(udev_enumerate_unrefp
) struct udev_enumerate
*e
= NULL
;
279 struct udev_list_entry
*item
= NULL
, *first
= NULL
;
280 _cleanup_(udev_unrefp
) struct udev
*udev
= NULL
;
289 e
= udev_enumerate_new(udev
);
293 r
= udev_enumerate_add_match_subsystem(e
, "block");
297 r
= udev_enumerate_add_match_sysname(e
, "dm-*");
301 r
= udev_enumerate_scan_devices(e
);
305 first
= udev_enumerate_get_list_entry(e
);
306 udev_list_entry_foreach(item
, first
) {
307 _cleanup_(udev_device_unrefp
) struct udev_device
*d
;
310 _cleanup_free_ MountPoint
*m
= NULL
;
312 d
= udev_device_new_from_syspath(udev
, udev_list_entry_get_name(item
));
316 devnum
= udev_device_get_devnum(d
);
317 dn
= udev_device_get_devnode(d
);
318 if (major(devnum
) == 0 || !dn
)
321 m
= new0(MountPoint
, 1);
326 r
= free_and_strdup(&m
->path
, dn
);
330 LIST_PREPEND(mount_point
, *head
, m
);
337 static int delete_loopback(const char *device
) {
338 _cleanup_close_
int fd
= -1;
343 fd
= open(device
, O_RDONLY
|O_CLOEXEC
);
345 return errno
== ENOENT
? 0 : -errno
;
347 r
= ioctl(fd
, LOOP_CLR_FD
, 0);
351 /* ENXIO: not bound, so no error */
358 static int delete_dm(dev_t devnum
) {
360 struct dm_ioctl dm
= {
364 DM_VERSION_PATCHLEVEL
366 .data_size
= sizeof(dm
),
370 _cleanup_close_
int fd
= -1;
372 assert(major(devnum
) != 0);
374 fd
= open("/dev/mapper/control", O_RDWR
|O_CLOEXEC
);
378 if (ioctl(fd
, DM_DEV_REMOVE
, &dm
) < 0)
384 static bool nonunmountable_path(const char *path
) {
385 return path_equal(path
, "/")
387 || path_equal(path
, "/usr")
389 || path_startswith(path
, "/run/initramfs");
392 static int remount_with_timeout(MountPoint
*m
, int umount_log_level
) {
396 BLOCK_SIGNALS(SIGCHLD
);
400 /* Due to the possiblity of a remount operation hanging, we
401 * fork a child process and set a timeout. If the timeout
402 * lapses, the assumption is that that particular remount
404 r
= safe_fork("(sd-remount)", FORK_RESET_SIGNALS
|FORK_CLOSE_ALL_FDS
|FORK_LOG
|FORK_REOPEN_LOG
, &pid
);
408 log_info("Remounting '%s' read-only in with options '%s'.", m
->path
, m
->remount_options
);
410 /* Start the mount operation here in the child */
411 r
= mount(NULL
, m
->path
, NULL
, m
->remount_flags
, m
->remount_options
);
413 log_full_errno(umount_log_level
, errno
, "Failed to remount '%s' read-only: %m", m
->path
);
415 _exit(r
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
);
418 r
= wait_for_terminate_with_timeout(pid
, DEFAULT_TIMEOUT_USEC
);
419 if (r
== -ETIMEDOUT
) {
420 log_error_errno(r
, "Remounting '%s' timed out, issuing SIGKILL to PID " PID_FMT
".", m
->path
, pid
);
421 (void) kill(pid
, SIGKILL
);
422 } else if (r
== -EPROTO
)
423 log_debug_errno(r
, "Remounting '%s' failed abnormally, child process " PID_FMT
" aborted or exited non-zero.", m
->path
, pid
);
425 log_error_errno(r
, "Remounting '%s' failed unexpectedly, couldn't wait for child process " PID_FMT
": %m", m
->path
, pid
);
430 static int umount_with_timeout(MountPoint
*m
, int umount_log_level
) {
434 BLOCK_SIGNALS(SIGCHLD
);
438 /* Due to the possiblity of a umount operation hanging, we
439 * fork a child process and set a timeout. If the timeout
440 * lapses, the assumption is that that particular umount
442 r
= safe_fork("(sd-umount)", FORK_RESET_SIGNALS
|FORK_CLOSE_ALL_FDS
|FORK_LOG
|FORK_REOPEN_LOG
, &pid
);
446 log_info("Unmounting '%s'.", m
->path
);
448 /* Start the mount operation here in the child Using MNT_FORCE
449 * causes some filesystems (e.g. FUSE and NFS and other network
450 * filesystems) to abort any pending requests and return -EIO
451 * rather than blocking indefinitely. If the filesysten is
452 * "busy", this may allow processes to die, thus making the
453 * filesystem less busy so the unmount might succeed (rather
454 * then return EBUSY).*/
455 r
= umount2(m
->path
, MNT_FORCE
);
457 log_full_errno(umount_log_level
, errno
, "Failed to unmount %s: %m", m
->path
);
459 _exit(r
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
);
462 r
= wait_for_terminate_with_timeout(pid
, DEFAULT_TIMEOUT_USEC
);
463 if (r
== -ETIMEDOUT
) {
464 log_error_errno(r
, "Unmounting '%s' timed out, issuing SIGKILL to PID " PID_FMT
".", m
->path
, pid
);
465 (void) kill(pid
, SIGKILL
);
466 } else if (r
== -EPROTO
)
467 log_debug_errno(r
, "Unmounting '%s' failed abnormally, child process " PID_FMT
" aborted or exited non-zero.", m
->path
, pid
);
469 log_error_errno(r
, "Unmounting '%s' failed unexpectedly, couldn't wait for child process " PID_FMT
": %m", m
->path
, pid
);
474 /* This includes remounting readonly, which changes the kernel mount options.
475 * Therefore the list passed to this function is invalidated, and should not be reused. */
476 static int mount_points_list_umount(MountPoint
**head
, bool *changed
, int umount_log_level
) {
483 LIST_FOREACH(mount_point
, m
, *head
) {
484 if (m
->try_remount_ro
) {
485 /* We always try to remount directories
486 * read-only first, before we go on and umount
489 * Mount points can be stacked. If a mount
490 * point is stacked below / or /usr, we
491 * cannot umount or remount it directly,
492 * since there is no way to refer to the
493 * underlying mount. There's nothing we can do
494 * about it for the general case, but we can
495 * do something about it if it is aliased
496 * somehwere else via a bind mount. If we
497 * explicitly remount the super block of that
498 * alias read-only we hence should be
499 * relatively safe regarding keeping a dirty fs
500 * we cannot otherwise see.
502 * Since the remount can hang in the instance of
503 * remote filesystems, we remount asynchronously
504 * and skip the subsequent umount if it fails. */
505 if (remount_with_timeout(m
, umount_log_level
) < 0) {
506 /* Remount failed, but try unmounting anyway,
507 * unless this is a mount point we want to skip. */
508 if (nonunmountable_path(m
->path
)) {
515 /* Skip / and /usr since we cannot unmount that
516 * anyway, since we are running from it. They have
517 * already been remounted ro. */
518 if (nonunmountable_path(m
->path
))
521 /* Trying to umount */
522 if (umount_with_timeout(m
, umount_log_level
) < 0)
531 static int swap_points_list_off(MountPoint
**head
, bool *changed
) {
538 LIST_FOREACH_SAFE(mount_point
, m
, n
, *head
) {
539 log_info("Deactivating swap %s.", m
->path
);
540 if (swapoff(m
->path
) == 0) {
542 mount_point_free(head
, m
);
544 log_warning_errno(errno
, "Could not deactivate swap %s: %m", m
->path
);
552 static int loopback_points_list_detach(MountPoint
**head
, bool *changed
, int umount_log_level
) {
560 k
= lstat("/", &root_st
);
562 LIST_FOREACH_SAFE(mount_point
, m
, n
, *head
) {
564 struct stat loopback_st
;
567 major(root_st
.st_dev
) != 0 &&
568 lstat(m
->path
, &loopback_st
) >= 0 &&
569 root_st
.st_dev
== loopback_st
.st_rdev
) {
574 log_info("Detaching loopback %s.", m
->path
);
575 r
= delete_loopback(m
->path
);
580 mount_point_free(head
, m
);
582 log_full_errno(umount_log_level
, errno
, "Could not detach loopback %s: %m", m
->path
);
590 static int dm_points_list_detach(MountPoint
**head
, bool *changed
, int umount_log_level
) {
598 r
= get_block_device("/", &rootdev
);
602 LIST_FOREACH_SAFE(mount_point
, m
, n
, *head
) {
604 if (major(rootdev
) != 0 && rootdev
== m
->devnum
) {
609 log_info("Detaching DM %u:%u.", major(m
->devnum
), minor(m
->devnum
));
610 r
= delete_dm(m
->devnum
);
613 mount_point_free(head
, m
);
615 log_full_errno(umount_log_level
, errno
, "Could not detach DM %s: %m", m
->path
);
623 static int umount_all_once(bool *changed
, int umount_log_level
) {
625 _cleanup_(mount_points_list_free
) LIST_HEAD(MountPoint
, mp_list_head
);
629 LIST_HEAD_INIT(mp_list_head
);
630 r
= mount_points_list_get(NULL
, &mp_list_head
);
634 return mount_points_list_umount(&mp_list_head
, changed
, umount_log_level
);
637 int umount_all(bool *changed
, int umount_log_level
) {
643 /* Retry umount, until nothing can be umounted anymore. Mounts are
644 * processed in order, newest first. The retries are needed when
645 * an old mount has been moved, to a path inside a newer mount. */
647 umount_changed
= false;
649 r
= umount_all_once(&umount_changed
, umount_log_level
);
652 } while (umount_changed
);
657 int swapoff_all(bool *changed
) {
658 _cleanup_(mount_points_list_free
) LIST_HEAD(MountPoint
, swap_list_head
);
663 LIST_HEAD_INIT(swap_list_head
);
665 r
= swap_list_get(NULL
, &swap_list_head
);
669 return swap_points_list_off(&swap_list_head
, changed
);
672 int loopback_detach_all(bool *changed
, int umount_log_level
) {
673 _cleanup_(mount_points_list_free
) LIST_HEAD(MountPoint
, loopback_list_head
);
678 LIST_HEAD_INIT(loopback_list_head
);
680 r
= loopback_list_get(&loopback_list_head
);
684 return loopback_points_list_detach(&loopback_list_head
, changed
, umount_log_level
);
687 int dm_detach_all(bool *changed
, int umount_log_level
) {
688 _cleanup_(mount_points_list_free
) LIST_HEAD(MountPoint
, dm_list_head
);
693 LIST_HEAD_INIT(dm_list_head
);
695 r
= dm_list_get(&dm_list_head
);
699 return dm_points_list_detach(&dm_list_head
, changed
, umount_log_level
);