Skip to content

shutdown: Finalize file systems and devices after attempting exitrd - #38767

Closed
ElvishJerricco wants to merge 1 commit into
systemd:mainfrom
ElvishJerricco:push-syyrkvkssyqn
Closed

ElvishJerricco wants to merge 1 commit into
systemd:mainfrom
ElvishJerricco:push-syyrkvkssyqn

Conversation

@ElvishJerricco

Copy link
Copy Markdown
Contributor

The nonunmountable_path function attempts to avoid unmounting OS file systems like /usr during shutdown so that the system-shutdown/ executables can function. However, some "extrinsic" file systems may still be necessary here, which aren't covered by that function.

For example, if /usr is an overlayfs with a lowerdir= / upperdir= based on another file system, those other file systems should be considered extrinsic, and should also not be unmounted before /usr.

More generally, a file system is considered "extrinsic" when it is setup by the initrd, and these file systems should accordingly be torn down by the exitrd. Similar points can be made for MD / DM / loopback devices. If these resources survive past umount.target and shutdown.target, we should allow exitrd to be the one responsible for reaping them.

If there is no exitrd, fallback to the old behavior.


A little extra context: We ran into this with NixOS because our VM tests usually use an overlay for /nix/store (and we patch nonunmountable_paths to include /nix/store), and the MNT_FORCE in umount_with_timeout on /nix/.ro-store (the lowerdir) broke the overlay and prevented our system-shutdown/ scripts from functioning. But it seems logical to me in the general case that file systems setup by initrd should be torn down by exitrd, and not before the transition to exitrd.

@github-actions github-actions Bot added shutdown please-review PR is ready for (re-)review by a maintainer labels Aug 30, 2025
@yuwata

yuwata commented Sep 24, 2025

Copy link
Copy Markdown
Member

Please rebase.

@yuwata yuwata added needs-rebase and removed please-review PR is ready for (re-)review by a maintainer labels Sep 24, 2025
The `nonunmountable_path` function attempts to avoid unmounting OS
file systems like `/usr` during shutdown so that the
`system-shutdown/` executables can function. However, some "extrinsic"
file systems may still be necessary here, which aren't covered by that
function.

For example, if `/usr` is an overlayfs with a `lowerdir=` /
`upperdir=` based on another file system, those other file systems
should be considered extrinsic, and should also not be unmounted
before `/usr`.

More generally, a file system is considered "extrinsic" when it is
setup by the initrd, and these file systems should accordingly be torn
down by the exitrd. Similar points can be made for MD / DM / loopback
devices. If these resources survive past `umount.target` and
`shutdown.target`, we should allow exitrd to be the one responsible
for reaping them.

If there is no exitrd, fallback to the old behavior.
@github-actions github-actions Bot added please-review PR is ready for (re-)review by a maintainer and removed needs-rebase labels Sep 25, 2025
@ElvishJerricco

Copy link
Copy Markdown
Contributor Author

@yuwata Done

@poettering

Copy link
Copy Markdown
Member

Humpf, this doesn't seem right to me. It's a major change in behaviour to let the exitrd do everything. So far the exitrd was just the fallback to clean up the rest, not the primary thing. I am not convicned we are ready to change this all.

Why this at all? it appears to me this is a cosmetic issue, no? when we dissassmble things and cannot disassemble some dirs, then we'll give up and hand over to exitrd, which seems to be exactly what you want?

@poettering

Copy link
Copy Markdown
Member

it might make sense to add x-systemd.extrinsic or so as a userspace mount option you could set, that if set we'll skip during the umount loop.

@ElvishJerricco

Copy link
Copy Markdown
Contributor Author

when we dissassmble things and cannot disassemble some dirs, then we'll give up and hand over to exitrd, which seems to be exactly what you want?

The problem is that it can disassemble some things that it really shouldn't be. It uses MNT_FORCE to umount things, which means an overlayfs-based / or /usr is going to have its underlying dirs destroyed, which breaks the system-shutdown hooks.

it might make sense to add x-systemd.extrinsic or so as a userspace mount option you could set, that if set we'll skip during the umount loop.

Note that the term "extrinsic" is already used to describe these mounts, because we give them x-initrd.mount. And IMO that's already indication enough that it can't be torn down normally. Currently the shutdown umount loop just checks nonunmountable_path for which things it shouldn't unmount. Maybe that should be replaced with fstab_is_extrinsic?

That said, to me letting the exitrd handle things first just makes sense. The exitrd seems less like a fallback and more like a counterpart to initrd to me. It makes sense to me that, if we have an exitrd, we should let it handle anything that the initrd had set up.

@poettering

Copy link
Copy Markdown
Member

when we dissassmble things and cannot disassemble some dirs, then we'll give up and hand over to exitrd, which seems to be exactly what you want?

The problem is that it can disassemble some things that it really shouldn't be. It uses MNT_FORCE to umount things, which means an overlayfs-based / or /usr is going to have its underlying dirs destroyed, which breaks the system-shutdown hooks.

hmm, "destroyed"? didn't overlayfs pin the superblocks of its layers, so that they can otherwise be unmounted?

what do you mean by "breaks the systemd-shutdown hooks" precisely?

@poettering

Copy link
Copy Markdown
Member

That said, to me letting the exitrd handle things first just makes sense. The exitrd seems less like a fallback and more like a counterpart to initrd to me. It makes sense to me that, if we have an exitrd, we should let it handle anything that the initrd had set up.

well, our detach loop does considerably more than unmounting, i.e. killing and detaching all kinds of stuff. I am quite confident we should disassemble everything we can before letting the exitrd do the final bits.

@ElvishJerricco

Copy link
Copy Markdown
Contributor Author

didn't overlayfs pin the superblocks of its layers, so that they can otherwise be unmounted?

Huh... I was quite confident when I made this PR that there was a problem with this. I thought that unmounting with MNT_FORCE was breaking the overlay. But in fact, I can only get said broken behavior when the FS I'm unmounting is our 9p virtio FS...

If I make an overlay whose lowerdir is a 9p with trans=virtio, and unmount the 9p with MNT_FORCE, reading from the overlay yields EIO. Trying this with other file systems as the lowerdir, like ext4 or tmpfs, does not result in the same problem. I guess that means the problem is a 9p bug?

@ElvishJerricco

Copy link
Copy Markdown
Contributor Author

well, our detach loop does considerably more than unmounting, i.e. killing and detaching all kinds of stuff. I am quite confident we should disassemble everything we can before letting the exitrd do the final bits.

It still seems reasonable to me that the extrinsic mounts should be left for exitrd to unmount. So maybe we leave the loop where it is, but change which things it unmounts. Do you think it would be good to be checking against fstab_is_extrinsic instead and get rid of nonunmountable_path?

@poettering

Copy link
Copy Markdown
Member

hmm, this is all written with the thinking that file systems which are still used return EBUSY. The only reason we do nonunmountable_path() is because trying to unmount those is pointless or wrong.

but the extrinsic ones are not of that kind i would say, it just means that during normal operation they shouldn't be shut down, i.e. via .mount units. it does not say anything about the final shutdown phases.

or to say this differently: i really prefer if the file systems in question would actually be fixed to not fail on any of this and would behave like all others, i.e. return EBUSY if referenced without causing desaster.

(i'd be more open to adding a new userspace mount option that allows excluding mounts from the final umount spree, but i think that'd be a hack too, and the right fix would be to make sure your p9/overlayfs case would be fixed in the kernel)

@poettering

Copy link
Copy Markdown
Member

hmm, i wonder if this is caused by our use of MNT_FORCE, as per umount2() docs this has an effect on 9pfs..

@poettering poettering removed the please-review PR is ready for (re-)review by a maintainer label Oct 22, 2025
@poettering

Copy link
Copy Markdown
Member

hmm, i wonder if this is caused by our use of MNT_FORCE, as per umount2() docs this has an effect on 9pfs..

confirmed this with my resident kernel vfs expert.

Not sure what the right approach to deal with that is now.

Maybe something like this:

Right now our shutdown umount loop works something like this:

  1. in each cycle: try to umount each fs with MNT_FORCE
  2. in each cycle: if 1 doesn't work, and it is rw, remount it to read-only instead
  3. goto 1 as long as either 1 or 2 worked on at least one mount

Let's modify this loop so that we do step 1 without MNT_FORCE first. and then in step 3, set a flag so that the next umount() we call, we'll do with MNT_FORCE. if that worked for one umount we immediately unset that flag again, and do another run of the loop, and continue as usual. Only if we did a full loop cycle where the flag stayed on until the end (i.e. where MNT_FORCE didn't help) we'll exit the loop.

That would mean, in your overlayfs on top of 9pfs we'd always try to umount the overlayfs before issuing a destructive 9p umount request.

Does that make sense?

@daandemeyer

Copy link
Copy Markdown
Collaborator

Let's close this one, new pr with the approach suggested by @poettering would be welcome

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Development

Successfully merging this pull request may close these issues.

4 participants