genrootfs.sh 1.34 KB
Newer Older
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
#!/bin/sh -e

cleanup() {
	rm -rf "$tmp"
}

tmp="$(mktemp -d)"
trap cleanup EXIT

arch="$(apk --print-arch)"
repositories_file=/etc/apk/repositories
keys_dir=/etc/apk/keys

while getopts "a:r:k:o:" opt; do
	case $opt in
	a) arch="$OPTARG";;
	r) repositories_file="$OPTARG";;
	k) keys_dir="$OPTARG";;
	o) outfile="$OPTARG";;
	esac
done
shift $(( $OPTIND - 1))

cat "$repositories_file"

if [ -z "$outfile" ]; then
	outfile=$name-$arch.tar.gz
fi

30
${APK:-apk} add --keys-dir "$keys_dir" --no-cache \
31
	--repositories-file "$repositories_file" \
32
	--no-script --root "$tmp" --initdb --arch "$arch" \
33 34 35 36 37
	"$@"
for link in $("$tmp"/bin/busybox --list-full); do
	[ -e "$tmp"/$link ] || ln -s /bin/busybox "$tmp"/$link
done

38
${APK:-apk} fetch --keys-dir "$keys_dir" --no-cache \
39
	--repositories-file "$repositories_file" --root "$tmp" \
40
	--stdout --quiet alpine-base | tar -zx -C "$tmp" etc/
41

42 43 44
# make sure root login is disabled
sed -i -e 's/^root::/root:!:/' "$tmp"/etc/shadow

45 46 47 48 49 50 51 52 53 54 55 56
branch=edge
VERSION_ID=$(awk -F= '$1=="VERSION_ID" {print $2}'  "$tmp"/etc/os-release)
case $VERSION_ID in
*_alpha*|*_beta*) branch=edge;;
*.*.*) branch=v${VERSION_ID%.*};;
esac

cat > "$tmp"/etc/apk/repositories <<EOF
http://dl-cdn.alpinelinux.org/alpine/$branch/main
http://dl-cdn.alpinelinux.org/alpine/$branch/community
EOF

57
tar --numeric-owner --exclude='dev/*' -c -C "$tmp" . | gzip -9n > "$outfile"