Linux and servers · Intermediate · 13 min
Partition and format a new disk with parted and mkfs
By Padding · Updated 18 Nov 2026
Ubuntu 26.04.1 LTS · parted 3.6 · e2fsprogs 1.47.2 · xfsprogs 6.18 · util-linux 2.41.3
You will identify a newly attached data volume without guessing, give it a GPT partition table and one aligned partition with parted, format it as ext4 or XFS with a label, and read the filesystem UUID you need to mount it at boot.
On this page
You have attached a new volume to your server in the provider's console, and it shows up as a bare block device. Before you can store anything on it, it needs a partition table, a partition and a filesystem. Each of those steps writes to the start of the disk, so the one mistake that matters is running them against the wrong disk. On a server with an operating system disk, a database volume and a new empty volume, /dev/sdb today may be a different disk after the next reboot.
This tutorial goes in an order that makes that mistake hard: find the disk by a name that cannot change, prove it is empty, then partition and format it. You will use parted to write a GPT partition table and one partition aligned to the disk's physical layout, choose between ext4 and XFS, format it with a label, and read the filesystem UUID. That UUID is what the fstab tutorial uses to mount the disk at every boot.
The commands work the same on any provider and on a physical disk. If you manage servers over mobile data, run the steps inside tmux or screen, so a dropped SSH session does not cut a command off halfway.
Step 1 · Install the tools and find the new disk
Ubuntu Server usually has these packages already. Installing them makes sure, and adds xfsprogs in case you choose XFS later:
sudo apt update
sudo apt install -y parted fdisk e2fsprogs xfsprogsparted writes partition tables, fdisk provides sfdisk, which you use to save a copy of the table, and the other two packages make and inspect ext4 and XFS filesystems.
List the block devices with the columns that tell disks apart:
lsblk -o NAME,SIZE,TYPE,MOUNTPOINTS,SERIALYou see something like this, with your own names and sizes:
NAME SIZE TYPE MOUNTPOINTS SERIAL
nvme0n1 40G disk vol0a1b2c3d4e5f60718
├─nvme0n1p1 39G part /
├─nvme0n1p15 106M part /boot/efi
└─nvme0n1p16 913M part /boot
nvme1n1 100G disk vol0f9e8d7c6b5a49382The new volume is the disk with no partitions under it, no mount points and the size you chose in the console. Do not stop at the size: if two disks are the same size, the serial number decides. Many providers put the volume ID from their console into the serial. On AWS, for example, an EBS volume vol-0f9e8d7c6b5a49382 has the serial vol0f9e8d7c6b5a49382. Compare it with the volume page in your console.
Kernel names such as nvme1n1 and sdb are given out in the order the disks are found, so they can change. udev also creates links under /dev/disk/by-id/ built from the model and serial number, and those stay with the disk:
ls -l /dev/disk/by-id/Find the link that points at your new disk (-> ../../nvme1n1 in the example) and does not end in -partN. On AWS it looks like nvme-Amazon_Elastic_Block_Store_vol0f9e8d7c6b5a49382; other providers use their own name in the same place. Put the full path in a shell variable, so every later command names the same disk. Replace the example with your own link:
DISK=/dev/disk/by-id/REPLACE-WITH-YOUR-NEW-DISK-IDThe variable lasts for this shell session only. If you log out or open a new terminal, set it again before you carry on.
Check it worked
Resolve the link and look at that one disk:
readlink -f "$DISK"lsblk -o NAME,SIZE,TYPE,MOUNTPOINTS,SERIAL "$DISK"readlink -f prints the current kernel name, such as /dev/nvme1n1. lsblk prints exactly one row: TYPE is disk, SIZE and SERIAL match the volume in your console, MOUNTPOINTS is empty and there are no partition rows below it. If you see partitions or a mount point, you have the wrong disk. Set DISK again before going further.
Step 2 · Prove the disk is empty before you write to it
The next step replaces whatever is at the start of the disk. A new volume is blank, but a volume detached from another server, restored from a snapshot or picked by mistake is not. Check for existing partition tables and filesystems first:
sudo wipefs "$DISK"Without options, wipefs only reads. It lists every signature it finds (a partition table, a filesystem, RAID or LVM metadata) with its offset, and prints nothing for a blank disk. Ask parted as well:
sudo parted "$DISK" printCheck it worked
wipefs printed nothing, and parted reports Error: ... unrecognised disk label with no partition list. Both mean there is nothing on the disk to lose.
If either one shows something, stop and find out what the data is. When you are certain a reused volume holds nothing you need, this command erases every signature wipefs listed. It makes the old data unreachable and you cannot undo it, so run it only against the disk you checked in the previous step:
sudo wipefs --all "$DISK"Step 3 · Write a GPT label and one aligned partition
Use GPT rather than the old MBR (msdos) format. GPT handles disks over 2 TiB, keeps a backup copy of the table at the end of the disk and gives each partition a name.
This command writes a new partition table to $DISK without asking. --script turns off parted's confirmation prompts, which is why you checked the disk first:
sudo parted --script "$DISK" mklabel gpt mkpart data ext4 1MiB 100%Reading it left to right:
mklabel gptwrites an empty GPT partition table.mkpart data ext4 1MiB 100%creates one partition nameddata. On GPT the first word is the partition's name, not a type.ext4only sets the partition type to "Linux filesystem"; it does not format anything, and the same type is right for XFS.1MiBas the start leaves the first mebibyte for the GPT structures and puts the partition on a 1 MiB boundary. That is a whole number of the 4 KiB physical sectors modern disks and cloud volumes use, so every filesystem block lines up with the hardware.100%runs the partition to the end of the usable space and leaves room for the backup table.
Ask parted to confirm the alignment of partition 1. Leave out --script here: in script mode parted reports the result only through its exit status and prints nothing:
sudo parted "$DISK" align-check optimal 1It prints 1 aligned.
Check it worked
Print the new table, in MiB so the start is easy to read:
sudo parted --script "$DISK" unit MiB printYou see Partition Table: gpt and one line for partition 1 that starts at 1.00MiB, ends close to the size of the disk and has the name data. The File system column is empty, because there is no filesystem yet.
Save a text copy of the table. sfdisk can write it back if the table is ever damaged, and the file is a short record of how the disk was laid out:
sudo sfdisk --dump "$DISK" > ~/data-disk.sfdisk
cat ~/data-disk.sfdiskThe file starts with label: gpt and ends with one partition line showing start=2048 (2048 sectors of 512 bytes is 1 MiB), type=0FC63DAF-8483-4772-8E79-3D69D8477DE4 (the GPT code for a Linux filesystem partition) and name="data". Keep a copy somewhere other than this disk, such as your configuration repository.
Step 4 · Make sure the kernel sees the partition
parted tells the kernel about the new table as it writes it, and udev then creates the device node and links for the partition. If something else had the disk open, that can be missed. These two commands ask the kernel to reread the table and wait for udev to finish:
sudo partprobe "$DISK"
sudo udevadm settleudev names the partition's stable link after the disk's link with -part1 on the end. Keep it in a second variable:
PART="${DISK}-part1"Check it worked
lsblk -o NAME,SIZE,TYPE,PARTLABEL "$DISK"
readlink -f "$PART"lsblk now shows the disk with one part row under it, such as nvme1n1p1 or sdc1, with PARTLABEL data. readlink -f prints that partition's kernel path. If PART does not resolve, run ls -l /dev/disk/by-id/ | grep part1 and use the link that points at your new partition.
Step 5 · Choose ext4 or XFS and create the filesystem
Both are mature, journalled filesystems that Ubuntu supports fully. For one data volume the choice comes down to a few practical differences:
- ext4 is Ubuntu's default and what most guides and recovery tools assume. It can be grown while mounted and shrunk while unmounted, so it is the forgiving choice if you are not sure how the disk will be used.
- XFS handles very large files and many parallel writers well, which suits large media stores, log archives and some databases. It grows while mounted with
xfs_growfs, but in practice it cannot be shrunk: to make it smaller you copy the data to a new, smaller filesystem.
If nothing in your workload points to XFS, use ext4. Whichever you pick, mkfs erases anything on the partition, so check that PART is the partition you just created on the new disk.
For ext4, with the label data:
sudo mkfs.ext4 -L data "$PART"The label can be up to 16 bytes. ext4 reserves 5 per cent of the blocks for root by default, so system services can still write when the disk is full. On a large data-only volume that is a lot of unused space; sudo tune2fs -m 1 "$PART" lowers it to 1 per cent at any time.
For XFS, run this instead of the ext4 command, not as well. XFS labels can be up to 12 characters:
sudo mkfs.xfs -L data "$PART"Check it worked
mkfs.ext4 ends with Writing superblocks and filesystem accounting information: done. mkfs.xfs prints a table of its settings (meta-data=, data =, log =) with no error after it.
For ext4, check the filesystem is consistent. -n opens it read-only and answers no to every question, so it changes nothing:
sudo e2fsck -f -n "$PART"The last line reports the number of files and blocks in use, something like ...-part1: 11/131072 files (0.0% non-contiguous), 26156/524032 blocks, with no errors above it. For XFS, sudo xfs_repair -n "$PART" does the same job.
Step 6 · Read the UUID that identifies the filesystem
mkfs gave the filesystem a random UUID. Unlike the kernel name, it stays the same until you reformat, which is why fstab should mount by it:
sudo blkid "$PART"The line looks like this, with your own path and values:
/dev/disk/by-id/nvme-Amazon_Elastic_Block_Store_vol0f9e8d7c6b5a49382-part1: LABEL="data" UUID="5f3c9a6e-2b71-4d0e-9a54-8e1c2f7b3d90" BLOCK_SIZE="4096" TYPE="ext4" PARTLABEL="data" PARTUUID="c2e1b8f4-6a3d-4f19-b7e2-0d5a9c4e1f36"There are two UUIDs in it. UUID belongs to the filesystem and changes if you reformat. PARTUUID belongs to the partition table entry and stays the same even if you reformat. For mounting a filesystem, use UUID. Print it on its own:
sudo blkid -s UUID -o value "$PART"For ext4, dumpe2fs reads the same values straight from the superblock, along with the block size and the reserved block count from the previous step:
sudo dumpe2fs -h "$PART" | grep -E '^(Filesystem volume name|Filesystem UUID|Block size|Reserved block count):'Check it worked
blkid -s UUID -o value prints one UUID, and dumpe2fs shows the same value on its Filesystem UUID: line, with Filesystem volume name: set to data. Copy the UUID into your notes or configuration repository, next to the saved partition table. It goes into /etc/fstab in the next tutorial.
Step 7 · Try a temporary mount
Before you make anything permanent, mount the filesystem by hand to see that it works. This mount lasts until you unmount it or reboot:
sudo mkdir -p /mnt/data-test
sudo mount "$PART" /mnt/data-test
df -hT /mnt/data-testThen unmount it and remove the empty directory, so the next tutorial starts from a clean state:
sudo umount /mnt/data-test
sudo rmdir /mnt/data-testCheck it worked
df -hT printed one line for your partition with type ext4 (or xfs), a size slightly below the volume's size and almost nothing used. A new ext4 filesystem also has a lost+found directory, which fsck uses for recovered files; leave it in place. After umount, findmnt /mnt/data-test prints nothing.
Next steps
- Mount the disk at every boot by its UUID, with options that stop a missing volume blocking the boot: /tutorials/mount-a-disk-at-boot-with-fstab-and-uuids.
- Pool several disks and grow filesystems without repartitioning: /tutorials/add-disks-and-grow-filesystems-with-lvm.
- When the volume fills up, resize it in the console and grow the partition and filesystem in place: /tutorials/grow-a-cloud-server-disk-without-downtime.
- Find out what is filling a disk before you add another: /tutorials/find-what-is-using-disk-space-linux.
Related tutorials
- Write safer Bash scripts with strict mode and trapsYou will build a small Bash release script that stops on the first error, says which line failed, survives file names with spaces and always removes its temporary files.
- Write least-privilege sudo rules with visudoYou will replace blanket sudo access with checked rule files in /etc/sudoers.d that let two operators restart one service and edit one file, give an automation account a password-free command list, and audit what every account can run.
- Upgrade a server from Ubuntu 24.04 LTS to 26.04 LTSYou will take a backed-up Ubuntu 24.04 server through do-release-upgrade to 26.04 LTS over SSH with a fallback login, then confirm services, repositories and language runtimes work on the new release.
Need this done for you?
Padding does this work for growing companies, from the first diagnosis to a system that keeps working.
Get new tutorials