This article describes the process for expanding a newly enlarged physical volume. It is not for extending an LVM to a new physical volume. That process will be covered in another article. Much of the process is the same, but there are some slight differences you should be aware of.
This article focuses on CentOS7, but will also work in Debian/Ubuntu (with some minor differences). As always, please be sure you have a good, solid backup of the virtual machine BEFORE you start this process. If you do anything wrong (or mercury is in retrograde under a venus star poke bowl), you’ll lose all your data. Back up your VM!
1: Verify the Disk's Size
Force a rescan of the drive. In this case, we are using /dev/sda. You will need to modify this command if your disk is different.
echo 1 > /sys/block/sda/device/rescan
Let’s see what that process looks like below:
[root@localhost ~]# fdisk -l
Disk /dev/sda: 17.2 GB, 17179869184 bytes, 33554432 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
Disk label type: dos
Disk identifier: 0x000b76ff
Device Boot Start End Blocks Id System
/dev/sda1 * 2048 2099199 1048576 83 Linux
/dev/sda2 2099200 33554431 15727616 8e Linux LVM
Disk /dev/mapper/centos-root: 14.4 GB, 14382268416 bytes, 28090368 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
Disk /dev/mapper/centos-swap: 1719 MB, 1719664640 bytes, 3358720 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
[root@localhost ~]# echo 1 > /sys/block/sda/device/rescan
[root@localhost ~]# fdisk -l
Disk /dev/sda: 34.4 GB, 34359738368 bytes, 67108864 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
Disk label type: dos
Disk identifier: 0x000b76ff
Device Boot Start End Blocks Id System
/dev/sda1 * 2048 2099199 1048576 83 Linux
/dev/sda2 2099200 33554431 15727616 8e Linux LVM
Disk /dev/mapper/centos-root: 14.4 GB, 14382268416 bytes, 28090368 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
Disk /dev/mapper/centos-swap: 1719 MB, 1719664640 bytes, 3358720 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
The size reported by fdisk should now match the size shown in vCenter.
2: Resize the partition
We will be using fdisk to delete the existing partition, then create a new partition to replace it. You will need to note the following details about the partition before you delete it:
A: Primary/Extended?
B: Partition number
C: Partition type
If this is your first rodeo, and even if it isn’t, please make sure you’ve got your good backup before proceeding!
The highlighted lines indicate user input is needed.
[root@localhost ~]# fdisk /dev/sda
Welcome to fdisk (util-linux 2.23.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Command (m for help): d
Partition number (1,2, default 2): 2
Partition 2 is deleted
Command (m for help): n
Partition type:
p primary (1 primary, 0 extended, 3 free)
e extended
Select (default p): p
Partition number (2-4, default 2):
First sector (2099200-67108863, default 2099200):
Using default value 2099200
Last sector, +sectors or +size{K,M,G} (2099200-67108863, default 67108863):
Using default value 67108863
Partition 2 of type Linux and of size 31 GiB is set
Command (m for help): t
Partition number (1,2, default 2): 2
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'
Command (m for help): p
Disk /dev/sda: 34.4 GB, 34359738368 bytes, 67108864 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
Disk label type: dos
Disk identifier: 0x000b76ff
Device Boot Start End Blocks Id System
/dev/sda1 * 2048 2099199 1048576 83 Linux
/dev/sda2 2099200 67108863 32504832 8e Linux LVM
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
3: Rescan the partition
In order to see the new size of the partition, we will need to issue the following command:
[root@localhost ~]# partx -u /dev/sda
4: Resize the LVM Physical Volume
First, let’s check what the physical volume currently shows:
[root@localhost ~]# pvdisplay
--- Physical volume ---
PV Name /dev/sda2
VG Name centos
PV Size <15.00 GiB / not usable 3.00 MiB
Allocatable yes (but full)
PE Size 4.00 MiB
Total PE 3839
Free PE 0
Allocated PE 3839
PV UUID MdAZF1-10rp-eAgK-FRlM-nji2-9fJC-tlaFJe
We see that the current PV size isĀ 15GiB. Now run pvresize:
[root@localhost ~]# pvresize /dev/sda2
Physical volume "/dev/sda2" changed
1 physical volume(s) resized or updated / 0 physical volume(s) not resized
Last, check that the physical volume has been successfully resized:
[root@localhost ~]# pvdisplay
--- Physical volume ---
PV Name /dev/sda2
VG Name centos
PV Size <31.00 GiB / not usable 2.00 MiB
Allocatable yes
PE Size 4.00 MiB
Total PE 7935
Free PE 4096
Allocated PE 3839
PV UUID MdAZF1-10rp-eAgK-FRlM-nji2-9fJC-tlaFJe
5: Resize the Logical Volume
Check the current size of the logical volume via ‘lvdisplay’:
[root@localhost ~]# lvdisplay
--- Logical volume ---
LV Path /dev/centos/swap
LV Name swap
VG Name centos
LV UUID SATDXS-drDY-BVK2-gVKD-Qk4Q-iCrz-PUv2nY
LV Write Access read/write
LV Creation host, time localhost, 2022-11-04 19:40:49 -0400
LV Status available
# open 2
LV Size 1.60 GiB
Current LE 410
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:1
--- Logical volume ---
LV Path /dev/centos/root
LV Name root
VG Name centos
LV UUID vD2Glj-jtsX-dxtu-3dyz-mSoJ-uQf5-kZ043U
LV Write Access read/write
LV Creation host, time localhost, 2022-11-04 19:40:49 -0400
LV Status available
# open 1
LV Size 13.39 GiB
Current LE 3429
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:0
Right now, we’re not interested in the swap partition, only root. Extend the logical volume using lvextend:
[root@localhost ~]# lvextend -l +100%FREE /dev/centos/root
Size of logical volume centos/root changed from 13.39 GiB (3429 extents) to 29.39 GiB (7525 extents).
Logical volume centos/root successfully resized.
Let’s double check that the volume has successfully grown:
[root@localhost ~]# lvdisplay
--- Logical volume ---
LV Path /dev/centos/swap
LV Name swap
VG Name centos
LV UUID SATDXS-drDY-BVK2-gVKD-Qk4Q-iCrz-PUv2nY
LV Write Access read/write
LV Creation host, time localhost, 2022-11-04 19:40:49 -0400
LV Status available
# open 2
LV Size 1.60 GiB
Current LE 410
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:1
--- Logical volume ---
LV Path /dev/centos/root
LV Name root
VG Name centos
LV UUID vD2Glj-jtsX-dxtu-3dyz-mSoJ-uQf5-kZ043U
LV Write Access read/write
LV Creation host, time localhost, 2022-11-04 19:40:49 -0400
LV Status available
# open 1
LV Size 29.39 GiB
Current LE 7525
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:0
6: Grow the filesystem
Check the current filesystem size:
[root@localhost ~]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 1.9G 0 1.9G 0% /dev
tmpfs 1.9G 0 1.9G 0% /dev/shm
tmpfs 1.9G 8.9M 1.9G 1% /run
tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup
/dev/mapper/centos-root 14G 1.3G 13G 10% /
/dev/sda1 1014M 152M 863M 15% /boot
tmpfs 379M 0 379M 0% /run/user/0
This is where the instructions for Debian/Ubuntu differ from CentOS7.
If you’re using CentOS7, it uses XFS and we will use xfs_growfs:
[root@localhost ~]# xfs_growfs /dev/mapper/centos-root
meta-data=/dev/mapper/centos-root isize=512 agcount=4, agsize=877824 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0 spinodes=0
data = bsize=4096 blocks=3511296, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal bsize=4096 blocks=2560, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
data blocks changed from 3511296 to 7705600
If you’re using Debian/Ubuntu, they use EXT4 and we will useĀ resize2fs.
root@ubuntu:~# resize2fs /dev/ubuntu-vg/root
#I'll come back to this one.
#It's late and I'm tired.
#Sorry for anyone that's left hanging!
Now that we’ve grown our file systems, let’s check that the new size is properly displayed/detected:
[root@localhost ~]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 1.9G 0 1.9G 0% /dev
tmpfs 1.9G 0 1.9G 0% /dev/shm
tmpfs 1.9G 8.9M 1.9G 1% /run
tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup
/dev/mapper/centos-root 30G 1.3G 29G 5% /
/dev/sda1 1014M 152M 863M 15% /boot
tmpfs 379M 0 379M 0% /run/user/0
In this example, df -h originally reported the size as 14G. After finishing the process, we see that the drive is now 30G.