Only use qemu -no-hpet option on x86.
[supernested.git] / run-supernested.sh.in
1 #!/bin/bash -
2 # supernested Makefile.am
3 # @configure_input@
4 # (C) Copyright 2014 Red Hat Inc.
5 #
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 #
20 # Written by Richard W.M. Jones <rjones@redhat.com>
21
22 arch="@host_cpu@"
23 qemu="@QEMU@"
24 image="supernested-@VERSION@-@DISTRO@.qcow2"
25 format=qcow2
26 kernel=kernel
27 initrd=initrd
28 append="panic=1 udevtimeout=6000 udev.event-timeout=6000 no_timer_check printk.time=1 root=/dev/sda selinux=0"
29 smp=
30 tcg=
31
32 case "$arch" in
33     aarch64)
34         append="$append earlyprintk=pl011,0x9000000 ignore_loglevel efi-rtc=noprobe"
35         ;;
36     *)
37         append="$append console=ttyS0"
38         ;;
39 esac
40
41 TEMP=`getopt \
42         -o '' \
43         --longoptions 'help,nested,smp:,tcg' \
44         -n run-supernested.sh -- "$@"`
45 if [ $? != 0 ]; then
46     echo "$0: problem parsing the command line arguments"
47     exit 1
48 fi
49 eval set -- "$TEMP"
50
51 usage ()
52 {
53     echo "$0: Run the supernested appliance."
54     echo "  $0 [--smp=N] [--tcg]"
55     echo "Options:"
56     echo "  --smp=N    Use N virtual CPUs (at all levels)."
57     echo "  --tcg      Use TCG (at all levels) instead of KVM."
58 }
59
60 while true ; do
61     case "$1" in
62         --nested)
63             # When the nested guest calls this script, it uses the
64             # --nested flag.  DO NOT ADD THIS FLAG YOURSELF!
65             image=/dev/sda
66             format=raw
67             kernel=/kernel
68             initrd=/initrd
69             smp="$(
70                 grep -Eo 'supernested.smp=[[:digit:]]+' /proc/cmdline |
71                 grep -Eo '[[:digit:]]+'
72                 )"
73             tcg=
74             if grep -q supernested.tcg=1 /proc/cmdline; then
75                 tcg=1
76             fi
77             shift
78             ;;
79         --smp)
80             smp="$2"
81             shift 2
82             ;;
83         --tcg)
84             tcg=1
85             shift
86             ;;
87         --help)
88             usage
89             exit 0
90             ;;
91         --)
92             shift
93             break
94             ;;
95         *)
96             echo "internal error ($1)"
97             exit 1
98             ;;
99     esac
100 done
101
102 # How much memory to give to the guest?
103 memory="$(
104     grep -Eo '^MemTotal:.*[[:digit:]]+ kB' /proc/meminfo |
105     grep -Eo '[[:digit:]]+'
106     )"
107 if [ -z "memory" ]; then
108     echo "$0: could not get total memory from /proc/meminfo"
109     exit 1
110 fi
111 memory=$((memory / 1024))
112 if [ $memory -lt 2048 ]; then
113     echo "$0: not enough memory left: memory=$memory MB"
114     exit 1
115 fi
116 memory=$((memory - 1500))
117
118 # ARM mach_virt (-M virt) model only supports 30GB of RAM.
119 if [ $memory -gt 30000 ]; then
120     case "$arch" in
121         arm*|aarch64)
122             echo "$0: limiting memory to 30000 MB for ARM mach_virt"
123             memory=30000
124             ;;
125         *)
126     esac
127 fi
128
129 # Get current nesting level from /proc/cmdline, increment it, add it
130 # to the command line.
131 level="$(
132     grep -Eo 'supernested.level=[[:digit:]]+' /proc/cmdline |
133     grep -Eo '[[:digit:]]+'
134     )"
135 if [ -z "$level" ]; then level=0; fi
136 append="$append supernested.level=$((level+1))"
137
138 # Append other parameters to nested command line.
139 append="$append supernested.smp=$smp supernested.tcg=$tcg"
140
141 extra_args=
142 if [ -n "$smp" ]; then extra_args="$extra_args -smp $smp"; fi
143 if [ -n "$tcg" ]; then
144     extra_args="$extra_args -machine accel=tcg"
145 else
146     extra_args="$extra_args -cpu host -machine accel=kvm"
147 fi
148
149 case "$arch" in
150     i[3456]86|x86_64)
151         extra_args="$extra_args -no-hpet"
152         ;;
153     *)
154         ;;
155 esac
156
157 echo "Running"
158 echo "    qemu = $qemu"
159 echo "    extra_args = $extra_args"
160 echo "    memory = $memory"
161 echo "    kernel = $kernel"
162 echo "    initrd = $initrd"
163 echo "    disk = $image"
164 echo "    append = $append"
165
166 # Since this could crash the machine, better to sync ...
167 sync
168
169 exec \
170 "$qemu" \
171     $extra_args \
172     -display none \
173     -m $memory \
174     -no-reboot \
175     -rtc driftfix=slew \
176     -global kvm-pit.lost_tick_policy=discard \
177     -kernel "$kernel" \
178     -initrd "$initrd" \
179     -device virtio-scsi-pci,id=scsi \
180     -drive file="$image",snapshot=on,cache=unsafe,format="$format",id=hd0,if=none \
181     -device scsi-hd,drive=hd0 \
182     -device virtio-serial-pci \
183     -serial stdio \
184     -device sga \
185     -append "$append"