Disks and filesystems

Create and identify a filesystem

Format only a verified empty target and record a stable UUID or label for future mounts.

8 minute lesson

~~~

Formatting creates filesystem metadata and destroys access to existing filesystem contents on the target.

mkfs doesn’t erase every block, but it overwrites the structures that locate your files. From the system’s point of view, the old data is gone. So the command itself is easy; the discipline is in what you do before running it.

Verify, then format

Choose a filesystem supported by your distribution and workload. For a general-purpose data disk on Ubuntu or Debian, ext4 is the safe default. Run the appropriate mkfs command only after target verification:

sudo blkid /dev/sdb1        # no output = no existing signature
sudo mkfs.ext4 /dev/sdb1
Creating filesystem with 52428800 4k blocks and 13107200 inodes
Filesystem UUID: 9a7f43c2-6c1d-4c2e-8f3a-2b64d0e51c77
Writing superblocks and filesystem accounting information: done

If mkfs.ext4 finds an existing filesystem it stops and asks Proceed anyway? (y,N). That prompt is a stop sign, not an inconvenience. Answer n and go figure out what’s on that device.

Use blkid to record its UUID and type afterward:

sudo blkid /dev/sdb1
# /dev/sdb1: UUID="9a7f43c2-6c1d-4c2e-8f3a-2b64d0e51c77" TYPE="ext4" PARTLABEL="data"

The UUID is generated at format time and stays with the filesystem forever, even if the device name changes. That’s the identifier you’ll use in /etc/fstab later, never /dev/sdb1.

You can also assign a human-readable label at format time with mkfs.ext4 -L data /dev/sdb1, or later with e2label. Labels are nicer to read, but nothing stops two disks from carrying the same label, and then which one mounts is a coin flip. UUIDs don’t have that problem. My advice: set a label for humans, mount by UUID for the machine, and write both down somewhere your future self will look.

Practice on a loop device

You don’t need spare hardware to rehearse this. A loop device turns a plain file into a block device:

truncate -s 1G /tmp/disk.img
sudo losetup --find --show /tmp/disk.img   # prints /dev/loop0
sudo mkfs.ext4 /dev/loop0
sudo mount /dev/loop0 /mnt/lab
echo "hello" | sudo tee /mnt/lab/test.txt
sudo umount /mnt/lab
sudo losetup -d /dev/loop0

Same commands, zero risk. This is how I test any formatting or mount procedure before touching a real disk.

Use a loop device or disposable volume. Verify it, create one filesystem, inspect its UUID, mount it temporarily, write a test file, and unmount it.

Lesson completed

Take this course offline

Get every free book and course as PDF and EPUB files.

Get the download library →