How to Partition, Format and Mount a Disk in Linux
Adding a disk to a Linux machine takes four steps: find the device, partition it, create a filesystem, and mount it. The fifth step, adding it to /etc/fstab, is where most problems come from, because a bad entry can stop the machine from booting. The same steps apply to a new physical disk, a cloud volume (EBS, Persistent Disk, Azure managed disk) or a USB drive.
1. Find the disk
lsblk -f
NAME FSTYPE FSVER LABEL UUID MOUNTPOINTS
nvme0n1
├─nvme0n1p1 vfat FAT32 6C1A-2B3D /boot/efi
└─nvme0n1p2 ext4 1.0 0b6a4e3c-... /
nvme1n1
The new disk is the one with no partitions, no filesystem and no mount point: here nvme1n1. Device names depend on the hardware: sda, sdb for SATA, SAS and USB, nvme0n1 for NVMe (including EBS volumes on current EC2 instance types), vda for virtio disks in KVM. Check the size with lsblk or sudo fdisk -l to be sure you have the right one.
Device names are not stable across reboots, especially with several NVMe disks. Never use them in fstab. Use UUIDs, as shown below.
Before you write anything, make sure the disk is really empty:
sudo wipefs /dev/nvme1n1 # lists existing signatures, changes nothing
No output means no partition table or filesystem. Everything below destroys whatever is on the device.
2. Partition it
sudo parted --script /dev/nvme1n1 mklabel gpt mkpart data ext4 0% 100%
lsblk /dev/nvme1n1
This creates a GPT partition table and one partition (nvme1n1p1) over the whole disk. Using 0% and 100% lets parted align the partition correctly. GPT is the right choice for anything new. MBR is limited to 2 TiB disks and four primary partitions.
On cloud data volumes, many people skip the partition and put the filesystem directly on the whole device (/dev/nvme1n1). That works and makes growing the volume later one step simpler. A partition table makes it obvious to other tools and people that the disk is in use. Either is fine, just be consistent.
3. Create a filesystem (format)
sudo mkfs.ext4 -L data /dev/nvme1n1p1
# or
sudo mkfs.xfs -L data /dev/nvme1n1p1
Which one:
- ext4: the default on Debian and Ubuntu. Can be grown and shrunk (shrinking only while unmounted).
- XFS: the default on RHEL and its rebuilds, good with large files and parallel I/O. Can be grown online but never shrunk.
- exFAT: for USB drives shared with Windows and macOS.
sudo mkfs.exfat -n USB /dev/sdc1(packageexfatprogs). - FAT32: maximum compatibility, 4 GiB file size limit.
sudo mkfs.vfat -F 32 -n USB /dev/sdc1.
ext4 reserves 5% of the space for root by default. On a large data-only disk, that is a lot of wasted space. Use -m 0 at creation, or change it later with sudo tune2fs -m 1 /dev/nvme1n1p1.
4. Mount it
sudo mkdir -p /mnt/data
sudo mount /dev/nvme1n1p1 /mnt/data
df -h /mnt/data
mount detects the filesystem type. Add -t only when detection fails or you want a specific driver.
A new ext4 or XFS filesystem is owned by root. Give it to the user or service that will write there:
sudo chown alice:alice /mnt/data
5. Mount at boot with fstab
Get the UUID:
sudo blkid /dev/nvme1n1p1
Add a line to /etc/fstab:
UUID=3f1c2b9e-7a4d-4e8b-9c1a-2d5e6f708192 /mnt/data ext4 defaults,nofail 0 2
The fields are: device, mount point, filesystem type, options, dump (always 0), and fsck order (1 for root, 2 for other ext4 filesystems, 0 for XFS and for filesystems you do not want checked at boot).
nofail is the important part. Without it, if the disk is missing or broken, the system stops at boot in emergency mode. On a cloud VM without console access, that can mean an unreachable instance. With nofail, boot continues and only the mount is missing.
Test the entry before you reboot:
sudo findmnt --verify # checks fstab syntax and that devices exist
sudo umount /mnt/data
sudo systemctl daemon-reload # systemd generates mount units from fstab
sudo mount -a # mounts everything in fstab that is not mounted
findmnt /mnt/data
If mount -a fails, fix the line now, not after a reboot.
For network shares or disks that are not always present, add x-systemd.automount to the options. systemd then mounts the filesystem on first access instead of at boot.
USB drives and NTFS
On a desktop, plugging in a drive mounts it under /media/$USER/ or /run/media/$USER/ automatically. From a terminal, without sudo:
udisksctl mount -b /dev/sdc1
udisksctl unmount -b /dev/sdc1
To mount manually with write access for your user:
sudo mount -o uid=$(id -u),gid=$(id -g) /dev/sdc1 /mnt/usb # exFAT or FAT32
sudo mount -t ntfs3 -o uid=$(id -u),gid=$(id -g) /dev/sdc1 /mnt/usb # NTFS
FAT and exFAT have no Unix permissions, so the uid and gid options decide who owns the files. ntfs3 is the in-kernel NTFS driver (kernel 5.15 and later). The older FUSE driver ntfs-3g works too. If Windows was hibernated or used Fast Startup, the NTFS volume is marked as in use and Linux will refuse to mount it read-write. Shut Windows down fully first.
Unmount
sudo umount /mnt/data
"target is busy" means a process has a file open there, or a shell has it as its current directory:
sudo fuser -vm /mnt/data
sudo lsof +f -- /mnt/data
Stop those processes, then unmount. umount -l (lazy) hides the problem instead of fixing it. Avoid it on disks you are about to detach.
Growing a cloud volume
After resizing the volume in the cloud console, grow the partition and then the filesystem, both online:
sudo growpart /dev/nvme1n1 1 # package cloud-guest-utils or cloud-utils-growpart
sudo resize2fs /dev/nvme1n1p1 # ext4
sudo xfs_growfs /mnt/data # XFS takes the mount point
Without a partition, skip growpart and resize the filesystem directly.
Quick reference
lsblk -f # disks, filesystems, mounts
sudo wipefs /dev/sdX # check for existing data
sudo parted --script /dev/sdX mklabel gpt mkpart data ext4 0% 100%
sudo mkfs.ext4 -L data /dev/sdX1 # format (destroys data)
sudo mkdir -p /mnt/data && sudo mount /dev/sdX1 /mnt/data
sudo blkid /dev/sdX1 # UUID for fstab
# fstab: UUID=... /mnt/data ext4 defaults,nofail 0 2
sudo findmnt --verify && sudo mount -a # test fstab
sudo umount /mnt/data # unmount
