Skip to content

Instantly share code, notes, and snippets.

@SecurityPhoton
Created November 12, 2024 15:31
Show Gist options
  • Select an option

  • Save SecurityPhoton/628417847cf8a55201d7393922bf6be0 to your computer and use it in GitHub Desktop.

Select an option

Save SecurityPhoton/628417847cf8a55201d7393922bf6be0 to your computer and use it in GitHub Desktop.
RESIZE (EXTEND) DISK IN LINUX 🐧

RESIZE (EXTEND) DISK IN LINUX 🐧

Summary of high level steps

  • Resize the partition using fdisk.
  • Extend the physical volume with pvresize.
  • Expand the logical volume with lvextend.
  • Resize the filesystem with resize2fs.

Asume we have added HW or virtual disk in Hypervisor. Make snapshot if possible before any action just to sleep well 😴.

OS: Ubuntu \ Debian

Steps for scenario without LVM πŸ’Ύ

1. Add Space in HW

Ensure that you have added additional disk space via your hardware or hypervisor settings.

2. Open the Disk

df -h
sudo fdisk -l
sudo fdisk /dev/sdX

3. Repartition without data loss

a) Type p to view the partition table and note down the partition number (e.g., /dev/sda3).

b) Type d to delete the existing partition (don’t worry, this doesn’t delete data).

c) Type n to create a new partition. Choose Primary (usually option p) and specify the same starting sector as before. This ensures no data is lost. Press Enter for the default (which will use the full available size).

πŸ“’ If we get the message:

Created a new partition 3 of type 'Linux filesystem' and of size 46.9 GiB.
Partition #3 contains a LVM2_member signature.		
Do you want to remove the signature? [Y]es/[N]o: 

Select n !!! Do not remove signature ☠️ !!!

d) Type w to write changes and exit.

4. Refresh the partition table

You may need to inform the kernel of the partition changes:

sudo partprobe /dev/sdX

5. Resize the filesystem on the partition

If you are using ext4:

sudo resize2fs /dev/sdX1

6. Verify the new size πŸ“

df -h
lsblk

Steps for scenario with LVM 🎒

1. Add Space in HW

Ensure that you have added additional disk space via your hardware or hypervisor settings.

2. Open the Disk

df -h
sudo fdisk -l
sudo fdisk /dev/sdX

3. Repartition without data loss

a) Type p to view the partition table and note down the partition number (e.g., /dev/sda3).

b) Type d to delete the existing partition (don’t worry, this doesn’t delete data).

c) Type n to create a new partition. Choose Primary (usually option p) and specify the same starting sector as before. This ensures no data is lost. Press Enter for the default (which will use the full available size).

πŸ“’ If we get the message:

Created a new partition 3 of type 'Linux filesystem' and of size 46.9 GiB.
Partition #3 contains a LVM2_member signature.		
Do you want to remove the signature? [Y]es/[N]o: 

Select n !!! Do not remove signature ☠️!!!

d) Type w to write changes and exit.

4. Refresh the partition table

You may need to inform the kernel of the partition changes:

sudo partprobe /dev/sdX

5. Extend the physical volume

sudo pvresize /dev/sda3

6. Check how much free space is now available in your volume group

vgdisplay

Look for the "Free PE / Size" field, which indicates the amount of space you can allocate.

7. Extend the logical volume

Then extend the logical volume using all or part of the available free space (replace ubuntu-vg and ubuntu-lv with your volume group and logical volume names, if different):

sudo lvextend -l +100%FREE /dev/ubuntu-vg/ubuntu-lv

8. Resize the filesystem on the logical volume

Finally, resize the filesystem on the logical volume to make use of the new space:

sudo resize2fs /dev/ubuntu-vg/ubuntu-lv

9. Verify the new size πŸ“

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