Table of Contents

  1. Using parted for Partitioning and LVM
    1. Conceptual Flows
      1. Standard Partition
      2. LVM Partition
    2. 1. Basic Non‑Interactive Partitioning (Recommended)
      1. Notes
    3. 2. Multiple Standard Partitions
    4. 3. LVM Partition (GPT – Recommended)
    5. 4. What the LVM Flag Does
    6. 5. Full Workflow: Partition → LVM → Filesystem
    7. 6. Multiple LVM Partitions on One Disk
    8. 7. MBR (msdos) Example
    9. 8. Safe Automation Pattern (Abort if Disk Is Used)
    10. 9. Detect Disk Size Dynamically
    11. 10. One‑Liner (Exam Speed)
    12. 11. Verification Commands
    13. 12. Common Mistakes (RHCSA Killers)
    14. Summary

Using parted for Partitioning and LVM

This page documents scriptable, exam-safe usage of parted for:

  • Standard disk partitioning
  • LVM‑ready partition creation
  • Safe automation patterns

Targeted for RHEL / Alma / Rocky / Debian and RHCSA 9 / 10 preparation.

parted only creates partitions.
Filesystems and LVM are created after partitioning.


Conceptual Flows

Standard Partition

parted → partition → mkfs → mount

LVM Partition

parted → LVM-marked partition → pvcreate → vgcreate → lvcreate → mkfs

#!/bin/bash
set -e

DISK="/dev/sdb"

parted -s "$DISK" \
  mklabel gpt \
  mkpart primary xfs 1MiB 100%

partprobe "$DISK"

Notes

  • -s → script mode (non‑interactive)
  • 1MiB start → avoids alignment issues
  • 100% → uses entire disk

2. Multiple Standard Partitions

DISK=/dev/sdb

parted -s "$DISK" mklabel gpt
parted -s "$DISK" mkpart primary xfs 1MiB 2GiB
parted -s "$DISK" mkpart primary ext4 2GiB 6GiB
parted -s "$DISK" mkpart primary linux-swap 6GiB 100%

partprobe "$DISK"
mkfs.xfs  /dev/sdb1
mkfs.ext4 /dev/sdb2
mkswap    /dev/sdb3

DISK=/dev/sdb

parted -s "$DISK" \
  mklabel gpt \
  mkpart primary 1MiB 100% \
  set 1 lvm on

partprobe "$DISK"

4. What the LVM Flag Does

Disk Label Effect
GPT Sets partition type to Linux LVM
MBR Sets partition type to 0x8e
  • Helps tooling and admins identify intent
  • Not strictly required, but strongly recommended
  • RHCSA graders expect it

5. Full Workflow: Partition → LVM → Filesystem

DISK=/dev/sdb

# Partition
parted -s "$DISK" mklabel gpt
parted -s "$DISK" mkpart primary 1MiB 100%
parted -s "$DISK" set 1 lvm on
partprobe "$DISK"

# LVM
pvcreate /dev/sdb1
vgcreate vg_data /dev/sdb1
lvcreate -n lv_data -l 100%FREE vg_data

# Filesystem
mkfs.xfs /dev/vg_data/lv_data

6. Multiple LVM Partitions on One Disk

parted -s /dev/sdb mklabel gpt
parted -s /dev/sdb mkpart primary 1MiB 10GiB
parted -s /dev/sdb mkpart primary 10GiB 100%
parted -s /dev/sdb set 1 lvm on
parted -s /dev/sdb set 2 lvm on
pvcreate /dev/sdb1 /dev/sdb2
vgcreate vg_pool /dev/sdb1 /dev/sdb2

7. MBR (msdos) Example

parted -s /dev/sdb mklabel msdos
parted -s /dev/sdb mkpart primary 1MiB 100%
parted -s /dev/sdb set 1 lvm on

This sets partition type 8e.


8. Safe Automation Pattern (Abort if Disk Is Used)

DISK=/dev/sdb

if lsblk "$DISK" | grep -q part; then
  echo "Disk already partitioned — aborting"
  exit 1
fi

parted -s "$DISK" mklabel gpt
parted -s "$DISK" mkpart primary 1MiB 100%
parted -s "$DISK" set 1 lvm on
partprobe "$DISK"

9. Detect Disk Size Dynamically

SIZE=$(lsblk -b -dn -o SIZE /dev/sdb)

if (( SIZE < 20 * 1024 * 1024 * 1024 )); then
  echo "Disk too small"
  exit 1
fi

10. One‑Liner (Exam Speed)

parted -s /dev/sdb mklabel gpt mkpart primary 1MiB 100% set 1 lvm on && partprobe /dev/sdb

11. Verification Commands

lsblk
blkid
pvs
vgs
lvs

12. Common Mistakes (RHCSA Killers)

❌ Forgetting partprobe

❌ Forgetting to set the lvm flag

❌ Running pvcreate on the disk instead of the partition

❌ Creating a filesystem before LVM

❌ Using interactive fdisk in scripts


Summary

  • parted is scriptable and exam‑safe
  • Always align with 1MiB
  • Use GPT unless legacy requires MBR
  • Mark LVM partitions explicitly
  • Follow partition → LVM → filesystem order

Suitable for RHCSA 9 / 10 exam prep and real‑world automation.