A raw disk is empty
Attaching a disk doesn't make it usable. The blocks are still blank: there's no structure that says where files begin and end, what they're called, or who owns them. That structure is a filesystem, and you create it with mkfs (make filesystem). This is called formatting.
Linux supports many filesystem types. You pick one when you format:
| Filesystem | Made with | Good for |
|---|---|---|
ext4 | mkfs.ext4 | The usual choice for Linux disks |
xfs | mkfs.xfs | Large filesystems and servers |
vfat / FAT | mkfs.vfat | USB sticks and SD cards read by any system |
btrfs | mkfs.btrfs | Snapshots and built-in volume management |
Formatting a disk: mkfs.ext4
Attach the disk, then format it as ext4. -L gives it a label, a friendly name. mkfs prints what it builds — the number of blocks and inodes:
sudo losetup -fP --show disk.img
sudo mkfs.ext4 -L mydata /dev/loop0In the terminal
student@vulntech:~$ sudo losetup -fP --show disk.img [sudo] password for student: /dev/loop0 student@vulntech:~$ sudo mkfs.ext4 -L mydata /dev/loop0 mke2fs 1.47.0 (5-Feb-2023) Creating filesystem with 65536 1k blocks and 16384 inodes Filesystem UUID: 0526eec9-f28f-4061-88c5-dfce4d3b5bcd Superblock backups stored on blocks: 8193, 24577, 40961, 57345 Allocating group tables: 0/8 done Writing inode tables: 0/8 done Creating journal (4096 blocks): done Writing superblocks and filesystem accounting information: 0/8 done
What's on the disk? blkid
blkid reads the filesystem's identity: its type, its label, and its UUID — a long unique id that never changes, even if the disk is renamed or moved. lsblk -f shows the same thing in the tree:
sudo blkid /dev/loop0
lsblk -f /dev/loop0In the terminal
student@vulntech:~$ sudo blkid /dev/loop0 [sudo] password for student: /dev/loop0: LABEL="mydata" UUID="33350c7a-835f-4921-bb10-c51d0f96bd9f" BLOCK_SIZE="1024" TYPE="ext4" student@vulntech:~$ lsblk -f /dev/loop0 NAME FSTYPE FSVER LABEL UUID FSAVAIL FSUSE% MOUNTPOINTS loop0 ext4 1.0 mydata 33350c7a-835f-4921-bb10-c51d0f96bd9f
A different filesystem: a FAT stick
To prepare a USB stick that Windows and macOS can also read, format it as vfat (FAT). Same idea, different tool; -n sets the label:
sudo losetup -fP --show usb.img
sudo mkfs.vfat -n MYUSB /dev/loop0
sudo blkid /dev/loop0In the terminal
student@vulntech:~$ sudo losetup -fP --show usb.img [sudo] password for student: /dev/loop0 student@vulntech:~$ sudo mkfs.vfat -n MYUSB /dev/loop0 mkfs.fat 4.2 (2021-01-31) student@vulntech:~$ sudo blkid /dev/loop0 /dev/loop0: SEC_TYPE="msdos" LABEL_FATBOOT="MYUSB" LABEL="MYUSB" UUID="4231-1FCF" BLOCK_SIZE="512" TYPE="vfat"
Common mistake:
mkfserases everything on the target. There is no undo. Always check the device name (lsblk) before you format — formatting the wrong disk destroys it.
Remember:
mkfs.ext4 DEVICEputs an ext4 filesystem on a disk (mkfs.vfatfor FAT).-L/-nsets a label.blkidandlsblk -fshow a filesystem's type, label and UUID.