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