Fotolia
Use virt-install to install VMs from the command line
Learn the basics of virt-install, including standard use cases, executing it on CentOS 7, different command-line options and the creation of kickstart files.
Virt-install is an easy way for virtualization administrators to automatically install VMs from the command line. It's used as an alternative to tools like VirtualBox or virt-manager, which are more complex and interactive.
Virt-install use cases
Virt-install is used to roll out new VMs without having to use some large and complicated system like OpenStack. This native Linux feature can also be used so you don't have to install Oracle VM VirtualBox, a VMware product or a third-party product. Also, you can use libvirt package templates as kickstart files, so once you decide on a configuration, you can copy it to other servers.
Virt-install can also be used when you want to create a paravirtualized environment, which is a VM inside a VM -- meaning one that is virtual and not hardware-based. This enables you to experiment with different virtualization technologies, like QEMU and KVM. You can also set up a Xen hypervisor without having to use Xen-specific tools. Although there are additional use cases, these are the most common.
Because you have setup scripts, you can add them to your Puppet, Ansible or Salt systems and push VMs out from a centralized mechanism, albeit one that's less overarching than OpenStack.
Execute virt-install
You can either kick off the install and then finish with a GUI using a virtual network computing (VNC) client -- like connecting to port 5900 -- or you can use a kickstart file to answer all the questions that a GUI install would include, like keyboard type and language, time zone, and, for CentOS, whether to enable networking. Another option is to use the ttyS0 serial interface to connect to the machine and complete those steps.
Normally, you would use a host OS to install a VM. Although you can use a VM to install another VM, you won't be able to connect to the VM within a VM unless you set up some kind of port forwarding and export the display -- in other words, you'd have to understand how to get the X graphical protocol working across a network.
Use virt-install on CentOS 7
In order to use virt-install, you need to install the following:
yum install qemu-kvm qemu-img libvirt libvirt-python libvirt-client virt-install virt-viewer bridge-utils tigervnc-server
Then, you can start the daemon:
systemctl start libvirtd
Now, download the OS you want to install, and then copy it to the folder shown below:
cp CentOS-7-x86_64-Everything-1708.iso /var/lib/libvirt/images/
Finally, run virt-install. Here is an example of what the script should look like:
os="--os-type=linux --os-variant=centos7.0"
location="--location=/var/lib/libvirt/images/CentOS-7-x86_64-Everything-1708.iso"
cpu="--vcpus 2"
ram="--ram 2048"
name="centos7"
disk="--disk /dev/mapper/centos_192-root,size=40"
type="--virt-type qemu"
network="--network network=default"
graphics="--graphics none"
virt-install $os $network $disk $location $cpu $ram $type $disk $graphics --name=$name
After running virt-install, verify that the VM is running using virsh:
virsh list
Id Name State
----------------------------------------------------
4 centos7 running
The virsh command line provides you with a lot of options, including stopping VMs and erasing them.
Virt-install command-line options
Below are some command-line options, but this isn't an extensive list, so I encourage you to do some research in order to fully take advantage of this tool.
os="--os-type=linux --os-variant=centos7.0" -- Some of these commands have main options, as well as sub options. For example, if you type os-type=linux, then you need to further specify --os-variant=centos7.0. You can get a list of OSes that virt-install supports by typing osinfo-query os.
location="--location=/var/lib/libvirt/images/CentOS-7-x86_64-Everything-1708.iso" -- This is where you've copied the ISO image file containing the OS you want to install.
cpu="--vcpus 2" -- The CPU command-line option enables you to specify the number of vCPUs assigned to the VM. In this example, I'm assigning two vCPUs.
ram="--ram 2048" -- The RAM command-line option enables you to specify the amount of memory assigned to the VM. In this example, I'm assigning 2,048 MBs -- or 2 GBs.
name="centos7" -- The name command-line option enables you to assign a name to the VM. In this example, I'm naming the VM centos7.
disk="--disk /dev/mapper/centos_192-root,size=40" -- This is where the VM will be installed and the size, in gigabytes, to be allocated. This must be a disk partition and not a mount point. Type df -h to list disk partitions.
type="--virt-type qemu" -- The type command-line enables you to choose the type of VM you want to install. You can use KVM, QEMU, Xen or KQEMU. Type virsh capabilities to list all of the options. In this example, I'm using QEMU.
network="--network network=default" -- Use network=default to set up bridge networking using the default bridge device. This is the easiest method, but there are other options.
graphics="--graphics none" -- The graphics command-line option specifies that no graphical VNC or SPICE interface should be created. Use this for a kickstart installation or if you want to use a ttyS0 serial connection.
Create a kickstart file
For an unattended install, create a kickstart file and tell virt-install to use it by adding the following to the command line:
--extra-args "ks=file:/ks.cfg console=ttys0"
There's a good example of a minimal kickstart file on the CentOS blog. You can also create the file from Red Hat -- as CentOS is the free version of Red Hat.