b93831ca3493a506c1040149a1b32e2dfd1b2a29
[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_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_TRUSTY=dhcp3-client,dhcp3-common,info
34 EXCLUDE_XENIAL=udhcpc,dibbler-client,info,tasksel,tasksel-data
35
36 # we need aptitude in the base file, since users may want to use it for installing packages
37 INCLUDE_DEBIAN=aptitude
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 chroot $xtmp apt-get clean
106 rm -f $xtmp/etc/hostname $xtmp/etc/resolv.conf \
107 $xtmp/var/lib/apt/lists/*_* $xtmp/usr/bin/qemu-*-static \
108 $xtmp/etc/udev/rules.d/70-persistent-net.rules
109 > $xtmp/etc/machine-id
110 }
111
112
113 cleanup-rinse() {
114
115 # check if chroot works
116 echo "Installed packages in chroot:"
117 chroot $xtmp rpm -qa|sort
118 echo -n "CHROOT rpm -qa: "
119 chroot $xtmp rpm -qa|wc -l
120
121 rm -f $xtmp/etc/resolv.conf $xtmp/post
122 if [ -d $xtmp/etc/yum.repos.d/orig ]; then
123 mv $xtmp/etc/yum.repos.d/orig/* $xtmp/etc/yum.repos.d/
124 rm -rf $xtmp/etc/yum.repos.d/orig
125 fi
126 }
127
128
129 tarit() {
130
131 tar $attributes --one-file-system -C $xtmp -cf - . | $zip > $target.$ext
132 }
133
134
135 centos() {
136
137 local arch=$1
138 local vers=$2
139 local domain=$(domainname)
140
141 check
142 setarch $arch
143 mkpost-centos
144 $l32 rinse --directory $xtmp --distribution centos-$vers --arch $arch --before-post-install $xtmp/post
145 domainname $domain # workaround for #613377
146 cleanup-rinse
147 tarit
148 }
149
150
151 slc() {
152
153 local arch=$1
154 local vers=$2
155
156 check
157 setarch $arch
158 mkpost-slc $vers
159 $l32 rinse --directory $xtmp --distribution slc-$vers --arch $arch --before-post-install $xtmp/post
160 cleanup-rinse
161 tarit
162 }
163
164
165 debgeneric() {
166
167 local DIST=$1
168 shift
169 local mirror=$1
170 shift
171 local arch=$1
172
173 dist=${DIST%%[0-9][0-9]}
174 local exc="EXCLUDE_$dist"
175 dist=${dist,,}
176
177 check
178 if [ -n "$arch" ]; then
179 qemu-debootstrap --arch $arch --exclude=${!exc} --include=${INCLUDE_DEBIAN} $dist $xtmp $mirror
180 target="${target}_${arch^^}"
181 else
182 if [[ $DIST =~ 64 ]]; then
183 arch=amd64
184 else
185 arch=i386
186 fi
187 debootstrap --arch $arch --exclude=${!exc} --include=${INCLUDE_DEBIAN} $dist $xtmp $mirror
188 fi
189 cleanup-deb
190 tarit
191 }
192
193 prtdists() {
194
195 echo "Available:
196
197 CENTOS5_32 CENTOS5_64
198 CENTOS6_32 CENTOS6_64
199 CENTOS7_32 CENTOS7_64
200 SLC5_32 SLC5_64
201 SLC6_32 SLC6_64
202 SLC7_64
203 TRUSTY32 TRUSTY64
204 XENIAL32 XENIAL64
205 SQUEEZE32 SQUEEZE64
206 WHEEZY32 WHEEZY64
207 JESSIE32 JESSIE64
208 STRETCH32 STRETCH64
209 BUSTER32 BUSTER64
210 "
211 }
212
213 usage() {
214
215 cat <<EOF
216 mk-basefile, create minimal base files for a Linux distritubtion
217
218 Copyright (C) 2011-2017 by Thomas Lange
219
220 Usage: mk-basefile [OPTION] ... DISTRIBUTION
221
222 -s Show list of supported linux distributions
223 -a Add xtattrs, acl and selinux properties to the tar file.
224 -f ARCH Build for foreign architecture ARCH.
225 -d DIR Use DIR for creating the temporary subtree structure.
226 -z Use gzip for compressing the tar file.
227 -J Use xz for compressing the tar file.
228 -k Keep the temporary subtree structure, do not remove it.
229 -h Print help.
230
231 Usage example: mk-basefile -J STRETCH64
232 This will create a STRETCH64.tar.xz basefile.
233
234 EOF
235 exit 0
236 }
237
238 # main routine
239
240 ext=tar
241 zip=cat
242 attributes=
243 cleanup=1
244
245 while getopts ashzJd:kf: opt ; do
246 case "$opt" in
247 a) attributes="--xattrs --selinux --acl" ;;
248 d) export TMPDIR=$OPTARG ;;
249 f) export ARCH=$OPTARG ;;
250 z) zip="gzip -9"; ext=tar.gz ;;
251 J) zip="xz -8" ext=tar.xz ;;
252 k) cleanup=0 ;;
253 h) usage ;;
254 s) prtdists ; exit 0;;
255 ?) exit 3 ;; # error in option parsing
256 esac
257 done
258 shift $(($OPTIND - 1))
259
260 xtmp=$(mktemp --tmpdir -d basefiles.XXXXXXXX)
261 if [ $? -eq 1 ]; then
262 echo "mktemp failed. Aborting."
263 exit 2
264 fi
265 chmod 755 $xtmp
266
267 target=$1 # also the name of the output file
268
269 [ -z "$target" ] && usage
270 case "$target" in
271 CENTOS5_32) centos i386 5 ;;
272 CENTOS5_64) centos amd64 5 ;;
273 CENTOS6_32) centos i386 6 ;;
274 CENTOS6_64) centos amd64 6 ;;
275 CENTOS7_32) centos i386 7 ;;
276 CENTOS7_64) centos amd64 7 ;;
277 SLC5_32) slc i386 5 ;;
278 SLC5_64) slc amd64 5 ;;
279 SLC6_32) slc i386 6 ;;
280 SLC6_64) slc amd64 6 ;;
281 SLC7_64) slc amd64 7 ;;
282 TRUSTY*|XENIAL*)
283 debgeneric $target $MIRROR_UBUNTU ;;
284 SQUEEZE*|WHEEZY*|JESSIE*|STRETCH*|BUSTER*)
285 debgeneric $target $MIRROR_DEBIAN $ARCH;;
286 *) echo "Unknown distribution. Aborting."
287 prtdists
288 exit 99 ;;
289 esac
290
291 # cleanup
292 if [ $cleanup -eq 1 ]; then
293 rm -rf $xtmp
294 fi