ubuntu-cloud-prep revision 5d066f24e65ef482cdfee241ce65e060d1652efc
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync#!/bin/bash
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync## If the container being cloned has one or more lxc.hook.clone
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync## specified, then the specified hooks will be called for the new
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync## container. The arguments passed to the clone hook are:
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync## 1. the container name
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync## 2. a section ('lxc')
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync## 3. hook type ('clone')
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync## 4. .. additional arguments to lxc-clone
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync## Environment variables:
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync## LXC_ROOTFS_MOUNT: path to the root filesystem
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync## LXC_CONFIG_FILE: path to config file
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync## LXC_SRC_NAME: old container name
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync## LXC_ROOTFS_PATH: path or device on which the root fs is located
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsyncset -f
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsyncVERBOSITY="0"
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsyncerror() { echo "$@" 1>&2; }
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsyncdebug() { [ "$VERBOSITY" -ge "$1" ] || return 0; shift; error "$@"; }
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsyncfail() { [ $# -eq 0 ] || error "$@"; exit 1; }
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsyncprep_usage() {
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsynccat <<EOF
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsyncUsage: ${0##*/} [options] root-dir
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync root-dir is the root directory to operate on
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync [ -C | --cloud ]: do not configure a datasource. incompatible with
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync options marked '[ds]'
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync [ -i | --instance-id]: instance-id for cloud-init, defaults to random [ds]
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync [ -L | --nolocales ]: Do not copy host's locales into container
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync [ -S | --auth-key ]: ssh public key file for datasource [ds]
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync [ -u | --userdata ]: user-data file for cloud-init [ds]
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync [ -V | --vendordata ]: vendor-data file for cloud-init [ds]
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsyncEOF
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync}
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsyncprep() {
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync local short_opts="Chi:L:S:u:v"
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync local long_opts="auth-key:,cloud,help,hostid:,name:,nolocales:,create-etc-init,userdata:,vendordata:,verbose"
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync local getopt_out getopt_ret
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync getopt_out=$(getopt --name "${0##*/}" \
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync --options "${short_opts}" --long "${long_opts}" -- "$@" 2>/dev/null) ||
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync :
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync getopt_ret=$?
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync if [ $getopt_ret -eq 0 ]; then
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync eval set -- "${getopt_out}" ||
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync { error "Unexpected error reading usage"; return 1; }
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync fi
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync local cur="" next=""
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync local vendordata="" userdata="" hostid="" authkey="" locales=1 cloud=0
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync local create_etc_init=0 name="ubuntucloud-lxc"
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync while [ $# -ne 0 ]; do
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync cur="$1"; next="$2";
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync case "$cur" in
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync -C|--cloud) cloud=1;;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync -h|--help) prep_usage; return 0;;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync --name) name="$next";;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync -i|--hostid) hostid="$next";;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync -L|--nolocales) locales=0;;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync --create-etc-init) create_etc_init=1;;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync -S|--auth-key)
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync [ -f "$next" ] ||
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync { error "--auth-key: '$next' not a file"; return 1; }
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync authkey="$next";;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync -V|--vendordata)
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync [ -f "$next" ] ||
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync { error "--vendordata: '$next' not a file"; return 1; }
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync vendordata="$next";;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync -u|--userdata)
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync [ -f "$next" ] ||
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync { error "--userdata: '$next' not a file"; return 1; }
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync userdata="$next";;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync -v|--verbose) VERBOSITY=$((${VERBOSITY}+1));;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync --) shift; break;;
14ea49401f3c8c61422aefbda43809e275f60c6cvboxsync esac
shift;
done
[ $# -eq 1 ] || {
prep_usage 1>&2;
error "expected 1 arguments, got ($_LXC_HOOK) $#: $*";
return 1;
}
local root_d="$1";
if [ $getopt_ret -ne 0 -a "$_LXC_HOOK" = "clone" ]; then
# getopt above failed, but we were called from lxc clone. there might
# be multiple clone hooks and the args provided here not for us. This
# seems like not the greatest interface, so all we'll do is mention it.
error "${0##*}: usage failed, continuing with defaults"
fi
[ "$create_etc_init" -eq 0 ] ||
echo "#upstart needs help for overlayfs (LP: #1213925)." > \
"$root_d/etc/init/.overlayfs-upstart-helper" ||
{ error "failed to create /etc/init in overlay"; return 1; }
local seed_d=""
seed_d="$root_d/var/lib/cloud/seed/nocloud-net"
echo "$name" > "$root_d/etc/hostname" ||
{ error "failed to write /etc/hostname"; return 1; }
if [ $cloud -eq 1 ]; then
debug 1 "--cloud provided, not modifying seed in '$seed_d'"
else
if [ -z "$hostid" ]; then
hostid=$(uuidgen | cut -c -8) && [ -n "$hostid" ] ||
{ error "failed to get hostid"; return 1; }
fi
mkdir -p "$seed_d" ||
{ error "failed to create '$seed_d'"; return 1; }
echo "instance-id: lxc-$hostid" > "$seed_d/meta-data" ||
{ error "failed to write to $seed_d/meta-data"; return 1; }
echo "local-hostname: $name" >> "$seed_d/meta-data" ||
{ error "failed to write to $seed_d/meta-data"; return 1; }
if [ -n "$authkey" ]; then
{
echo "public-keys:" &&
sed -e '/^$/d' -e 's,^,- ,' "$authkey"
} >> "$seed_d/meta-data"
[ $? -eq 0 ] ||
{ error "failed to write public keys to metadata"; return 1; }
fi
local larch="usr/lib/locale/locale-archive"
if [ $locales -eq 1 ]; then
cp "/$larch" "$root_d/$larch" || {
error "failed to cp '/$larch' '$root_d/$larch'";
return 1;
}
fi
if [ -z "$MIRROR" ]; then
MIRROR="http://archive.ubuntu.com/ubuntu"
fi
if [ -n "$userdata" ]; then
cp "$userdata" "$seed_d/user-data"
else
{
local lc=$(locale | awk -F= '/LANG=/ {print $NF; }')
echo "#cloud-config"
echo "output: {all: '| tee -a /var/log/cloud-init-output.log'}"
echo "apt_mirror: $MIRROR"
echo "manage_etc_hosts: localhost"
[ -z "$LANG" ] || echo "locale: $LANG";
echo "password: ubuntu"
echo "chpasswd: { expire: false; }"
} > "$seed_d/user-data"
fi
[ $? -eq 0 ] || {
error "failed to write user-data write to '$seed_d/user-data'";
return 1;
}
if [ -n "$vendordata" ]; then
cp "$vendordata" "$seed_d/vendor-data" || {
error "failed copy vendordata to $seed_d/vendor-data";
return 1;
}
fi
fi
}
main() {
# main just joins 2 modes of being called. from user one from lxc clone
local _LXC_HOOK
if [ -n "$LXC_ROOTFS_MOUNT" -a "$3" = "clone" ]; then
_LXC_HOOK="clone"
local name="$1" create_etc_init=""
shift 3
# if mountpoint is overlayfs then add '--create-etc-init'
[ "${LXC_ROOTFS_PATH#overlayfs}" != "${LXC_ROOTFS_PATH}" ] &&
create_etc_init="--create-etc-init"
debug 1 prep "--name=$name" $create_etc_init "$LXC_ROOTFS_MOUNT" "$@"
prep "--name=$name" $create_etc_init "$LXC_ROOTFS_MOUNT" "$@"
else
_LXC_HOOK=""
prep "$@"
fi
return $?
}
main "$@"
# vi: ts=4 expandtab