Merge branch 'master' into devel
[tools/build.git] / build_kiwi.sh
1 #!/bin/bash
2 run_kiwi()
3 {
4     imagetype=$(perl -I$BUILD_DIR -MBuild::Kiwi -e Build::Kiwi::show $BUILD_ROOT/$TOPDIR/SOURCES/$SPECFILE imagetype)
5     imagename=$(perl -I$BUILD_DIR -MBuild::Kiwi -e Build::Kiwi::show $BUILD_ROOT/$TOPDIR/SOURCES/$SPECFILE filename)
6     imageversion=$(perl -I$BUILD_DIR -MBuild::Kiwi -e Build::Kiwi::show $BUILD_ROOT/$TOPDIR/SOURCES/$SPECFILE version)
7     # prepare rpms as source and createrepo on the repositories
8     ln -sf $TOPDIR/SOURCES/repos $BUILD_ROOT/repos
9     cd $BUILD_ROOT/$TOPDIR/SOURCES/repos
10     for r in */* ; do
11         test -L $r && continue
12         test -d $r || continue
13         repo="$TOPDIR/SOURCES/repos/$r/"
14         # create compatibility link for old kiwi versions
15         rc="${r//:/:/}"
16         if test "$rc" != "$r" ; then
17         rl="${rc//[^\/]}"
18         rl="${rl//?/../}"
19         mkdir -p "${rc%/*}"
20         ln -s $rl$r "${rc%/*}/${rc##*/}"
21         repo="$TOPDIR/SOURCES/repos/${rc%/*}/${rc##*/}/"
22         fi
23         if test "$imagetype" != product -a "$DO_INIT" != "false" ; then
24             echo "creating repodata for $repo"
25             if chroot $BUILD_ROOT createrepo --no-database --simple-md-filenames --help >/dev/null 2>&1 ; then
26                 chroot $BUILD_ROOT createrepo --no-database --simple-md-filenames "$repo"
27             else
28                 chroot $BUILD_ROOT createrepo "$repo"
29             fi
30         fi
31     done
32     # unpack root tar
33     for t in $BUILD_ROOT/$TOPDIR/SOURCES/root.tar* ; do
34         test -f $t || continue
35         mkdir -p $BUILD_ROOT/$TOPDIR/SOURCES/root
36         chroot $BUILD_ROOT tar -C $TOPDIR/SOURCES/root -xf "$TOPDIR/SOURCES/${t##*/}"
37     done
38     # fix script permissions
39     chmod a+x $BUILD_ROOT/$TOPDIR/SOURCES/*.sh 2>/dev/null
40     # unpack tar files in image directories
41     if test -d $BUILD_ROOT/$TOPDIR/SOURCES/images ; then
42         (
43         cd $BUILD_ROOT/$TOPDIR/SOURCES/images
44         for r in */* ; do
45             test -L $r && continue
46             test -d $r || continue
47             for t in $r/root.tar* ; do
48                 test -f $t || continue
49                 mkdir -p $r/root
50                 chroot $BUILD_ROOT tar -C $TOPDIR/SOURCES/images/$r/root -xf "$TOPDIR/SOURCES/images/$r/${t##*/}"
51             done
52             # fix script permissions
53             chmod a+x $BUILD_ROOT/$TOPDIR/SOURCES/images/$r/*.sh 2>/dev/null
54             # create compatibility link for old kiwi versions
55             rc="${r//:/:/}"
56             if test "$rc" != "$r" ; then
57                 rl="${rc//[^\/]}"
58                 rl="${rl//?/../}"
59                 mkdir -p "${rc%/*}"
60                 ln -s $rl$r "${rc%/*}/${rc##*/}"
61             fi
62         done
63         )
64     fi
65     rm -f $BUILD_ROOT/$TOPDIR/SOURCES/config.xml
66     ln -s $SPECFILE $BUILD_ROOT/$TOPDIR/SOURCES/config.xml
67     if test "$imagetype" = product ; then
68         echo "running kiwi --create-instsource..."
69         # runs always as abuild user
70         mkdir -p "$BUILD_ROOT/$TOPDIR/KIWIROOT"
71         chroot "$BUILD_ROOT" chown -R abuild.abuild "$TOPDIR"
72         ver=`chroot "$BUILD_ROOT" su -c "/usr/sbin/kiwi --version | sed -n 's,.*kiwi version v\(.*\),\1,p'"`
73         if [ ${ver:0:1} == "3" ]; then
74           # old style kiwi 3 builds
75           chroot "$BUILD_ROOT" su -c "APPID=- LANG=POSIX /usr/sbin/kiwi --root $TOPDIR/KIWIROOT -v --logfile terminal -p $TOPDIR/SOURCES --instsource-local --create-instsource $TOPDIR/SOURCES" - abuild < /dev/null && BUILD_SUCCEEDED=true
76           if [ ${ver:2:2} == "01" ]; then
77             ## This block is obsolete with current kiwi versions, only needed for kiwi 3.01 version
78             for i in $BUILD_ROOT/$TOPDIR/KIWIROOT/main/* ; do
79                 test -d "$i" || continue
80                 n="${i##*/}"
81                 test "$n" = scripts && continue
82                 test "$n" != "${n%0}" && continue
83                 chroot $BUILD_ROOT su -c "suse-isolinux $TOPDIR/KIWIROOT/main/$n $TOPDIR/KIWI/$n.iso" - $BUILD_USER
84             done
85           fi
86         else
87           if [ ${ver:0:1} == "4" -a ${ver:2:2} -lt 90 ]; then
88             # broken kiwi version, not accepting verbose level
89             chroot "$BUILD_ROOT" su -c "APPID=- LANG=POSIX /usr/sbin/kiwi --root $TOPDIR/KIWIROOT -v -v --logfile terminal -p $TOPDIR/SOURCES --create-instsource $TOPDIR/SOURCES" - abuild < /dev/null && BUILD_SUCCEEDED=true
90           else
91             # current default
92             chroot "$BUILD_ROOT" su -c "APPID=- LANG=POSIX /usr/sbin/kiwi --root $TOPDIR/KIWIROOT -v 2 --logfile terminal -p $TOPDIR/SOURCES --create-instsource $TOPDIR/SOURCES" - abuild < /dev/null && BUILD_SUCCEEDED=true
93           fi
94         fi
95
96         # move created product to correct destination
97         for i in $BUILD_ROOT/$TOPDIR/KIWIROOT/main/* ; do
98             test -e "$i" || continue
99             f=${i##*/}
100             case $f in
101                 *.iso) mv $i $BUILD_ROOT/$TOPDIR/KIWI/. ;;
102                 *.packages) mv $i $BUILD_ROOT/$TOPDIR/OTHER/. ;;
103                 scripts) ;;
104                 *0) ;;
105                 *) test -d $i && mv $i $BUILD_ROOT/$TOPDIR/KIWI/. ;;
106             esac
107         done
108     else
109         BUILD_SUCCEEDED=true
110         if [ -z "$RUNNING_IN_VM" ]; then
111             # NOTE: this must be done with the outer system, because it loads the dm-mod kernel modules, which needs to fit to the kernel.
112             echo "starting device mapper for kiwi..."
113             [ -x /etc/init.d/boot.device-mapper ] && /etc/init.d/boot.device-mapper start
114         fi
115         for imgtype in $imagetype ; do
116             echo "running kiwi --prepare for $imgtype..."
117             # Do not use $BUILD_USER here, since we always need root permissions
118             if chroot $BUILD_ROOT su -c "cd $TOPDIR/SOURCES && kiwi --prepare $TOPDIR/SOURCES --logfile terminal --root $TOPDIR/KIWIROOT-$imgtype $KIWI_PARAMETERS" - root < /dev/null ; then
119                 echo "running kiwi --create for $imgtype..."
120                 mkdir -p $BUILD_ROOT/$TOPDIR/KIWI-$imgtype
121                 chroot $BUILD_ROOT su -c "cd $TOPDIR/SOURCES && kiwi --create $TOPDIR/KIWIROOT-$imgtype --logfile terminal --type $imgtype -d $TOPDIR/KIWI-$imgtype $KIWI_PARAMETERS" - root < /dev/null || cleanup_and_exit 1
122             else
123                 cleanup_and_exit 1
124             fi
125         done
126
127         # create tar.gz of images, in case it makes sense
128         imagearch=`uname -m`
129         buildnum=""
130           if test -n "$RELEASE"; then
131             buildnum="-Build$RELEASE"
132         fi
133         imageout="$imagename.$imagearch-$imageversion"
134         for imgtype in $imagetype ; do
135             case "$imgtype" in
136                 oem)
137                     cat > $BUILD_ROOT/kiwi_post.sh << EOF
138 echo "compressing oem images... "
139 cd /$TOPDIR/KIWI-oem
140 # do not store compressed file _and_ uncompressed one
141 [ -e "$imageout.gz" ] && rm -f "$imageout"
142 if [ -e "$imageout.iso" ]; then
143         echo "take iso file and create sha256..."
144         mv "$imageout.iso" "/$TOPDIR/KIWI/$imageout$buildnum.iso"
145         pushd /$TOPDIR/KIWI
146         if [ -x /usr/bin/sha256sum ]; then
147            /usr/bin/sha256sum "$imageout$buildnum.iso" > "$imageout$buildnum.iso.sha256"
148         fi
149         popd
150 fi
151 if [ -e "$imageout.install.iso" ]; then
152         echo "take install.iso file and create sha256..."
153         mv "$imageout.install.iso" "/$TOPDIR/KIWI/$imageout$buildnum.install.iso"
154         pushd /$TOPDIR/KIWI
155         if [ -x /usr/bin/sha256sum ]; then
156            /usr/bin/sha256sum "$imageout$buildnum.install.iso" > "$imageout$buildnum.install.iso.sha256"
157         fi
158         popd
159 fi
160 if [ -e "$imageout.qcow2" ]; then
161         mv "$imageout.qcow2" "/$TOPDIR/KIWI/$imageout$buildnum.qcow2"
162         pushd /$TOPDIR/KIWI
163         if [ -x /usr/bin/sha256sum ]; then
164             echo "Create sha256 file..."
165             /usr/bin/sha256sum "$imageout$buildnum.qcow2" > "$imageout$buildnum.qcow2.sha256"
166         fi
167         popd
168 fi
169 if [ -e "$imageout.raw.install.raw" ]; then
170         compress_tool="bzip2"
171         compress_suffix="bz2"
172         if [ -x /usr/bin/xz ]; then
173             # take xz to get support for sparse files
174             compress_tool="xz -2"
175             compress_suffix="xz"
176         fi
177         mv "$imageout.raw.install.raw" "/$TOPDIR/KIWI/$imageout$buildnum.raw.install.raw"
178         pushd /$TOPDIR/KIWI
179         echo "\$compress_tool raw.install.raw file..."
180         \$compress_tool "$imageout$buildnum.raw.install.raw"
181         if [ -x /usr/bin/sha256sum ]; then
182             echo "Create sha256 file..."
183             /usr/bin/sha256sum "$imageout$buildnum.raw.install.raw.\${compress_suffix}" > "$imageout$buildnum.raw.install.raw.\${compress_suffix}.sha256"
184         fi
185         popd
186 fi
187 if [ -e "$imageout.raw" ]; then
188         compress_tool="bzip2"
189         compress_suffix="bz2"
190         if [ -x /usr/bin/xz ]; then
191             # take xz to get support for sparse files
192             compress_tool="xz -2"
193             compress_suffix="xz"
194         fi
195         mv "$imageout.raw" "/$TOPDIR/KIWI/$imageout$buildnum.raw"
196         pushd /$TOPDIR/KIWI
197         echo "\$compress_tool raw file..."
198         \$compress_tool "$imageout$buildnum.raw"
199         if [ -x /usr/bin/sha256sum ]; then
200             echo "Create sha256 file..."
201             /usr/bin/sha256sum "$imageout$buildnum.raw.\${compress_suffix}" > "$imageout$buildnum.raw.\${compress_suffix}.sha256"
202         fi
203         popd
204 fi
205
206 tar cvjfS "/$TOPDIR/KIWI/$imageout$buildnum-raw.tar.bz2" \
207         --exclude="$imageout.iso" --exclude="$imageout.raw" --exclude="$imageout.qcow2" *
208 cd /$TOPDIR/KIWI
209 if [ -x /usr/bin/sha256sum ]; then
210    /usr/bin/sha256sum "$imageout$buildnum-raw.tar.bz2" > "$imageout$buildnum-raw.tar.bz2.sha256"
211 fi
212 EOF
213                     ;;
214                 vmx)
215                     cat > $BUILD_ROOT/kiwi_post.sh << EOF
216 echo "compressing vmx images... "
217 cd /$TOPDIR/KIWI-vmx
218 for suffix in "ovf" "qcow2" "ova"; do
219   if [ -e "$imageout.\$suffix" ]; then
220         mv "$imageout.\$suffix" "/$TOPDIR/KIWI/$imageout$buildnum.\$suffix"
221         pushd /$TOPDIR/KIWI
222         if [ -x /usr/bin/sha256sum ]; then
223             echo "Create sha256 \$suffix file..."
224             /usr/bin/sha256sum "$imageout$buildnum.\$suffix" > "$imageout$buildnum.\$suffix.sha256"
225         fi
226         popd
227   fi
228 done
229 # This option has a number of format parameters
230 VMXFILES=""
231 SHAFILES=""
232 for i in "$imageout.vmx" "$imageout.vmdk" "$imageout-disk*.vmdk"; do
233         test -e \$i && VMXFILES="\$VMXFILES \$i"
234 done
235 # take raw files as fallback
236 if [ -n "\$VMXFILES" ]; then
237         tar cvjfS "/$TOPDIR/KIWI/$imageout$buildnum-vmx.tar.bz2" \$VMXFILES
238         SHAFILES="\$SHAFILES $imageout$buildnum-vmx.tar.bz2"
239 elif [ -e  "$imageout.raw" ]; then
240         compress_tool="bzip2"
241         compress_suffix="bz2"
242         if [ -x /usr/bin/xz ]; then
243             # take xz to get support for sparse files
244             compress_tool="xz -2"
245             compress_suffix="xz"
246         fi
247         mv "$imageout.raw" "/$TOPDIR/KIWI/$imageout$buildnum-vmx.raw"
248         pushd /$TOPDIR/KIWI
249         echo "\$compress_tool raw file..."
250         \$compress_tool "$imageout$buildnum-vmx.raw"
251         SHAFILES="\$SHAFILES $imageout$buildnum-vmx.raw.\${compress_suffix}"
252         popd
253 fi
254 if [ -e "$imageout.xenconfig" ]; then
255         tar cvjfS "/$TOPDIR/KIWI/$imageout$buildnum-vmx.tar.bz2" $imageout.xenconfig $imageout.raw initrd-*
256         SHAFILES="\$SHAFILES $imageout$buildnum-vmx.tar.bz2"
257 fi
258 # FIXME: do we need a single .raw file in any case ?
259
260 cd /$TOPDIR/KIWI
261 if [ -n "\$SHAFILES" -a -x /usr/bin/sha256sum ]; then
262         for i in \$SHAFILES; do
263                 echo "Create sha256 file..."
264                 /usr/bin/sha256sum "\$i" > "\$i.sha256"
265         done
266 fi
267 EOF
268                     ;;
269                 xen)
270                     cat > $BUILD_ROOT/kiwi_post.sh << EOF
271 echo "compressing xen images... "
272 cd /$TOPDIR/KIWI-xen
273 # do not store compressed file _and_ uncompressed one
274 [ -e "$imageout.gz" ] && rm -f "$imageout"
275 tar cvjfS "/$TOPDIR/KIWI/$imageout$buildnum-xen.tar.bz2" \
276         `grep ^kernel $imageout.xenconfig | cut -d'"'  -f2` \
277         `grep ^ramdisk $imageout.xenconfig | cut -d'"'  -f2` \
278         initrd-* \
279         "$imageout.xenconfig" \
280         "$imageout"
281 if [ -x /usr/bin/sha256sum ]; then
282    echo "Create sha256 file..."
283    cd $TOPDIR/KIWI
284    /usr/bin/sha256sum "$imageout$buildnum-xen.tar.bz2" > "$imageout$buildnum-xen.tar.bz2.sha256"
285 fi
286 EOF
287                     ;;
288                 pxe)
289                     cat > $BUILD_ROOT/kiwi_post.sh << EOF
290 echo "compressing pxe images... "
291 cd /$TOPDIR/KIWI-pxe
292 # do not store compressed file _and_ uncompressed one
293 [ -e "$imageout.gz" ] && rm -f "$imageout"
294 tar cvjfS "/$TOPDIR/KIWI/$imageout$buildnum-pxe.tar.bz2" ${imageout}* initrd-*
295 if [ -x /usr/bin/sha256sum ]; then
296    echo "Create sha256 file..."
297    cd $TOPDIR/KIWI
298    /usr/bin/sha256sum "$imageout$buildnum-pxe.tar.bz2" > "$imageout$buildnum-pxe.tar.bz2.sha256"
299 fi
300 EOF
301                     ;;
302                 iso)
303                     cat > $BUILD_ROOT/kiwi_post.sh << EOF
304 cd /$TOPDIR/KIWI-iso
305 for i in *.iso; do
306         mv "\$i" "/$TOPDIR/KIWI/\${i%.iso}$buildnum.iso"
307 done
308 if [ -x /usr/bin/sha256sum ]; then
309    echo "creating sha256 sum for iso images... "
310    cd $TOPDIR/KIWI
311    for i in *.iso; do
312         /usr/bin/sha256sum "\$i" > "\$i.sha256"
313    done
314 fi
315 EOF
316                     ;;
317                 tbz)
318                     cat > $BUILD_ROOT/kiwi_post.sh << EOF
319 cd /$TOPDIR/KIWI-tbz
320 for i in *.tbz; do
321         file=\$(readlink -f "\$i")
322         [ -z "\$file" ] && echo readlink failed for $i
323         mv "\$file" "/$TOPDIR/KIWI/\${i%.tbz}$buildnum.tbz"
324 done
325 if [ -x /usr/bin/sha256sum ]; then
326    echo "creating sha256 sum for tar balls... "
327    cd $TOPDIR/KIWI
328    for i in *.tbz; do
329         /usr/bin/sha256sum "\$i" > "\$i.sha256"
330    done
331 fi
332 EOF
333                     ;;
334                 *)
335                     cat > $BUILD_ROOT/kiwi_post.sh << EOF
336 echo "compressing unkown images... "
337 cd /$TOPDIR/KIWI-$imgtype
338 # do not store compressed file _and_ uncompressed one
339 [ -e "$imageout.gz" ] && rm -f "$imageout"
340 tar cvjfS "/$TOPDIR/KIWI/$imageout$buildnum-$imgtype.tar.bz2" *
341 if [ -x /usr/bin/sha256sum ]; then
342    echo "Create sha256 file..."
343    cd /$TOPDIR/KIWI
344    /usr/bin/sha256sum "$imageout$buildnum-$imgtype.tar.bz2" > "$imageout$buildnum-$imgtype.tar.bz2.sha256"
345 fi
346 EOF
347                     ;;
348             esac
349             cat >> $BUILD_ROOT/kiwi_post.sh << EOF
350 cd /$TOPDIR/KIWI-$imgtype
351 if [ -e "$imageout.packages" ]; then
352    echo "Found kiwi package list file, exporting as well..."
353    cp "$imageout.packages" "/$TOPDIR/OTHER/$imageout$buildnum-$imgtype.packages"
354 fi
355 if [ -e "$imageout.verified" ]; then
356    echo "Found rpm verification report, exporting as well..."
357    cp "$imageout.verified" "/$TOPDIR/OTHER/$imageout$buildnum-$imgtype.verified"
358 fi
359 EOF
360             chroot $BUILD_ROOT su -c "sh -e /kiwi_post.sh" || cleanup_and_exit 1
361             rm -f $BUILD_ROOT/kiwi_post.sh
362         done
363     fi
364     # Hook for running post kiwi build scripts like QA scripts if installed
365     if [ -x $BUILD_ROOT/usr/lib/build/kiwi_post_run ]; then
366         chroot $BUILD_ROOT su -c /usr/lib/build/kiwi_post_run || cleanup_and_exit 1
367     fi
368 }