Skip to content

Instantly share code, notes, and snippets.

@vi7
Last active September 28, 2021 10:31
Show Gist options
  • Save vi7/173b6253821433012dad6df46c237f4b to your computer and use it in GitHub Desktop.
Save vi7/173b6253821433012dad6df46c237f4b to your computer and use it in GitHub Desktop.
Resizing existing partition on Linux using parted and LVM

Resizing existing partition on Linux

This guide was tested on CentOS 7 but should be in general applicable for other Linux distros with parted and LVM

WARN You can’t resize partitions on the running OS in CentOS 6 (unless you’re brave enough to mess with the sfdisk) So the fastest option is to add new disk partition, create LVM PV from it and extend needed LVM VG accordingly Bad luck for you if you’re not using LVM ☠️

  1. Add required amount of space to the virtual machine disk

  2. Force Linux to rescan disk and load its new size - example for the /dev/sda and CentOS 7:

    echo 1 > /sys/block/sda/device/rescan
  3. Check that free space is detected - example for the /dev/sda:

    # Check free space
    parted /dev/sda print free
    
    # Example output:
    Model: VMware Virtual disk (scsi)                                                                                                                                                        
    Disk /dev/sda: 429GB                                                                                                                                                                     
    Sector size (logical/physical): 512B/512B                                                                                                                                                
    Partition Table: msdos                                                                                                                                                                   
    Disk Flags: 
    
    Number  Start   End     Size    Type     File system  Flags
            32.3kB  1049kB  1016kB           Free Space
     1      1049kB  1075MB  1074MB  primary  xfs          boot
     2      1075MB  268GB   267GB   primary               lvm
            268GB   429GB   161GB            Free Space    <<< DETECTED FREE SPACE
  4. Resize partition - in this example using all the free space (161GB or 150GiB) to resize /dev/sda2:

    parted -a optimal /dev/sda resizepart 2 429GB

If you just need to resize a partition then you’re done, if you need to resize the LVM disk - see the next steps below.

  1. Resize LVM PV and check:

    # Resize
    pvresize /dev/sda2
    # Check
    pvs
  2. Resize LV and underlying FS (enabled by the option -r). Add 100GiB to the LV (leaving 50GiB free for the future usage) and check - example for the lv_data:

    lvresize /dev/vg00/lv_data -r -L +100G
    # Check
    lvs

6a. Alternatively you can add all the available free space to the LV by doing so: bash lvresize /dev/vg00/lv_data -r -l +100%FREE # Check lvs

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