update to upstream 5.3.4
[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-2016
6 # based on the Makefile implementation of Michael Goetze
7
8 # Supported distributions (each i386/amd64):
9 # Debian GNU/Linux
10 # Ubuntu 14.04/16.04
11 # CentOS 5/6/7
12 # Scientific Linux Cern 5/6
13 #
14 # Packages you might want to install to use this command:
15 # debootstrap, rinse, xz-utils
16
17
18 # Define your local mirros here
19 # For the first stage, set the CentOS/SLC mirror in /etc/rinse/rinse.conf
20 MIRROR_DEBIAN=http://httpredir.debian.org/debian/
21 MIRROR_UBUNTU=http://mirror.netcologne.de/ubuntu/
22 MIRROR_CENTOS=http://mirror.netcologne.de/
23
24 EXCLUDE_SQUEEZE=isc-dhcp-client,isc-dhcp-common,info,tasksel,tasksel-data
25 EXCLUDE_WHEEZY=info,tasksel,tasksel-data
26 EXCLUDE_JESSIE=tasksel,tasksel-data
27 EXCLUDE_STRETCH=tasksel,tasksel-data
28
29 EXCLUDE_TRUSTY=dhcp3-client,dhcp3-common,info
30 EXCLUDE_XENIAL=tasksel,tasksel-data
31
32 INCLUDE_DEBIAN=aptitude
33
34
35 setarch() {
36
37 l32=
38 if [ X$1 = Xi386 ]; then
39 l32=linux32
40 fi
41 }
42
43 check() {
44
45 if [ `id -u` != 0 ]; then
46 echo "You must be root to create chroots."
47 exit 1
48 fi
49 mknod $xtmp/test-dev-null c 1 3
50 if [ $? -eq 1 ]; then
51 echo "Cannot create device files on $xtmp, aborting."
52 echo "Perhaps this directory is mounted with option nodev."
53 rm -rf $xtmp
54 exit 1
55 fi
56 echo test > $xtmp/test-dev-null
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 rm -f $xtmp/test-dev-null
64 }
65
66
67 mkpost-centos() {
68
69 [ -z "$MIRROR_CENTOS" ] && return
70 cat <<EOM > $xtmp/post
71 #! /bin/sh
72 mkdir -p $xtmp/etc/yum.repos.d/orig
73 cp -p $xtmp/etc/yum.repos.d/*.repo $xtmp/etc/yum.repos.d/orig
74 perl -pi -e 's,mirrorlist=,#mirrorlist=,; s,#baseurl=http://mirror.centos.org,baseurl=$MIRROR_CENTOS,;' $xtmp/etc/yum.repos.d/CentOS-Base.repo
75 EOM
76 chmod 555 $xtmp/post
77 }
78
79
80 mkpost-slc() {
81
82 ver=$1
83 [ -z "$MIRROR_SLC" ] && return
84 cat <<EOM > $xtmp/post
85 #! /bin/sh
86 mkdir -p $xtmp/etc/yum.repos.d/orig
87 cp -p $xtmp/etc/yum.repos.d/*.repo $xtmp/etc/yum.repos.d/orig
88 perl -pi -e 's,baseurl=http://linuxsoft.cern.ch,baseurl=$MIRROR_SLC,;' $xtmp/etc/yum.repos.d/slc$ver-os.repo
89 perl -pi -e 's,baseurl=http://linuxsoft.cern.ch,baseurl=$MIRROR_SLC,;' $xtmp/etc/yum.repos.d/slc$ver-updates.repo
90
91 EOM
92 chmod 555 $xtmp/post
93 }
94
95
96 cleanup-deb() {
97
98 chroot $xtmp aptitude clean
99 rm -f $xtmp/etc/hostname $xtmp/etc/resolv.conf $xtmp/etc/machine-id
100 rm $xtmp/var/lib/apt/lists/*_*
101 rm -f $xtmp/etc/udev/rules.d/70-persistent-net.rules
102 }
103
104
105 cleanup-rinse() {
106
107 # check if chroot works
108 echo "Installed packages in chroot:"
109 chroot $xtmp rpm -qa|sort
110 echo -n "CHROOT rpm -qa: "
111 chroot $xtmp rpm -qa|wc -l
112
113 rm -f $xtmp/etc/resolv.conf $xtmp/post
114 if [ -d $xtmp/etc/yum.repos.d/orig ]; then
115 mv $xtmp/etc/yum.repos.d/orig/* $xtmp/etc/yum.repos.d/
116 rm -rf $xtmp/etc/yum.repos.d/orig
117 fi
118 }
119
120
121 tarit() {
122
123 tar $attributes --one-file-system -C $xtmp -cf - . | $zip > $target.$ext
124 }
125
126
127 centos() {
128
129 local arch=$1
130 local vers=$2
131 local domain=$(domainname)
132
133 check
134 setarch $arch
135 mkpost-centos
136 $l32 rinse --directory $xtmp --distribution centos-$vers --arch $arch --before-post-install $xtmp/post
137 domainname $domain # workaround for #613377
138 cleanup-rinse
139 tarit
140 }
141
142
143 slc() {
144
145 local arch=$1
146 local vers=$2
147
148 check
149 setarch $arch
150 mkpost-slc $vers
151 $l32 rinse --directory $xtmp --distribution slc-$vers --arch $arch --before-post-install $xtmp/post
152 cleanup-rinse
153 tarit
154 }
155
156
157 debgeneric() {
158
159 local DIST=$1
160 shift
161 local mirror=$1
162
163 if [[ $DIST =~ 64 ]]; then
164 arch=amd64
165 else
166 arch=i386
167 fi
168
169 DIST=${DIST%%??}
170 dist=${DIST,,}
171
172 local exc="EXCLUDE_$DIST"
173 check
174 debootstrap --arch $arch --exclude=${!exc} --include=${INCLUDE_DEBIAN} $dist $xtmp $mirror
175 cleanup-deb
176 tarit
177 }
178
179 prtdists() {
180
181 echo "Available:
182
183 CENTOS5_32 CENTOS5_64
184 CENTOS6_32 CENTOS6_64
185 CENTOS7_32 CENTOS7_64
186 SLC5_32 SLC5_64
187 SLC6_32 SLC6_64
188 SLC7_64
189 TRUSTY32 TRUSTY64
190 XENIAL32 XENIAL64
191 SQUEEZE32 SQUEEZE64
192 WHEEZY32 WHEEZY64
193 JESSIE32 JESSIE64
194 STRETCH32 STRETCH64
195 "
196 }
197
198 usage() {
199
200 cat <<EOF
201 mk-basefile, create minimal base files for a Linux distritubtion
202
203 Copyright (C) 2011-2016 by Thomas Lange
204
205 Usage: mk-basefile [OPTION] ... DISTRIBUTION
206
207 -s Show list of supported linux distributions
208 -a Add xtattrs, acl and selinux properties to the tar file.
209 -d DIR Use DIR for creating the temporary subtree structure.
210 -z Use gzip for compressing the tar file.
211 -J Use xz for compressing the tar file.
212 -k Keep the temporary subtree structure, do not remove it.
213 -h Print help.
214
215 Usage example: mk-basefile -J STRETCH64
216 This will create a STRETCH64.tar.xz basefile.
217
218 EOF
219 exit 0
220 }
221
222 # main routine
223
224 ext=tar
225 zip=cat
226 attributes=
227 cleanup=1
228
229 while getopts ashzJd:k opt ; do
230 case "$opt" in
231 a) attributes="--xattrs --selinux --acl" ;;
232 d) export TMPDIR=$OPTARG ;;
233 z) zip="gzip -9"; ext=tar.gz ;;
234 J) zip="xz -8" ext=tar.xz ;;
235 k) cleanup=0 ;;
236 h) usage ;;
237 s) prtdists ; exit 0;;
238 ?) exit 3 ;; # error in option parsing
239 esac
240 done
241 shift $(($OPTIND - 1))
242
243 xtmp=$(mktemp --tmpdir -d basefiles.XXXXXXXX)
244 if [ $? -eq 1 ]; then
245 echo "mktemp failed. Aborting."
246 exit 2
247 fi
248
249 target=$1 # also the name of the output file
250
251 case "$target" in
252 CENTOS5_32) centos i386 5 ;;
253 CENTOS5_64) centos amd64 5 ;;
254 CENTOS6_32) centos i386 6 ;;
255 CENTOS6_64) centos amd64 6 ;;
256 CENTOS7_32) centos i386 7 ;;
257 CENTOS7_64) centos amd64 7 ;;
258 SLC5_32) slc i386 5 ;;
259 SLC5_64) slc amd64 5 ;;
260 SLC6_32) slc i386 6 ;;
261 SLC6_64) slc amd64 6 ;;
262 SLC7_64) slc amd64 7 ;;
263 TRUSTY*|XENIAL*)
264 debgeneric $target $MIRROR_UBUNTU ;;
265 SQUEEZE*|WHEEZY*|JESSIE*|STRETCH*)
266 debgeneric $target $MIRROR_DEBIAN ;;
267 *) echo "Unknown distribution. Aborting."
268 prtdists
269 exit 99 ;;
270 esac
271
272 # cleanup
273 if [ $cleanup -eq 1 ]; then
274 rm -rf $xtmp
275 fi