Knowledge
Partitions and file systems on Ubuntu 24.04: lsblk, parted and mkfs.ext4
- A new disk needs three steps before it is usable: a partition table, a partition, and a file system.
- Use
lsblkto look,partedto divide,mkfs.ext4to create the file system, andmountto attach it. - Writing to the wrong disk destroys data: always double-check the device name and take a backup first.
Goal
After this article you can list the disks on Ubuntu 24.04 LTS, create a GPT partition table and one partition on an empty disk, create an ext4 file system, and mount it on a directory. This is the first part of a series about disks on Linux; the next articles cover LVM and the /etc/fstab file.
Three concepts to keep apart:
- Disk (block device): the raw storage device, for example
/dev/sdb, or/dev/vdbon a virtual machine. - Partition: a slice of the disk, for example
/dev/sdb1. The slicing is recorded in the partition table; GPT is the modern kind, replacing the older MBR, which is limited to 2 TiB. - File system: the structure that records file names, directories and permissions on the partition. ext4 is the common default on Ubuntu.
Preparation
- An Ubuntu 24.04 LTS machine and
sudorights. - An empty disk with no data you want to keep. In this article it is
/dev/sdb; on your machine it may have another name such as/dev/vdb. - The
partedpackage. If it is missing:sudo apt install parted.
Warning: the
parted mklabelandmkfscommands below erase data on the chosen device. Back up first, and never run them against a disk that holds the operating system or data in use.
Steps
Steps
Step 1: list the disks
lsblk -o NAME,SIZE,TYPE,FSTYPE,MOUNTPOINTS
The TYPE column says disk or part. A new disk usually shows only a disk line with no child partitions and an empty FSTYPE. The disk holding the operating system has / under MOUNTPOINTS. Confirm the empty disk by its size: picking the wrong name is the most dangerous mistake in this whole article.
Step 2: create a GPT partition table
sudo parted /dev/sdb --script mklabel gpt
The --script option (short form -s) makes parted run without asking, so check the device name once more before pressing Enter.
Step 3: create one partition using the whole disk
sudo parted /dev/sdb --script mkpart data ext4 1MiB 100%
Explanation: data is the partition name stored in GPT, ext4 is only a hint about the file system type recorded in the partition table (no file system is created yet), 1MiB is the start point so the partition is aligned correctly, and 100% is the end point. Check the result:
sudo parted /dev/sdb --script print
lsblk /dev/sdb
You will see the new partition /dev/sdb1.
Step 4: create the ext4 file system
sudo mkfs.ext4 -L dulieu /dev/sdb1
-L sets a label for the file system. The command prints the file system UUID; you can see it again with lsblk -f or sudo blkid /dev/sdb1.
Step 5: mount it on a directory
sudo mkdir -p /mnt/dulieu
sudo mount /dev/sdb1 /mnt/dulieu
This mount lasts only until the next reboot. To make it permanent, see fstab on Ubuntu 24.04: mounting disks safely at boot with UUID and nofail.
Verification
findmnt /mnt/dulieu
df -h /mnt/dulieu
lsblk -f /dev/sdb
findmnt shows the device and file system type that are mounted; df -h shows the available space; lsblk -f shows FSTYPE as ext4 and the label dulieu. Try creating a file: sudo touch /mnt/dulieu/thu.txt. When you are done, unmount with sudo umount /mnt/dulieu.
Note that ext4 reserves part of the space (5% by default) for the root user, so df may report slightly less available space than the disk size. That is normal.
Common errors
- Wrong device name.
/dev/sdXnames can change between boots when you attach more disks. Before every destructive action, runlsblkagain and compare sizes. mount: wrong fs type, bad option, bad superblock. Usually the partition has no file system yet or you typed the wrong device; check withlsblk -f.target is busyonumount. A process is using the directory; leave it (cd /) or usefindmntand then look at which process holds the directory.mkfsrefuses because the device is mounted. Unmount first; if it is the system disk, stop, because you picked the wrong device.- Creating a file system on a whole disk without a partition. It works, but it is harder to grow and can confuse other tools; create a partition table first, as in this article.
Summary
The workflow is: lsblk to confirm the disk, parted to create GPT and a partition, mkfs.ext4 to create the file system, mount to use it, then findmnt/df -h to verify. Every command that writes to a disk is irreversible, so read the device name carefully. The next article in the series is LVM on Ubuntu 24.04: creating logical volumes and growing a disk online, which allows more flexible resizing than ordinary partitions.