Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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

6 Answers 6

up vote 17 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

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)
    p
    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 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 at 11:53

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 centos.org/docs/5/html/Deployment_Guide-en-US/… –  Nux Jun 7 '13 at 16:13

An othere 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

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

  1. Turn of 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

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

Your Answer

 
discard

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.