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