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