update upstream to 9084a3cbc0a55422beea4a55b530c1f03a910617 feb 2024
[automated-distro-installer] / fai / config / basefiles / mk-basefile
1 #! /bin/bash
2
3 # mk-basefile, create basefiles for some distributions
4 #
5 # Thomas Lange, Uni Koeln, 2011-2024
6 # based on the Makefile implementation of Michael Goetze
7 #
8 # Usage example: mk-basefile -J STRETCH64
9 # This will create a STRETCH64.tar.xz basefile.
10
11 # Supported distributions (i386/amd64):
12 # Debian GNU/Linux
13 # Ubuntu 14.04/16.04/20.04/22.04
14 # AlmaLinux 9
15 # Rocky Linux 8/9
16 # CentOS 5/6/7/8
17 # Scientific Linux Cern 5/6
18 #
19 # Packages you might want to install to use this command:
20 # debootstrap, rinse, xz-utils
21
22
23 # Define your local mirros here
24 # For the first stage, set the CentOS/SLC mirror in /etc/rinse/rinse.conf
25 MIRROR_DEBIAN=http://deb.debian.org/debian/
26 MIRROR_UBUNTU=http://mirror.netcologne.de/ubuntu/
27 MIRROR_CENTOS=http://mirror.netcologne.de/
28
29 EXCLUDE_SQUEEZE=isc-dhcp-client,isc-dhcp-common,info
30 EXCLUDE_WHEEZY=info
31 EXCLUDE_JESSIE=info
32 EXCLUDE_STRETCH=info
33 EXCLUDE_BUSTER=
34 EXCLUDE_BULLSEYE=
35 EXCLUDE_BOOKWORM=
36 EXCLUDE_TRIXIE=
37 EXCLUDE_FORKY=
38 EXCLUDE_SID=
39
40 EXCLUDE_TRUSTY=dhcp3-client,dhcp3-common,info
41 EXCLUDE_XENIAL=udhcpc,dibbler-client,info
42 EXCLUDE_BIONIC=udhcpc,dibbler-client,info
43 EXCLUDE_FOCAL=udhcpc,dibbler-client,info
44 EXCLUDE_JAMMY=udhcpc,dibbler-client,info
45 EXCLUDE_NOBLE=udhcpc,dibbler-client,info
46
47 # here you can add packages, that are needed very early
48 INCLUDE_DEBIAN=
49
50
51 setarch() {
52
53 l32=
54 if [ X$1 = Xi386 ]; then
55 l32=linux32
56 fi
57 }
58
59 check() {
60
61 if [ $(id -u) != 0 ]; then
62 echo "You must be root to create chroots."
63 exit 1
64 fi
65 mknod $xtmp/test-dev-null c 1 3
66 if [ $? -eq 1 ]; then
67 echo "Cannot create device files on $xtmp, aborting."
68 echo "Perhaps this directory is mounted with option nodev."
69 rm -rf $xtmp
70 exit 1
71 fi
72 echo test > $xtmp/test-dev-null
73 if [ $? -eq 1 ]; then
74 echo "Cannot create device files on $xtmp, aborting."
75 echo "Perhaps this directory is mounted with option nodev."
76 rm -rf $xtmp
77 exit 1
78 fi
79 rm -f $xtmp/test-dev-null
80 }
81
82
83 mkpost-centos() {
84
85 # set local mirror for rinse post script
86 [ -z "$MIRROR_CENTOS" ] && return
87 cat <<EOM > $xtmp/post
88 #! /bin/sh
89 mkdir -p $xtmp/etc/yum.repos.d/orig
90 cp -p $xtmp/etc/yum.repos.d/*.repo $xtmp/etc/yum.repos.d/orig
91 perl -pi -e 's,mirrorlist=,#mirrorlist=,; s,#baseurl=http://mirror.centos.org,baseurl=$MIRROR_CENTOS,;' $xtmp/etc/yum.repos.d/CentOS-Base.repo
92 EOM
93 chmod 555 $xtmp/post
94 }
95
96
97 mkpost-slc() {
98
99 # set local mirror for rinse post script
100 ver=$1
101 [ -z "$MIRROR_SLC" ] && return
102 cat <<EOM > $xtmp/post
103 #! /bin/sh
104 mkdir -p $xtmp/etc/yum.repos.d/orig
105 cp -p $xtmp/etc/yum.repos.d/*.repo $xtmp/etc/yum.repos.d/orig
106 perl -pi -e 's,baseurl=http://linuxsoft.cern.ch,baseurl=$MIRROR_SLC,;' $xtmp/etc/yum.repos.d/slc$ver-os.repo
107 perl -pi -e 's,baseurl=http://linuxsoft.cern.ch,baseurl=$MIRROR_SLC,;' $xtmp/etc/yum.repos.d/slc$ver-updates.repo
108
109 EOM
110 chmod 555 $xtmp/post
111 }
112
113
114 cleanup-deb() {
115
116 chroot $xtmp apt-get clean
117 rm -f $xtmp/etc/hostname $xtmp/etc/resolv.conf \
118 $xtmp/var/lib/apt/lists/*_* $xtmp/usr/bin/qemu-*-static \
119 $xtmp/etc/udev/rules.d/70-persistent-net.rules \
120 $xtmp/var/lib/dbus/machine-id
121 > $xtmp/etc/machine-id
122 }
123
124
125 cleanup-rinse() {
126
127 # check if chroot works
128 echo "Installed packages in chroot:"
129 chroot $xtmp rpm -qa|sort
130 echo -n "CHROOT rpm -qa: "
131 chroot $xtmp rpm -qa|wc -l
132
133 rm -f $xtmp/etc/resolv.conf $xtmp/post
134 if [ -d $xtmp/etc/yum.repos.d/orig ]; then
135 mv $xtmp/etc/yum.repos.d/orig/* $xtmp/etc/yum.repos.d/
136 rm -rf $xtmp/etc/yum.repos.d/orig
137 fi
138 }
139
140
141 tarit() {
142
143 tar $attributes --numeric-owner --one-file-system -C $xtmp -cf - . | $zip > $target.$ext
144 }
145
146
147 rpmdist() {
148
149 local arch=$1
150 local vers=$2
151 local dist=$3
152 local domain=$(domainname)
153
154 check
155 setarch $arch
156 $l32 rinse --directory $xtmp --distribution $dist-$vers --arch $arch --before-post-install $xtmp/post
157 domainname $domain # workaround for #613377
158 cleanup-rinse
159 tarit
160 }
161
162
163 alma() {
164 rpmdist $1 $2 alma
165 }
166
167 rocky() {
168 rpmdist $1 $2 rocky
169 }
170
171 centos() {
172
173 local arch=$1
174 local vers=$2
175 local domain=$(domainname)
176
177 check
178 setarch $arch
179 mkpost-centos
180 $l32 rinse --directory $xtmp --distribution centos-$vers --arch $arch --before-post-install $xtmp/post
181 domainname $domain # workaround for #613377
182 cleanup-rinse
183 tarit
184 }
185
186
187 slc() {
188
189 local arch=$1
190 local vers=$2
191
192 check
193 setarch $arch
194 mkpost-slc $vers
195 $l32 rinse --directory $xtmp --distribution slc-$vers --arch $arch --before-post-install $xtmp/post
196 cleanup-rinse
197 tarit
198 }
199
200
201 debgeneric() {
202
203 local DIST=$1
204 shift
205 local mirror=$1
206 shift
207 local arch=$1
208
209 dist=${DIST%%[0-9][0-9]}
210 local exc="EXCLUDE_$dist"
211 [ -n "${!exc}" ] && exc="--exclude=${!exc}" || unset exc
212 dist=${dist,,}
213
214 check
215 if [ -n "$INCLUDE_DEBIAN" ]; then
216 local inc="--include=$INCLUDE_DEBIAN"
217 fi
218
219 if [ -n "$arch" ]; then
220 qemu-debootstrap --arch $arch ${exc} $inc $dist $xtmp $mirror
221 target="${target}_${arch^^}"
222 else
223 if [[ $DIST =~ 64 ]]; then
224 arch=amd64
225 else
226 arch=i386
227 fi
228 debootstrap --arch $arch ${exc} $inc $dist $xtmp $mirror
229 fi
230 cleanup-deb
231 tarit
232 }
233
234 prtdists() {
235
236 echo "Available:
237
238 ALMA9_64
239 ROCKY8_64
240 ROCKY9_64
241 CENTOS5_32 CENTOS5_64
242 CENTOS6_32 CENTOS6_64
243 CENTOS7_32 CENTOS7_64
244 CENTOS8_64
245 SLC5_32 SLC5_64
246 SLC6_32 SLC6_64
247 SLC7_64
248 TRUSTY32 TRUSTY64
249 XENIAL32 XENIAL64
250 BIONIC64
251 FOCAL64
252 JAMMY64
253 NOBLE64
254 SQUEEZE32 SQUEEZE64
255 WHEEZY32 WHEEZY64
256 JESSIE32 JESSIE64
257 STRETCH32 STRETCH64
258 BUSTER32 BUSTER64
259 BULLSEYE32 BULLSEYE64
260 BOOKWORM32 BOOKWORM64
261 TRIXIE32 TRIXIE64
262 FORKY32 FORKY64
263 SID32 SID64
264 "
265 }
266
267 usage() {
268
269 cat <<EOF
270 mk-basefile, create minimal base files for a Linux distritubtion
271
272 Copyright (C) 2011-2023 by Thomas Lange
273
274 Usage: mk-basefile [OPTION] ... DISTRIBUTION
275
276 -s Show list of supported linux distributions
277 -f ARCH Build for foreign architecture ARCH.
278 -d DIR Use DIR for creating the temporary subtree structure.
279 -z Use zstd for compressing the tar file.
280 -J Use xz for compressing the tar file.
281 -k Keep the temporary subtree structure, do not remove it.
282 -h Print help.
283
284 Usage example: mk-basefile -J STRETCH64
285 This will create a STRETCH64.tar.xz basefile.
286
287 EOF
288 exit 0
289 }
290
291 # main routine
292
293 ext=tar
294 zip=cat
295 attributes=
296 cleanup=1
297 attributes="--xattrs --selinux --acls"
298
299 while getopts ashzJd:kf: opt ; do
300 case "$opt" in
301 a) echo "$0: Warning. -a is ignored, because xtattrs, acls and selinux are always added." ;;
302 d) export TMPDIR=$OPTARG ;;
303 f) export ARCH=$OPTARG ;;
304 z) zip="zstd -9"; ext=tar.zst ;;
305 J) zip="xz -8" ext=tar.xz ;;
306 k) cleanup=0 ;;
307 h) usage ;;
308 s) prtdists ; exit 0;;
309 ?) exit 3 ;; # error in option parsing
310 esac
311 done
312 shift $((OPTIND - 1))
313
314 xtmp=$(mktemp --tmpdir -d basefiles.XXXXXXXX)
315 if [ $? -eq 1 ]; then
316 echo "mktemp failed. Aborting."
317 exit 2
318 fi
319 chmod 755 $xtmp
320
321 target=$1 # also the name of the output file
322
323 [ -z "$target" ] && usage
324 case "$target" in
325 CENTOS5_32) centos i386 5 ;;
326 CENTOS5_64) centos amd64 5 ;;
327 CENTOS6_32) centos i386 6 ;;
328 CENTOS6_64) centos amd64 6 ;;
329 CENTOS7_32) centos i386 7 ;;
330 CENTOS7_64) centos amd64 7 ;;
331 CENTOS8_64) centos amd64 8 ;;
332 ROCKY8_64) rocky amd64 8 ;;
333 ROCKY9_64) rocky amd64 9 ;;
334 ALMA9_64) alma amd64 9 ;;
335 SLC5_32) slc i386 5 ;;
336 SLC5_64) slc amd64 5 ;;
337 SLC6_32) slc i386 6 ;;
338 SLC6_64) slc amd64 6 ;;
339 SLC7_64) slc amd64 7 ;;
340 TRUSTY*|XENIAL*|BIONIC*|FOCAL*|JAMMY*|NOBLE*)
341 debgeneric $target $MIRROR_UBUNTU ;;
342 SQUEEZE*|WHEEZY*|JESSIE*|STRETCH*|BUSTER*|BULLSEYE*|BOOKWORM*|TRIXIE*|FORKY*|SID*)
343 debgeneric $target $MIRROR_DEBIAN $ARCH;;
344 *) echo "Unknown distribution. Aborting."
345 prtdists
346 exit 99 ;;
347 esac
348
349 # cleanup
350 if [ $cleanup -eq 1 ]; then
351 rm -rf $xtmp
352 fi