virt-install-cloud/virt-install-cloud.sh

224 lines
7.4 KiB
Bash
Raw Normal View History

2018-04-09 22:46:46 -06:00
#!/bin/bash
# Heavily Modified Script By: Brielle Bruns
# Date: 05/13/2018
# URL: https://git.sosdg.org/brielle/virt-install-cloud
# Version: 1.0
2018-04-09 22:46:46 -06:00
#
# Originally based on:
2018-04-09 22:46:46 -06:00
# virt-install-cloud.sh : script to start an OpenStack cloud image on kvm
# version : 1.2
2018-04-09 22:46:46 -06:00
#
# Author : Claude Durocher
# License : GPLv3
#
# ref. http://mojodna.net/2014/05/14/kvm-libvirt-and-ubuntu-14-04.html
#
# requires the following packages on Ubuntu host:
# wget qemu-kvm libvirt-bin virtinst bridge-utils genisoimage|xorriso
# requires the following packages on CentOS host:
# wget qemu-kvm libvirt virt-install bridge-utils genisoimage|xorriso
#
# check if the script is run as root user
if [[ $USER != "root" ]]; then
echo "This script must be run as root!" && exit 1
fi
WORKING_DIR=$(cd -P -- "$(dirname -- "$0")" && pwd -P)
usage() {
echo "${0} Help"
2018-04-11 17:03:36 -06:00
echo -e "\t-h show this help"
echo -e "\t-n hostname *"
echo -e "\t-d domainame *"
echo -e "\t-s disk size (num + count size - M=Megabytes G=Gigabytes) *"
2018-04-29 23:53:32 -06:00
echo -e "\t-r ram size (in megabytes) *"
2018-04-11 17:03:36 -06:00
echo -e "\t-c number of vcpus"
echo -e "\t-b bridge to attach to *"
2018-05-13 19:50:23 -06:00
echo -e "\t-x network info"
2018-05-13 20:00:21 -06:00
echo -e "\t (type:ip:netmask:gateway - type=eni/1, ip=dhcp or address, default eni dhcp)"
echo -e "\t-i distribution to use"
echo -e "\t (see script for supported options) - default is stretch"
2018-04-11 17:03:36 -06:00
echo -e "\t-o OS type and variant (default: generic:generic - also set by distro option when available)"
}
2018-05-13 19:50:23 -06:00
while getopts "c:r:s:n:d:i:o:b:x:h" opt; do
case $opt in
2018-04-19 12:03:32 -06:00
c) cpucount=$OPTARG;;
r) ram=$OPTARG;;
s) storage=$OPTARG;;
n) hostname=$OPTARG;;
d) domain=$OPTARG;;
i) distro=$OPTARG;;
o) ostype=$OPTARG;;
b) bridge=$OPTARG;;
2018-05-13 19:50:23 -06:00
x) netinfo=$OPTARG;;
h) help="yes";;
esac
done
2018-04-19 11:48:22 -06:00
if [[ -z $ram || -z $storage || -z $hostname || -z $domain || -z $bridge || $help == "yes" ]]; then
echo "Error: Incorrect command usage, must provide all * flags."
usage
exit 1
fi
2018-05-13 19:50:23 -06:00
if [[ -f ${WORKING_DIR}/config ]]; then
source ${WORKING_DIR}/config
else
echo "Error: Missing ${WORKING_DIR}/config !"
exit 1
fi
if [[ -f ${WORKING_DIR}/config.local ]]; then
source ${WORKING_DIR}/config.local
fi
if [[ -z $distro ]]; then
distro="stretch"
fi
if [[ -z $cpucount ]]; then
cpucount="2"
fi
2018-05-13 19:50:23 -06:00
if [[ -z $netinfo ]]; then
netinfo="eni:dhcp"
fi
if [[ ! -z $netinfo ]]; then
OIFS=${IFS}
IFS=:
2018-05-13 20:23:19 -06:00
read -ra netinfo_config <<< "$netinfo"
2018-05-13 19:50:23 -06:00
virt_netinfo_type=${netinfo_config[0]}
virt_netinfo_address=${netinfo_config[1]}
virt_netinfo_netmask=${netinfo_config[2]}
virt_netinfo_gateway=${netinfo_config[3]}
IFS=${OIFS}
2018-05-13 20:21:33 -06:00
echo "Configuring network as type=${virt_netinfo_type} addr=${virt_netinfo_address}/${virt_netinfo_netmask}/${virt_netinfo_gateway}"
2018-05-13 19:50:23 -06:00
fi
if [[ ! -z $ostype ]]; then
OIFS=${IFS}
IFS=:
read -ra virt_os <<< "$ostype"
virt_ostype=${virt_os[0]}
virt_osvariant=${virt_os[1]}
IFS=${OIFS}
else
virt_ostype="generic"
virt_osvariant="generic"
fi
arch="amd64"
source distros
2018-04-09 22:46:46 -06:00
2018-05-13 19:50:23 -06:00
sed -e "s/%GUEST_NAME%/${hostname}/" ${WORKING_DIR}/meta-data > ${WORKING_DIR}/output/meta-data
sed -e "s/%FQDN%/${hostname}.${domain}/" ${WORKING_DIR}/user-data > ${WORKING_DIR}/output/user-data
if [[ ${virt_netinfo_type} == "eni" ]]; then
2018-05-13 20:25:14 -06:00
cat ${WORKING_DIR}/network/legacy-eni >> ${WORKING_DIR}/output/meta-data
2018-05-13 19:50:23 -06:00
if [[ ${virt_netinfo_address} == "dhcp" ]]; then
2018-05-13 20:32:13 -06:00
echo -e "\n iface eth0 inet dhcp" >> ${WORKING_DIR}/output/meta-data
2018-05-13 19:50:23 -06:00
else
2018-05-13 20:32:13 -06:00
echo -e "\n iface eth0 inet static" >> ${WORKING_DIR}/output/meta-data
2018-05-13 20:25:14 -06:00
echo " address ${virt_netinfo_address}" >> ${WORKING_DIR}/output/meta-data
echo " netmask ${virt_netinfo_netmask}" >> ${WORKING_DIR}/output/meta-data
echo " gateway ${virt_netinfo_gateway}" >> ${WORKING_DIR}/output/meta-data
2018-05-13 19:50:23 -06:00
fi
elif [[ $virt_netinfo_type == "1" ]]; then
2018-05-13 21:06:03 -06:00
if [[ ${virt_netinfo_address} == "dhcp" ]]; then
sed -e "s/%DOMAIN%/${domain}/" ${WORKING_DIR}/network/network-config-dhcp-v1 > ${WORKING_DIR}/output/network-config
extra_iso_files=" -map $WORKING_DIR/output/network-config network-config"
2018-05-13 21:18:43 -06:00
else
sed -e "s/%DOMAIN%/${domain}/" -e "s/%IP_ADDRESS%/${virt_netinfo_address}/" -e "s/%NETMASK%/${virt_netinfo_netmask}/" \
-e "s/%GATEWAY%/${virt_netinfo_gateway}/" -e "s/%NAME_SERVER%/${nameservers}/" \
${WORKING_DIR}/network/network-config-v1 > ${WORKING_DIR}/output/network-config
extra_iso_files=" -map $WORKING_DIR/output/network-config network-config"
2018-05-13 21:10:10 -06:00
fi
2018-05-13 19:50:23 -06:00
elif [[ $virt_netinfo_type == "2" ]]; then
echo "Not implemented yet!"
fi
2018-04-09 22:46:46 -06:00
if [[ ! -f images/${IMG_NAME} ]]; then
2018-04-09 22:46:46 -06:00
echo "Downloading image ${IMG_NAME}..."
wget ${IMG_URL}/${IMG_NAME} -O images/${IMG_NAME}
2018-04-09 22:46:46 -06:00
chmod 644 ${IMG_NAME}
else
echo "Using existing image ${IMG_NAME}..."
fi
# check if pool exists, otherwise create it
if [[ "$(virsh pool-list|grep ${POOL} -c)" -ne "1" ]]; then
echo "Creating pool ${POOL}..."
virsh pool-define-as --name ${POOL} --type dir --target ${POOL_PATH}
virsh pool-autostart ${POOL}
virsh pool-build ${POOL}
virsh pool-start ${POOL}
fi
# write the two cloud-init files into an ISO
echo "Preparing ISO file required by cloud-init..."
#genisoimage -input-charset utf8 -output configuration.iso -volid cidata -joliet -rock user-data meta-data
2018-05-13 21:06:03 -06:00
xorriso -in_charset utf8 -outdev configuration.iso -volid cidata -joliet on -rockridge on -map output/user-data user-data -map output/meta-data meta-data ${extra_iso_files}
2018-04-09 22:46:46 -06:00
# keep a backup of the files for future reference
2018-05-13 20:12:46 -06:00
if [[ "${BACKUP_ISO_FILES}" == "yes" ]]; then
2018-05-13 20:22:10 -06:00
cp -f ${WORKING_DIR}/output/user-data ${WORKING_DIR}/backups/${hostname}.${domain}.user-data
cp -f ${WORKING_DIR}/output/meta-data ${WORKING_DIR}/backups/${hostname}.${domain}.meta-data
2018-05-13 21:06:03 -06:00
if [[ -f ${WORKING_DIR}/output/network-config ]]; then
cp -f ${WORKING_DIR}/output/network-config ${WORKING_DIR}/backups/${hostname}.${domain}.network-config
chmod 640 ${WORKING_DIR}/backups/${hostname}.${domain}.network-config
fi
2018-05-13 20:17:30 -06:00
chmod 640 ${WORKING_DIR}/backups/${hostname}.${domain}.user-data ${WORKING_DIR}/backups/${hostname}.${domain}.meta-data
2018-04-09 22:46:46 -06:00
fi
# copy ISO into libvirt's directory
cp configuration.iso ${POOL_PATH}/${hostname}.${domain}.configuration.iso
2018-04-09 22:46:46 -06:00
virsh pool-refresh ${POOL}
# copy image to libvirt's pool
if [[ ! -f ${POOL_PATH}/${IMG_NAME} ]]; then
cp images/${IMG_NAME} ${POOL_PATH}
2018-04-09 22:46:46 -06:00
virsh pool-refresh ${POOL}
fi
# clone cloud image
virsh vol-clone --pool ${POOL} ${IMG_NAME} ${hostname}.${domain}.root.img
virsh vol-resize --pool ${POOL} ${hostname}.${domain}.root.img ${storage}
2018-04-09 22:46:46 -06:00
# convert image format
if [[ "${CONVERT}" == "yes" ]]; then
echo "Converting image to format ${FORMAT}..."
qemu-img convert -O ${FORMAT} ${POOL_PATH}/${hostname}.${domain}.root.img ${POOL_PATH}/${hostname}.${domain}.root.img.${FORMAT}
rm ${POOL_PATH}/${hostname}.${domain}.root.img
mv ${POOL_PATH}/${hostname}.${domain}.root.img.${FORMAT} ${POOL_PATH}/${hostname}.${domain}.root.img
2018-04-09 22:46:46 -06:00
virsh pool-refresh ${POOL}
fi
echo "Creating guest ${hostname}.${domain}..."
2018-04-09 22:46:46 -06:00
virt-install \
--name ${hostname}.${domain} \
2018-04-09 22:46:46 -06:00
--cpu host \
2018-04-29 23:53:32 -06:00
--memory ${ram} \
--vcpus=${cpucount} \
2018-04-09 22:46:46 -06:00
--noautoconsole \
2018-04-20 09:55:46 -06:00
--graphics spice \
2018-04-09 22:46:46 -06:00
--memballoon virtio \
--network ${net_interface} \
2018-04-09 22:46:46 -06:00
--boot hd \
--disk vol=${POOL}/${hostname}.${domain}.root.img,format=${FORMAT},bus=virtio,cache=none \
--disk vol=${POOL}/${hostname}.${domain}.configuration.iso,bus=virtio,device=cdrom \
2018-04-09 22:46:46 -06:00
--console pty,target_type=serial \
--os-type=${virt_ostype} --os-variant=${virt_osvariant}
2018-04-09 22:46:46 -06:00
2018-04-29 23:09:08 -06:00
sleep 5
2018-04-09 22:46:46 -06:00
2018-04-29 23:09:08 -06:00
virsh change-media ${hostname}.${domain} vdb --eject --config
2018-04-09 22:46:46 -06:00
# cleanup
rm configuration.iso output/meta-data output/user-data