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