Update to include getopts use, add distros config

master
Brie Bruns 2018-04-11 16:55:21 -06:00
parent dfd0d0a492
commit 0e6a6e540c
2 changed files with 146 additions and 104 deletions

64
distros Normal file
View File

@ -0,0 +1,64 @@
case $distro in
trusty) IMG_USER="ubuntu"
IMG_URL="http://cloud-images.ubuntu.com/releases/14.04/release"
IMG_NAME="ubuntu-14.04-server-cloudimg-${ARCH}-disk1.img"
;;
xenial) IMG_USER="ubuntu"
IMG_URL="http://cloud-images.ubuntu.com/releases/16.04/release"
IMG_NAME="ubuntu-16.04-server-cloudimg-${ARCH}-disk1.img"
;;
artful) IMG_USER="ubuntu"
IMG_URL="http://cloud-images.ubuntu.com/releases/17.10/release"
IMG_NAME="ubuntu-17.10-server-cloudimg-${ARCH}-disk1.img"
;;
bionic) IMG_USER="ubuntu"
IMG_URL="http://cloud-images.ubuntu.com/releases/18.04/release"
IMG_NAME="ubuntu-18.04-server-cloudimg-${ARCH}.img"
;;
centos6) IMG_USER="centos"
IMG_URL="https://cloud.centos.org/centos/6/images"
if [[ $arch = "amd64" ]]; then
IMG_NAME="CentOS-6-x86_64-GenericCloud.qcow2"
else
echo "Cloud image not available!"; exit 1
fi
;;
centos7) IMG_USER="centos"
IMG_URL="https://cloud.centos.org/centos/7/images"
if [[ $arch = "amd64" ]]; then
IMG_NAME="CentOS-7-x86_64-GenericCloud.qcow2"
else
echo "Cloud image not available!"; exit 1
fi
;;
fedora27) IMG_USER="fedora"
if [[ $arch = "amd64" ]]; then
IMG_URL="https://download.fedoraproject.org/pub/fedora/linux/releases/27/CloudImages/x86_64/images/"
IMG_NAME="Fedora-Cloud-Base-27-1.6.x86_64.qcow2"
else
echo "Cloud image not available!"; exit 1
fi
;;
jessie) IMG_USER="debian"
if [[ $arch = "amd64" ]]; then
IMG_URL="https://cdimage.debian.org/cdimage/openstack/current-8"
IMG_NAME="debian-8-openstack-amd64.qcow2"
virt_ostype="debian"
virt_osvariant="debian8"
else
echo "Cloud image not available!"; exit 1
fi
;;
stretch) IMG_USER="debian"
if [[ $arch = "amd64" ]]; then
IMG_URL="https://cdimage.debian.org/cdimage/openstack/current-9"
IMG_NAME="debian-9-openstack-amd64.qcow2"
virt_ostype="debian"
virt_osvariant="debian9"
else
echo "Cloud image not available!"; exit 1
fi
;;
*) echo "Cloud image not available!"; exit 1
;;
esac

View File

@ -1,16 +1,16 @@
#!/bin/bash #!/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 # virt-install-cloud.sh : script to start an OpenStack cloud image on kvm
# version : 1.1 # version : 1.1
# #
# Author : Claude Durocher # Author : Claude Durocher
# License : GPLv3 # License : GPLv3
# #
#
# Heavily Modified By: Brielle Bruns
# Date: 04/10/2018
# URL: https://git.sosdg.org/brielle/virt-install-cloud
#
# ref. http://mojodna.net/2014/05/14/kvm-libvirt-and-ubuntu-14-04.html # ref. http://mojodna.net/2014/05/14/kvm-libvirt-and-ubuntu-14-04.html
# #
# requires the following packages on Ubuntu host: # requires the following packages on Ubuntu host:
@ -19,33 +19,76 @@
# wget qemu-kvm libvirt virt-install bridge-utils genisoimage|xorriso # wget qemu-kvm libvirt virt-install bridge-utils genisoimage|xorriso
# #
# Example CLI
# virt-install-cloud.sh hostname domain disk-size ramsize
# check if the script is run as root user # check if the script is run as root user
if [[ $USER != "root" ]]; then if [[ $USER != "root" ]]; then
echo "This script must be run as root!" && exit 1 echo "This script must be run as root!" && exit 1
fi fi
# image selection : trusty, precise, centos7, fedora26, ... while getopts "cr:s:n:d:iob" opt; do
IMG="stretch" case $opt in
# architecture : amd64 or i386 c) cpucount=("$OPTARG");;
ARCH="amd64" r) ram=("$OPTARG");;
s) storage=("$OPTARG");;
n) hostname=("$OPTARG");;
d) domain=("$OPTARG");;
i) distro=("$OPTARG");;
o) ostype=("$OPTARG");;
b) bridge=("$OPTARG");;
h) usage;;
esac
done
usage() {
echo "${0} Help"
echo "-h show this help"
echo "-n hostname *"
echo "-d domainame *"
echo "-s disk size (num + count size - M=Megabytes G=Gigabytes) *"
echo "-r ram size (in kilobytes) *"
echo "-c number of vcpus"
echo "-b bridge to attach to *"
echo "-i distribution to use (see script for supported options) - default is stretch"
echo "-o OS type and variant (default: generic:generic - also set by distro option when available)"
}
if [[ -z $ram || -z $storage || -z $hostname || -z $domain || -z $bridge ]]; 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
OS_TYPE="debian"
OS_VARIANT="debian9"
# kvm defaults pool paths # kvm defaults pool paths
DEF_POOL=images DEF_POOL=images
DEF_POOL_PATH=/data/images DEF_POOL_PATH=/data/images
# vm prefs : specify vm preferences for your guest # vm prefs : specify vm preferences for your guest
BACKUP_ISO_FILES=no # yes or no BACKUP_ISO_FILES=no # yes or no
GUEST_NAME=${1}
DOMAIN=${2} net_interface="bridge=${bridge},model=virtio"
VROOTDISKSIZE=${3}
VCPUS=2
VMEM=${4}
NETWORK="bridge=vlan2-bridge,model=virtio"
# guest image format: qcow2 or raw # guest image format: qcow2 or raw
FORMAT=raw FORMAT=raw
# convert image format : yes or no # convert image format : yes or no
@ -54,69 +97,9 @@ CONVERT=yes
POOL=images POOL=images
POOL_PATH=/data/images POOL_PATH=/data/images
sed -e "s/%GUEST_NAME%/${GUEST_NAME}/" meta-data > output/meta-data sed -e "s/%GUEST_NAME%/${hostname}/" meta-data > output/meta-data
sed -e "s/%FQDN%/${GUEST_NAME}.${DOMAIN}/" user-data > output/user-data sed -e "s/%FQDN%/${hostname}.${domain}/" user-data > output/user-data
case $IMG in
trusty) IMG_USER="ubuntu"
IMG_URL="http://cloud-images.ubuntu.com/releases/14.04/release"
IMG_NAME="ubuntu-14.04-server-cloudimg-${ARCH}-disk1.img"
;;
xenial) IMG_USER="ubuntu"
IMG_URL="http://cloud-images.ubuntu.com/releases/16.04/release"
IMG_NAME="ubuntu-16.04-server-cloudimg-${ARCH}-disk1.img"
;;
artful) IMG_USER="ubuntu"
IMG_URL="http://cloud-images.ubuntu.com/releases/17.10/release"
IMG_NAME="ubuntu-17.10-server-cloudimg-${ARCH}-disk1.img"
;;
bionic) IMG_USER="ubuntu"
IMG_URL="http://cloud-images.ubuntu.com/releases/18.04/release"
IMG_NAME="ubuntu-18.04-server-cloudimg-${ARCH}.img"
;;
centos6) IMG_USER="centos"
IMG_URL="https://cloud.centos.org/centos/6/images"
if [[ $ARCH = "amd64" ]]; then
IMG_NAME="CentOS-6-x86_64-GenericCloud.qcow2"
else
echo "Cloud image not available!"; exit 1
fi
;;
centos7) IMG_USER="centos"
IMG_URL="https://cloud.centos.org/centos/7/images"
if [[ $ARCH = "amd64" ]]; then
IMG_NAME="CentOS-7-x86_64-GenericCloud.qcow2"
else
echo "Cloud image not available!"; exit 1
fi
;;
fedora27) IMG_USER="fedora"
if [[ $ARCH = "amd64" ]]; then
IMG_URL="https://download.fedoraproject.org/pub/fedora/linux/releases/27/CloudImages/x86_64/images/"
IMG_NAME="Fedora-Cloud-Base-27-1.6.x86_64.qcow2"
else
echo "Cloud image not available!"; exit 1
fi
;;
jessie) IMG_USER="debian"
if [[ $ARCH = "amd64" ]]; then
IMG_URL="https://cdimage.debian.org/cdimage/openstack/current-8"
IMG_NAME="debian-8-openstack-amd64.qcow2"
else
echo "Cloud image not available!"; exit 1
fi
;;
stretch) IMG_USER="debian"
if [[ $ARCH = "amd64" ]]; then
IMG_URL="https://cdimage.debian.org/cdimage/openstack/current-9"
IMG_NAME="debian-9-openstack-amd64.qcow2"
else
echo "Cloud image not available!"; exit 1
fi
;;
*) echo "Cloud image not available!"; exit 1
;;
esac
if [[ ! -f images/${IMG_NAME} ]]; then if [[ ! -f images/${IMG_NAME} ]]; then
echo "Downloading image ${IMG_NAME}..." echo "Downloading image ${IMG_NAME}..."
@ -142,12 +125,12 @@ xorriso -in_charset utf8 -outdev configuration.iso -volid cidata -joliet on -roc
# keep a backup of the files for future reference # keep a backup of the files for future reference
if [[ "${BACKUP_ISO_FILE}" == "yes" ]]; then if [[ "${BACKUP_ISO_FILE}" == "yes" ]]; then
cp user-data ${GUEST_NAME}.user-data cp user-data ${hostname}.user-data
cp meta-data ${GUEST_NAME}.meta-data cp meta-data ${hostname}.meta-data
chmod 640 ${GUEST_NAME}.user-data ${GUEST_NAME}.meta-data chmod 640 ${hostname}.user-data ${hostname}.meta-data
fi fi
# copy ISO into libvirt's directory # copy ISO into libvirt's directory
cp configuration.iso ${POOL_PATH}/${GUEST_NAME}.configuration.iso cp configuration.iso ${POOL_PATH}/${hostname}.${domain}.configuration.iso
virsh pool-refresh ${POOL} virsh pool-refresh ${POOL}
# copy image to libvirt's pool # copy image to libvirt's pool
@ -157,38 +140,33 @@ if [[ ! -f ${POOL_PATH}/${IMG_NAME} ]]; then
fi fi
# clone cloud image # clone cloud image
virsh vol-clone --pool ${POOL} ${IMG_NAME} ${GUEST_NAME}.root.img virsh vol-clone --pool ${POOL} ${IMG_NAME} ${hostname}.${domain}.root.img
virsh vol-resize --pool ${POOL} ${GUEST_NAME}.root.img ${VROOTDISKSIZE} virsh vol-resize --pool ${POOL} ${hostname}.${domain}.root.img ${storage}
# convert image format # convert image format
if [[ "${CONVERT}" == "yes" ]]; then if [[ "${CONVERT}" == "yes" ]]; then
echo "Converting image to format ${FORMAT}..." echo "Converting image to format ${FORMAT}..."
qemu-img convert -O ${FORMAT} ${POOL_PATH}/${GUEST_NAME}.root.img ${POOL_PATH}/${GUEST_NAME}.root.img.${FORMAT} qemu-img convert -O ${FORMAT} ${POOL_PATH}/${hostname}.${domain}.root.img ${POOL_PATH}/${hostname}.${domain}.root.img.${FORMAT}
rm ${POOL_PATH}/${GUEST_NAME}.root.img rm ${POOL_PATH}/${hostname}.${domain}.root.img
mv ${POOL_PATH}/${GUEST_NAME}.root.img.${FORMAT} ${POOL_PATH}/${GUEST_NAME}.root.img mv ${POOL_PATH}/${hostname}.${domain}.root.img.${FORMAT} ${POOL_PATH}/${hostname}.${domain}.root.img
virsh pool-refresh ${POOL} virsh pool-refresh ${POOL}
fi fi
echo "Creating guest ${GUEST_NAME}..." echo "Creating guest ${hostname}.${domain}..."
virt-install \ virt-install \
--name ${GUEST_NAME} \ --name ${hostname}.${domain} \
--cpu host \ --cpu host \
--ram ${VMEM} \ --ram ${ram} \
--vcpus=${VCPUS} \ --vcpus=${cpucount} \
--noautoconsole \ --noautoconsole \
--memballoon virtio \ --memballoon virtio \
--network ${NETWORK} \ --network ${net_interface} \
--boot hd \ --boot hd \
--disk vol=${POOL}/${GUEST_NAME}.root.img,format=${FORMAT},bus=virtio,cache=none \ --disk vol=${POOL}/${hostname}.${domain}.root.img,format=${FORMAT},bus=virtio,cache=none \
--disk vol=${POOL}/${GUEST_NAME}.configuration.iso,bus=virtio \ --disk vol=${POOL}/${hostname}.${domain}.configuration.iso,bus=virtio \
--console pty,target_type=serial \ --console pty,target_type=serial \
--os-type=${OS_TYPE} --os-variant=${OS_VARIANT} --os-type=${virt_ostype} --os-variant=${virt_osvariant}
# display result
echo
echo "List of running VMs :"
echo
virsh list
# cleanup # cleanup
rm configuration.iso output/meta-data output/user-data rm configuration.iso output/meta-data output/user-data