Skip to content

Instantly share code, notes, and snippets.

@ganto
Last active August 29, 2015 14:21
Show Gist options
  • Save ganto/d5aff921c91adfeeae85 to your computer and use it in GitHub Desktop.
Save ganto/d5aff921c91adfeeae85 to your computer and use it in GitHub Desktop.
static address lxc template
---
# --------------------------------------------
# Add static hostnames to local /etc/hosts
# --------------------------------------------
- name: Add container names to /etc/hosts
lineinfile:
dest: '/etc/hosts'
regexp: '{{ item.name }}'
line: '{{ item.template_options.split("--address=")[1].split()[0] }} {{ item.name }}'
when: '"hosts_entry" in item.keys()'
with_items: lxc_containers
diff --git a/usr/share/lxc/templates/lxc-inofix.j2 b/usr/share/lxc/templates/lxc-inofix.j2
index 244363d..73c98a7 100755
--- a/usr/share/lxc/templates/lxc-inofix.j2
+++ b/usr/share/lxc/templates/lxc-inofix.j2
@@ -7,6 +7,7 @@
# Authors:
# Daniel Lezcano <daniel.lezcano@free.fr>
# Maciej Delmanowski <drybjed@gmail.com>
+# Reto Gantenbein <reto.gantenbein@inofix.ch>
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
@@ -280,14 +281,29 @@ EOF
mkdir -p $rootfs/selinux
echo 0 > $rootfs/selinux/enforce
- # configure the network using the dhcp
+ # configure the network
cat <<EOF > $rootfs/etc/network/interfaces
auto lo
iface lo inet loopback
auto eth0
-iface eth0 inet dhcp
EOF
+ if [ -n "$address" ]; then
+ cat << EOF >> $rootfs/etc/network/interfaces
+iface eth0 inet static
+ address $address
+ netmask ${netmask:-255.255.255.0}
+ gateway ${gateway:-${address%.*}.1}
+EOF
+ save_ifs=$IFS
+ IFS=,
+ for addr in ${nameserver:-${address%.*}.1}; do
+ echo " dns-nameservers ${addr}" >> $rootfs/etc/network/interfaces
+ done
+ IFS=$save_ifs
+ else
+ echo "iface eth0 inet dhcp" >> $rootfs/etc/network/interfaces
+ fi
# set the hostname
cat <<EOF > $rootfs/etc/hostname
@@ -569,13 +585,18 @@ usage()
{
cat <<EOF
$1 -h|--help -p|--path=<path> [-a|--arch] [-r|--release=<release>] [-c|--clean]
+ [--address=<ip>] [--netmask=<nm>] [--gateway=<ip>] [--nameserver=<ip>[,<ip>]]
release: the debian release (e.g. wheezy): defaults to host release
arch: the container architecture (e.g. amd64): defaults to host arch
+address: ip address, enables static network configuration
+netmask: subnetmask for static network config: defaults to 255.255.255.0
+gateway: gateway for static network config: defaults to .1 of static network
+nameserver: dns server(s) for static network config: defaults to .1 of static network
EOF
return 0
}
-options=$(getopt -o hp:n:a:r:c -l help,rootfs:,path:,name:,arch:,release:,clean -- "$@")
+options=$(getopt -o hp:n:a:r:c -l help,rootfs:,path:,name:,arch:,release:,clean,address:,netmask:,gateway:,nameserver: -- "$@")
if [ $? -ne 0 ]; then
usage $(basename $0)
exit 1
@@ -606,6 +627,10 @@ do
-r|--release) release=$2; shift 2;;
-n|--name) name=$2; shift 2;;
-c|--clean) clean=$2; shift 1;;
+ --address) address=$2; shift 2;;
+ --netmask) netmask=$2; shift 2;;
+ --gateway) gateway=$2; shift 2;;
+ --nameserver) nameserver=$2; shift 2;;
--) shift 1; break ;;
*) break ;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment