forked from openSUSE/obs-build
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build-vm
1148 lines (1067 loc) · 35.9 KB
/
build-vm
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
#
# VM specific functions for the build script
#
################################################################
#
# Copyright (c) 1995-2014 SUSE Linux Products GmbH
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 or 3 as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program (see the file COPYING); if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
#
################################################################
# ignore not backward compatible ext fs options like metadata_csum
# Only protecting nonroot from root inside guest -> but anyone can be root inside guest
# so disabling spectre/meltdown mitigations doesn't hurt security and gains performance
vm_linux_kernel_parameter="ext4.allow_unsupported=1 mitigations=off"
# guest visible devices
VM_ROOTDEV=/dev/hda1
VM_SWAPDEV=/dev/hda2
VM_TYPE=
VM_ROOT=
VM_SWAP=
VM_ROOT_TYPE=
VM_SWAP_TYPE=
VM_KERNEL=
VM_INITRD=
VM_WORKER=
VM_SERVER=
# the qemu default size is not able to run our default kernel anymore
VM_MEMSIZE=512
VM_NETOPT=()
VM_NETDEVOPT=()
VM_DEVICEOPT=()
VM_TELNET=
VM_CONSOLE_INPUT=
VM_USER=
VMDISK_ROOTSIZE=4096
VMDISK_SWAPSIZE=1024
VMDISK_FILESYSTEM=
VMDISK_MOUNT_OPTIONS=__default
VMDISK_CLEAN=
VM_HOSTNAME=
VM_USE_VIRT_FS=
# zvm specific?
VM_WORKER_NO=
# kvm specific?
HUGETLBFSPATH=
VM_CUSTOMOPT=
# emulator specific?
EMULATOR_SCRIPT=
# openstack specific
VM_OPENSTACK_FLAVOR=
for i in ec2 emulator kvm lxc openstack qemu uml xen zvm docker pvm nspawn; do
. "$BUILD_DIR/build-vm-$i"
done
VM_WATCHDOG=
VM_WATCHDOG_PID=
# the following functions just call the corresponding vm versions
vm_verify_options() {
vm_verify_options_$VM_TYPE "$@"
}
vm_attach_root() {
vm_attach_root_$VM_TYPE "$@"
}
vm_attach_swap() {
vm_attach_swap_$VM_TYPE "$@"
}
vm_detach_root() {
vm_detach_root_$VM_TYPE "$@"
}
vm_detach_swap() {
vm_detach_swap_$VM_TYPE "$@"
}
vm_fixup() {
vm_fixup_$VM_TYPE "$@"
}
vm_startup() {
vm_startup_$VM_TYPE "$@"
}
vm_sysrq() {
vm_sysrq_$VM_TYPE "$@"
}
vm_kill() {
vm_kill_$VM_TYPE "$@"
}
vm_cleanup() {
kill_watchdog
vm_cleanup_$VM_TYPE "$@"
}
vm_parse_options() {
case ${PARAM/#--/-} in
-vm-emulator-script|-emulator-script)
needarg
EMULATOR_SCRIPT="$ARG"
shift
;;
-xen|-kvm|-uml|-qemu|-emulator)
VM_TYPE=${PARAM##*-}
test -z "$VM_ROOT" && VM_ROOT=1
if test -n "$ARG" ; then
VM_ROOT="$ARG"
shift
fi
;;
-zvm|-lxc)
VM_TYPE=${PARAM##*-}
shift
;;
-vm-type)
needarg
VM_TYPE="$ARG"
case "$VM_TYPE" in
lxc|docker|nspawn) ;;
ec2|xen|kvm|uml|qemu|emulator|openstack|zvm|pvm)
test -z "$VM_ROOT" && VM_ROOT=1
;;
none|chroot) VM_TYPE= ;;
*)
cleanup_and_exit 1 "VM '$VM_TYPE' is not supported"
;;
esac
shift
;;
-vm-worker)
needarg
VM_WORKER="$ARG"
shift
;;
-vm-worker-nr|-vm-worker-no)
needarg
VM_WORKER_NO="$ARG"
shift
;;
-vm-server|-vm-region)
needarg
VM_SERVER="$ARG"
shift
;;
-vm-disk)
needarg
VM_ROOT="$ARG"
shift
;;
-vm-swap|-xenswap|-swap)
needarg
VM_SWAP="$ARG"
shift
;;
-vm-memory|-xenmemory|-memory)
needarg
VM_MEMSIZE="$ARG"
shift
;;
-vm-kernel)
needarg
VM_KERNEL="$ARG"
shift
;;
-vm-initrd)
needarg
VM_INITRD="$ARG"
shift
;;
-vm-disk-size|-vmdisk-rootsize)
needarg
VMDISK_ROOTSIZE="$ARG"
shift
;;
-vm-swap-size|-vmdisk-swapsize)
needarg
VMDISK_SWAPSIZE="$ARG"
shift
;;
-vm-disk-filesystem|-vmdisk-filesystem)
needarg
VMDISK_FILESYSTEM="$ARG"
shift
;;
-vm-disk-filesystem-options|-vmdisk-filesystem-options)
needarg
VMDISK_FILESYSTEM_OPTIONS="$ARG"
shift
;;
-vm-disk-mount-options|-vmdisk-mount-options)
needarg
VMDISK_MOUNT_OPTIONS="$ARG"
# silly code for compat with old bs_worker versions...
if test "$1" != "----noarg=$PARAM" -a "$ARG" != "${ARG#\"}" -a "$ARG" != "${ARG%\"}" ; then
VMDISK_MOUNT_OPTIONS="${VMDISK_MOUNT_OPTIONS#\"}"
VMDISK_MOUNT_OPTIONS="${VMDISK_MOUNT_OPTIONS%\"}"
fi
shift
;;
-vm-disk-clean|-vmdisk-clean)
# delete old root/swap to get rid of the old blocks
VMDISK_CLEAN=true
;;
-vm-hugetlbfs|-hugetlbfs)
needarg
HUGETLBFSPATH="$ARG"
shift
;;
-vm-watchdog)
VM_WATCHDOG=true
;;
-vm-user)
needarg
VM_USER="$ARG"
shift
;;
-vm-enable-console)
VM_CONSOLE_INPUT=true
;;
-vm-telnet)
needarg
VM_TELNET="$ARG"
shift
;;
-vm-net)
needarg
VM_NETOPT=("${VM_NETOPT[@]}" "$ARG")
shift
;;
-vm-netdev)
needarg
VM_NETDEVOPT=("${VM_NETDEVOPT[@]}" "$ARG")
shift
;;
-vm-device)
needarg
VM_DEVICEOPT=("${VM_DEVICEOPT[@]}" "$ARG")
shift
;;
-vm-custom-opt)
needarg
VM_CUSTOMOPT="$ARG"
shift
;;
-vm-openstack-flavor|-openstack-flavor)
needarg
VM_OPENSTACK_FLAVOR="$ARG"
shift
;;
-*)
return 1
;;
esac
nextargs=("$@")
return 0
}
vm_set_buildstatus() {
if test -n "$VM_ROOT" -a -n "$VM_SWAP" -a "$VM_SWAP_TYPE" != unattached -a -e "$VM_SWAP" ; then
echo -n "BUILDSTATUS$1" >"$VM_SWAP"
fi
}
#
# shutdown the system from inside the VM
#
vm_shutdown() {
test -n "$VM_WATCHDOG" && echo "### VM INTERACTION START ###"
cd /
test -n "$1" || set 1
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
test "$VM_TYPE" = lxc -o "$VM_TYPE" = docker -o "$VM_TYPE" = nspawn && exit $1
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
sync # halt from systemd is not syncing anymore.
halt -f -p
fi
echo "Warning: clean shut down of the VM didn't work"
exit $1 # init died...
}
vm_img_create() {
local img="$1"
local size="$2"
if test -e "${img}" ; then
local origsize=$(cat "${img}.size" 2> /dev/null)
if test -z "$origsize" -o "$origsize" != "$size" ; then
echo "Resizing $img (${size}M)"
fi
else
echo "Creating $img (${size}M)"
rm -f "${img}.size"
fi
mkdir -p "${img%/*}" || cleanup_and_exit 4
# truncate file to the desired size
dd if=/dev/zero of="$img" bs=1M count=0 seek="$size" || cleanup_and_exit 4
echo "$size" > "${img}.size"
# allocate blocks
if type -p fallocate > /dev/null ; then
fallocate -p -l "${size}M" "$img" 2> /dev/null
errout=$( fallocate -l "${size}M" "$img" 2>&1 )
if test $? != 0; then
echo $errout
if test "${errout/Operation not supported/}" = "$errout"; then
# Do not fail on not support file systems, eg ext2 or ext3
cleanup_and_exit 4
fi
fi
fi
}
vm_img_wipe() {
vm_wipe_$VM_TYPE "$@"
if test -n "$VM_ROOT" -a "$VM_ROOT_TYPE" = file ; then
rm -f "$VM_ROOT"
fi
if test -n "$VM_SWAP" -a "$VM_SWAP_TYPE" = file ; then
rm -f "$VM_SWAP"
fi
}
vm_img_mkfs() {
local fs="$1"
local img="$2"
local options="$3"
local mkfs tunefs
case "$fs" in
ext[234]|xfs|btrfs|reiserfs) ;;
*) cleanup_and_exit 1 "filesystem \"$fs\" is not supported" ;;
esac
# defaults for creating the filesystem
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_mkfs_ext3='mkfs.ext3 -m 0 -q -F'
vm_img_mkfs_ext2='mkfs.ext2 -m 0 -q -F'
vm_img_mkfs_reiserfs='mkreiserfs -q -f'
vm_img_mkfs_btrfs='mkfs.btrfs'
vm_img_mkfs_xfs='mkfs.xfs -f'
# defaults for tuning the filesystem
vm_img_tunefs_ext4='tune2fs -c 0'
vm_img_tunefs_ext3='tune2fs -c 0 -o journal_data_writeback'
vm_img_tunefs_ext2='tune2fs -c 0'
var="vm_img_mkfs_${fs}"; mkfs="${!var}"
var="vm_img_mkfs_extra_options_${fs}"; mkfs_extra_options="${!var}"
var="vm_img_tunefs_${fs}"; vm_img_tunefs="${!var}"
local labelopt=
test "$VM_ROOTDEV" != "${VM_ROOTDEV#LABEL=}" && labelopt="-L ${VM_ROOTDEV#LABEL=}"
# extend options if wanted, multiple -O are fine
if test "$options" = "nodirindex" && test "$fs" = "ext2" -o "$fs" = "ext3" -o "$fs" = "ext4"; then
mkfs_extra_options="$mkfs_extra_options -O ^dir_index"
fi
if test -z "$mkfs"; then
cleanup_and_exit 1 "filesystem \"$fs\" is not supported"
fi
echo "Creating $fs filesystem on $img"
export MKE2FS_SYNC=0
if ! $mkfs $labelopt $mkfs_extra_options "$img" ; then
if test -z "$mkfs_extra_options" ; then
cleanup_and_exit 3
else
echo "Filesystem creation failed, trying again without extra options..."
$mkfs $labelopt "$img" || cleanup_and_exit 4
fi
fi
if test -n "$tunefs" ; then
$tunefs "$img" || cleanup_and_exit 4
fi
}
background_monitor_process() {
max_disk=0
max_mem=0
while sleep 5; do
test -e /.build/_statistics.exit && exit 0
# memory usage
if test -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 test ${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 test ${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
done
}
background_watchdog() {
WATCHDOG_START=
WATCHDOG_TIMEOUT=300
BUILD_OPTIONS_PARSED=
while sleep 5 ; do
WATCH=$(grep -a "### VM INTERACTION" "$LOGFILE" | tr '\0' a | tail -n 1)
case $WATCH in
*VM\ INTERACTION\ START*) test -n "$WATCHDOG_START" || WATCHDOG_START=`date +%s` ;;
*VM\ INTERACTION\ END*) WATCHDOG_START= ;;
esac
if test -n "$WATCHDOG_START" ; then
NOW=`date +%s`
ELAPSED=$((NOW-WATCHDOG_START))
if test $ELAPSED -gt $WATCHDOG_TIMEOUT ; then
# kill the VM
echo
echo "### WATCHDOG TRIGGERED, KILLING VM ###"
vm_kill
exit 0
fi
fi
done
}
start_watchdog() {
local wf=$(mktemp)
( background_watchdog & echo $! > "$wf" )
read VM_WATCHDOG_PID < "$wf"
rm -f "$wf"
}
kill_watchdog() {
test -n "$VM_WATCHDOG_PID" && kill "$VM_WATCHDOG_PID"
VM_WATCHDOG_PID=
}
vm_set_personality_syscall() {
local archname
archname=`perl -V:archname 2>/dev/null`
archname="${archname#archname=?}"
case "$archname" in
x86_64*) PERSONALITY_SYSCALL=135 ;;
alpha*) PERSONALITY_SYSCALL=324 ;;
sparc*) PERSONALITY_SYSCALL=191 ;;
ia64*) PERSONALITY_SYSCALL=1140 ;;
i?86*|ppc*|aarch64*|arm*|sh4|cris|m68k*|s390*|unicore32|microblaze|riscv*) PERSONALITY_SYSCALL=136 ;;
*) cleanup_and_exit 1 "Unknown architecture personality: '$archname'" ;;
esac
}
# used before calling kvm or xen
linux64() {
perl -e 'syscall('$PERSONALITY_SYSCALL', 0); exec(@ARGV) || die("$ARGV[0]: $!\n")' "$@"
}
vm_start_statistics() {
if test "$DO_STATISTICS" = 1 ; then
rm -f /.build/_statistics.exit
( background_monitor_process & )
fi
}
vm_exit_statistics() {
if test "$DO_STATISTICS" = 1 ; then
rm -f /.build/_statistics.exit
fi
}
vm_detect_2nd_stage() {
if test ! -e /.build/build.data -o -n "$BUILD_IGNORE_2ND_STAGE" ; then
return 1
fi
. /.build/build.data
if test -z "$VM_TYPE" ; then
return 1
fi
BUILD_OPTIONS_PARSED=true
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
test -n "$VM_WATCHDOG" -a -z "$PERSONALITY_SET" && echo "### VM INTERACTION END ###"
echo "2nd stage started in virtual machine"
# fedora packages sometimes do not have the needed links
ldconfig
BUILD_ROOT=/
BUILD_DIR=/.build
echo "machine type: `uname -m`"
echo "Linux version: `uname -rv`"
echo "Increasing log level from now on..."
echo 4 > /proc/sysrq-trigger
echo "Enable sysrq operations"
echo 1 > /proc/sys/kernel/sysrq
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 -a "$VM_TYPE" != docker -a "$VM_TYPE" != nspawn ; then
mount -n ${VMDISK_MOUNT_OPTIONS},remount,rw /
fi
umount /run >/dev/null 2>&1
# mount /sys
if ! test -e /sys/block; then
mkdir -p /sys
mount -orw -n -tsysfs sysfs /sys
# Docker already has sysfs mounted ro elsewhere,
# need to remount rw explicitly.
mount -o remount,rw sysfs /sys
fi
# qemu inside of xen does not work, check again with kvm later before enabling this
# if test -e /dev/kqemu ; then
# # allow abuild user to run qemu
# chmod 0666 /dev/kqemu
# fi
test -d /dev/shm || rm -f /dev/shm
mkdir -p /dev/pts
mkdir -p /dev/shm
mount -n -tdevpts -omode=0620,gid=5 none /dev/pts
mount -n -ttmpfs none /dev/shm
if test -n "$VM_SWAP" ; then
if test "$VM_SWAP" != "${VM_SWAP#LABEL=}" ; then
i=$(blkid -l -o device -t "$VM_SWAP")
if test "$i" = "${i#/}" ; then
cleanup_and_exit 1 "could not find swap device with $VM_SWAP"
fi
echo "resolved swap device $VM_SWAP to $i"
VM_SWAP=$i
fi
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
# Do not rely on external system writing the signature, it might differ...
mkswap "$VM_SWAP"
swapon -v "$VM_SWAP" || exit 1
fi
HOST="$VM_HOSTNAME"
# repair dracut damage, see bsc#922676
test -L /var/run -a ! -e /var/run && rm -f /var/run
test -L /var/lock -a ! -e /var/lock && rm -f /var/lock
# start a process monitoring max filesystem usage during build
vm_start_statistics
if test ! -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 test $(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
# Enable Core dump generation
mkdir -p "$BUILD_ROOT/.build/cores"
echo "/.build/cores/%p" > /proc/sys/kernel/core_pattern
return 0
}
vm_set_filesystem_type() {
vmfstype=""
vmfsoptions=""
if test -n "$BUILD_DIST" ; then
# testing for build specific filesystem, which are more important then worker defaults
vmfstype=`queryconfig --dist "$BUILD_DIST" --configdir "$CONFIG_DIR" --archpath "$BUILD_ARCH" buildflags vmfstype`
vmfsoptions=`queryconfig --dist "$BUILD_DIST" --configdir "$CONFIG_DIR" --archpath "$BUILD_ARCH" buildflags vmfsoptions`
fi
test -n "$vmfstype" && VMDISK_FILESYSTEM="$vmfstype"
test -n "$vmfsoptions" && VMDISK_FILESYSTEM_OPTIONS="$vmfsoptions"
# use either commandline specified fs or ext3 as fallback
test -n "$VMDISK_FILESYSTEM" || VMDISK_FILESYSTEM=ext3
}
vm_set_mount_options() {
if test "$VMDISK_MOUNT_OPTIONS" = __default; then
if test "$VMDISK_FILESYSTEM" = reiserfs ; then
VMDISK_MOUNT_OPTIONS='-o data=writeback,commit=150,noatime'
elif test "$VMDISK_FILESYSTEM" = btrfs ; then
VMDISK_MOUNT_OPTIONS='-o nobarrier,noatime'
elif test "$VMDISK_FILESYSTEM" = "ext4" ; then
VMDISK_MOUNT_OPTIONS='-o noatime'
elif test "$VMDISK_FILESYSTEM" = "ext3" ; then
VMDISK_MOUNT_OPTIONS='-o data=writeback,nobarrier,commit=150,noatime'
elif test "$VMDISK_FILESYSTEM" = "ext2" ; then
VMDISK_MOUNT_OPTIONS='-o noacl,noatime'
elif test "$VMDISK_FILESYSTEM" = "xfs" ; then
VMDISK_MOUNT_OPTIONS='-o noatime'
else
VMDISK_MOUNT_OPTIONS='-o noatime'
fi
fi
}
vm_set_defaults() {
# setup root/swap defaults and type, called after option verification
if test "$VM_ROOT" = 1 ; then
VM_ROOT="$BUILD_ROOT.img"
if test -z "$VM_SWAP" -a "$VM_TYPE" != emulator ; then
VM_SWAP="$BUILD_ROOT.swap"
fi
fi
if test -n "$VM_ROOT" -a -z "$VM_ROOT_TYPE" ; then
VM_ROOT_TYPE=file
test -b "$VM_ROOT" && VM_ROOT_TYPE=device
fi
if test -n "$VM_SWAP" -a -z "$VM_SWAP_TYPE" ; then
VM_SWAP_TYPE=file
test -b "$VM_SWAP" && VM_SWAP_TYPE=device
fi
}
#
# create swap space
#
vm_setup_swap() {
if test -n "$VMDISK_CLEAN" ; then
# delete old swap to get rid of the old blocks
if test -n "$VM_SWAP" -a "$VM_SWAP_TYPE" = file -a -f "$VM_SWAP" ; then
echo "Deleting old $VM_SWAP"
rm -f "$VM_SWAP"
fi
fi
if test -n "$VM_SWAP" -a "$VM_SWAP_TYPE" = file ; then
vm_img_create "$VM_SWAP" "$VMDISK_SWAPSIZE"
fi
if test -n "$VM_SWAP" ; then
vm_attach_swap
dd if=/dev/zero of="$VM_SWAP" bs=1024 count=1 conv=notrunc 2>/dev/null || cleanup_and_exit 4
if test "$VM_SWAPDEV" != "${VM_SWAPDEV#LABEL=}"; then
# call mkswap to set a label
mkswap -L "${VM_SWAPDEV#LABEL=}" "$VM_SWAP" || cleanup_and_exit 4
fi
vm_detach_swap
# mkswap happens inside of the vm
fi
}
#
# create file system, mount file system to $BUILD_ROOT
#
vm_setup() {
vm_set_filesystem_type
vm_set_mount_options
echo "VM_ROOT: $VM_ROOT, VM_SWAP: $VM_SWAP"
VM_USE_VIRT_FS=
if test -z "$RUNNING_IN_VM" -a -n "$VM_TYPE" -a -n "$VM_ROOT"; then
if test -x /usr/bin/virt-make-fs -a "$VMDISK_FILESYSTEM" = ext3 -a -z "$VMDISK_FILESYSTEM_OPTIONS" ; then
VM_USE_VIRT_FS=true
fi
fi
vm_attach_root
vm_setup_swap
# this should not be needed, but sometimes a xen instance got lost
test "$VM_TYPE" = xen && vm_purge_xen
if test -n "$VMDISK_CLEAN" ; then
# delete old root to get rid of the old blocks
if test -n "$VM_ROOT" -a "$VM_ROOT_TYPE" = file -a -f "$VM_ROOT" ; then
echo "Deleting old $VM_ROOT"
rm -f "$VM_ROOT"
fi
fi
# using virt-make-fs for ext3 file VMDISK_FILESYSTEM
# so nothing is needed to do part from creating appropriate folders
# and calling virt-make-fs accordingly
if test -n "$VM_USE_VIRT_FS" ; then
echo "Using virt-make-fs to create root image"
rm -rf "$BUILD_ROOT"/* "$BUILD_ROOT"/.[^.]*
if test -z "$CLEAN_BUILD" ; then
echo "Recovering former build root"
debugfs -f <(echo rdump / "$BUILD_ROOT") "$VM_ROOT"
fi
return 0
fi
if test "$VM_ROOT_TYPE" = file ; then
if test -n "$CLEAN_BUILD" ; then
vm_img_create "$VM_ROOT" "$VMDISK_ROOTSIZE"
else
local origrootsize
test -e "$VM_ROOT" -a -e "${VM_ROOT}.size" && origrootsize=$(cat "${VM_ROOT}.size" 2>/dev/null)
if test -z "$origrootsize" -o "$origrootsize" != "$VMDISK_ROOTSIZE" ; then
# the size has changed, re-create file system
vm_img_create "$VM_ROOT" "$VMDISK_ROOTSIZE"
vm_img_mkfs "$VMDISK_FILESYSTEM" "$VM_ROOT" "$VMDISK_FILESYSTEM_OPTIONS" || cleanup_and_exit 4
fi
fi
fi
if test ! -e "$VM_ROOT" ; then
cleanup_and_exit 4 "you need to create $VM_ROOT first"
fi
if test -n "$CLEAN_BUILD" ; then
vm_img_mkfs "$VMDISK_FILESYSTEM" "$VM_ROOT" "$VMDISK_FILESYSTEM_OPTIONS" || cleanup_and_exit 4
fi
# now mount root/swap
mkdir_build_root
if test -w /root ; then
if test -b $VM_ROOT ; then
# mount device directly
mount $VMDISK_MOUNT_OPTIONS $VM_ROOT $BUILD_ROOT || cleanup_and_exit 4
else
mount ${VMDISK_MOUNT_OPTIONS},loop $VM_ROOT $BUILD_ROOT || cleanup_and_exit 4
fi
else
if ! mount $BUILD_ROOT; then
echo "mounting the build root failed. An fstab entry is probably missing or incorrect."
echo "/etc/fstab should contain an entry like this:"
echo "$VM_ROOT $BUILD_ROOT auto noauto,user,loop 0 0"
cleanup_and_exit 4
fi
fi
}
vm_update_hostarch() {
local kernel="$vm_kernel"
local hostarchfile
local newhostarch
local vm_type="$VM_TYPE"
test "$vm_type" = "qemu" && vm_type=kvm
if test -z "$VM_KERNEL" -a -f "$BUILD_ROOT/.build.kernel.$vm_type" -a ! -L "$BUILD_ROOT/.build.kernel.$vm_type" ; then
kernel="$BUILD_ROOT/.build.kernel.$vm_type"
if test -f "$BUILD_ROOT/.build.hostarch.$vm_type" -a ! -L "$BUILD_ROOT/.build.hostarch.$vm_type" ; then
hostarchfile="$BUILD_ROOT/.build.hostarch.$vm_type"
fi
elif test -n "$kernel" -a -f "$kernel" -a -f "$kernel.hostarch" ; then
hostarchfile="$kernel.hostarch"
fi
if test -n "$hostarchfile" -a -f "$hostarchfile" ; then
local dummy
read newhostarch dummy < "$hostarchfile"
elif test -n "$kernel" -a -f "$kernel" ; then
case `objdump -f "$kernel" 2>/dev/null | sed -ne 's/.*file format //p'` in
elf64-powerpcle) newhostarch=ppc64le ;;
elf64-powerpc) newhostarch=ppc64 ;;
esac
fi
if test -n "$newhostarch" -a "$newhostarch" != "$BUILD_HOST_ARCH" ; then
echo "setting hostarch to $newhostarch"
BUILD_HOST_ARCH="$newhostarch"
# update BUILD_INITVM_ARCH
build_host_arch
fi
}
#
# prepare for vm startup
#
vm_first_stage() {
vm_set_personality_syscall
rm -rf "$BUILD_ROOT/.build"
mkdir -p "$BUILD_ROOT/.build"
TIME_PREINSTALL=
if test "$DO_INIT" = true ; then
# do first stage of init_buildsystem
rm -f $BUILD_ROOT/.build.success
set -- init_buildsystem --configdir "$CONFIG_DIR" --cachedir "$CACHE_DIR" --prepare "${initbuildsysstuff[@]}" "${definesnstuff[@]}" "${repos[@]}" $CLEAN_BUILD $USEUSEDFORBUILD $RPMLIST "$MYSRCDIR/$RECIPEFILE" $ADDITIONAL_PACKS
echo "$* ..."
start_time=`date +%s`
"$@" || cleanup_and_exit 1
check_exit
TIME_PREINSTALL=$(( `date +%s` - $start_time ))
unset start_time
if test ! -w /root ; then
# remove setuid bit if files belong to user to make e.g. mount work
find $BUILD_ROOT/{bin,sbin,usr/bin,usr/sbin} -type f -uid $UID -perm /4000 -print0 | xargs -0 --no-run-if-empty chmod -s
fi
copy_oldpackages
fi
# start up VM, rerun ourself
cp -a $BUILD_DIR/. $BUILD_ROOT/.build
if ! test "$MYSRCDIR" = $BUILD_ROOT/.build-srcdir ; then
rm -rf "$BUILD_ROOT/.build-srcdir"
mkdir "$BUILD_ROOT/.build-srcdir" || cleanup_and_exit 1
if test "$BUILDTYPE" = kiwi -o "$BUILDTYPE" = docker -o "$BUILDTYPE" = fissile -o "$BUILDTYPE" = podman ; then
cp -pRL "$MYSRCDIR"/* $BUILD_ROOT/.build-srcdir
else
cp -p "$MYSRCDIR"/* $BUILD_ROOT/.build-srcdir
fi
MYSRCDIR=$BUILD_ROOT/.build-srcdir
else
# cwd is at $BUILD_ROOT/.build-srcdir which we want to
# umount later so step aside
cd "$SRCDIR"
fi
# some time has passed copying files, lets make sure we should not exit
check_exit
# do vm specific fixups
vm_fixup
# update the hostarch
if test -n "$VM_ROOT" ; then
vm_update_hostarch
fi
# the watchdog needs a log file
test -n "$LOGFILE" || VM_WATCHDOG=
# put our config into .build/build.data
rm -rf $BUILD_ROOT/.build/build.data
Q="'\''"
echo "RECIPEFILE='${RECIPEFILE//"'"/$Q}'" > $BUILD_ROOT/.build/build.data
echo "BUILD_JOBS='${BUILD_JOBS//"'"/$Q}'" >> $BUILD_ROOT/.build/build.data
echo "BUILD_ARCH='${BUILD_ARCH//"'"/$Q}'" >> $BUILD_ROOT/.build/build.data
echo "BUILD_RPMS='${BUILD_RPMS//"'"/$Q}'" >> $BUILD_ROOT/.build/build.data
case $BUILD_DIST in
*/*)
cp $BUILD_DIST $BUILD_ROOT/.build/build.dist
BUILD_DIST=/.build/build.dist
;;
esac
echo "BUILD_DIST='${BUILD_DIST//"'"/$Q}'" >> $BUILD_ROOT/.build/build.data
echo "RELEASE='${RELEASE//"'"/$Q}'" >> $BUILD_ROOT/.build/build.data
echo "BUILD_DEBUG='${BUILD_DEBUG//"'"/$Q}'" >> $BUILD_ROOT/.build/build.data
echo "SIGNDUMMY='${SIGNDUMMY//"'"/$Q}'" >> $BUILD_ROOT/.build/build.data
echo "DO_LINT='${DO_LINT//"'"/$Q}'" >> $BUILD_ROOT/.build/build.data
echo "DO_CHECKS='${DO_CHECKS//"'"/$Q}'" >> $BUILD_ROOT/.build/build.data
echo "NOROOTFORBUILD='${NOROOTFORBUILD//"'"/$Q}'" >> $BUILD_ROOT/.build/build.data
echo "CREATE_BASELIBS='$CREATE_BASELIBS'" >> $BUILD_ROOT/.build/build.data
echo "REASON='${REASON//"'"/$Q}'" >> $BUILD_ROOT/.build/build.data
echo "CHANGELOG='${CHANGELOG//"'"/$Q}'" >> $BUILD_ROOT/.build/build.data
echo "INCARNATION='${INCARNATION//"'"/$Q}'" >> $BUILD_ROOT/.build/build.data
echo "DISTURL='${DISTURL//"'"/$Q}'" >> $BUILD_ROOT/.build/build.data
echo "DO_INIT='${DO_INIT//"'"/$Q}'" >> $BUILD_ROOT/.build/build.data
echo "DO_INIT_TOPDIR='${DO_INIT_TOPDIR//"'"/$Q}'" >> $BUILD_ROOT/.build/build.data
echo "KIWI_PARAMETERS='${KIWI_PARAMETERS//"'"/$Q}'" >> $BUILD_ROOT/.build/build.data
echo "VM_TELNET='${VM_TELNET//"'"/$Q}'" >> $BUILD_ROOT/.build/build.data
echo "VM_CONSOLE_INPUT='${VM_CONSOLE_INPUT//"'"/$Q}'" >> $BUILD_ROOT/.build/build.data
test -n "$VM_SWAP" && echo "VM_SWAP='${VM_SWAPDEV//"'"/$Q}'" >> $BUILD_ROOT/.build/build.data
test -n "$VMDISK_MOUNT_OPTIONS" && echo "VMDISK_MOUNT_OPTIONS='${VMDISK_MOUNT_OPTIONS//"'"/$Q}'" >> $BUILD_ROOT/.build/build.data
PERSONALITY=0
test -n "$PERSONALITY_SYSCALL" && PERSONALITY=`perl -e 'print syscall('$PERSONALITY_SYSCALL', 0)."\n"'`
test "$PERSONALITY" = -1 && PERSONALITY=0 # syscall failed?
case $(uname -m) in
ppc|ppcle|s390) PERSONALITY=8 ;; # ppc/s390 kernel never tells us if a 32bit personality is active, assume we run on 64bit
aarch64) test "$BUILD_ARCH" != "${BUILD_ARCH#armv[567]}" && PERSONALITY=8 ;; # workaround, to be removed
esac
test "$VM_TYPE" = lxc -o "$VM_TYPE" = docker -o "$VM_TYPE" = nspawn && PERSONALITY=0
echo "PERSONALITY='$PERSONALITY'" >> $BUILD_ROOT/.build/build.data
echo "VM_HOSTNAME='$HOST'" >> $BUILD_ROOT/.build/build.data
echo -n "definesnstuff=(" >> $BUILD_ROOT/.build/build.data
shellquote "${definesnstuff[@]}" >> $BUILD_ROOT/.build/build.data
echo ")" >> $BUILD_ROOT/.build/build.data
echo -n "repos=(" >> $BUILD_ROOT/.build/build.data
shellquote "${repos[@]}" >> $BUILD_ROOT/.build/build.data
echo ")" >> $BUILD_ROOT/.build/build.data
echo "VM_TYPE='$VM_TYPE'" >> $BUILD_ROOT/.build/build.data
echo "RUN_SHELL='$RUN_SHELL'" >> $BUILD_ROOT/.build/build.data
echo "RUN_SHELL_AFTER_FAIL='$RUN_SHELL_AFTER_FAIL'" >> $BUILD_ROOT/.build/build.data
echo "RUN_SHELL_CMD='$RUN_SHELL_CMD'" >> $BUILD_ROOT/.build/build.data
echo "DO_STATISTICS='$DO_STATISTICS'" >> $BUILD_ROOT/.build/build.data
echo "TIME_PREINSTALL='$TIME_PREINSTALL'" >> $BUILD_ROOT/.build/build.data
echo "VM_WATCHDOG='$VM_WATCHDOG'" >> $BUILD_ROOT/.build/build.data
echo "BUILDENGINE='$BUILDENGINE'" >> $BUILD_ROOT/.build/build.data
echo "CCACHE='$CCACHE'" >> $BUILD_ROOT/.build/build.data
echo "ABUILD_TARGET='$ABUILD_TARGET'" >> $BUILD_ROOT/.build/build.data
echo "BUILD_FLAVOR='$BUILD_FLAVOR'" >> $BUILD_ROOT/.build/build.data
echo "OBS_PACKAGE='$OBS_PACKAGE'" >> $BUILD_ROOT/.build/build.data
echo "REMOVE_CCACHE='$REMOVE_CCACHE'" >> $BUILD_ROOT/.build/build.data
# fallback time for broken hosts
rm -rf $BUILD_ROOT/.build/.date
date '+@%s' > $BUILD_ROOT/.build/.date
# we're done with the root file system, unmount
buildroot_umount /proc/sys/fs/binfmt_misc
buildroot_umount /proc
buildroot_umount /sys
buildroot_umount /dev/pts
buildroot_umount /dev/shm
buildroot_umount /mnt
vm_init_script="/.build/build"
if check_use_emulator ; then
vm_init_script="/.build/$INITVM_NAME"
fi
# rsync as source and dest could be same
test -L "$BUILD_ROOT/.build.oldpackages/_ccache.tar" && buildroot_rm .build.oldpackages/_ccache.tar
test -n "$PKG_CCACHE" && \
mkdir -p "$BUILD_ROOT/.build.oldpackages/" && \
rsync -av "$PKG_CCACHE" "$BUILD_ROOT/.build.oldpackages/_ccache.tar"
if test -n "$VM_ROOT" ; then
# copy out kernel & initrd (if they exist) during unmounting VM image
KERNEL_TEMP_DIR=
local vm_type="$VM_TYPE"
test "$vm_type" = "qemu" && vm_type=kvm
if test -z "$VM_KERNEL" -a -f "$BUILD_ROOT/.build.kernel.$vm_type" -a ! -L "$BUILD_ROOT/.build.kernel.$vm_type" ; then
KERNEL_TEMP_DIR=`mktemp -d`
cp "$BUILD_ROOT/.build.kernel.$vm_type" "$KERNEL_TEMP_DIR/kernel"
if test -f "$BUILD_ROOT/.build.initrd.$vm_type" -a ! -L "$BUILD_ROOT/.build.initrd.$vm_type" ; then
cp "$BUILD_ROOT/.build.initrd.$vm_type" "$KERNEL_TEMP_DIR/initrd"
fi
fi
check_exit
# needs to work otherwise we have a corrupted file system
if test -z "$VM_USE_VIRT_FS" ; then
if ! umount $BUILD_ROOT; then
test -n "$KERNEL_TEMP_DIR" && rm -rf "$KERNEL_TEMP_DIR"
cleanup_and_exit 4
fi
else
export LIBGUESTFS_CACHEDIR=$BUILD_ROOT/.guestfs
test -d $BUILD_ROOT/.guestfs || mkdir $BUILD_ROOT/.guestfs
/usr/bin/virt-make-fs --format=raw -t ext3 --size="$VMDISK_ROOTSIZE"M $BUILD_ROOT $VM_ROOT || cleanup_and_exit 4
#rm -rf -- "$BUILD_ROOT"/*
fi
# copy back the kernel and set it for VM
if test -n "$KERNEL_TEMP_DIR" ; then
rm -rf "$BUILD_ROOT/boot"
mkdir -p "$BUILD_ROOT/boot"
mv "$KERNEL_TEMP_DIR/kernel" "$BUILD_ROOT/boot/kernel"
vm_kernel="$BUILD_ROOT/boot/kernel"
if test -e "$KERNEL_TEMP_DIR/initrd" ; then
mv "$KERNEL_TEMP_DIR/initrd" "$BUILD_ROOT/boot/initrd"
test -z "$VM_INITRD" && vm_initrd="$BUILD_ROOT/boot/initrd"
fi
rmdir "$KERNEL_TEMP_DIR"
fi
fi
vm_detach_root