I setup an Ubuntu guest on a CentOS KVM host with initially 6GB of disk space. How do I go about increasing the Ubuntu guest's disk space from the command line?
EDIT #1: I'm using a disk image file (qemu).
I setup an Ubuntu guest on a CentOS KVM host with initially 6GB of disk space. How do I go about increasing the Ubuntu guest's disk space from the command line?
EDIT #1: I'm using a disk image file (qemu).
qemu-img resize vmdisk.img +10G
to increase image size by 10GbOnline Method (using qemu, libvirt, and virtio-block)
For better or worse, the commands below will run even if the target virtual disk is mounted. This can be useful in environments where the disk cannot be unmounted (such as a root partition), the VM must stay on, and the system owner is willing to assume the risk of data corruption. To remove that risk, you would need to log into the VM and unmount the target disk first, something that isn't always possible.
Perform the following from the KVM hypervisor.
Increase the size of the disk image file itself (specify the amount to increase):
Get the name of the virtio device, via the libvirt shell (
drive-virtio-disk0
in this example):Signal the virtio driver to detect the new size (specify the total new capacity):
Then log into the VM. Running
dmesg
should report that the virtio disk detected a capacity change. At this point, go ahead and resize your partitions and LVM structure as needed.These serverfault questions are similar but more specific, KVM online disk resize? & Centos Xen resizing DomU partition and volume group. The 1st asks the question of how to increase a KVM guest while it's online, while the 2nd is XEN specific using LVM. I'm asking how to accomplish this while the KVM is offline.
NOTE: This link was useful for METHOD #1, and shows how to accomplish increasing a KVM's disk space (ext3 based), HOWTO: Resize a KVM Virtual Machine Image.
One thing to be aware of with KVM guests is that the partitions they're using inside can effect which method you can use to increase their disk space.
METHOD #1: Partitions are ext2/ext3/ext4 based
The nuts of this method are as follows:
Now with the larger mykvm.img file in hand, boot gparted and extend the existing partition into the newly added disk space. This last step basically extends the OS partition so that it can make use of the extra space.
METHOD #2: Partitions are LVM based
Here are the steps that I roughly followed to resize a KVM guest that used LVM internally.
run fdisk inside VM and delete & re-create LVM partition
Reboot the VM
Resize the LVM physical volume
Resize the LVM Logical Volume
Grow the File system
The above is my example, but I followed the steps on this website
Resize and Expand Internal Partitions in One Step
I had an Ubuntu host with a qcow2 guest file image and wanted to resize the disk and expand the appropriate partitions all in one step. It requires you to set up the libvirt guest filesystem utilities, but those are useful to have around anyway.
Inspiration from here: http://libguestfs.org/virt-resize.1.html
The key command here is:
virt-resize
Preparation:
Now do the following:
1) shutdown the guest:
2) Check out the current sizing and view the partition name you want to expand using libvirt utility:
sudo virt-filesystems --long --parts --blkdevs -h -a name-of-guest-disk-file
3) Create the new (40G) output disk:
4) Copy the old to the new while expand the appropriate partition (assuming your disk partition from step 2 was /dev/sda1):
5) Rename the indisk file as a backup, rename the outdisk as indisk (or modify the guest XML)
6) Reboot the guest and test the new disk file carefully before deleting the original file
7) Profit!
It is possible to do online resize, without stopping the VM. libvirtd supports this natively:
Find the block device name. Should be something like "vda"
Resize the virtual device:
Here is an example were I expand the
vda
disk from50GB
to51GB
forundercloud
VM.Now take a look at the .qcow2 image file's details:
Now let's resize the vda block device:
And confirm:
Then you can use this script inside the VM to show the commands to resize the block devices and fs: https://github.com/mircea-vutcovici/scripts/blob/master/vol_resize.sh. Here is a sample output:
If you are using LVM within the VM the simplest way to do this woudl be to add a new virtual disk to the VM and expand the volume group and logical volumes onto that.
To check if you are using LVM run
sudo pvs; sudo vgs; sudo lvs
, you will get something like this out:if the VM's OS is using LVM. In the above example the VM has a 30Gbyte vdisk, configured using LVM with one volume group called vgWWW containing two logical volumes, one for swap and one for everything else.
If LV is in use in the VM:
sudo pvcreate /dev/vdb
sudo vgextend vgWWW /dev/vdb
sudo lvextend --extents +100%FREE /dev/vgWWW/root
(or something likesudo lvextend --size +8G /dev/vgWWW/root
if you don't want to grow it all the way, this example would add 8Gb to the volume)resize2fs /dev/vgWWW/root
Note: the above assumes the vg/lv names are the same as my example which is unlikely, change as appropriate, also if the VM already had a virtual drive called
vdb
the new one will be something else (vdc
, and so on)Note:
resize2fs
will only work on ext2, ext3 and ext4 filesystem. If you are using something else it will error and do nothing.Note: as you are resizing a live filesystem
resize2fs
won't prompt you to runfsck
first as it would for an unmounted filesystem, it will just go ahead. You might want to run a read-only filesystem check there are no issues before proceeding.This way you can expand the partition you want:
See more examples here.
There is a possibility to increase the size of your VM's disk without rebooting the VM if you're using virtio drive and LVM.
(Optional) Create a primary partition with fdisk to get /dev/vdb1, then use kpartx -a /dev/vdb to reread the partition table
Use vgextend vg_name /dev/vdb1 (or /dev/vdb if you did not create a partition)
You're done.
Another way to do it
go in the make a disk rescan and after you can do your lvm resize.
If you have LVM in your VM then this is crazy easy and fast.
sudo system-config-lvm
in terminal)*.I found the GUI quite intuitive, but follow next steps if you have problems.
Note! At least on CentOS 6 LVM GUI is not installed by default, but you can install it with
yum install system-config-lvm
.