210 lines
6.2 KiB
Bash
Executable File
210 lines
6.2 KiB
Bash
Executable File
#!/bin/bash
|
|
# Heavily Modified Script By: Brielle Bruns
|
|
# Date: 05/13/2018
|
|
# URL: https://git.sosdg.org/brielle/virt-install-cloud
|
|
# Version: 1.0
|
|
#
|
|
# Originally based on:
|
|
# virt-install-cloud.sh : script to start an OpenStack cloud image on kvm
|
|
# version : 1.2
|
|
#
|
|
# 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"
|
|
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) *"
|
|
echo -e "\t-r ram size (in megabytes) *"
|
|
echo -e "\t-c number of vcpus"
|
|
echo -e "\t-b bridge to attach to *"
|
|
echo -e "\t-x network info"
|
|
echo -e "(type:ip:netmask:gateway - type=eni/1, ip=dhcp or address, default eni dhcp)"
|
|
echo -e "\t-i distribution to use (see script for supported options) - default is stretch"
|
|
echo -e "\t-o OS type and variant (default: generic:generic - also set by distro option when available)"
|
|
}
|
|
|
|
while getopts "c:r:s:n:d:i:o:b:x:h" opt; do
|
|
case $opt in
|
|
c) cpucount=$OPTARG;;
|
|
r) ram=$OPTARG;;
|
|
s) storage=$OPTARG;;
|
|
n) hostname=$OPTARG;;
|
|
d) domain=$OPTARG;;
|
|
i) distro=$OPTARG;;
|
|
o) ostype=$OPTARG;;
|
|
b) bridge=$OPTARG;;
|
|
x) netinfo=$OPTARG;;
|
|
h) help="yes";;
|
|
esac
|
|
done
|
|
|
|
|
|
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
|
|
|
|
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
|
|
|
|
if [[ -z $netinfo ]]; then
|
|
netinfo="eni:dhcp"
|
|
fi
|
|
|
|
if [[ ! -z $netinfo ]]; then
|
|
OIFS=${IFS}
|
|
IFS=:
|
|
read -ra $netinfo_config <<< "$netinfo"
|
|
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}
|
|
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
|
|
|
|
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
|
|
cat ${WORK_DIR}/network/legacy-eni >> ${WORK_DIR}/output/meta-data
|
|
if [[ ${virt_netinfo_address} == "dhcp" ]]; then
|
|
echo " iface eth0 inet dhcp" >> ${WORK_DIR}/output/meta-data
|
|
else
|
|
echo " iface eth0 inet static" >> ${WORK_DIR}/output/meta-data
|
|
echo " address ${virt_netinfo_address}" >> ${WORK_DIR}/output/meta-data
|
|
echo " netmask ${virt_netinfo_netmask}" >> ${WORK_DIR}/output/meta-data
|
|
echo " gateway ${virt_netinfo_gateway}" >> ${WORK_DIR}/output/meta-data
|
|
fi
|
|
elif [[ $virt_netinfo_type == "1" ]]; then
|
|
echo "Not implemented yet!"
|
|
elif [[ $virt_netinfo_type == "2" ]]; then
|
|
echo "Not implemented yet!"
|
|
fi
|
|
|
|
if [[ ! -f images/${IMG_NAME} ]]; then
|
|
echo "Downloading image ${IMG_NAME}..."
|
|
wget ${IMG_URL}/${IMG_NAME} -O images/${IMG_NAME}
|
|
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
|
|
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
|
|
|
|
# keep a backup of the files for future reference
|
|
if [[ "${BACKUP_ISO_FILE}" == "yes" ]]; then
|
|
cp user-data ${hostname}.user-data
|
|
cp meta-data ${hostname}.meta-data
|
|
chmod 640 ${hostname}.user-data ${hostname}.meta-data
|
|
fi
|
|
# copy ISO into libvirt's directory
|
|
cp configuration.iso ${POOL_PATH}/${hostname}.${domain}.configuration.iso
|
|
virsh pool-refresh ${POOL}
|
|
|
|
# copy image to libvirt's pool
|
|
if [[ ! -f ${POOL_PATH}/${IMG_NAME} ]]; then
|
|
cp images/${IMG_NAME} ${POOL_PATH}
|
|
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}
|
|
|
|
# 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
|
|
virsh pool-refresh ${POOL}
|
|
fi
|
|
|
|
echo "Creating guest ${hostname}.${domain}..."
|
|
virt-install \
|
|
--name ${hostname}.${domain} \
|
|
--cpu host \
|
|
--memory ${ram} \
|
|
--vcpus=${cpucount} \
|
|
--noautoconsole \
|
|
--graphics spice \
|
|
--memballoon virtio \
|
|
--network ${net_interface} \
|
|
--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 \
|
|
--console pty,target_type=serial \
|
|
--os-type=${virt_ostype} --os-variant=${virt_osvariant}
|
|
|
|
sleep 5
|
|
|
|
virsh change-media ${hostname}.${domain} vdb --eject --config
|
|
|
|
# cleanup
|
|
rm configuration.iso output/meta-data output/user-data
|