152392d5b17c0bc1a59703736627168a6699c361
[automated-distro-installer] / fai / config / basefiles / mk-basefile
1 #! /bin/bash
2 set -x
3 # mk-basefile, create basefiles for some distributions
4 #
5 # Thomas Lange, Uni Koeln, 2011-2018
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
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_TRISQUEL=http://mirror.fsf.org/trisquel/
26 MIRROR_CENTOS=http://mirror.netcologne.de/
27
28 EXCLUDE_SQUEEZE=isc-dhcp-client,isc-dhcp-common,info
29 EXCLUDE_WHEEZY=info
30 EXCLUDE_JESSIE=info
31 EXCLUDE_STRETCH=info
32 EXCLUDE_BUSTER=info
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
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 if [[ $cmd ]]; then
112 if [[ -e $cmd ]]; then
113 cp "$cmd" $xtmp/tmp
114 chroot $xtmp "/tmp/$cmd"
115 else
116 chroot $xtmp $cmd
117 fi
118 fi
119 chroot $xtmp apt-get clean
120 rm -f $xtmp/etc/hostname $xtmp/etc/resolv.conf \
121 $xtmp/var/lib/apt/lists/*_* $xtmp/usr/bin/qemu-*-static \
122 $xtmp/etc/udev/rules.d/70-persistent-net.rules
123 > $xtmp/etc/machine-id
124 }
125
126
127 cleanup-rinse() {
128
129 # check if chroot works
130 echo "Installed packages in chroot:"
131 chroot $xtmp rpm -qa|sort
132 echo -n "CHROOT rpm -qa: "
133 chroot $xtmp rpm -qa|wc -l
134
135 rm -f $xtmp/etc/resolv.conf $xtmp/post
136 if [ -d $xtmp/etc/yum.repos.d/orig ]; then
137 mv $xtmp/etc/yum.repos.d/orig/* $xtmp/etc/yum.repos.d/
138 rm -rf $xtmp/etc/yum.repos.d/orig
139 fi
140 }
141
142
143 tarit() {
144
145 tar $attributes --one-file-system -C $xtmp -cf - . | $zip > $target.$ext
146 }
147
148
149 centos() {
150
151 local arch=$1
152 local vers=$2
153 local domain=$(domainname)
154
155 check
156 setarch $arch
157 mkpost-centos
158 $l32 rinse --directory $xtmp --distribution centos-$vers --arch $arch --before-post-install $xtmp/post
159 domainname $domain # workaround for #613377
160 cleanup-rinse
161 tarit
162 }
163
164
165 slc() {
166
167 local arch=$1
168 local vers=$2
169
170 check
171 setarch $arch
172 mkpost-slc $vers
173 $l32 rinse --directory $xtmp --distribution slc-$vers --arch $arch --before-post-install $xtmp/post
174 cleanup-rinse
175 tarit
176 }
177
178
179 debgeneric() {
180
181 local DIST=$1
182 shift
183 local mirror=$1
184 shift
185 local arch=$1
186
187 dist=${DIST%%[0-9][0-9]}
188 local exc="EXCLUDE_$dist"
189 [ -n "${!exc}" ] && exc="--exclude=${!exc}" || unset exc
190 dist=${dist,,}
191
192 check
193 if [ -n "$INCLUDE_DEBIAN" ]; then
194 local inc="--include=$INCLUDE_DEBIAN"
195 fi
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 SLC5_32 SLC5_64
220 SLC6_32 SLC6_64
221 SLC7_64
222 TRUSTY32 TRUSTY64
223 XENIAL32 XENIAL64
224 FLIDAS64
225 BIONIC64
226 ETIONA64
227 SQUEEZE32 SQUEEZE64
228 WHEEZY32 WHEEZY64
229 JESSIE32 JESSIE64
230 STRETCH32 STRETCH64
231 BUSTER32 BUSTER64
232 SID32 SID64
233 "
234 }
235
236 usage() {
237
238 cat <<EOF
239 mk-basefile, create minimal base files for a Linux distritubtion
240
241 Copyright (C) 2011-2018 by Thomas Lange
242
243 Usage: mk-basefile [OPTION] ... DISTRIBUTION
244
245 -s Show list of supported linux distributions
246 -f ARCH Build for foreign architecture ARCH.
247 -d DIR Use DIR for creating the temporary subtree structure.
248 -z Use gzip for compressing the tar file.
249 -J Use xz for compressing the tar file.
250 -k Keep the temporary subtree structure, do not remove it.
251 -x CMD Run CMD in chroot. If CMD exists as a file, copy it and run it.
252 Debian based only
253 -h Print help.
254
255 Usage example: mk-basefile -J STRETCH64
256 This will create a STRETCH64.tar.xz basefile.
257
258 EOF
259 exit 0
260 }
261
262 # main routine
263
264 ext=tar
265 zip=cat
266 attributes=
267 cleanup=1
268 attributes="--xattrs --selinux --acls"
269
270 while getopts ashzJd:kf:x: opt ; do
271 case "$opt" in
272 a) echo "$0: Warning. -a is ignored, because xtattrs, acls and selinux are always added." ;;
273 d) export TMPDIR=$OPTARG ;;
274 f) export ARCH=$OPTARG ;;
275 z) zip="gzip -9"; ext=tar.gz ;;
276 J) zip="xz -8" ext=tar.xz ;;
277 k) cleanup=0 ;;
278 h) usage ;;
279 s) prtdists ; exit 0;;
280 x) cmd="$OPTARG" ;;
281 ?) exit 3 ;; # error in option parsing
282 esac
283 done
284 shift $(($OPTIND - 1))
285
286 xtmp=$(mktemp --tmpdir -d basefiles.XXXXXXXX)
287 if [ $? -eq 1 ]; then
288 echo "mktemp failed. Aborting."
289 exit 2
290 fi
291 chmod 755 $xtmp
292
293 target=$1 # also the name of the output file
294
295 [ -z "$target" ] && usage
296 case "$target" in
297 CENTOS5_32) centos i386 5 ;;
298 CENTOS5_64) centos amd64 5 ;;
299 CENTOS6_32) centos i386 6 ;;
300 CENTOS6_64) centos amd64 6 ;;
301 CENTOS7_32) centos i386 7 ;;
302 CENTOS7_64) centos amd64 7 ;;
303 SLC5_32) slc i386 5 ;;
304 SLC5_64) slc amd64 5 ;;
305 SLC6_32) slc i386 6 ;;
306 SLC6_64) slc amd64 6 ;;
307 SLC7_64) slc amd64 7 ;;
308 BELENOS*|FLIDAS*|ETIONA*)
309 debgeneric $target $MIRROR_TRISQUEL ;;
310 TRUSTY*|XENIAL*|BIONIC*)
311 debgeneric $target $MIRROR_UBUNTU ;;
312 SQUEEZE*|WHEEZY*|JESSIE*|STRETCH*|BUSTER*|SID*)
313 debgeneric $target $MIRROR_DEBIAN $ARCH;;
314 *) echo "Unknown distribution. Aborting."
315 prtdists
316 exit 99 ;;
317 esac
318
319 # cleanup
320 if [ $cleanup -eq 1 ]; then
321 rm -rf $xtmp
322 fi