2018-04-09 22:46:46 -06:00
|
|
|
#!/bin/bash
|
2018-04-11 16:55:21 -06:00
|
|
|
# Heavily Modified Script By: Brielle Bruns
|
|
|
|
# Date: 04/11/2018
|
|
|
|
# URL: https://git.sosdg.org/brielle/virt-install-cloud
|
|
|
|
# Version: 1.0
|
2018-04-09 22:46:46 -06:00
|
|
|
#
|
2018-04-11 16:55:21 -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.1
|
|
|
|
#
|
|
|
|
# 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
|
|
|
|
|
2018-04-11 17:01:16 -06:00
|
|
|
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) *"
|
|
|
|
echo -e "\t-r ram size (in kilobytes) *"
|
|
|
|
echo -e "\t-c number of vcpus"
|
|
|
|
echo -e "\t-b bridge to attach to *"
|
|
|
|
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)"
|
2018-04-11 17:01:16 -06:00
|
|
|
}
|
|
|
|
|
2018-04-11 16:57:33 -06:00
|
|
|
while getopts "hcr:s:n:d:iob" opt; do
|
2018-04-11 16:55:21 -06:00
|
|
|
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");;
|
2018-04-11 17:01:16 -06:00
|
|
|
h) help="yes";;
|
2018-04-11 16:55:21 -06:00
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
|
|
|
|
2018-04-11 17:01:16 -06:00
|
|
|
if [[ -z $ram || -z $storage || -z $hostname || -z $domain || -z $bridge || $help = "yes" ]]; then
|
2018-04-11 16:55:21 -06:00
|
|
|
echo "Error: Incorrect command usage, must provide all * flags."
|
|
|
|
usage
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [[ -z $distro ]]; then
|
|
|
|
distro="stretch"
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [[ -z $cpucount ]]; then
|
|
|
|
cpucount="2"
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
# kvm defaults pool paths
|
|
|
|
DEF_POOL=images
|
|
|
|
DEF_POOL_PATH=/data/images
|
|
|
|
# vm prefs : specify vm preferences for your guest
|
|
|
|
BACKUP_ISO_FILES=no # yes or no
|
2018-04-11 16:55:21 -06:00
|
|
|
|
|
|
|
net_interface="bridge=${bridge},model=virtio"
|
2018-04-09 22:46:46 -06:00
|
|
|
# guest image format: qcow2 or raw
|
|
|
|
FORMAT=raw
|
|
|
|
# convert image format : yes or no
|
|
|
|
CONVERT=yes
|
|
|
|
# kvm pool
|
|
|
|
POOL=images
|
|
|
|
POOL_PATH=/data/images
|
|
|
|
|
2018-04-11 16:55:21 -06:00
|
|
|
sed -e "s/%GUEST_NAME%/${hostname}/" meta-data > output/meta-data
|
|
|
|
sed -e "s/%FQDN%/${hostname}.${domain}/" user-data > output/user-data
|
|
|
|
|
2018-04-09 22:46:46 -06:00
|
|
|
|
2018-04-10 09:41:31 -06:00
|
|
|
if [[ ! -f images/${IMG_NAME} ]]; then
|
2018-04-09 22:46:46 -06:00
|
|
|
echo "Downloading image ${IMG_NAME}..."
|
2018-04-10 09:41:31 -06:00
|
|
|
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
|
|
|
|
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
|
2018-04-11 16:55:21 -06:00
|
|
|
cp user-data ${hostname}.user-data
|
|
|
|
cp meta-data ${hostname}.meta-data
|
|
|
|
chmod 640 ${hostname}.user-data ${hostname}.meta-data
|
2018-04-09 22:46:46 -06:00
|
|
|
fi
|
|
|
|
# copy ISO into libvirt's directory
|
2018-04-11 16:55:21 -06:00
|
|
|
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
|
2018-04-10 09:41:31 -06:00
|
|
|
cp images/${IMG_NAME} ${POOL_PATH}
|
2018-04-09 22:46:46 -06:00
|
|
|
virsh pool-refresh ${POOL}
|
|
|
|
fi
|
|
|
|
|
|
|
|
# clone cloud image
|
2018-04-11 16:55:21 -06:00
|
|
|
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}..."
|
2018-04-11 16:55:21 -06:00
|
|
|
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
|
|
|
|
|
2018-04-11 16:55:21 -06:00
|
|
|
echo "Creating guest ${hostname}.${domain}..."
|
2018-04-09 22:46:46 -06:00
|
|
|
virt-install \
|
2018-04-11 16:55:21 -06:00
|
|
|
--name ${hostname}.${domain} \
|
2018-04-09 22:46:46 -06:00
|
|
|
--cpu host \
|
2018-04-11 16:55:21 -06:00
|
|
|
--ram ${ram} \
|
|
|
|
--vcpus=${cpucount} \
|
2018-04-09 22:46:46 -06:00
|
|
|
--noautoconsole \
|
|
|
|
--memballoon virtio \
|
2018-04-11 16:55:21 -06:00
|
|
|
--network ${net_interface} \
|
2018-04-09 22:46:46 -06:00
|
|
|
--boot hd \
|
2018-04-11 16:55:21 -06:00
|
|
|
--disk vol=${POOL}/${hostname}.${domain}.root.img,format=${FORMAT},bus=virtio,cache=none \
|
|
|
|
--disk vol=${POOL}/${hostname}.${domain}.configuration.iso,bus=virtio \
|
2018-04-09 22:46:46 -06:00
|
|
|
--console pty,target_type=serial \
|
2018-04-11 16:55:21 -06:00
|
|
|
--os-type=${virt_ostype} --os-variant=${virt_osvariant}
|
2018-04-09 22:46:46 -06:00
|
|
|
|
|
|
|
|
|
|
|
# cleanup
|
|
|
|
rm configuration.iso output/meta-data output/user-data
|