Files as storage devices for KVM guests

A note on adding extra raw files for extra storage to KVM guests.



Create the "empty" "image" 1GB file named vm4_xtra.img
# dd if=/dev/zero of=/home/vm/vm4_xtra.img bs=1M count=1024


For larger files you would not want to use dd. Use fallocate instead eg:
# fallocate -l 50G vm4_xtra.img


Backup KVM host configuration
# cd /etc/libvirt/qemu/
# stor vm4.xml 
devz:vm4.xml is at ./stor/vm4.xml.0


Add the new virtual drive to the KVM guest configuration. eg:
 
    <disk type='file' device='disk'>
      <driver name='qemu' type='raw'/>
      <source file='/home/vm/vm4_xtra.img'/>
      <target dev='hda' bus='ide'/>
    <address type='drive' controller='0' bus='1' unit='0'/>
   </disk>

  


Redefine KVM guest
# virsh
virsh # define /etc/libvirt/qemu/vm4.xml 
Domain vm4 defined from /etc/libvirt/qemu/vm4.xml


Start KVM guest
virsh # start vm4
Domain vm4 started
virsh # quit


Log into the KVM guest and list drives
# fdisk -l
The new virtual HD should be /dev/sdb

Partition /dev/sdb
# fdisk /dev/sdb
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0x54ac0969.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content won't be recoverable.

Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)

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-2097151, default 2048): 
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-2097151, default 2097151): 
Using default value 2097151

Command (m for help): t
Selected partition 1
Hex code (type L to list codes): 83

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.


Format
# mkfs.ext4 /dev/sdb1


Adjust root reserved blocks percentage
# tune2fs -m 0 /dev/sdb1
tune2fs 1.42.5 (29-Jul-2012)
Setting reserved blocks percentage to 0% (0 blocks)


Mount
# mkdir /vm4_xtra
# mount /dev/sdb1 /vm4_xtra/


Adjust fstab
# echo "/dev/sdb1  /vm4_xtra  ext4  defaults  0  2" >> /etc/fstab


The system used
# cat /etc/issue /etc/debian_version
Debian GNU/Linux 6.0 \n \l

6.0.7
# uname -r
2.6.32-5-amd64






adding file storage devices in KVM guests