ubuntu-cloud-prep revision 65d8ae9c4a66f5ca85289c02dc06d63261c84619
c892d0dfeda5c022104813bf59275f840923551bCraig McDonnell#!/bin/bash
f0b8f6dd844f8752946b865bdd44175863b15812Peter Major## If the container being cloned has one or more lxc.hook.clone specified,
f0b8f6dd844f8752946b865bdd44175863b15812Peter Major## then the specified hooks will be called for the new container. The first
f0b8f6dd844f8752946b865bdd44175863b15812Peter Major## 3 arguments passed to the clone hook will be:
f0b8f6dd844f8752946b865bdd44175863b15812Peter Major## 1. the container name
f0b8f6dd844f8752946b865bdd44175863b15812Peter Major## 2. a section ('lxc')
f0b8f6dd844f8752946b865bdd44175863b15812Peter Major## 3. hook type ('clone')
f0b8f6dd844f8752946b865bdd44175863b15812Peter Major## 4. .. additional arguments to lxc-clone
f0b8f6dd844f8752946b865bdd44175863b15812Peter Major## Environment variables:
f0b8f6dd844f8752946b865bdd44175863b15812Peter Major## LXC_ROOTFS_MOUNT: path under which the container's root fs is mounted.
f0b8f6dd844f8752946b865bdd44175863b15812Peter Major## LXC_CONFIG_FILE: The configuration file pathname
f0b8f6dd844f8752946b865bdd44175863b15812Peter Major## LXC_SRC_NAME: old container name
f0b8f6dd844f8752946b865bdd44175863b15812Peter Major## LXC_ROOTFS_PATH: path or device on which the root fs is located
f0b8f6dd844f8752946b865bdd44175863b15812Peter Major
f0b8f6dd844f8752946b865bdd44175863b15812Peter MajorVERBOSITY=""
f0b8f6dd844f8752946b865bdd44175863b15812Peter Major
c892d0dfeda5c022104813bf59275f840923551bCraig McDonnellerror() { echo "$@" 1>&2; }
c892d0dfeda5c022104813bf59275f840923551bCraig McDonnelldebug() { [ "$1" -ge "$VERBOSITY" ] || return; shift; error "$@"; }
c892d0dfeda5c022104813bf59275f840923551bCraig McDonnellfail() { [ $# -eq 0 ] || error "$@"; exit 1; }
c892d0dfeda5c022104813bf59275f840923551bCraig McDonnell
2d8eba3efb0ced5c2ab004e5b3e949094747aeb3jenkinsprep_usage() {
c892d0dfeda5c022104813bf59275f840923551bCraig McDonnellcat <<EOF
c892d0dfeda5c022104813bf59275f840923551bCraig McDonnellUsage: ${0##*/} [options] root-dir
c892d0dfeda5c022104813bf59275f840923551bCraig McDonnell
c892d0dfeda5c022104813bf59275f840923551bCraig McDonnell root-dir is the root directory to operate on
fba815ec883afa7d9675096f5266e43b224bbec4Peter Major
c892d0dfeda5c022104813bf59275f840923551bCraig McDonnell [ -C | --cloud ]: do not configure a datasource. incompatible with
c892d0dfeda5c022104813bf59275f840923551bCraig McDonnell options marked '[ds]'
c892d0dfeda5c022104813bf59275f840923551bCraig McDonnell [ -i | --instance-id]: instance-id for cloud-init, defaults to random [ds]
c892d0dfeda5c022104813bf59275f840923551bCraig McDonnell [ -L | --nolocales ]: Do not copy host's locales into container
c892d0dfeda5c022104813bf59275f840923551bCraig McDonnell [ -S | --auth-key ]: ssh public key file for datasource [ds]
c892d0dfeda5c022104813bf59275f840923551bCraig McDonnell [ -u | --userdata ]: user-data file for cloud-init [ds]
c892d0dfeda5c022104813bf59275f840923551bCraig McDonnell
c892d0dfeda5c022104813bf59275f840923551bCraig McDonnellEOF
c892d0dfeda5c022104813bf59275f840923551bCraig McDonnell}
c892d0dfeda5c022104813bf59275f840923551bCraig McDonnell
c892d0dfeda5c022104813bf59275f840923551bCraig McDonnellprep() {
c892d0dfeda5c022104813bf59275f840923551bCraig McDonnell local short_opts="Chi:L:S:u:v"
c892d0dfeda5c022104813bf59275f840923551bCraig McDonnell local long_opts="auth-key:,cloud,help,hostid:,name:,nolocales:,userdata:,verbose"
c892d0dfeda5c022104813bf59275f840923551bCraig McDonnell local getopt_out getopt_ret
c892d0dfeda5c022104813bf59275f840923551bCraig McDonnell getopt_out=$(getopt --name "${0##*/}" \
c892d0dfeda5c022104813bf59275f840923551bCraig McDonnell --options "${short_opts}" --long "${long_opts}" -- "$@" 2>/dev/null) ||
c892d0dfeda5c022104813bf59275f840923551bCraig McDonnell :
c892d0dfeda5c022104813bf59275f840923551bCraig McDonnell getopt_ret=$?
c892d0dfeda5c022104813bf59275f840923551bCraig McDonnell if [ $getopt_ret -eq 0 ]; then
c892d0dfeda5c022104813bf59275f840923551bCraig McDonnell eval set -- "${getopt_out}" ||
c892d0dfeda5c022104813bf59275f840923551bCraig McDonnell { error "Unexpected error reading usage"; return 1; }
c892d0dfeda5c022104813bf59275f840923551bCraig McDonnell fi
c892d0dfeda5c022104813bf59275f840923551bCraig McDonnell
c892d0dfeda5c022104813bf59275f840923551bCraig McDonnell local cur="" next=""
c892d0dfeda5c022104813bf59275f840923551bCraig McDonnell local userdata="" hostid="" authkey="" locales=1 cloud=0 name=""
c892d0dfeda5c022104813bf59275f840923551bCraig McDonnell while [ $# -ne 0 ]; do
9e20026ee2defa33ee5175d93139414793419181David Luna cur="$1"; next="$2";
9e20026ee2defa33ee5175d93139414793419181David Luna case "$cur" in
9e20026ee2defa33ee5175d93139414793419181David Luna -C|--cloud) cloud=1;;
2d8eba3efb0ced5c2ab004e5b3e949094747aeb3jenkins -h|--help) prep_usage; return 0;;
9e20026ee2defa33ee5175d93139414793419181David Luna --name) name="$next";;
c892d0dfeda5c022104813bf59275f840923551bCraig McDonnell -i|--hostid) hostid="$next";;
c892d0dfeda5c022104813bf59275f840923551bCraig McDonnell -L|--nolocales) locales=0;;
fba815ec883afa7d9675096f5266e43b224bbec4Peter Major -S|--auth-key)
[ -f "$next" ] ||
{ error "--auth-key: '$next' not a file"; return 1; }
authkey="$next";;
-u|--userdata)
[ -f "$next" ] ||
{ error "--userdata: '$next' not a file"; return 1; }
userdata="$next";;
-v|--verbose) VERBOSITY=$((${VERBOSITY}+1));;
--) shift; break;;
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
local seed_d=""
seed_d="$root_d/var/lib/cloud/seed/nocloud-net"
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; }
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
{
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"
[ $? -eq 0 ] || {
error "failed to write user-data write to '$seed_d/user-data'";
return 1;
}
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"
shift 3
debug 1 prep "--name=$name" "$LXC_ROOTFS_MOUNT" "$@"
prep "--name=$name" "$LXC_ROOTFS_MOUNT" "$@"
else
_LXC_HOOK=""
prep "$@"
fi
return $?
}
main "$@"
# vi: ts=4 expandtab