#!/bin/sh set -e # defaults RAM=512 DISK=20 STORAGE_POOL=default QEMU_URI=qemu:///system SUITE=wheezy CONSOLE='console=tty0 console=ttyS0,115200n8' DEBUG=0 WIPE=0 RESTART=0 while [ -n "$1" ]; do case "$1" in (--debug) DEBUG=1 ;; (-S|--suite) shift if [ -z "$1" ]; then echo >&2 'E: --suite/-S needs suite argument' exit 1 fi SUITE="$1" ;; (-n|--name) shift if [ -z "$1" ]; then echo >&2 'E: --name/-n needs hostname argument' exit 1 fi HOSTNAME="$1" ;; (-i|--ipaddress) shift if [ -z "$1" ]; then echo >&2 'E: --ipaddress/-i needs IP address/netmask' exit 1 fi case "$1" in (*/*) :;; (*) echo >&2 'E: missing netmask' exit 1 ;; esac IPADDRESS="$1" ;; (-d|--dns) shift if [ -z "$1" ]; then echo >&2 'E: --dns/-d needs IP address' exit 1 fi NAMESERVER="$1" ;; (-g|--gateway) shift if [ -z "$1" ]; then echo >&2 'E: --gateway/-g needs IP address' exit 1 fi GATEWAY="$1" ;; (-r|--ram) shift if [ -z "$1" ]; then echo >&2 'E: --ram/-r needs number (megabytes)' exit 1 fi RAM="$1" ;; (-s|--size) shift if [ -z "$1" ]; then echo >&2 'E: --size/-s needs size argument' exit 1 fi DISK="$1" ;; (-a|--arch) shift if [ -z "$1" ]; then echo >&2 'E: --arch/-a needs arch argument' exit 1 fi ARCH="$1" ;; (-m|--mac) shift if [ -z "$1" ]; then echo >&2 'E: --mac/-m needs mac address argument' exit 1 fi MAC="$1" ;; (--debug) DEBUG=1 set -vx ;; (--wipe) WIPE=1 ;; (--restart) RESTART=1 ;; esac shift done if [ -z "${HOSTNAME:-}" ]; then echo >&2 'E: hostname is required' exit 2 fi BASEDIR=$(cd ${0%/*}; pwd) tmpdir=$(mktemp -d) tar -C $BASEDIR/preseed/$SUITE -cf $tmpdir/commands.tar commands extra_kargs="auto $CONSOLE \ hostname=${HOSTNAME%%.*} \ domain=${HOSTNAME#*.}" extra_opts= if [ -n "$DEBUG" ]; then extra_opts='--debug' fi if [ -n "$IPADDRESS" ]; then if [ -z "$GATEWAY" ] && [ -x $(command -v ipcalc) ]; then # default to the first IP in the network GATEWAY=$(ipcalc $IPADDRESS | grep HostMin | awk '{print $2}') fi NETMASK="${IPADDRESS#*/}" IPADDRESS="${IPADDRESS%/*}" : ${NAMESERVER:=$GATEWAY} extra_kargs="$extra_kargs netcfg/disable_dhcp=true \ netcfg/get_ipaddress=$IPADDRESS \ netcfg/get_netmask=$NETMASK \ netcfg/get_gateway=$GATEWAY \ netcfg/get_nameservers=$NAMESERVER" BRIDGE=wan else extra_kargs="$extra_kargs netcfg/disable_dhcp=false" BRIDGE=virt-br fi if [ -z "$ARCH" ]; then ARCH=$(dpkg --print-architecture) fi if [ -z "$MAC" ]; then bytes=$(dd status=noxfer if=/dev/urandom bs=3 count=1 | hexdump -v -e '/1 ":%02X"') MAC=52:54:00$bytes fi cat >&2 <<_eof hostname: $HOSTNAME arch: $ARCH suite: $SUITE ram: $RAM MiB disk: $DISK GiB wipe: $WIPE console: $CONSOLE restart: $RESTART net bridge: $BRIDGE macaddress: $MAC _eof if [ -n "$IPADDRESS" ]; then cat >&2 <<_eof ipaddress: $IPADDRESS netmask: $NETMASK gateway: $GATEWAY nameserver: $NAMESERVER _eof else cat >&2 <<_eof dhcp: true _eof fi echo >&2 virsh --connect=$QEMU_URI vol-create-as $STORAGE_POOL virt-$HOSTNAME ${DISK}G if [ $WIPE = 1 ]; then echo >&2 "Wiping diskā€¦" virsh --connect=$QEMU_URI vol-wipe --pool $STORAGE_POOL --algorithm zero virt-$HOSTNAME fi if [ $DEBUG = 1 ]; then DEBUGFLAG=--debug extra_args="$extra_args DEBCONF_DEBUG=5" fi virt-install --connect=$QEMU_URI \ ${DEBUGFLAG:-} \ -n $HOSTNAME \ -r $RAM \ --disk vol=$STORAGE_POOL/virt-$HOSTNAME \ --network bridge=$BRIDGE,mac=$MAC \ --graphics=vnc \ --serial=pty \ --watchdog i6300esb,action=reset \ --os-variant=debianwheezy \ --initrd-inject=$BASEDIR/preseed/$SUITE/preseed.cfg \ --initrd-inject=$tmpdir/commands.tar \ --extra-args="$extra_kargs" \ --prompt --noautoconsole --autostart $extra_opts \ -l $BASEDIR/installer-amd64/$SUITE \ "$@" virsh --connect=$QEMU_URI console $HOSTNAME if [ $RESTART = 1 ]; then virsh --connect=$QEMU_URI "start $HOSTNAME; console $HOSTNAME" fi