Merge branch 'upstream'
[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
46 # here you can add packages, that are needed very early
47 INCLUDE_DEBIAN=
48 INCLUDE_ETIONA=ifupdown
49 INCLUDE_NABIA=ifupdown
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 if [[ $cmd ]]; then
117 if [[ -e $cmd ]]; then
118 cp "$cmd" $xtmp/tmp
119 chroot $xtmp "/tmp/$cmd"
120 else
121 chroot $xtmp $cmd
122 fi
123 fi
124 chroot $xtmp apt-get clean
125 rm -f $xtmp/etc/hostname $xtmp/etc/resolv.conf \
126 $xtmp/var/lib/apt/lists/*_* $xtmp/usr/bin/qemu-*-static \
127 $xtmp/etc/udev/rules.d/70-persistent-net.rules
128 > $xtmp/etc/machine-id
129 }
130
131
132 cleanup-rinse() {
133
134 # check if chroot works
135 echo "Installed packages in chroot:"
136 chroot $xtmp rpm -qa|sort
137 echo -n "CHROOT rpm -qa: "
138 chroot $xtmp rpm -qa|wc -l
139
140 rm -f $xtmp/etc/resolv.conf $xtmp/post
141 if [ -d $xtmp/etc/yum.repos.d/orig ]; then
142 mv $xtmp/etc/yum.repos.d/orig/* $xtmp/etc/yum.repos.d/
143 rm -rf $xtmp/etc/yum.repos.d/orig
144 fi
145 }
146
147
148 tarit() {
149
150 tar $attributes --numeric-owner --one-file-system -C $xtmp -cf - . | $zip > $target.$ext
151 }
152
153
154 centos() {
155
156 local arch=$1
157 local vers=$2
158 local domain=$(domainname)
159
160 check
161 setarch $arch
162 mkpost-centos
163 $l32 rinse --directory $xtmp --distribution centos-$vers --arch $arch --before-post-install $xtmp/post
164 domainname $domain # workaround for #613377
165 cleanup-rinse
166 tarit
167 }
168
169
170 slc() {
171
172 local arch=$1
173 local vers=$2
174
175 check
176 setarch $arch
177 mkpost-slc $vers
178 $l32 rinse --directory $xtmp --distribution slc-$vers --arch $arch --before-post-install $xtmp/post
179 cleanup-rinse
180 tarit
181 }
182
183
184 debgeneric() {
185
186 local DIST=$1
187 shift
188 local mirror=$1
189 shift
190 local arch=$1
191
192 dist=${DIST%%[0-9][0-9]}
193 local exc="EXCLUDE_$dist"
194 [ -n "${!exc}" ] && exc="--exclude=${!exc}" || unset exc
195 local inc="INCLUDE_$dist"
196 [ -n "${!inc}" ] && inc="--include=${!inc}" || unset inc
197 dist=${dist,,}
198
199 check
200
201 if [ -n "$arch" ]; then
202 qemu-debootstrap --arch $arch ${exc} $inc $dist $xtmp $mirror
203 target="${target}_${arch^^}"
204 else
205 if [[ $DIST =~ 64 ]]; then
206 arch=amd64
207 else
208 arch=i386
209 fi
210 debootstrap --arch $arch ${exc} $inc $dist $xtmp $mirror
211 fi
212 cleanup-deb
213 tarit
214 }
215
216 prtdists() {
217
218 echo "Available:
219
220 CENTOS5_32 CENTOS5_64
221 CENTOS6_32 CENTOS6_64
222 CENTOS7_32 CENTOS7_64
223 CENTOS8_64
224 SLC5_32 SLC5_64
225 SLC6_32 SLC6_64
226 SLC7_64
227 TRUSTY32 TRUSTY64
228 XENIAL32 XENIAL64
229 FLIDAS64
230 BIONIC64
231 ETIONA64
232 FOCAL64
233 NABIA64
234 SQUEEZE32 SQUEEZE64
235 WHEEZY32 WHEEZY64
236 JESSIE32 JESSIE64
237 STRETCH32 STRETCH64
238 BUSTER32 BUSTER64
239 BULLSEYE32 BULLSEYE64
240 BOOKWORM32 BOOKWORM64
241 TRIXIE32 TRIXIE64
242 SID32 SID64
243 "
244 }
245
246 usage() {
247
248 cat <<EOF
249 mk-basefile, create minimal base files for a Linux distritubtion
250
251 Copyright (C) 2011-2020 by Thomas Lange
252
253 Usage: mk-basefile [OPTION] ... DISTRIBUTION
254
255 -s Show list of supported linux distributions
256 -f ARCH Build for foreign architecture ARCH.
257 -d DIR Use DIR for creating the temporary subtree structure.
258 -z Use gzip for compressing the tar file.
259 -J Use xz for compressing the tar file.
260 -k Keep the temporary subtree structure, do not remove it.
261 -x CMD Run CMD in chroot. If CMD exists as a file, copy it and run it.
262 Debian based only
263 -h Print help.
264
265 Usage example: mk-basefile -J STRETCH64
266 This will create a STRETCH64.tar.xz basefile.
267
268 EOF
269 exit 0
270 }
271
272 # main routine
273
274 ext=tar
275 zip=cat
276 attributes=
277 cleanup=1
278 attributes="--xattrs --selinux --acls"
279
280 while getopts ashzJd:kf:x: opt ; do
281 case "$opt" in
282 a) echo "$0: Warning. -a is ignored, because xtattrs, acls and selinux are always added." ;;
283 d) export TMPDIR=$OPTARG ;;
284 f) export ARCH=$OPTARG ;;
285 z) zip="gzip -9"; ext=tar.gz ;;
286 J) zip="xz -8" ext=tar.xz ;;
287 k) cleanup=0 ;;
288 h) usage ;;
289 s) prtdists ; exit 0;;
290 x) cmd="$OPTARG" ;;
291 ?) exit 3 ;; # error in option parsing
292 esac
293 done
294 shift $((OPTIND - 1))
295
296 xtmp=$(mktemp --tmpdir -d basefiles.XXXXXXXX)
297 if [ $? -eq 1 ]; then
298 echo "mktemp failed. Aborting."
299 exit 2
300 fi
301 chmod 755 $xtmp
302
303 target=$1 # also the name of the output file
304
305 [ -z "$target" ] && usage
306 case "$target" in
307 CENTOS5_32) centos i386 5 ;;
308 CENTOS5_64) centos amd64 5 ;;
309 CENTOS6_32) centos i386 6 ;;
310 CENTOS6_64) centos amd64 6 ;;
311 CENTOS7_32) centos i386 7 ;;
312 CENTOS7_64) centos amd64 7 ;;
313 CENTOS8_64) centos amd64 8 ;;
314 SLC5_32) slc i386 5 ;;
315 SLC5_64) slc amd64 5 ;;
316 SLC6_32) slc i386 6 ;;
317 SLC6_64) slc amd64 6 ;;
318 SLC7_64) slc amd64 7 ;;
319 BELENOS*|FLIDAS*|ETIONA*|NABIA*)
320 debgeneric $target $MIRROR_TRISQUEL ;;
321 TRUSTY*|XENIAL*|BIONIC*|FOCAL*)
322 debgeneric $target $MIRROR_UBUNTU ;;
323 SQUEEZE*|WHEEZY*|JESSIE*|STRETCH*|BUSTER*|BULLSEYE*|BOOKWORM*|TRIXIE*|SID*)
324 debgeneric $target $MIRROR_DEBIAN $ARCH;;
325 *) echo "Unknown distribution. Aborting."
326 prtdists
327 exit 99 ;;
328 esac
329
330 # cleanup
331 if [ $cleanup -eq 1 ]; then
332 rm -rf $xtmp
333 fi