Repository navigation
Expand file tree
/
Copy pathupdate.sh
More file actions
executable file
·152 lines (115 loc) · 4.39 KB
/
Copy pathupdate.sh
File metadata and controls
executable file
·152 lines (115 loc) · 4.39 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#!/bin/sh
#
# This script running on linux amd64 platform, generate rootfs and
# build base image for imghub(https://hub.docker.com/r/sandwichimg)
#
set -xe
for DEPS in docker wget curl grep strings tar xz ; do
which $DEPS > /dev/null || exit
done
if [ "$(id -u)" -ne 0 ]; then
die 'This script must be run as root!'
fi
usage() {
cat <<EOF
Usage: ./update.sh [options]
Example:
sudo ./update.sh -d alpine -a x86_64 -b edge
Options and environment variables:
-d DISTRO Linux distribution(alpine/debian/deepin/ubuntu).
-a ARCH CPU architecture for the distribution.
-b BRANCH Linux distribution branch.
-h Show this help message and exit.
EOF
}
while getopts 'a:b:d:h' OPTION; do
case "$OPTION" in
a) ARCH="$OPTARG";;
b) BRANCH="$OPTARG";;
d) DISTRO="$OPTARG";;
h) usage; exit 0;;
esac
done
: ${ALPINE_BRANCH:="edge"}
: ${ALPINE_MIRROR:="http://mirror.leaseweb.com/alpine"}
: ${ALPINE_PACKAGES:="apk-tools-static debootstrap perl"}
: ${ARCH:=}
: ${ALPINE_ARCH:=}
: ${ALPINE_CHROOT:="alpine_chroot"}
: ${QEMU_VER:="v5.0.0-2"}
: ${TEMP_DIR:="tmp"}
normalize_alpine_arch() {
case "$1" in
armv7h) echo 'armv7' ;;
arm64 ) echo 'aarch64';;
i386 ) echo 'x86' ;;
amd64 ) echo 'x86_64' ;;
*) echo "$1";;
esac
}
normalize_qemu_arch() {
case "$1" in
armhf | armv7 ) echo 'arm' ;;
arm64 ) echo 'aarch64';;
*) echo "$1";;
esac
}
ALPINE_ARCH="$(normalize_alpine_arch $ARCH)"
QEMU_ARCH="$(normalize_qemu_arch $ARCH)"
QEMU_URI="https://github.com/multiarch/qemu-user-static/releases/download/${QEMU_VER}/x86_64_qemu-${QEMU_ARCH}-static.tar.gz"
mkdir -p $TEMP_DIR/$ALPINE_ARCH $ALPINE_CHROOT/$ALPINE_ARCH/usr/bin
# download apk.static
if [ ! -f $TEMP_DIR/$ALPINE_ARCH/apk.static ]; then
apkv=$(curl -sSL "$ALPINE_MIRROR"/$ALPINE_BRANCH/main/$ALPINE_ARCH/APKINDEX.tar.gz | tar -Oxz | strings |
grep '^P:apk-tools-static$' -A1 | tail -n1 | cut -d: -f2)
curl -sSL "$ALPINE_MIRROR"/$ALPINE_BRANCH/main/$ALPINE_ARCH/apk-tools-static-${apkv}.apk | tar -xz -C $TEMP_DIR/$ALPINE_ARCH sbin/apk.static --strip=1
fi
mkdir -p "$ALPINE_CHROOT"/$ALPINE_ARCH/usr/bin "$ALPINE_CHROOT"/$ALPINE_ARCH/mnt/rootfs/usr/bin
case $ARCH in
arm* | aarch64 | mips*) if [ ! -f x86_64_qemu-${QEMU_ARCH}-static.tar.gz ]; then
wget "$QEMU_URI"
fi
if [ ! -f "$ALPINE_CHROOT"/$ALPINE_ARCH/usr/bin/qemu-$QEMU_ARCH-static ]; then
tar -xvf x86_64_qemu-${QEMU_ARCH}-static.tar.gz -C "$ALPINE_CHROOT"/$ALPINE_ARCH/usr/bin
fi
if [ ! -f "$ALPINE_CHROOT"/$ALPINE_ARCH/mnt/rootfs/usr/bin/qemu-$QEMU_ARCH-static ]; then
cp "$ALPINE_CHROOT"/$ALPINE_ARCH/usr/bin/qemu* "$ALPINE_CHROOT"/$ALPINE_ARCH/mnt/rootfs/usr/bin
fi
;;
esac
install_alpine_chroot() {
mkdir -p "$ALPINE_CHROOT"/$ALPINE_ARCH/etc/apk
printf '%s\n' \
"$ALPINE_MIRROR/$ALPINE_BRANCH/main" \
"$ALPINE_MIRROR/$ALPINE_BRANCH/community" \
> "$ALPINE_CHROOT"/$ALPINE_ARCH/etc/apk/repositories
cp /etc/resolv.conf "$ALPINE_CHROOT"/$ALPINE_ARCH/etc/resolv.conf
$TEMP_DIR/$ALPINE_ARCH/apk.static --update-cache --allow-untrusted \
--root $ALPINE_CHROOT/$ALPINE_ARCH --initdb add alpine-base $ALPINE_PACKAGES --verbose
}
if [ ! -f $ALPINE_CHROOT/$ALPINE_ARCH/etc/os-release ]; then
install_alpine_chroot
fi
cp $DISTRO/gen-rootfs.sh $ALPINE_CHROOT/$ALPINE_ARCH
mount -t proc none $ALPINE_CHROOT/$ALPINE_ARCH/proc
mount -o bind /sys $ALPINE_CHROOT/$ALPINE_ARCH/sys
mount -o bind /dev $ALPINE_CHROOT/$ALPINE_ARCH/dev
ARCH=$ARCH BRANCH=$BRANCH chroot $ALPINE_CHROOT/$ALPINE_ARCH /gen-rootfs.sh
umount -l $ALPINE_CHROOT/$ALPINE_ARCH/proc
umount -l $ALPINE_CHROOT/$ALPINE_ARCH/sys
umount -l $ALPINE_CHROOT/$ALPINE_ARCH/dev
case $ARCH in
arm* | aarch64) rm -rf $ALPINE_CHROOT/$ALPINE_ARCH/mnt/rootfs/usr/bin/qemu* ;;
esac
tar --numeric-owner --create --auto-compress --file $ALPINE_CHROOT/$ALPINE_ARCH/mnt/rootfs.tar.xz --directory $ALPINE_CHROOT/$ALPINE_ARCH/mnt/rootfs --transform='s,^./,,' . &&
mkdir -p $DISTRO/$BRANCH/$ARCH &&
mv $ALPINE_CHROOT/$ALPINE_ARCH/mnt/rootfs.tar.xz $DISTRO/$BRANCH/$ARCH
rm -rf $ALPINE_CHROOT/$ALPINE_ARCH/mnt/rootfs $ALPINE_CHROOT/$ALPINE_ARCH/gen-rootfs.sh
cat > $DISTRO/$BRANCH/$ARCH/Dockerfile <<EOF
FROM scratch
ADD rootfs.tar.xz /
EOF
case $ARCH in
arm* | aarch64) echo "ADD x86_64_qemu-*-static.tar.gz /usr/bin" >> $DISTRO/$BRANCH/$ARCH/Dockerfile && cp x86_64_qemu-${QEMU_ARCH}-static.tar.gz $DISTRO/$BRANCH/$ARCH ;;
esac
docker build -t sandwichimg/$DISTRO:$ARCH-$BRANCH $DISTRO/$BRANCH/$ARCH