Disks and partitions: lsblk, parted, fdisk
This lesson is open to everyone. The other lessons open with a package: sign in or sign up, then choose a package.
Key points
- Find the new disk:
lsblk,lsblk -f(no file system),blkid; on a virtual server usuallyvdborsdb. parted -s /dev/sdb mklabel gpt mkpart data xfs 1MiB 2GiB— partitioning from a script; write sizes in MiB/GiB.fdisk /dev/sdb:gGPT,nnew,ttype (lvmfor LVM,swapfor swap),wwrite.- After partitioning,
udevadm settleorpartprobe— so the kernel sees the new partition. - Partition types: Linux filesystem, Linux LVM, Linux swap — choose the right type for LVM and swap.
Finding the new disk
[ali@srv-web01 ~]$ lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS sda 8:0 0 40G 0 disk ├─sda1 8:1 0 600M 0 part /boot/efi ├─sda2 8:2 0 1G 0 part /boot └─sda3 8:3 0 38.4G 0 part ├─rhel-root 253:0 0 34.4G 0 lvm / └─rhel-swap 253:1 0 4G 0 lvm [SWAP] sdb 8:16 0 10G 0 disk
sdb — 10 GB, with no partitions and no mount point. On KVM/virtio virtual machines disks are called vda, vdb — in the exam always check the name with lsblk.
The first rule: never touch sda (the system disk). Read the disk name a second time before every command.
parted: in one command
We covered fdisk interactively in the Ubuntu course. In scripts and for quick work parted is handy:
[ali@srv-web01 ~]$ sudo parted -s /dev/sdb mklabel gpt [ali@srv-web01 ~]$ sudo parted -s /dev/sdb mkpart data xfs 1MiB 2GiB [ali@srv-web01 ~]$ sudo parted -s /dev/sdb mkpart lvm1 2GiB 8GiB set 2 lvm on [ali@srv-web01 ~]$ sudo parted -s /dev/sdb mkpart swap1 linux-swap 8GiB 100% [ali@srv-web01 ~]$ sudo parted /dev/sdb print Model: QEMU QEMU HARDDISK (scsi) Disk /dev/sdb: 10.7GB Sector size (logical/physical): 512B/512B Partition Table: gpt Disk Flags: Number Start End Size File system Name Flags 1 1049kB 2147MB 2146MB data 2 2147MB 8590MB 6442MB lvm1 lvm 3 8590MB 10.7GB 2147MB swap1 swap
| Part | Meaning |
|---|---|
mklabel gpt | A GPT partition table (erases the old one!) |
mkpart name type start end | A partition; on GPT the name is required |
1MiB | Start the first partition at 1 MiB — correct alignment |
100% | To the end of the disk |
set 2 lvm on | The LVM flag on partition 2 |
Watch the units: in parted GB = 1000³, GiB = 1024³. If the task says "2 GiB", write GiB; working in binary units (MiB, GiB) is always more precise.
fdisk: interactive
[ali@srv-web01 ~]$ sudo fdisk /dev/sdb Command (m for help): n Partition number (4-128, default 4): First sector (20969472-20971486, default 20969472): Last sector, +/-sectors or +/-size{K,M,G,T,P} (20969472-20971486, default 20971486): +512M Command (m for help): t Partition number (1-4, default 4): 4 Partition type or alias (type L to list all): lvm Changed type of partition 'Linux filesystem' to 'Linux LVM'. Command (m for help): w
The partition size is written as +512M, +2G. t selects the type — by alias (lvm, swap, linux) or number.
| Type (fdisk alias) | What for |
|---|---|
linux | A regular file system |
lvm | An LVM physical volume (next lessons) |
swap | Swap |
uefi | EFI system partition |
Make the kernel see the partition
[ali@srv-web01 ~]$ sudo udevadm settle [ali@srv-web01 ~]$ lsblk /dev/sdb NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS sdb 8:16 0 10G 0 disk ├─sdb1 8:17 0 2G 0 part ├─sdb2 8:18 0 6G 0 part └─sdb3 8:19 0 2G 0 part
If a new partition does not appear in lsblk: sudo partprobe /dev/sdb. On a disk that is in use (for example the system disk), the kernel sometimes updates its table only after a reboot.
Deleting partitions
[ali@srv-web01 ~]$ sudo parted -s /dev/sdb rm 3 [ali@srv-web01 ~]$ sudo wipefs -a /dev/sdb
wipefs -a erases all signatures on the disk (partition table, file system, LVM) — returning it to a "fresh" state. There is no way back.
Lab: prepare the disk
In the Red Hat simulator the first step of the "LVM on a new disk" task is finding the disk with lsblk; try the partitioning part on your own VM:
- Find the empty disk with
lsblk(sdb). - On your own VM: create a GPT table and three partitions — 2 GiB (for xfs), 6 GiB (with the LVM flag), the rest (swap).
- Check with
parted /dev/sdb printandlsblk.
[ali@srv-web01 ~]$ lsblk [ali@srv-web01 ~]$ sudo parted -s /dev/sdb mklabel gpt mkpart data xfs 1MiB 2GiB mkpart lvm1 2GiB 8GiB set 2 lvm on mkpart swap1 linux-swap 8GiB 100% [ali@srv-web01 ~]$ lsblk /dev/sdb
We will use these partitions in the next lessons: sdb1 → XFS, sdb2 → LVM, sdb3 → swap. In the simulator, LVM is built on the whole /dev/sdb (6.3).
Check: lsblk shows sdb1, sdb2, sdb3, and sdb2 has the LVM flag.
Check yourself
5 questions · answers are shown instantly · 3 correct answers complete the lesson
1What is the first step before an RHCSA storage task?
2Which unit is right for a 2 GiB partition in parted?
3Which fdisk command makes a partition's type LVM?
4A new partition does not appear in lsblk. What do you do?
5What does wipefs -a /dev/sdb do?
Comments
Sign in to comment.