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