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