forked from openSUSE/obs-build
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build
executable file
·3217 lines (2934 loc) · 103 KB
/
build
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/bash
# Script to build a package. It uses init_buildsystem to setup a chroot
# building tree. This script needs a directory as parameter. This directory
# has to include sources and a spec file.
#
# BUILD_ROOT here the packages will be built
#
# (c) 1997-2008 SuSE GmbH Nuernberg, Germany
# some VMs do not allow to specify the init process...
if test "$0" = "/sbin/init" ; then
exec /.build/build "$@"
fi
test -z "$BUILD_DIR" && BUILD_DIR=/usr/lib/build
test -z "$BUILD_ROOT" && BUILD_ROOT=/var/tmp/build-root
test -z "$CONFIG_DIR" && CONFIG_DIR="$BUILD_DIR/configs"
export BUILD_ARCH BUILD_HOST_ARCH BUILD_ROOT BUILD_RPMS BUILD_DIR BUILD_DEBUG
export BUILD_DIST
ccache=0
icecream=0
statistics=0
shell=
definesnstuff=()
repos=()
old_packages=()
# defaults for vm_img_mkfs
vm_img_mkfs_ext4_options='-O ^has_journal,^huge_file,^resize_inode,sparse_super'
vm_img_mkfs_ext4_extra='-E lazy_itable_init,discard'
vm_img_mkfs_ext4="mkfs.ext4 -m 0 -q -F $vm_img_mkfs_ext4_options"
vm_img_tunefs_ext4='tune2fs -c 0'
vm_img_mkfs_ext3='mkfs.ext3 -m 0 -q -F'
vm_img_tunefs_ext3='tune2fs -c 0 -o journal_data_writeback'
vm_img_mkfs_ext2='mkfs.ext2 -m 0 -q -F'
vm_img_tunefs_ext2='tune2fs -c 0'
vm_img_mkfs_reiserfs='mkreiserfs -q -f'
vm_img_mkfs_btrfs='mkfs.btrfs'
vm_img_mkfs_xfs='mkfs.xfs -f'
vm_kernel=/boot/vmlinuz
vm_initrd=/boot/initrd
qemu_bin=/usr/bin/qemu
uml_kernel=/boot/vmlinux-um
uml_initrd=/boot/initrd-um
# z/VM: use default kernel image from local machine
# lets go with the default parameters. However zvm_initrd will be a required parameter
#zvm_kernel=/boot/image
#zvm_initrd=/boot/initrd_worker
zvm_param="root=/dev/disk/by-path/ccw-0.0.0150-part1 hvc_iucv=8 console=hvc0"
zvm_mult_pass="THR4ME"
#zvm_worker_nr="1"
zvm_init_script="/.build/build"
kvm_bin=/usr/bin/qemu-kvm
# whether we have virtio support
kvm_virtio=
# guest visible console device name
console=ttyS0
# need to restore build root owner for non-root builds
browner=
# additional kiwi parameters, used for appliance builds with obsrepositories:/ directive
KIWI_PARAMETERS=
# Default uid:gid for the build user
ABUILD_UID=399
ABUILD_GID=399
DO_INIT=true
DO_LINT=
DO_CHECKS=true
CLEAN_BUILD=
USE_SYSTEM_QEMU=
SPECFILES=()
SRCDIR=
BUILD_JOBS=
ABUILD_TARGET=
CREATE_BASELIBS=
USEUSEDFORBUILD=
LIST_STATE=
VM_IMAGE=
VM_SWAP=
VM_KERNEL=
VM_INITRD=
VMDISK_ROOTSIZE=4096
VMDISK_SWAPSIZE=1024
VMDISK_FILESYSTEM=ext4
# settings are for speed and not data safety, we format anyway on next run
VMDISK_MOUNT_OPTIONS=__default
VMDISK_CLEAN=
HUGETLBFSPATH=
MEMSIZE=
RUNNING_IN_VM=
RPMLIST=
RELEASE=
REASON=
NOROOTFORBUILD=
LOGFILE=
KILL=
CHANGELOG=
BUILD_DEBUG=
PERSONALITY_SYSCALL=
INCARNATION=
DISTURL=
LINKSOURCES=
OVERLAY=
RSYNCSRC=
RSYNCDEST=
RSYNCDONE=
SIGNDUMMY=
HOST_ARCH=
EMULATOR_SCRIPT=
KVM_OPTIONS=
BUILD_EC2_TYPE="t1.micro"
# This is for insserv
export YAST_IS_RUNNING=instsys
unset LANGUAGE
unset LANG
export LC_ALL=POSIX
umask 022
echo_help () {
cat << EOT
Some comments for build
-----------------------
With build you can create rpm packages. They will be built in a chroot
system. This chroot system will be setup automatically. Normally you can
simply call build with a spec file as parameter - nothing else has to be
set.
If you want to set the directory were the chroot system will be setup
(at the moment it uses $BUILD_ROOT),
simply set the the environment variable BUILD_ROOT.
Example:
export BUILD_ROOT=/var/tmp/mybuildroot
Normally build builds the complete package including src.rpm (rpmbuild -ba).
If you want let build only make the binary package, simply set
export BUILD_RPM_BUILD_STAGE=-bb
(or -bc, -bp, -bi, ... see "Maximum RPM" for more details [*]).
When the build command succeeds, the rpm files can be found under
$BUILD_ROOT/usr/src/packages/RPMS/
Known Parameters:
--help You already got it :)
--kill Instead of starting a build kill the one currently
running.
--shell Instead of starting a build start a root shell in
the build root.
--clean Delete old build root before initializing it
--no-init Skip initialization of build root and start with build
immediately.
--no-checks Do not run post-build checks
--lint Run rpmlint after build.
--logfile logfile
Capture build output to logfile. Defaults to
.build.log in the build root for non-VM builds.
--repository PATH
Use package repository at PATH. Supported formats are
rpm-md and yast2.
Alternatively zypp://NAME specifies the zypp
repository NAME. The repo must be refreshed with zypp
so package meta data is available locally. With emtpy
NAME all enabled repositories are used.
a url can specify a remote repo.
--rpms path1:path2:...
Specify path where to find the RPMs for the build system
--arch arch1:arch2:...
Specify what architectures to select from the RPMs
--verify Run verify when initializing the build root
--extra-packs pack
-X pack
Also install package 'pack'
--root rootdir
Use 'rootdir' to setup chroot environment
--cachedir cachedir
Use 'cachedir' to cache remote repo's packages, the
default cache dir is /var/cache/build, every repo
given by --repository corresponds to a subdir named
as md5sum of its repo url, for example:
/var/cache/build/3e8ea9b47808629414a0cebc33ea285e
--oldpackages oldpackagesdir
Define a directory with a former build
--baselibs Create -32bit/-64bit/-x86 rpms for other architectures
--list-state
List rpms that would be used to create a fresh build root.
Does not create the build root or perform a build.
--dist dist
Distribution to use
--with X
Enable feature X for build
--without X
Disable feature X for build
--define 'X Y'
Define macro X with value Y
--release release
Override Release in spec file
--stage -bSTAGE
Set stage for rpmbuild. Defaults to -ba.
--target platform
Set target platform for rpmbuild
--jobs N Use N parallel processes during build.
Sets %jobs and %_smp_mflags macros and
defines the number of CPUs to use for
VMs.
--ccache
Use ccache to speed up rebuilds
--icecream N
Use N parallel build jobs with icecream
--overlay OVERLAY
Copy overlay filesystem to buildroot after installing
all RPMs. This must be a valid directory.
--rsync-src RSYNCSRC
Copy overlay folder (RSYNCSRC) to a folder (RSYNCDEST)
inside the buildroot using rsync.
It will "%define RSYNCDONE 1" for handling %setup in your
specfile. E.g.:
%prep
%if 0%{?RSYNCDONE}
%setup -n aaa_base -T -D -b 5 -b 7
%else
%setup -n aaa_base -b 5 -b 7
%endif
--rsync-dest RSYNCDEST
Copy overlay folder (RSYNCSRC) to a folder (RSYNCDEST)
inside the buildroot using rsync.
--uid uid:gid
Specify the uid and gid to use for the abuild user.
This is useful if you are hacking in the buildroot.
This must be set to the same value if the buildroot is re-used.
--statistics
monitor used resources during build inside VM
--kvm
Use KVM to secure build process. Your hardware needs to support
virtualization
--xen
Use XEN to secure build process, you to run in a XEN Dom0 environment.
--lxc
Use Linux Containers to isolate the process. This may not be 100% safe.
--openstack
Cloud build
--ec2
Cloud build
--emulator
Use any generic emulator to isolate the build process. You need to write
an emulator/emulator.sh script and put it next to the build script sources.
You must NOT use any vm swap file for this.
--emulator-script SCRIPT
specify another emulator instead of emulator.sh
--vm-type TYPE
Use virtual machine instead of chroot
TYPE is one of xen|kvm|uml|qemu|lxc|zvm|openstack|ec2
--vm-worker GUEST
GUEST is a z/VM build worker controlled by the controlling
z/VM build machine.
--vm-worker-nr N
Each worker in z/VM needs a uniq number. This is needed to
calculate uniq device addresses for root and swap device.
--vm-region NAME
EC2 only: defines amazon control server
--vm-server NAME
openstack only: defines control server name
--vm-disk FILE
Use FILE as disk for virtual machine.
Defaults to \$BUILD_ROOT.img if unset
--vm-swap FILE
Use FILE as swap space for virtual machine. The swap space is
also used for retrieving packages from the VM so its size must
be sufficiently large
--vm-disk-size SIZEINMB
--vm-swap-size SIZEINMB
--vm-disk-filesystem TYPE
Defaults for automatic setup of VM root/swap files
--vm-memory SIZEINMB
Set amount of RAM for VMs
--hugetlbfs HUGETLBFSPATH
Use hugetlb for memory management, path to mounted hugetlbfs.
--vm-kernel FILE
--vm-initrd FILE
Kernel and initrd to use for VM (kvm and qemu only)
--debug
Enable creation of a debuginfo package
Remember to have fun!
[*] Maximum RPM: http://www.rpm.org/max-rpm/
EOT
}
usage () {
echo "Usage: `basename $0` [--no-init|--clean|--rpms path|--verify|--help] [dir-to-build|spec-to-build]"
cleanup_and_exit 1
}
#
# cleanup_and_exit
# return values: 0 -> success, new packages built
# 1 -> error, build failed
# 2 -> successfull build, but no changes to former built packages
# 3 -> something wrong with build host
#
cleanup_and_exit () {
trap EXIT
test -z "$1" && set 0
rm -f $BUILD_ROOT/exit
if test "$1" -eq 1 -a -x /bin/df ; then
# okay, it failed, but maybe because disk space?
if df $BUILD_ROOT 2>/dev/null | grep -q "100%"; then
set 3
fi
fi
if test -n "$RUNNING_IN_VM" ; then
echo "$1" > /.build/_exitcode
test -n "$browner" && chown "$browner" $BUILD_ROOT
cd /
if test -n "$VM_SWAP" -a -e "$VM_SWAP" ; then
swapoff "$VM_SWAP" 2>/dev/null
echo -n "BUILDSTATUS$1" >"$VM_SWAP"
fi
exec >&0 2>&0 # so that the logging tee finishes
sleep 1 # wait till tee terminates
if test "$VM_TYPE" != lxc; then
kill -9 -1 # goodbye cruel world
if ! test -x /sbin/halt ; then
test -e /proc/sysrq-trigger || mount -n -tproc none /proc
sync
sleep 2 # like halt does
if test -e /proc/sysrq-trigger; then
echo o > /proc/sysrq-trigger
sleep 5 # wait for sysrq to take effect
else
echo "Warning: VM doesn't support sysrq and /sbin/halt not installed"
fi
else
halt -f -p
fi
echo "Warning: clean shut down of the VM didn't work"
fi
else
# nop if unused
cloud_volume_detach "$VM_SERVER" "$VM_VOLUME_NAME"
cloud_volume_detach "$VM_SERVER" "$VM_VOLUME_SWAP"
cloud_volume_detach "$VM_SERVER" "$EC2_EXTRACT_VOLUME_root"
cloud_volume_detach "$VM_SERVER" "$EC2_EXTRACT_VOLUME_swap"
[ -n "$EC2_SNAP_root" ] && ec2-delete-snapshot --region "$BUILD_EC2_REGION" "$EC2_SNAP_root"
[ -n "$EC2_SNAP_swap" ] && ec2-delete-snapshot --region "$BUILD_EC2_REGION" "$EC2_SNAP_swap"
[ -n "$EC2_EXTRACT_VOLUME_root" ] && ec2-delete-volume --region "$BUILD_EC2_REGION" "$EC2_EXTRACT_VOLUME_root"
[ -n "$EC2_EXTRACT_VOLUME_swap" ] && ec2-delete-volume --region "$BUILD_EC2_REGION" "$EC2_EXTRACT_VOLUME_swap"
umount -n $BUILD_ROOT/proc/sys/fs/binfmt_misc 2> /dev/null || true
umount -n $BUILD_ROOT/proc 2>/dev/null || true
umount -n $BUILD_ROOT/dev/pts 2>/dev/null || true
umount -n $BUILD_ROOT/dev/shm 2>/dev/null || true
test "$VM_IMAGE" = 1 && VM_IMAGE=
[ -n "$VM_IMAGE" ] && umount $BUILD_ROOT 2>/dev/null || true
fi
# echo "pid $$ exit $1"
exit $1
}
fail_exit()
{
cleanup_and_exit 1
}
shellquote()
{
for arg; do
arg=${arg/\\/\\\\}
arg=${arg/\$/\\\$}
arg=${arg/\"/\\\"}
arg=${arg/\`/\\\`}
echo -n " \"$arg\""
done
}
# create a shell script from command line. Used for preserving arguments
# through /bin/su -c
toshellscript()
{
echo "#!/bin/sh -x"
echo -n exec
shellquote "$@"
echo
}
setupccache()
{
if [ "$ccache" = 1 ]; then
if mkdir -p $BUILD_ROOT/var/lib/build/ccache/bin; then
for i in $(ls $BUILD_ROOT/usr/bin | grep -E '^(cc|gcc|[cg][+][+])([-]?[234][.]?[0-9])*$'); do
# ln -sf /usr/bin/ccache $BUILD_ROOT/var/lib/build/ccache/bin/$i
rm -f $BUILD_ROOT/var/lib/build/ccache/bin/$i
test -e $BUILD_ROOT/usr/bin/$i || continue
echo '#! /bin/sh' > $BUILD_ROOT/var/lib/build/ccache/bin/$i
echo "test -e /usr/bin/$i || exit 1" >> $BUILD_ROOT/var/lib/build/ccache/bin/$i
echo 'export PATH=/usr/lib/icecc/bin:/opt/icecream/bin:/usr/bin:$PATH' >> $BUILD_ROOT/var/lib/build/ccache/bin/$i
echo "ccache $i \"\$@\"" >> $BUILD_ROOT/var/lib/build/ccache/bin/$i
chmod 755 $BUILD_ROOT/var/lib/build/ccache/bin/$i
echo "Installed ccache wrapper as $BUILD_ROOT/var/lib/build/ccache/bin/$i"
done
fi
mkdir -p "$BUILD_ROOT"/.ccache
chown -R "$ABUILD_UID:$ABUILD_GID" "$BUILD_ROOT/.ccache"
echo "export CCACHE_DIR=/.ccache" > "$BUILD_ROOT"/etc/profile.d/build_ccache.sh
echo 'export PATH=/var/lib/build/ccache/bin:$PATH' >> "$BUILD_ROOT"/etc/profile.d/build_ccache.sh
else
rm -f "$BUILD_ROOT"/var/lib/build/ccache/bin/{gcc,g++,cc,c++}
fi
}
setupicecream()
{
if [ "$icecream" -eq 0 ]; then
rm -rf "$BUILD_ROOT/var/run/icecream"
rm -f "$BUILD_ROOT/etc/profile.d/build_icecream.sh"
return
fi
if ! chroot "$BUILD_ROOT" rpm -q icecream >/dev/null 2>/dev/null; then
echo "*** icecream package not installed ***"
false
return
fi
echo "using icecream with $icecream jobs"
if [ "$ccache" -ne 1 ]; then
echo 'export PATH=/usr/lib/icecc/bin:/opt/icecream/bin:$PATH' > "$BUILD_ROOT"/etc/profile.d/build_icecream.sh
else
echo 'export CCACHE_PATH=/usr/lib/icecc/bin:/opt/icecream/bin' > "$BUILD_ROOT"/etc/profile.d/build_icecream.sh
fi
local icecc_vers=(`shopt -s nullglob; echo $BUILD_ROOT/var/run/icecream/*.tar.{bz2,gz}`)
icecc_vers=${icecc_vers//$BUILD_ROOT/}
# XXX use changelog like autobuild does instead?
# only run create-env if compiler or glibc changed
if [ -z "$icecc_vers" \
-o ! -e "$BUILD_ROOT/$icecc_vers" \
-o "$BUILD_ROOT/usr/bin/gcc" -nt "$BUILD_ROOT/$icecc_vers" \
-o "$BUILD_ROOT/usr/bin/g++" -nt "$BUILD_ROOT/$icecc_vers" \
-o "$BUILD_ROOT/usr/bin/as" -nt "$BUILD_ROOT/$icecc_vers" \
-o "$BUILD_ROOT/lib/libc.so.6" -nt "$BUILD_ROOT/$icecc_vers" ]
then
rm -rf "$BUILD_ROOT/var/run/icecream"
mkdir -p "$BUILD_ROOT/var/run/icecream"
if [ -e "$BUILD_ROOT"/usr/bin/create-env ]; then
createenv=/usr/bin/create-env
elif [ -e "$BUILD_ROOT"/usr/lib/icecc/icecc-create-env ]; then
createenv="/usr/lib/icecc/icecc-create-env /usr/bin/gcc /usr/bin/g++" # XXX
elif [ -e "$BUILD_ROOT"/usr/lib64/icecc/icecc-create-env ]; then
createenv="/usr/lib64/icecc/icecc-create-env /usr/bin/gcc /usr/bin/g++" # XXX
else
echo "create-env not found"
false
return
fi
chroot $BUILD_ROOT bash -c "cd /var/run/icecream; $createenv" || cleanup_and_exit 1
icecc_vers=(`shopt -s nullglob; echo $BUILD_ROOT/var/run/icecream/*.tar.{bz2,gz}`)
icecc_vers=${icecc_vers//$BUILD_ROOT/}
else
echo "reusing existing icecream environment $icecc_vers"
fi
if [ -n "$icecc_vers" ]; then
echo "export ICECC_VERSION=$icecc_vers" >> "$BUILD_ROOT"/etc/profile.d/build_icecream.sh
fi
}
setmemorylimit()
{
if [ -n "$VM_IMAGE" -o -n "$RUNNING_IN_VM" ]; then
return
fi
local mem
local limit
while read mem; do
case "$mem" in
MemTotal:*)
set -- $mem
eval "limit=\$(($2/3*4))"
;;
SwapTotal:*)
set -- $mem
eval "limit=\$(($2/3*4+$limit))"
;;
esac
done < <(cat /proc/meminfo) # cat for proc stuff
ulimit -v $limit
echo "Memory limit set to ${limit}KB"
}
create_baselibs()
{
local pkgs=()
local line
BASELIBS_CFG=
if test "$BUILDTYPE" == "arch" ; then
return
fi
if test "$BUILDTYPE" == "dsc" ; then
pkgs=($DEBS)
else # spec and kiwi
if test -e $BUILD_ROOT$TOPDIR/SOURCES/baselibs.conf ; then
BASELIBS_CFG="-c $TOPDIR/SOURCES/baselibs.conf"
fi
if test -e $BUILD_ROOT/usr/lib/build/baselibs_global.conf; then
BASELIBS_GLOBAL="-c /usr/lib/build/baselibs_global.conf"
fi
pkgs=($RPMS)
fi
mount -n -tproc none $BUILD_ROOT/proc 2> /dev/null
# don't use -R as extracted sources, build root etc might be below $TOPDIR
chown "$ABUILD_UID:$ABUILD_GID" "$BUILD_ROOT$TOPDIR"/* "$BUILD_ROOT$TOPDIR"/RPMS/* || true
local mkbaselibs="/usr/lib/build/mkbaselibs"
local whichone=''
# $BUILD_DIR is set to /.build when using a vm. So we need to
# hardcode /usr/lib/build instead of $BUILD_DIR to prefer
# mkbaselibs from the distro.
if test -f $BUILD_ROOT$mkbaselibs; then
if test -z "$BASELIBS_CFG" -a -e $BUILD_ROOT/usr/lib/build/baselibs.conf ; then
BASELIBS_CFG="-c /usr/lib/build/baselibs.conf"
fi
else
if test "$CREATE_BASELIBS" = 'internal'; then
echo "Warning: mkbaselibs missing in build root, skipping baselibs"
return
fi
# use external version
whichone=" (external)"
mkbaselibs="/.mkbaselibs/mkbaselibs"
rm -rf "$BUILD_ROOT/.mkbaselibs"
mkdir -p "$BUILD_ROOT/.mkbaselibs"
cp -f $BUILD_DIR/mkbaselibs $BUILD_ROOT/.mkbaselibs/
if test "$BUILDTYPE" == "dsc" ; then
cp -f $BUILD_DIR/baselibs_global-deb.conf $BUILD_ROOT/.mkbaselibs/baselibs_g.conf
cp -f $BUILD_ROOT$TOPDIR/SOURCES/baselibs-deb.conf $BUILD_ROOT/.mkbaselibs/baselibs-deb.conf
BASELIBS_CFG="-c /.mkbaselibs/baselibs-deb.conf"
else
cp -f $BUILD_DIR/baselibs_global.conf $BUILD_ROOT/.mkbaselibs/baselibs_g.conf
if test -z "$BASELIBS_CFG" -a -e $BUILD_DIR/baselibs.conf; then
cp -f $BUILD_DIR/baselibs.conf $BUILD_ROOT/.mkbaselibs/baselibs.conf
BASELIBS_CFG="-c /.mkbaselibs/baselibs.conf"
fi
fi
if test -e $BUILD_ROOT/.mkbaselibs/baselibs_g.conf; then
BASELIBS_GLOBAL="-c /.mkbaselibs/baselibs_g.conf"
fi
fi
echo "... creating baselibs$whichone"
while read line
do
chroot $BUILD_ROOT su -c "$mkbaselibs $BASELIBS_GLOBAL $BASELIBS_CFG $line" - $BUILD_USER || cleanup_and_exit 1
done < <(IFS=$'\n'; echo "${pkgs[*]#$BUILD_ROOT}" | xargs -n 1024)
rm -rf "$BUILD_ROOT/.mkbaselibs"
}
copy_oldpackages()
{
local i=0
local d
local dest
[ -z "$RUNNING_IN_VM" ] || return 0
if [ -z "$old_packages" ]; then
rm -rf "$BUILD_ROOT"/.build.oldpackages*
return 0
fi
for d in "${old_packages[@]}"; do
dest="$BUILD_ROOT/.build.oldpackages"
test "$i" = 0 || dest="$dest$i"
if [ -d "$d" -a "$d" != "$dest" ] ; then
rm -rf "$dest"
mkdir -p "$dest"
cp -L $d/* "$dest"
: $((++i))
fi
done
}
vm_img_create()
{
local img="$1"
local size="$2"
echo "Creating $img (${size}M)"
mkdir -p "${img%/*}" || cleanup_and_exit 3
# prefer fallocate, which avoids fragmentation
r=1
if type -p fallocate > /dev/null; then
fallocate -l "${size}M" "$img"
r=$?
fi
# fall back to dd method if fallocate is not supported
if [ "$r" -gt "0" ]; then
dd if=/dev/zero of="$img" bs=1M count=0 seek="$size" || cleanup_and_exit 3
fi
}
vm_img_mkfs()
{
local fs="$1"
local img="$2"
local mkfs tunefs
eval "mkfs=\"\$vm_img_mkfs_${fs}\""
eval "mkfs_exta_options=\"\$vm_img_mkfs_${fs}_extra\""
eval "tunefs=\"\$vm_img_tunefs_${fs}\""
if test -z "$mkfs"; then
echo "filesystem \"$fs\" isn't supported"
cleanup_and_exit 3
fi
echo "Creating $fs filesystem on $img"
if ! $mkfs $mkfs_exta_options "$img"; then
if test -z "$mkfs_exta_options"; then
cleanup_and_exit 3
else
echo "Format call failed, trying again without extra options..."
$mkfs "$img" || cleanup_and_exit 3
fi
fi
if test -n "$tunefs" ; then
$tunefs "$img" || cleanup_and_exit 3
fi
}
background_monitor_process()
{
max_disk=0
max_mem=0
while sleep 5; do
# memory usage
if [ -e /proc/meminfo ]; then
memtotal=0
while read key value unit; do
case $key in
MemTotal:|SwapTotal:)
memtotal=$(( $memtotal + $value ))
;;
MemFree:|SwapFree:|SwapCached:|Cached:|Buffers:)
memtotal=$(( $memtotal - $value ))
;;
esac
done < /proc/meminfo
if [ ${memtotal} -gt $max_mem ]; then
max_mem="${memtotal}"
echo -n $(( $max_mem / 1024 )) > /.build/_statistics.memory.new && mv /.build/_statistics.memory.new /.build/_statistics.memory
fi
fi
# disk storage usage
if type -p df >& /dev/null; then
c=(`df -m / 2>/dev/null | tail -n 1`)
if [ ${c[2]} -gt $max_disk ]; then
max_disk="${c[2]}"
echo -n $max_disk > /.build/_statistics.df.new && mv /.build/_statistics.df.new /.build/_statistics.df
fi
fi
[ -e /.build/_statistics.exit ] && exit 0
done
}
detect_vm_2nd_stage()
{
if test -e /.build/build.data; then
. /.build/build.data
fi
if test -z "$VM_TYPE" ; then
return 1
fi
if test $$ -eq 1 || test $$ -eq 2; then
# ignore special init signals if we're init
# we're using ' ' instead of '' so that the signal handlers
# are reset in the child processes
trap ' ' HUP TERM
$0 "$@"
cleanup_and_exit $?
fi
echo "2nd stage started in virtual machine"
BUILD_ROOT=/
BUILD_DIR=/.build
echo "machine type: `uname -m`"
if test "$PERSONALITY" != 0 -a -z "$PERSONALITY_SET" ; then
export PERSONALITY_SET=true
echo "switching personality to $PERSONALITY..."
# this is 32bit perl/glibc, thus the 32bit syscall number
exec perl -e 'syscall(136, '$PERSONALITY') == -1 && warn("personality: $!\n");exec "/.build/build" || die("/.build/build: $!\n")'
fi
RUNNING_IN_VM=true
test -e /proc/version || mount -orw -n -tproc none /proc
if test "$VM_TYPE" != 'lxc'; then
mount -n ${VMDISK_MOUNT_OPTIONS},remount,rw /
fi
umount /run >/dev/null 2>&1
# qemu inside of xen does not work, check again with kvm later before enabling this
# if [ -e /dev/kqemu ]; then
# # allow abuild user to run qemu
# chmod 0666 /dev/kqemu
# fi
if test "$VM_TYPE" = 'zvm' ; then
VM_SWAP='/dev/dasdb1'
fi
if test "$VM_TYPE" = 'ec2' ; then
VM_SWAP='/dev/sdb1'
# Usually the external system is writing the swap signature, but EC2 volume
# attach is very slow, so let's do it in the VM for now.
mkswap "$VM_SWAP"
fi
if test -n "$VM_SWAP" ; then
for i in 1 2 3 4 5 6 7 8 9 10 ; do
test -e "$VM_SWAP" && break
test $i = 1 && echo "waiting for $VM_SWAP to appear"
echo -n .
sleep 1
done
test $i = 1 || echo
# recreate the swap device manually if it didn't exist for some
# reason, hardcoded to hda2 atm
if ! test -b "$VM_SWAP" ; then
rm -f "$VM_SWAP"
umask 027
mknod "$VM_SWAP" b 3 2
umask 022
fi
swapon -v "$VM_SWAP" || exit 1
fi
HOST="$MYHOSTNAME"
# fork a process monitoring max filesystem fillment during build
if test "$statistics" = "1"; then
background_monitor_process &
fi
if [ ! -e /dev/.udev ]; then
echo "WARNING: udev not running, creating extra device nodes"
test -e /dev/fd || ln -sf /proc/self/fd /dev/fd
test -e /etc/mtab || ln -sf /proc/mounts /etc/mtab
fi
# set date to build start on broken systems (now < build start)
if [ $(date '+%s') -lt $(date -r /.build/.date '+%s') ]; then
echo -n "WARNING: system has a broken clock, setting it to a newer time: "
date -s `cat /.build/.date`
fi
return 0
}
find_spec_files()
{
local spec files
if [ -z "$SPECFILES" ]; then
set -- "`pwd`"
else
set -- "${SPECFILES[@]}"
fi
SPECFILES=()
for spec in "$@"; do
if [ "$spec" = "${spec#/}" ]; then
spec="`pwd`/$spec"
fi
if [ -d "$spec" ]; then
specs=("$spec"/*.spec)
if [ -n "$specs" ]; then
SPECFILES=("${SPECFILES[@]}" "${specs[@]}")
else
specs=("$spec"/*.spec)
if [ -n "$specs" ]; then
SPECFILES=("${SPECFILES[@]}" "${specs[@]}")
fi
fi
else
SPECFILES[${#SPECFILES[@]}]="$spec";
fi
done
if test -z "$SPECFILES"; then
echo no spec files or src rpms found in $@. exit...
cleanup_and_exit 1
fi
}
become_root_or_fail()
{
if [ ! -w /root ]; then
echo "You have to be root to use $0" >&2
exit 1
fi
cleanup_and_exit 1
}
mkdir_build_root()
{
if [ -d "$BUILD_ROOT" ]; then
# check if it is owned by root
if [ -z "$RUNNING_IN_VM" -a \! -O "$BUILD_ROOT" -a "`stat -c %u $BUILD_ROOT`" -ne 0 ]; then
echo "BUILD_ROOT=$BUILD_ROOT must be owned by root. Exit..."
cleanup_and_exit 1
fi
else
test "$BUILD_ROOT" != "${BUILD_ROOT%/*}" && mkdir -p "${BUILD_ROOT%/*}"
if ! mkdir $BUILD_ROOT; then
echo "can not create BUILD_ROOT=$BUILD_ROOT. Exit..."
cleanup_and_exit 1
fi
fi
if [ ! -w "$BUILD_ROOT" ]; then
echo "Error: BUILD_ROOT=$BUILD_ROOT not writeable, try --clean."
cleanup_and_exit 3
fi
rm -rf "$BUILD_ROOT/.build.packages"
if [ -z "$VM_TYPE" -a -z "$RUNNING_IN_VM" ]; then
# don't touch this in VM
rm -rf "$BUILD_ROOT/.build"
mkdir -p "$BUILD_ROOT/.build"
fi
}
ec2_volume_state()
{
local VM_VOL_NAME="$1"
temp_file=`mktemp`
ec2-describe-volumes "$VM_VOL_NAME" --region "$BUILD_EC2_REGION" > $temp_file
if grep -q ^ATTACHMENT "$temp_file"; then
grep ^ATTACHMENT "$temp_file" | awk '{ print $5 }'
else
grep ^VOLUME "$temp_file" | awk '{ print $5 }'
fi
rm "$temp_file"
}
cloud_volume_attach()
{
local VM_SERVER="$1"
local VM_VOL_NAME="$2"
local VM_VOL_DEV="$3"
if [ "$VM_TYPE" = 'openstack' ]; then
if ! nova volume-attach "$VM_SERVER" "$VM_VOL_NAME" "$VM_VOL_DEV"; then
echo "ERROR: nova attach failed. $?" >&2
return 3
fi
while true; do
state=`nova volume-show "$VM_VOL_NAME" | sed -n 's,^|[ ]*status[ ]*|[ ]*\([^ ]*\).*,\1,p'`
[ "$state" == "in-use" ] && break
if [ -z "$state" ]; then
echo "ERROR: unable to find state of volume $VM_VOL_NAME" >&2
return 3
fi
if [ "$state" == "available" ]; then
echo "WARNING: volume $VM_VOL_NAME got not attached, retrying" >&2
if ! nova volume-attach "$VM_SERVER" "$VM_VOL_NAME" "$VM_VOL_DEV"; then
echo "ERROR: nova attach failed. $?" >&2
return 3
fi
fi
sleep 3
done
if [ ! -e "$VM_VOL_DEV" ]; then
#GROSS HACK: kernel does not care about the given device name
# VM_VOL_DEV="/dev/"`dmesg| sed -n 's,.*\(vd.\): unknown partition tab.*,\1,p' | tail -n 1`
VM_VOL_DEV=`ls -1 /dev/vd? | tail -n 1`
fi
echo "$VM_VOL_DEV"
elif [ "$VM_TYPE" = 'ec2' ]; then
temp_file=`mktemp`
if ! ec2-attach-volume "$VM_VOL_NAME" -d /dev/sdz -i `ec2-instance-id` --region $BUILD_EC2_REGION > "$temp_file"; then
rm "$temp_file"
cleanup_and_exit 1
fi
# wait that it becomes available
while true; do
state=`ec2_volume_state "$VM_VOL_NAME"`
[ "$state" = "attached" ] && break
sleep 1
done
# print device node
grep ^ATTACHMENT "$temp_file" | awk '{ print $4 }'
rm "$temp_file"
fi
}
cloud_volume_detach()
{
local VM_SERVER="$1"
local VM_VOL_NAME="$2"
if [ "$VM_TYPE" = 'openstack' ]; then
# needed at all?
nova volume-show "$VM_VOL_NAME" | grep -q in-use || return 0
# umount seems not to be enough
sync
if ! nova volume-detach "$VM_SERVER" "$VM_VOL_NAME"; then
echo "ERROR: nova detach of $VM_VOL_NAME failed." >&2
return 3
fi
while nova volume-show "$VM_VOL_NAME" | grep -q availabe; do
sleep 3
done
elif [ "$VM_TYPE" = 'ec2' ]; then
state=`ec2_volume_state "$VM_VOL_NAME"`
if [ "$state" != "available" ]; then
ec2-detach-volume "$VM_VOL_NAME" --region $BUILD_EC2_REGION || return 3
fi
fi
return 0
}
linux64()
{
perl -e 'syscall('$PERSONALITY_SYSCALL', 0); exec(@ARGV) || die("$ARGV[0]: $!\n")' "$@"
}