A new disk needs three steps before it is usable: a partition table, a partition, and a file system.
Use lsblk to look, parted to divide, mkfs.ext4 to create the file system, and mount to attach it.
Writing to the wrong disk destroys data: always double-check the device name and take a backup first.
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/vdb on 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.
An Ubuntu 24.04 LTS machine and sudo rights.
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 parted package. If it is missing: sudo apt install parted.
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
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.
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.
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.
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.
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.
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.
Wrong device name. /dev/sdX names can change between boots when you attach more disks. Before every destructive action, run lsblk again 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 with lsblk -f.
target is busy on umount. A process is using the directory; leave it (cd /) or use findmnt and then look at which process holds the directory.
mkfs refuses 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.
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.