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