Title: Create and Import Virtual Machines
Course: CIS 208: Linux/UNIX III: Intermediate System Administration, Networking, and Security ~ For IGNITE Review > Section: Learning Modules

The virt-install command will create a domain with options from the command line.  To make this process more convenient, create variables which contain the options for the new domain.  This process could easily be put into a script.

Choose a name for the domain and store it in the Name variable.

Name="--name server1"

Run the virt-install command to see the list of available operating system versions.  By selecting the appropriate operating system, the domain will inherit default settings which will match the operating system.  Set the OS variable with one of the options.

virt-install --os-variant=list | more

OS="--os-variant=rhel6"
OS="--os-variant=rhel7"
OS="--os-variant=freebsd8"

Next select a network, if desired.  If using bridged networking, select the host's interface to use.  Set the Net variable with the selected network options.

virsh net-list

Net="--network network=default"
Net="--network bridge=virbr0"
Net="--network bridge=br0,mac=52:54:00:00:00:00"

Select a disk option.  Valid choices are a pre-existing image file, an image file to be created on the fly, or no disks.  The size option is a value in GigaBytes for disks to be created.  Set the Disk variable with the selected options.

Disk="--disk /var/lib/libvirt/images/$Name.img"
Disk="--disk /var/lib/libvirt/images/$Name.img,sparse=false,size=8"
Disk="--disk /var/lib/libvirt/images/$Name.qcow2,sparse=true,size=8"
Disk="--livecd --nodisks"

A source is needed, unless the hard drive image already has an operating system installed.  For example, imported hard drive images typically already have the OS installed.  The source can be an iso image file, a PXE boot server, or a URL to a repository.  Store the selected source in the Src variable.  Either set or clear the KS variable depending on whether or not a kickstart file is to be used.

Src="--cdrom=/var/lib/libvirt/images/LiveCd.iso"
Src="--pxe"
Src="-l=http://mirror.centos.org/centos/6/os/x86_64"

KS="-x ks=http://172.18.1.1/centos6-64.ks"
KS=""

Next select a graphics option.  Note that a password can be selected if using VNC.  Store the selection in the Gr variable.

Gr="--graphics spice"
Gr="--graphics none"
Gr="--graphics vnc"
Gr="--graphics vnc,password=foo"

Finally select the number of virtual cpus and the amount of ram in megabytes.

Cpu="--vcpus 2"

Ram="--ram 1024"

The domain can be created.  And if any options need to be changed, undefine the domain, update the options, then create the domain again.

virt-install $OS $Net $KS $Disk $Src $Gr $As $Cpu $Ram $Name
virsh destroy $Name; virsh undefine $Name
Gr="--graphics vnc"
virt-install $OS $Net $KS $Disk $Src $Gr $Cpu $Ram $Name

Use the virt-viewer command to connect to the console.  If ssh is not configured appropriately, use the virt-manager command instead.

virt-viewer --connect qemu+ssh://host/system $Name &

sudo virt-manager &