Skip to content

Instantly share code, notes, and snippets.

@sandhose
Created December 4, 2016 12:15
Show Gist options
  • Save sandhose/d4b65c3527fbe2e2dac40e4a0364ea81 to your computer and use it in GitHub Desktop.
Save sandhose/d4b65c3527fbe2e2dac40e4a0364ea81 to your computer and use it in GitHub Desktop.
#!/bin/sh
if [ $# -ne 2 ]; then
echo "Usage: `basename $0` name ip"
exit 1
fi
TEMPLATE=${TEMPLATE:-"alpine"}
NAME=$1
IP=$2
LXC_PATH=/srv/virt/lxc/
DHCP_HOSTS=/srv/virt/dhcp-hosts
if [ -d $LXC_PATH/$NAME ]; then
echo "$NAME already exists. Please choose another container name"
exit 2
fi
if `grep -q $IP $DHCP_HOSTS`; then
echo "$IP is already taken. Please choose another IP for $NAME"
exit 2
fi
echo " → Creating LXC"
lxc-create -n $NAME -t $TEMPLATE
if [ $? -ne 0 ]; then
echo "\`lxc-create -n $NAME -t $TEMPLATE\` failed."
exit 3
fi
echo " → Configuring dnsmasq"
HW_ADDR=`grep lxc.network.hwaddr $LXC_PATH/$NAME/config | sed 's/lxc.network.hwaddr = //'`
echo "$HW_ADDR,$IP,$NAME" >> $DHCP_HOSTS
echo "$IP $NAME" >> /etc/hosts
service dnsmasq reload
echo " → Creating service"
ln -s lxc /etc/init.d/lxc.$NAME
echo -e "\n type \`service lxc.$NAME start\` to start the LXC\n"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment