Skip to content

Instantly share code, notes, and snippets.

@LenzGr
Created February 18, 2015 11:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LenzGr/ab372bbf30d755e711d9 to your computer and use it in GitHub Desktop.
Save LenzGr/ab372bbf30d755e711d9 to your computer and use it in GitHub Desktop.
A shell script to quickly clone an existing LXC Container (e.g. a plain CentOS6 installation) and to update the local DNS to make it accessible.
#!/bin/bash
HOSTSFILE="/etc/hosts"
if test -z "$1"
then
echo "Please provide a container/host name."
exit 1
else
NAME="$1"
fi
if [ $(whoami) != "root" ]
then
echo "Root privileges required to run this script!"
exit 1
fi
if test -n "$(lxc-ls -1 | grep $NAME)"
then
read -n 1 -p "$NAME exists. OK to destroy (y/n)? " answer
case ${answer:0:1} in
y|Y )
echo
lxc-destroy -f -n $NAME
;;
* )
echo
echo "OK, exiting."
exit 0
;;
esac
fi
lxc-clone -s -o centos6base -n $NAME
echo "Starting $NAME."
lxc-start -d -n $NAME
echo -n "Determining IP address."
while test -z "$HOSTIP"
do
echo -n "."
HOSTIP=$(lxc-info -H -i -n $NAME)
sleep 1
done
echo
echo "$NAME has IP $HOSTIP - updating $HOSTSFILE"
if test -n "$(grep $NAME $HOSTSFILE)"
then
sed -e "s/^\(.*\) $NAME $NAME\.local/$HOSTIP $NAME $NAME\.local/" -i $HOSTSFILE
else
echo "$HOSTIP $NAME $NAME.local" >> $HOSTSFILE
fi
echo "Restarting dnsmasq..."
pkill -HUP dnsmasq
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment