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