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