The ovf file is useless alone, package it as part of the vmdk/vmx tar ball.
[platform/upstream/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 ; then
24         echo "creating repodata for $repo"
25         chroot $BUILD_ROOT createrepo "$repo"
26         fi
27     done
28     # unpack root tar
29     for t in $BUILD_ROOT/$TOPDIR/SOURCES/root.tar* ; do
30         test -f $t || continue
31         mkdir -p $BUILD_ROOT/$TOPDIR/SOURCES/root
32         chroot $BUILD_ROOT tar -C $TOPDIR/SOURCES/root -xf "$TOPDIR/SOURCES/${t##*/}"
33     done
34     # fix script permissions
35     chmod a+x $BUILD_ROOT/$TOPDIR/SOURCES/*.sh 2>/dev/null
36     # unpack tar files in image directories
37     if test -d $BUILD_ROOT/$TOPDIR/SOURCES/images ; then
38         (
39         cd $BUILD_ROOT/$TOPDIR/SOURCES/images
40         for r in */* ; do
41             test -L $r && continue
42             test -d $r || continue
43             for t in $r/root.tar* ; do
44                 test -f $t || continue
45                 mkdir -p $r/root
46                 chroot $BUILD_ROOT tar -C $TOPDIR/SOURCES/images/$r/root -xf "$TOPDIR/SOURCES/images/$r/${t##*/}"
47             done
48             # fix script permissions
49             chmod a+x $BUILD_ROOT/$TOPDIR/SOURCES/images/$r/*.sh 2>/dev/null
50             # create compatibility link for old kiwi versions
51             rc="${r//:/:/}"
52             if test "$rc" != "$r" ; then
53                 rl="${rc//[^\/]}"
54                 rl="${rl//?/../}"
55                 mkdir -p "${rc%/*}"
56                 ln -s $rl$r "${rc%/*}/${rc##*/}"
57             fi
58         done
59         )
60     fi
61     rm -f $BUILD_ROOT/$TOPDIR/SOURCES/config.xml
62     ln -s $SPECFILE $BUILD_ROOT/$TOPDIR/SOURCES/config.xml
63     chroot $BUILD_ROOT su -c "kiwi --version" -
64     if test "$imagetype" = product ; then
65         echo "running kiwi --create-instsource..."
66         # runs always as abuild user
67         mkdir -p "$BUILD_ROOT/$TOPDIR/KIWIROOT"
68         chroot "$BUILD_ROOT" chown -R abuild.abuild "$TOPDIR"
69         # --instsource-local is only needed for openSUSE 11.1 and SLE 11 SP0 kiwi.
70         chroot "$BUILD_ROOT" su -c "APPID=- LANG=POSIX /usr/sbin/kiwi --root $TOPDIR/KIWIROOT -v -v --logfile terminal -p $TOPDIR/SOURCES --instsource-local --create-instsource $TOPDIR/SOURCES" - abuild < /dev/null && BUILD_SUCCEEDED=true
71 ### This block is obsolete with current kiwi versions, only needed for kiwi 3.01 version
72 #            for i in $BUILD_ROOT/$TOPDIR/KIWIROOT/main/* ; do
73 #                test -d "$i" || continue
74 #                n="${i##*/}"
75 #                test "$n" = scripts && continue
76 #                test "$n" != "${n%0}" && continue
77 #                chroot $BUILD_ROOT su -c "suse-isolinux $TOPDIR/KIWIROOT/main/$n $TOPDIR/KIWI/$n.iso" - $BUILD_USER
78 #            done
79
80         # move created product to correct destination
81         for i in $BUILD_ROOT/$TOPDIR/KIWIROOT/main/* ; do
82             test -e "$i" || continue
83             f=${i##*/}
84             case $f in
85                 *.iso) mv $i $BUILD_ROOT/$TOPDIR/KIWI/. ;;
86                 scripts) ;;
87                 *0) ;;
88                 *) test -d $i && mv $i $BUILD_ROOT/$TOPDIR/KIWI/. ;;
89             esac
90         done
91     else
92         BUILD_SUCCEEDED=true
93         if [ -z "$RUNNING_IN_VM" ]; then
94             # NOTE: this must be done with the outer system, because it loads the dm-mod kernel modules, which needs to fit to the kernel.
95             echo "starting device mapper for kiwi..."
96             [ -x /etc/init.d/boot.device-mapper ] && /etc/init.d/boot.device-mapper start
97         fi
98         for imgtype in $imagetype ; do
99             echo "running kiwi --prepare for $imgtype..."
100             # Do not use $BUILD_USER here, since we always need root permissions
101             if chroot $BUILD_ROOT su -c "cd $TOPDIR/SOURCES && kiwi --prepare $TOPDIR/SOURCES --logfile terminal --root $TOPDIR/KIWIROOT-$imgtype" - root < /dev/null ; then
102                 echo "running kiwi --create for $imgtype..."
103                 mkdir -p $BUILD_ROOT/$TOPDIR/KIWI-$imgtype
104                 chroot $BUILD_ROOT su -c "cd $TOPDIR/SOURCES && kiwi --create $TOPDIR/KIWIROOT-$imgtype --logfile terminal --type $imgtype -d $TOPDIR/KIWI-$imgtype" - root < /dev/null || cleanup_and_exit 1
105             else
106                 cleanup_and_exit 1
107             fi
108         done
109
110         # create tar.gz of images, in case it makes sense
111         imagearch=`uname -m`
112         buildnum=""
113           if test -n "$RELEASE"; then
114             buildnum="-Build$RELEASE"
115         fi
116         imageout="$imagename.$imagearch-$imageversion"
117         for imgtype in $imagetype ; do
118             case "$imgtype" in
119                 oem)
120                     cat > $BUILD_ROOT/kiwi_post.sh << EOF
121 echo "compressing oem images... "
122 cd /$TOPDIR/KIWI-oem
123 if [ -e "$imageout.iso" ]; then
124         echo "take iso file and create sha256..."
125         mv "$imageout.iso" "/$TOPDIR/KIWI/$imageout$buildnum.iso"
126         pushd /$TOPDIR/KIWI
127         if [ -x /usr/bin/sha256sum ]; then
128            /usr/bin/sha256sum "$imageout$buildnum.iso" > "$imageout$buildnum.iso.sha256"
129         fi
130         popd
131 fi
132 if [ -e "$imageout.raw" ]; then
133         mv "$imageout.raw" "/$TOPDIR/KIWI/$imageout$buildnum.raw"
134         pushd /$TOPDIR/KIWI
135         echo "bzip2 raw file..."
136         bzip2 "$imageout$buildnum.raw"
137         if [ -x /usr/bin/sha256sum ]; then
138             echo "Create sha256 file..."
139             /usr/bin/sha256sum "$imageout$buildnum.raw.bz2" > "$imageout$buildnum.raw.bz2.sha256"
140         fi
141         popd
142 fi
143
144 tar cvjfS "/$TOPDIR/KIWI/$imageout$buildnum-raw.tar.bz2" \
145         --exclude="$imageout.iso" --exclude="$imageout.raw" *
146 cd /$TOPDIR/KIWI
147 if [ -x /usr/bin/sha256sum ]; then
148    /usr/bin/sha256sum "$imageout$buildnum-raw.tar.bz2" > "$imageout$buildnum-raw.tar.bz2.sha256"
149 fi
150 EOF
151                     ;;
152                 vmx)
153                     cat > $BUILD_ROOT/kiwi_post.sh << EOF
154 echo "compressing vmx images... "
155 cd /$TOPDIR/KIWI-vmx
156 # This option has a number of format parameters
157 VMXFILES=""
158 SHAFILES=""
159 for i in "$imageout.vmx" "$imageout.vmdk" "$imageout-disk*.vmdk" "$imageout.ovf"; do
160         ls \$i >& /dev/null && VMXFILES="\$VMXFILES \$i"
161 done
162 if [ -n "\$VMXFILES" ]; then
163         tar cvjfS "/$TOPDIR/KIWI/$imageout$buildnum-vmx.tar.bz2" \$VMXFILES
164         SHAFILES="\$SHAFILES $imageout$buildnum-vmx.tar.bz2"
165 fi
166
167 if [ -e "$imageout.xenconfig" ]; then
168         tar cvjfS "/$TOPDIR/KIWI/$imageout$buildnum-vmx.tar.bz2" $imageout.xenconfig $imageout.raw initrd-*
169         SHAFILES="\$SHAFILES $imageout$buildnum-vmx.tar.bz2"
170 fi
171 # FIXME: do we need a single .raw file in any case ?
172
173 cd /$TOPDIR/KIWI
174 if [ -n "\$SHAFILES" -a -x /usr/bin/sha256sum ]; then
175         for i in \$SHAFILES; do
176                 echo "Create sha256 file..."
177                 /usr/bin/sha256sum "\$i" > "\$i.sha256"
178         done
179 fi
180 EOF
181                     ;;
182                 xen)
183                     cat > $BUILD_ROOT/kiwi_post.sh << EOF
184 echo "compressing xen images... "
185 cd /$TOPDIR/KIWI-xen
186 tar cvjfS "/$TOPDIR/KIWI/$imageout$buildnum-xen.tar.bz2" \
187         `grep ^kernel $imageout.xenconfig | cut -d'"'  -f2` \
188         `grep ^ramdisk $imageout.xenconfig | cut -d'"'  -f2` \
189         initrd-* \
190         "$imageout.xenconfig" \
191         "$imageout"
192 if [ -x /usr/bin/sha256sum ]; then
193    echo "Create sha256 file..."
194    cd $TOPDIR/KIWI
195    /usr/bin/sha256sum "$imageout$buildnum-xen.tar.bz2" > "$imageout$buildnum-xen.tar.bz2.sha256"
196 fi
197 EOF
198                     ;;
199                 pxe)
200                     cat > $BUILD_ROOT/kiwi_post.sh << EOF
201 echo "compressing pxe images... "
202 cd /$TOPDIR/KIWI-pxe
203 tar cvjfS "/$TOPDIR/KIWI/$imageout$buildnum-pxe.tar.bz2" ${imageout}* initrd-*
204 if [ -x /usr/bin/sha256sum ]; then
205    echo "Create sha256 file..."
206    cd $TOPDIR/KIWI
207    /usr/bin/sha256sum "$imageout$buildnum-pxe.tar.bz2" > "$imageout$buildnum-pxe.tar.bz2.sha256"
208 fi
209 EOF
210                     ;;
211                 iso)
212                     cat > $BUILD_ROOT/kiwi_post.sh << EOF
213 cd /$TOPDIR/KIWI-iso
214 for i in *.iso; do
215         mv "\$i" "/$TOPDIR/KIWI/\${i%.iso}$buildnum.iso"
216 done
217 if [ -x /usr/bin/sha256sum ]; then
218    echo "creating sha256 sum for iso images... "
219    cd $TOPDIR/KIWI
220    for i in *.iso; do
221         /usr/bin/sha256sum "\$i" > "\$i.sha256"
222    done
223 fi
224 EOF
225                     ;;
226                 *)
227                     cat > $BUILD_ROOT/kiwi_post.sh << EOF
228 echo "compressing unkown images... "
229 cd /$TOPDIR/KIWI-$imgtype
230 tar cvjfS "/$TOPDIR/KIWI/$imageout$buildnum-$imgtype.tar.bz2" *
231 if [ -x /usr/bin/sha256sum ]; then
232    echo "Create sha256 file..."
233    cd /$TOPDIR/KIWI
234    /usr/bin/sha256sum "$imageout$buildnum-$imgtype.tar.bz2" > "$imageout$buildnum-$imgtype.tar.bz2.sha256"
235 fi
236 EOF
237                     ;;
238             esac
239             chroot $BUILD_ROOT su -c "sh -x -e /kiwi_post.sh" || cleanup_and_exit 1
240             rm -f $BUILD_ROOT/kiwi_post.sh
241         done
242     fi
243 }