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