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