How to partition and format a disk
Topic: Servers linux
Summary
Create a partition table and partitions with fdisk or parted, format with mkfs.ext4 or mkfs.xfs, then mount and add to fstab. Use this when adding a new data disk so the system can use it safely after reboot.
Intent: How-to
Quick answer
- Identify disk: lsblk, fdisk -l; do not use the system disk. Create partition: fdisk /dev/sdb, n for new, p primary, default start/end, w to write.
- Format: mkfs.ext4 /dev/sdb1 or mkfs.xfs /dev/sdb1; create mount point, mount, add to fstab with UUID from blkid.
- Verify: mount and df; reboot and confirm mount is present; never format a disk that is in use or that contains data you need.
Prerequisites
Steps
-
Identify the disk
lsblk; fdisk -l; ensure you use the correct device (e.g. /dev/sdb not /dev/sda if sda is system); back up data if the disk had content.
-
Create partition table and partition
fdisk /dev/sdb: g for GPT, n new, p primary, 1, default start, default or custom end, w. Or use parted for scripting.
-
Format and mount
mkfs.ext4 /dev/sdb1; blkid for UUID; mkdir /data; mount /dev/sdb1 /data; add UUID=... /data ext4 defaults 0 2 to /etc/fstab; mount -a.
-
Verify
df -h /data; reboot and check df again; set ownership if a service will use the mount.
Summary
You will partition a new disk, format it with a filesystem, mount it, and add it to fstab. Use this when adding storage; never format the wrong device.
Prerequisites
- Root; the disk must be unused (not the OS disk) and identified correctly.
Steps
Step 1: Identify the disk
lsblk
sudo fdisk -l
Confirm which device is the new disk (e.g. /dev/sdb). Do not use the disk that holds /.
Step 2: Create partition table and partition
sudo fdisk /dev/sdb
# g (GPT), n (new), p (primary), 1, Enter, Enter (full size), w
Step 3: Format and mount
sudo mkfs.ext4 /dev/sdb1
sudo blkid /dev/sdb1
sudo mkdir -p /data
sudo mount /dev/sdb1 /data
echo 'UUID=xxx /data ext4 defaults 0 2' | sudo tee -a /etc/fstab
sudo mount -a
Step 4: Verify
df -h /data
sudo reboot
df -h /data
Verification
- Partition exists (lsblk); filesystem type correct (blkid); mount persists after reboot.
Troubleshooting
Wrong disk formatted — If data was lost, restore from backup; always double-check the device name.
Mount fails at boot — Check fstab UUID and options; run fsck if the fs was not cleanly unmounted.