force enable DO_CUMULATE
[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 -a "$DO_INIT" != "false" ; then
24             echo "creating repodata for $repo"
25             if chroot $BUILD_ROOT createrepo --simple-md-filenames --help >/dev/null 2>&1 ; then
26                 chroot $BUILD_ROOT createrepo --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                 scripts) ;;
103                 *0) ;;
104                 *) test -d $i && mv $i $BUILD_ROOT/$TOPDIR/KIWI/. ;;
105             esac
106         done
107     else
108         BUILD_SUCCEEDED=true
109         if [ -z "$RUNNING_IN_VM" ]; then
110             # NOTE: this must be done with the outer system, because it loads the dm-mod kernel modules, which needs to fit to the kernel.
111             echo "starting device mapper for kiwi..."
112             [ -x /etc/init.d/boot.device-mapper ] && /etc/init.d/boot.device-mapper start
113         fi
114         for imgtype in $imagetype ; do
115             echo "running kiwi --prepare for $imgtype..."
116             # Do not use $BUILD_USER here, since we always need root permissions
117             if chroot $BUILD_ROOT su -c "cd $TOPDIR/SOURCES && kiwi --prepare $TOPDIR/SOURCES --logfile terminal --root $TOPDIR/KIWIROOT-$imgtype" - root < /dev/null ; then
118                 echo "running kiwi --create for $imgtype..."
119                 mkdir -p $BUILD_ROOT/$TOPDIR/KIWI-$imgtype
120                 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
121             else
122                 cleanup_and_exit 1
123             fi
124         done
125
126         # create tar.gz of images, in case it makes sense
127         imagearch=`uname -m`
128         buildnum=""
129           if test -n "$RELEASE"; then
130             buildnum="-Build$RELEASE"
131         fi
132         imageout="$imagename.$imagearch-$imageversion"
133         for imgtype in $imagetype ; do
134             case "$imgtype" in
135                 oem)
136                     cat > $BUILD_ROOT/kiwi_post.sh << EOF
137 echo "compressing oem images... "
138 cd /$TOPDIR/KIWI-oem
139 # do not store compressed file _and_ uncompressed one
140 [ -e "$imageout.gz" ] && rm -f "$imageout"
141 if [ -e "$imageout.iso" ]; then
142         echo "take iso file and create sha256..."
143         mv "$imageout.iso" "/$TOPDIR/KIWI/$imageout$buildnum.iso"
144         pushd /$TOPDIR/KIWI
145         if [ -x /usr/bin/sha256sum ]; then
146            /usr/bin/sha256sum "$imageout$buildnum.iso" > "$imageout$buildnum.iso.sha256"
147         fi
148         popd
149 fi
150 if [ -e "$imageout.qcow2" ]; then
151         mv "$imageout.qcow2" "/$TOPDIR/KIWI/$imageout$buildnum.qcow2"
152         pushd /$TOPDIR/KIWI
153         if [ -x /usr/bin/sha256sum ]; then
154             echo "Create sha256 file..."
155             /usr/bin/sha256sum "$imageout$buildnum.qcow2" > "$imageout$buildnum.qcow2.sha256"
156         fi
157         popd
158 fi
159 if [ -e "$imageout.raw" ]; then
160         compress_tool="bzip2"
161         compress_suffix="bz2"
162         if [ -x /usr/bin/xz ]; then
163             # take xz to get support for sparse files
164             compress_tool="xz -2"
165             compress_suffix="xz"
166         fi
167         mv "$imageout.raw" "/$TOPDIR/KIWI/$imageout$buildnum.raw"
168         pushd /$TOPDIR/KIWI
169         echo "\$compress_tool raw file..."
170         \$compress_tool "$imageout$buildnum.raw"
171         if [ -x /usr/bin/sha256sum ]; then
172             echo "Create sha256 file..."
173             /usr/bin/sha256sum "$imageout$buildnum.raw.\${compress_suffix}" > "$imageout$buildnum.raw.\${compress_suffix}.sha256"
174         fi
175         popd
176 fi
177
178 tar cvjfS "/$TOPDIR/KIWI/$imageout$buildnum-raw.tar.bz2" \
179         --exclude="$imageout.iso" --exclude="$imageout.raw" --exclude="$imageout.qcow2" *
180 cd /$TOPDIR/KIWI
181 if [ -x /usr/bin/sha256sum ]; then
182    /usr/bin/sha256sum "$imageout$buildnum-raw.tar.bz2" > "$imageout$buildnum-raw.tar.bz2.sha256"
183 fi
184 EOF
185                     ;;
186                 vmx)
187                     cat > $BUILD_ROOT/kiwi_post.sh << EOF
188 echo "compressing vmx images... "
189 cd /$TOPDIR/KIWI-vmx
190 for suffix in "ovf" "qcow2"; do
191   if [ -e "$imageout.\$suffix" ]; then
192         mv "$imageout.\$suffix" "/$TOPDIR/KIWI/$imageout$buildnum.\$suffix"
193         pushd /$TOPDIR/KIWI
194         if [ -x /usr/bin/sha256sum ]; then
195             echo "Create sha256 \$suffix file..."
196             /usr/bin/sha256sum "$imageout$buildnum.\$suffix" > "$imageout$buildnum.\$suffix.sha256"
197         fi
198         popd
199   fi
200 done
201 # This option has a number of format parameters
202 VMXFILES=""
203 SHAFILES=""
204 for i in "$imageout.vmx" "$imageout.vmdk" "$imageout-disk*.vmdk"; do
205         test -e \$i && VMXFILES="\$VMXFILES \$i"
206 done
207 # take raw files as fallback
208 if [ -z "\$VMXFILES" ]; then
209         test -e "$imageout.raw" && VMXFILES="$imageout.raw"
210 fi
211 if [ -n "\$VMXFILES" ]; then
212         tar cvjfS "/$TOPDIR/KIWI/$imageout$buildnum-vmx.tar.bz2" \$VMXFILES
213         SHAFILES="\$SHAFILES $imageout$buildnum-vmx.tar.bz2"
214 fi
215 if [ -e "$imageout.xenconfig" ]; then
216         tar cvjfS "/$TOPDIR/KIWI/$imageout$buildnum-vmx.tar.bz2" $imageout.xenconfig $imageout.raw initrd-*
217         SHAFILES="\$SHAFILES $imageout$buildnum-vmx.tar.bz2"
218 fi
219 # FIXME: do we need a single .raw file in any case ?
220
221 cd /$TOPDIR/KIWI
222 if [ -n "\$SHAFILES" -a -x /usr/bin/sha256sum ]; then
223         for i in \$SHAFILES; do
224                 echo "Create sha256 file..."
225                 /usr/bin/sha256sum "\$i" > "\$i.sha256"
226         done
227 fi
228 EOF
229                     ;;
230                 xen)
231                     cat > $BUILD_ROOT/kiwi_post.sh << EOF
232 echo "compressing xen images... "
233 cd /$TOPDIR/KIWI-xen
234 # do not store compressed file _and_ uncompressed one
235 [ -e "$imageout.gz" ] && rm -f "$imageout"
236 tar cvjfS "/$TOPDIR/KIWI/$imageout$buildnum-xen.tar.bz2" \
237         `grep ^kernel $imageout.xenconfig | cut -d'"'  -f2` \
238         `grep ^ramdisk $imageout.xenconfig | cut -d'"'  -f2` \
239         initrd-* \
240         "$imageout.xenconfig" \
241         "$imageout"
242 if [ -x /usr/bin/sha256sum ]; then
243    echo "Create sha256 file..."
244    cd $TOPDIR/KIWI
245    /usr/bin/sha256sum "$imageout$buildnum-xen.tar.bz2" > "$imageout$buildnum-xen.tar.bz2.sha256"
246 fi
247 EOF
248                     ;;
249                 pxe)
250                     cat > $BUILD_ROOT/kiwi_post.sh << EOF
251 echo "compressing pxe images... "
252 cd /$TOPDIR/KIWI-pxe
253 # do not store compressed file _and_ uncompressed one
254 [ -e "$imageout.gz" ] && rm -f "$imageout"
255 tar cvjfS "/$TOPDIR/KIWI/$imageout$buildnum-pxe.tar.bz2" ${imageout}* initrd-*
256 if [ -x /usr/bin/sha256sum ]; then
257    echo "Create sha256 file..."
258    cd $TOPDIR/KIWI
259    /usr/bin/sha256sum "$imageout$buildnum-pxe.tar.bz2" > "$imageout$buildnum-pxe.tar.bz2.sha256"
260 fi
261 EOF
262                     ;;
263                 iso)
264                     cat > $BUILD_ROOT/kiwi_post.sh << EOF
265 cd /$TOPDIR/KIWI-iso
266 for i in *.iso; do
267         mv "\$i" "/$TOPDIR/KIWI/\${i%.iso}$buildnum.iso"
268 done
269 if [ -x /usr/bin/sha256sum ]; then
270    echo "creating sha256 sum for iso images... "
271    cd $TOPDIR/KIWI
272    for i in *.iso; do
273         /usr/bin/sha256sum "\$i" > "\$i.sha256"
274    done
275 fi
276 EOF
277                     ;;
278                 tbz)
279                     cat > $BUILD_ROOT/kiwi_post.sh << EOF
280 cd /$TOPDIR/KIWI-tbz
281 for i in *.tbz; do
282         file=\$(readlink -f "\$i")
283         [ -z "\$file" ] && echo readlink failed for $i
284         mv "\$file" "/$TOPDIR/KIWI/\${i%.tbz}$buildnum.tbz"
285 done
286 if [ -x /usr/bin/sha256sum ]; then
287    echo "creating sha256 sum for tar balls... "
288    cd $TOPDIR/KIWI
289    for i in *.tbz; do
290         /usr/bin/sha256sum "\$i" > "\$i.sha256"
291    done
292 fi
293 EOF
294                     ;;
295                 *)
296                     cat > $BUILD_ROOT/kiwi_post.sh << EOF
297 echo "compressing unkown images... "
298 cd /$TOPDIR/KIWI-$imgtype
299 # do not store compressed file _and_ uncompressed one
300 [ -e "$imageout.gz" ] && rm -f "$imageout"
301 tar cvjfS "/$TOPDIR/KIWI/$imageout$buildnum-$imgtype.tar.bz2" *
302 if [ -x /usr/bin/sha256sum ]; then
303    echo "Create sha256 file..."
304    cd /$TOPDIR/KIWI
305    /usr/bin/sha256sum "$imageout$buildnum-$imgtype.tar.bz2" > "$imageout$buildnum-$imgtype.tar.bz2.sha256"
306 fi
307 EOF
308                     ;;
309             esac
310             chroot $BUILD_ROOT su -c "sh -e /kiwi_post.sh" || cleanup_and_exit 1
311             rm -f $BUILD_ROOT/kiwi_post.sh
312         done
313     fi
314     # Hook for running post kiwi build scripts like QA scripts if installed
315     if [ -x $BUILD_ROOT/usr/lib/build/kiwi_post_run ]; then
316         chroot $BUILD_ROOT su -c /usr/lib/build/kiwi_post_run || cleanup_and_exit 1
317     fi
318 }