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