centos creating a virtual machine


references: http://virt-tools.org/learning/install-with-command-line/

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:

  • --vcpus=N Specify an SMP guest with N virtual CPUs.
  • --description Give a description string which appears in the libvirt XML.
  • -l Use this to install from a network URL (instead of needing to download an ISO). See network installs below.
  • --disk This option lets you specify other aspects of the disk such as the format (qcow2 instead of raw). See the man page for the full details.
  • --soundhw ac97 Give the guest a (virtual) AC'97 soundcard. Without this option no soundcard is provided for the guest.
2016-03-16 11:35:24gstlouis

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.

2016-03-16 11:35:30