Why are file systems created with mkfs
not immediately available after creation?
The following command line probably best describes what I'm seeing:
/sbin/mkfs.ext4 /dev/sdc && lsblk -o uuid --noheadings /dev/sdc
First I'm creating an ext4 filesystem on /dev/sdc
and then I'm trying to immediately show the UUID of that filesystem.
This is the corresponding output of those commands:
mke2fs 1.44.1 (24-Mar-2018)
/dev/sdc contains a ext4 file system
created on Wed Oct 5 00:40:08 2022
Proceed anyway? (y,N) y
Discarding device blocks: done
Creating filesystem with 268435456 4k blocks and 67108864 inodes
Filesystem UUID: d54169aa-faab-49b5-b0fd-fdf98b603df0
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
4096000, 7962624, 11239424, 20480000, 23887872, 71663616, 78675968,
102400000, 214990848
Allocating group tables: done
Writing inode tables: done
Creating journal (262144 blocks): done
Writing superblocks and filesystem accounting information: done
434269b5-59e4-4fd0-95f0-376090e0e0a9
You'll notice that "Filesystem UUID" from the mkfs.ext4
output does not match the output of lsblk
shown at the end of the output.
However, if I rerun lsblk -o uuid --noheadings /dev/sdc
a little bit later, I get:
d54169aa-faab-49b5-b0fd-fdf98b603df0
And that does match the mkfs.ext4
output. Why the delay?
If mkfs.ext4 returns without error, it can be used, including by file system UUID.
mount UUID=434269b5-59e4-4fd0-95f0-376090e0e0a9 /mnt
or whatever, and put it in fstab if you are happy.On Linux, some distros decided a nice hardware experience means some user space programs meddle in the process. Namely udev (hardware events system) and blkid (block device probing functions). Per man lsblk:
A user space program created a file system, and a user space program figured out the resulting UUID, but they have no direct integration. Also lsblk has a cache. Resulting in short delays. And stale data from any previous file system, note that you were re-creating over an existing ext4 without something like a wipefs command.
Mounting it directly results in a low level blkid probe same as the command
blkid --uuid d54169aa-faab-49b5-b0fd-fdf98b603df0
which by default will scan all block devices to find unknown volumes.