Installing a virtual machine using virt-install

(from Tutorials and videos)

Creating a virtual machine with virt-install involves the same steps as doing it graphically with virt-manager.

First you need to download an ISO of the operating system you want to install or you can do a network install.

Create space for the virtual disk #

There are several decisions you have to make about how and where you want to create the virtual hard disk for the new guest:

  1. How large? Decide how large you want the disk image to be. This is the maximum amount of disk space that the virtual machine will be able to use, unless you take manual steps to increase it (using virt-resize), so choose this wisely. Even if you choose a format which grows (like sparse or qcow2), this is a hard limit that you cannot easily grow beyond. Below are some rough guides for minimum operating system only space. Remember if you want to store significant amounts of data in the guest, you must add that requirement on top of these figures.
    Minimal eg. Debian or FreeBSD, text only, configured with just the essential components. 2048 MB
    Linux Typical Linux distribution, base install with graphical components. 8192 MB
    Windows Recent version of Windows. 16384 MB
    An example calculation: a Linux virtual machine for developers — 8192 (base OS) + 1000 (compiler tools) + 20000 (space to check out and compile software in home directory) = 29192 MB.
  2. File, logical volume or SAN? Storing the disk as a plain host file is the easiest option. As long as you have enough free space, you can put the file anywhere, and move it around later. However file access isn't very fast, so for the best performance you should choose an LVM2 logical volume (LV) or (if you have it) a dedicated LUN on your SAN storage. Using a host partition or host disk directly is not recommended for security reasons.
  3. Fully allocated or sparse? If you choose file then you also have the option of using a sparse file. Sparse files are allocated on demand (as the guest writes to them), which means you don't need to dedicate disk space up-front. On the other hand, sparse allocation is slow, and if you allow the host to run out of disk space this can cause dangerous data loss in the guest.
  4. Raw or qcow2? Again for file storage, you can choose a raw disk (just a massive file which is a direct image of what is in the guest's disk), or the more flexible qcow2 format. qcow2 images grow on-demand (a bit like sparse files), and there are other features like snapshotting. However out of all the file options, qcow2 is the slowest.
  5. Snapshot? If you chose LV, qcow2 or SAN, then you might want to clone an existing guest. Snapshots are out of scope for this document. For more information we suggest reading the lvcreate(8) and/or qemu-img(1) man pages.
  6. libvirt storage pools? Libvirt now features storage management, and you can store your VM disk images in a volume in a libvirt storage pool. This is out of scope for the current document, but we suggest reading about libvirt storage management here.

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:

Advanced topic: Installing over the network #

Instead of downloading the ISO, you can install from public repositories over HTTP. To do this, remove the --cdrom option and instead specify the -l URL option. Some common locations that virt-install knows how to handle:

Back to Tutorials and videos.

Copyright © 2011 Red Hat, Inc. — License: GPL or CC — Last updated on 13:42:47 14-Jun-2011