Increase disk of kvm client

What happens if your virtual kvm machine runs out of diskspace and you have no possibility to mount something like a livecd to resize or open a graphical interface like virt-manager?

You need to do it on bash console. And it is not really complicated. In my situation I don’t really use a kvm image. I use something like a real disk.

Get disk location

First of all we need to figure out where we can find that disk. Therefore we call the following command on our host machine where VM_NAME is the name of our virtual machine (you can get it via virsh list --all):

$ virsh dumpxml VM_NAME

now we get the output on command line. The important line is <source file='path/to/disk'>.

check kvm pool

Just to be sure, check if the vm is in the default pool. If you have nothing like that configured, it should be.

$ virsh vol-list --pool default
...
VM_NAME-disk /var/lib/libvirt/images/VM_NAME-disk
...

shutdown vm

Now we have all relevant informations we need and can shutdown the vm:

$ ssh VM_NAME
$ poweroff

increase disk

If the VM is completely shut down (you can check it with virsh list --all), we can resize the VM disk:

$ virsh vol-resize VM_NAME-disk 100G --pool default 
Size of volume 'VM_NAME-disk' successfully changed to 100G

start vm

If we have successfully resized the disk volume, we can start it again:

$ virsh start VM_NAME

resize disk inside the vm

Now we have resized the disk itself, but not the partition inside our vm.

Here I have a very simple partition table with just one root partition and one for swap.

$ ssh VM_NAME
$ fdisk -l
Disk /dev/sda: 107.4 GB, 107374182400 bytes
255 heads, 63 sectors/track, 13054 cylinders, total 209715200 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
Disk identifier: 0x000e276e
   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048   100665343    50331648   83  Linux
/dev/sda2       100667390   104855551     2094081    5  Extended
/dev/sda5       100667392   104855551     2094080   82  Linux swap / Solaris

With fdisk we are going to adapt this partition table so it will match the new size. First we have to delete all existing partitions and create the “new” one.

$ fdisk /dev/sda
Command (m for help): d
Partition number (1-5): 5

Command (m for help): d
Partition number (1-5): 2

Command (m for help): p
Disk /dev/sda: 107.4 GB, 107374182400 bytes
255 heads, 63 sectors/track, 13054 cylinders, total 209715200 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
Disk identifier: 0x000e276e
   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048   100665343    50331648   83  Linux
Command (m for help): d
Selected partition 1
Command (m for help): n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-209715199, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-209715199, default 209715199): +90G

Command (m for help): p
Disk /dev/sda: 107.4 GB, 107374182400 bytes
255 heads, 63 sectors/track, 13054 cylinders, total 209715200 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
Disk identifier: 0x000e276e
   Device Boot      Start         End      Blocks   Id  System
/dev/sda1            2048   188745727    94371840   83  Linux
Command (m for help): n
Partition type:
   p   primary (1 primary, 0 extended, 3 free)
   e   extended
Select (default p): e
Partition number (1-4, default 2):
Using default value 2
First sector (188745728-209715199, default 188745728):
Using default value 188745728
Last sector, +sectors or +size{K,M,G} (188745728-209715199, default 209715199):
Using default value 209715199

Command (m for help): n
Partition type:
   p   primary (1 primary, 1 extended, 2 free)
   l   logical (numbered from 5)
Select (default p): l
Adding logical partition 5
First sector (188747776-209715199, default 188747776):
Using default value 188747776
Last sector, +sectors or +size{K,M,G} (188747776-209715199, default 209715199):
Using default value 209715199
Command (m for help): t
Partition number (1-5): 5
Hex code (type L to list codes): 82
Changed system type of partition 5 to 82 (Linux swap / Solaris)

Command (m for help): p
Disk /dev/sda: 107.4 GB, 107374182400 bytes
255 heads, 63 sectors/track, 13054 cylinders, total 209715200 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
Disk identifier: 0x000e276e
   Device Boot      Start         End      Blocks   Id  System
/dev/sda1            2048   188745727    94371840   83  Linux
/dev/sda2       188745728   209715199    10484736    5  Extended
/dev/sda5       188747776   209715199    10483712   82  Linux swap / Solaris
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 or after you run partprobe(8) or kpartx(8)
Syncing disks.

With the t command we change the partition code for our logical partition to 82 which means Swap

After that we need to restart our vm, because with an fdisk call on the root partition, the kernel still uses the old partition table and will only recognize it if we reboot the host.

reboot and resize partition

$ ssh VM_NAME
$ shutdown -r now
ssh VM_NAME
$ resize2fs /dev/sda1
resize2fs 1.42.5 (29-Jul-2012)
Filesystem at /dev/sda1 is mounted on /; on-line resizing required
old_desc_blocks = 3, new_desc_blocks = 6
Performing an on-line resize of /dev/sda1 to 23592960 (4k) blocks.
The filesystem on /dev/sda1 is now 23592960 blocks long.

Thats it. Now we have resized the vm image up to 100 GB.

comments powered by Disqus