update to upstream 20150115
[tools/build.git] / build-vm-kvm
1 #
2 # kvm/qemu specific functions
3 #
4 ################################################################
5 #
6 # Copyright (c) 1995-2014 SUSE Linux Products GmbH
7 #
8 # This program is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License version 2 or 3 as
10 # published by the Free Software Foundation.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with this program (see the file COPYING); if not, write to the
19 # Free Software Foundation, Inc.,
20 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
21 #
22 ################################################################
23
24 kvm_bin=/usr/bin/qemu-kvm
25 kvm_console=ttyS0
26
27 # assume virtio support by default
28 kvm_device=virtio-blk-pci
29 kvm_options=
30
31 kvm_check_ppc970() {
32     if ! grep -q -E '(kvm_rma_count.*kvm_hpt_count)|(kvm_hpt_count.*kvm_rma_count)' /proc/cmdline ; then 
33         echo "put kvm_rma_count=<VM number> or kvm_hpt_count=<> to your boot options"
34         cleanup_and_exit 3
35     fi
36 }
37
38 kvm_check_hugetlb() {
39     if ! grep -q "$HUGETLBFSPATH" /proc/mounts ; then
40         echo "hugetlbfs is not mounted to $HUGETLBFSPATH"
41         cleanup_and_exit 3
42     fi
43     local HUGETLBBLKSIZE=$(stat -f -c "%S" "$HUGETLBFSPATH")
44     HUGETLBBLKSIZE=$(( ${HUGETLBBLKSIZE:-0} / 1024 ))
45     if test "$HUGETLBBLKSIZE" -lt 1 -o ! -e  "/sys/kernel/mm/hugepages/hugepages-${HUGETLBBLKSIZE}kB" ; then
46         echo "could not determine hugetlbfs block size"
47         cleanup_and_exit 3
48     fi
49     local PAGES_FREE=$(cat /sys/kernel/mm/hugepages/hugepages-${HUGETLBBLKSIZE}kB/free_hugepages)
50     local PAGES_REQ=$(( ${VM_MEMSIZE:-64} * 1024 / $HUGETLBBLKSIZE ))
51     if test "$PAGES_FREE" -lt "$PAGES_REQ" ; then
52         echo "expected $PAGES_REQ to be available (have $PAGES_FREE)"
53         echo "please adjust nr_hugepages"
54         cleanup_and_exit 3
55     fi
56 }
57
58 vm_verify_options_kvm() {
59     vm_kernel=
60     vm_initrd=
61     
62     # overwrite some options for specific host architectures
63     case `uname -m` in
64         armv7l)
65             kvm_bin="/usr/bin/qemu-system-arm"
66             kvm_console=ttyAMA0
67             kvm_options="-enable-kvm -M vexpress-a15 -dtb /boot/a15-guest.dtb -cpu cortex-a15"
68             vm_kernel=/boot/zImage
69             vm_initrd=/boot/initrd
70             # prefer the guest kernel/initrd
71             test -e /boot/zImage.guest && vm_kernel=/boot/zImage.guest
72             test -e /boot/initrd.guest && vm_initrd=/boot/initrd.guest
73             kvm_device=virtio-blk-device
74             ;;
75         aarch64)
76             kvm_bin="/usr/bin/qemu-system-aarch64"
77             kvm_console=ttyAMA0
78             kvm_options="-enable-kvm -M virt -cpu host"
79             vm_kernel=/boot/Image
80             vm_initrd=/boot/initrd
81             # prefer the guest kernel/initrd
82             test -e /boot/Image.guest && vm_kernel=/boot/Image.guest
83             test -e /boot/initrd.guest && vm_initrd=/boot/initrd.guest
84             kvm_device=virtio-blk-device
85             ;;
86         ppc|ppcle|ppc64|ppc64le)
87             kvm_bin="/usr/bin/qemu-system-ppc64"
88             kvm_console=hvc0
89             kvm_options="-enable-kvm -M pseries"
90             grep -q PPC970MP /proc/cpuinfo && kvm_check_ppc970
91             vm_kernel=/boot/vmlinux
92             vm_initrd=/boot/initrd
93             if test "$BUILD_ARCH" = ppc64le -a -e /boot/vmlinuxle ; then
94                 vm_kernel=/boot/vmlinuxle
95                 vm_initrd=/boot/initrdle
96             fi
97             grep -q "pSeries" /proc/cpuinfo && kvm_device=scsi-hd       # no virtio on pSeries
98             grep -q "PowerNV" /proc/cpuinfo || kvm_device=scsi-hd       # no virtio on ppc != power7 yet
99             ;;
100         s390|s390x)
101             kvm_bin="/usr/bin/qemu-system-s390x"
102             kvm_options="-enable-kvm"
103             kvm_console=hvc0
104             vm_kernel=/boot/image
105             vm_initrd=/boot/initrd
106             kvm_device=virtio-blk-ccw
107             ;;
108     esac
109
110     # check if we can run kvm
111     if ! test -r /dev/kvm -a -x "$kvm_bin" ; then
112         echo "host does not support kvm"
113         echo "either the kvm kernel-module is not loaded or kvm is not installed or hardware virtualization is deactivated in the BIOS."
114         cleanup_and_exit 3
115     fi
116
117     # check hugepages
118     test -n "$HUGETLBFSPATH" -a "$VM_TYPE" = kvm && kvm_check_hugetlb
119
120     # set kernel
121     test -n "$VM_KERNEL" && vm_kernel="$VM_KERNEL"
122     test -z "$vm_kernel" && vm_kernel=/boot/vmlinuz
123
124     # set initrd
125     test -n "$VM_INITRD" && vm_initrd="$VM_INITRD"
126     if test -z "$vm_initrd" ; then
127         # find a nice default
128         if test -e "/boot/initrd-build" ; then
129             vm_initrd="/boot/initrd-build"
130         elif test -e "/boot/initrd-virtio" ; then
131             vm_initrd="/boot/initrd-virtio"
132         else
133             vm_initrd="/boot/initrd"
134             kvm_device=ide-hd
135             # use /etc/sysconfig/kernel as indication if we have virtio
136             if test -e /etc/sysconfig/kernel ; then
137                 local im=$(INITRD_MODULES=; . /etc/sysconfig/kernel; echo "$INITRD_MODULES")
138                 if test "$im" != "${im/virtio/}" ; then
139                     kvm_device=virtio-blk-pci
140                 fi
141             fi
142         fi
143     fi
144
145     case $kvm_device in
146         virtio*)
147             qemu_rootdev=/dev/disk/by-id/virtio-0
148             VM_SWAPDEV=/dev/disk/by-id/virtio-1
149             ;;
150         *)
151             qemu_rootdev=/dev/sda
152             VM_SWAPDEV=/dev/sdb
153             ;;
154     esac
155 }
156
157 vm_startup_kvm() {
158     qemu_bin="$kvm_bin"
159     qemu_args=(-drive file="$VM_IMAGE",if=none,id=disk,serial=0,cache=unsafe -device "$kvm_device",drive=disk)
160     if test -n "$VM_SWAP" ; then
161         qemu_args=("${qemu_args[@]}" -drive file="$VM_SWAP",if=none,id=swap,serial=1,cache=unsafe -device "$kvm_device",drive=swap)
162     fi
163
164     if test -n "$BUILD_JOBS" -a "$icecream" = 0 -a -z "$BUILD_THREADS" ; then
165         qemu_args=("${qemu_args[@]}" "-smp" "$BUILD_JOBS")
166     elif test -n "$BUILD_JOBS" -a -n "$BUILD_THREADS" ; then
167         qemu_args=("${qemu_args[@]}" "-smp" "$BUILD_JOBS,threads=$BUILD_THREADS")
168     fi
169     if test "$VM_TYPE" = kvm ; then
170         test "$kvm_console" != ttyAMA0 && kvm_options="$kvm_options -cpu host"
171         test -n "$HUGETLBFSPATH" && kvm_options="$kvm_options -mem-prealloc -mem-path $HUGETLBFSPATH"
172     fi
173     set -- $qemu_bin -no-reboot -nographic -vga none -net none $kvm_options \
174         -kernel $vm_kernel \
175         -initrd $vm_initrd \
176         -append "root=$qemu_rootdev panic=1 quiet no-kvmclock nmi_watchdog=0 rw rd.driver.pre=binfmt_misc elevator=noop console=$kvm_console init=$vm_init_script" \
177         ${VM_MEMSIZE:+-m $VM_MEMSIZE} \
178         "${qemu_args[@]}"
179
180     if test "$PERSONALITY" != 0 ; then
181         # have to switch back to PER_LINUX to make qemu work
182         set -- linux64 "$@"
183     fi
184     export QEMU_AUDIO_DRV=none          # we do not want to have sound inside the VMs
185     echo "$@"
186     "$@"
187 }
188
189 vm_kill_kvm() {
190     if ! fuser -k -TERM "$VM_IMAGE" ; then
191         echo "could not kill build in $VM_IMAGE"
192         cleanup_and_exit 1
193     fi
194 }
195
196 vm_fixup_kvm() {
197     # check if we will use a kernel from the build root, in this case
198     # we assume the kernel does virtio
199     if test -z "$VM_KERNEL" -a -e "$BUILD_ROOT/.build.kernel.$VM_TYPE" ; then
200         # ide-hd is the non-virtio default
201         if test "$kvm_device" = ide-hd ; then
202             kvm_device=virtio-blk-pci
203             qemu_rootdev=/dev/disk/by-id/virtio-0
204             VM_SWAPDEV=/dev/disk/by-id/virtio-1
205         fi
206     fi
207 }
208
209 vm_attach_root_kvm() {
210     :
211 }
212
213 vm_attach_swap_kvm() {
214     :
215 }
216
217 vm_detach_root_kvm() {
218     :
219 }
220
221 vm_detach_swap_kvm() {
222     :
223 }
224
225 vm_cleanup_kvm() {
226     :
227 }
228