1#!/bin/bash 2 3#set -x 4set -e 5 6function usage() { 7 cat <<DELIM__ 8usage: $(basename $0) [options] 9 10Options: 11 -b, --bridge BRNAME Which linux bridge to attach to 12 -c, --cpu NUM Number of CPUs to reserve to the instance (default 4) 13 -g, --github_token X HTTP Github oauth token (for buildbots) 14 -k, --kickstart KS Path to kickstart file to use (required) 15 -m, --mirror URL URL at which to reach netinstallable packages 16 -M, --mem NUM Number of MB to reserve to the instance (default 4094) 17 -n, --name NAME Name of the instance (required) 18 -p, --password PASS Password to set in the VM 19 -s, --size NUM Size in GB to reserve for the virtual HDD (default 40GB) 20DELIM__ 21} 22 23TEMP=$(getopt -o b:c:k:m:M:n:p:s: --long bridge:,cpu:,kickstart:,mirror:,mem:,name:,password:size: -- "$@") 24if [[ $? -ne 0 ]]; then 25 usage 26 exit 1 27fi 28 29eval set -- "$TEMP" 30 31while true; do 32 case "$1" in 33 -b|--bridge) BRIDGE="$2"; shift 2 ;; 34 -c|--cpu) CPU="$2"; shift 2 ;; 35 -k|--kickstart) KICKSTART="$2"; shift 2 ;; 36 -n|--name) NAME="$2"; shift 2 ;; 37 -m|--mirror) MIRROR="$2"; shift 2 ;; 38 -M|--mem) MEM="$2"; shift 2 ;; 39 -p|--password) PASSWORD="$2"; shift 2 ;; 40 -s|--size) SIZE="$2"; shift 2 ;; 41 --) shift; break ;; 42 *) usage; exit 1 43 ;; 44 esac 45done 46[[ ! -f "$KICKSTART" ]] && { usage; exit 1; } 47[[ -z "$NAME" ]] && { usage; exit 1; } 48 49PASSWORD=${PASSWORD:-"iovisor"} 50BRIDGE=${BRIDGE:-virbr0} 51MIRROR=${MIRROR:-http://mirror.pnl.gov/fedora/linux/releases/22} 52MEM=${MEM:-4094} 53CPU=${CPU:-4} 54SIZE=${SIZE:-40} 55 56if [[ "$(id -u)" != "0" ]]; then 57 sudo="sudo" 58fi 59 60if ! which virt-install &> /dev/null; then 61 echo "Error: virt-install is not installed" 62 exit 1 63fi 64 65libvirt_dir=/var/lib/libvirt/images 66img_name=$NAME 67tmpdir=$(mktemp -d /tmp/virt-install_XXXXX) 68tmp_ks_file=$tmpdir/$img_name.ks 69 70function cleanup() { 71 set +e 72 [[ -d "$tmpdir" ]] && rm -fr "$tmpdir" 73 local destroy_kvm=n 74 [[ -f "/etc/libvirt/qemu/$img_name.xml" ]] && read -p "Destroy libvirt VM (y/n)? " destroy_kvm 75 if [[ "$destroy_kvm" != n* ]]; then 76 virsh destroy $img_name 77 virsh undefine $img_name 78 virsh vol-delete $img_name.img --pool default 79 $sudo rm -f $libvirt_dir/$img_name.img 80 fi 81} 82trap cleanup EXIT 83 84ruby <<DELIM__ 85require 'erb' 86@password="$PASSWORD" 87@name="$NAME" 88@domain="example.com" 89@github_access_token="$GITHUB_ACCESS_TOKEN" 90@mirror="$MIRROR" 91File.open('$tmp_ks_file', 'w') do |f| 92 f.puts ERB.new(File.open('$KICKSTART', 'rb').read, nil, '-').result() 93end 94DELIM__ 95 96tree=$MIRROR/Server/x86_64/os/ 97virt-install --connect=qemu:///system \ 98 --network=bridge:$BRIDGE \ 99 --initrd-inject=$tmp_ks_file \ 100 --controller type=scsi,model=virtio-scsi \ 101 --extra-args="ks=file:/$(basename $tmp_ks_file) console=tty0 console=ttyS0,115200" \ 102 --name=$img_name \ 103 --disk $libvirt_dir/$img_name.img,cache=none,format=qcow2,size=$SIZE,bus=scsi \ 104 --ram $MEM \ 105 --vcpus=$CPU \ 106 --check-cpu \ 107 --accelerate \ 108 --hvm \ 109 --location=$tree \ 110 --nographics 111 112echo "SUCCESS" 113exit 0 114