Skip to content

Commit

Permalink
umount: unmount profcs/sysfs/.. lazily
Browse files Browse the repository at this point in the history
Alternative for: df48b43
  • Loading branch information
poettering committed Aug 31, 2022
1 parent c905aaf commit f11efcb
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 15 deletions.
25 changes: 11 additions & 14 deletions src/shutdown/umount.c
Expand Up @@ -80,7 +80,7 @@ int mount_points_list_get(const char *mountinfo, MountPoint **head) {
struct libmnt_fs *fs;
const char *path, *fstype;
unsigned long remount_flags = 0u;
bool try_remount_ro;
bool try_remount_ro, is_api_vfs;
_cleanup_free_ MountPoint *m = NULL;

r = mnt_table_next_fs(table, iter, &fs);
Expand Down Expand Up @@ -115,6 +115,8 @@ int mount_points_list_get(const char *mountinfo, MountPoint **head) {
PATH_STARTSWITH_SET(path, "/dev", "/sys", "/proc"))
continue;

is_api_vfs = fstype_is_api_vfs(fstype);

/* If we are in a container, don't attempt to read-only mount anything as that brings no real
* benefits, but might confuse the host, as we remount the superblock here, not the bind
* mount.
Expand All @@ -124,7 +126,7 @@ int mount_points_list_get(const char *mountinfo, MountPoint **head) {
* careful and will not hang because of the network being down. */
try_remount_ro = detect_container() <= 0 &&
!fstype_is_network(fstype) &&
!fstype_is_api_vfs(fstype) &&
!is_api_vfs &&
!fstype_is_ro(fstype) &&
!fstab_test_yes_no_option(options, "ro\0rw\0");

Expand Down Expand Up @@ -158,6 +160,10 @@ int mount_points_list_get(const char *mountinfo, MountPoint **head) {
.remount_options = remount_options,
.remount_flags = remount_flags,
.try_remount_ro = try_remount_ro,

/* Unmount sysfs/procfs/… lazily, since syncing doesn't matter there, and it's OK if
* something keeps an fd open to it. */
.umount_lazily = is_api_vfs,
};

m->path = strdup(path);
Expand Down Expand Up @@ -634,23 +640,14 @@ static int umount_with_timeout(MountPoint *m, bool last_try) {
* -EIO rather than blocking indefinitely. If the filesysten is "busy", this may allow
* processes to die, thus making the filesystem less busy so the unmount might succeed
* (rather than return EBUSY). */
r = RET_NERRNO(umount2(m->path, MNT_FORCE));
r = RET_NERRNO(umount2(m->path,
UMOUNT_NOFOLLOW | /* Don't follow symlinks: this should never happen unless our mount list was wrong */
(m->umount_lazily ? MNT_DETACH : MNT_FORCE)));
if (r < 0) {
log_full_errno(last_try ? LOG_ERR : LOG_INFO, r, "Failed to unmount %s: %m", m->path);

if (r == -EBUSY && last_try)
log_umount_blockers(m->path);

/* If API filesystems under /oldroot cannot be unmounted we can still lazily unmount
* them to unblock /oldroot. They serve no function to us anymore and should be
* memory-only and hence safe to unmount like this. */
if (in_initrd() &&
PATH_STARTSWITH_SET(m->path, "/oldroot/dev", "/oldroot/proc", "/oldroot/sys")) {
log_info("Lazily unmounting '%s' instead.", m->path);
r = umount2(m->path, MNT_FORCE | MNT_DETACH);
if (r < 0)
log_error_errno(errno, "Failed to lazily unmount %s: %m", m->path);
}
}

_exit(r < 0 ? EXIT_FAILURE : EXIT_SUCCESS);
Expand Down
3 changes: 2 additions & 1 deletion src/shutdown/umount.h
Expand Up @@ -18,7 +18,8 @@ typedef struct MountPoint {
char *path;
char *remount_options;
unsigned long remount_flags;
bool try_remount_ro;
bool try_remount_ro:1;
bool umount_lazily:1;
dev_t devnum;
LIST_FIELDS(struct MountPoint, mount_point);
} MountPoint;
Expand Down

0 comments on commit f11efcb

Please sign in to comment.