Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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).

share|improve this question
Are you using LVM or disk image in a file? – Selivanov Pavel Oct 24 '11 at 14:27
up vote 70 down vote accepted
  1. stop the VM
  2. run qemu-img resize vmdisk.img +10G to increase image size by 10Gb
  3. start the VM, resize the partitions and LVM structure within it normally
share|improve this answer
Ah, you beat me to it, I just noticed the resize options on qemu-img in the man page. So this save a couple of steps. Nice! – slm Oct 24 '11 at 15:53
it has been around forever. actually, your way will not work with qcow, only with raw images, and will definitely fail if there's a snapshot chain involved – dyasny Oct 24 '11 at 15:55
The version of qemu-img on CentOS5.6 doesn't include the resize option, so your only option is to use the trick of creating a raw 10GB .img file and then catting it together with the existing .img file, like so: cat vmdisk.img addon.raw >> vmdisk_new.img. The 10GB .img file can be created like so: qemu-img create -f raw addon.raw 10G. – slm Dec 7 '11 at 5:40
Just checked in RHEL6.1 - the option is there. – dyasny Dec 7 '11 at 8:54
start the VM, resize the partitions and LVM structure within it normally How to do it? Please explain it. – Waki Jul 2 '15 at 19:17

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:

# 1. stop the VM
# 2. move the current image
mv mykvm.img mykvm.img.bak

# 3. create a new image
qemu-img create -f raw addon.raw 30G

# 4. concatenate the 2 images
cat mykvm.img.bak addon.raw >> mykvm.img

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.

  1. Shutdown the VM
  2. add more space to the guest's "image file" (something like: cat old.img 10G_addon.raw >> new.img
  3. start the VM (using the newly created new.img)
  4. run fdisk inside VM and delete & re-create LVM partition

    % fdisk /dev/vda
    Device Boot      Start         End      Blocks   Id  System
    /dev/vda1   *           1          13      104391   83  Linux
    /dev/vda2              14        3263    26105625   8e  Linux LVM
    Command (m for help): d
    Partition number (1-4): 2
    Command (m for help): p
    Disk /dev/vda: 48.3 GB, 48318382080 bytes
    255 heads, 63 sectors/track, 5874 cylinders
    Units = cylinders of 16065 * 512 = 8225280 bytes
    Device Boot      Start         End      Blocks   Id  System
    /dev/vda1   *           1          13      104391   83  Linux
    Command (m for help): n 
    Command action
      e   extended
      p   primary partition (1-4)
    Partition number (1-4): 2
    First cylinder (14-5874, default 14): 14
    Last cylinder or +size or +sizeM or +sizeK (14-5874, default 5874): 
    Using default value 5874
    Command (m for help): p
    Disk /dev/vda: 48.3 GB, 48318382080 bytes
    255 heads, 63 sectors/track, 5874 cylinders
    Units = cylinders of 16065 * 512 = 8225280 bytes
    Device Boot      Start         End      Blocks   Id  System
    /dev/vda1   *           1          13      104391   83  Linux
    /dev/vda2              14        5874    47078482+  83  Linux
    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: 48.3 GB, 48318382080 bytes
    255 heads, 63 sectors/track, 5874 cylinders
    Units = cylinders of 16065 * 512 = 8225280 bytes
    Device Boot      Start         End      Blocks   Id  System
    /dev/vda1   *           1          13      104391   83  Linux
    /dev/vda2              14        5874    47078482+  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.
    Syncing disks.
  5. Reboot the VM

  6. Resize the LVM physical volume

    % pvdisplay 
      --- Physical volume ---
      PV Name               /dev/vda2
      VG Name               VolGroup00
      PV Size               24.90 GB / not usable 21.59 MB
      Allocatable           yes (but full)
      PE Size (KByte)       32768
      Total PE              796
      Free PE               0
    % pvresize /dev/vda2
    % pvdisplay
      --- Physical volume ---
      PV Name               /dev/vda2
      VG Name               VolGroup00
      PV Size               44.90 GB / not usable 22.89 MB
      Allocatable           yes 
      PE Size (KByte)       32768
      Total PE              1436
      Free PE               640
  7. Resize the LVM Logical Volume

      % lvresize /dev/VolGroup00/LogVol00 -l +640
      Extending logical volume LogVol00 to 43.88 GB
      Logical volume LogVol00 successfully resized
  8. Grow the File system

      % resize2fs /dev/VolGroup00/LogVol00 
      resize2fs 1.39 (29-May-2006)
      Filesystem at /dev/VolGroup00/LogVol00 is mounted on /; on-line resizing required
      Performing an on-line resize of /dev/VolGroup00/LogVol00 to 11501568 (4k) blocks.
      The filesystem on /dev/VolGroup00/LogVol00 is now 11501568 blocks long.

The above is my example, but I followed the steps on this website

share|improve this answer
method #1 in this post doesn't seem to work that well - virtual machine manager in red hat does not report the new size afterwards. The method suggested by @dyasny seems to work better at-least in this respect. – Iwan Aucamp May 30 '14 at 8:36
@IwanAucamp - interesting, I use virt-manager and have used method #1 & #2 extensively before I migrated our KVM servers to a newer version of KVM (CentOS 5 -> CentOS 6). I now use the qemu-img resize method as well. – slm May 30 '14 at 11:53
For method #2, you might find lvextend handier as step 7: lvextend /dev/Volgroup/lvname /dev/vda2 (all on one line, where /dev/vda2 would be your physical volume). Without any other options, it will use the maximum size in that volume group. – Mike S May 5 '15 at 18:42

Online Method (using qemu, libvirt, and virtio-block)

Perform the following from the KVM hypervisor.

  1. Increase the size of the disk image file itself (specify the amount to increase):

    qemu-img resize <my_vm>.img +10G
  2. Get the name of the virtio device, via the libvirt shell (drive-virtio-disk0 in this example):

    virsh qemu-monitor-command <my_vm> info block --hmp
      drive-virtio-disk0: removable=0 io-status=ok file=/var/lib/libvirt/images/<my_vm>.img ro=0 drv=raw encrypted=0
      drive-ide0-1-0: removable=1 locked=0 tray-open=0 io-status=ok [not inserted]
  3. Signal the virtio driver to detect the new size (specify the total new capacity):

    virsh qemu-monitor-command <my_vm> block_resize drive-virtio-disk0 20G --hmp

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.

share|improve this answer

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:

PV         VG     Fmt  Attr PSize  PFree
/dev/vda1  vgWWW  lvm2 a-   30.00g    0

VG     #PV #LV #SN Attr   VSize  VFree
vgWWW    1   2   0 wz--n- 30.00g    0

LV   VG    Attr   LSize 
root vgWWW -wi-ao 28.80g
swap vgWWW -wi-ao  1.19g

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:

  1. Shut down the VM
  2. On the host create a new virtual disk file and add it to the VM
  3. Restart the VM and login
  4. Mark the new drive as being a physical volume for LVM with sudo pvcreate /dev/vdb
  5. Extend the volume group to include this new block of disk with sudo vgextend vgWWW /dev/vdb
  6. Extend the logical volume to take up the new space with sudo lvextend --extents +100%FREE /dev/vgWWW/root (or something like sudo 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)
  7. Resize the filesystem with 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 run fsck 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.

share|improve this answer
This can also be done with GUI that is quite nice… – Nux Jun 7 '13 at 16:13

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.

  1. Use virt-manager or virsh to define a new empty disk
  2. The kernel should see a new /dev/vdb
  3. (Optional) Create a primary partition with fdisk to get /dev/vdb1, then use kpartx -a /dev/vdb to reread the partition table

  4. Use vgextend vg_name /dev/vdb1 (or /dev/vdb if you did not create a partition)

  5. Use lvextend -l +100%FREE /dev/vg_name/lv_name
  6. Use resize2fs /dev/vg_name/lv_name to extend your filesystem

You're done.

share|improve this answer
I'll add that ""; should also help. – FearlessFuture Nov 12 '15 at 17:49

If you have LVM in your VM then this is crazy easy and fast.

  1. Turn off VM (guest machine).
  2. In your host machine add new storage device.
  3. Turn on guest.
  4. Open System -> Administration -> Logical Volume Management (or type sudo system-config-lvm in terminal)*.

I found the GUI quite intuitive, but follow next steps if you have problems.

  1. Open "Uninitialized Entities" and find your new drive.
  2. After selecting new drive, below the image you should find an initialize button. Press it.
  3. It will ask you about partitioning - you don't need it.
  4. When done add your drive to existing volume group.
  5. Finally you need to grow your logical volume(s) - this is done in Logical View of the group by editing properties of your volume.
  6. Save and your done. No need to restart VM.

Note! At least on CentOS 6 LVM GUI is not installed by default, but you can install it with yum install system-config-lvm.

share|improve this answer

Another way to do it

truncate -s +2G vm1.img 

go in the make a disk rescan and after you can do your lvm resize.

share|improve this answer

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:

The key command here is: virt-resize

  • this is a libvirt utility
  • it can expand a guest disk AND expand the partitions inside in one step
  • it copies the disk, it doesn't expand it in-place
    • this is good because you have the untouched original as a backup


* Install libvirt file system utilities package 
    * sudo apt-get install libguestfs-tools

* Test to see if it works (it won't) -- you need to see "===== TEST FINISHED OK =====" at the bottom:
    * sudo libguestfs-test-tool

* If you don't see "===== TEST FINISHED OK =====" at the bottom then repair it:
    * sudo update-guestfs-appliance

* Run the test again and verify it works
    * sudo libguestfs-test-tool

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:

qcow: sudo qemu-img create -f qcow2 -o preallocation=metadata outdisk 40G
img:  sudo truncate -s 40G outdisk

4) Copy the old to the new while expand the appropriate partition (assuming your disk partition from step 2 was /dev/sda1):

sudo virt-resize --expand /dev/sda1 indisk outdisk

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!

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.