Skip to content

Commit

Permalink
Xen support (#40)
Browse files Browse the repository at this point in the history
* Add command wrapper for verbose mode

Add a wrapper function to automatically print command arguments when
running in verbose mode. Currently, this is only done for the `virsh
pool-create-as` and `virt-install` commands, but more commands could
be added in the future.

Having the wrapper run the command removes the redundant command
arguments and will allow us to more easily support a variable number
of virt-install arguments

This commit introduces the following shell helper functions:

    join             Join strings into a single string, joined
                     by a delimiter.

    output_command   Print the command arguments for verbose mode,
                     formatted as one argument per line, with line
                     continuation characters as needed.

    run              Command execution wrapper to print the command
                     when running in verbose mode and to redirect
                     output to the current log file.

* Support virt-install graphics auto-detection

Typically, the `virt-install` user does not need to specify the
--graphics option. When the --graphics flag is not specified,
virt-install will try and choose a useful default and launch a suitable
connection.

Support graphics auto-detection by omitting the virt-install --grahics
option when the kvm-install-vm '-g' flag is set to 'auto' or the
GRAPHICS variable in the ~/.kivrc file is set to 'auto'.

* Hypervisor custom parameters

Support custom virt-install parameters for advanced usage. This allows
for advanced customization and to support Xen and other non-KVM
hypervisors.

Users may specify hypervisor specific settings in the $HOME/.kivrc
customization file, without resorting to patching kvm-install-vm.

With this change, none of the virt-install parameters are hardcoded in
the script, values may be specified to be empty to avoid setting a given
parameter, and additional options may be provided as needed.

This commit adds the following custom virt-install options:

    NETWORK_MODEL       --network model value (default: virtio)
    NETWORK_EXTRA       extra --network options
    DISK_BUS            --disk bus type (default: virtio)
    DISK_EXTRA          extra --disk parameters
    CI_ISO_DEVICE       ci cdrom device type (default: cdrom)
    CI_ISO_EXTRA        extra ci cdrom parameters (default: "")
    GRAPHICS_LISTEN     --graphics listen value (default: localhost)
    GRAPHICS_EXTRA      extra graphics parameters
    VIRT_INSTALL_EXTRA  extra virt-install options

The following virt-install options are dependent on the image being
installed, and so are determined the value of -t command line option.

    OS_TYPE             --os-type (currently always "linux")
    DISK_FORMAT         --disk format (currently always "qcow2")
  • Loading branch information
meffie authored and giovtorres committed Aug 23, 2019
1 parent 693e8b9 commit d053064
Show file tree
Hide file tree
Showing 2 changed files with 171 additions and 49 deletions.
35 changes: 35 additions & 0 deletions .kivrc
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,38 @@

# Verbosity
#VERBOSE=0


#
# Advanced virt-install options.
#
# You probably will not need to change any of the following
# unless you are running a non-KVM hypervisor, such as Xen.
#

# The --network model type.
#NETWORK_MODEL=virtio

# Additional --network parameters.
#NETWORK_EXTRA=""

# The --disk bus type. Specify none to auto-detect.
#DISK_BUS=virtio

# Additional --disk parameters.
#DISK_EXTRA=""

# The CI ISO --disk device type.
#CI_ISO_DEVICE=cdrom

# Additional CI ISO --disk parameters.
#CI_ISO_EXTRA=""

# The --graphics listen parameter.
#GRAPHICS_LISTEN=localhost

# Additional --graphics parameters.
#GRAPHICS_EXTRA=""

# Additional virt-install options.
#VIRT_INSTALL_EXTRA=""
185 changes: 136 additions & 49 deletions kvm-install-vm
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,58 @@ ok() { green "${@:-OK}" ; }
pushd() { command pushd "$@" >/dev/null ; }
popd() { command popd "$@" >/dev/null ; }

# Join zero or more strings into a delimited string.
function join ()
{
local sep="$1"
if [ $# -eq 0 ]; then
return
fi
shift
while [ $# -gt 1 ]; do
printf "%s%s" "$1" "$sep"
shift
done
printf "%s\n" "$1"
}

# Print an optional name=value[,value,..] parameter.
# Prints nothing if no values are given.
function param ()
{
if [ $# -lt 2 ]; then
return # skip empty value
fi
local name="$1"
shift
local values="$(join ',' "$@")"
printf "%s=%s\n" $name $values
}

# Output a command, one argument per line.
function output_command ()
{
local line_cont=$' \\ \n '
local command_lines=$(join "$line_cont" "$@")
printf " %s\n" "$command_lines"
}

# Command wrapper to output the command to be run in verbose
# mode and redirect stdout and stderr to the vm log file.
function run ()
{
local msg="$1"
shift
if [ "${VERBOSE}" -eq 1 ]
then
output "$msg with the following command"
output_command "$@"
else
outputn "$msg"
fi
( "$@" &>> ${VMNAME}.log && ok )
}

# Detect OS and set wget parameters
function set_wget ()
{
Expand Down Expand Up @@ -235,69 +287,91 @@ function fetch_images ()
case "$DISTRO" in
amazon2)
QCOW=amzn2-kvm-2.0.20190313-x86_64.xfs.gpt.qcow2
OS_TYPE="linux"
OS_VARIANT="auto"
IMAGE_URL=https://cdn.amazonlinux.com/os-images/2.0.20190313/kvm
DISK_FORMAT=qcow2
LOGIN_USER=ec2-user
;;
centos7)
QCOW=CentOS-7-x86_64-GenericCloud.qcow2
OS_TYPE="linux"
OS_VARIANT="centos7.0"
IMAGE_URL=https://cloud.centos.org/centos/7/images
DISK_FORMAT=qcow2
LOGIN_USER=centos
;;
centos7-atomic)
QCOW=CentOS-Atomic-Host-7-GenericCloud.qcow2
OS_TYPE="linux"
OS_VARIANT="centos7.0"
IMAGE_URL=http://cloud.centos.org/centos/7/atomic/images
DISK_FORMAT=qcow2
LOGIN_USER=centos
;;
centos6)
QCOW=CentOS-6-x86_64-GenericCloud.qcow2
OS_TYPE="linux"
OS_VARIANT="centos6.9"
IMAGE_URL=https://cloud.centos.org/centos/6/images
DISK_FORMAT=qcow2
LOGIN_USER=centos
;;
debian8)
# FIXME: Not yet working.
QCOW=debian-8-openstack-amd64.qcow2
OS_TYPE="linux"
OS_VARIANT="debian8"
IMAGE_URL=https://cdimage.debian.org/cdimage/openstack/current-8
DISK_FORMAT=qcow2
LOGIN_USER=debian
;;
debian9)
QCOW=debian-9-openstack-amd64.qcow2
OS_TYPE="linux"
OS_VARIANT="debian9"
IMAGE_URL=https://cdimage.debian.org/cdimage/openstack/current-9
DISK_FORMAT=qcow2
LOGIN_USER=debian
;;
fedora29)
QCOW=Fedora-Cloud-Base-29-1.2.x86_64.qcow2
OS_TYPE="linux"
OS_VARIANT="fedora29"
IMAGE_URL=https://download.fedoraproject.org/pub/fedora/linux/releases/29/Cloud/x86_64/images
DISK_FORMAT=qcow2
LOGIN_USER=fedora
;;
fedora29-atomic)
QCOW=Fedora-AtomicHost-29-20190611.0.x86_64.qcow2
OS_TYPE="linux"
OS_VARIANT="fedora29"
IMAGE_URL=https://download.fedoraproject.org/pub/alt/atomic/stable/Fedora-29-updates-20190611.0/AtomicHost/x86_64/images/
DISK_FORMAT=qcow2
LOGIN_USER=fedora
;;
fedora30)
QCOW=Fedora-Cloud-Base-30-1.2.x86_64.qcow2
OS_TYPE="linux"
OS_VARIANT="fedora29"
IMAGE_URL=https://download.fedoraproject.org/pub/fedora/linux/releases/30/Cloud/x86_64/images
DISK_FORMAT=qcow2
LOGIN_USER=fedora
;;
ubuntu1604)
QCOW=ubuntu-16.04-server-cloudimg-amd64-disk1.img
OS_TYPE="linux"
OS_VARIANT="ubuntu16.04"
IMAGE_URL=https://cloud-images.ubuntu.com/releases/16.04/release
DISK_FORMAT=qcow2
LOGIN_USER=ubuntu
;;
ubuntu1804)
QCOW=ubuntu-18.04-server-cloudimg-amd64.img
OS_TYPE="linux"
OS_VARIANT="ubuntu18.04"
IMAGE_URL=https://cloud-images.ubuntu.com/releases/18.04/release
DISK_FORMAT=qcow2
LOGIN_USER=ubuntu
;;
*)
Expand Down Expand Up @@ -535,65 +609,67 @@ _EOF_
|| die "Could not generate ISO."
fi

if [ "${VERBOSE}" -eq 1 ]
then
output "Creating storage pool with the following command"
printf " virsh pool-create-as \\ \n"
printf " --name ${VMNAME} \\ \n"
printf " --type dir \\ \n"
printf " --target ${VMDIR}/${VMNAME} \n"
else
outputn "Creating storage pool"
fi

# Create new storage pool for new VM
(virsh pool-create-as \
--name ${VMNAME} \
--type dir \
--target ${VMDIR}/${VMNAME} &>> ${VMNAME}.log && ok) \
run "Creating storage pool" \
virsh pool-create-as \
--name=${VMNAME} \
--type=dir \
--target=${VMDIR}/${VMNAME} \
|| die "Could not create storage pool."

# Add custom MAC Address if specified
if [ -z "${MACADDRESS}" ]
NETWORK_PARAMS="$(join ',' \
$(param bridge ${BRIDGE}) \
$(param model ${NETWORK_MODEL}) \
$(param mac ${MACADDRESS}) \
${NETWORK_EXTRA})"

# Assemble disk parameters.
DISK_PARAMS="$(join ',' \
${DISK} \
$(param format ${DISK_FORMAT}) \
$(param bus ${DISK_BUS}) \
${DISK_EXTRA})"

# Assemble CI ISO disk parameters.
CI_ISO_PARAMS="$(join ',' \
${CI_ISO} \
$(param device ${CI_ISO_DEVICE}) \
${CI_ISO_EXTRA})"

# Omit the --graphics option to auto-detect.
if [ "${GRAPHICS}" = 'auto' ]
then
NETWORK_PARAMS="bridge=${BRIDGE},model=virtio"
GRAPHICS_PARAMS=""
else
NETWORK_PARAMS="bridge=${BRIDGE},model=virtio,mac=${MACADDRESS}"
GRAPHICS_PARAMS="$(join ',' \
${GRAPHICS} \
$(param port ${PORT}) \
$(param listen ${GRAPHICS_LISTEN}) \
${GRAPHICS_EXTRA})"
fi

if [ "${VERBOSE}" -eq 1 ]
then
output "Installing the domain with the following command"
printf " virt-install \\ \n"
printf " --import \\ \n"
printf " --name ${VMNAME} \\ \n"
printf " --memory ${MEMORY} \\ \n"
printf " --vcpus ${CPUS} \\ \n"
printf " --cpu ${FEATURE} \\ \n"
printf " --disk ${DISK},format=qcow2,bus=virtio \\ \n"
printf " --disk ${CI_ISO},device=cdrom \\ \n"
printf " --network ${NETWORK_PARAMS} \\ \n"
printf " --os-type=linux \\ \n"
printf " --os-variant=${OS_VARIANT} \\ \n"
printf " --graphics ${GRAPHICS},port=${PORT},listen=localhost \\ \n"
printf " --noautoconsole \n"
else
outputn "Installing the domain"
fi
# Assemble virt-install options.
NETWORK_OPTION="$(param --network ${NETWORK_PARAMS})"
DISK_OPTION="$(param --disk ${DISK_PARAMS})"
CI_ISO_OPTION="$(param --disk ${CI_ISO_PARAMS})"
GRAPHICS_OPTION="$(param --graphics ${GRAPHICS_PARAMS})"

# Call virt-install to import the cloud image and create a new VM
(virt-install --import \
--name ${VMNAME} \
--memory ${MEMORY} \
--vcpus ${CPUS} \
--cpu ${FEATURE} \
--disk ${DISK},format=qcow2,bus=virtio \
--disk ${CI_ISO},device=cdrom \
--network ${NETWORK_PARAMS} \
--os-type=linux \
run "Installing the domain" \
virt-install --import \
--name=${VMNAME} \
--memory=${MEMORY} \
--vcpus=${CPUS} \
--cpu=${FEATURE} \
${DISK_OPTION} \
${CI_ISO_OPTION} \
${NETWORK_OPTION} \
--os-type=${OS_TYPE} \
--os-variant=${OS_VARIANT} \
--graphics ${GRAPHICS},port=${PORT},listen=localhost \
--noautoconsole &>> ${VMNAME}.log && ok ) \
--noautoconsole \
${GRAPHICS_OPTION} \
${VIRT_INSTALL_EXTRA} \
|| die "Could not create domain with virt-install."

virsh dominfo ${VMNAME} &>> ${VMNAME}.log
Expand Down Expand Up @@ -693,7 +769,7 @@ function set_defaults ()
MEMORY=1024 # Amount of RAM in MB
DISK_SIZE="" # Disk Size in GB
DNSDOMAIN=example.local # DNS domain
GRAPHICS=spice # Graphics type
GRAPHICS=spice # Graphics type or "auto"
RESIZE_DISK=false # Resize disk (boolean)
IMAGEDIR=${HOME}/virt/images # Directory to store images
VMDIR=${HOME}/virt/vms # Directory to store virtual machines
Expand All @@ -710,6 +786,17 @@ function set_defaults ()

# Reset OPTIND
OPTIND=1

# Advanced hypervisor options. Override in ~/.kivrc if needed.
NETWORK_MODEL=virtio
NETWORK_EXTRA=""
DISK_BUS=virtio
DISK_EXTRA=""
CI_ISO_DEVICE=cdrom
CI_ISO_EXTRA=""
GRAPHICS_LISTEN=localhost
GRAPHICS_EXTRA=""
VIRT_INSTALL_EXTRA=""
}

function set_custom_defaults ()
Expand Down

0 comments on commit d053064

Please sign in to comment.