#! /bin/sh -x
-. ../config/default
-if [ -f ../config/`hostname` ] ; then . ../config/`hostname`; fi
-path=/var/lib/lxc/$1
-rootfs=$path/rootfs
-
-if [ -e $rootfs/etc/network/interfaces ]
+configure_network()
+{
+if [ $# -ne 4 ]
then
-cat <<EOF > $rootfs/etc/network/interfaces
+ exit 1
+else
+ rootfs=$1
+ address=$2
+ netmask=$3
+ gateway=$4
+ if [ -e $rootfs/etc/network/interfaces ]
+ then
+ cat <<EOF > $rootfs/etc/network/interfaces
auto lo
iface lo inet loopback
auto eth0
iface eth0 inet static
- address $IPDEBBASE.$VEIDDEB
- netmask 255.255.255.0
- gateway $IPDEBBASE.1
+ address $address
+ netmask $netmask
+ gateway $gateway
EOF
+ fi
+else
+ exit 2
+fi
+}
+
+configure_pubkey(){
+ rootfs=$1
+ pubkey=$2
+ if [ $# -ne 2 ]
+ then
+ exit 1
+ else
+ [ -d $rootfs/root/.ssh ] || mkdir $rootfs/root/.ssh
+ cp $pubkey $rootfs/root/.ssh/authorized_keys
+ fi
+}
+
+configure_hostkeydir(){
+ rootfs=$1
+ hostkeydir=$2
+ if [ $# -ne 2 ]
+ then
+ exit 1
+ else
+ [ -d $rootfs/etc/ssh ] || mkdir -p $rootfs/etc/ssh
+ [ -e $hostkeydir ] || cp $hostkeydir/ssh_host_* $rootfs/etc/ssh/
+ fi
+}
+
+usage()
+{
+ cat <<EOF
+$1 -h|--help -p|--path=<path> --clean --address=<address> --netmask=<netmask> --gateway=<gateway> --pubkey=<pubkey> --hostkeydir=<hostkeydir>
+EOF
+ return 0
+}
+
+options=$(getopt -o hp:n:c -l help,path:,name:,clean:,address:,netmask:,gateway:,pubkey:,hostkeydir -- "$@")
+if [ $? -ne 0 ]; then
+ usage $(basename $0)
+ exit 1
+fi
+eval set -- "$options"
+
+while true
+do
+ case "$1" in
+ -h|--help) usage $0 && exit 0;;
+ -p|--path) path=$2; shift 2;;
+ -n|--name) name=$2; shift 2;;
+ --address) address=$2; shift 2;;
+ --netmask) netmask=$2; shift 2;;
+ --gateway) gateway=$2; shift 2;;
+ --pubkey) pubkey=$2; shift 2;;
+ --hostkeydir) hostkeydir=$2; shift 2;;
+ --) shift 1; break ;;
+ *) break ;;
+ esac
+done
+
+if [ -z "$path" ]; then
+ echo "'path' parameter is required"
+ exit 1
fi
-[ -d $rootfs/root/.ssh ] || mkdir $rootfs/root/.ssh
-cp $SSHPUBKEY $rootfs/root/.ssh/authorized_keys
-[ -d $rootfs/etc/ssh ] || mkdir -p $rootfs/etc/ssh
-[ -e ssh ] || cp ssh/ssh_host_* $rootfs/etc/ssh/
+if [ "$(id -u)" != "0" ]; then
+ echo "This script should be run as 'root'"
+ exit 1
+fi
+
+rootfs=$path/rootfs
+
+install_debian $rootfs
+if [ $? -ne 0 ]; then
+ echo "failed to install debian"
+ exit 1
+fi
+
+configure_network $rootfs $address $netmask $gateway
+if [ $? -ne 0 ]; then
+ echo "failed to configure network"
+ exit 1
+fi
+
+configure_pubkey $rootfs $pubkey
+if [ $? -ne 0 ]; then
+ echo "failed to configure pubkey"
+ exit 1
+fi
+
+configure_hostkeydir $rootfs $hostkeydir
+if [ $? -ne 0 ]; then
+ echo "failed to configure hostkeydir"
+ exit 1
+fi