Creating file systems: ext4 & xfs
Create, label, check and grow ext4 and xfs file systems.
By Invicly Tech, , in Linux Administration (RHCSA-style). About 11 minutes to read.
In short
A file system organises a partition or logical volume into files and directories. RHEL defaults to xfs and Ubuntu to ext4; both are journaling file systems. You create them with mkfs, identify them by UUID or label, and grow them when the underlying device grows.
Explained simply
A partition is just a long row of empty blocks. A file system is the system of shelves, labels and an index that turns those blocks into files and folders you can name, open and search.
Linux has many file systems, but on servers you mostly meet two. xfs is the default on RHEL, Rocky and Alma. ext4 is the default on Ubuntu and Debian. Both are journaling file systems: they write a short note of what they are about to change before they change it, so after a power cut the system can tidy up in seconds instead of checking the whole disk.
You create a file system with mkfs, give it a readable name with a label, and refer to it by its UUID, a long unique ID that stays the same even if the disk gets a different /dev name.
Disks fill up, so you will also measure space (df and du) and grow file systems when you give them more room. Growing is easy and usually safe while the file system is in use. Shrinking is harder, and xfs cannot shrink at all.
Press Play on a diagram to walk through it one step at a time.
- mkfs.ext4 /dev/vg/lv
- Grow: resize2fs
- Can shrink offline
- Label: e2label
- mkfs.xfs /dev/vg/lv
- Grow online: xfs_growfs /mountpoint
- Cannot shrink
- Label: xfs_admin -L
- Directory entry
- Name report.pdf
- Points to inode 1342
- Inode 1342
- Owner, group, mode
- Size and timestamps
- Link count
- Pointers to data blocks
- Data blocks
- 4 KiB each
- The actual file content
- Journal
- Records metadata changes first
- Replayed after a crash
In depth
Inodes, blocks and the journal
A file system splits space into blocks, usually 4 KiB each. Every file also has an inode: a small record that holds its owner, permissions, timestamps, size and the list of blocks with its data. The file name is not in the inode; it lives in the directory, which is just a table of names pointing to inode numbers. That is why a hard link is simply a second name for the same inode.
ext4 creates all its inodes when you run mkfs, by default one per 16 KiB of space, so millions of tiny files can use them all up while space remains. xfs allocates inodes as needed, so this is much rarer there.
The journal records metadata changes before they are applied. ext4's default mode, data=ordered, writes file data before committing the metadata that points to it, which avoids exposing stale data after a crash.
- ls -i shows inode numbers
- df -i shows inode use per file system
- stat file shows size, blocks and inode
mkfs options worth knowing
mkfs.xfs and mkfs.ext4 pick good defaults, so most of the time you only add a label: -L data. A few options change real behaviour. For ext4, -N or -i sets how many inodes you get, useful for mail spools or caches with millions of small files. -m sets the reserved space for root, 5% by default; on a 2 TB data disk that is 100 GB, so many admins lower it with tune2fs -m 1 on non-system volumes.
mkfs.xfs refuses to overwrite a device that already holds a file system unless you add -f. mkfs.ext4 asks before doing so. Treat both warnings as a last chance to check the device name.
Label limits differ: ext4 labels can be up to 16 characters, xfs labels up to 12.
- mkfs.ext4 -L logs -m 1 /dev/vdb1
- mkfs.xfs -f -L data /dev/vdb2
- tune2fs -l /dev/vdb1 shows ext4 settings
- xfs_info /mountpoint shows xfs settings
Labels and UUIDs after the fact
You can change a label later. On ext4, e2label /dev/vdb1 newname works even while it is mounted. On xfs, xfs_admin -L newname /dev/vdb2 needs the file system unmounted. Check the result with blkid or lsblk -f.
UUIDs cause a classic problem when you clone a disk or restore a VM snapshot next to its original: two file systems now share one UUID. xfs refuses to mount the second one and logs a duplicate UUID error. You can mount it once with -o nouuid, or give it a new UUID with xfs_admin -U generate on the unmounted device. For ext4 the command is tune2fs -U random. Remember to update /etc/fstab if it refers to the old UUID.
Growing, checking and repairing
Growing is a two-step job: first make the device bigger (lvextend, or a larger partition), then tell the file system. xfs_growfs takes the mount point and works only while mounted. resize2fs takes the device and can grow ext4 online. Run without a size, both fill the whole device. lvextend -r does both steps for you.
Shrinking only works for ext4, and only unmounted: e2fsck -f first, then resize2fs to the smaller size, then shrink the device. Get the order wrong and you cut off live data.
Repairs also need the file system unmounted. e2fsck -f /dev/vdb1 checks ext4, and xfs_repair /dev/vdb2 repairs xfs; xfs_repair -n checks without changing anything. The fsck.xfs program does nothing on purpose, because xfs replays its journal at mount time instead.
- Grow online: resize2fs /dev/vg/lv
- Shrink offline: e2fsck -f, then resize2fs
- Check: e2fsck -f (unmounted)
- Reserved root space: tune2fs -m
- Grow online: xfs_growfs /mountpoint
- Shrink: not possible; back up and recreate
- Check: xfs_repair -n (unmounted)
- Journal replays at mount time
Why df and du disagree
df asks the file system how many blocks are used in total. du walks the directory tree and adds up the files it can see. They often differ, and the reasons are useful to know. Files deleted while a process still has them open still use space but have no name, so du cannot find them; lsof +L1 lists them. Files hidden under a mount point (written to /data before a disk was mounted there) are invisible to du but count in df for the parent file system. Reserved root blocks on ext4 show as used in df's percentage.
du also counts real blocks by default, so a sparse file such as a VM image may show 2 GB with du but 50 GB with du --apparent-size or ls -l.
- df -hT adds the file system type column
- du -xsh /var stays on one file system
- du -h --max-depth=1 /var | sort -h finds the biggest folder
Learn it in 4 short steps
About 40m each. Track them with a timer in your plan.
- 1Create file systems with mkfs~40m
- 2Label and identify by UUID~40m
- 3Grow xfs and ext4 file systems~40m
- 4Check usage with df and du~40m
Key points
- df -h shows free space per mounted file system; df -i shows free inodes.
- du -sh dir shows how much space a directory uses.
- xfs_growfs takes the mount point; resize2fs takes the device.
- A file system can run out of inodes while df still shows free space.
Key terms
Tap a card to flip it. 0/5 known
Examples
sudo mkfs.xfs -L data /dev/vdb1
sudo mkfs.ext4 -L logs /dev/vgdata/lvlogs
sudo blkid /dev/vdb1
df -hT; df -i
sudo du -sh /var/* 2>/dev/null | sort -h | tailCreate, identify and measure file systems.
The disk is not full, but writes fail
An app that stores session files in /var/cache/app reports No space left on device, yet df -h shows 40% used. Step 1: run df -i and see the file system is at 100% inodes. Step 2: find which directory holds the most files by counting entries. Step 3: clean the old files. Step 4: for the long term, put the cache on its own ext4 volume created with more inodes, or on xfs, which allocates inodes as needed.
df -h /var; df -i /var
sudo find /var -xdev -type f | cut -d/ -f2-4 | sort | uniq -c | sort -n | tail -5
sudo find /var/cache/app -type f -mtime +7 -delete
df -i /var
# new ext4 volume with one inode per 4 KiB instead of 16 KiB:
sudo mkfs.ext4 -L appcache -i 4096 /dev/vgdata/lvcacheA cloned VM disk will not mount
You attached a copy of a VM disk to the original VM to recover a file. mount /dev/vdc1 /mnt fails, and the kernel log mentions a duplicate UUID. Step 1: confirm with blkid that vdc1 has the same UUID as a mounted file system. Step 2: for a quick one-time look, mount read-only with nouuid. Step 3: if you want to keep the copy attached, unmount it and give it a new UUID.
sudo blkid /dev/vda1 /dev/vdc1
sudo dmesg | tail -3
sudo mount -o ro,nouuid /dev/vdc1 /mnt
sudo cp /mnt/etc/app.conf /root/recovered.conf
sudo umount /mnt
sudo xfs_admin -U generate /dev/vdc1
sudo blkid /dev/vdc1Quick reference
- mkfs.xfs -L data /dev/vdb1 creates xfs; add -f to overwrite an old file system
- mkfs.ext4 -L logs /dev/vdb2 creates ext4; -i 4096 gives more inodes for many small files
- xfs_growfs /mountpoint grows mounted xfs; resize2fs /dev/device grows ext4
- df -hT shows space and type; df -i shows inodes
- e2label (ext4, max 16 chars) and xfs_admin -L (xfs, max 12 chars, unmounted) set labels
- tune2fs -m 1 /dev/vdb2 lowers ext4 reserved root space from 5% to 1%
- xfs_repair -n /dev/vdb1 checks xfs without changes; e2fsck -f checks ext4 (both unmounted)
- sudo lsof +L1 finds deleted files that still hold disk space
Quick quiz
Pick an answer to see if you're right and why.
Practice exercises
Try each one yourself first. Use the hint if you get stuck, then compare with the solution.
- Exercise 1easy
Find the type, label and UUID of every file system on your machine, and the total and free inodes of the root file system.
- Exercise 2medium
Create a 200 MiB test file, turn it into an ext4 file system through a loop device, label it test, mount it, and fill it with 10,000 empty files. Then check inode use.
- Exercise 3hard
Shrink an unmounted ext4 file system on /dev/vgdata/lvold from 2 GiB to 1 GiB safely, including the logical volume.
Check yourself
Answer in your head, reveal, then be honest with yourself.
Feel confident about this topic?
Frequently asked questions
- Should I choose xfs or ext4?
- Use your distribution's default unless you have a reason not to: xfs on RHEL family, ext4 on Ubuntu and Debian. Pick ext4 if you may need to shrink the volume later, since xfs cannot shrink.
- What does No space left on device mean if df shows free space?
- Usually the file system has run out of inodes; check df -i. It can also come from a full quota or a deleted file still held open.
- Do I need to run fsck regularly?
- No. Both ext4 and xfs recover from crashes using their journal. Run e2fsck or xfs_repair on an unmounted file system only when the kernel reports corruption.
- Can I format a disk while it is mounted?
- No, and you should never try. Unmount it first; mkfs on a mounted device is refused or leads to data loss.