Fix --tcg option (it broken KVM).
[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 qemu="@QEMU@"
23 image="supernested-@VERSION@-@DISTRO@.qcow2"
24 format=qcow2
25 kernel=kernel
26 initrd=initrd
27 append="panic=1 console=ttyS0 udevtimeout=6000 no_timer_check printk.time=1 root=/dev/sda selinux=0"
28 smp=
29 accel=kvm
30 cpu="-cpu host"
31
32 TEMP=`getopt \
33         -o '' \
34         --longoptions 'help,nested,smp:,tcg' \
35         -n run-supernested.sh -- "$@"`
36 if [ $? != 0 ]; then
37     echo "$0: problem parsing the command line arguments"
38     exit 1
39 fi
40 eval set -- "$TEMP"
41
42 usage ()
43 {
44     echo "$0: Run the supernested appliance."
45     echo "  $0 [--smp=N] [--tcg]"
46     echo "Options:"
47     echo "  --smp=N    Use N virtual CPUs (at all levels)."
48     echo "  --tcg      Use TCG (at all levels) instead of KVM."
49 }
50
51 while true ; do
52     case "$1" in
53         --nested)
54             # When the nested guest calls this script, it uses the
55             # --nested flag.  DO NOT ADD THIS FLAG YOURSELF!
56             image=/dev/sda
57             format=raw
58             kernel=/kernel
59             initrd=/initrd
60             smp="$(
61                 grep -Eo 'supernested.smp=[[:digit:]]+' /proc/cmdline |
62                 grep -Eo '[[:digit:]]+'
63                 )"
64             if [ -n "$smp" ]; then smp="-smp $smp"; fi
65             accel="$(
66                 grep -Eo 'supernested.accel=tcg' /proc/cmdline
67                 )"
68             if [ -n "$accel" ]; then
69                 accel=tcg
70                 cpu=
71             else
72                 accel=kvm
73             fi
74             shift
75             ;;
76         --smp)
77             smp="-smp $2"
78             append="$append supernested.smp=$2"
79             shift 2
80             ;;
81         --tcg)
82             accel=tcg
83             cpu=
84             append="$append supernested.accel=tcg"
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 # Get current nesting level from /proc/cmdline, increment it, add it
119 # to the command line.
120 level="$(
121     grep -Eo 'supernested.level=[[:digit:]]+' /proc/cmdline |
122     grep -Eo '[[:digit:]]+'
123     )"
124 if [ -z "$level" ]; then level=0; fi
125 append="$append supernested.level=$((level+1))"
126
127 echo "Running"
128 echo "    qemu = $qemu"
129 echo "    cpu = $cmp"
130 echo "    smp = $smp"
131 echo "    accel = $accel"
132 echo "    memory = $memory"
133 echo "    kernel = $kernel"
134 echo "    initrd = $initrd"
135 echo "    disk = $image"
136 echo "    append = $append"
137
138 # Since this could crash the machine, better to sync ...
139 sync
140
141 exec \
142 "$qemu" \
143     $cpu \
144     $smp \
145     -machine accel=$accel \
146     -display none \
147     -m $memory \
148     -no-reboot \
149     -rtc driftfix=slew \
150     -no-hpet \
151     -global kvm-pit.lost_tick_policy=discard \
152     -kernel "$kernel" \
153     -initrd "$initrd" \
154     -device virtio-scsi-pci,id=scsi \
155     -drive file="$image",snapshot=on,cache=unsafe,format="$format",id=hd0,if=none \
156     -device scsi-hd,drive=hd0 \
157     -device virtio-serial-pci \
158     -serial stdio \
159     -device sga \
160     -append "$append"