-
Notifications
You must be signed in to change notification settings - Fork 5
/
update.sh
executable file
·197 lines (181 loc) · 6.09 KB
/
update.sh
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#!/bin/bash
set -xeo pipefail
# A POSIX variable
OPTIND=1 # Reset in case getopts has been used previously in the shell.
while getopts "v:u:d:" opt; do
case "$opt" in
v) VERSION=$OPTARG
;;
u) QEMU_VER=$OPTARG
;;
d) DOCKER_REPO=$OPTARG
;;
esac
done
cd "$(readlink -f "$(dirname "$BASH_SOURCE")")"
versions=""
if [ "${VERSION}" != "" ]; then
versions="${VERSION}"
elif [ ${#} -gt 0 ]; then
versions=( "$@" )
else
versions=( */ )
fi
get_part() {
dir="$1"
shift
part="$1"
shift
if [ -f "$dir/$part" ]; then
cat "$dir/$part"
return 0
fi
if [ -f "$part" ]; then
cat "$part"
return 0
fi
if [ $# -gt 0 ]; then
echo "$1"
return 0
fi
return 1
}
for version in "${versions[@]}"; do
version="${version%/}"
dir="$(readlink -f "$version")"
url="$(get_part "$dir" url)"
tags="$(get_part "$dir" tags)"
centos_version="$(get_part "$dir" version)"
arch="$(get_part "$dir" arch)"
qemu_arch="$(get_part "$dir" qemu_arch "")"
scw_arch="$(get_part "$dir" scw_arch "$arch")"
cd "$dir"
# fetch image
wget -N $url
mkdir -p iso-slim
# create rootfs.tar
# Minimaize used sudo scope.
# virt-tar-out and guestfish outputs below meesage without sudo.
# "*stdin*:1: libguestfs: error: /usr/bin/supermin exited with error status 1"
# https://bugzilla.redhat.com/show_bug.cgi?id=1591617
# That's a bug of Ubuntu kernel. This does not happen on Fedora.
# https://bugs.launchpad.net/ubuntu/+source/linux/+bug/759725
if [[ $url =~ \.(img|raw)\.xz$ ]]; then
imgxzfilename="$(basename $url)"
imgfilename="${imgxzfilename%.xz}"
if [ ! -f "${imgfilename}" ]; then
cat "${imgxzfilename}" | unxz > "${imgfilename}"
fi
if [ ! -f iso-slim/rootfs.tar ]; then
sudo virt-tar-out -a "${imgfilename}" / iso-slim/rootfs.tar
user_name=$(id -un)
group_name=$(id -gn)
sudo chown ${user_name}:${group_name} iso-slim/rootfs.tar
fi
fi
if [[ $url =~ \.tar\.xz$ ]]; then
xzfilename="$(basename $url)"
if [ ! -f iso-slim/rootfs.tar ]; then
cat "${xzfilename}" | unxz > iso-slim/rootfs.tar
fi
fi
if [[ $url =~ \.qcow2$ ]]; then
filename="$(basename $url)"
if [ ! -f iso-slim/rootfs.tar ]; then
sudo virt-tar-out -a "${filename}" / iso-slim/rootfs.tar
user_name=$(id -un)
group_name=$(id -gn)
sudo chown ${user_name}:${group_name} iso-slim/rootfs.tar
fi
fi
if [[ $url =~ \.iso$ ]]; then
filename="$(basename $url)"
if [ ! -f iso-slim/rootfs.tar ]; then
# Use guestfish directly because of virt-tar-out issue for iso file.
# https://github.com/libguestfs/libguestfs/issues/37
# https://www.redhat.com/archives/libguestfs/2019-May/msg00019.html
file_system=$(
sudo guestfish -a ${filename} --ro <<EOF | cut -d: -f 1
run
list-filesystems
EOF
)
sudo guestfish --ro -a "${filename}" -m "${file_system}" tar-out / iso-slim/rootfs.tar
user_name=$(id -un)
group_name=$(id -gn)
sudo chown ${user_name}:${group_name} iso-slim/rootfs.tar
fi
fi
# create iso-slim dockerfile
cat > iso-slim/Dockerfile <<EOF
FROM scratch
ADD rootfs.tar /
ENV ARCH=${scw_arch} CENTOS_VERSION=${centos_version} DOCKER_REPO=${DOCKER_REPO} CENTOS_IMAGE_URL=${url} QEMU_ARCH=${qemu_arch}
EOF
## build iso-slim image
docker build -t ${DOCKER_REPO}:$version-iso-slim iso-slim
for tag in $tags; do
docker tag ${DOCKER_REPO}:$version-iso-slim ${DOCKER_REPO}:$tag-iso-slim
done
# create iso dockerfile
mkdir -p iso
if [ -n "${qemu_arch}" -a ! -f "iso/qemu-${qemu_arch}-static" ]; then
wget https://github.com/multiarch/qemu-user-static/releases/download/${QEMU_VER}/qemu-${qemu_arch}-static -O "iso/qemu-${qemu_arch}-static"
chmod +x "iso/qemu-${qemu_arch}-static"
fi
if [ -n "${qemu_arch}" ]; then
cat > iso/Dockerfile <<EOF
FROM ${DOCKER_REPO}:$version-iso-slim
ADD qemu-${qemu_arch}-static /usr/bin
EOF
else
cat > iso/Dockerfile <<EOF
FROM ${DOCKER_REPO}:$version-iso-slim
EOF
fi
docker build -t ${DOCKER_REPO}:$version-iso iso
for tag in $tags; do
docker tag ${DOCKER_REPO}:$version-iso ${DOCKER_REPO}:$tag-iso
done
docker run -it --rm ${DOCKER_REPO}:$version-iso bash -xc '
uname -a
echo
cat /etc/os-release 2>/dev/null
echo
cat /etc/redhat-release 2>/dev/null
true
'
# build iso-cleanup image
mkdir -p iso-clean
cat > iso-clean/Dockerfile <<EOF
FROM ${DOCKER_REPO}:$version-iso
RUN yum remove -y \
kernel-* *-firmware grub* centos-logos mariadb* \
postfix btrfs* mozjs17 xfsprogs cloud-init pciutils* \
libsoup* libgudev* python-prettytable \
python-setuptools python-boto yum-utils \
libsysfs* glib-networking libproxy plymouth* \
libdrm wpa_supplicant *-desktop-* \
perl gcc cpp doxygen emacs-nox || true
RUN rm -rf /boot
EOF
docker build -t tmp-${DOCKER_REPO}:$version-iso-cleaner iso-clean
tmpname=export-$(openssl rand -base64 10 | sed 's@[=/+]@@g')
docker run --name="$tmpname" --entrypoint=/does/not/exist tmp-${DOCKER_REPO}:$version-iso-cleaner 2>/dev/null || true
docker export "$tmpname" | \
docker import \
-c "ENV ARCH=${scw_arch} CENTOS_VERSION=${centos_version} DOCKER_REPO=${DOCKER_REPO} CENTOS_IMAGE_URL=${url} QEMU_ARCH=${qemu_arch}" \
- "${DOCKER_REPO}:$version-clean"
docker rm "$tmpname"
for tag in $tags; do
docker tag ${DOCKER_REPO}:$version-clean ${DOCKER_REPO}:$tag-clean
done
docker run -it --rm ${DOCKER_REPO}:$version-clean bash -xc '
uname -a
echo
cat /etc/os-release 2>/dev/null
echo
cat /etc/redhat-release 2>/dev/null
true
'
done