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

195 lines
6.3 KiB
Bash
Raw Normal View History

2018-04-09 22:46:46 -06:00
#!/bin/bash
#
# virt-install-cloud.sh : script to start an OpenStack cloud image on kvm
# version : 1.1
#
# Author : Claude Durocher
# License : GPLv3
#
#
# Heavily Modified By: Brielle Bruns
# Date: 04/09/2018
# URL: https://git.sosdg.org/brielle/virt-install-cloud
#
# 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
#
# Example CLI
# virt-install-cloud.sh hostname domain disk-size ramsize
# check if the script is run as root user
if [[ $USER != "root" ]]; then
echo "This script must be run as root!" && exit 1
fi
# image selection : trusty, precise, centos7, fedora26, ...
IMG="stretch"
# architecture : amd64 or i386
ARCH="amd64"
OS_TYPE="debian"
OS_VARIANT="debian9"
# 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
GUEST_NAME=${1}
DOMAIN=${2}
VROOTDISKSIZE=${3}
VCPUS=2
VMEM=${4}
NETWORK="bridge=vlan2-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%/${GUEST_NAME}/" meta-data > output/meta-data
sed -e "s/%FQDN%/${GUEST_NAME}.${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 ${IMG_NAME} ]]; then
echo "Downloading image ${IMG_NAME}..."
wget ${IMG_URL}/${IMG_NAME} -O ${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 ${GUEST_NAME}.user-data
cp meta-data ${GUEST_NAME}.meta-data
chmod 640 ${GUEST_NAME}.user-data ${GUEST_NAME}.meta-data
fi
# copy ISO into libvirt's directory
cp configuration.iso ${POOL_PATH}/${GUEST_NAME}.configuration.iso
virsh pool-refresh ${POOL}
# copy image to libvirt's pool
if [[ ! -f ${POOL_PATH}/${IMG_NAME} ]]; then
cp ${IMG_NAME} ${POOL_PATH}
virsh pool-refresh ${POOL}
fi
# clone cloud image
virsh vol-clone --pool ${POOL} ${IMG_NAME} ${GUEST_NAME}.root.img
virsh vol-resize --pool ${POOL} ${GUEST_NAME}.root.img ${VROOTDISKSIZE}
# convert image format
if [[ "${CONVERT}" == "yes" ]]; then
echo "Converting image to format ${FORMAT}..."
qemu-img convert -O ${FORMAT} ${POOL_PATH}/${GUEST_NAME}.root.img ${POOL_PATH}/${GUEST_NAME}.root.img.${FORMAT}
rm ${POOL_PATH}/${GUEST_NAME}.root.img
mv ${POOL_PATH}/${GUEST_NAME}.root.img.${FORMAT} ${POOL_PATH}/${GUEST_NAME}.root.img
virsh pool-refresh ${POOL}
fi
echo "Creating guest ${GUEST_NAME}..."
virt-install \
--name ${GUEST_NAME} \
--cpu host \
--ram ${VMEM} \
--vcpus=${VCPUS} \
--noautoconsole \
--memballoon virtio \
--network ${NETWORK} \
--boot hd \
--disk vol=${POOL}/${GUEST_NAME}.root.img,format=${FORMAT},bus=virtio,cache=none \
--disk vol=${POOL}/${GUEST_NAME}.configuration.iso,bus=virtio \
--console pty,target_type=serial \
--os-type=${OS_TYPE} --os-variant=${OS_VARIANT}
# display result
echo
echo "List of running VMs :"
echo
virsh list
# cleanup
rm configuration.iso output/meta-data output/user-data