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.
1. Command Overview
Section titled “1. Command Overview”- 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.
2. Basic Syntax
Section titled “2. Basic Syntax”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.
3. Practical Examples
Section titled “3. Practical Examples”Disk Management:
-
List block devices:
Terminal window lsblkNAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTsda 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 disksdc 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/sdb1with ext4:Terminal window mkfs.ext4 /dev/sdb1 -
Mount the partition
/dev/sdb1to/mnt/data:Terminal window mkdir -p /mnt/datamount /dev/sdb1 /mnt/data -
Add an entry to
/etc/fstabfor 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/sdband/dev/sdc:Terminal window pvcreate /dev/sdbpvcreate /dev/sdcPhysical 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/sdcVolume group "data_vg" successfully created -
Create a Logical Volume (LV) named
data_lvof size 8GB:Terminal window lvcreate -L 8G -n data_lv data_vgLogical volume "data_lv" created. -
Format the LV
/dev/data_vg/data_lvwith ext4:Terminal window mkfs.ext4 /dev/data_vg/data_lv -
Mount the LV
/dev/data_vg/data_lvto/mnt/data:Terminal window mkdir -p /mnt/datamount /dev/data_vg/data_lv /mnt/data -
Extend the LV
data_lvby 2GB:Terminal window lvextend -L +2G /dev/data_vg/data_lvresize2fs /dev/data_vg/data_lv # Resize the filesystem -
Reduce the LV
data_lvby 1GB (WARNING: DATA LOSS POSSIBLE! BACKUP FIRST!)Terminal window umount /mnt/data # Unmount the LV before reducinge2fsck -f /dev/data_vg/data_lv # Check for filesystem errorsresize2fs /dev/data_vg/data_lv 9G # Resize the filesystem to the new sizelvreduce -L 9G /dev/data_vg/data_lvmount /dev/data_vg/data_lv /mnt/data # Remount the LV -
Remove the LV
data_lv:Terminal window umount /mnt/datalvremove /dev/data_vg/data_lv -
Remove the VG
data_vg:Terminal window vgremove data_vg -
Remove the PVs
/dev/sdband/dev/sdc:Terminal window pvremove /dev/sdbpvremove /dev/sdc
4. Common Options
Section titled “4. Common Options”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.
5. Advanced Usage
Section titled “5. Advanced Usage”LVM:
-
Creating Snapshots:
Terminal window lvcreate -s -L 2G -n data_snap -p r /dev/data_vg/data_lv # Create a snapshotmount /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 poollvconvert --thinpool data_vg/thinpool --yeslvcreate -T data_vg/thinpool -V 10G -n thinvol1 # Create a thin volumelvcreate -T data_vg/thinpool -V 10G -n thinvol2 # Create another thin volumemkfs.ext4 /dev/data_vg/thinvol1mkfs.ext4 /dev/data_vg/thinvol2mount /dev/data_vg/thinvol1 /mnt/thin1mount /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/sdcvgreduce 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 VGvgchange -a n data_vg # Deactivate the VG-a y: Activate the VG.-a n: Deactivate the VG.
6. Tips & Tricks
Section titled “6. Tips & Tricks”- Tab Completion: Use tab completion for device names, VG names, and LV names.
watchcommand: Usewatch -n 1 lvsorwatch -n 1 pvsto monitor LVM changes in real-time.pvresize: Usepvresizeafter 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
ddto create disk images, or use LVM snapshots. - Filesystem Check: Always run
fsckon 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.conffor theauto_activationsetting. - 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.
7. Troubleshooting
Section titled “7. Troubleshooting”- “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
fsckon 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/fstabfor correct entries. - Verify that the
lvm2-activationservice is running.
- Stale Physical Volumes: If a PV is no longer available (e.g., disk failure), you may need to use
vgreduce --removemissingto remove it from the VG. WARNING: Data loss is possible if the PV contained important data.
8. Related Commands
Section titled “8. Related Commands”xfs_growfs: Resize an XFS filesystem.resize2fs: Resize an ext2/3/4 filesystem.gdisk: GPT disk partitioner, alternative tofdiskfor 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.