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