1 of 3

Storage and LVMLesson 1 of 3

Disks and GPT partitions

Find out what storage exists and what is free, then add partitions with fdisk or parted without touching data in use.

  • lsblk
  • blkid
  • fdisk -l
  • fdisk
  • parted
  • partprobe
  • udevadm settle
  • df -hT
  • findmnt

45 min5 exercises

After this lesson you can

  • Read lsblk, blkid and fdisk output to tell used from free disks and free space inside a disk.
  • Create a GPT partition table on an empty disk and add partitions of exact sizes with fdisk and parted.
  • Make the kernel see new partitions without rebooting, and avoid commands that destroy existing data.

The idea

Storage on Linux is a stack. A disk (/dev/vda, /dev/vdb, /dev/nvme0n1) is divided into partitions (/dev/vdb1), or handed whole to LVM; on top of either sits a file system, which you mount to a directory. Before changing anything, you must be able to answer what is where and what is free, because a wrong command on the wrong disk destroys data and nothing asks you to confirm.

Look first

CommandShows
lsblkTree of disks, partitions, LVM volumes, sizes and mount points
lsblk -fAdds file system type, label and UUID
blkidUUID, label and type of every block device
fdisk -l /dev/vdbPartition table of one disk, in sectors
parted /dev/vdb unit MiB print freePartitions and free space in MiB
df -hTMounted file systems with usage

An unused disk has no children in lsblk and no signature in blkid. A disk showing LVM2_member belongs to LVM and must not be repartitioned.

GPT

A partition table (the “label”) records where partitions start and end. Two kinds exist: the old MBR (max 2 TiB, four primary partitions) and GPT (huge disks, 128 partitions, redundant copy). The RHCSA expects GPT. The tools:

ToolStyle
fdiskInteractive menu; understands GPT; g creates a GPT table
partedScriptable one-liners (parted -s DISK mkpart ...)
gdiskGPT-specific, very similar to fdisk

In fdisk the useful keys: g new GPT label, n new partition, d delete, t change type, p print, w write and quit, q quit without saving. Nothing is written until w. Sizes accept suffixes: +500M, +2G.

After writing, the kernel may not have noticed. partprobe /dev/vdb (or udevadm settle) updates it, so /dev/vdb1 appears without a reboot.

Try it

lsblk -o NAME,SIZE,TYPE,FSTYPE,MOUNTPOINT
NAME          SIZE TYPE FSTYPE      MOUNTPOINT
vda            20G disk
├─vda1          1G part xfs         /boot
└─vda2         19G part LVM2_member
  ├─rhel-root  17G lvm  xfs         /
  └─rhel-swap   2G lvm  swap        [SWAP]
vdb             2G disk

vda is in use (root on LVM); vdb is empty and is the one to work on.

Check free space before adding

sudo parted /dev/vdb unit MiB print free
Error: /dev/vdb: unrecognised disk label
Model: Virtio Block Device (virtblk)
Disk /dev/vdb: 2048MiB
Sector size (logical/physical): 512B/512B
Partition Table: unknown

“Unrecognised disk label” means no table yet: the disk is blank and a new GPT label is safe.

fdisk: create a GPT label and a partition

sudo fdisk /dev/vdb
Command (m for help): g
Created a new GPT disklabel (GUID: 6F1D8F3B-...).

Command (m for help): n
Partition number (1-128, default 1):
First sector (2048-4194270, default 2048):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-4194270, default 4194270): +500M

Created a new partition 1 of type 'Linux filesystem' and of size 500 MiB.

Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

Pressing Enter accepts the default, which is the first free sector. The same session can be scripted when you know exactly what you want:

printf 'g\nn\n\n\n+500M\nw\n' | sudo fdisk /dev/vdb

parted: scriptable, and good for adding

sudo parted -s /dev/vdb mkpart part2 501MiB 801MiB
sudo parted -s /dev/vdb mkpart part3 801MiB 1001MiB
sudo partprobe /dev/vdb
sudo parted /dev/vdb unit MiB print
Number  Start    End      Size     File system  Name   Flags
 1      1.00MiB  501MiB   500MiB                primary
 2      501MiB   801MiB   300MiB                part2
 3      801MiB   1001MiB  200MiB                part3

parted changes are written immediately, with no w. The name after mkpart is a GPT partition name (a label), not a file system type.

Partition types

Each partition has a type code. Linux filesystem is the default; use Linux LVM for partitions that will become physical volumes and Linux swap for swap. In fdisk, t then the partition number, then lvm or swap. LVM and swap work whatever the type says, but the correct type documents intent for the next administrator and for tools that read it.

Going further

sgdisk is the scriptable form of gdisk. lsblk -J prints JSON, lsblk -b sizes in bytes. A partition cannot be extended unless free space follows it, which is one reason LVM is preferred for anything that may grow. wipefs -a /dev/vdb1 removes file-system and LVM signatures, and is the right tool to clear a disk you are deliberately reusing. On a virtual machine, adding another virtual disk is the usual way to practise; reboot is not needed, and echo 1 > /sys/block/vda/device/rescan rescans a disk whose size changed.

Practice

0 of 5 done

Do these on your own system. They get harder. Check each result, then tick it off.

  1. Warm-up

    What storage do I have?

    List all block devices with their size, type, file system and mount point. Identify the disk that holds the root file system, and any disk that is completely unused.

    Check your result

    lsblk -f shows every disk and partition. The disk with / mounted (directly or through LVM) is in use. An unused disk has no children and no mount point.

    Hint

    lsblk -o NAME,SIZE,TYPE,FSTYPE,MOUNTPOINT. blkid lists UUIDs and types. df -hT shows mounted ones only.

    Solution
    lsblk -o NAME,SIZE,TYPE,FSTYPE,MOUNTPOINT
    sudo blkid
    df -hT -x tmpfs -x devtmpfs
  2. Practice

    Is there free space on the disk I plan to use?

    For /dev/vdb (substitute your spare disk) show the partition table type, the existing partitions, and the free space in MiB. Decide whether adding a partition would be safe.

    Check your result

    parted /dev/vdb unit MiB print free lists partitions and a Free Space row. The table type is gpt or unknown.

    Hint

    sudo parted /dev/vdb unit MiB print free and sudo fdisk -l /dev/vdb.

    Solution
    sudo parted /dev/vdb unit MiB print free
    sudo fdisk -l /dev/vdb
  3. Practice

    Create a GPT partition with fdisk

    On the empty spare disk, create a GPT label and one 500 MiB partition. Re-read the table so the kernel sees it, and confirm with lsblk.

    Check your result

    lsblk /dev/vdb shows vdb1 of 500M. sudo fdisk -l /dev/vdb shows Disklabel type: gpt.

    Hint

    In fdisk: g new GPT table, n new partition (accept defaults, last sector +500M), w write. Then partprobe /dev/vdb.

    Solution
    sudo fdisk /dev/vdb
    #   g        (new GPT label)
    #   n        (new partition: Enter, Enter, +500M)
    #   w        (write and exit)
    sudo partprobe /dev/vdb
    lsblk /dev/vdb
  4. Practice

    Add more partitions with parted, without touching the first

    On the same disk add a second partition of 300 MiB and a third of 200 MiB, directly after the first, using parted in non-interactive mode. Confirm that partition 1 is unchanged. Do not run mklabel again.

    Check your result

    parted /dev/vdb print lists three partitions in order with no overlap. The first still starts and ends where it did.

    Hint

    parted -s /dev/vdb mkpart NAME start end with MiB units. mklabel would erase the table, so never repeat it on a disk in use.

    Solution
    sudo parted /dev/vdb unit MiB print free
    sudo parted -s /dev/vdb mkpart part2 501MiB 801MiB
    sudo parted -s /dev/vdb mkpart part3 801MiB 1001MiB
    sudo partprobe /dev/vdb
    sudo parted /dev/vdb unit MiB print
  5. Challenge

    Set partition types for LVM and swap

    Mark partition 2 as type Linux LVM and partition 3 as type Linux swap. Show the types, and explain what the type code is used for (and what happens if you leave the default).

    Check your result

    sudo fdisk -l /dev/vdb lists Linux LVM and Linux swap in the Type column. The type is a label for tools and humans; LVM and swap work without it, but tools like the installer and blkid use it.

    Hint

    In fdisk: t, choose the partition, then type lvm or swap. Or use parted: set 2 lvm on, set 3 swap on.

    Solution
    sudo fdisk /dev/vdb
    #   t, 2, lvm
    #   t, 3, swap
    #   w
    sudo partprobe /dev/vdb
    sudo fdisk -l /dev/vdb