add syncthing port forward
[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-2015
6 # based on the Makefile implementation of Michael Goetze
7 #
8 # Usage example: fai-mk-basefile -J SQUEEZE64
9 # This will create a SQUEEZE64.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://http.us.debian.org/debian
24 #MIRROR_DEBIAN=http://localmirror/debian/
25 MIRROR_UBUNTU=http://mirror.netcologne.de/ubuntu/
26 MIRROR_TRISQUEL=http://mirror.fsf.org/trisquel/
27 MIRROR_CENTOS=http://mirror.netcologne.de/
28 #MIRROR_CENTOS=http://localmirror
29 #MIRROR_SLC=http://localmirror
30
31 EXCLUDE_SQUEEZE=isc-dhcp-client,isc-dhcp-common,info,tasksel,tasksel-data
32 EXCLUDE_WHEEZY=isc-dhcp-client,isc-dhcp-common,info,tasksel,tasksel-data
33 EXCLUDE_JESSIE=isc-dhcp-client,isc-dhcp-common,info,tasksel,tasksel-data
34 EXCLUDE_BELENOS=dhcp3-client,dhcp3-common,info
35 EXCLUDE_TRUSTY=dhcp3-client,dhcp3-common,info
36 EXCLUDE_XENIAL=isc-dhcp-client,isc-dhcp-common,udhcpc,dibbler-client,dhcpcd5,info,tasksel,tasksel-data
37
38 INCLUDE_DEBIAN=aptitude
39
40
41 setarch() {
42
43 l32=
44 if [ X$1 = Xi386 ]; then
45 l32=linux32
46 fi
47 }
48
49 check() {
50
51 if [ `id -u` != 0 ]; then
52 echo "You must be root to create chroots."
53 exit 1
54 fi
55 mknod $xtmp/test-dev-null c 1 3
56 if [ $? -eq 1 ]; then
57 echo "Cannot create device files on $xtmp, aborting."
58 echo "Perhaps this directory is mounted with option nodev."
59 rm -rf $xtmp
60 exit 1
61 fi
62 echo test > $xtmp/test-dev-null
63 if [ $? -eq 1 ]; then
64 echo "Cannot create device files on $xtmp, aborting."
65 echo "Perhaps this directory is mounted with option nodev."
66 rm -rf $xtmp
67 exit 1
68 fi
69 rm -f $xtmp/test-dev-null
70 }
71
72
73 mkpost-centos() {
74
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 ver=$1
89 [ -z "$MIRROR_SLC" ] && return
90 cat <<EOM > $xtmp/post
91 #! /bin/sh
92 mkdir -p $xtmp/etc/yum.repos.d/orig
93 cp -p $xtmp/etc/yum.repos.d/*.repo $xtmp/etc/yum.repos.d/orig
94 perl -pi -e 's,baseurl=http://linuxsoft.cern.ch,baseurl=$MIRROR_SLC,;' $xtmp/etc/yum.repos.d/slc$ver-os.repo
95 perl -pi -e 's,baseurl=http://linuxsoft.cern.ch,baseurl=$MIRROR_SLC,;' $xtmp/etc/yum.repos.d/slc$ver-updates.repo
96
97 EOM
98 chmod 555 $xtmp/post
99 }
100
101
102 cleanup-deb() {
103
104 chroot $xtmp aptitude clean
105 rm -f $xtmp/etc/hostname $xtmp/etc/resolv.conf $xtmp/etc/machine-id
106 rm $xtmp/var/lib/apt/lists/*_*
107 rm -f $xtmp/etc/udev/rules.d/70-persistent-net.rules
108 }
109
110
111 cleanup-rinse() {
112
113 # check if chroot works
114 echo "Installed packages in chroot:"
115 chroot $xtmp rpm -qa|sort
116 echo -n "CHROOT rpm -qa: "
117 chroot $xtmp rpm -qa|wc -l
118
119 rm -f $xtmp/etc/resolv.conf $xtmp/post
120 if [ -d $xtmp/etc/yum.repos.d/orig ]; then
121 mv $xtmp/etc/yum.repos.d/orig/* $xtmp/etc/yum.repos.d/
122 rm -rf $xtmp/etc/yum.repos.d/orig
123 fi
124 }
125
126
127 tarit() {
128
129 tar --xattrs --selinux --acl --one-file-system -C $xtmp -cf - . | $zip > $target.$ext
130 }
131
132
133 centos() {
134
135 local arch=$1
136 local vers=$2
137 local domain=$(domainname)
138
139 check
140 setarch $arch
141 mkpost-centos
142 $l32 rinse --directory $xtmp --distribution centos-$vers --arch $arch --before-post-install $xtmp/post
143 domainname $domain # workaround for #613377
144 cleanup-rinse
145 tarit
146 }
147
148
149 slc() {
150
151 local arch=$1
152 local vers=$2
153
154 check
155 setarch $arch
156 mkpost-slc $vers
157 $l32 rinse --directory $xtmp --distribution slc-$vers --arch $arch --before-post-install $xtmp/post
158 cleanup-rinse
159 tarit
160 }
161
162
163 squeeze() {
164
165 local arch=$1
166
167 check
168 debootstrap --arch $arch --exclude=${EXCLUDE_SQUEEZE} squeeze $xtmp ${MIRROR_DEBIAN}
169 cleanup-deb
170 tarit
171 }
172
173 wheezy() {
174
175 local arch=$1
176
177 check
178 debootstrap --arch $arch --exclude=${EXCLUDE_WHEEZY} wheezy $xtmp ${MIRROR_DEBIAN}
179 cleanup-deb
180 tarit
181 }
182
183 jessie() {
184
185 local arch=$1
186
187 check
188 debootstrap --arch $arch --exclude=${EXCLUDE_JESSIE} --include=${INCLUDE_DEBIAN} jessie $xtmp ${MIRROR_DEBIAN}
189 cleanup-deb
190 tarit
191 }
192
193 stretch() {
194
195 local arch=$1
196
197 check
198 debootstrap --arch $arch --exclude=${EXCLUDE_JESSIE} --include=${INCLUDE_DEBIAN} stretch $xtmp ${MIRROR_DEBIAN}
199 cleanup-deb
200 tarit
201 }
202
203 trusty() {
204
205 local arch=$1
206
207 check
208 debootstrap --arch $arch --exclude=${EXCLUDE_TRUSTY} --include=${INCLUDE_DEBIAN} trusty $xtmp ${MIRROR_UBUNTU}
209 cleanup-deb
210 tarit
211 }
212
213 xenial() {
214
215 local arch=$1
216
217 check
218 debootstrap --arch $arch --exclude=${EXCLUDE_XENIAL} --include=${INCLUDE_DEBIAN} xenial $xtmp ${MIRROR_UBUNTU}
219 cleanup-deb
220 tarit
221 }
222
223
224 belenos() {
225 ln -sf gutsy /usr/share/debootstrap/scripts/belenos
226 local arch=$1
227
228 check
229 debootstrap --arch $arch --exclude=${EXCLUDE_BELENOS} --include=${INCLUDE_DEBIAN} belenos $xtmp ${MIRROR_TRISQUEL}
230 cleanup-deb
231 tarit
232 }
233
234 unknown() {
235
236 echo "Unknown distribution. Aborting."
237 echo "Available:
238
239 CENTOS5_32 CENTOS5_64
240 CENTOS6_32 CENTOS6_64
241 CENTOS7_32 CENTOS7_64
242 SLC5_32 SLC5_64
243 SLC6_32 SLC6_64
244 SLC7_64
245 TRUSTY32 TRUSTY64
246 XENIAL32 XENIAL64
247 SQUEEZE32 SQUEEZE64
248 WHEEZY32 WHEEZY64
249 JESSIE32 JESSIE64
250 "
251 exit 99
252 }
253
254
255 # main routine
256
257 ext=tar
258 zip=cat
259 tmpdir=/var/tmp
260
261 while getopts zJd: opt ; do
262 case "$opt" in
263 d) tmpdir=$OPTARG ;;
264 z) zip="gzip -9"; ext=tar.gz ;;
265 J) zip="xz -8" ext=tar.xz ;;
266 esac
267 done
268 shift $(($OPTIND - 1))
269
270 xtmp=$(mktemp -d $tmpdir/basefiles.XXXXXXXX)
271 if [ $? -eq 1 ]; then
272 echo "mktemp failed. Aborting."
273 exit 2
274 fi
275
276 target=$1 # also the name of the output file
277
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 TRUSTY32) trusty i386 ;;
291 TRUSTY64) trusty amd64 ;;
292 BELENOS64) belenos amd64 ;;
293 XENIAL32) xenial i386 ;;
294 XENIAL64) xenial amd64 ;;
295 SQUEEZE32) squeeze i386 ;;
296 SQUEEZE64) squeeze amd64 ;;
297 WHEEZY32) wheezy i386 ;;
298 WHEEZY64) wheezy amd64 ;;
299 JESSIE32) jessie i386 ;;
300 JESSIE64) jessie amd64 ;;
301 STRETCH64) stretch amd64 ;;
302 *) unknown ;;
303 esac
304
305 # cleanup
306 rm -rf $xtmp