Tag Archives: resize2fs

KVM: How to resize (grow) an ext4 LVM VM

We use LVM both on the guest and the CentOS KVM host. Let’s upgrade from 5 GB to 15 GB on the guest.

1. Shutdown the VM.

2. On the KVM host, take a backup of the VM LV in case something goes wrong.

[root@host ~]# dd if=/dev/vg_host01/lv_guest01-vm of=./lv_guest01-vm-5gb

3. On the KVM host, resize the VM LV.

Add 1 GB:

[root@host ~]# lvresize -L+1G /dev/vg_host01/lv_guest01-vm

Or add all available space from the VG:

[root@host ~]# lvresize -l+100%FREE /dev/vg_host01/lv_guest01-vm

4. Boot up the VM again.

5. Delete and recreate the LVM partition.

root@guest:~$ parted /dev/vda
(parted) unit s
(parted) print
Model: Virtio Block Device (virtblk)
Disk /dev/vda: 30720000s
Sector size (logical/physical): 512B/512B
Partition Table: msdos

Number Start End Size Type File system Flags
1 2048s 1026047s 1024000s primary ext4 boot
2 1026048s 10239999s 9213952s primary lvm

(parted) rm 2
Warning: WARNING: the kernel failed to re-read the partition table on /dev/vda (Device or resource busy). As a result, it may not reflect all of your changes until after reboot.

Exit parted. You can also use fdisk to delete the partition, but when recreating the partition, fdisk doesn’t know about the right first and last cylinders. Maybe exiting and running fdisk again solves that (forgot to check that, we’ll try that next time) – UPDATE: on another try with fdisk it worked, no need for parted. You can do everything with fdisk in one run, just don’t exit it and use the start cylinder from the deleted partition. It will know the end cylinder automatically. Ok, so we deleted the partition using parted and now we start fdisk again to recreate the partition. Also, for some reason, the first partition (1) is now just half in size, 512 MB instead of 1024 MB. Weird. parted messed something up there?

root@guest:~$ fdisk /dev/vda

WARNING: DOS-compatible mode is deprecated. It’s strongly recommended to
switch off the mode (command ‘c’) and change display units to
sectors (command ‘u’).

Command (m for help): p

Disk /dev/vda: 15.7 GB, 15728640000 bytes
222 heads, 30 sectors/track, 4612 cylinders
Units = cylinders of 6660 * 512 = 3409920 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000831eb

Device Boot Start End Blocks Id System
/dev/vda1 * 1 155 512000 83 Linux

Command (m for help): n
Command action
e extended
p primary partition (1-4) p
Partition number (1-4): 2
First cylinder (155-4612, default 155):
Using default value 155
Last cylinder, +cylinders or +size{K,M,G} (155-4612, default 4612):
Using default value 4612

Command (m for help): t
Partition number (1-4): 2
Hex code (type L to list codes): 8e
Changed system type of partition 2 to 8e (Linux LVM)

Command (m for help): p

Disk /dev/vda: 15.7 GB, 15728640000 bytes
222 heads, 30 sectors/track, 4612 cylinders
Units = cylinders of 6660 * 512 = 3409920 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000831eb

Device Boot Start End Blocks Id System
/dev/vda1 * 1 155 512000 83 Linux
/dev/vda2 155 4612 14844936 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)
Syncing disks.

6. Reboot the VM.

7. Resize the PV.

Let’s look at the PV first:

root@guest:~$ pvdisplay
— Physical volume —
PV Name /dev/vda2
VG Name vg_guest01
PV Size 4.39 GiB / not usable 3.00 MiB
Allocatable yes (but full)
PE Size 4.00 MiB
Total PE 1124
Free PE 0
Allocated PE 1124
PV UUID IzkvJd-G9JW-Ju7r-1bDK-FLzC-I9y1-Ll3N4R

Still 5 GB. Let’s resize it:

root@guest:~$ pvresize /dev/vda2
Failed to read physical volume “/dev/vda2”
0 physical volume(s) resized / 0 physical volume(s) not resized

Don’t worry about the “not resized”. It worked:

root@guest:~$ pvdisplay
— Physical volume —
PV Name /dev/vda2
VG Name vg_guest01
PV Size 14.16 GiB / not usable 1016.00 KiB
Allocatable yes
PE Size 4.00 MiB
Total PE 3624
Free PE 2500
Allocated PE 1124
PV UUID IzkvJd-G9JW-Ju7r-1bDK-FLzC-I9y1-Ll3N4R

8. Resize the LV.

root@guest:~$ lvresize -l+100%FREE /dev/vg_guest01/lv_root

Check it:

root@guest:~$ lvdisplay /dev/vg_guest01/lv_root
— Logical volume —
LV Path /dev/vg_guest01/lv_root
LV Name lv_root
VG Name vg_guest01
LV UUID 3Qfe2E-5j5H-H8el-iwxt-oN81-DvYq-AzENqH
LV Write Access read/write
LV Creation host, time guest01, 2013-03-02 00:41:16 +0100
LV Status available
# open 1
LV Size 14.03 GiB
Current LE 3592
Segments 2
Allocation inherit
Read ahead sectors auto
– currently set to 256
Block device 253:0

9. Resize the ext4 FS.

root@guest:~$ resize2fs /dev/vg_guest01/lv_root
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/vg_guest01/lv_root is mounted on /; on-line resizing required
old desc_blocks = 1, new_desc_blocks = 1
Performing an on-line resize of /dev/vg_guest01/lv_root to 3678208 (4k) blocks.
The filesystem on /dev/vg_guest01/lv_root is now 3678208 blocks long.

10. Reboot the VM.

11. Done.

root@guest:~$ df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/vg_guest01-lv_root
14G 1.2G 12G 9% /
tmpfs 939M 0 939M 0% /dev/shm
/dev/vda1 485M 53M 408M 12% /boot