| 1 |
#!/bin/bash |
| 2 |
# |
| 3 |
# SPDX-License-Identifier: LGPL-2.1+ |
| 4 |
# |
| 5 |
## If the container being cloned has one or more lxc.hook.clone |
| 6 |
## specified, then the specified hooks will be called for the new |
| 7 |
## container. The arguments passed to the clone hook are: |
| 8 |
## 1. the container name |
| 9 |
## 2. a section ('lxc') |
| 10 |
## 3. hook type ('clone') |
| 11 |
## 4. .. additional arguments to lxc-clone |
| 12 |
## Environment variables: |
| 13 |
## LXC_ROOTFS_MOUNT: path to the root filesystem |
| 14 |
## LXC_CONFIG_FILE: path to config file |
| 15 |
## LXC_SRC_NAME: old container name |
| 16 |
## LXC_ROOTFS_PATH: path or device on which the root fs is located |
| 17 |
|
| 18 |
set -f |
| 19 |
VERBOSITY="0" |
| 20 |
|
| 21 |
error() { echo "$@" 1>&2; } |
| 22 |
debug() { [ "$VERBOSITY" -ge "$1" ] || return 0; shift; error "$@"; } |
| 23 |
fail() { [ $# -eq 0 ] || error "$@"; exit 1; } |
| 24 |
|
| 25 |
prep_usage() { |
| 26 |
cat <<EOF |
| 27 |
Usage: ${0##*/} [options] root-dir |
| 28 |
|
| 29 |
root-dir is the root directory to operate on |
| 30 |
|
| 31 |
[ -C | --cloud ]: do not configure a datasource. incompatible with |
| 32 |
options marked '[ds]' |
| 33 |
[ -i | --instance-id]: instance-id for cloud-init, defaults to random [ds] |
| 34 |
[ -L | --nolocales ]: Do not copy host's locales into container |
| 35 |
[ -S | --auth-key ]: ssh public key file for datasource [ds] |
| 36 |
[ -u | --userdata ]: user-data file for cloud-init [ds] |
| 37 |
[ -V | --vendordata ]: vendor-data file for cloud-init [ds] |
| 38 |
|
| 39 |
EOF |
| 40 |
} |
| 41 |
|
| 42 |
prep() { |
| 43 |
local short_opts="Chi:L:S:u:v" |
| 44 |
local long_opts="auth-key:,cloud,help,hostid:,name:,nolocales:,create-etc-init,userdata:,vendordata:,verbose" |
| 45 |
local getopt_out getopt_ret |
| 46 |
getopt_out=$(getopt --name "${0##*/}" \ |
| 47 |
--options "${short_opts}" --long "${long_opts}" -- "$@" 2>/dev/null) || |
| 48 |
: |
| 49 |
getopt_ret=$? |
| 50 |
if [ $getopt_ret -eq 0 ]; then |
| 51 |
eval set -- "${getopt_out}" || |
| 52 |
{ error "Unexpected error reading usage"; return 1; } |
| 53 |
fi |
| 54 |
|
| 55 |
local cur="" next="" |
| 56 |
local vendordata="" userdata="" hostid="" authkey="" locales=1 cloud=0 |
| 57 |
local create_etc_init=0 name="ubuntucloud-lxc" |
| 58 |
|
| 59 |
while [ $# -ne 0 ]; do |
| 60 |
cur="$1"; next="$2"; |
| 61 |
case "$cur" in |
| 62 |
-C|--cloud) cloud=1;; |
| 63 |
-h|--help) prep_usage; return 0;; |
| 64 |
--name) name="$next";; |
| 65 |
-i|--hostid) hostid="$next";; |
| 66 |
-L|--nolocales) locales=0;; |
| 67 |
--create-etc-init) create_etc_init=1;; |
| 68 |
-S|--auth-key) |
| 69 |
[ -f "$next" ] || |
| 70 |
{ error "--auth-key: '$next' not a file"; return 1; } |
| 71 |
authkey="$next";; |
| 72 |
-V|--vendordata) |
| 73 |
[ -f "$next" ] || |
| 74 |
{ error "--vendordata: '$next' not a file"; return 1; } |
| 75 |
vendordata="$next";; |
| 76 |
-u|--userdata) |
| 77 |
[ -f "$next" ] || |
| 78 |
{ error "--userdata: '$next' not a file"; return 1; } |
| 79 |
userdata="$next";; |
| 80 |
-v|--verbose) VERBOSITY=$((${VERBOSITY}+1));; |
| 81 |
--) shift; break;; |
| 82 |
esac |
| 83 |
shift; |
| 84 |
done |
| 85 |
|
| 86 |
[ $# -eq 1 ] || { |
| 87 |
prep_usage 1>&2; |
| 88 |
error "expected 1 arguments, got ($_LXC_HOOK) $#: $*"; |
| 89 |
return 1; |
| 90 |
} |
| 91 |
|
| 92 |
local root_d="$1"; |
| 93 |
|
| 94 |
if [ $getopt_ret -ne 0 -a "$_LXC_HOOK" = "clone" ]; then |
| 95 |
# getopt above failed, but we were called from lxc clone. there might |
| 96 |
# be multiple clone hooks and the args provided here not for us. This |
| 97 |
# seems like not the greatest interface, so all we'll do is mention it. |
| 98 |
error "${0##*}: usage failed, continuing with defaults" |
| 99 |
fi |
| 100 |
|
| 101 |
local seed_d="" |
| 102 |
seed_d="$root_d/var/lib/cloud/seed/nocloud-net" |
| 103 |
|
| 104 |
echo "$name" > "$root_d/etc/hostname" || |
| 105 |
{ error "failed to write /etc/hostname"; return 1; } |
| 106 |
|
| 107 |
if [ $cloud -eq 1 ]; then |
| 108 |
debug 1 "--cloud provided, not modifying seed in '$seed_d'" |
| 109 |
else |
| 110 |
if [ -z "$hostid" ]; then |
| 111 |
hostid=$(uuidgen | cut -c -8) && [ -n "$hostid" ] || |
| 112 |
{ error "failed to get hostid"; return 1; } |
| 113 |
fi |
| 114 |
mkdir -p "$seed_d" || |
| 115 |
{ error "failed to create '$seed_d'"; return 1; } |
| 116 |
|
| 117 |
echo "instance-id: lxc-$hostid" > "$seed_d/meta-data" || |
| 118 |
{ error "failed to write to $seed_d/meta-data"; return 1; } |
| 119 |
|
| 120 |
echo "local-hostname: $name" >> "$seed_d/meta-data" || |
| 121 |
{ error "failed to write to $seed_d/meta-data"; return 1; } |
| 122 |
|
| 123 |
if [ -n "$authkey" ]; then |
| 124 |
{ |
| 125 |
echo "public-keys:" && |
| 126 |
sed -e '/^$/d' -e 's,^,- ,' "$authkey" |
| 127 |
} >> "$seed_d/meta-data" |
| 128 |
[ $? -eq 0 ] || |
| 129 |
{ error "failed to write public keys to metadata"; return 1; } |
| 130 |
fi |
| 131 |
|
| 132 |
local larch="usr/lib/locale/locale-archive" |
| 133 |
if [ $locales -eq 1 ]; then |
| 134 |
cp "/$larch" "$root_d/$larch" || { |
| 135 |
error "failed to cp '/$larch' '$root_d/$larch'"; |
| 136 |
return 1; |
| 137 |
} |
| 138 |
fi |
| 139 |
|
| 140 |
if [ -z "$MIRROR" ]; then |
| 141 |
MIRROR="http://archive.ubuntu.com/ubuntu" |
| 142 |
fi |
| 143 |
|
| 144 |
if [ -n "$userdata" ]; then |
| 145 |
cp "$userdata" "$seed_d/user-data" |
| 146 |
else |
| 147 |
{ |
| 148 |
local lc=$(locale | awk -F= '/LANG=/ {print $NF; }') |
| 149 |
echo "#cloud-config" |
| 150 |
echo "output: {all: '| tee -a /var/log/cloud-init-output.log'}" |
| 151 |
echo "apt_mirror: $MIRROR" |
| 152 |
echo "manage_etc_hosts: localhost" |
| 153 |
[ -z "$LANG" ] || echo "locale: $LANG"; |
| 154 |
echo "password: ubuntu" |
| 155 |
echo "chpasswd: { expire: false; }" |
| 156 |
} > "$seed_d/user-data" |
| 157 |
fi |
| 158 |
[ $? -eq 0 ] || { |
| 159 |
error "failed to write user-data write to '$seed_d/user-data'"; |
| 160 |
return 1; |
| 161 |
} |
| 162 |
|
| 163 |
if [ -n "$vendordata" ]; then |
| 164 |
cp "$vendordata" "$seed_d/vendor-data" || { |
| 165 |
error "failed copy vendordata to $seed_d/vendor-data"; |
| 166 |
return 1; |
| 167 |
} |
| 168 |
fi |
| 169 |
fi |
| 170 |
|
| 171 |
} |
| 172 |
|
| 173 |
main() { |
| 174 |
# main just joins 2 modes of being called. from user one from lxc clone |
| 175 |
local _LXC_HOOK |
| 176 |
if [ -n "$LXC_ROOTFS_MOUNT" -a "$3" = "clone" ]; then |
| 177 |
_LXC_HOOK="clone" |
| 178 |
local name="$1" create_etc_init="" |
| 179 |
shift 3 |
| 180 |
# if mountpoint is overlayfs then add '--create-etc-init' |
| 181 |
[ "${LXC_ROOTFS_PATH#overlayfs}" != "${LXC_ROOTFS_PATH}" ] && |
| 182 |
create_etc_init="--create-etc-init" |
| 183 |
debug 1 prep "--name=$name" $create_etc_init "$LXC_ROOTFS_MOUNT" "$@" |
| 184 |
prep "--name=$name" $create_etc_init "$LXC_ROOTFS_MOUNT" "$@" |
| 185 |
else |
| 186 |
_LXC_HOOK="" |
| 187 |
prep "$@" |
| 188 |
fi |
| 189 |
return $? |
| 190 |
} |
| 191 |
|
| 192 |
main "$@" |
| 193 |
|
| 194 |
# vi: ts=4 expandtab |