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