dns updates
[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
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 dist=${dist,,}
190
191 check
192 if [ -n "$INCLUDE_DEBIAN" ]; then
193 local inc="--include=$INCLUDE_DEBIAN"
194 fi
195
196 if [ -n "$arch" ]; then
197 qemu-debootstrap --arch $arch ${exc} $inc $dist $xtmp $mirror
198 target="${target}_${arch^^}"
199 else
200 if [[ $DIST =~ 64 ]]; then
201 arch=amd64
202 else
203 arch=i386
204 fi
205 debootstrap --arch $arch ${exc} $inc $dist $xtmp $mirror
206 fi
207 cleanup-deb
208 tarit
209 }
210
211 prtdists() {
212
213 echo "Available:
214
215 CENTOS5_32 CENTOS5_64
216 CENTOS6_32 CENTOS6_64
217 CENTOS7_32 CENTOS7_64
218 SLC5_32 SLC5_64
219 SLC6_32 SLC6_64
220 SLC7_64
221 TRUSTY32 TRUSTY64
222 XENIAL32 XENIAL64
223 FLIDAS64
224 BIONIC64
225 ETIONA64
226 SQUEEZE32 SQUEEZE64
227 WHEEZY32 WHEEZY64
228 JESSIE32 JESSIE64
229 STRETCH32 STRETCH64
230 BUSTER32 BUSTER64
231 SID32 SID64
232 "
233 }
234
235 usage() {
236
237 cat <<EOF
238 mk-basefile, create minimal base files for a Linux distritubtion
239
240 Copyright (C) 2011-2018 by Thomas Lange
241
242 Usage: mk-basefile [OPTION] ... DISTRIBUTION
243
244 -s Show list of supported linux distributions
245 -f ARCH Build for foreign architecture ARCH.
246 -d DIR Use DIR for creating the temporary subtree structure.
247 -z Use gzip for compressing the tar file.
248 -J Use xz for compressing the tar file.
249 -k Keep the temporary subtree structure, do not remove it.
250 -x CMD Run CMD in chroot. If CMD exists as a file, copy it and run it.
251 Debian based only
252 -h Print help.
253
254 Usage example: mk-basefile -J STRETCH64
255 This will create a STRETCH64.tar.xz basefile.
256
257 EOF
258 exit 0
259 }
260
261 # main routine
262
263 ext=tar
264 zip=cat
265 attributes=
266 cleanup=1
267 attributes="--xattrs --selinux --acls"
268
269 while getopts ashzJd:kf:x: opt ; do
270 case "$opt" in
271 a) echo "$0: Warning. -a is ignored, because xtattrs, acls and selinux are always added." ;;
272 d) export TMPDIR=$OPTARG ;;
273 f) export ARCH=$OPTARG ;;
274 z) zip="gzip -9"; ext=tar.gz ;;
275 J) zip="xz -8" ext=tar.xz ;;
276 k) cleanup=0 ;;
277 h) usage ;;
278 s) prtdists ; exit 0;;
279 x) cmd="$OPTARG" ;;
280 ?) exit 3 ;; # error in option parsing
281 esac
282 done
283 shift $(($OPTIND - 1))
284
285 xtmp=$(mktemp --tmpdir -d basefiles.XXXXXXXX)
286 if [ $? -eq 1 ]; then
287 echo "mktemp failed. Aborting."
288 exit 2
289 fi
290 chmod 755 $xtmp
291
292 target=$1 # also the name of the output file
293
294 [ -z "$target" ] && usage
295 case "$target" in
296 CENTOS5_32) centos i386 5 ;;
297 CENTOS5_64) centos amd64 5 ;;
298 CENTOS6_32) centos i386 6 ;;
299 CENTOS6_64) centos amd64 6 ;;
300 CENTOS7_32) centos i386 7 ;;
301 CENTOS7_64) centos amd64 7 ;;
302 SLC5_32) slc i386 5 ;;
303 SLC5_64) slc amd64 5 ;;
304 SLC6_32) slc i386 6 ;;
305 SLC6_64) slc amd64 6 ;;
306 SLC7_64) slc amd64 7 ;;
307 BELENOS*|FLIDAS*|ETIONA*)
308 debgeneric $target $MIRROR_TRISQUEL ;;
309 TRUSTY*|XENIAL*|BIONIC*)
310 debgeneric $target $MIRROR_UBUNTU ;;
311 SQUEEZE*|WHEEZY*|JESSIE*|STRETCH*|BUSTER*|SID*)
312 debgeneric $target $MIRROR_DEBIAN $ARCH;;
313 *) echo "Unknown distribution. Aborting."
314 prtdists
315 exit 99 ;;
316 esac
317
318 # cleanup
319 if [ $cleanup -eq 1 ]; then
320 rm -rf $xtmp
321 fi