Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ashiwanikumar/08c9c05f354991dea3e1808807fd4f6c to your computer and use it in GitHub Desktop.
Save ashiwanikumar/08c9c05f354991dea3e1808807fd4f6c to your computer and use it in GitHub Desktop.
Complete Guide on Extending LVM with a New Disk and Reconfiguring OS Disk

Complete Guide on Extending LVM with a New Disk and Reconfiguring OS Disk


Step 1: Scan for New Disks

To detect newly added disks without rebooting, use the following command:

echo "- - -" > /sys/class/scsi_host/host32/scan

Replace host32 with the appropriate host number if different on your system.

Step 2: Prepare the New Disk

Create a physical volume on the new disk:

sudo pvcreate /dev/sdx  # Replace /dev/sdx with your new disk identifier

Step 3: Extend Volume Group

Add the new physical volume to your existing volume group:

sudo vgextend your-vg-name /dev/sdx  # Replace your-vg-name and /dev/sdx appropriately

Step 4: Extend Logical Volume

Allocate the newly available space to the logical volume:

sudo lvextend -l +100%FREE /dev/your-vg-name/your-lv-name  # Replace with your volume group and logical volume names

Step 5: Resize the Filesystem

Resize the filesystem to use the new space on the logical volume:

sudo resize2fs /dev/your-vg-name/your-lv-name  # Adjust as necessary, assuming an ext4 filesystem

Step 6: Copying Data to the New Logical Volume

Before manipulating /var, ensure all data is backed up and services that write to /var are stopped:

sudo systemctl stop nginx zabbix-server grafana-server prometheus

Mount the new logical volume temporarily to /mnt and copy all data:

sudo mount /dev/your-vg-name/your-lv-name /mnt
sudo rsync -avxHAX /var/ /mnt/

Update /etc/fstab to use the new logical volume for /var:

# Open /etc/fstab with an editor, e.g., nano
sudo nano /etc/fstab
# Add or modify the line for /var
/dev/your-vg-name/your-lv-name /var ext4 defaults 0 2

Unmount and remount to verify:

sudo umount /mnt
sudo mount -a

Restart the stopped services:

sudo systemctl start nginx zabbix-server grafana-server prometheus

Step 7: Deleting an OS Disk Partition and Adding to LVM

a. Delete the Partition

Using fdisk or parted, remove the OS disk partition (e.g., /dev/sda3):

sudo fdisk /dev/sda

In fdisk, use d to delete and w to write changes to the disk.

b. Update the Kernel's Partition Table

Inform the kernel of the changes to the partition table:

sudo partprobe /dev/sda
c. Create a Physical Volume

Once the partition is deleted, convert it into a physical volume:

sudo pvcreate /dev/sda3
d. Extend the Volume Group and Logical Volume

Extend your volume group and logical volume as described in Steps 3 and 4.

Step 8: Verify Changes

Check the new size of the filesystem:

df -h /var

@ashiwanikumar
Copy link
Author

# Extend an LVM Logical Volume

If you have created or extended a physical volume (for example, `/dev/sdb2`) and added it to an existing volume group (such as `vgdata`), follow these steps to grow a logical volume (for example, `lvdata`) to use the newly available space.

## 1. Check Available Free Space in the Volume Group

```bash
vgs

This command shows the volume groups and how much free space (VFree) is available.

2. Extend the Logical Volume

Use the free space in vgdata to extend lvdata. To use all available free space:

lvextend -l +100%FREE /dev/vgdata/lvdata

If you only want to extend by a specific size (e.g., 200GB), use:

lvextend -L +200G /dev/vgdata/lvdata

3. Resize the Filesystem

After extending the LV, resize the filesystem to make use of the additional space.

If Using ext4:

resize2fs /dev/vgdata/lvdata

If Using XFS:

xfs_growfs /mountpoint

Replace /mountpoint with the actual mount location of /dev/vgdata/lvdata.

4. Verify the New Size

Check the filesystem size:

df -hT /mountpoint

Optionally, confirm the updated logical volume size:

lvdisplay /dev/vgdata/lvdata

You should now see the extended size of your logical volume and filesystem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment