minor fixes and documentation
[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-2016
6 # based on the Makefile implementation of Michael Goetze
7
8 # Supported distributions (each i386/amd64):
9 # Debian GNU/Linux
10 # Ubuntu 14.04/16.04
11 # CentOS 5/6/7
12 # Scientific Linux Cern 5/6
13 #
14 # Packages you might want to install to use this command:
15 # debootstrap, rinse, xz-utils
16
17
18 # Define your local mirros here
19 # For the first stage, set the CentOS/SLC mirror in /etc/rinse/rinse.conf
20 MIRROR_DEBIAN=http://http.us.debian.org/debian
21 MIRROR_UBUNTU=http://mirror.netcologne.de/ubuntu/
22 MIRROR_TRISQUEL=http://mirror.fsf.org/trisquel/
23 MIRROR_CENTOS=http://mirror.netcologne.de/
24
25 EXCLUDE_SQUEEZE=isc-dhcp-client,isc-dhcp-common,info,tasksel,tasksel-data
26 EXCLUDE_WHEEZY=info,tasksel,tasksel-data
27 EXCLUDE_JESSIE=tasksel,tasksel-data
28 EXCLUDE_STRETCH=tasksel,tasksel-data
29 EXCLUDE_BELENOS=dhcp3-client,dhcp3-common,info
30 EXCLUDE_TRUSTY=dhcp3-client,dhcp3-common,info
31 EXCLUDE_XENIAL=tasksel,tasksel-data
32
33 INCLUDE_DEBIAN=aptitude
34
35
36 setarch() {
37
38 l32=
39 if [ X$1 = Xi386 ]; then
40 l32=linux32
41 fi
42 }
43
44 check() {
45
46 if [ `id -u` != 0 ]; then
47 echo "You must be root to create chroots."
48 exit 1
49 fi
50 mknod $xtmp/test-dev-null c 1 3
51 if [ $? -eq 1 ]; then
52 echo "Cannot create device files on $xtmp, aborting."
53 echo "Perhaps this directory is mounted with option nodev."
54 rm -rf $xtmp
55 exit 1
56 fi
57 echo test > $xtmp/test-dev-null
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 rm -f $xtmp/test-dev-null
65 }
66
67
68 mkpost-centos() {
69
70 [ -z "$MIRROR_CENTOS" ] && return
71 cat <<EOM > $xtmp/post
72 #! /bin/sh
73 mkdir -p $xtmp/etc/yum.repos.d/orig
74 cp -p $xtmp/etc/yum.repos.d/*.repo $xtmp/etc/yum.repos.d/orig
75 perl -pi -e 's,mirrorlist=,#mirrorlist=,; s,#baseurl=http://mirror.centos.org,baseurl=$MIRROR_CENTOS,;' $xtmp/etc/yum.repos.d/CentOS-Base.repo
76 EOM
77 chmod 555 $xtmp/post
78 }
79
80
81 mkpost-slc() {
82
83 ver=$1
84 [ -z "$MIRROR_SLC" ] && return
85 cat <<EOM > $xtmp/post
86 #! /bin/sh
87 mkdir -p $xtmp/etc/yum.repos.d/orig
88 cp -p $xtmp/etc/yum.repos.d/*.repo $xtmp/etc/yum.repos.d/orig
89 perl -pi -e 's,baseurl=http://linuxsoft.cern.ch,baseurl=$MIRROR_SLC,;' $xtmp/etc/yum.repos.d/slc$ver-os.repo
90 perl -pi -e 's,baseurl=http://linuxsoft.cern.ch,baseurl=$MIRROR_SLC,;' $xtmp/etc/yum.repos.d/slc$ver-updates.repo
91
92 EOM
93 chmod 555 $xtmp/post
94 }
95
96
97 cleanup-deb() {
98
99 chroot $xtmp aptitude clean
100 rm -f $xtmp/etc/hostname $xtmp/etc/resolv.conf $xtmp/etc/machine-id
101 rm $xtmp/var/lib/apt/lists/*_*
102 rm -f $xtmp/etc/udev/rules.d/70-persistent-net.rules
103 }
104
105
106 cleanup-rinse() {
107
108 # check if chroot works
109 echo "Installed packages in chroot:"
110 chroot $xtmp rpm -qa|sort
111 echo -n "CHROOT rpm -qa: "
112 chroot $xtmp rpm -qa|wc -l
113
114 rm -f $xtmp/etc/resolv.conf $xtmp/post
115 if [ -d $xtmp/etc/yum.repos.d/orig ]; then
116 mv $xtmp/etc/yum.repos.d/orig/* $xtmp/etc/yum.repos.d/
117 rm -rf $xtmp/etc/yum.repos.d/orig
118 fi
119 }
120
121
122 tarit() {
123
124 tar $attributes --one-file-system -C $xtmp -cf - . | $zip > $target.$ext
125 }
126
127
128 centos() {
129
130 local arch=$1
131 local vers=$2
132 local domain=$(domainname)
133
134 check
135 setarch $arch
136 mkpost-centos
137 $l32 rinse --directory $xtmp --distribution centos-$vers --arch $arch --before-post-install $xtmp/post
138 domainname $domain # workaround for #613377
139 cleanup-rinse
140 tarit
141 }
142
143
144 slc() {
145
146 local arch=$1
147 local vers=$2
148
149 check
150 setarch $arch
151 mkpost-slc $vers
152 $l32 rinse --directory $xtmp --distribution slc-$vers --arch $arch --before-post-install $xtmp/post
153 cleanup-rinse
154 tarit
155 }
156
157
158 debgeneric() {
159
160 local DIST=$1
161 shift
162 local mirror=$1
163
164 if [[ $DIST =~ 64 ]]; then
165 arch=amd64
166 else
167 arch=i386
168 fi
169
170 DIST=${DIST%%??}
171 dist=${DIST,,}
172
173 local exc="EXCLUDE_$DIST"
174 check
175 debootstrap --arch $arch --exclude=${!exc} --include=${INCLUDE_DEBIAN} $dist $xtmp $mirror
176 cleanup-deb
177 tarit
178 }
179
180 prtdists() {
181
182 echo "Available:
183
184 CENTOS5_32 CENTOS5_64
185 CENTOS6_32 CENTOS6_64
186 CENTOS7_32 CENTOS7_64
187 SLC5_32 SLC5_64
188 SLC6_32 SLC6_64
189 SLC7_64
190 TRUSTY32 TRUSTY64
191 XENIAL32 XENIAL64
192 SQUEEZE32 SQUEEZE64
193 WHEEZY32 WHEEZY64
194 JESSIE32 JESSIE64
195 STRETCH32 STRETCH64
196 "
197 }
198
199 usage() {
200
201 cat <<EOF
202 mk-basefile, create minimal base files for a Linux distritubtion
203
204 Copyright (C) 2011-2016 by Thomas Lange
205
206 Usage: mk-basefile [OPTION] ... DISTRIBUTION
207
208 -s Show list of supported linux distributions
209 -a Add xtattrs, acl and selinux properties to the tar file.
210 -d DIR Use DIR for creating the temporary subtree structure.
211 -z Use gzip for compressing the tar file.
212 -J Use xz for compressing the tar file.
213 -k Keep the temporary subtree structure, do not remove it.
214 -h Print help.
215
216 Usage example: mk-basefile -J STRETCH64
217 This will create a STRETCH64.tar.xz basefile.
218
219 EOF
220 exit 0
221 }
222
223 # main routine
224
225 ext=tar
226 zip=cat
227 attributes=
228 cleanup=1
229
230 while getopts ashzJd:k opt ; do
231 case "$opt" in
232 a) attributes="--xattrs --selinux --acl" ;;
233 d) export TMPDIR=$OPTARG ;;
234 z) zip="gzip -9"; ext=tar.gz ;;
235 J) zip="xz -8" ext=tar.xz ;;
236 k) cleanup=0 ;;
237 h) usage ;;
238 s) prtdists ; exit 0;;
239 ?) exit 3 ;; # error in option parsing
240 esac
241 done
242 shift $(($OPTIND - 1))
243
244 xtmp=$(mktemp --tmpdir -d basefiles.XXXXXXXX)
245 if [ $? -eq 1 ]; then
246 echo "mktemp failed. Aborting."
247 exit 2
248 fi
249 chmod 755 $xtmp
250
251 target=$1 # also the name of the output file
252
253 case "$target" in
254 CENTOS5_32) centos i386 5 ;;
255 CENTOS5_64) centos amd64 5 ;;
256 CENTOS6_32) centos i386 6 ;;
257 CENTOS6_64) centos amd64 6 ;;
258 CENTOS7_32) centos i386 7 ;;
259 CENTOS7_64) centos amd64 7 ;;
260 SLC5_32) slc i386 5 ;;
261 SLC5_64) slc amd64 5 ;;
262 SLC6_32) slc i386 6 ;;
263 SLC6_64) slc amd64 6 ;;
264 SLC7_64) slc amd64 7 ;;
265 BELANOS*)
266 debgeneric $target $MIRROR_TRISQUEL ;;
267 TRUSTY*|XENIAL*)
268 debgeneric $target $MIRROR_UBUNTU ;;
269 SQUEEZE*|WHEEZY*|JESSIE*|STRETCH*)
270 debgeneric $target $MIRROR_DEBIAN ;;
271 *) echo "Unknown distribution. Aborting."
272 prtdists
273 exit 99 ;;
274 esac
275
276 # cleanup
277 if [ $cleanup -eq 1 ]; then
278 rm -rf $xtmp
279 fi