[% topdir = "../.." -%] [% PROCESS globals -%] [% WRAPPER page title = "Installing a virtual machine using virt-install" h1 = "Installing a virtual machine using virt-install" section = "learning" local_stylesheet = 1 %]
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.
[% WRAPPER h2 h2="Create space for the virtual disk" anchor="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:
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 |
filethen 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.
lvcreate(8)
and/or qemu-img(1) man pages.
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.
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.
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:
-l http://download.fedoraproject.org/pub/fedora/linux/releases/13/Fedora/i386/os/
Change 13to the Fedora release, and
i386to
x86_64 for a 64 bit guest.
-l http://ftp.us.debian.org/debian/dists/stable/main/installer-i386/
Change usto your country code (for faster access to a local mirror), and
i386to
amd64 for a 64 bit guest.
-l http://ftp.ubuntu.com/ubuntu/dists/maverick/main/installer-i386/
Change maverickto the name of the version of Ubuntu to install, and
i386to
amd64 for a 64 bit guest.