aboutsummaryrefslogtreecommitdiffstats
path: root/main/lxc/setup-lxc-template
blob: f8e9fe4a3967161eb8384205a2ed9877259a4a62 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#!/bin/sh

# create a lxc template
PROG=$0

usage() {
	echo "usage: $PROG [-fhq] [-o OUTFILE] [-X repository] [packages...]"
	exit $1
}

clean_exit() {
	rm -rf "$tmpdir"
	exit $1
}

outfile=template.tar.gz
fakeroot=
repos=
while getopts "ho:qX:" opt; do
	case "$opt" in
	h) usage 0;;
	o) outfile="$OPTARG";;
	q) quiet=-q;;
	X) repos="$repos --repository $OPTARG";;
	esac
done
shift $(( $OPTIND - 1 ))

if [ "$(whoami)" != "root" ]; then
	echo "Warning: you need root permissions" >&2
fi

tmpdir=$(mktemp -d ${TMPDIR:-/tmp}/setup-lxc-template-XXXXXX)

apk add --root "$tmpdir" --initdb --keys-dir /etc/apk/keys -U \
	$quiet ${repos:---repositories-file /etc/apk/repositories} \
	alpine-base $@ \
	|| clean_exit 1

tar -czf "$outfile" -C "$tmpdir" $(ls "$tmpdir") || clean_exit 1

[ -z "$quiet" ] && echo "Created $outfile"
clean_exit 0