Skip to content

Instantly share code, notes, and snippets.

@yuvalif
Last active October 18, 2018 07:00
Show Gist options
  • Select an option

  • Save yuvalif/b6253ef3e8d2f89c13982dbdbe35d6e1 to your computer and use it in GitHub Desktop.

Select an option

Save yuvalif/b6253ef3e8d2f89c13982dbdbe35d6e1 to your computer and use it in GitHub Desktop.
#!/bin/bash
set -e
if [ "$#" -ne 2 ]; then
echo "Usage: $0 <hostname> <cluster-name>"
exit 1
fi
set -x
NAME=$1
CLUSTER=$2
HOST=$1.$2
OSVERSION=fedora-28
OSVARIANT=fedora28
ARCHITECTURE=x86_64
DISK_SIZE=20G
CPUS=2
RAM=2048
POOL=vms
POOL_DIR=~/${POOL}
IMAGE_FILE=${NAME}-${OSVERSION}-${ARCHITECTURE}-sda.qcow2
set +x
# build the vm pool if does not exists
mkdir -p ${POOL_DIR}
if [ ! -f ${POOL_DIR}/${IMAGE_FILE} ]; then
# build an image file and store into a local "vms" pool
virt-builder ${OSVERSION} -o ${POOL_DIR}/${IMAGE_FILE} \
--no-network --format qcow2 --arch ${ARCHITECTURE} --size ${DISK_SIZE} --root-password=password:fedora \
--hostname ${HOST}
fi
# refresh the pool
virsh pool-refresh vms
# installl Fedora28 in that image
# "hostpassthrough is defined so that nested VMs will be supported
# "import" option to bypass actuall install
# "extra-args" to allocate static IP
virt-install -n ${NAME}-${OSVERSION}-${ARCHITECTURE} --vcpus ${CPUS} --cpu host-passthrough,cache.mode=passthrough \
--arch ${ARCHITECTURE} --memory ${RAM} --import --os-variant ${OSVARIANT} --controller scsi,model=virtio-scsi \
--disk vol=${POOL}/${IMAGE_FILE},device=disk,bus=scsi,discard=unmap \
--network network=default,model=virtio \
--graphics spice --channel unix,name=org.qemu.guest_agent.0 --noautoconsole --noreboot
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment