Resize UCS root file system
Q: How do I resize the root file system of my UCS VM?
A: It depends on if you are using Logical Volume Manager (LVM) or not.
With LVM
By default all of our ucs-kt-get
templates use LVM.
- Create a new QCOW2 file:
qemu-img create -f qcow2 "/var/lib/libvirt/images/${DOMAIN}-2.qcow2" 100G
. - Make sure the file is writable by group
Tech
:chmod 664 "/var/lib/libvirt/images/${DOMAIN}-2.qcow2"
. - (Live-)attach the additional volume to the VM:
virsh attach-disk --persistent --live --domain "$DOMAIN" --type disk --driver qemu --subdriver qcow2 --cache unsafe --sourcetype file --source "/var/lib/libvirt/images/${DOMAIN}-2.qcow2" --targetbus virtio --target vdb
. - Login to the VM as user
root
. - Create a Physical Volume (PV) signature on the block device — no MBR/GPT partition table needed:
pvcreate /dev/vdb
. - Add the PV to the existing Volume Group (VG):
vgextend vg_ucs /dev/vdb
. - Resize the existing Logical Volume (LV) containing the root file system:
lvresize /dev/vg_ucs/root -L +100%FREE
. - Resize the file system itself:
resize2fs /dev/vg_ucs/root
.
If you get any error message like Command failed with status code 5
from vgextend
this might be cause by your file system being too full or mounted read only:
LVM tries to create a backup of the LVM metadata before modifying it.
Try to free some space or try to remount the file system read-write with mount -o remount,rw /
.
Without LVM
This only works if the VM does not have snapshots;
they must be removed one-by-one with virsh snapshot-delete "$DOMAIN" "$SNAPSHOT"
first.
The main problem here is that by growing the underlying QCOW2 image file new free space appears at the end of the disk. By default the SWAP partition is located there, which must be removed to then grow the root file system, which is located at the beginning of the disk.
- Shutdown the VM.
- Find the QCOW2 file of the VM:
virsh domblklist "$DOMAIN"
. - Get the current allocation size the of QCOW2 image:
virsh vol-info "/var/lib/libvirt/images/${DOMAIN}-0.qcow2"
. - Grow the image size:
virsh vol-resize "/var/lib/libvirt/images/${DOMAIN}-0.qcow2" 100G
. - Start the VM.
- Login to the VM as user
root
. - Remove SWAP from
/etc/fstab
. - Go into rescue mode:
systemctl rescue
. - Turn of SWAP:
swapoff -a
. - Edit the partition table:
fdisk /dev/vda
.- Dump the current partition table:
p
. - Write down the new size of the disk in sectors and the old start sector of
/dev/vda1
; usually2048
. - Delete the local partition
d 5
containing the SWAP space. - Delete the extended partition
d 2
which contained the logical partition. - Delete the primary partitions
d 1
which contains the root file system. - Re-create the primary partition
n p 1 2048 default No.
. This will warn you that the oldext4
signature was re-detected; do not delete it. - Write the new partition table
w
and exitfdisk
.
- Dump the current partition table:
- Resize the file system itself:
resize2fs /dev/vda1
. reboot
The procedure above deletes the SWAP space.
If you need one do not allocate all space when re-sizing partition 1.
Afterwards create another new partition (can also be a primary, no need to create an extended and logical partition again).
Make sure to use partition type 82
.
Run mkswap /dev/vda2
to initialize it.
Afterwards update /etc/fstab
to use /dev/vda2
or lookup the UUID from /dev/disk/by-uuid/
.