Partitions and file systems on Ubuntu 24.04: lsblk, parted and mkfs.ext4

  1. Goal
  2. Preparation
  3. Steps
  4. Step 1: list the disks
  5. Step 2: create a GPT partition table
  6. Step 3: create one partition using the whole disk
  7. Step 4: create the ext4 file system
  8. Step 5: mount it on a directory
  9. Verification
  10. Common errors
  11. Summary
  12. References

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:

Preparation

Warning: the parted mklabel and mkfs commands 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

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.

References

Partitions and file systems on Ubuntu 24.04: lsblk, parted and mkfs.ext4