Skip to content

Instantly share code, notes, and snippets.

@therayyanawaz
Last active March 28, 2025 06:01
Show Gist options
  • Select an option

  • Save therayyanawaz/93d9e1c34a2fa61641e674d854bc7516 to your computer and use it in GitHub Desktop.

Select an option

Save therayyanawaz/93d9e1c34a2fa61641e674d854bc7516 to your computer and use it in GitHub Desktop.
This guide covers installing LVM tools, partitioning a disk, preparing it as an LVM physical volume, troubleshooting usage issues, and extending an existing LVM logical volume with filesystem resizing.

Comprehensive Guide to Disk Configuration, LVM Setup, and Resizing

Prerequisites

Before starting, ensure the following:

  1. Root Privileges: You’ll need sudo or root access to execute these commands.
  2. Identify the Disk: Know the target disk/partition (e.g., /dev/sdb or /dev/sdb1). Use lsblk or fdisk -l to confirm.
  3. Backup Data: Back up any critical data on the disk, as partitioning or wiping signatures will erase existing content.
  4. Volume Group Space: For extending a logical volume (e.g., /dev/my_vg/my_lv), ensure the volume group (VG) has enough free space (check with vgdisplay).
  5. Correct Device Names: Replace placeholders like /dev/diskname and /dev/my_vg/my_lv with your actual device paths and LVM names (use lvs to list logical volumes).
  6. Filesystem Tools: If resizing a filesystem, ensure the appropriate tool is installed (e.g., resize2fs for ext2/3/4, included in e2fsprogs; xfsprogs for XFS).
  7. Unmounted State (If Needed): Some steps (e.g., fsck, resize2fs on older systems) may require the target to be unmounted. Check with df -h.

1. Install LVM2 Package

sudo apt install lvm2
  • Purpose: Installs the lvm2 package for LVM management tools.
  • Details: Ensures commands like pvcreate, lvextend, and pvs are available.

2. List Disks and Partitions

sudo fdisk -l
  • Purpose: Displays all disks and partitions.
  • Details: Use this to identify your target disk (e.g., /dev/sdb) before proceeding.

3. Partition the Disk

sudo fdisk /dev/diskname
  • Purpose: Modifies the partition table of the specified disk (e.g., /dev/sdb).
  • Sub-Steps:
    • Create a New Partition:
      • Type n (new) and press Enter.
      • Select partition type (e.g., p for primary) and number (e.g., 1).
      • Set first sector (press Enter for default).
      • Set last sector (press Enter for all space, or specify like +20G).
    • Set Partition Type:
      • Type t (change type) and press Enter.
      • Enter 8e (Linux LVM).
    • Save Changes:
      • Type w to write and exit.
  • Details: Prepares a partition (e.g., /dev/sdb1) for LVM.

4. Create a Physical Volume (Initial Attempt)

sudo pvcreate /dev/diskname
  • Purpose: Initializes the disk/partition as an LVM physical volume (PV).
  • Details: May fail if the device has existing data; proceed to troubleshoot if needed.

5. Unmount the Disk (If Mounted)

sudo umount /dev/diskname
  • Purpose: Unmounts the device if it’s in use.
  • Details: Corrects your typo (umount, not umonth). Ensures the disk is free.

6. Check Swap Usage

cat /proc/swaps
  • Purpose: Lists active swap areas.
  • Details: If /dev/diskname is a swap partition, it must be disabled.

7. Disable Swap (If Active)

sudo swapoff /dev/diskname
  • Purpose: Deactivates swap on the device.
  • Details: Frees the partition for other uses.

8. Identify Processes Using the Disk

sudo lsof | grep /dev/diskname
  • Purpose: Lists processes locking the device.
  • Details: Troubleshoots if umount or swapoff fails.

9. Terminate Interfering Processes

pkill gvfsd
pkill gvfs-afc
  • Purpose: Kills processes (e.g., GNOME daemons) locking the disk.
  • Details: Adjust based on lsof output if different processes are found.

10. Verify Physical Volumes

sudo pvs
  • Purpose: Lists all LVM physical volumes.
  • Details: Checks if /dev/diskname is recognized as a PV.

11. Wipe Existing Signatures

sudo wipefs -a /dev/diskname
  • Purpose: Removes all filesystem, swap, or LVM signatures.
  • Details: Ensures pvcreate succeeds by clearing residual metadata.

12. Create a Physical Volume (Final Attempt)

sudo pvcreate /dev/diskname
  • Purpose: Initializes the device as an LVM physical volume.
  • Details: Should now succeed; the device can join a volume group.

13. List Block Devices and Filesystems

lsblk -f
  • Purpose: Shows block devices, partitions, and filesystem types.
  • Details: Verifies the disk’s structure and LVM status.

14. Check Disk Usage

df -h
  • Purpose: Displays mounted filesystems and their sizes.
  • Details: Confirms mount points and usage.

15. Check and Repair Filesystem (If Needed)

fsck /dev/diskname
  • Purpose: Checks and repairs the filesystem.
  • Details: Use only if the partition had a prior filesystem needing repair. Skip for new LVM PVs.

Extending an LVM Logical Volume and Resizing the Filesystem

To expand an existing logical volume and its filesystem (e.g., adding 50GB):

16. Extend the Logical Volume

lvextend -L +50G /dev/my_vg/my_lv
  • Purpose: Increases the logical volume size by 50GB.
  • Details:
    • -L +50G: Adds 50GB (use -L 50G for an absolute size).
    • Replace /dev/my_vg/my_lv with your VG and LV names (check with lvs).
    • Verify VG free space with vgdisplay.

17. Resize the Filesystem

resize2fs /dev/my_vg/my_lv
  • Purpose: Expands the ext2/ext3/ext4 filesystem to use the new LV size.
  • Details:
    • Corrects your typo (resize2fs, not resice2fs).
    • Can often run online if mounted; otherwise, unmount first (umount /mount/point).
    • For XFS, use xfs_growfs /mount/point instead.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment