]>
git.proxmox.com Git - mirror_lxc.git/blob - hooks/ubuntu-cloud-prep
2 ## If the container being cloned has one or more lxc.hook.clone
3 ## specified, then the specified hooks will be called for the new
4 ## container. The arguments passed to the clone hook are:
5 ## 1. the container name
6 ## 2. a section ('lxc')
7 ## 3. hook type ('clone')
8 ## 4. .. additional arguments to lxc-clone
9 ## Environment variables:
10 ## LXC_ROOTFS_MOUNT: path to the root filesystem
11 ## LXC_CONFIG_FILE: path to config file
12 ## LXC_SRC_NAME: old container name
13 ## LXC_ROOTFS_PATH: path or device on which the root fs is located
18 error
() { echo "$@" 1>&2; }
19 debug
() { [ "$VERBOSITY" -ge "$1" ] ||
return 0; shift; error
"$@"; }
20 fail
() { [ $# -eq 0 ] || error
"$@"; exit 1; }
24 Usage: ${0##*/} [options] root-dir
26 root-dir is the root directory to operate on
28 [ -C | --cloud ]: do not configure a datasource. incompatible with
30 [ -i | --instance-id]: instance-id for cloud-init, defaults to random [ds]
31 [ -L | --nolocales ]: Do not copy host's locales into container
32 [ -S | --auth-key ]: ssh public key file for datasource [ds]
33 [ -u | --userdata ]: user-data file for cloud-init [ds]
34 [ -V | --vendordata ]: vendor-data file for cloud-init [ds]
40 local short_opts
="Chi:L:S:u:v"
41 local long_opts
="auth-key:,cloud,help,hostid:,name:,nolocales:,create-etc-init,userdata:,vendordata:,verbose"
42 local getopt_out getopt_ret
43 getopt_out
=$
(getopt
--name "${0##*/}" \
44 --options "${short_opts}" --long "${long_opts}" -- "$@" 2>/dev
/null
) ||
47 if [ $getopt_ret -eq 0 ]; then
48 eval set -- "${getopt_out}" ||
49 { error
"Unexpected error reading usage"; return 1; }
53 local vendordata
="" userdata
="" hostid
="" authkey
="" locales
=1 cloud
=0
54 local create_etc_init
=0 name
="ubuntucloud-lxc"
56 while [ $# -ne 0 ]; do
60 -h|
--help) prep_usage
; return 0;;
61 --name) name
="$next";;
62 -i|
--hostid) hostid
="$next";;
63 -L|
--nolocales) locales
=0;;
64 --create-etc-init) create_etc_init
=1;;
67 { error
"--auth-key: '$next' not a file"; return 1; }
71 { error
"--vendordata: '$next' not a file"; return 1; }
75 { error
"--userdata: '$next' not a file"; return 1; }
77 -v|
--verbose) VERBOSITY
=$
((${VERBOSITY}+1));;
85 error
"expected 1 arguments, got ($_LXC_HOOK) $#: $*";
91 if [ $getopt_ret -ne 0 -a "$_LXC_HOOK" = "clone" ]; then
92 # getopt above failed, but we were called from lxc clone. there might
93 # be multiple clone hooks and the args provided here not for us. This
94 # seems like not the greatest interface, so all we'll do is mention it.
95 error
"${0##*}: usage failed, continuing with defaults"
98 [ "$create_etc_init" -eq 0 ] ||
99 echo "#upstart needs help for overlayfs (LP: #1213925)." > \
100 "$root_d/etc/init/.overlayfs-upstart-helper" ||
101 { error
"failed to create /etc/init in overlay"; return 1; }
104 seed_d
="$root_d/var/lib/cloud/seed/nocloud-net"
106 echo "$name" > "$root_d/etc/hostname" ||
107 { error
"failed to write /etc/hostname"; return 1; }
109 if [ $cloud -eq 1 ]; then
110 debug
1 "--cloud provided, not modifying seed in '$seed_d'"
112 if [ -z "$hostid" ]; then
113 hostid
=$
(uuidgen | cut
-c -8) && [ -n "$hostid" ] ||
114 { error
"failed to get hostid"; return 1; }
116 mkdir
-p "$seed_d" ||
117 { error
"failed to create '$seed_d'"; return 1; }
119 echo "instance-id: lxc-$hostid" > "$seed_d/meta-data" ||
120 { error
"failed to write to $seed_d/meta-data"; return 1; }
122 echo "local-hostname: $name" >> "$seed_d/meta-data" ||
123 { error
"failed to write to $seed_d/meta-data"; return 1; }
125 if [ -n "$authkey" ]; then
127 echo "public-keys:" &&
128 sed -e '/^$/d' -e 's,^,- ,' "$authkey"
129 } >> "$seed_d/meta-data"
131 { error
"failed to write public keys to metadata"; return 1; }
134 local larch
="usr/lib/locale/locale-archive"
135 if [ $locales -eq 1 ]; then
136 cp "/$larch" "$root_d/$larch" ||
{
137 error
"failed to cp '/$larch' '$root_d/$larch'";
142 if [ -z "$MIRROR" ]; then
143 MIRROR
="http://archive.ubuntu.com/ubuntu"
146 if [ -n "$userdata" ]; then
147 cp "$userdata" "$seed_d/user-data"
150 local lc
=$
(locale |
awk -F= '/LANG=/ {print $NF; }')
152 echo "output: {all: '| tee -a /var/log/cloud-init-output.log'}"
153 echo "apt_mirror: $MIRROR"
154 echo "manage_etc_hosts: localhost"
155 [ -z "$LANG" ] ||
echo "locale: $LANG";
156 echo "password: ubuntu"
157 echo "chpasswd: { expire: false; }"
158 } > "$seed_d/user-data"
161 error
"failed to write user-data write to '$seed_d/user-data'";
165 if [ -n "$vendordata" ]; then
166 cp "$vendordata" "$seed_d/vendor-data" ||
{
167 error
"failed copy vendordata to $seed_d/vendor-data";
176 # main just joins 2 modes of being called. from user one from lxc clone
178 if [ -n "$LXC_ROOTFS_MOUNT" -a "$3" = "clone" ]; then
180 local name
="$1" create_etc_init
=""
182 # if mountpoint is overlayfs then add '--create-etc-init'
183 [ "${LXC_ROOTFS_PATH#overlayfs}" != "${LXC_ROOTFS_PATH}" ] &&
184 create_etc_init
="--create-etc-init"
185 debug
1 prep
"--name=$name" $create_etc_init "$LXC_ROOTFS_MOUNT" "$@"
186 prep
"--name=$name" $create_etc_init "$LXC_ROOTFS_MOUNT" "$@"