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