From: Brett Parker Date: Sun, 22 May 2016 13:26:56 +0000 (+0100) Subject: Add rudimentary network configuration support X-Git-Url: https://git.sommitrealweird.co.uk/lxc-debian-unprivileged.git/commitdiff_plain/00184e955388c820536d620f4f8b5b44aaa142ab?hp=6b2f4764180633ef9eed9a89f89086a877f50218 Add rudimentary network configuration support --- diff --git a/templates/lxc-debian-unprivileged b/templates/lxc-debian-unprivileged index d801b9d..9a3f248 100755 --- a/templates/lxc-debian-unprivileged +++ b/templates/lxc-debian-unprivileged @@ -54,11 +54,40 @@ Required arguments: Optional arguments: [ -m | --mirror ]: The debian mirror to user +[ -n | --network ]: How to configure networking + +Network spec: + [,options] + + type is one of: + dhcp4: v4 dhcp will be enabled. + dhcp6: v6 dhcp will be enabled. + dhcp: v4 and v6 dhcp will be enabled. + static: no dhcp will be enabled + + options: + this is a , seperated list, and sets up static assignments for v4 or v6, + regardless of type. + The order of arguments is + staticv4/staticnetmask + staticv6/staticv6netmask + gateway + v6gateway + + examples: + + --network dhcp4 (default) + --network dhcp6 (v6 dhcp) + --network dhcp (v4 and v6 dhcp) + --network dhcp4,,2001:db8:1234:5678::1/64 (dhcp4 and static v6 address) + --network static,,2001:db8:1234:5678::5/64,,fe80::1 (static v6) + --network static,192.0.2.15/24,,192.0.2.1 (static v4) + EOF return 0 } -options=$(getopt -o r:m:h -l release:,mirror:,help,mapped-uid:,mapped-gid:,name:,path:,rootfs: -- "$@") +options=$(getopt -o r:m:n:h -l release:,mirror:,network:,help,mapped-uid:,mapped-gid:,name:,path:,rootfs: -- "$@") if [ $? -ne 0 ]; then usage @@ -69,6 +98,7 @@ eval set -- "$options" DEBIAN_MIRROR="http://mirror.mythic-beasts.com/debian/" DEBIAN_RELEASE="jessie" +NETWORK_CONFIG="" disable_initscripts() { cat < "${LXC_ROOTFS}/usr/sbin/policy-rc.d" @@ -90,6 +120,7 @@ while :; do -h|--help) usage && exit 1;; -r|--release) DEBIAN_RELEASE="$2"; shift 2;; -m|--mirror) DEBIAN_MIRROR="$2"; shift 2;; + -n|--network) NETWORK_CONFIG="$2"; shift 2;; --mapped-uid) MAPPED_UID="$2"; shift 2;; --mapped-gid) MAPPED_GID="$2"; shift 2;; --name) LXC_NAME="$2"; shift 2;; @@ -99,6 +130,136 @@ while :; do esac done +INTERFACE_DEFAULTS="auto eth0 +iface eth0 inet dhcp" + +generate_network_config() { + if [ "$NETWORK_CONFIG" == "" ]; then + echo "$INTERFACE_DEFAULTS" + return 0 + fi + + echo "auto eth0" + # see if there's a type + network_type=${NETWORK_CONFIG/,*} + other_params=${NETWORK_CONFIG#*,} + + v4_configured=no + v6_configured=no + + if [ "$network_type" == "dhcp" ]; then + echo "iface eth0 inet dhcp" + echo "iface eth0 inet6 dhcp" + v4_configured=yes + v6_configured=yes + elif [ "$network_type" == "dhcp4" ]; then + echo "iface eth0 inet dhcp" + v4_configured=yes + elif [ "$network_type" == "dhcp6" ]; then + echo "iface eth0 inet6 dhcp" + v6_configured=yes + elif [ "$network_type" != "static" ]; then + echo "Unknown network type: $network_type" 1>&2 + echo 1>&2 + usage 1>&2 + exit 1 + fi + + [ "$network_type" == "$other_params" ] && return 0 + + v4_static=${other_params/,*} + other_params=${other_params#*,} + + if [ "$v4_static" != "" ]; then + if [ "$v4_configured" == "yes" ]; then + echo "Both v4 DHCP and Static - giving up." 1>&2 + echo 1>&2 + usage 1>&2 + exit 1 + fi + fi + + if [ "$v4_static" == "$other_params" ]; then + if [ "$v4_static" != "" ]; then + echo "iface eth0 inet static" + echo " address $v4_static" + fi + fi + + v6_static=${other_params/,*} + other_params=${other_params#*,} + + if [ "$v6_static" != "" ]; then + if [ "$v6_configured" == "yes" ]; then + echo "Both v6 DHCP and Static - giving up." 1>&2 + echo 1>&2 + usage 1>&2 + exit 1 + fi + fi + + if [ "$v6_static" == "$other_params" ]; then + if [ "$v4_static" ]; then + echo "iface eth0 inet static" + echo " address $v4_static" + echo + fi + + echo "iface eth0 inet6 static" + echo " address $v6_static" + + return 0 + fi + + v4_gateway=${other_params/,*} + other_params=${other_params#*,} + + if [ "$v4_gateway" == "$other_params" ]; then + if [ "$v4_static" != "" ]; then + echo "iface eth0 inet static" + echo " address $v4_static" + [ "$v4_gateway" != "" ] && echo " gateway $v4_gateway" + + if [ "$v6_static" != "" ]; then + echo "iface eth0 inet6 static" + echo " address $v6_static" + fi + + return 0 + fi + + if [ "$v4_configured" == "yes" ]; then + echo "DHCP and static gateway not supported, giving up." 1>&2 + echo 1>&2 + usage 1>&2 + exit 1 + fi + fi + + v6_gateway=${other_params/,*} + + if [ "$v4_static" != "" ]; then + echo "iface eth0 inet static" + echo " address $v4_static" + [ "$v4_gateway" != "" ] && echo " gateway $v4_gateway" + echo + fi + + if [ "$v6_static" != "" ]; then + echo "iface eth0 inet6 static" + echo " address $v6_static" + [ "$v6_gateway" != "" ] && echo " gateway $v6_gateway" + fi + + return 0 +} + +INTERFACE_DETAILS="$(generate_network_config)" + +# temporarily do something stupid +echo "$INTERFACE_DETAILS" +exit 1 + # rewrite the default config file sed -i -e "/lxc./{w ${LXC_PATH}/config-auto" -e "d}" "${LXC_PATH}/config" @@ -236,10 +397,7 @@ if [ -e "${LXC_ROOTFS}/lib/systemd/system/systemd-journald-audit.socket" ]; then touch "${LXC_ROOTFS}/etc/systemd/system/systemd-journald-audit.socket" fi -cat <> "${LXC_ROOTFS}${NETWORK_FILE}" -auto eth0 -iface eth0 inet dhcp -EOF +echo "$INTERFACE_DETAILS" >> "${LXC_ROOTFS}${NETWORK_FILE}" # and update to the latest security chroot "${LXC_ROOTFS}" apt-get update @@ -265,6 +423,7 @@ enable_initscripts rm -r "${LXC_PATH}/bin" cat <