#!/bin/sh
name=$1
[ -n "$name" ] || { echo "must give name"; exit 1; }
set -ex
lxc init ubuntu-daily:zesty $name
lxc network attach lxdbr0 $name eth1
# pastebinit `which lxc-chroot`
# http://paste.ubuntu.com/24198752/
# 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.
mode=${2:-bond}
p=/var/lib/cloud/seed/nocloud-net/network-config
set +x
lxc-chroot "$name" sh -c '
#!/bin/sh
fname="$1"
mode="$2"
scn="/sys/class/net/"
read addr0 < $scn/eth0/address
read addr1 < $scn/eth1/address
if [ "$mode" = "bond" ]; then
cat > "$fname" <<EOF
version: 1
config:
- type: physical
name: iface0
mac_address: $addr0
- type: physical
name: iface1
mac_address: $addr1
- type: bond
name: bond0
bond_interfaces: [iface0, iface1]
params:
bond-mode: active-backup
subnets:
- type: dhcp
control: auto
EOF
else
cat > "$fname" <<EOF
version: 1
config:
- type: physical
name: eth1
mac_address: $addr0
subnets:
- type: dhcp
control: auto
- type: physical
name: eth2
mac_address: $addr1
subnets:
- type: dhcp
control: auto
EOF
fi
cat "$fname"
' -- "$p" "$mode"
lxc start "$name"