Skip to content

Instantly share code, notes, and snippets.

@smoser
Last active August 22, 2016 16:44
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/f112518132f2849443f8edfcf292c8ec to your computer and use it in GitHub Desktop.
Save smoser/f112518132f2849443f8edfcf292c8ec to your computer and use it in GitHub Desktop.
cloud-init build and run lxc stuff

cloud-init testing in lxc

build to current to /tmp/cloud

deb="/tmp/cloud-init_all.deb"
UNCOMMITTED=1 ./packages/bddeb -S && rm -Rf ../build/ &&
  schroot -rc cidev -- ../go-schroot-build cloud-init.dsc ../build -us -uc && cp cloud-init_all.deb "$deb"

create but not start an lxc container

lxc init xenial x1

patch in $deb

lxc-chroot x1 -- sh -ec 'd=/tmp/my.deb; trap "rm -f $d" EXIT; cat > $d && dpkg -i $d' < "$deb"

patch in datasource seed

add a seed datasource and clean others. The example path here is in https://code.launchpad.net/~smoser/+junk/cloudinit-ds-test/

dsdir=$HOME/src/cloud-init/cloudinit-ds-test/tests/config-drive/network_json_and_interfaces/local/configdrive/

tar -C "$dsdir" -cf - . |
    lxc-chroot x1 -- sh -ec \
         'vlc=/var/lib/cloud; sdir=$vlc/seed/config_drive; 
          rm -Rf $vlc && mkdir -p $sdir && tar -C "$sdir" -xvf -'

patch instance to not use rsyslog for cloud-init

lxc-chroot x1 -- sed -i "/[*]log_syslog/s/^[^#]/#/" /etc/cloud/cloud.cfg.d/05_logging.cfg

start it

lxc start x1

random notes

  • remove cloud-iinit things other than seed for rapid testing rm -Rf /var/lib/cloud/{data,handlers,instance,instances,scripts,sem} /var/log/cloud-init*.log
  • add a nic to a container (y1) with name some-name2 lxc config device add y1 some-name2 nic nictype=bridged parent=lxdbr0 name=eth1
  • remove nic lxc config device remove y1 some-name2
#!/bin/sh
# ./go-create-container y3 52:54:00:12:34:aa 52:54:00:12:34:ab
rel="yakkety"
name=$1
shift
fail() { echo "$@" 1>&2; exit 1; }
xlxc() { set -- lxc "$@"; echo "$@" 1>&2; "$@"; }
xlxc init "$rel" "$name" || fail
n=0
for p in "$@"; do
nname=${p%/*}
nmac=${p#*/}
if [ "$nname" = "$p" ]; then
nname=$(printf "eth%s" "$n")
fi
if [ "$n" = "0" ]; then
nname=eth0
#xlxc config unset "$name" "volatile.$nname.hwaddr" &&
xlxc config set "$name" "volatile.$nname.hwaddr" "$nmac" ||
fail "failed setting volatile.$name.hwaddr=$nmac"
else
xlxc config device add "$name" "$nname" nic \
nictype=bridged parent=lxdbr0 "name=$nname" hwaddr="$nmac" ||
fail "failed adding nic $nname to $name"
fi
n=$((n+1))
done
#!/bin/sh
##
## Create a patched image '$patched' and then indirectly use it
## as a master image providing your own user-data to it.
##
## This works around behavior in lxc that renders template files only
## on 'copy'. but copy provides no --config option.
set -e
ref=ubuntu-daily:yakkety
patched=y-patched
deb=/tmp/cloud-init_all.deb
name="${1:-y1}"
userdata="$(cat user-data)"
run() { echo "$@" 1>&2; "$@"; }
## patch the image by installing a new deb
run lxc init "$ref" "$patched";
run lxc-chroot "$patched" -- sh -ec 'd=/tmp/my.deb;
trap "rm -f $d" EXIT; cat > $d && dpkg -i $d' < "$deb"
## dance around
## get '$name-tmp' with the right user.user-data before copying it to '$name'.
run lxc copy "$patched" "$name-tmp"
run lxc config set "$name-tmp" user.user-data "$userdata"
run lxc copy "$name-tmp" "$name"
run lxc delete "$name-tmp"
## start
run lxc start "$name"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment