From e6136b01ef4fbfe9e4dd1fc85a9dce51d626e4f8 Mon Sep 17 00:00:00 2001 From: "Dustin C. Hatch" Date: Sun, 10 Jul 2016 17:21:53 -0500 Subject: [PATCH] g2testvm: Quickly create Gentoo-based test VMs The `g2testvm.sh` script, inspired by `c7testvm.py`, creates a test virtual machine using `stage3img.py` and `virt-install`. --HG-- extra : amend_source : fd4a60820f255d74e8abf0df252ff5068af101f8 --- g2testvm.sh | 90 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100755 g2testvm.sh diff --git a/g2testvm.sh b/g2testvm.sh new file mode 100755 index 0000000..20105c4 --- /dev/null +++ b/g2testvm.sh @@ -0,0 +1,90 @@ +#!/bin/zsh + +name=${NAME:-${PREFIX:-Test-}$(uuidgen | cut -c -6)} +hostname=${name:l} + +: ${STAGEDIR:=~/.cache/stage3s} +: ${STAGEARCH:=amd64} +: ${POOL:=default} +: ${KVER:=4.4.6} +: ${KERNDIR:=/var/lib/libvirt/images} +: ${KERNEL:=${KERNDIR}/vmlinuz-${KVER}-gentoo.vm} +: ${INITRD:=${KERNDIR}/initramfs-genkernel-x86_64-${KVER}-gentoo.vm} +: ${KCMDLINE:=root=/dev/${hostname}/root ro dolvm rootfstype=ext4 nodetect fastboot quiet} +: ${BRIDGE:=br0} + + +stagetbz="${1}" +if [[ -z ${stagetbz} ]]; then + stagetbz=$(find "${STAGEDIR}" -name "stage3-${STAGEARCH}-*.tar.bz2" \ + -printf '%T@\t%p\n' | sort -nr | awk '{print $2;exit}') +fi +if [[ -z ${stagetbz} ]]; then + printf 'usage: %s tarball\n' "${0##*/}" >&2 + exit 2 +fi + + +set -e + +vmsetup=$(mktemp) +trap 'rm -f ${vmsetup}' EXIT +cat > ${vmsetup} < /etc/conf.d/net +ln -snf /usr/share/zoneinfo/America/Chicago /etc/localtime +echo 'America/Chicago' > /etc/timezone +sed -i s/localhost/${hostname}/ /etc/conf.d/hostname +ssh-keygen -A +ln -snf /proc/self/mounts /etc/mtab +ln -snf /run/resolv.conf /etc/resolv.conf +ln -snf /run/ntp.conf /etc/ntp.conf +EOF + +pooldir=$(virsh pool-dumpxml ${POOL} | xml sel -t -v '//pool/target/path') + +ssh_key=~/.ssh/id_ed25519.pub +if [[ ! -f ${ssh_key} ]]; then + ssh_keys=( ~/.ssh/id_*.pub ) + ssh_key=${ssh_keys[1]} + unset ssh_keys +fi + +printf 'Creating %s from %s\n' "${pooldir}/${name}.img" "${stagetbz}" +stage3img "${stagetbz}" "${pooldir}/${name}.img" \ + -i ${ssh_key} \ + -L ${hostname} \ + -V '/=2G;;;ro,noatime,discard' \ + -V '/var/log=512M;;;noatime,discard' \ + -V '/home=12M;;;noatime,discard' \ + -V '/var=;;;noatime' \ + -S ${vmsetup} + +printf 'Converting %s to %s\n' "${name}".img "${name}".qcow2 +qemu-img convert -p -O qcow2 "${pooldir}/${name}".{img,qcow2} +rm -f ${pooldir}/${name}.img || : +virsh pool-refresh ${POOL} + +virt-install \ + --name ${name} \ + --memory 1024 \ + --vcpus 2 \ + --cpu host \ + --import \ + --os-variant fedora23 \ + --boot kernel=${KERNEL},initrd=${INITRD},kernel_args=${KCMDLINE} \ + --disk vol=${POOL}/${name}.qcow2 \ + --network bridge=${BRIDGE},model=virtio \ + --sound none \ + --redirdev none \ + --noautoconsole + +ansible -i /dev/null localhost -mwait_for -a "host=${hostname} port=22" + +exec ssh \ + -o StrictHostKeyChecking=no \ + -o UserKnownHostsFile=/dev/null \ + root@${hostname}