Skip to content
systemcoding.dev
Kernels

Build and Inspect an ext4 Image With mkfs.ext4, dumpe2fs and debugfs

Create an ext4 filesystem inside a plain file, read its superblock with dumpe2fs, and walk inodes and directories with debugfs without mounting anything.

6 min read
Summarize in AI
Flat illustration of user space above the system call boundary, the kernel and the hardware, with the Linux, FreeBSD and GNU logos.

You do not need a spare disk to study ext4. A regular file formatted with mkfs.ext4 is a complete filesystem, and the e2fsprogs tools can read its superblock, inode table, journal and directory blocks without ever mounting it. That makes it a safe way to learn what the kernel's ext4 driver reads and writes, and a handy test bed before you touch real storage. This post builds a 64 MiB image, inspects it with dumpe2fs, then uses debugfs to list the root directory and examine an inode. It uses e2fsprogs 1.47 on Linux 6.12, and all UUIDs, times and block numbers are examples that will differ on your machine.

Create the image

An image is a sparse file. truncate sets its length without writing data, and mkfs.ext4 lays the structures inside it. The -F flag is required because the target is not a block device, and -L sets a label:

Shell
truncate -s 64M lab.img
mkfs.ext4 -F -L labdisk lab.img
Text
mke2fs 1.47.0 (5-Feb-2023)
Discarding device blocks: done
Creating filesystem with 65536 1k blocks and 16384 inodes

Allocating group tables: done
Writing inode tables: done
Creating journal (4096 blocks): done
Writing superblocks and filesystem accounting information: done

Two numbers matter. For a filesystem this small, mke2fs picks a 1 KiB block size and one inode per 4 KiB of space, which is where 16384 inodes comes from. Larger filesystems use 4 KiB blocks and fewer inodes per byte. Both choices come from /etc/mke2fs.conf, and you can override them with -b for block size and -N for inode count. The 4096-block journal is 4 MiB, sized by mke2fs from the filesystem size.

Read the superblock with dumpe2fs

dumpe2fs -h prints only the superblock, which is the filesystem's own description of itself:

Shell
dumpe2fs -h lab.img
Text
Filesystem volume name:   labdisk
Last mounted on:          <not available>
Filesystem UUID:          5f3c2d1e-7a44-4c52-9b0d-3e1f6a8c2b17
Filesystem magic number:  0xEF53
Filesystem features:      has_journal ext_attr resize_inode dir_index filetype extent 64bit flex_bg sparse_super large_file huge_file dir_nlink extra_isize metadata_csum
Inode count:              16384
Block count:              65536
Free blocks:              56800
Free inodes:              16373
Block size:               1024
Blocks per group:         8192
Inodes per group:         2048
Journal inode:            8

The magic number 0xEF53 is how the kernel recognizes ext2, ext3 and ext4. The features line is the interesting one. extent means files map data through extent trees instead of block lists, metadata_csum means metadata blocks carry checksums, and has_journal means the jbd2 journal is present. The feature list on your system depends on e2fsprogs defaults. With 8192 blocks per group, 64 MiB gives eight groups, which you can list by dropping the -h:

Shell
dumpe2fs lab.img 2>/dev/null | grep -A6 '^Group 0:'
Text
Group 0: (Blocks 1-8192) csum 0x3a91 [ITABLE_ZEROED]
  Primary superblock at 1, Group descriptors at 2-2
  Reserved GDT blocks at 3-258
  Block bitmap at 259 (+258), csum 0x00000000
  Inode bitmap at 275 (+274), csum 0x00000000
  Inode table at 291-546 (+290)
  7627 free blocks, 2037 free inodes, 2 directories

Each block group has its own block bitmap, inode bitmap and slice of the inode table. Spreading these across the disk is what lets ext4 keep a file's metadata near its data, and flex_bg packs the bitmaps of several groups together for faster scanning.

Walk the filesystem with debugfs

debugfs is an interactive filesystem debugger that opens the image directly. By default it is read-only, which is the right default for exploration. Run a single command with -R:

Shell
debugfs -R "ls -l /" lab.img
Text
      2   40755 (2)      0      0    1024 23-Nov-2026 10:30 .
      2   40755 (2)      0      0    1024 23-Nov-2026 10:30 ..
     11   40700 (2)      0      0   12288 23-Nov-2026 10:30 lost+found

Inode 2 is always the root directory in ext4, and lost+found is inode 11, the first inode available to ordinary files. Inodes 1 to 10 are reserved: bad blocks, root, the user quota files, the boot loader, the undelete directory, the resize inode, and the journal at inode 8, matching the Journal inode line from dumpe2fs.

Print the root inode's contents with stat, using the <2> notation for an inode number:

Shell
debugfs -R "stat <2>" lab.img
Text
Inode: 2   Type: directory    Mode:  0755   Flags: 0x80000
Generation: 0    Version: 0x00000000:00000002
User:     0   Group:     0   Project:     0   Size: 1024
File ACL: 0
Links: 3   Blockcount: 2
Fragment:  Address: 0    Number: 0    Size: 0
 ctime: 0x6561b3c8:00000000 -- Thu Nov 23 10:30:00 2026
EXTENTS:
(0):1311

The Links: 3 count is . in the root, .. which also points at the root, and lost+found's .. entry. The EXTENTS line says logical block 0 of the directory lives at physical block 1311. You can dump that block to see the raw directory entries:

Shell
debugfs -R "bd 1311" lab.img | head -4
Text
0000  0200 0000 0c00 0102 2e00 0000 0200 0000  ................
0020  0c00 0202 2e2e 0000 0b00 0000 e803 0a02  ................
0040  6c6f 7374 2b66 6f75 6e64 0000 0000 0000  lost+found......

Each entry is an inode number, a record length, a name length, a file type and the name. The first entry is inode 2, record length 12, name length 1, type 2 (directory) and the name .. That is the directory table the previous posts described, in the flesh.

Put a file in and find its blocks

debugfs can write if you open the image with -w. A safer route is to mount the image, add a file, unmount, and inspect it afterwards. That needs root:

Shell
sudo mount -o loop lab.img /mnt
echo "hello ext4" | sudo tee /mnt/greeting.txt
sudo umount /mnt
debugfs -R "stat /greeting.txt" lab.img | tail -3
Text
 ctime: 0x6561b3f2:2a1b7c40 -- Thu Nov 23 10:30:42 2026
 crtime: 0x6561b3f2:2a1b7c40 -- Thu Nov 23 10:30:42 2026
EXTENTS:
(0):8705

The file's eleven bytes sit in a single block, physical block 8705, referenced from the inode by an extent. debugfs -R "bd 8705" would show the text. The crtime field is the creation time that ext4 stores and stat prints as Birth.

Check and shrink the image

Two more tools complete the set. e2fsck -fn lab.img checks the filesystem read-only and reports problems without changing anything, which is the safe way to see whether an image is consistent. resize2fs lab.img 32M shrinks the filesystem after a check, though you also need truncate to shrink the file itself.

Look at the journal and tune the image

The journal is just inode 8, and debugfs can show what it holds. On a freshly formatted image it is empty, so logdump prints a short summary:

Shell
debugfs -R "logdump" lab.img
Text
Journal starts at block 0, transaction 1

After you mount the image, write files and unmount cleanly, the journal is checkpointed and empty again. If the system crashes mid-write, logdump lists the committed transactions that the kernel will replay at the next mount. That replay is what keeps metadata consistent, and the next post in this cluster explains how jbd2 produces it.

Some settings can change after creation with tune2fs. For example, tune2fs -l lab.img lists the same superblock fields as dumpe2fs -h, and tune2fs -o journal_data_writeback lab.img sets the default journal mode used at mount time. You can also read the mount count and the maximum with the same tool, which matters on older images where a forced check is scheduled. None of these change the inode count or block size, which are fixed at format time.

A useful habit is to keep a pristine copy of the image and work on a copy. Because the image is a file, cp --sparse=always lab.img work.img takes a second and keeps the copy small, and you can destroy it in an experiment without caring. Compare two states with cmp -l to see exactly which bytes a kernel operation changed. That is a good way to learn, for instance, how many blocks a single mkdir touches.

Boundaries

debugfs works on a raw filesystem, so never point its write mode at a mounted device: the kernel's cached view and your edits will disagree, and the result is corruption. Treat images as disposable. The tools show ext4 as e2fsprogs understands it, which is the right reference, but the kernel driver also depends on mount options such as data=ordered and commit=5, which live in the mount, not the image.

Share

Keep reading

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.