]> git.proxmox.com Git - mirror_lxc.git/blob - templates/lxc-alpine.in
lxc-alpine: make --release work when apk exists
[mirror_lxc.git] / templates / lxc-alpine.in
1 #!/bin/sh
2
3 key_sha256sums="9c102bcc376af1498d549b77bdbfa815ae86faa1d2d82f040e616b18ef2df2d4 alpine-devel@lists.alpinelinux.org-4a6a0840.rsa.pub
4 2adcf7ce224f476330b5360ca5edb92fd0bf91c92d83292ed028d7c4e26333ab alpine-devel@lists.alpinelinux.org-4d07755e.rsa.pub"
5
6 get_static_apk () {
7 wget="wget -q -O -"
8 pkglist=alpine-keys:apk-tools-static
9 auto_repo_dir=
10
11 if [ -z "$repository" ]; then
12 url=http://wiki.alpinelinux.org/cgi-bin/dl.cgi
13 if [ -z "$release" ]; then
14 echo -n "Determining the latest release... "
15 release=$($wget $url/.latest.$apk_arch.txt | \
16 cut -d " " -f 3 | cut -d / -f 1 | uniq)
17 if [ -z "$release" ]; then
18 echo failed
19 return 1
20 fi
21 echo $release
22 fi
23 auto_repo_dir=$release/main
24 repository=$url/$auto_repo_dir
25 pkglist=$pkglist:alpine-mirrors
26 fi
27
28 rootfs="$1"
29 echo "Using static apk from $repository/$apk_arch"
30 wget="$wget $repository/$apk_arch"
31
32 # parse APKINDEX to find the current versions
33 static_pkgs=$($wget/APKINDEX.tar.gz | \
34 tar -Oxz APKINDEX | \
35 awk -F: -v pkglist=$pkglist '
36 BEGIN { split(pkglist,pkg) }
37 $0 != "" { f[$1] = $2 }
38 $0 == "" { for (i in pkg)
39 if (pkg[i] == f["P"])
40 print(f["P"] "-" f["V"] ".apk") }')
41 [ "$static_pkgs" ] || return 1
42
43 mkdir -p "$rootfs" || return 1
44 for pkg in $static_pkgs; do
45 echo "Downloading $pkg"
46 $wget/$pkg | tar -xz -C "$rootfs"
47 done
48
49 # clean up .apk meta files
50 rm -f "$rootfs"/.[A-Z]*
51
52 # verify checksum of the key
53 keyname=$(echo $rootfs/sbin/apk.static.*.pub | sed 's/.*\.SIGN\.RSA\.//')
54 checksum=$(echo "$key_sha256sums" | grep -w "$keyname")
55 if [ -z "$checksum" ]; then
56 echo "ERROR: checksum is missing for $keyname"
57 return 1
58 fi
59 (cd $rootfs/etc/apk/keys && echo "$checksum" | sha256sum -c -) || return 1
60
61 # verify the static apk binary signature
62 APK=$rootfs/sbin/apk.static
63 openssl dgst -verify $rootfs/etc/apk/keys/$keyname \
64 -signature "$APK.SIGN.RSA.$keyname" "$APK" || return 1
65
66 if [ "$auto_repo_dir" ]; then
67 mirror_list=$rootfs/usr/share/alpine-mirrors/MIRRORS.txt
68 mirror_count=$(wc -l $mirror_list | cut -d " " -f 1)
69 repository=$(sed $(expr $RANDOM % $mirror_count + 1)\!d \
70 $mirror_list)$auto_repo_dir
71 echo "Selecting mirror $repository"
72 fi
73 }
74
75 install_alpine() {
76 rootfs="$1"
77 shift
78 mkdir -p "$rootfs"/etc/apk || return 1
79 : ${keys_dir:=/etc/apk/keys}
80 if ! [ -d "$rootfs"/etc/apk/keys ] && [ -d "$keys_dir" ]; then
81 cp -r "$keys_dir" "$rootfs"/etc/apk/keys
82 fi
83 if [ -n "$repository" ]; then
84 echo "$repository" > "$rootfs"/etc/apk/repositories
85 else
86 cp /etc/apk/repositories "$rootfs"/etc/apk/repositories || return 1
87 if [ -n "$release" ]; then
88 sed -i -e "s:/[^/]\+/\([^/]\+\)$:/$release/\1:" \
89 "$rootfs"/etc/apk/repositories
90 fi
91 fi
92 opt_arch=
93 if [ -n "$apk_arch" ]; then
94 opt_arch="--arch $apk_arch"
95 fi
96 $APK add -U --initdb --root $rootfs $opt_arch "$@" alpine-base
97 }
98
99 configure_alpine() {
100 rootfs="$1"
101 echo "Setting up /etc/inittab"
102 cat >"$rootfs"/etc/inittab<<EOF
103 ::sysinit:/sbin/rc sysinit
104 ::wait:/sbin/rc default
105 tty1:12345:respawn:/sbin/getty 38400 tty1
106 ::ctrlaltdel:/sbin/reboot
107 ::shutdown:/sbin/rc shutdown
108 EOF
109 # set up nameserver
110 grep nameserver /etc/resolv.conf > "$rootfs/etc/resolv.conf"
111
112 # configure the network using the dhcp
113 # note that lxc will set up lo interface
114 cat <<EOF > $rootfs/etc/network/interfaces
115 #auto lo
116 iface lo inet loopback
117
118 auto eth0
119 iface eth0 inet dhcp
120 EOF
121
122 # set the hostname
123 echo $hostname > $rootfs/etc/hostname
124
125 # missing device nodes
126 echo "Setting up device nodes"
127 mkdir -p -m 755 "$rootfs/dev/pts"
128 mkdir -p -m 1777 "$rootfs/dev/shm"
129 mknod -m 666 "$rootfs/dev/full" c 1 7
130 mknod -m 666 "$rootfs/dev/random" c 1 8
131 mknod -m 666 "$rootfs/dev/urandom" c 1 9
132 mknod -m 666 "$rootfs/dev/tty0" c 4 0
133 mknod -m 666 "$rootfs/dev/tty1" c 4 1
134 mknod -m 666 "$rootfs/dev/tty2" c 4 2
135 mknod -m 666 "$rootfs/dev/tty3" c 4 3
136 mknod -m 666 "$rootfs/dev/tty4" c 4 4
137 # mknod -m 600 "$rootfs/dev/initctl" p
138 mknod -m 666 "$rootfs/dev/tty" c 5 0
139 mknod -m 666 "$rootfs/dev/console" c 5 1
140 mknod -m 666 "$rootfs/dev/ptmx" c 5 2
141
142 # start services
143 ln -s /etc/init.d/syslog "$rootfs"/etc/runlevels/default/syslog
144
145 return 0
146 }
147
148 copy_configuration() {
149 path=$1
150 rootfs=$2
151 hostname=$3
152
153 grep -q "^lxc.rootfs" $path/config 2>/dev/null \
154 || echo "lxc.rootfs = $rootfs" >> $path/config
155 if [ -n "$lxc_arch" ]; then
156 echo "lxc.arch = $lxc_arch" >> $path/config
157 fi
158
159 lxc_network_link_line="# lxc.network.link = br0"
160 for br in lxcbr0 virbr0 br0; do
161 if [ -d /sys/class/net/$br/bridge ]; then
162 lxc_network_link_line="lxc.network.link = $br"
163 break
164 fi
165 done
166
167 if ! grep -q "^lxc.network.type" $path/config 2>/dev/null; then
168 cat <<EOF >> $path/config
169 lxc.network.type = veth
170 $lxc_network_link_line
171 lxc.network.flags = up
172 EOF
173 fi
174
175 # if there is exactly one veth network entry, make sure it has an
176 # associated mac address.
177 nics=$(grep -e '^lxc\.network\.type[ \t]*=[ \t]*veth' $path/config | wc -l)
178 if [ "$nics" -eq 1 ] && ! grep -q "^lxc.network.hwaddr" $path/config; then
179 # see http://sourceforge.net/tracker/?func=detail&aid=3411497&group_id=163076&atid=826303
180 hwaddr="fe:$(dd if=/dev/urandom bs=8 count=1 2>/dev/null |od -t x8 | \
181 head -1 |awk '{print $2}' | cut -c1-10 |\
182 sed 's/\(..\)/\1:/g; s/.$//')"
183 echo "lxc.network.hwaddr = $hwaddr" >> $path/config
184 fi
185
186 cat <<EOF >> $path/config
187
188 lxc.tty = 4
189 lxc.pts = 1024
190 lxc.utsname = $hostname
191 lxc.cap.drop = sys_module mac_admin mac_override sys_time
192
193 # When using LXC with apparmor, uncomment the next line to run unconfined:
194 #lxc.aa_profile = unconfined
195
196 # devices
197 lxc.cgroup.devices.deny = a
198 # /dev/null and zero
199 lxc.cgroup.devices.allow = c 1:3 rwm
200 lxc.cgroup.devices.allow = c 1:5 rwm
201 # consoles
202 lxc.cgroup.devices.allow = c 5:1 rwm
203 lxc.cgroup.devices.allow = c 5:0 rwm
204 lxc.cgroup.devices.allow = c 4:0 rwm
205 lxc.cgroup.devices.allow = c 4:1 rwm
206 # /dev/{,u}random
207 lxc.cgroup.devices.allow = c 1:9 rwm
208 lxc.cgroup.devices.allow = c 1:8 rwm
209 lxc.cgroup.devices.allow = c 136:* rwm
210 lxc.cgroup.devices.allow = c 5:2 rwm
211 # rtc
212 lxc.cgroup.devices.allow = c 254:0 rm
213
214 # mounts point
215 lxc.mount.entry=proc proc proc nodev,noexec,nosuid 0 0
216 lxc.mount.entry=run run tmpfs nodev,noexec,nosuid,relatime,size=1m,mode=0755 0 0
217 lxc.mount.entry=none dev/pts devpts gid=5,mode=620 0 0
218
219 EOF
220
221 return 0
222 }
223
224 die() {
225 echo "$@" >&2
226 exit 1
227 }
228
229 usage() {
230 cat >&2 <<EOF
231 Usage: $(basename $0) [-h|--help] [-r|--repository <url>]
232 [-R|--release <release>] [-a|--arch <arch>]
233 [--rootfs <rootfs>] -p|--path <path> -n|--name <name>
234 [PKG...]
235 EOF
236 }
237
238 usage_err() {
239 usage
240 exit 1
241 }
242
243 optarg_check() {
244 if [ -z "$2" ]; then
245 usage_err "option '$1' requires an argument"
246 fi
247 }
248
249 default_path=@LXCPATH@
250 release=
251 arch=$(uname -m)
252
253 while [ $# -gt 0 ]; do
254 opt="$1"
255 shift
256 case "$opt" in
257 -h|--help)
258 usage
259 exit 0
260 ;;
261 -n|--name)
262 optarg_check $opt "$1"
263 name=$1
264 shift
265 ;;
266 --rootfs)
267 optarg_check $opt "$1"
268 rootfs=$1
269 shift
270 ;;
271 -p|--path)
272 optarg_check $opt "$1"
273 path=$1
274 shift
275 ;;
276 -r|--repository)
277 optarg_check $opt "$1"
278 repository=$1
279 shift
280 ;;
281 -R|--release)
282 optarg_check $opt "$1"
283 release=$1
284 shift
285 ;;
286 -a|--arch)
287 optarg_check $opt "$1"
288 arch=$1
289 shift
290 ;;
291 --)
292 break;;
293 --*=*)
294 # split --myopt=foo=bar into --myopt foo=bar
295 set -- ${opt%=*} ${opt#*=} "$@"
296 ;;
297 -?)
298 usage_err "unknown option '$opt'"
299 ;;
300 -*)
301 # split opts -abc into -a -b -c
302 set -- $(echo "${opt#-}" | sed 's/\(.\)/ -\1/g') "$@"
303 ;;
304 esac
305 done
306
307
308 [ -z "$name" ] && usage_err
309
310 if [ -z "${path}" ]; then
311 path="${default_path}/${name}"
312 fi
313
314 if [ -z "$rootfs" ]; then
315 rootfs=`awk -F= '$1 ~ /^lxc.rootfs/ { print $2 }' "$path/config" 2>/dev/null`
316 if [ -z "$rootfs" ]; then
317 rootfs="${path}/rootfs"
318 fi
319 fi
320
321 lxc_arch=$arch
322 apk_arch=$arch
323
324 case "$arch" in
325 i[3-6]86)
326 apk_arch=x86;;
327 x86)
328 lxc_arch=i686;;
329 x86_64|"") ;;
330 *) die "unsupported architecture: $arch";;
331 esac
332
333 : ${APK:=apk}
334 if ! which $APK >/dev/null; then
335 get_static_apk "$rootfs" || die "Failed to download a valid static apk"
336 fi
337
338 install_alpine "$rootfs" "$@" || die "Failed to install rootfs for $name"
339 configure_alpine "$rootfs" "$name" || die "Failed to configure $name"
340 copy_configuration "$path" "$rootfs" "$name"