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