Creating and Exploring a Logical Disk
In this lab we’ll create a logical disk using a loopback device, format it with ext4, and explore the structures the filesystem lays down.
We’ll stick with ext4 here since it’s the default on many Linux distros and strikes a balance of performance and reliability. Other options include XFS, Btrfs, and ZFS—I’ve done comparisons of those in a separate article.
1. Setup
Install required tools:
sudo apt-get update
sudo apt-get install -y e2fsprogs util-linux
2. Create a Logical Disk
We’ll use a sparse file as the backing store. Sparse means the file appears to be 1 GiB, but the actual space is only allocated as we write into it.
mkdir -p ~/fs-lab && cd ~/fs-lab
truncate -s 1G disk.img # create a 1 GiB sparse file
Check the difference between apparent vs actual size:
ls -lh disk.img # shows "apparent size" (1.0G)
du -h disk.img # shows "real allocated size" (33MB for me)
3. Attach as a Loopback Device
Turn the file into a block device with losetup
:
sudo losetup -fP disk.img
losetup -a # list loop devices
You’ll see something like:
/dev/loop7: [2065]:1234567 (/home/user/fs-lab/disk.img)
Here /dev/loop7
is our logical disk.
4. Format with ext4
sudo mkfs.ext4 -L FSLAB /dev/loop7
mkdir -p mnt
sudo mount /dev/loop7 mnt
Now mnt/
is an ext4 filesystem inside disk.img
.
5. Inspect the Filesystem
Immediately after formatting:
du -h .
Example output:
16K ./mnt/lost+found
20K ./mnt
33M ./disk.img
33M .
What’s going on?
-
lost+found (16K) → auto-created by ext4; fsck uses it to store orphaned files recovered after crashes.
-
mnt/ (20K total) → just the root directory plus lost+found.
-
disk.img (33M) → although created as 1 GiB sparse, ext4 consumes ~33 MB immediately for metadata:
- superblock + backup superblocks
- group descriptors
- inode tables (preallocated)
- block & inode bitmaps
- journal area
6. Logical vs Physical Space
Key distinction:
- Logical size: what the file pretends to be (
ls -lh
). - Physical size: how much real disk space is consumed (
du -h
).
Sparse allocation means you can over-provision. For example, you could create 5 × 400 GB loop disks on a 1 TB SSD. They’d all exist, but once you actually try to use more than 1 TB total, you’ll hit ENOSPC
(“no space left on device”).
7. Safety Note
- Anything under
/dev/loopX
is safe to experiment with—it only touchesdisk.img
. - Never run
mkfs
on your real disk (/dev/nvme0n1
or similar).
8. Next Steps
From here, we can:
- Explore the superblock with
dumpe2fs
andtune2fs
. - Inspect journaling mode (
data=ordered
by default). - To Disk and Back
rm
: A File’s Tale — trace what happens when wewrite()
andrm
files. - Ext4 Inodes and Extents: A Hands-On Exercise — play with fragmentation, inodes, and extent mapping.
- Shadows on Disk: Ext4 Deletion and Recovery — why
rm -rf /
isn’t enough, and how deleted files can sometimes be recovered (or really destroyed).