]> git.proxmox.com Git - mirror_lxc.git/blame - hooks/ubuntu-cloud-prep
api: convert lxc_wait, lxc_freeze, and lxc_unfreeze
[mirror_lxc.git] / hooks / ubuntu-cloud-prep
CommitLineData
65d8ae9c 1#!/bin/bash
79159a86
SM
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:
65d8ae9c
SM
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:
79159a86
SM
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
65d8ae9c 14
d24d56d7 15set -f
54e339f9 16VERBOSITY="0"
65d8ae9c
SM
17
18error() { echo "$@" 1>&2; }
79159a86 19debug() { [ "$VERBOSITY" -ge "$1" ] || return; shift; error "$@"; }
65d8ae9c
SM
20fail() { [ $# -eq 0 ] || error "$@"; exit 1; }
21
22prep_usage() {
23cat <<EOF
24Usage: ${0##*/} [options] root-dir
25
26 root-dir is the root directory to operate on
27
28 [ -C | --cloud ]: do not configure a datasource. incompatible with
29 options marked '[ds]'
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
35EOF
36}
37
38prep() {
39 local short_opts="Chi:L:S:u:v"
fb760f70 40 local long_opts="auth-key:,cloud,help,hostid:,name:,nolocales:,create-etc-init,userdata:,verbose"
65d8ae9c
SM
41 local getopt_out getopt_ret
42 getopt_out=$(getopt --name "${0##*/}" \
43 --options "${short_opts}" --long "${long_opts}" -- "$@" 2>/dev/null) ||
44 :
45 getopt_ret=$?
46 if [ $getopt_ret -eq 0 ]; then
47 eval set -- "${getopt_out}" ||
48 { error "Unexpected error reading usage"; return 1; }
49 fi
50
51 local cur="" next=""
52 local userdata="" hostid="" authkey="" locales=1 cloud=0 name=""
fb760f70 53 local create_etc_init=0
d24d56d7 54
65d8ae9c
SM
55 while [ $# -ne 0 ]; do
56 cur="$1"; next="$2";
57 case "$cur" in
58 -C|--cloud) cloud=1;;
59 -h|--help) prep_usage; return 0;;
60 --name) name="$next";;
61 -i|--hostid) hostid="$next";;
62 -L|--nolocales) locales=0;;
fb760f70 63 --create-etc-init) create_etc_init=1;;
65d8ae9c
SM
64 -S|--auth-key)
65 [ -f "$next" ] ||
66 { error "--auth-key: '$next' not a file"; return 1; }
67 authkey="$next";;
68 -u|--userdata)
69 [ -f "$next" ] ||
70 { error "--userdata: '$next' not a file"; return 1; }
71 userdata="$next";;
72 -v|--verbose) VERBOSITY=$((${VERBOSITY}+1));;
73 --) shift; break;;
74 esac
75 shift;
76 done
77
78 [ $# -eq 1 ] || {
79 prep_usage 1>&2;
80 error "expected 1 arguments, got ($_LXC_HOOK) $#: $*";
81 return 1;
82 }
83
84 local root_d="$1";
85
86 if [ $getopt_ret -ne 0 -a "$_LXC_HOOK" = "clone" ]; then
87 # getopt above failed, but we were called from lxc clone. there might
88 # be multiple clone hooks and the args provided here not for us. This
89 # seems like not the greatest interface, so all we'll do is mention it.
90 error "${0##*}: usage failed, continuing with defaults"
91 fi
92
fb760f70
SM
93 [ "$create_etc_init" -eq 0 ] ||
94 echo "#upstart needs help for overlayfs (LP: #1213925)." > \
95 "$root_d/etc/init/.overlayfs-upstart-helper" ||
96 { error "failed to create /etc/init in overlay"; return 1; }
d24d56d7 97
65d8ae9c
SM
98 local seed_d=""
99 seed_d="$root_d/var/lib/cloud/seed/nocloud-net"
100 if [ $cloud -eq 1 ]; then
101 debug 1 "--cloud provided, not modifying seed in '$seed_d'"
102 else
103 if [ -z "$hostid" ]; then
104 hostid=$(uuidgen | cut -c -8) && [ -n "$hostid" ] ||
105 { error "failed to get hostid"; return 1; }
106 fi
107 mkdir -p "$seed_d" ||
108 { error "failed to create '$seed_d'"; return 1; }
109
110 echo "instance-id: lxc-$hostid" > "$seed_d/meta-data" ||
111 { error "failed to write to $seed_d/meta-data"; return 1; }
112
113 if [ -n "$authkey" ]; then
114 {
115 echo "public-keys:" &&
116 sed -e '/^$/d' -e 's,^,- ,' "$authkey"
117 } >> "$seed_d/meta-data"
118 [ $? -eq 0 ] ||
119 { error "failed to write public keys to metadata"; return 1; }
120 fi
121
122 local larch="usr/lib/locale/locale-archive"
123 if [ $locales -eq 1 ]; then
124 cp "/$larch" "$root_d/$larch" || {
125 error "failed to cp '/$larch' '$root_d/$larch'";
126 return 1;
127 }
128 fi
129
130 if [ -z "$MIRROR" ]; then
131 MIRROR="http://archive.ubuntu.com/ubuntu"
132 fi
133
79159a86
SM
134 if [ -n "$userdata" ]; then
135 cp "$userdata" "$seed_d/user-data"
136 else
137 {
65d8ae9c
SM
138 local lc=$(locale | awk -F= '/LANG=/ {print $NF; }')
139 echo "#cloud-config"
140 echo "output: {all: '| tee -a /var/log/cloud-init-output.log'}"
141 echo "apt_mirror: $MIRROR"
142 echo "manage_etc_hosts: localhost"
143 [ -z "$LANG" ] || echo "locale: $LANG";
144 echo "password: ubuntu"
145 echo "chpasswd: { expire: false; }"
79159a86
SM
146 } > "$seed_d/user-data"
147 fi
65d8ae9c
SM
148 [ $? -eq 0 ] || {
149 error "failed to write user-data write to '$seed_d/user-data'";
150 return 1;
151 }
152 fi
153
154}
155
156main() {
157 # main just joins 2 modes of being called. from user one from lxc clone
158 local _LXC_HOOK
159 if [ -n "$LXC_ROOTFS_MOUNT" -a "$3" = "clone" ]; then
160 _LXC_HOOK="clone"
fb760f70 161 local name="$1" create_etc_init=""
65d8ae9c 162 shift 3
fb760f70 163 # if mountpoint is overlayfs then add '--create-etc-init'
d24d56d7 164 [ "${LXC_ROOTFS_PATH#overlayfs}" != "${LXC_ROOTFS_PATH}" ] &&
fb760f70
SM
165 create_etc_init="--create-etc-init"
166 debug 1 prep "--name=$name" $create_etc_init "$LXC_ROOTFS_MOUNT" "$@"
167 prep "--name=$name" $create_etc_init "$LXC_ROOTFS_MOUNT" "$@"
65d8ae9c
SM
168 else
169 _LXC_HOOK=""
170 prep "$@"
171 fi
172 return $?
173}
174
175main "$@"
176
177# vi: ts=4 expandtab