Before starting, ensure the following:
- Root Privileges: You’ll need
sudoor root access to execute these commands. - Identify the Disk: Know the target disk/partition (e.g.,
/dev/sdbor/dev/sdb1). Uselsblkorfdisk -lto confirm. - Backup Data: Back up any critical data on the disk, as partitioning or wiping signatures will erase existing content.
- 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 withvgdisplay). - Correct Device Names: Replace placeholders like
/dev/disknameand/dev/my_vg/my_lvwith your actual device paths and LVM names (uselvsto list logical volumes). - Filesystem Tools: If resizing a filesystem, ensure the appropriate tool is installed (e.g.,
resize2fsfor ext2/3/4, included ine2fsprogs;xfsprogsfor XFS). - Unmounted State (If Needed): Some steps (e.g.,
fsck,resize2fson older systems) may require the target to be unmounted. Check withdf -h.
sudo apt install lvm2- Purpose: Installs the
lvm2package for LVM management tools. - Details: Ensures commands like
pvcreate,lvextend, andpvsare available.
sudo fdisk -l- Purpose: Displays all disks and partitions.
- Details: Use this to identify your target disk (e.g.,
/dev/sdb) before proceeding.
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.,
pfor 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).
- Type
- Set Partition Type:
- Type
t(change type) and press Enter. - Enter
8e(Linux LVM).
- Type
- Save Changes:
- Type
wto write and exit.
- Type
- Create a New Partition:
- Details: Prepares a partition (e.g.,
/dev/sdb1) for LVM.
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.
sudo umount /dev/diskname- Purpose: Unmounts the device if it’s in use.
- Details: Corrects your typo (
umount, notumonth). Ensures the disk is free.
cat /proc/swaps- Purpose: Lists active swap areas.
- Details: If
/dev/disknameis a swap partition, it must be disabled.
sudo swapoff /dev/diskname- Purpose: Deactivates swap on the device.
- Details: Frees the partition for other uses.
sudo lsof | grep /dev/diskname- Purpose: Lists processes locking the device.
- Details: Troubleshoots if
umountorswapofffails.
pkill gvfsd
pkill gvfs-afc- Purpose: Kills processes (e.g., GNOME daemons) locking the disk.
- Details: Adjust based on
lsofoutput if different processes are found.
sudo pvs- Purpose: Lists all LVM physical volumes.
- Details: Checks if
/dev/disknameis recognized as a PV.
sudo wipefs -a /dev/diskname- Purpose: Removes all filesystem, swap, or LVM signatures.
- Details: Ensures
pvcreatesucceeds by clearing residual metadata.
sudo pvcreate /dev/diskname- Purpose: Initializes the device as an LVM physical volume.
- Details: Should now succeed; the device can join a volume group.
lsblk -f- Purpose: Shows block devices, partitions, and filesystem types.
- Details: Verifies the disk’s structure and LVM status.
df -h- Purpose: Displays mounted filesystems and their sizes.
- Details: Confirms mount points and usage.
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.
To expand an existing logical volume and its filesystem (e.g., adding 50GB):
lvextend -L +50G /dev/my_vg/my_lv- Purpose: Increases the logical volume size by 50GB.
- Details:
-L +50G: Adds 50GB (use-L 50Gfor an absolute size).- Replace
/dev/my_vg/my_lvwith your VG and LV names (check withlvs). - Verify VG free space with
vgdisplay.
resize2fs /dev/my_vg/my_lv- Purpose: Expands the ext2/ext3/ext4 filesystem to use the new LV size.
- Details:
- Corrects your typo (
resize2fs, notresice2fs). - Can often run online if mounted; otherwise, unmount first (
umount /mount/point). - For XFS, use
xfs_growfs /mount/pointinstead.
- Corrects your typo (