Skip to content

Disk Management and LVM

Category: Advanced Linux Administration
Type: Linux Commands
Generated on: 2025-07-10 03:14:07
For: System Administration, Development & Technical Interviews


Disk Management and LVM Cheatsheet (Linux)

Section titled “Disk Management and LVM Cheatsheet (Linux)”

This cheatsheet provides a comprehensive overview of disk management and Logical Volume Management (LVM) commands in Linux, useful for both beginners and experienced system administrators.


  • Disk Management: Deals with physical disks and partitions. Includes commands for listing, creating, formatting, and mounting partitions.
  • LVM (Logical Volume Management): Provides a layer of abstraction over physical disks, allowing for flexible management of storage space. LVM enables easy resizing, snapshotting, and striping of volumes.

Disk Management:

  • fdisk <disk>: Partition table manipulator.
  • parted <disk>: Advanced partition editor.
  • mkfs.<fstype> <device>: Create a filesystem on a partition (e.g., mkfs.ext4 /dev/sda1).
  • mount <device> <mountpoint>: Mount a filesystem.
  • umount <mountpoint>: Unmount a filesystem.
  • df -h: Display disk space usage.
  • du -sh <directory>: Display directory size.
  • lsblk: List block devices.
  • blkid: Display block device attributes (UUID, TYPE).

LVM:

  • pvcreate <device>: Create a Physical Volume (PV).
  • vgcreate <vgname> <pv>: Create a Volume Group (VG).
  • lvcreate -L <size> -n <lvname> <vgname>: Create a Logical Volume (LV).
  • pvdisplay <pv>: Display PV information.
  • vgdisplay <vg>: Display VG information.
  • lvdisplay <lv>: Display LV information.
  • lvextend -L <size> <lv>: Extend an LV.
  • lvreduce -L <size> <lv>: Reduce an LV (requires filesystem resize).
  • lvremove <lv>: Remove an LV.
  • vgremove <vg>: Remove a VG.
  • pvremove <pv>: Remove a PV.
  • vgs: Summary of Volume Groups.
  • lvs: Summary of Logical Volumes.
  • pvs: Summary of Physical Volumes.
  • lvresize: Resize a logical volume (requires filesystem resize).
  • vgextend: Add a physical volume to a volume group.
  • vgreduce: Remove a physical volume from a volume group.

Disk Management:

  • List block devices:

    Terminal window
    lsblk
    NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
    sda 8:0 0 20G 0 disk
    ├─sda1 8:1 0 500M 0 part /boot/efi
    ├─sda2 8:2 0 1G 0 part /boot
    └─sda3 8:3 0 18.5G 0 part
    └─vg0-root 253:0 0 18.5G 0 lvm /
    sdb 8:16 0 10G 0 disk
    sdc 8:32 0 10G 0 disk
  • Create a partition table (GPT) on /dev/sdb:

    Terminal window
    parted /dev/sdb mklabel gpt
  • Create a new partition using parted (1GB, starting at 1MB):

    Terminal window
    parted /dev/sdb mkpart primary ext4 1MB 1025MB
  • Format the partition /dev/sdb1 with ext4:

    Terminal window
    mkfs.ext4 /dev/sdb1
  • Mount the partition /dev/sdb1 to /mnt/data:

    Terminal window
    mkdir -p /mnt/data
    mount /dev/sdb1 /mnt/data
  • Add an entry to /etc/fstab for persistent mounting:

    Terminal window
    echo "/dev/sdb1 /mnt/data ext4 defaults 0 0" | sudo tee -a /etc/fstab
  • Unmount /mnt/data:

    Terminal window
    umount /mnt/data

LVM:

  • Create Physical Volumes (PVs) on /dev/sdb and /dev/sdc:

    Terminal window
    pvcreate /dev/sdb
    pvcreate /dev/sdc
    Physical volume "/dev/sdb" successfully created.
    Physical volume "/dev/sdc" successfully created.
  • Create a Volume Group (VG) named data_vg:

    Terminal window
    vgcreate data_vg /dev/sdb /dev/sdc
    Volume group "data_vg" successfully created
  • Create a Logical Volume (LV) named data_lv of size 8GB:

    Terminal window
    lvcreate -L 8G -n data_lv data_vg
    Logical volume "data_lv" created.
  • Format the LV /dev/data_vg/data_lv with ext4:

    Terminal window
    mkfs.ext4 /dev/data_vg/data_lv
  • Mount the LV /dev/data_vg/data_lv to /mnt/data:

    Terminal window
    mkdir -p /mnt/data
    mount /dev/data_vg/data_lv /mnt/data
  • Extend the LV data_lv by 2GB:

    Terminal window
    lvextend -L +2G /dev/data_vg/data_lv
    resize2fs /dev/data_vg/data_lv # Resize the filesystem
  • Reduce the LV data_lv by 1GB (WARNING: DATA LOSS POSSIBLE! BACKUP FIRST!)

    Terminal window
    umount /mnt/data # Unmount the LV before reducing
    e2fsck -f /dev/data_vg/data_lv # Check for filesystem errors
    resize2fs /dev/data_vg/data_lv 9G # Resize the filesystem to the new size
    lvreduce -L 9G /dev/data_vg/data_lv
    mount /dev/data_vg/data_lv /mnt/data # Remount the LV
  • Remove the LV data_lv:

    Terminal window
    umount /mnt/data
    lvremove /dev/data_vg/data_lv
  • Remove the VG data_vg:

    Terminal window
    vgremove data_vg
  • Remove the PVs /dev/sdb and /dev/sdc:

    Terminal window
    pvremove /dev/sdb
    pvremove /dev/sdc

Disk Management:

  • fdisk:
    • n: Create a new partition.
    • d: Delete a partition.
    • p: Print the partition table.
    • w: Write changes to disk. USE WITH CAUTION!
    • q: Quit without saving.
  • mkfs:
    • -t <fstype>: Specify the filesystem type (e.g., ext4, xfs, btrfs).
    • -L <label>: Set the volume label.
  • mount:
    • -t <fstype>: Specify the filesystem type.
    • -o <options>: Mount options (e.g., ro, noatime, defaults).
  • df:
    • -h: Human-readable format.

LVM:

  • lvcreate:
    • -L <size>: Specify the size of the LV (e.g., 10G, 500M).
    • -n <lvname>: Specify the name of the LV.
    • -l <extents>: Specify the size in logical extents (instead of size).
    • -T: Create a thin pool logical volume.
  • lvextend:
    • -L <size>: Specify the new size of the LV. Use + to add to the existing size (e.g., +2G).
    • -r: Resize the filesystem automatically after extending the LV.
  • lvreduce:
    • -L <size>: Specify the new size of the LV. Use - to subtract from the existing size. WARNING: DATA LOSS POSSIBLE!
  • vgcreate:
    • -s <size>: Specify the physical extent size (PE size). Smaller PE sizes allow for finer-grained allocation, but can increase metadata overhead.
  • lvdisplay, vgdisplay, pvdisplay:
    • -v: Verbose output.

LVM:

  • Creating Snapshots:

    Terminal window
    lvcreate -s -L 2G -n data_snap -p r /dev/data_vg/data_lv # Create a snapshot
    mount /dev/data_vg/data_snap /mnt/snapshot
    # Make changes to the original volume /dev/data_vg/data_lv
    # To revert to the snapshot: umount /mnt/data; lvconvert --merge /dev/data_vg/data_snap
    • -s: Create a snapshot.
    • -p r: Set read-only permissions on the snapshot.
    • lvconvert --merge: Merge the snapshot back into the original volume. WARNING: This is a destructive operation!
  • Creating Thin Pools and Thin Volumes:

    Terminal window
    lvcreate -L 100G -n thinpool data_vg # Create a thin pool
    lvconvert --thinpool data_vg/thinpool --yes
    lvcreate -T data_vg/thinpool -V 10G -n thinvol1 # Create a thin volume
    lvcreate -T data_vg/thinpool -V 10G -n thinvol2 # Create another thin volume
    mkfs.ext4 /dev/data_vg/thinvol1
    mkfs.ext4 /dev/data_vg/thinvol2
    mount /dev/data_vg/thinvol1 /mnt/thin1
    mount /dev/data_vg/thinvol2 /mnt/thin2
    • -T: Create a thin pool LV.
    • -V: Virtual size of the thin volume. The actual space used will be less until data is written.
    • Thin pools enable over-provisioning.
  • LVM Striping (Improved Performance):

    Terminal window
    lvcreate -i 2 -I 64 -L 10G -n striped_lv data_vg # Striped across 2 PVs, 64KB stripe size
    • -i <number>: Number of stripes.
    • -I <size>: Stripe size in KB.
  • Migrating Physical Volumes:

    Terminal window
    pvmove /dev/sdb /dev/sdc # Move data from /dev/sdb to /dev/sdc
    vgreduce data_vg /dev/sdb # Remove /dev/sdb from the VG after successful migration
  • Activating/Deactivating Volume Groups:

    Terminal window
    vgchange -a y data_vg # Activate the VG
    vgchange -a n data_vg # Deactivate the VG
    • -a y: Activate the VG.
    • -a n: Deactivate the VG.
  • Tab Completion: Use tab completion for device names, VG names, and LV names.
  • watch command: Use watch -n 1 lvs or watch -n 1 pvs to monitor LVM changes in real-time.
  • pvresize: Use pvresize after resizing a partition that contains a PV. This updates the PV’s size to match the partition.
  • Backup: Always back up important data before performing any disk management or LVM operations. Use dd to create disk images, or use LVM snapshots.
  • Filesystem Check: Always run fsck on a filesystem after resizing or if you suspect corruption.
  • LVM Auto-activation: Ensure LVM is configured to automatically activate volume groups on boot. Check /etc/lvm/lvm.conf for the auto_activation setting.
  • Logical Extent Size: The logical extent size is crucial in LVM. Once a VG is created with a specific extent size, it cannot be changed. Smaller extent sizes allow for more granular control but increase metadata overhead.
  • pvs -o +pe_start: View the starting physical extent for each PV within a VG.
  • “Device or resource busy”: The device is likely mounted. Unmount it before attempting to modify it.
  • “Invalid argument”: Check the syntax of your command. Double-check device names and sizes.
  • “Volume Group not found”: The VG may not be activated. Use vgchange -a y <vgname>. Also, ensure LVM is properly initialized on boot.
  • “Filesystem needs to be checked”: Run fsck on the filesystem.
  • Data Loss: Always back up your data before resizing, reducing, or removing volumes. Double-check your commands before executing them.
  • LVM Not Activating on Boot:
    • Ensure LVM is enabled in your initramfs.
    • Check /etc/fstab for correct entries.
    • Verify that the lvm2-activation service is running.
  • Stale Physical Volumes: If a PV is no longer available (e.g., disk failure), you may need to use vgreduce --removemissing to remove it from the VG. WARNING: Data loss is possible if the PV contained important data.
  • xfs_growfs: Resize an XFS filesystem.
  • resize2fs: Resize an ext2/3/4 filesystem.
  • gdisk: GPT disk partitioner, alternative to fdisk for GPT disks.
  • lvmdiskscan: Scan for LVM physical volumes.
  • pvck, vgck, lvck: LVM consistency check tools.
  • cryptsetup: For encrypting disks and partitions.
  • mdadm: Software RAID management.
  • systemd-mount: Mount filesystems using systemd units.
  • wipefs: Clear filesystem, RAID or partition-table signatures from a device.

This cheatsheet provides a solid foundation for managing disks and LVM in Linux. Remember to always back up your data and carefully review commands before execution, especially when dealing with destructive operations.