-
Notifications
You must be signed in to change notification settings - Fork 1
/
build.sh
executable file
·248 lines (227 loc) · 8.57 KB
/
build.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
#!/bin/bash
###
### build.sh
###
### Usage:
### build.sh [-h] -a i386|amd64 -o focal|jammy|noble [-r|package...]
###
### Options:
### -a architecture ... i386 or amd64
### -o os ............. focal (20.04) or jammy (22.04) or noble (24.04)
### -i image.tar.xz ... lxc image file
### -r ................ rebuild all required packages for the architecture/os
### package ........... packages to build
###
### Examples:
### ./build.sh -a i386 -o jammy at
### ./build.sh -a i386 -o noble -r
###
BN="$(basename "$0")"
D="$(dirname "$0")"
D="$(cd "${D}" && pwd)"
help () {
sed -rn 's/^### ?//;T;p' "$0"|sed -e "s/BN/${BN}/g"
}
usage () {
help|sed -n "/^Usage:/,/^\s*$/p"
}
OS=noble
ARCHITECTURE=i386
IMAGE=
HELP=
USAGE=
REBUILD=
while getopts 'hra:o:i:' opt; do
case $opt in
a)
ARCHITECTURE="${OPTARG}"
;;
o)
OS="${OPTARG}"
;;
i)
IMAGE="${OPTARG}"
;;
h)
HELP=y
;;
r)
REBUILD=y
;;
*)
USAGE=y
;;
esac
done
shift "$(expr "${OPTIND}" - 1)"
test -n "${HELP}" && {
help
exit 0
}
test -n "${USAGE}" && {
usage >&2
exit 1
}
#
# Extend parameter list for 'rebuild'
#
test -n "${REBUILD}" && {
test -s "${D}/debs/${OS}/${ARCHITECTURE}/rebuild.conf" && {
for p in $(cat "${D}/debs/${OS}/${ARCHITECTURE}/rebuild.conf"|grep -v '^#'); do
set -- "${p}" "$@"
done
}
}
sudo true || exit 1
OSDIR="build-${OS}-${ARCHITECTURE}"
test -d "${OSDIR}" || mkdir "${OSDIR}"
TMPDIR="/tmp/${BN}-$(openssl rand -hex 20)-$$~"
ROOTFS="${OSDIR}/rootfs"
test -d "${ROOTFS}" || {
test -z "${IMAGE}" && IMAGE="$(echo "${D}/${OS}-"*"-${ARCHITECTURE}-lxcimage.tar.xz")"
test -e "${IMAGE}" || {
echo >&2 "${BN}: LXC image '${IMAGE}' does not exist!"
exit 1
}
xz -cd "${IMAGE}"|( cd "${OSDIR}" ; sudo tar -xf -; )
}
cleanUp () {
rm -rf "${TMPDIR}"
test "$(df "${ROOTFS}/dev"|cut -d" " -f1)" != "$(df "${ROOTFS}/tmp"|cut -d" " -f1)" && {
sudo "${D}/umount.sh" "${ROOTFS}"
}
test -n "${RC}" && exit "${RC}"
}
trap cleanUp 0 1 2 3 4 5 6 7 8 9 10 12 13 14 15
mkdir "${TMPDIR}"
touch "${TMPDIR}/empty"
"${D}/init-gpg.sh"
"${D}/rebuild-ppa.sh"
#
# replaceVariables <from >to
#
replaceVariables () {
sed -e 's/\${\OS\}/'"${OS}/g" -e 's/\${\VERSION\}/'"${NEW_VERSION}/g" -e 's/\${\TIMESTAMP\}/'"${TIMESTAMP}/g"
}
RC=0
sudo "${D}/mount.sh" "${ROOTFS}"
sudo chroot "${ROOTFS}" cat /etc/apt/sources.list >"${TMPDIR}/sources.list"
sed -e "s/^deb /deb-src /" <"${TMPDIR}/sources.list" >"${TMPDIR}/debsrc"
sudo chroot "${ROOTFS}" tee /etc/apt/sources.list.d/deb-src.list <"${TMPDIR}/debsrc" >/dev/null
sudo mkdir -p "${ROOTFS}/var/cache/lxc-ppa"
sudo cp "${D}/debs/${OS}/${ARCHITECTURE}"/* "${ROOTFS}/var/cache/lxc-ppa"
sudo cp "${D}/debs/${OS}/${ARCHITECTURE}"/lxc.public.gpg "${ROOTFS}/etc/apt/trusted.gpg.d/."
echo "deb file:/var/cache/lxc-ppa/ ./"|sudo tee "${ROOTFS}/etc/apt/sources.list.d/lxc-ppa.list"
sudo chroot "${ROOTFS}" apt update
sudo chroot "${ROOTFS}" apt upgrade -y
sudo chroot "${ROOTFS}" apt install -y dpkg-dev devscripts equivs
sudo chroot "${ROOTFS}" bash -c "mkdir -p /src"
while [ $# -gt 0 -a "${RC}" -eq 0 ]; do
PACKAGE="$1"
sudo chroot "${ROOTFS}" bash -c "cd /src && rm -rf '${PACKAGE}'"
sudo chroot "${ROOTFS}" bash -c "cd /src && mkdir -p '${PACKAGE}' && cd '${PACKAGE}' && ls *dsc" >"${TMPDIR}/before"
sudo chroot "${ROOTFS}" bash -c "cd /src && cd '${PACKAGE}' && ls" >"${TMPDIR}/before.ls"
sudo chroot "${ROOTFS}" bash -c "cd /src && cd '${PACKAGE}' && apt-get source --download-only '${PACKAGE}'"
sudo chroot "${ROOTFS}" bash -c "cd /src && cd '${PACKAGE}' && ls *dsc" >"${TMPDIR}/after"
sudo chroot "${ROOTFS}" bash -c "cd /src && cd '${PACKAGE}' && ls" >"${TMPDIR}/after.ls"
cmp "${TMPDIR}/before" "${TMPDIR}/after" >/dev/null 2>&1 || {
(
RC=0
set -x
sudo find "${ROOTFS}/src/${PACKAGE}" -mindepth 1 -maxdepth 1 -type d|sudo xargs rm -rf
sudo find "${ROOTFS}/src/${PACKAGE}" -name "*.deb"|sudo xargs rm -rf
sudo chroot "${ROOTFS}" bash -c "cd /src && cd '${PACKAGE}' && apt-get source '${PACKAGE}'" || exit 1
PACKAGE_FOLDER="$(sudo chroot "${ROOTFS}" bash -c "cd /src && cd '${PACKAGE}'/*/. && pwd")"
if [ -d "${D}/patches/${OS}/${PACKAGE}" ]; then
PATCH_FOLDER="${D}/patches/${OS}/${PACKAGE}"
# We do have a patch for the package. Now the situation
# might be quite difficult related to version number
# and dependencies
#
# Save the changelog - it might have been modified since we created the patch
sudo chroot "${ROOTFS}" bash -c "cd '${PACKAGE_FOLDER}' && cat debian/changelog" >"${TMPDIR}/changelog" || exit 1
DIFF=
test -e "${PATCH_FOLDER}/"*.diff && DIFF="$(echo "${PATCH_FOLDER}/"*.diff)"
CHANGELOG_PARAMETERS="${TMPDIR}/empty"
test -e "${PATCH_FOLDER}/changelog.parameters" && CHANGELOG_PARAMETERS="${PATCH_FOLDER}/changelog.parameters"
PREPARE_BUILD="$(grep "^PREPARE_BUILD=" "${CHANGELOG_PARAMETERS}"|cut -d= -f2-)"
VERSION_SEARCH="$(grep "^VERSION_SEARCH=" "${CHANGELOG_PARAMETERS}"|cut -d= -f2-)"
VERSION_REPLACE="$(grep "^VERSION_REPLACE=" "${CHANGELOG_PARAMETERS}"|cut -d= -f2-)"
OLD_VERSION="$(head -1 "${ROOTFS}/${PACKAGE_FOLDER}/debian/changelog"|grep -o '(.*)'|tr -d '()')"
NEW_VERSION="$(echo "${OLD_VERSION}"|sed "s/${VERSION_SEARCH}/${VERSION_REPLACE}/")"
TIMESTAMP="$(date -R)"
test -n "${DIFF}" && {
# Modify the patch - skip changelog
sed -e '/^diff.*changelog$/,/^diff/ d' "${DIFF}" >"${TMPDIR}/diff-without-changelog"
# Apply the modified patch
sudo chroot "${ROOTFS}" bash -c "cd '${PACKAGE_FOLDER}' && patch -p1" <"${TMPDIR}/diff-without-changelog" || exit 1
# Adjust the changelog
replaceVariables <"${D}/patches/${OS}/${PACKAGE}/changelog.tpl" >"${TMPDIR}/changelog.start"
echo >>"${TMPDIR}/changelog.start"
cat "${TMPDIR}/changelog.start" "${TMPDIR}/changelog"| sudo chroot "${ROOTFS}" bash -c "cd '${PACKAGE_FOLDER}' && tee debian/changelog" >/dev/null || exit 1
}
# Install dependencies
echo "yes"|sudo chroot "${ROOTFS}" bash -c "cd '${PACKAGE_FOLDER}' && LC_ALL=C mk-build-deps -i" || exit 1
test -n "${PREPARE_BUILD}" && {
PREPARE_BUILD_EXPANDED="$(echo "${PREPARE_BUILD}"|replaceVariables)"
sudo chroot "${ROOTFS}" bash -c "cd '${PACKAGE_FOLDER}' && eval ${PREPARE_BUILD_EXPANDED}" || exit 1
}
else
sudo chroot "${ROOTFS}" bash -c "cd '${PACKAGE_FOLDER}' && apt-get build-dep -y '${PACKAGE}'" || RC=1
fi
sudo chroot "${ROOTFS}" bash -c "cd '${PACKAGE_FOLDER}' && LC_ALL=C dpkg-buildpackage --build=binary" || RC=1
test "${RC}" -eq "0" || { echo >&2 "Probleme beim Auspacken oder bauen - EXIT"; exit 1; }
test -d "${D}/debs/${OS}/${ARCHITECTURE}" || mkdir -p "${D}/debs/${OS}/${ARCHITECTURE}"
sudo cp "${ROOTFS}/src/${PACKAGE}"/*.deb "${D}/debs/${OS}/${ARCHITECTURE}/."
sudo chown "$(id -un):$(id -gn)" "${D}/debs/${OS}/${ARCHITECTURE}"/*.deb
"${D}/rebuild-ppa.sh"
sudo cp "${D}/debs/${OS}/${ARCHITECTURE}"/* "${ROOTFS}/var/cache/lxc-ppa"
sudo chroot "${ROOTFS}" apt update
) || {
#sudo find "${ROOTFS}/src/${PACKAGE}" -mindepth 1 -maxdepth 1 -newer "${TMPDIR}/before"|sudo xargs -t rm -rf
(
cd "${ROOTFS}/src/${PACKAGE}"
diff "${TMPDIR}/before.ls" "${TMPDIR}/after.ls"|grep "^>"|cut -c2-|sudo xargs -t rm -f
)
RC=1
}
}
test "${RC}" -ne 0 && {
echo >&2 "${BN}: error building package '${PACKAGE}' -> ABORTING"
cleanUp
exit 1
}
rm -rf "${TMPDIR}/before" "${TMPDIR}/after" "${TMPDIR}/before.ls" "${TMPDIR}/after.ls"
shift
done
sudo chroot "${ROOTFS}" tee /etc/apt/sources.list <"${TMPDIR}/sources.list" >/dev/null
sudo "${D}/umount.sh" "${ROOTFS}"
#
# Create the GPG key for the ppa
#
test "${RC}" -eq 0 && {
"${D}/init-gpg.sh"
(
cd "${D}/debs/${OS}/${ARCHITECTURE}"
dpkg-scanpackages . >Packages
{
AFR="APT::FTPArchive::Release::"
echo "${AFR}Origin \"lxc-ubuntu\";"
echo "${AFR}Label \"lxc-ubuntu\";"
echo "${AFR}Suite \"${OS}\";"
echo "${AFR}Codename \"${OS}\";"
echo "${AFR}Architectures \"${ARCHITECTURE}\";"
#echo "${AFR}Components \".\";"
echo "${AFR}Description \"Precompiled packages for our LXC containers\";"
} >"Release.tmp"
apt-ftparchive -c "Release.tmp" release "." >"./Release"
rm -f "Release.tmp"
rm -f "Release.gpg"
gpg --homedir "${D}/gpg" -abs --digest-algo SHA256 -u "$(cat "${D}/gpg/keyid")" -o "Release.gpg" "Release"
rm -f "InRelease"
gpg --homedir "${D}/gpg" -abs --digest-algo SHA256 -u "$(cat "${D}/gpg/keyid")" --clearsign -o "InRelease" "Release"
cp "${D}/gpg/lxc.public.asc" .
cp "${D}/gpg/lxc.public.gpg" .
)
}
cleanUp