Intermediate · Storage: partitions, file systems and LVM · topic 24 of 57 · 35 min
Disks and partitions: MBR, GPT, fdisk and parted
Explanation
Block devices and their names
A disk appears in /dev as a block device: a file that represents storage read and written in blocks. The name tells you the kind of disk. SATA, SAS and many virtual disks are /dev/sda, /dev/sdb and so on. KVM virtual disks are /dev/vda, /dev/vdb. NVMe drives are /dev/nvme0n1. Partitions add a number: /dev/sdb1, /dev/vdb2, or for NVMe /dev/nvme0n1p1.
$ lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
vda 252:0 0 20G 0 disk
├─vda1 252:1 0 1G 0 part /boot
└─vda2 252:2 0 19G 0 part
├─rhel-root 253:0 0 17G 0 lvm /
└─rhel-swap 253:1 0 2G 0 lvm [SWAP]
vdb 252:16 0 5G 0 disk
vdc 252:32 0 5G 0 diskWhy partition?
A partition is a fixed slice of a disk that the system treats as a separate area. You partition to keep things apart (so a full log area cannot fill the root file system), to use different file systems, or to hand space to LVM or swap. The partition table, at the start of the disk, records where each partition begins and ends.
| MBR (msdos) | GPT | |
|---|---|---|
| Max disk size | 2 TiB | Far beyond any real disk (8 ZiB) |
| Partitions | 4 primary, or 3 primary + 1 extended with logical partitions inside | 128 by default |
| Boot firmware | Classic BIOS | UEFI (and BIOS with a small BIOS boot partition) |
| Safety | One copy of the table | Table copied at the end of the disk, with checksums |
For new disks, use GPT unless you have a reason not to.
Creating partitions with parted
# parted /dev/vdb mklabel gpt
# parted /dev/vdb mkpart data xfs 1MiB 2GiB
# parted /dev/vdb mkpart lvmpart 2GiB 4GiB
# parted /dev/vdb set 2 lvm on
# parted /dev/vdb print
Model: Virtio Block Device (virtblk)
Disk /dev/vdb: 5369MB
Partition Table: gpt
Number Start End Size File system Name Flags
1 1049kB 2147MB 2146MB data
2 2147MB 4295MB 2147MB lvmpart lvm
# udevadm settle # wait until /dev/vdb1 and /dev/vdb2 appearThe file-system word in mkpart (xfs above) only sets a type hint; it does not create a file system. You still run mkfs in the next lesson.
The same with fdisk (interactive)
# fdisk /dev/vdc
Command (m for help): g # new empty GPT table (o = MBR)
Command (m for help): n # new partition
Partition number (1-128, default 1): 1
First sector (2048-10485726, default 2048): <Enter>
Last sector, +/-sectors or +/-size{K,M,G,T,P} (...): +1G
Command (m for help): t # change type
Partition type or alias (type L to list all): swap
Command (m for help): p # print, to check
Command (m for help): w # write and quit (q quits WITHOUT saving)Partition types tell tools what a partition is for: Linux filesystem, Linux LVM, Linux swap. On MBR they are hex codes (83 Linux, 8e LVM, 82 swap). If the kernel does not see new partitions on a disk that is in use, run partprobe /dev/vdc or udevadm settle.
Common mistakes
- Partitioning the wrong disk. Always run lsblk first and check the size and that there are no mount points. Mistakes here destroy data.
- Writing a new label (mklabel or fdisk g/o) on a disk that has data. It wipes the table and every partition with it.
- Expecting MBR to use a 4 TB disk fully. It cannot go past 2 TiB; use GPT.
- Forgetting to set the LVM or swap type. Many tools still work, but people and installers can be confused later.
Need help?
Stuck on a word or an idea? Search this course, or every course, and jump straight to the explanation or a worked question.
Try:
Watch the lesson
Disks and partitions: MBR, GPT, fdisk and parted · 1/6
Disks appear as block devices in slash dev: sda, vda or nvme zero n one.
Prefer a video by a teacher? Find free videos on “rhel 9 fdisk parted gpt partition tutorial rhcsa” on YouTube ↗
In real life: Adding a disk for CCTV recordings at a Jaipur hotel
A hotel in Jaipur stores CCTV recordings on a RHEL server, and the old disk is full. The vendor adds a new 8 TB disk. The admin checks with lsblk that /dev/sdb is the new, empty 8 TB disk, creates a GPT label (MBR could not use more than 2 TiB of it), and makes one partition across the whole disk for the recordings.
# lsblk /dev/sdb
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
sdb 8:16 0 7.3T 0 disk
# parted /dev/sdb mklabel gpt
# parted /dev/sdb mkpart cctv xfs 1MiB 100%
# lsblk /dev/sdb
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
sdb 8:16 0 7.3T 0 disk
└─sdb1 8:17 0 7.3T 0 partPractice (0/4 done)
You must use all of a 6 TB disk as one partition. Which partition table do you choose?
In fdisk, which key writes the changes to disk?
Write fits_mbr(disk_tib, partitions) that returns True if a disk layout is possible with an MBR table using only primary partitions: the disk must be at most 2 TiB AND there must be at most 4 partitions.
On your lab VM's first spare disk, create a GPT label and three partitions: 1 GiB for a file system, 1 GiB for swap (type swap), and 2 GiB for LVM (lvm flag).
Quick quiz
Read more (free)
Related help
Before this, learn
This topic builds on these. If something here feels new, open them first.
Related topics
- File systems and mounting: XFS, ext4, mount and dfStorage: partitions, file systems and LVM · same module
- Permanent mounts with /etc/fstab and UUIDsStorage: partitions, file systems and LVM · same module
- LVM: physical volumes, volume groups and logical volumesStorage: partitions, file systems and LVM · same module
- OVER and PARTITION BYSQL and Databases · Window functions
- Merge sort and quick sortData Structures and Algorithms (DSA) for interviews · Sorting and searching
Questions people ask about this
Open a question to see the answer and why. The first ones are this topic's own quiz, so try the quiz above before you peek.
What is the first partition on /dev/nvme0n1 called?
Answer: /dev/nvme0n1p1
NVMe partitions add p and a number.
How many partitions does a GPT table allow by default?
Answer: 128
GPT reserves room for 128 entries.
What does "parted /dev/vdb mkpart data xfs 1MiB 2GiB" NOT do?
Answer: Create an XFS file system
The xfs word is only a hint; run mkfs.xfs afterwards.
Which command should you ALWAYS run before partitioning?
Answer: lsblk (to check the disk)
Confirm the disk name, size and that it is not in use.
Which creates an LV using all remaining space in vg01?Related
Answer: lvcreate -n lv1 -l 100%FREE vg01
Percentages go with -l, e.g. 100%FREE.
From LVM: physical volumes, volume groups and logical volumes
employees has 11 rows. How many rows does SELECT name, AVG(salary) OVER (PARTITION BY dept_id) FROM employees return?Related
Answer: 11
A window function never removes rows; each of the 11 rows gets its department average.
Which command shows file system types and UUIDs of block devices?Related
Answer: lsblk -f
lsblk -f (or blkid) shows FSTYPE and UUID.
Which command grows a mounted XFS file system to fill its LV?Related
Pass the quiz (60% or more) or mark this topic as done when you are ready.
Your progress is saved on this device only.
Next steps
- 4 more topics in “Storage: partitions, file systems and LVM” before its exam: File systems and mounting: XFS, ext4, mount and df, Permanent mounts with /etc/fstab and UUIDs, LVM: physical volumes, volume groups and logical volumes, Growing storage and adding swap.
- Module 5 exam: Storage · 14 questions · 25 min, without help. 5 topics of this module still to finish first.
- Then the next module, SELinux and SSH. 57 topics left in the course before the final exam.