Resizing an existing disk with LVM¶
- Table of contents
- Resizing an existing disk with LVM
Synopsis¶
If an existing disk is resized and you want to add the extra space to an existing LVM logical volume, follow this guide.
Source: https://www.ibm.com/support/pages/how-increase-partition-size-using-existing-disk-rhel-lvm
Procedure¶
The output of the commands below are from a real situation:- The disk was resized from 100GB to 300GB
- The extra space was to be added to the root filesystem
- The disk is
/dev/sda
; adapt where necessary for your situation
Rescan the disk¶
When a disk is resized, the Linux kernel doesn't automatically pick up this change.
To trigger it to rescan a disk, execute this command:
root@server:~# echo 1 > /sys/class/block/sda/device/rescan
Make sure the disk space is refreshed:
root@server:~# fdisk -l
Disk /dev/sda: 300 GiB, 322122547200 bytes, 629145600 sectors
Disk model: Virtual disk
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: 0xc4a390f9
Device Boot Start End Sectors Size Id Type
/dev/sda1 * 2048 999423 997376 487M 83 Linux
/dev/sda2 1001470 104855551 103854082 49.5G 5 Extended
/dev/sda5 1001472 104855551 103854080 49.5G 8e Linux LVM
Disk /dev/mapper/volume-vg-root: 48.56 GiB, 52143587328 bytes, 101842944 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/volume-vg-swap_1: 980 MiB, 1027604480 bytes, 2007040 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
Looks good.
Create a new partition containing the extra disk space¶
Before disk space can be added to a LVM volume group, it needs to be inside a disk partition.
We create a primary partition (which will become /dev/sda3
) for the disk space:
root@server:~# fdisk /dev/sda Welcome to fdisk (util-linux 2.36.1). Changes will remain in memory only, until you decide to write them. Be careful before using the write command. Command (m for help): F Unpartitioned space /dev/sda: 250 GiB, 268436504576 bytes, 524290048 sectors Units: sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes Start End Sectors Size 104855552 629145599 524290048 250G Command (m for help): n
Partition type
p primary (1 primary, 1 extended, 2 free)
l logical (numbered from 5)
Select (default p): p Partition number (3,4, default 3): <ENTER> First sector (104855552-629145599, default 104855552): <ENTER> Last sector, +/-sectors or +/-size{K,M,G,T,P} (104855552-629145599, default 629145599): <ENTER> Created a new partition 3 of type 'Linux' and of size 250 GiB. Command (m for help): p Disk /dev/sda: 300 GiB, 322122547200 bytes, 629145600 sectors Disk model: Virtual disk 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: 0xc4a390f9 Device Boot Start End Sectors Size Id Type /dev/sda1 * 2048 999423 997376 487M 83 Linux /dev/sda2 1001470 104855551 103854082 49.5G 5 Extended /dev/sda3 104855552 629145599 524290048 250G 83 Linux /dev/sda5 1001472 104855551 103854080 49.5G 8e Linux LVM Partition table entries are not in disk order. Command (m for help): w The partition table has been altered. Syncing disks.
Refresh the partition table¶
Like we had to trigger the kernel to rescan the disk to pick up on the newly created diskspace, we need to trigger the kernel to reread the partition table to pick up the changes we just made.
root@server:~# partprobe -s /dev/sda: msdos partitions 1 2 <5> 3 /dev/mapper/volume-vg-swap_1: loop partitions 1 /dev/mapper/volume-vg-root: loop partitions 1
Create a physical volume (PV) for LVM¶
In order to use a raw disk partition with LVM, it must be wrapped in a LVM physical volume.
To create such a physical volume with our new partition, execute:
root@server:~# pvcreate /dev/sda3 Physical volume "/dev/sda3" successfully created.
Add the PV to the volume group (VG)¶
First we need to get the name of the VG where we want to add the disk space:
root@server:~# vgdisplay
--- Volume group ---
VG Name volume-vg
System ID
Format lvm2
Metadata Areas 1
Metadata Sequence No 3
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 <49.52 GiB
PE Size 4.00 MiB
Total PE 12677
Alloc PE / Size 12677 / <49.52 GiB
Free PE / Size 0 / 0
VG UUID qE7Xrc-ePAK-B6vq-eGHt-qRCw-M6K9-TR9YlL
Now we'll extend the volume group by adding the physical volume /dev/sda3
:
root@server:~# vgextend volume-vg /dev/sda3 Volume group "volume-vg" successfully extended
List the PVs on the system to verify:
root@server:~# pvscan
PV /dev/sda5 VG volume-vg lvm2 [<49.52 GiB / 0 free]
PV /dev/sda3 VG volume-vg lvm2 [250.00 GiB / 250.00 GiB free]
Total: 2 [<299.52 GiB] / in use: 2 [<299.52 GiB] / in no VG: 0 [0 ]
Check the VG to verify that the free space is added:
root@server:~# vgdisplay
--- Volume group ---
VG Name volume-vg
System ID
Format lvm2
Metadata Areas 2
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 2
Act PV 2
VG Size <299.52 GiB
PE Size 4.00 MiB
Total PE 76677
Alloc PE / Size 12677 / <49.52 GiB
Free PE / Size 64000 / 250.00 GiB
VG UUID qE7Xrc-ePAK-B6vq-eGHt-qRCw-M6K9-TR9YlL
Extend the logical volume (LV) with the new disk space¶
First, display the LV so we can get the path:
root@server:~# lvdisplay
--- Logical volume ---
LV Path /dev/volume-vg/root
LV Name root
VG Name volume-vg
LV UUID cH0jGQ-qF4y-aoMg-2mXp-qM6g-FOIu-2vH7sr
LV Write Access read/write
LV Creation host, time volume, 2023-05-24 09:13:41 +0200
LV Status available
# open 1
LV Size 48.56 GiB
Current LE 12432
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 254:0
--- Logical volume ---
LV Path /dev/volume-vg/swap_1
LV Name swap_1
VG Name volume-vg
LV UUID nt3bJn-w6lN-RSnY-iEFQ-uWd7-f3ve-jkj2jw
LV Write Access read/write
LV Creation host, time volume, 2023-05-24 09:13:41 +0200
LV Status available
# open 2
LV Size 980.00 MiB
Current LE 245
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 254:1
Extend the LV, giving it all the free space.
By using the --resizefs
parameter, lvresize
will automatically resize the underlying filesystem to accomodate for the change in available diskspace. Without this parameter, resizing the filesystem must be done manually (using resize2fs
, xfs_growfs
, …).
Execute:
root@server:~# lvresize --resizefs --extents +100%FREE /dev/volume-vg/root Size of logical volume volume-vg/root changed from 48.56 GiB (12432 extents) to 298.56 GiB (76432 extents). Logical volume volume-vg/root successfully resized. resize2fs 1.46.2 (28-Feb-2021) Filesystem at /dev/mapper/volume-vg-root is mounted on /; on-line resizing required old_desc_blocks = 7, new_desc_blocks = 38 The filesystem on /dev/mapper/volume-vg-root is now 78266368 (4k) blocks long.
Finally, use df
to verify the disk space is actually available on the filesystem:
root@server:~# df -h
Filesystem Size Used Avail Use% Mounted on
udev 16G 0 16G 0% /dev
tmpfs 3.2G 604K 3.2G 1% /run
/dev/mapper/volume-vg-root 294G 1.4G 280G 1% /
tmpfs 16G 0 16G 0% /dev/shm
tmpfs 5.0M 0 5.0M 0% /run/lock
/dev/sda1 470M 87M 359M 20% /boot
tmpfs 3.2G 0 3.2G 0% /run/user/1000
Updated by offbyone almost 2 years ago · 1 revisions