systemd-fsck can race with udev probing during journal replay
systemd version the issue has been seen with
Reproduced with systemd 255 but I also found no whole-device lock in systemd-fsck in v261.2 or current main (3b2900f0508b4875a53e412bd02fd12b80eaf215).
Used distribution
Azure Linux 3.0
Linux kernel version used
6.6.143.1
CPU architectures issue was seen on
x86_64
Component
systemd-fsck, systemd-udevd
Expected behaviour you didn't see
udev should not probe a block device while a filesystem checker is updating filesystem metadata.
Unexpected behaviour you saw
During journal replay in the initrd, systemd-fsck updated filesystem metadata while udev concurrently probed the same disk. We have seen filesystem identification temporarily disappear, causing udev to remove the /dev/disk/by-uuid/<root-uuid> link and transition its corresponding dev-disk-by\x2duuid-<escaped-root-uuid>.device unit from plugged to dead. The physical partition device and its PARTUUID aliases remained present.
That UUID-backed device unit was part of the generated root transaction. Its loss stopped or cancelled systemd-fsck-root.service, sysroot.mount, initrd-root-fs.target, and initrd-parse-etc.service. The UUID link and device unit returned shortly afterward, but the cancelled start jobs were not recreated. /sysroot therefore remained unmounted and the boot stayed in the initrd.
Steps to reproduce the problem
The repro requires an ext4 journal replay by e2fsck on the root partition to overlap a synthetic block-subsystem change uevent for that same partition. When e2fsck closes a writable descriptor for the block device, the kernel reports the close through the inotify event IN_CLOSE_WRITE. systemd-udevd watches block-device nodes for that event and responds by triggering SD_DEVICE_CHANGE; the resulting uevent is exposed to udev rules as ACTION=change. This path is implemented by the IN_CLOSE_WRITE handler and the SD_DEVICE_CHANGE trigger. The race is timing dependent. In the observed sequence:
systemd-fsckstartsfsck, ande2fsckreports that it is recovering the ext4 journal on the root partition.- During the check,
e2fsckcloses a writable descriptor for the root partition, producingIN_CLOSE_WRITEon that device node. systemd-udevdreceivesIN_CLOSE_WRITEand synthesizes anSD_DEVICE_CHANGEuevent for the root partition. In the captured event environment this isACTION=change,DEVNAME=/dev/<root-partition>, andDEVTYPE=partition;systemd-udevdstarts processing it before thefsck.ext4process exits.- The persistent-storage rules run
blkidagainst the partition while journal replay is still completing. - For that specific synthetic
ACTION=changeprocessing pass,blkidreturns noID_FS_UUID. Comparing those new properties with the existing device database,udevconcludes that the old filesystem UUID link no longer belongs to the partition and removes/dev/disk/by-uuid/<root-uuid>. - The corresponding UUID-backed device unit changes from
pluggedtodead, cancelling the generated root mount transaction.
Additional program output to the terminal or log subsystem illustrating the issue
Sanitized excerpt from one reproduced boot. The udev change starts while fsck.ext4 is still running, the UUID link is removed, and the root mount then fails:
[ 2.036215] systemd-fsck[431]: /dev/<root-partition>: recovering journal
[ 2.788153] systemd-udevd[380]: <root-partition>: processing device (ACTION=change)
[ 2.801198] systemd-udevd[380]: <root-partition>: Removing/updating old device symlink '/dev/disk/by-uuid/<root-uuid>', which is no longer belonging to this device.
[ 2.801732] systemd-udevd[380]: <root-partition>: No reference left for '/dev/disk/by-uuid/<root-uuid>', removing
[ 2.805147] fsck.ext4[431]: sched_process_exit
[ 2.833393] mount[435]: mount: /sysroot: fsconfig system call failed: /dev/disk/by-uuid/<root-uuid>: Can't lookup blockdev.A separate reproduced boot captured the udev change environment without ID_FS_UUID; its DEVLINKS retained the partition UUID and label but not the filesystem UUID. The root mount failed against the missing UUID link:
ACTION=change
DEVNAME=/dev/<root-partition>
DEVTYPE=partition
DEVLINKS=/dev/disk/by-partuuid/<root-partuuid> /dev/disk/by-partlabel/rootfs ...
[ 5.762142] mount[456]: mount: /sysroot: fsconfig system call failed: /dev/disk/by-uuid/<root-uuid>: Can't lookup blockdev.Another debug reproduction recorded the resulting unit transition and cancellation cascade. The UUID-backed device unit returned, but the cancelled root transaction did not:
[ 5.929688] dev-disk-by\x2duuid-<escaped-root-uuid>.device: Changed plugged -> dead
initrd-parse-etc.service: Job initrd-parse-etc.service/start finished, result=canceled
initrd-root-fs.target: Job initrd-root-fs.target/start finished, result=canceled
systemd-fsck-root.service: Job systemd-fsck-root.service/start finished, result=canceled
sysroot.mount: Job sysroot.mount/start finished, result=canceled
[ 6.086570] dev-disk-by\x2duuid-<escaped-root-uuid>.device: Changed dead -> pluggedHistorical context
fsck -loriginally locked the whole device.udevskipped events for locked devices, which could leave device links stale: freedesktop.org #79576.- util-linux changed
fsck -lto lock/run/fsck/<disk>.lock, limiting synchronization tofsckinstances: util-linux 3bbdae633f4a. - systemd later changed
udevto requeue events when another process holds the whole-device lock: systemd 5d354e525a5. - The current block-device locking protocol recommends
LOCK_EXfor tools such asfsck, whileudevtakesLOCK_SH|LOCK_NB. - A whole-device lock in
e2fsckis currently under review in the e2fsprogs thread. There's an ask in ddstreet's summary whether locking shouldn't belong infsckorsystemd-fsckinstead.
Change proposal in systemd-fsck
Draft implementation: https://github.com/systemd/systemd/pull/43696
The draft PR uses systemd's existing whole-disk lock helper, takes a blocking exclusive lock before starting a repair-capable checker, and releases it after the child exits.
Applying the protocol at that layer covers all filesystem-specific checkers invoked through the fsck dispatcher and keeps the systemd/udev coordination in one place. I have validated this proposal locally, but do not want to preempt the ongoing e2fsprogs work or assume that this is the preferred ownership model.
Guidance requested:
- What is your opinion about
systemd-fsckowning this lock centrally? - Should lock acquisition use the
fsckunits' infinite timeout or a separate bounded timeout?
Source: systemd/systemd