How to extend an existing drive in Linux?
November 24, 2023 at 2:43 PMHow to extend an existing drive in Linux?
Table of Contents
A brief summary of the commands:
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).
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
-
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
. -
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)
You need to determine the system partition and the type of file system on it, which you want to expand using the commanddf -hT
. -
Now you need to check the available disk partitions using the utility
fdisk -l
.
More on step 1
1.1 List block devices with the lsblk command. Determine the partition you are going to expand and the amount of unallocated space on the disk.
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.
1.2 Check the existing LVM
configuration by determining the name and number of available physical volumes (PV), volume group (VG), and logical volumes (LV) with the following commands pvs
, vgs
, and lvs
respectively:
In this example, we see that the system has a physical volume /dev/sda2
, which is part of the created centos volume group. This volume group has 2 logical volumes: root and swap.
1.3 Check disk partitions using the fdisk -l
utility.
Display example for CentOS
:
The /dev/sda2
partition is an extensible LVM partition.
Display example for Ubuntu
:
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.
growpart
Utility
Step 2. Expanding the Partition Using the 2.1 First, you need to install the growpart
utility according to your distribution:
- command for Fedora, CentOS, RHEL, Oracle Linux, Alma Linux, and Rocky Linux distributions:
dnf -y install cloud-utils-growpart
Note: For legacy distributions (CentOS 7.9), use yum
instead of dnf
.
- Command for Ubuntu, Debian distributions:
apt -y install cloud-guest-utils`
2.2 Then you need to extend the partition with the command:
growpart /dev/sda 2`
where /dev/sda 2
is the name of the expandable partition.
Note: Ubuntu may need to expand 2 partitions (this can be found with the fdisk -l
command).
2.3 To make sure the partition is expanding, repeat the command lsblk
.
More on step 2
2.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
- for Ubuntu distributions, the Debian command:
[root@ubuntu ~]# apt -y install cloud-guest-utils
2.2 Extend the partition (in our case /dev/sda2
- the root partition (root)) to the entire unallocated area, i.e. in our case, 15Gb. (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, as we found out in step 1.3, 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.
2.3 Make sure the partition is extended with the lsblk
command.
Step 3. Expanding the LVM space
3.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.
3.2 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 -hT
command), and the result will be the same.
More on step 3
3.1 Extend an existing physical volume with the pvresize
command.
We check the execution with the pvs
command - the value of the PFree column should increase by the expandable volume. (In our example, the physical volume is /dev/sda2
. Note that you may have another different physical volume).
3.2 Check the volume group size. The value of the VFree column should increase by the expandable amount.
3.3 Extend the logical volume using the lvextend -r -l +100%FREE /dev/centos/root
command, where
centos is the name of the volume group and root is the name of the logical volume. (Note that you may have a different group and logical volume names).
Check the execution of the lvs
command - the value of the PFree column should increase.
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.
To increase the file system on a logical volume, you must enter the command, depending on the distribution:
-
Fedora, CentOS, RHEL, Oracle Linux, Alma Linux, Rocky Linux (XFS file system):
xfs_growfs /dev/mapper/centos-root
, where/dev/mapper/centos-root
is the name of the expandable file system (the name and type can be found with thedf -hT
command). -
Ubuntu, Debian (ext4 file system):
resize2fs /dev/mapper/ubuntu-root
, where/dev/mapper/ubuntu-root
is the name of the expandable file system (the name and type can be found with thedf -hT
command).
More on step 4
4.1 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 that you will have your own path and file system type.
4.2 Next, you need to increase the size of the file system on the logical volume. Depending on the type of file system and distribution, select the type that suits you below.
4.2.1 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
).
4.2.2 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
).
Step 5. Verification
To check the new disk size and file system, repeat the commands df -hT
and lsblk
.
More on step 5