Skip to content

How to extend an existing drive in Linux?

A brief summary of the commands:

Command Description
lsblk list block devices
pvs list physical volumes
vgs list volume groups
lvs list logical volumes
fdisk -l view disk partitions in detail
dnf -y install cloud-utils-growpart install growpart utility for Fedora, CentOS, RHEL, Oracle Linux, Alma Linux, Rocky Linux;
apt -y install cloud-guest-utils install growpart utility for Ubuntu, Debian
growpart /dev/sda 2 extend /dev/sda2 partition
pvresize /dev/sda2 extend existing physical volume /dev/sda2
lvextend -r -l +100%FREE /dev/centos/root extend logical volume /dev/centos/root from centos volume group
df -hT list partitions and file systems
xfs_growfs /dev/mapper/centos-root increase the size of the XFS file system on the /dev/mapper/centos-root logical volume (for Fedora, CentOS, RHEL, Oracle Linux, Alma Linux, Rocky Linux)
resize2fs /dev/mapper/ubuntu-root increase the size of the EXT4 file system on the logical volume /dev/mapper/ubuntu-root (for Ubuntu, Debian)

Info

For the btrfs file system, you should use different instructions.

This example will describe how to expand an existing partition on a disk using 15Gb of free disk space from the unallocated area of the same disk. The extension will be performed on the example of the CentOS distribution.
This manual is suitable for most Linux distributions, gives detailed information on how to correctly expand disk space, and takes into account the features of the commands of various distributions.

Note

All commands must be run as root or superuser (sudo).
When following this instruction, there is a risk of data loss, so it is recommended to make a backup of the virtual machine first.

Step 1. Analysis of disk space configuration

  1. First, you need to determine the name of the partition that you are going to expand and the amount of unallocated area on the device (disk) using the command lsblk.

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

    In this example, we see that the system has 1 device (disk) /dev/sda with a size of 45Gb.
    There are 2 partitions on the disk with a total volume of 30G:

    • main /dev/sda1 - 1Gb,
    • extended /dev/sda2 - 29Gb).
      The remaining 15Gb remain in the unallocated area.

    We also see that the /dev/sda2 (29Gb) partition is an extended LVM partition, where there is a centos volume group and 2 root and swap logical volumes. We will expand the root logical volume on the extended dev/sda2 partition using free 15Gb from the unallocated area of the sda disk.

  2. Then you should check the existing LVM configuration with the commands:
    pvs - display the name and number of available physical volumes (PV)
    vgs - list volume groups (VG)
    lvs - list logical volumes (LV)

    [root@centos~]# pvs  
    PV         VG     Fm   Attr PSize    PFree    
    dev/sda    centos lvm2 a--  <29.00g  0      
    
    [root@centos~]# vgs  
    VG         #PV    #LV  #SN  Attr   VSize    PFree  
    dev/sda    centos lvm2 0    wz--n  <29.00g  0  
    
    [root@centos~]# lvs  
    LG         VG      Attr       LSize    
    root       centos  -wi-ao---- <28.00g  
    swap       centos  -wi-ao---- 1.00g  
    
    3. Check disk partitions using the fdisk -l utility.

    Display example for CentOS:

    [root@centos~]# fdisk -l
    Disk /dev/sda: 45GiB, 48318382080 bytes, 94371840 sectors
    Units: sectors of 1*512=512 bytes 
    Sector size (logical/physical): 512 bytes / 512 bytes
    I / O size (minimum/optimal): 512 bytes / 512 bytes
    Disklabel type: dos
    Disk identifier: 0xa2d4566c
    Device    Boot   Start   End      Sectors  Size Id  Type
    /dev/sda1   *    2048    2099199  2097152  1G   83  Linux
    /dev/sda2        2099200 65914559 60815360 29G  8e  Linux LVM
    

    The /dev/sda2 partition is an extensible LVM partition.

    Display example for Ubuntu:

    [root@centos~]# fdisk -l
    Disk /dev/sda: 45GiB, 48318382080 bytes, 94371840 sectors
    Disk model: QEMU HARDDISK
    Units: sectors of 1*512=512 bytes 
    Sector size (logical/physical): 512 bytes / 512 bytes
    I / O size (minimum/optimal): 512 bytes / 512 bytes
    Disklabel type: dos
    Disk identifier: 0xa2d4566c
    
    Device    Boot   Start   End      Sectors  Size  Id  Type
    /dev/sda1   *    2048    1050623  1048576  512M   b  W95 FAT32
    /dev/sda2        1052670 61863933 61863933 29.5G  5  Extended
    /dev/sda5        1052672 61863933 61863931 29.5G  8e Linux LVM
    

    Note

    Please note that in the Ubuntu distribution, the sectors of the /dev/sda5 partition are included in the sectors of the /dev/sda2 partition i.e. have one disk space.

    The /dev/sda2 partition is extended, the /dev/sda5 partition is an LVM partition created on the extended partition. Therefore, in order to expand the root logical volume, you must first expand both of these partitions - this will be discussed later.

Step 2. Expanding the Partition Using the growpart Utility

  1. Install the growpart utility according to your distribution.

    • for distributions Fedora, CentOS, RHEL, Oracle Linux, Alma Linux, Rocky Linux command:
    [root@centos ~]# dnf -y install cloud-utils-growpart
    

    Note

    For legacy distributions (CentOS 7.9), use yum instead of dnf.

    • for Ubuntu distributions, the Debian command:
    [root@ubuntu ~]# apt -y install cloud-guest-utils
    
  2. Extend the partition (in our case /dev/sda2) to the entire unallocated area, i.e. in our case, 15Gb.

    Note

    In our example, /dev/sda2 is the root partition. Note that you may have a different partition.

    [root@centos ~]# growpart /dev/sda 2
    

    For Ubuntu, you need to expand 2 partitions: the extended partition /dev/sda2 and the LVM partition /dev/sda5:

    [root@ubuntu ~]# growpart /dev/sda 2
    [root@ubuntu ~]# growpart /dev/sda 5    
    

    Note

    You may have other partitions to expand, you can check this with the lsblk and fdisk -l commands.

  3. Make sure the partition is extended with the lsblk command.

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

Step 3. Expanding the LVM space

  1. First, you need to expand the existing physical volume using the command:
    pvcreate /dev/sda2 - where /dev/sda2 is the name of the expandable partition.
    We check the execution with the pvs command - the value of the PFree column should increase by the expandable volume.

    Note

    In our example, the physical volume is /dev/sda2. Note that you may have another different physical volume.

    [root@centos~]# pvs  
    PV           VG     Fmt   Attr PSize    PFree    
    /dev/sda2    centos lvm2  a--  <29.00g  0      
    
    [root@centos~]# pvresize /dev/sda2  
     Physical volume "dev/sda2" changed
     1 physical volume(s) resized or updated / 0 physical volume(s) not relized
    
    [root@centos~]# pvs  
    PV           VG     Fmt   Attr PSize    PFree    
    /dev/sda2    centos lvm2  a--  <44.00g  15.00g 
    
  2. Check the volume group size. The value of the VFree column should increase by the expandable amount.

    [root@centos~]# vgs  
    VG        #PV   #LV   #SN   Attr   VSize    VFree    
    centos    1     2     0     wz--n- <44.00g  15.00g 
    
  3. Then you need to expand the logical volume by running the command:
    lvextend -r -l +100%FREE /dev/centos/root, where
    /dev/centos/root is the path of the expandable file system,
    centos is the name of the group (can be found using the vgs command),
    root is the name of the logical volume (can be found by lvs command).

    In the command above, instead of /dev/centos/root, you can specify another path /dev/mapper/centos-root (can be found with the df -hTcommand), and the result will be the same.

    [root@centos~]# lvs  
    LV         VG       Attr       LSize    
    root       centos  -wi-ao----  <28.00g  
    swap       centos  -wi-ao----  1.00g  
    
    [root@centos~]# lvextend -l +100%FREE /dev/centos/root
    Size of logical volume centos/root changed from <28.00 GiB (7167 extents) to <43.00 GiB (1107 extents).
    Logical volume centos/root successfully resized.
    
    meta-data=/dev/mapper/centos-root isize=512 agcount=4, agsize=1834752blks
        =       sectsz=512  attr=2, projid32bit=1
        =       crc=1   finobt=1,sparce=1,rmapbt=0
        =       reglink=1
    data=       bsize=4096  blocks=7339008,imaxpct=25
        =       sunit=0  swidth=0 blks
    naming =version2    bsize=4096  ascii-ci=0,ftype=1
    log  =internal log  bsize=4096  blocks=3583, version=2
        =       sectsz=512 sunit=0 blks, lazy-count=1
    realtime=none       extsz=4096 blocks=0, rtextents=0
    data blocks changed from 7339008 to 11271168
    
    [root@centos~]# lvs  
    LV         VG       Attr       LSize    
    root       centos  -wi-ao----  <43.00g  
    swap       centos  -wi-ao----  1.00g 
    

Step 4. Expanding the file system

Note

This operation carries the risk of data loss. It is recommended to make a backup copy of the virtual machine beforehand.

  1. To increase the file system on a logical volume, you must enter the command, depending on the distribution:

    • For the XFS file system (Fedora, CentOS, RHEL, Oracle Linux, Alma Linux, Rocky Linux distributions), the xfs_growfs utility is used and the path specified in the output of the df -hT command for the expandable partition (in the example below for Centos it is /dev/mapper/centos- root).
    [root@centos~]# df -hT /dev/centos/root
    Filesystem              Type  Size  Used Avail Use% Mounted on
    /dev/mapper/centos-root xfs    28G  2.1G   26G   8% /
    [root@centos~]]# xfs_growfs /dev/mapper/centos-root
    meta-data=/dev/mapper/centos-root isize=512    agcount=4, agsize=1834752 blks
            =                       sectsz=512   attr=2, projid32bit=1
            =                       crc=1        finobt=1, sparse=1, rmapbt=0
            =                       reflink=1
    data    =                       bsize=4096   blocks=7339008, imaxpct=25
            =                       sunit=0      swidth=0 blks
    naming  =version 2              bsize=4096   ascii-ci=0, ftype=1
    log     =internal log           bsize=4096   blocks=3583, version=2
            =                       sectsz=512   sunit=0 blks, lazy-count=1
    realtime =none                   extsz=4096   blocks=0, rtextents=0
    data blocks changed from 7339008 to 9959424
    
    • For the ext4 file system (Ubuntu, Debian distributions), the resize2fs utility is used and the path specified in the output of the df -hT command for the expandable partition (in the example below for Ubuntu this is /dev/mapper/ubuntu-root).
    [root@ubuntu:~]# df -hT 
    Filesystem              Type  Size  Used Avail Use% Mounted on
    /dev/mapper/ubuntu-root ext4  38G   7.1G  29G   20% /
    
    root@ubuntu:~# resize2fs /dev/mapper/ubuntu-root
    resize2fs 1.45.5 (07-Jan-2020)
    Filesystem at /dev/mapper/ubuntu-root is mounted on/; on-line resizing required
    old_desc_blocks=4, new_desc_blocks=5
    The filesystem on /dev/mapper/ubuntu-root is now 10101760 (4k) blocks long.
    
  2. Let's determine the mount path of the file system of the expanding partition, the mount point, and the file system type.
    In our example, the path is /dev/mapper/centos-root (or /dev/centos/root),
    the mount point is /,
    the file system type is xfs.

    Note

    Note that you will have your own path and file system type.

Step 5. Verification

To check the new disk size and file system, repeat the commands df -hT and lsblk.

[root@centos~]# df-hT  
Filesystem              Type     Size   Used  Avail Use  Mounted on      
devtmpfs                devtmpfs 1.8G   0     1.8G  0%   /dev  
tmpfs                   tmpfs    1.9G   0     1.9G  0%   /dev/shm  
tmpfs                   tmpfs    1.9G   9.2M  1.9G  1%   /run  
tmpfs                   tmpfs    1.9G   0     1.9G  0%   /sys/fs/cgroup  
/dev/mapper/centos-root xfs      43G    5.0G  39G   12%   /  
/dev/sda1               xfs      1014M  427M  588M  43%  /boot  
tmpfs                   tmpfs    374M   32K   374M  1%   /run/user/1000  

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