centos creating a virtual machine
With these decisions made, you can now go ahead and create the storage for the guest. The examples below all assume that the disk will be 8192 MB in size (as the hard upper limit). Adjust the number as required.
To create a fully-allocated (non-sparse) raw file:
dd if=/dev/zero of=/var/lib/libvirt/images/guest.img bs=1M count=8192
or for newer versions of Linux, use the faster fallocate(1) program:
fallocate -l 8192M /var/lib/libvirt/images/guest.img
To create a sparse raw file:
rm -f /var/lib/libvirt/images/guest.img truncate --size=8192M /var/lib/libvirt/images/guest.img
To create a qcow2 file:
qemu-img create -f qcow2 /var/lib/libvirt/images/guest.qcow2 8192
To create an LVM2 logical volume in the volume group called vg_host:
lvcreate -n lv_guest -L 8192M /dev/vg_host
SAN LUN creation depends on your SAN, and you should consult that documentation.
To create a libvirt volume in the default storage pool, do:
virsh vol-create-as default guest 8192M
The libvirt default storage pool is a directory /var/lib/libvirt/images, and you'll find the disk image under there. virsh vol-create-as has several other options, and you might want to consult the virsh(1) man page.
Create the virtual machine #
Now you can create the virtual machine itself from the ISO which you downloaded and the disk image that you created.
This is the basic virt-install command:
virt-install -r 1024 --accelerate -n Fedora14 \ -f /path/to/guest.img \ --cdrom Fedora-14-x86_64-Live.iso
The -r option specifies the amount of RAM (in megabytes). This depends on the operating system, but 768 MB is a good starting point these days, and I use 1024 MB for modern graphical Linux and Windows guests.
--accelerate indicates you want to use hardware acceleration. Recent versions of virt-install default to this.
-n specifies the name of the virtual machine (as known to libvirt), and this is the name you will see in listings and use when starting and stopping the VM.
-f is the full path to the disk image you created before. For LVs, use the device path, eg. -f /dev/vg_host/lv_guest
--cdrom is the path to the ISO file that you downloaded. The ISO is only needed during installation, and can be deleted after that.
Other virt-install options that might be useful (read virt-install(1) for the full list) include:
A sparse file is one where file blocks that would contain all zeroes are omitted from the file (and don’t take up any space in the filesystem). A sparse virtual disk image is the same sort of thing: blocks that the guest hasn’t written to yet are not stored by the host, and read as all zeroes. Sparse disk images can be implemented using sparse files on the host, or you can use a format like qcow2 which inherently supports sparse files.
The problem with sparse files is that they gradually grow. When a guest writes a block it is allocated, and potentially this is never freed, even if the guest deletes the file or writes all zeroes to the block. [Eventually this problem will be solved by implementing the TRIM command which lets the host know that the guest no longer requires a block, but we’re not quite there yet.]
This is of course a problem if you fill up the guest disk and then delete the files. The host file does not regain its sparseness.