1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 Copyright © 2010 ProFUSION embedded systems
8 #include <linux/loop.h>
10 #include <sys/mount.h>
13 /* This needs to be after sys/mount.h :( */
16 #include "sd-device.h"
18 #include "alloc-util.h"
19 #include "blockdev-util.h"
21 #include "device-util.h"
24 #include "fstab-util.h"
25 #include "linux-3.13/dm-ioctl.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"
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
, *fstype
;
78 _cleanup_free_
char *options
= NULL
;
79 _cleanup_free_
char *p
= NULL
;
80 unsigned long remount_flags
= 0u;
81 _cleanup_free_
char *remount_options
= NULL
;
85 r
= mnt_table_next_fs(t
, i
, &fs
);
89 return log_error_errno(r
, "Failed to get next entry from %s: %m", mountinfo
);
91 path
= mnt_fs_get_target(fs
);
95 if (cunescape(path
, UNESCAPE_RELAX
, &p
) < 0)
98 fstype
= mnt_fs_get_fstype(fs
);
100 /* Combine the generic VFS options with the FS-specific
101 * options. Duplicates are not a problem here, because the only
102 * options that should come up twice are typically ro/rw, which
103 * are turned into MS_RDONLY or the invertion of it.
105 * Even if there are duplicates later in mount_option_mangle()
106 * it shouldn't hurt anyways as they override each other.
108 if (!strextend_with_separator(&options
, ",",
109 mnt_fs_get_vfs_options(fs
),
112 if (!strextend_with_separator(&options
, ",",
113 mnt_fs_get_fs_options(fs
),
117 /* Ignore mount points we can't unmount because they
118 * are API or because we are keeping them open (like
119 * /dev/console). Also, ignore all mounts below API
120 * file systems, since they are likely virtual too,
121 * and hence not worth spending time on. Also, in
122 * unprivileged containers we might lack the rights to
123 * unmount these things, hence don't bother. */
124 if (mount_point_is_api(p
) ||
125 mount_point_ignore(p
) ||
126 PATH_STARTSWITH_SET(p
, "/dev", "/sys", "/proc"))
129 /* If we are in a container, don't attempt to
130 * read-only mount anything as that brings no real
131 * benefits, but might confuse the host, as we remount
132 * the superblock here, not the bind mount.
134 * If the filesystem is a network fs, also skip the
135 * remount. It brings no value (we cannot leave
136 * a "dirty fs") and could hang if the network is down.
137 * Note that umount2() is more careful and will not
138 * hang because of the network being down. */
139 try_remount_ro
= detect_container() <= 0 &&
140 !fstype_is_network(fstype
) &&
141 !fstype_is_api_vfs(fstype
) &&
142 !fstype_is_ro(fstype
) &&
143 !fstab_test_yes_no_option(options
, "ro\0rw\0");
145 if (try_remount_ro
) {
146 /* mount(2) states that mount flags and options need to be exactly the same
147 * as they were when the filesystem was mounted, except for the desired
148 * changes. So we reconstruct both here and adjust them for the later
149 * remount call too. */
151 r
= mnt_fs_get_propagation(fs
, &remount_flags
);
153 log_warning_errno(r
, "mnt_fs_get_propagation() failed for %s, ignoring: %m", path
);
157 r
= mount_option_mangle(options
, remount_flags
, &remount_flags
, &remount_options
);
159 log_warning_errno(r
, "mount_option_mangle failed for %s, ignoring: %m", path
);
163 /* MS_BIND is special. If it is provided it will only make the mount-point
164 * read-only. If left out, the super block itself is remounted, which we want. */
165 remount_flags
= (remount_flags
|MS_REMOUNT
|MS_RDONLY
) & ~MS_BIND
;
168 m
= new0(MountPoint
, 1);
172 free_and_replace(m
->path
, p
);
173 free_and_replace(m
->remount_options
, remount_options
);
174 m
->remount_flags
= remount_flags
;
175 m
->try_remount_ro
= try_remount_ro
;
177 LIST_PREPEND(mount_point
, *head
, m
);
183 int swap_list_get(const char *swaps
, MountPoint
**head
) {
184 _cleanup_(mnt_free_tablep
) struct libmnt_table
*t
= NULL
;
185 _cleanup_(mnt_free_iterp
) struct libmnt_iter
*i
= NULL
;
191 i
= mnt_new_iter(MNT_ITER_FORWARD
);
195 r
= mnt_table_parse_swaps(t
, swaps
);
197 return log_error_errno(r
, "Failed to parse %s: %m", swaps
);
200 struct libmnt_fs
*fs
;
204 _cleanup_free_
char *d
= NULL
;
206 r
= mnt_table_next_fs(t
, i
, &fs
);
210 return log_error_errno(r
, "Failed to get next entry from %s: %m", swaps
);
212 source
= mnt_fs_get_source(fs
);
216 r
= cunescape(source
, UNESCAPE_RELAX
, &d
);
220 swap
= new0(MountPoint
, 1);
224 free_and_replace(swap
->path
, d
);
225 LIST_PREPEND(mount_point
, *head
, swap
);
231 static int loopback_list_get(MountPoint
**head
) {
232 _cleanup_(sd_device_enumerator_unrefp
) sd_device_enumerator
*e
= NULL
;
238 r
= sd_device_enumerator_new(&e
);
242 r
= sd_device_enumerator_allow_uninitialized(e
);
246 r
= sd_device_enumerator_add_match_subsystem(e
, "block", true);
250 r
= sd_device_enumerator_add_match_sysname(e
, "loop*");
254 r
= sd_device_enumerator_add_match_sysattr(e
, "loop/backing_file", NULL
, true);
258 FOREACH_DEVICE(e
, d
) {
259 _cleanup_free_
char *p
= NULL
;
263 if (sd_device_get_devname(d
, &dn
) < 0)
270 lb
= new(MountPoint
, 1);
278 LIST_PREPEND(mount_point
, *head
, lb
);
284 static int dm_list_get(MountPoint
**head
) {
285 _cleanup_(sd_device_enumerator_unrefp
) sd_device_enumerator
*e
= NULL
;
291 r
= sd_device_enumerator_new(&e
);
295 r
= sd_device_enumerator_allow_uninitialized(e
);
299 r
= sd_device_enumerator_add_match_subsystem(e
, "block", true);
303 r
= sd_device_enumerator_add_match_sysname(e
, "dm-*");
307 FOREACH_DEVICE(e
, d
) {
308 _cleanup_free_
char *p
= NULL
;
313 if (sd_device_get_devnum(d
, &devnum
) < 0 ||
314 sd_device_get_devname(d
, &dn
) < 0)
321 m
= new(MountPoint
, 1);
330 LIST_PREPEND(mount_point
, *head
, m
);
336 static int delete_loopback(const char *device
) {
337 _cleanup_close_
int fd
= -1;
342 fd
= open(device
, O_RDONLY
|O_CLOEXEC
);
344 return errno
== ENOENT
? 0 : -errno
;
346 r
= ioctl(fd
, LOOP_CLR_FD
, 0);
350 /* ENXIO: not bound, so no error */
357 static int delete_dm(dev_t devnum
) {
359 struct dm_ioctl dm
= {
363 DM_VERSION_PATCHLEVEL
365 .data_size
= sizeof(dm
),
369 _cleanup_close_
int fd
= -1;
371 assert(major(devnum
) != 0);
373 fd
= open("/dev/mapper/control", O_RDWR
|O_CLOEXEC
);
377 if (ioctl(fd
, DM_DEV_REMOVE
, &dm
) < 0)
383 static bool nonunmountable_path(const char *path
) {
384 return path_equal(path
, "/")
386 || path_equal(path
, "/usr")
388 || path_startswith(path
, "/run/initramfs");
391 static int remount_with_timeout(MountPoint
*m
, int umount_log_level
) {
395 BLOCK_SIGNALS(SIGCHLD
);
399 /* Due to the possiblity of a remount operation hanging, we
400 * fork a child process and set a timeout. If the timeout
401 * lapses, the assumption is that that particular remount
403 r
= safe_fork("(sd-remount)", FORK_RESET_SIGNALS
|FORK_CLOSE_ALL_FDS
|FORK_LOG
|FORK_REOPEN_LOG
, &pid
);
407 log_info("Remounting '%s' read-only in with options '%s'.", m
->path
, m
->remount_options
);
409 /* Start the mount operation here in the child */
410 r
= mount(NULL
, m
->path
, NULL
, m
->remount_flags
, m
->remount_options
);
412 log_full_errno(umount_log_level
, errno
, "Failed to remount '%s' read-only: %m", m
->path
);
414 _exit(r
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
);
417 r
= wait_for_terminate_with_timeout(pid
, DEFAULT_TIMEOUT_USEC
);
418 if (r
== -ETIMEDOUT
) {
419 log_error_errno(r
, "Remounting '%s' timed out, issuing SIGKILL to PID " PID_FMT
".", m
->path
, pid
);
420 (void) kill(pid
, SIGKILL
);
421 } else if (r
== -EPROTO
)
422 log_debug_errno(r
, "Remounting '%s' failed abnormally, child process " PID_FMT
" aborted or exited non-zero.", m
->path
, pid
);
424 log_error_errno(r
, "Remounting '%s' failed unexpectedly, couldn't wait for child process " PID_FMT
": %m", m
->path
, pid
);
429 static int umount_with_timeout(MountPoint
*m
, int umount_log_level
) {
433 BLOCK_SIGNALS(SIGCHLD
);
437 /* Due to the possiblity of a umount operation hanging, we
438 * fork a child process and set a timeout. If the timeout
439 * lapses, the assumption is that that particular umount
441 r
= safe_fork("(sd-umount)", FORK_RESET_SIGNALS
|FORK_CLOSE_ALL_FDS
|FORK_LOG
|FORK_REOPEN_LOG
, &pid
);
445 log_info("Unmounting '%s'.", m
->path
);
447 /* Start the mount operation here in the child Using MNT_FORCE
448 * causes some filesystems (e.g. FUSE and NFS and other network
449 * filesystems) to abort any pending requests and return -EIO
450 * rather than blocking indefinitely. If the filesysten is
451 * "busy", this may allow processes to die, thus making the
452 * filesystem less busy so the unmount might succeed (rather
453 * then return EBUSY).*/
454 r
= umount2(m
->path
, MNT_FORCE
);
456 log_full_errno(umount_log_level
, errno
, "Failed to unmount %s: %m", m
->path
);
458 _exit(r
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
);
461 r
= wait_for_terminate_with_timeout(pid
, DEFAULT_TIMEOUT_USEC
);
462 if (r
== -ETIMEDOUT
) {
463 log_error_errno(r
, "Unmounting '%s' timed out, issuing SIGKILL to PID " PID_FMT
".", m
->path
, pid
);
464 (void) kill(pid
, SIGKILL
);
465 } else if (r
== -EPROTO
)
466 log_debug_errno(r
, "Unmounting '%s' failed abnormally, child process " PID_FMT
" aborted or exited non-zero.", m
->path
, pid
);
468 log_error_errno(r
, "Unmounting '%s' failed unexpectedly, couldn't wait for child process " PID_FMT
": %m", m
->path
, pid
);
473 /* This includes remounting readonly, which changes the kernel mount options.
474 * Therefore the list passed to this function is invalidated, and should not be reused. */
475 static int mount_points_list_umount(MountPoint
**head
, bool *changed
, int umount_log_level
) {
482 LIST_FOREACH(mount_point
, m
, *head
) {
483 if (m
->try_remount_ro
) {
484 /* We always try to remount directories
485 * read-only first, before we go on and umount
488 * Mount points can be stacked. If a mount
489 * point is stacked below / or /usr, we
490 * cannot umount or remount it directly,
491 * since there is no way to refer to the
492 * underlying mount. There's nothing we can do
493 * about it for the general case, but we can
494 * do something about it if it is aliased
495 * somehwere else via a bind mount. If we
496 * explicitly remount the super block of that
497 * alias read-only we hence should be
498 * relatively safe regarding keeping a dirty fs
499 * we cannot otherwise see.
501 * Since the remount can hang in the instance of
502 * remote filesystems, we remount asynchronously
503 * and skip the subsequent umount if it fails. */
504 if (remount_with_timeout(m
, umount_log_level
) < 0) {
505 /* Remount failed, but try unmounting anyway,
506 * unless this is a mount point we want to skip. */
507 if (nonunmountable_path(m
->path
)) {
514 /* Skip / and /usr since we cannot unmount that
515 * anyway, since we are running from it. They have
516 * already been remounted ro. */
517 if (nonunmountable_path(m
->path
))
520 /* Trying to umount */
521 if (umount_with_timeout(m
, umount_log_level
) < 0)
530 static int swap_points_list_off(MountPoint
**head
, bool *changed
) {
537 LIST_FOREACH_SAFE(mount_point
, m
, n
, *head
) {
538 log_info("Deactivating swap %s.", m
->path
);
539 if (swapoff(m
->path
) == 0) {
541 mount_point_free(head
, m
);
543 log_warning_errno(errno
, "Could not deactivate swap %s: %m", m
->path
);
551 static int loopback_points_list_detach(MountPoint
**head
, bool *changed
, int umount_log_level
) {
559 k
= lstat("/", &root_st
);
561 LIST_FOREACH_SAFE(mount_point
, m
, n
, *head
) {
563 struct stat loopback_st
;
566 major(root_st
.st_dev
) != 0 &&
567 lstat(m
->path
, &loopback_st
) >= 0 &&
568 root_st
.st_dev
== loopback_st
.st_rdev
) {
573 log_info("Detaching loopback %s.", m
->path
);
574 r
= delete_loopback(m
->path
);
579 mount_point_free(head
, m
);
581 log_full_errno(umount_log_level
, errno
, "Could not detach loopback %s: %m", m
->path
);
589 static int dm_points_list_detach(MountPoint
**head
, bool *changed
, int umount_log_level
) {
597 r
= get_block_device("/", &rootdev
);
601 LIST_FOREACH_SAFE(mount_point
, m
, n
, *head
) {
603 if (major(rootdev
) != 0 && rootdev
== m
->devnum
) {
608 log_info("Detaching DM %u:%u.", major(m
->devnum
), minor(m
->devnum
));
609 r
= delete_dm(m
->devnum
);
612 mount_point_free(head
, m
);
614 log_full_errno(umount_log_level
, errno
, "Could not detach DM %s: %m", m
->path
);
622 static int umount_all_once(bool *changed
, int umount_log_level
) {
624 _cleanup_(mount_points_list_free
) LIST_HEAD(MountPoint
, mp_list_head
);
628 LIST_HEAD_INIT(mp_list_head
);
629 r
= mount_points_list_get(NULL
, &mp_list_head
);
633 return mount_points_list_umount(&mp_list_head
, changed
, umount_log_level
);
636 int umount_all(bool *changed
, int umount_log_level
) {
642 /* Retry umount, until nothing can be umounted anymore. Mounts are
643 * processed in order, newest first. The retries are needed when
644 * an old mount has been moved, to a path inside a newer mount. */
646 umount_changed
= false;
648 r
= umount_all_once(&umount_changed
, umount_log_level
);
651 } while (umount_changed
);
656 int swapoff_all(bool *changed
) {
657 _cleanup_(mount_points_list_free
) LIST_HEAD(MountPoint
, swap_list_head
);
662 LIST_HEAD_INIT(swap_list_head
);
664 r
= swap_list_get(NULL
, &swap_list_head
);
668 return swap_points_list_off(&swap_list_head
, changed
);
671 int loopback_detach_all(bool *changed
, int umount_log_level
) {
672 _cleanup_(mount_points_list_free
) LIST_HEAD(MountPoint
, loopback_list_head
);
677 LIST_HEAD_INIT(loopback_list_head
);
679 r
= loopback_list_get(&loopback_list_head
);
683 return loopback_points_list_detach(&loopback_list_head
, changed
, umount_log_level
);
686 int dm_detach_all(bool *changed
, int umount_log_level
) {
687 _cleanup_(mount_points_list_free
) LIST_HEAD(MountPoint
, dm_list_head
);
692 LIST_HEAD_INIT(dm_list_head
);
694 r
= dm_list_get(&dm_list_head
);
698 return dm_points_list_detach(&dm_list_head
, changed
, umount_log_level
);