1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 Copyright © 2010 ProFUSION embedded systems
8 #include <linux/dm-ioctl.h>
9 #include <linux/loop.h>
11 #include <sys/mount.h>
14 #include <sys/types.h>
17 #include "sd-device.h"
19 #include "alloc-util.h"
20 #include "blockdev-util.h"
22 #include "device-util.h"
25 #include "fstab-util.h"
26 #include "libmount-util.h"
27 #include "mount-setup.h"
28 #include "mount-util.h"
29 #include "mountpoint-util.h"
30 #include "path-util.h"
31 #include "process-util.h"
32 #include "signal-util.h"
33 #include "string-util.h"
39 static void mount_point_free(MountPoint
**head
, MountPoint
*m
) {
43 LIST_REMOVE(mount_point
, *head
, m
);
46 free(m
->remount_options
);
50 void mount_points_list_free(MountPoint
**head
) {
54 mount_point_free(head
, *head
);
57 int mount_points_list_get(const char *mountinfo
, MountPoint
**head
) {
58 _cleanup_(mnt_free_tablep
) struct libmnt_table
*t
= NULL
;
59 _cleanup_(mnt_free_iterp
) struct libmnt_iter
*i
= NULL
;
65 i
= mnt_new_iter(MNT_ITER_FORWARD
);
69 r
= mnt_table_parse_mtab(t
, mountinfo
);
71 return log_error_errno(r
, "Failed to parse %s: %m", mountinfo
);
75 const char *path
, *fstype
;
76 _cleanup_free_
char *options
= NULL
;
77 unsigned long remount_flags
= 0u;
78 _cleanup_free_
char *remount_options
= NULL
;
80 _cleanup_free_ MountPoint
*m
= NULL
;
82 r
= mnt_table_next_fs(t
, i
, &fs
);
86 return log_error_errno(r
, "Failed to get next entry from %s: %m", mountinfo
);
88 path
= mnt_fs_get_target(fs
);
92 fstype
= mnt_fs_get_fstype(fs
);
94 /* Combine the generic VFS options with the FS-specific
95 * options. Duplicates are not a problem here, because the only
96 * options that should come up twice are typically ro/rw, which
97 * are turned into MS_RDONLY or the inversion of it.
99 * Even if there are duplicates later in mount_option_mangle()
100 * they shouldn't hurt anyways as they override each other.
102 if (!strextend_with_separator(&options
, ",",
103 mnt_fs_get_vfs_options(fs
),
106 if (!strextend_with_separator(&options
, ",",
107 mnt_fs_get_fs_options(fs
),
111 /* Ignore mount points we can't unmount because they
112 * are API or because we are keeping them open (like
113 * /dev/console). Also, ignore all mounts below API
114 * file systems, since they are likely virtual too,
115 * and hence not worth spending time on. Also, in
116 * unprivileged containers we might lack the rights to
117 * unmount these things, hence don't bother. */
118 if (mount_point_is_api(path
) ||
119 mount_point_ignore(path
) ||
120 PATH_STARTSWITH_SET(path
, "/dev", "/sys", "/proc"))
123 /* If we are in a container, don't attempt to
124 * read-only mount anything as that brings no real
125 * benefits, but might confuse the host, as we remount
126 * the superblock here, not the bind mount.
128 * If the filesystem is a network fs, also skip the
129 * remount. It brings no value (we cannot leave
130 * a "dirty fs") and could hang if the network is down.
131 * Note that umount2() is more careful and will not
132 * hang because of the network being down. */
133 try_remount_ro
= detect_container() <= 0 &&
134 !fstype_is_network(fstype
) &&
135 !fstype_is_api_vfs(fstype
) &&
136 !fstype_is_ro(fstype
) &&
137 !fstab_test_yes_no_option(options
, "ro\0rw\0");
139 if (try_remount_ro
) {
140 /* mount(2) states that mount flags and options need to be exactly the same
141 * as they were when the filesystem was mounted, except for the desired
142 * changes. So we reconstruct both here and adjust them for the later
143 * remount call too. */
145 r
= mnt_fs_get_propagation(fs
, &remount_flags
);
147 log_warning_errno(r
, "mnt_fs_get_propagation() failed for %s, ignoring: %m", path
);
151 r
= mount_option_mangle(options
, remount_flags
, &remount_flags
, &remount_options
);
153 log_warning_errno(r
, "mount_option_mangle failed for %s, ignoring: %m", path
);
157 /* MS_BIND is special. If it is provided it will only make the mount-point
158 * read-only. If left out, the super block itself is remounted, which we want. */
159 remount_flags
= (remount_flags
|MS_REMOUNT
|MS_RDONLY
) & ~MS_BIND
;
162 m
= new0(MountPoint
, 1);
166 m
->path
= strdup(path
);
170 m
->remount_options
= TAKE_PTR(remount_options
);
171 m
->remount_flags
= remount_flags
;
172 m
->try_remount_ro
= try_remount_ro
;
174 LIST_PREPEND(mount_point
, *head
, TAKE_PTR(m
));
180 int swap_list_get(const char *swaps
, MountPoint
**head
) {
181 _cleanup_(mnt_free_tablep
) struct libmnt_table
*t
= NULL
;
182 _cleanup_(mnt_free_iterp
) struct libmnt_iter
*i
= NULL
;
188 i
= mnt_new_iter(MNT_ITER_FORWARD
);
192 r
= mnt_table_parse_swaps(t
, swaps
);
194 return log_error_errno(r
, "Failed to parse %s: %m", swaps
);
197 struct libmnt_fs
*fs
;
198 _cleanup_free_ MountPoint
*swap
= NULL
;
201 r
= mnt_table_next_fs(t
, i
, &fs
);
205 return log_error_errno(r
, "Failed to get next entry from %s: %m", swaps
);
207 source
= mnt_fs_get_source(fs
);
211 swap
= new0(MountPoint
, 1);
215 swap
->path
= strdup(source
);
219 LIST_PREPEND(mount_point
, *head
, TAKE_PTR(swap
));
225 static int loopback_list_get(MountPoint
**head
) {
226 _cleanup_(sd_device_enumerator_unrefp
) sd_device_enumerator
*e
= NULL
;
232 r
= sd_device_enumerator_new(&e
);
236 r
= sd_device_enumerator_allow_uninitialized(e
);
240 r
= sd_device_enumerator_add_match_subsystem(e
, "block", true);
244 r
= sd_device_enumerator_add_match_sysname(e
, "loop*");
248 r
= sd_device_enumerator_add_match_sysattr(e
, "loop/backing_file", NULL
, true);
252 FOREACH_DEVICE(e
, d
) {
253 _cleanup_free_
char *p
= NULL
;
257 if (sd_device_get_devname(d
, &dn
) < 0)
264 lb
= new(MountPoint
, 1);
272 LIST_PREPEND(mount_point
, *head
, lb
);
278 static int dm_list_get(MountPoint
**head
) {
279 _cleanup_(sd_device_enumerator_unrefp
) sd_device_enumerator
*e
= NULL
;
285 r
= sd_device_enumerator_new(&e
);
289 r
= sd_device_enumerator_allow_uninitialized(e
);
293 r
= sd_device_enumerator_add_match_subsystem(e
, "block", true);
297 r
= sd_device_enumerator_add_match_sysname(e
, "dm-*");
301 FOREACH_DEVICE(e
, d
) {
302 _cleanup_free_
char *p
= NULL
;
307 if (sd_device_get_devnum(d
, &devnum
) < 0 ||
308 sd_device_get_devname(d
, &dn
) < 0)
315 m
= new(MountPoint
, 1);
324 LIST_PREPEND(mount_point
, *head
, m
);
330 static int delete_loopback(const char *device
) {
331 _cleanup_close_
int fd
= -1;
336 fd
= open(device
, O_RDONLY
|O_CLOEXEC
);
338 return errno
== ENOENT
? 0 : -errno
;
340 r
= ioctl(fd
, LOOP_CLR_FD
, 0);
344 /* ENXIO: not bound, so no error */
351 static int delete_dm(dev_t devnum
) {
353 struct dm_ioctl dm
= {
357 DM_VERSION_PATCHLEVEL
359 .data_size
= sizeof(dm
),
363 _cleanup_close_
int fd
= -1;
365 assert(major(devnum
) != 0);
367 fd
= open("/dev/mapper/control", O_RDWR
|O_CLOEXEC
);
371 if (ioctl(fd
, DM_DEV_REMOVE
, &dm
) < 0)
377 static bool nonunmountable_path(const char *path
) {
378 return path_equal(path
, "/")
380 || path_equal(path
, "/usr")
382 || path_startswith(path
, "/run/initramfs");
385 static int remount_with_timeout(MountPoint
*m
, int umount_log_level
) {
389 BLOCK_SIGNALS(SIGCHLD
);
393 /* Due to the possiblity of a remount operation hanging, we
394 * fork a child process and set a timeout. If the timeout
395 * lapses, the assumption is that that particular remount
397 r
= safe_fork("(sd-remount)", FORK_RESET_SIGNALS
|FORK_CLOSE_ALL_FDS
|FORK_LOG
|FORK_REOPEN_LOG
, &pid
);
401 log_info("Remounting '%s' read-only in with options '%s'.", m
->path
, m
->remount_options
);
403 /* Start the mount operation here in the child */
404 r
= mount(NULL
, m
->path
, NULL
, m
->remount_flags
, m
->remount_options
);
406 log_full_errno(umount_log_level
, errno
, "Failed to remount '%s' read-only: %m", m
->path
);
408 _exit(r
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
);
411 r
= wait_for_terminate_with_timeout(pid
, DEFAULT_TIMEOUT_USEC
);
412 if (r
== -ETIMEDOUT
) {
413 log_error_errno(r
, "Remounting '%s' timed out, issuing SIGKILL to PID " PID_FMT
".", m
->path
, pid
);
414 (void) kill(pid
, SIGKILL
);
415 } else if (r
== -EPROTO
)
416 log_debug_errno(r
, "Remounting '%s' failed abnormally, child process " PID_FMT
" aborted or exited non-zero.", m
->path
, pid
);
418 log_error_errno(r
, "Remounting '%s' failed unexpectedly, couldn't wait for child process " PID_FMT
": %m", m
->path
, pid
);
423 static int umount_with_timeout(MountPoint
*m
, int umount_log_level
) {
427 BLOCK_SIGNALS(SIGCHLD
);
431 /* Due to the possiblity of a umount operation hanging, we
432 * fork a child process and set a timeout. If the timeout
433 * lapses, the assumption is that that particular umount
435 r
= safe_fork("(sd-umount)", FORK_RESET_SIGNALS
|FORK_CLOSE_ALL_FDS
|FORK_LOG
|FORK_REOPEN_LOG
, &pid
);
439 log_info("Unmounting '%s'.", m
->path
);
441 /* Start the mount operation here in the child Using MNT_FORCE
442 * causes some filesystems (e.g. FUSE and NFS and other network
443 * filesystems) to abort any pending requests and return -EIO
444 * rather than blocking indefinitely. If the filesysten is
445 * "busy", this may allow processes to die, thus making the
446 * filesystem less busy so the unmount might succeed (rather
447 * then return EBUSY).*/
448 r
= umount2(m
->path
, MNT_FORCE
);
450 log_full_errno(umount_log_level
, errno
, "Failed to unmount %s: %m", m
->path
);
452 _exit(r
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
);
455 r
= wait_for_terminate_with_timeout(pid
, DEFAULT_TIMEOUT_USEC
);
456 if (r
== -ETIMEDOUT
) {
457 log_error_errno(r
, "Unmounting '%s' timed out, issuing SIGKILL to PID " PID_FMT
".", m
->path
, pid
);
458 (void) kill(pid
, SIGKILL
);
459 } else if (r
== -EPROTO
)
460 log_debug_errno(r
, "Unmounting '%s' failed abnormally, child process " PID_FMT
" aborted or exited non-zero.", m
->path
, pid
);
462 log_error_errno(r
, "Unmounting '%s' failed unexpectedly, couldn't wait for child process " PID_FMT
": %m", m
->path
, pid
);
467 /* This includes remounting readonly, which changes the kernel mount options.
468 * Therefore the list passed to this function is invalidated, and should not be reused. */
469 static int mount_points_list_umount(MountPoint
**head
, bool *changed
, int umount_log_level
) {
476 LIST_FOREACH(mount_point
, m
, *head
) {
477 if (m
->try_remount_ro
) {
478 /* We always try to remount directories
479 * read-only first, before we go on and umount
482 * Mount points can be stacked. If a mount
483 * point is stacked below / or /usr, we
484 * cannot umount or remount it directly,
485 * since there is no way to refer to the
486 * underlying mount. There's nothing we can do
487 * about it for the general case, but we can
488 * do something about it if it is aliased
489 * somehwere else via a bind mount. If we
490 * explicitly remount the super block of that
491 * alias read-only we hence should be
492 * relatively safe regarding keeping a dirty fs
493 * we cannot otherwise see.
495 * Since the remount can hang in the instance of
496 * remote filesystems, we remount asynchronously
497 * and skip the subsequent umount if it fails. */
498 if (remount_with_timeout(m
, umount_log_level
) < 0) {
499 /* Remount failed, but try unmounting anyway,
500 * unless this is a mount point we want to skip. */
501 if (nonunmountable_path(m
->path
)) {
508 /* Skip / and /usr since we cannot unmount that
509 * anyway, since we are running from it. They have
510 * already been remounted ro. */
511 if (nonunmountable_path(m
->path
))
514 /* Trying to umount */
515 if (umount_with_timeout(m
, umount_log_level
) < 0)
524 static int swap_points_list_off(MountPoint
**head
, bool *changed
) {
531 LIST_FOREACH_SAFE(mount_point
, m
, n
, *head
) {
532 log_info("Deactivating swap %s.", m
->path
);
533 if (swapoff(m
->path
) == 0) {
535 mount_point_free(head
, m
);
537 log_warning_errno(errno
, "Could not deactivate swap %s: %m", m
->path
);
545 static int loopback_points_list_detach(MountPoint
**head
, bool *changed
, int umount_log_level
) {
553 k
= lstat("/", &root_st
);
555 LIST_FOREACH_SAFE(mount_point
, m
, n
, *head
) {
557 struct stat loopback_st
;
560 major(root_st
.st_dev
) != 0 &&
561 lstat(m
->path
, &loopback_st
) >= 0 &&
562 root_st
.st_dev
== loopback_st
.st_rdev
) {
567 log_info("Detaching loopback %s.", m
->path
);
568 r
= delete_loopback(m
->path
);
573 mount_point_free(head
, m
);
575 log_full_errno(umount_log_level
, errno
, "Could not detach loopback %s: %m", m
->path
);
583 static int dm_points_list_detach(MountPoint
**head
, bool *changed
, int umount_log_level
) {
591 r
= get_block_device("/", &rootdev
);
595 LIST_FOREACH_SAFE(mount_point
, m
, n
, *head
) {
597 if (major(rootdev
) != 0 && rootdev
== m
->devnum
) {
602 log_info("Detaching DM %u:%u.", major(m
->devnum
), minor(m
->devnum
));
603 r
= delete_dm(m
->devnum
);
606 mount_point_free(head
, m
);
608 log_full_errno(umount_log_level
, errno
, "Could not detach DM %s: %m", m
->path
);
616 static int umount_all_once(bool *changed
, int umount_log_level
) {
618 _cleanup_(mount_points_list_free
) LIST_HEAD(MountPoint
, mp_list_head
);
622 LIST_HEAD_INIT(mp_list_head
);
623 r
= mount_points_list_get(NULL
, &mp_list_head
);
627 return mount_points_list_umount(&mp_list_head
, changed
, umount_log_level
);
630 int umount_all(bool *changed
, int umount_log_level
) {
636 /* Retry umount, until nothing can be umounted anymore. Mounts are
637 * processed in order, newest first. The retries are needed when
638 * an old mount has been moved, to a path inside a newer mount. */
640 umount_changed
= false;
642 r
= umount_all_once(&umount_changed
, umount_log_level
);
645 } while (umount_changed
);
650 int swapoff_all(bool *changed
) {
651 _cleanup_(mount_points_list_free
) LIST_HEAD(MountPoint
, swap_list_head
);
656 LIST_HEAD_INIT(swap_list_head
);
658 r
= swap_list_get(NULL
, &swap_list_head
);
662 return swap_points_list_off(&swap_list_head
, changed
);
665 int loopback_detach_all(bool *changed
, int umount_log_level
) {
666 _cleanup_(mount_points_list_free
) LIST_HEAD(MountPoint
, loopback_list_head
);
671 LIST_HEAD_INIT(loopback_list_head
);
673 r
= loopback_list_get(&loopback_list_head
);
677 return loopback_points_list_detach(&loopback_list_head
, changed
, umount_log_level
);
680 int dm_detach_all(bool *changed
, int umount_log_level
) {
681 _cleanup_(mount_points_list_free
) LIST_HEAD(MountPoint
, dm_list_head
);
686 LIST_HEAD_INIT(dm_list_head
);
688 r
= dm_list_get(&dm_list_head
);
692 return dm_points_list_detach(&dm_list_head
, changed
, umount_log_level
);