Skip to content

Instantly share code, notes, and snippets.

@smoser
Last active March 28, 2017 14:56
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 smoser/45935e78bf1b21ee7696be083240aa0f to your computer and use it in GitHub Desktop.
Save smoser/45935e78bf1b21ee7696be083240aa0f to your computer and use it in GitHub Desktop.
lxc / cloud-init network config: insert network config into a container

lxc-netcfg

script to insert some network configs into an lxc container and then start it. supports replacing @<nicname>_addr with the address of nicname inside the container.

has builtin configs for bond and dhcp4and6.

Notes:

  • bond is known not working (bonds do not work with ifupdown in a container).
  • if 'mode' starts with 'cc', it will write /etc/cloud/cloud.cfg.d/99-network-config.cfg otherwise it will write /var/lib/cloud/seed/nocloud-net/network-config

Example config

providing dhcp4and6 will write a network config like the following to the /etc/cloud/cloud.cfg.d path.

network: 
  version: 1
  config:
    - type: physical
      name: eth0
      mac_address: 00:16:3e:9c:58:7a
      subnets:
        - type: dhcp
          control: auto
        - type: dhcp6
          control: auto
#!/bin/bash
set -f
set -e
name=$1
mode="${2:-bond}"
fpath=${3}
[ -n "$name" ] || { echo "must give name"; exit 1; }
# pastebinit `which lxc-chroot`
# http://paste.ubuntu.com/24198752/
bond() {
cat <<"EOF"
version: 1
config:
- type: physical
name: iface0
mac_address: @eth0_addr
- type: physical
name: iface1
mac_address: @eth1_addr
- type: bond
name: bond0
bond_interfaces: [iface0, iface1]
params:
bond-mode: active-backup
subnets:
- type: dhcp
control: auto
EOF
}
dhcp4and6() {
cat <<"EOF"
version: 1
config:
- type: physical
name: eth0
mac_address: @eth0_addr
subnets:
- type: dhcp
control: auto
- type: dhcp6
control: auto
EOF
}
ccify() {
local out=""
out=$("$@") || return
echo "network:"
printf "%s" "$out" | sed 's,^, ,'
}
lxc init ubuntu-daily:zesty $name
lxc network attach lxdbr0 $name eth1
# lxc-chroot has access to /var/lib/cloud/seed/nocloud-net/
# despite https://github.com/lxc/lxd/issues/3088 because it does
# a 'start', which will get those things rendered.
# the start just doesn't actually run an init.
out=$(lxc-chroot "$name" sh -ec '
for i in /sys/class/net/*; do
[ -e "$i/address" ] || continue
n=${i##*/}
read addr < "$i/address"
echo $n,$addr
done
')
seds=( )
for pair in $out; do
addr=${pair#*,}
nicname=${pair%,*}
echo "@${nicname}_addr -> $addr" 1>&2
seds=( "${seds[@]}" -e "s/@${nicname}_addr/${addr}/" )
done
cmd=( )
case "$mode" in
bond|dhcp4and6) cmd=( $mode );;
cc-bond|cc-dhcp4and6) cmd=( ccify "${mode#cc-}" );;
*)
[ -f "$mode" ] ||
{ echo "$mode: not a file or known mode"; exit 1; }
cmd=( cat "$mode" )
if [ "${CCIFY:-0}" != "0" ]; then
cmd=$(ccify cat "$mode")
fi
esac
out=$("${cmd[@]}")
cfg=$(printf "%s" "$out" | sed "${seds[@]}")
if [ -z "$fpath" ]; then
case "$mode" in
cc-*) fpath="/etc/cloud/cloud.cfg.d/99-network-config.cfg";;
*) fpath="/var/lib/cloud/seed/nocloud-net/network-config";;
esac
fi
printf "%s\n" "$cfg" | lxc file push - "$name/$fpath"
printf "== %s ==\n%s\n" "$fpath" "$cfg"
lxc start "$name"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment