Increase the size of a VM's disk

From Newroco Tech Docs
Jump to navigationJump to search

Note: Both methods have the risk to break the whole VM so it's best to make a backup first.

Method 1

First of all take a note of the VM's partition that you want to increase. On guest run:

# df -h
Filesystem      Size  Used Avail Use% Mounted on
udev            479M     0  479M   0% /dev
tmpfs           100M  1.7M   98M   2% /run
/dev/sda1       8.3G  980M  6.9G  13% /
tmpfs           497M     0  497M   0% /dev/shm
tmpfs           5.0M     0  5.0M   0% /run/lock
tmpfs           497M     0  497M   0% /sys/fs/cgroup

On my case it's /dev/sda1.

Shutdown the VM from the host.

virsh shutdown my-vm

Resize the VM's image

qemu-img resize /var/lib/kvm/my-vm/my-vm.qcow2 +5G

Copy the VM's image

cp /var/lib/kvm/my-vm/my-vm.qcow2 /var/lib/kvm/my-vm/my-vm-original.qcow2

Increase the partition that you took note at the beginning

virt-resize --expand /dev/sda1 /var/lib/kvm/my-vm/my-vm-original.qcow2 /var/lib/kvm/my-vm/my-vm.qcow2

Note: to use virt-resize command you need package libguestfs-tools

Start the VM

virsh start my-vm

Check that your VM's partition increased

# df -h
Filesystem      Size  Used Avail Use% Mounted on
udev            479M     0  479M   0% /dev
tmpfs           100M  1.7M   98M   2% /run
/dev/sda1        14G  982M   13G   8% /
tmpfs           497M     0  497M   0% /dev/shm
tmpfs           5.0M     0  5.0M   0% /run/lock
tmpfs           497M     0  497M   0% /sys/fs/cgroup

Method 2

Shutdown the VM from the host.

virsh shutdown my-vm

Resize the VM's image

qemu-img resize /var/lib/kvm/my-vm/my-vm.qcow2 +5G

Start the VM

virsh start my-vm

SSH to the VM and check what partition do you want to increase

# df -h
Filesystem      Size  Used Avail Use% Mounted on
udev            479M     0  479M   0% /dev
tmpfs           100M  1.7M   98M   2% /run
/dev/sda1       8.3G  980M  6.9G  13% /
tmpfs           497M     0  497M   0% /dev/shm
tmpfs           5.0M     0  5.0M   0% /run/lock
tmpfs           497M     0  497M   0% /sys/fs/cgroup

In my case it is /dev/sda1. Running the next command you can see that /dev/sda has more space than the partitions are using

# fdisk -l
Disk /dev/sda: 16 GiB, 17144217600 bytes, 33484800 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: 0x91eb62c9

Device     Boot    Start      End  Sectors  Size Id Type
/dev/sda1             63 17931640 17931578  8.6G 83 Linux
/dev/sda2       17934336 21932031  3997696  1.9G 82 Linux swap / Solaris

Delete partitions /dev/sda1 and /dev/sda2

Note: This will not delete/corrupt the files on this partition if it is recreated correctly. To correctly recreate a partition it needs to start at the same sector as before so take note of that from the above output, and end at a higher sector as we want to enlarge the partition.

fdisk -c=dos /dev/sda
d
1
d
2  (this will be automatically selected as in my case at least, it was the last partition)

Recreate a larger /dev/sda1 partition

n
p
1
63  (first sector of the new /dev/sda1 partition must be the same as before, so check what it was)
+14G  (replace 14 with what you want the full size of the partition to be)

Check that partition /dev/sda1 has been created correctly and take a note of the last sector

p

Disk /dev/sda: 16 GiB, 17144217600 bytes, 33484800 sectors
Geometry: 4 heads, 32 sectors/track, 2084 cylinders
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: 0x91eb62c9

Device     Boot Start      End  Sectors Size Id Type
/dev/sda1          63 29360191 29360129  14G 83 Linux

Recreate the SWAP partition

n
p
2
29362887  (leave some free sectors between partitions; I left the same amount as before: 2696)
<press enter>  (it will default to the last sector -1)
t
2
82  (this will change the partition to SWAP)

Check that the partitions have been correctly recreated

p

Disk /dev/sda: 16 GiB, 17144217600 bytes, 33484800 sectors
Geometry: 4 heads, 32 sectors/track, 2084 cylinders
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: 0x91eb62c9

Device     Boot    Start      End  Sectors Size Id Type
/dev/sda1             63 29360191 29360129  14G 83 Linux
/dev/sda2       29362887 33484799  4121913   2G 82 Linux swap / Solaris

Write the changes and quit

w

Reboot the VM to make the kernel read the new partition table.

virsh reboot my-vm

SSH to the VM and run

resize2fs /dev/sda1

Check that the partition has increased

df -h

Format and activate the new SWAP partition

mkswap /dev/sda2
swapon /dev/sda2

Copy the new UUID and replace the old one in /etc/fstab so it is available after a reboot.

Check that it is available

free -m


Increase the size of a VM's disk for LVM groups

Shutdown the VM from the host:

virsh shutdown my-vm

Resize the VM's image:

qemu-img resize /var/lib/kvm/my-vm/my-vm.qcow2 +5G

Start the VM

virsh start my-vm

Create a new partition with fdisk. Note: Be careful what sectors do you choose for the partition start - end. You could check the free sectors with fdisk -l /dev/vda

fdisk /dev/vda

Welcome to fdisk (util-linux 2.31.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


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): 
First sector (487424-65011711, default 487424): 23066630
Last sector, +sectors or +size{K,M,G,T,P} (23066630-65011711, default 65011711): 

Created a new partition 3 of type 'Linux' and of size 20 GiB.

Command (m for help): t
Partition number (1-3,5, default 5): 3
Hex code (type L to list all codes): 8e

Changed type of partition 'Linux' to 'Linux LVM'.

Command (m for help): w
The partition table has been altered.
Syncing disks.

create new PV (Physical Volume)

pvcreate /dev/vda3

Add the newly created partition to Volume group:

sudo vgextend test-vg /dev/vda3

Extend the LV to the to the requested size or to full extend:

lvextend -L+6G /dev/test-vg/root
lvextend -l +100%FREE /dev/test-vg/root

Run the following command to have the new space recognized:

resize2fs /dev/test-vg/root