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