Skip to main content

Extend a disk in Linux - Knowledgebase / Virtual Private Servers (VPS) - Micron21 Knowledge Hub

Extend a disk in Linux

Authors list

For steps to extend a volume in mCloud, please see this article.

If you have extended a virtual disk for a Linux VM in your hypervisor (mCloud, Proxmox Virtual Enviuronment, VMware, etc.), then you will need to go into the operating system, rescan the attached disks, and extend the volume manually. To do this, follow the below steps.

Rescan attached disks

In older Linux operating systems, you may need to re-scan your SCSI device.

To do this, SSH into the server and run the following commands to refresh the SCSI hosts to ensure that the disk increase has been detected:

  1. ls /sys/class/scsi_device/

    Take note of the output here. This will be important in the next step.  An example is below:

    # ls /sys/class/scsi_device/
    0:0:0:0  3:0:0:0

  2. Run the command below for each result from the output above, replacing the numbers to match the numbers in the output above: An example is below:

    # echo 1 > /sys/class/scsi_device/0\:0\:0\:0/device/rescan
    # echo 1 > /sys/class/scsi_device/3\:0\:0\:0/device/rescan

Ensure The Disks are ready for increase

To ensure that the disks are ready for increse, you should run the lsblk command, and ensure the following occurs:

  1. The size of the disk (in this case sda) is larger than the sum of the partitions (sda1 and sda2 in the example below)
  2. There is no swap partition as the last partition (i.e. on the same level as sda1 and sda2 in the below example - if they are inside of the LVM group on the block device, this does not matter.).
  3. Only when you have confirmed that the drive is ready to be increased should you continue.  Two example outputs are below, and will form the basis of the examples in this document:

LVM based output

# lsblk
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda               8:0    0   55G  0 disk
├─sda1            8:1    0    1G  0 part /boot
└─sda2            8:2    0   49G  0 part
├─centos-root 253:0    0   47G  0 lvm  /
└─centos-swap 253:1    0    2G  0 lvm  [SWAP]
sr0              11:0    1 1024M  0 rom

Non-LVM based output:

# lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda      8:0    0  100G  0 disk 
└─sda1   8:1    0   98G  0 part /
loop0    7:0    0    4G  0 loop /var/tmp

Increase the partition using growpart

  1. Using the package manager in the operating system, install the growpart utility.  The package is in different repositories depending on the operating system:
    1. RHEL Based (AlmaLinux, CloudLinux, Rocky Linux etc): dnf install cloud-utils-growpart
    2. Debian Based (including Ubuntu): apt install cloud-guest-utils (For older Debian systems, this may be "cloud-utils" instead of cloud-guest-utils)
  2. Increase the partition using the below command (this is based on the LVM output above) Note the spaces in the command.

    # growpart /dev/sda 2

  3. The command output will show that the partition has been changed if successful. If the growpart command fails with an error about not having a handler for GPT you will need to install the 'gdisk' package

Increase the size of the filesystem

Increasing the size of the filesystem differs whether it is part of an LVM or not.  The steps are detailed below in both scenarios:

If the disk is not part of an LVM

If the disk is not part of an LVM, the filesystem can be resized using a single command.  To determine the filesystem type, you can run df -Th and looking for the filesystem you're wanting to expand:

NOTE: Be sure to select the correct device name

  • If the filesystem is not XFS: resize2fs /dev/sda1
  • If the filesystem is XFS: xfs_growfs /dev/sda1

If the disk is part of an LVM

If the disk is part of an LVM, you will need to resize the physical volume, then when there are free PE's, resize the logical volume.  To do this, follow the steps below:

  1. Use the pvresize command to resize the physical volume.  Change the sda2 to the correct device if required:

    # pvresize /dev/sda2 
       Physical volume "/dev/sda2" changed
       1 physical volume(s) resized or updated / 0 physical volume(s) not resized

  2. Confirm that the correct Volume Group has space, by using the 'vgdisplay' command.  The group name is usually the bit before the hyphen in the lsblk listings.  In this example it is centos:

    # vgdisplay
      --- Volume group ---
      VG Name               centos
      System ID             
      Format                lvm2
      Metadata Areas        1
      Metadata Sequence No  4
      VG Access             read/write
      VG Status             resizable
      MAX LV                0
      Cur LV                2
      Open LV               2
      Max PV                0
      Cur PV                1
      Act PV                1
      VG Size               <54.00 GiB
      PE Size               4.00 MiB
      Total PE              13823
      Alloc PE / Size       12542 / 48.99 GiB
      Free  PE / Size       1281 / 5.00 GiB
      VG UUID               P4eyuh-qXt3-IkpK-9F6n-FIeC-TiRq-wpFJws

  3. Use the lvextend command below to resize both the logical volume and underlying filesystem, ensuring you replace the centos-root with the name of the fs you want to extend.  this can be found in the lsblk output above.

    # lvextend -r -l +100%FREE /dev/mapper/centos-root
      Size of logical volume centos/root changed from 46.99 GiB (12030 extents) to <52.00 GiB (13311 extents).
      Logical volume centos/root successfully resized.

  4. Verify that the increase has been successful using the df -h command.
Helpful Unhelpful