-
-
Save vkmc/ea6b142cdae60f2b0839576b66ea5e5b to your computer and use it in GitHub Desktop.
script to install a VM for use as devstack host
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
## Derives from: http://giovannitorres.me/create-a-linux-lab-on-kvm-using-cloud-images.html | |
## Assumes you have a xenial cloud image in $DIR below, via e.g. | |
# wget https://cloud-images.ubuntu.com/xenial/current/xenial-server-cloudimg-amd64-disk1.img | |
# Take one argument from the commandline: VM name | |
if ! [ $# -eq 1 ]; then | |
echo "Usage: $0 <node-name>" | |
exit 1 | |
fi | |
# Check if domain already exists | |
virsh dominfo $1 > /dev/null 2>&1 | |
if [ "$?" -eq 0 ]; then | |
echo -n "[WARNING] $1 already exists. " | |
read -p "Do you want to overwrite $1 (y/[N])? " -r | |
if [[ $REPLY =~ ^[Yy]$ ]]; then | |
echo "" | |
virsh destroy $1 > /dev/null | |
virsh undefine $1 > /dev/null | |
else | |
echo -e "\nNot overwriting $1. Exiting..." | |
exit 1 | |
fi | |
fi | |
# Directory to store images | |
DIR=~/virt/images | |
# Location of cloud image | |
IMAGE=$DIR/xenial-server-cloudimg-amd64-disk1.img | |
# Amount of RAM in MB | |
MEM=4096 | |
# Number of virtual CPUs | |
CPUS=1 | |
# Cloud init files | |
USER_DATA=user-data | |
META_DATA=meta-data | |
CI_ISO=$1-cidata.iso | |
DISK=$1.qcow2 | |
# Bridge for VMs (default on Fedora is virbr0) | |
BRIDGE=virbr0 | |
# Start clean | |
rm -rf $DIR/$1 | |
mkdir -p $DIR/$1 | |
pushd $DIR/$1 > /dev/null | |
# Create log file | |
touch $1.log | |
echo "$(date -R) Destroying the $1 domain (if it exists)..." | |
# Remove domain with the same name | |
virsh destroy $1 >> $1.log 2>&1 | |
virsh undefine $1 >> $1.log 2>&1 | |
# cloud-init config: set hostname, remove cloud-init package, | |
# and add ssh-key | |
cat > $USER_DATA << _EOF_ | |
#cloud-config | |
# Hostname management | |
preserve_hostname: False | |
hostname: $1 | |
fqdn: $1.example.local | |
# Remove cloud-init when finished with it | |
runcmd: | |
- [ yum, -y, remove, cloud-init ] | |
# Configure where output will go | |
output: | |
all: ">> /var/log/cloud-init.log" | |
# configure interaction with ssh server | |
ssh_svcname: ssh | |
ssh_deletekeys: True | |
ssh_genkeytypes: ['rsa', 'ecdsa'] | |
# Change this to your own public ssh key!! | |
# Install my public ssh key to the first user-defined user configured | |
# in cloud.cfg in the template (which is centos for CentOS cloud images) | |
ssh_authorized_keys: | |
- ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDKdJTIRBb5+u3z11XLCiGqKidQ3gnwAFCReXEv4z5Hx+ghQeE2uEZ4WEIglcEmZ24T3DCmOtBGkej2PXJMGLiauXCiHBhPvAo1+bd1WKoAhXRa7XhTxp4sGc+KGd4E6JaAe3gn4IgNCcOhmvZsj0eQGLh/CXg4rONsS2REAof2W+JBg4QQwgHugG4MRV2MTuka64dM6uVRWEyUq1hDkmpcNT36mCW/V+auNP0ZUCXBktaOeUQDm0vfs4VeJp/YEBEwTitzdQOzOiBKp6Tvh85YOLOZXwZRpbb7aK8/gZ1lHY56g6hK6p/VyX0dkPvaAg2teAV/ZC2qlBt6gk06jvNb tbarron@tbarron | |
_EOF_ | |
echo "instance-id: $1; local-hostname: $1" > $META_DATA | |
echo "$(date -R) Copying template image..." | |
qemu-img create -f qcow2 -o preallocation=metadata $DISK 41G | |
virt-resize --resize /dev/sda1=40G $IMAGE $DISK | |
##cp $IMAGE $DISK | |
# Create CD-ROM ISO with cloud-init config | |
echo "$(date -R) Generating ISO for cloud-init..." | |
genisoimage -output $CI_ISO -volid cidata -joliet -r $USER_DATA $META_DATA &>> $1.log | |
echo "$(date -R) Installing the domain and adjusting the configuration..." | |
echo "[INFO] Installing with the following parameters:" | |
echo "virt-install --import --name $1 --ram $MEM --vcpus $CPUS --disk | |
$DISK,format=qcow2,bus=virtio --disk $CI_ISO,device=cdrom --network | |
bridge=virbr0,model=virtio --os-type=linux --os-variant=ubuntu15.04 --noautoconsole" | |
virt-install --import --name $1 --ram $MEM --vcpus $CPUS --disk \ | |
$DISK,format=qcow2,bus=virtio --disk $CI_ISO,device=cdrom --network \ | |
bridge=virbr0,model=virtio --os-type=linux --os-variant=ubuntu15.04 \ | |
--nographics --noautoconsole | |
MAC=$(virsh dumpxml $1 | awk -F\' '/mac address/ {print $2}') | |
while true | |
do | |
IP=$(grep -B1 $MAC /var/lib/libvirt/dnsmasq/$BRIDGE.status | head \ | |
-n 1 | awk '{print $2}' | sed -e s/\"//g -e s/,//) | |
if [ "$IP" = "" ] | |
then | |
sleep 1 | |
else | |
break | |
fi | |
done | |
# Eject cdrom | |
echo "$(date -R) Cleaning up cloud-init..." | |
virsh change-media $1 hda --eject --config >> $1.log | |
# Remove the unnecessary cloud init files | |
rm $USER_DATA $CI_ISO | |
echo "$(date -R) DONE. SSH to $1 using $IP, with username 'centos'." | |
popd > /dev/null | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment