]> git.proxmox.com Git - mirror_lxc.git/blob - hooks/ubuntu-cloud-prep
hooks: dhclient hook improvements
[mirror_lxc.git] / hooks / ubuntu-cloud-prep
1 #!/bin/bash
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
14
15 set -f
16 VERBOSITY="0"
17
18 error() { echo "$@" 1>&2; }
19 debug() { [ "$VERBOSITY" -ge "$1" ] || return 0; shift; error "$@"; }
20 fail() { [ $# -eq 0 ] || error "$@"; exit 1; }
21
22 prep_usage() {
23 cat <<EOF
24 Usage: ${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 [ -V | --vendordata ]: vendor-data file for cloud-init [ds]
35
36 EOF
37 }
38
39 prep() {
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) ||
45 :
46 getopt_ret=$?
47 if [ $getopt_ret -eq 0 ]; then
48 eval set -- "${getopt_out}" ||
49 { error "Unexpected error reading usage"; return 1; }
50 fi
51
52 local cur="" next=""
53 local vendordata="" userdata="" hostid="" authkey="" locales=1 cloud=0
54 local create_etc_init=0 name="ubuntucloud-lxc"
55
56 while [ $# -ne 0 ]; do
57 cur="$1"; next="$2";
58 case "$cur" in
59 -C|--cloud) cloud=1;;
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;;
65 -S|--auth-key)
66 [ -f "$next" ] ||
67 { error "--auth-key: '$next' not a file"; return 1; }
68 authkey="$next";;
69 -V|--vendordata)
70 [ -f "$next" ] ||
71 { error "--vendordata: '$next' not a file"; return 1; }
72 vendordata="$next";;
73 -u|--userdata)
74 [ -f "$next" ] ||
75 { error "--userdata: '$next' not a file"; return 1; }
76 userdata="$next";;
77 -v|--verbose) VERBOSITY=$((${VERBOSITY}+1));;
78 --) shift; break;;
79 esac
80 shift;
81 done
82
83 [ $# -eq 1 ] || {
84 prep_usage 1>&2;
85 error "expected 1 arguments, got ($_LXC_HOOK) $#: $*";
86 return 1;
87 }
88
89 local root_d="$1";
90
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"
96 fi
97
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; }
102
103 local seed_d=""
104 seed_d="$root_d/var/lib/cloud/seed/nocloud-net"
105
106 echo "$name" > "$root_d/etc/hostname" ||
107 { error "failed to write /etc/hostname"; return 1; }
108
109 if [ $cloud -eq 1 ]; then
110 debug 1 "--cloud provided, not modifying seed in '$seed_d'"
111 else
112 if [ -z "$hostid" ]; then
113 hostid=$(uuidgen | cut -c -8) && [ -n "$hostid" ] ||
114 { error "failed to get hostid"; return 1; }
115 fi
116 mkdir -p "$seed_d" ||
117 { error "failed to create '$seed_d'"; return 1; }
118
119 echo "instance-id: lxc-$hostid" > "$seed_d/meta-data" ||
120 { error "failed to write to $seed_d/meta-data"; return 1; }
121
122 echo "local-hostname: $name" >> "$seed_d/meta-data" ||
123 { error "failed to write to $seed_d/meta-data"; return 1; }
124
125 if [ -n "$authkey" ]; then
126 {
127 echo "public-keys:" &&
128 sed -e '/^$/d' -e 's,^,- ,' "$authkey"
129 } >> "$seed_d/meta-data"
130 [ $? -eq 0 ] ||
131 { error "failed to write public keys to metadata"; return 1; }
132 fi
133
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'";
138 return 1;
139 }
140 fi
141
142 if [ -z "$MIRROR" ]; then
143 MIRROR="http://archive.ubuntu.com/ubuntu"
144 fi
145
146 if [ -n "$userdata" ]; then
147 cp "$userdata" "$seed_d/user-data"
148 else
149 {
150 local lc=$(locale | awk -F= '/LANG=/ {print $NF; }')
151 echo "#cloud-config"
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"
159 fi
160 [ $? -eq 0 ] || {
161 error "failed to write user-data write to '$seed_d/user-data'";
162 return 1;
163 }
164
165 if [ -n "$vendordata" ]; then
166 cp "$vendordata" "$seed_d/vendor-data" || {
167 error "failed copy vendordata to $seed_d/vendor-data";
168 return 1;
169 }
170 fi
171 fi
172
173 }
174
175 main() {
176 # main just joins 2 modes of being called. from user one from lxc clone
177 local _LXC_HOOK
178 if [ -n "$LXC_ROOTFS_MOUNT" -a "$3" = "clone" ]; then
179 _LXC_HOOK="clone"
180 local name="$1" create_etc_init=""
181 shift 3
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" "$@"
187 else
188 _LXC_HOOK=""
189 prep "$@"
190 fi
191 return $?
192 }
193
194 main "$@"
195
196 # vi: ts=4 expandtab