175 lines
5.0 KiB
Bash
Executable File
175 lines
5.0 KiB
Bash
Executable File
#!/bin/bash
|
|
# Heavily Modified Script By: Brielle Bruns
|
|
# Date: 04/11/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.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
|
|
|
|
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 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)"
|
|
}
|
|
|
|
while getopts "c:r:s:n:d:i:o:b: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;;
|
|
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 [[ -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
|
|
|
|
|
|
# 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
|
|
|
|
net_interface="bridge=${bridge},model=virtio"
|
|
# guest image format: qcow2 or raw
|
|
FORMAT=raw
|
|
# convert image format : yes or no
|
|
CONVERT=yes
|
|
# kvm pool
|
|
POOL=images
|
|
POOL_PATH=/data/images
|
|
|
|
sed -e "s/%GUEST_NAME%/${hostname}/" meta-data > output/meta-data
|
|
sed -e "s/%FQDN%/${hostname}.${domain}/" user-data > output/user-data
|
|
|
|
|
|
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 \
|
|
--ram ${ram} \
|
|
--vcpus=${cpucount} \
|
|
--noautoconsole \
|
|
--graphics none \
|
|
--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 \
|
|
--console pty,target_type=serial \
|
|
--os-type=${virt_ostype} --os-variant=${virt_osvariant}
|
|
|
|
|
|
# cleanup
|
|
rm configuration.iso output/meta-data output/user-data
|