Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@shadycuz
Created November 8, 2016 10:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shadycuz/4da0cbabcaeddafa5e38b914ce4db0d7 to your computer and use it in GitHub Desktop.
Save shadycuz/4da0cbabcaeddafa5e38b914ce4db0d7 to your computer and use it in GitHub Desktop.
Allows you to create LXD containers with static ip in one go. Thanks to http://jason.trickett.us/2016/08/lxd-containers-static-ip-addresses-heres/
#!/bin/bash
conf="/etc/default/lxd_dnsmasq.conf"
func1 ()
{
sh -c "echo 'dhcp-host=$1,$2' >> $conf" && service lxd-bridge stop && service lxd-bridge start && lxc launch $3 $1
wait
lxc restart $1 &
}
func2 ()
{
if grep -q -w $1 $conf
then
str0=$(printf "There is already an entry for $1 in the lxd_dnsmasq.conf file.\nMake sure that no existing container is using that name (lxc list) before proceeding.\nChoose y to overwrite the existing entry for $2 and continue.\nChoose n to end this script") echo "$str0"
read
if [[ $REPLY == [Nn] ]]
then
exit 0
elif [[ $REPLY == [Yy] ]]
then
if grep -q -w $2 $conf
then
str1=$(printf "The ip address $2 is already in the lxd_dnsmasq.conf file.\nMake sure that no existing container is using that address (lxc list) before proceeding.\nChoose y to overwrite the existing entry for $2 and continue.\nChoose n to end this script") echo "$str1"
read
if [[ $REPLY == [Nn] ]]
then
exit 0
elif [[ $REPLY == [Yy] ]]
then
sed -i "/$2/d" $conf
sed -i "/$1/d" $conf
func1 $1 $2 $3
fi
else
sed -i "/$1/d" $conf
func1 $1 $2 $3
fi
else
func1 $1 $2 $3
fi
elif grep -q -w $2 $conf
then
str1=$(printf "The ip address $2 is already in the lxd_dnsmasq.conf file.\nMake sure that no existing container is using that address (lxc list) before proceeding.\nChoose y to overwrite the existing entry for $2 and continue.\nChoose n to end this script") echo "$str1"
read
if [[ $REPLY == [Nn] ]]
then
exit 0
elif [[ $REPLY == [Yy] ]]
then
sed -i "/$2/d" $conf
func1 $1 $2 $3
fi
else
func1 $1 $2 $3
fi
}
case "$1" in
(destroy)
lxc stop $2
wait
lxc delete $2
sed -i "/$2/d" $conf
;;
(*)
func2 $1 $2 $3
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment