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