--- /dev/null
+#!/bin/sh -e
+
+if [ -z $1 ]; then
+ echo "Usage:"
+ echo
+ echo "$0 cfg_hostname.sh [image | setup | xml | define | start]"
+ echo "$0 cfg_hostname.sh [undefine | delete]"
+ exit 1
+fi
+. ./$1 || exit 1
+
+IMAGE_POOL=/var/lib/lxc
+
+IMAGE_FILE=${IMAGE_POOL}/${FACTER_hostname}
+IMAGE_SOURCE=/var/lib/lxc/debian7-x86_64.tar.gz
+VIRSH_ARGS="-c lxc://"
+
+if [ -z $FACTER_fqdn ]; then
+ echo "ERROR: no facter fqdn settings found"
+ exit 1
+fi
+if [ -z $FACTER_hostname ]; then
+ echo "ERROR: no facter hostname settings found"
+ exit 1
+fi
+
+
+image() {
+ if [ -e ${IMAGE_FILE} ]; then
+ echo "ERROR: image ${IMAGE_FILE} already exists"
+ exit 1
+ fi
+
+ mkdir ${IMAGE_FILE}
+ (cd ${IMAGE_FILE}; tar xzf ${IMAGE_SOURCE})
+}
+
+
+setup() {
+ cd ${IMAGE_FILE} || exit 1
+
+ cp -vp /etc/{resolv.conf,apt/sources.list,krb5.conf} ./etc/
+ echo "pts/0" >> etc/securetty
+
+ echo "${FACTER_hostname}" > etc/hostname
+
+ cat << __EOF__ > etc/network/interfaces
+# This file describes the network interfaces available on your system
+# and how to activate them. For more information, see interfaces(5).
+
+# The loopback network interface
+auto lo
+iface lo inet loopback
+
+__EOF__
+ if [ -n "${FACTER_ipaddress}" ]; then
+ if [ "${FACTER_ipaddress}" = 'dhcp' ]; then
+ cat << __EOF__ >> etc/network/interfaces
+auto eth0
+iface eth0 inet dhcp
+
+__EOF__
+ else
+ cat << __EOF__ >> etc/network/interfaces
+auto eth0
+iface eth0 inet static
+ address $FACTER_ipaddress
+ netmask $FACTER_netmask
+ gateway $FACTER_gw
+
+__EOF__
+ fi
+ fi
+ if [ -n "${FACTER_ipaddress6}" ]; then
+ cat << __EOF__ >> etc/network/interfaces
+iface eth0 inet6 static
+ address ${FACTER_ipaddress6}
+ netmask ${FACTER_netmask6}
+ gateway ${FACTER_gw6}
+
+__EOF__
+ fi
+
+ if [ ! -d root/.ssh ]; then
+ mkdir -p root/.ssh
+ fi
+ if [ ! -f root/.ssh/authorized_keys -a -f /root/.ssh/authorized_keys ]; then
+ cp -v /root/.ssh/authorized_keys root/.ssh
+ fi
+
+ sed -i etc/inittab -e 's/^#T\(0\)/T\1/'
+ sed -i etc/inittab -e 's/^\([2-5]:\)/#\1/'
+
+ cat >> root/.k5login << __EOF__
+dexter@ADMIN.META
+mulac@ADMIN.META
+salvet@ADMIN.META
+valtri@ADMIN.META
+xparak@ADMIN.META
+__EOF__
+
+ cd
+}
+
+
+xml() {
+ i=1
+ for d in ${DISKS}; do
+ XML_DISK="${XML_DISK}
+ <filesystem type='block' accessmode='passthrough'>
+ <source dev='${d}'/>
+ <target dir='/data/${i}'/>
+ </filesystem>"
+ i=$((i+1))
+ done
+ if [ -n "${XENBR}" ]; then
+ XML_NET="
+ <interface type='bridge'>
+ <source bridge='${XENBR}'/>
+ <mac address='${FACTER_macaddress}'/>
+ <model type='virtio'/>
+ </interface>
+
+"
+ else
+ XML_NET="
+ <interface type='network'>
+ <source network='default'/>
+ <mac address='${FACTER_macaddress}'/>
+ <model type='virtio'/>
+ </interface>
+
+"
+ fi
+ cat << __EOF__ > /tmp/machine.xml
+<domain type='lxc'>
+ <name>${FACTER_hostname}</name>
+ <currentMemory unit='MiB'>${SIZE_MEM}</currentMemory>
+ <memory unit='MiB'>${SIZE_MEM}</memory>
+ <vcpu>${SIZE_CPU}</vcpu>
+ <os>
+ <type arch='x86_64'>exe</type>
+ <init>/sbin/init</init>
+ </os>
+ <features>
+ <privnet/>
+ </features>
+
+ <devices>
+
+ <emulator>/usr/lib/libvirt/libvirt_lxc</emulator>
+
+ <filesystem type='mount' accessmode='passthrough'>
+ <source dir='${IMAGE_FILE}'/>
+ <target dir='/'/>
+ </filesystem>${XML_DISK}
+${XML_NET}
+
+ <console type='pty'>
+ <target type='lxc' port='0'/>
+ </console>
+
+ </devices>
+</domain>
+__EOF__
+}
+
+shift
+actions="$@"
+
+if [ -z "${actions}" ]; then
+ actions='image setup xml define start'
+fi
+
+for action in ${actions}; do case ${action} in
+image) image ;;
+setup) setup ;;
+xml) xml ;;
+define)
+ virsh ${VIRSH_ARGS} define /tmp/machine.xml
+ ;;
+start)
+ virsh ${VIRSH_ARGS} start ${FACTER_hostname}
+ echo "INFO: ${FACTER_hostname} started"
+ ;;
+stop)
+ virsh ${VIRSH_ARGS} destroy ${FACTER_hostname}
+ echo "INFO: ${FACTER_hostname} destroyed"
+ ;;
+undefine)
+ virsh ${VIRSH_ARGS} undefine ${FACTER_hostname}
+ ;;
+delete)
+ virsh ${VIRSH_ARGS} undefine ${FACTER_hostname} || :
+ rm -fr ${IMAGE_POOL}/${FACTER_hostname}
+ ;;
+esac
+done
+
+echo "INFO: $0 done"