- do not specify verbose level on product build, some kiwi versions are buggy in...
[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             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     chroot $BUILD_ROOT su -c "kiwi --version" -
68     if test "$imagetype" = product ; then
69         echo "running kiwi --create-instsource..."
70         # runs always as abuild user
71         mkdir -p "$BUILD_ROOT/$TOPDIR/KIWIROOT"
72         chroot "$BUILD_ROOT" chown -R abuild.abuild "$TOPDIR"
73         ver=`chroot "$BUILD_ROOT" su -c "/usr/sbin/kiwi --version | sed -n 's,.*kiwi version v\(.*\),\1,p'"`
74         if [ ${ver:0:1} == "3" ]; then
75           # old style kiwi 3 builds
76           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
77           if [ ${ver:2:2} == "01" ]; then
78             ## This block is obsolete with current kiwi versions, only needed for kiwi 3.01 version
79             for i in $BUILD_ROOT/$TOPDIR/KIWIROOT/main/* ; do
80                 test -d "$i" || continue
81                 n="${i##*/}"
82                 test "$n" = scripts && continue
83                 test "$n" != "${n%0}" && continue
84                 chroot $BUILD_ROOT su -c "suse-isolinux $TOPDIR/KIWIROOT/main/$n $TOPDIR/KIWI/$n.iso" - $BUILD_USER
85             done
86           fi
87         else
88           # current default
89           chroot "$BUILD_ROOT" su -c "APPID=- LANG=POSIX /usr/sbin/kiwi --root $TOPDIR/KIWIROOT -v --logfile terminal -p $TOPDIR/SOURCES --create-instsource $TOPDIR/SOURCES" - abuild < /dev/null && BUILD_SUCCEEDED=true
90         fi
91
92         # move created product to correct destination
93         for i in $BUILD_ROOT/$TOPDIR/KIWIROOT/main/* ; do
94             test -e "$i" || continue
95             f=${i##*/}
96             case $f in
97                 *.iso) mv $i $BUILD_ROOT/$TOPDIR/KIWI/. ;;
98                 scripts) ;;
99                 *0) ;;
100                 *) test -d $i && mv $i $BUILD_ROOT/$TOPDIR/KIWI/. ;;
101             esac
102         done
103     else
104         BUILD_SUCCEEDED=true
105         if [ -z "$RUNNING_IN_VM" ]; then
106             # NOTE: this must be done with the outer system, because it loads the dm-mod kernel modules, which needs to fit to the kernel.
107             echo "starting device mapper for kiwi..."
108             [ -x /etc/init.d/boot.device-mapper ] && /etc/init.d/boot.device-mapper start
109         fi
110         for imgtype in $imagetype ; do
111             echo "running kiwi --prepare for $imgtype..."
112             # Do not use $BUILD_USER here, since we always need root permissions
113             if chroot $BUILD_ROOT su -c "cd $TOPDIR/SOURCES && kiwi --prepare $TOPDIR/SOURCES --logfile terminal --root $TOPDIR/KIWIROOT-$imgtype" - root < /dev/null ; then
114                 echo "running kiwi --create for $imgtype..."
115                 mkdir -p $BUILD_ROOT/$TOPDIR/KIWI-$imgtype
116                 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
117             else
118                 cleanup_and_exit 1
119             fi
120         done
121
122         # create tar.gz of images, in case it makes sense
123         imagearch=`uname -m`
124         buildnum=""
125           if test -n "$RELEASE"; then
126             buildnum="-Build$RELEASE"
127         fi
128         imageout="$imagename.$imagearch-$imageversion"
129         for imgtype in $imagetype ; do
130             case "$imgtype" in
131                 oem)
132                     cat > $BUILD_ROOT/kiwi_post.sh << EOF
133 echo "compressing oem images... "
134 cd /$TOPDIR/KIWI-oem
135 if [ -e "$imageout.iso" ]; then
136         echo "take iso file and create sha256..."
137         mv "$imageout.iso" "/$TOPDIR/KIWI/$imageout$buildnum.iso"
138         pushd /$TOPDIR/KIWI
139         if [ -x /usr/bin/sha256sum ]; then
140            /usr/bin/sha256sum "$imageout$buildnum.iso" > "$imageout$buildnum.iso.sha256"
141         fi
142         popd
143 fi
144 if [ -e "$imageout.raw" ]; then
145         mv "$imageout.raw" "/$TOPDIR/KIWI/$imageout$buildnum.raw"
146         pushd /$TOPDIR/KIWI
147         echo "bzip2 raw file..."
148         bzip2 "$imageout$buildnum.raw"
149         if [ -x /usr/bin/sha256sum ]; then
150             echo "Create sha256 file..."
151             /usr/bin/sha256sum "$imageout$buildnum.raw.bz2" > "$imageout$buildnum.raw.bz2.sha256"
152         fi
153         popd
154 fi
155
156 tar cvjfS "/$TOPDIR/KIWI/$imageout$buildnum-raw.tar.bz2" \
157         --exclude="$imageout.iso" --exclude="$imageout.raw" *
158 cd /$TOPDIR/KIWI
159 if [ -x /usr/bin/sha256sum ]; then
160    /usr/bin/sha256sum "$imageout$buildnum-raw.tar.bz2" > "$imageout$buildnum-raw.tar.bz2.sha256"
161 fi
162 EOF
163                     ;;
164                 vmx)
165                     cat > $BUILD_ROOT/kiwi_post.sh << EOF
166 echo "compressing vmx images... "
167 cd /$TOPDIR/KIWI-vmx
168 # This option has a number of format parameters
169 VMXFILES=""
170 SHAFILES=""
171 for i in "$imageout.vmx" "$imageout.vmdk" "$imageout-disk*.vmdk" "$imageout.ovf"; do
172         ls \$i >& /dev/null && VMXFILES="\$VMXFILES \$i"
173 done
174 if [ -n "\$VMXFILES" ]; then
175         tar cvjfS "/$TOPDIR/KIWI/$imageout$buildnum-vmx.tar.bz2" \$VMXFILES
176         SHAFILES="\$SHAFILES $imageout$buildnum-vmx.tar.bz2"
177 fi
178
179 if [ -e "$imageout.xenconfig" ]; then
180         tar cvjfS "/$TOPDIR/KIWI/$imageout$buildnum-vmx.tar.bz2" $imageout.xenconfig $imageout.raw initrd-*
181         SHAFILES="\$SHAFILES $imageout$buildnum-vmx.tar.bz2"
182 fi
183 # FIXME: do we need a single .raw file in any case ?
184
185 cd /$TOPDIR/KIWI
186 if [ -n "\$SHAFILES" -a -x /usr/bin/sha256sum ]; then
187         for i in \$SHAFILES; do
188                 echo "Create sha256 file..."
189                 /usr/bin/sha256sum "\$i" > "\$i.sha256"
190         done
191 fi
192 EOF
193                     ;;
194                 xen)
195                     cat > $BUILD_ROOT/kiwi_post.sh << EOF
196 echo "compressing xen images... "
197 cd /$TOPDIR/KIWI-xen
198 tar cvjfS "/$TOPDIR/KIWI/$imageout$buildnum-xen.tar.bz2" \
199         `grep ^kernel $imageout.xenconfig | cut -d'"'  -f2` \
200         `grep ^ramdisk $imageout.xenconfig | cut -d'"'  -f2` \
201         initrd-* \
202         "$imageout.xenconfig" \
203         "$imageout"
204 if [ -x /usr/bin/sha256sum ]; then
205    echo "Create sha256 file..."
206    cd $TOPDIR/KIWI
207    /usr/bin/sha256sum "$imageout$buildnum-xen.tar.bz2" > "$imageout$buildnum-xen.tar.bz2.sha256"
208 fi
209 EOF
210                     ;;
211                 pxe)
212                     cat > $BUILD_ROOT/kiwi_post.sh << EOF
213 echo "compressing pxe images... "
214 cd /$TOPDIR/KIWI-pxe
215 tar cvjfS "/$TOPDIR/KIWI/$imageout$buildnum-pxe.tar.bz2" ${imageout}* initrd-*
216 if [ -x /usr/bin/sha256sum ]; then
217    echo "Create sha256 file..."
218    cd $TOPDIR/KIWI
219    /usr/bin/sha256sum "$imageout$buildnum-pxe.tar.bz2" > "$imageout$buildnum-pxe.tar.bz2.sha256"
220 fi
221 EOF
222                     ;;
223                 iso)
224                     cat > $BUILD_ROOT/kiwi_post.sh << EOF
225 cd /$TOPDIR/KIWI-iso
226 for i in *.iso; do
227         mv "\$i" "/$TOPDIR/KIWI/\${i%.iso}$buildnum.iso"
228 done
229 if [ -x /usr/bin/sha256sum ]; then
230    echo "creating sha256 sum for iso images... "
231    cd $TOPDIR/KIWI
232    for i in *.iso; do
233         /usr/bin/sha256sum "\$i" > "\$i.sha256"
234    done
235 fi
236 EOF
237                     ;;
238                 *)
239                     cat > $BUILD_ROOT/kiwi_post.sh << EOF
240 echo "compressing unkown images... "
241 cd /$TOPDIR/KIWI-$imgtype
242 tar cvjfS "/$TOPDIR/KIWI/$imageout$buildnum-$imgtype.tar.bz2" *
243 if [ -x /usr/bin/sha256sum ]; then
244    echo "Create sha256 file..."
245    cd /$TOPDIR/KIWI
246    /usr/bin/sha256sum "$imageout$buildnum-$imgtype.tar.bz2" > "$imageout$buildnum-$imgtype.tar.bz2.sha256"
247 fi
248 EOF
249                     ;;
250             esac
251             chroot $BUILD_ROOT su -c "sh -x -e /kiwi_post.sh" || cleanup_and_exit 1
252             rm -f $BUILD_ROOT/kiwi_post.sh
253         done
254     fi
255 }