Add --smp option.
[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
30 TEMP=`getopt \
31         -o '' \
32         --longoptions 'help,nested,smp:' \
33         -n run-supernested.sh -- "$@"`
34 if [ $? != 0 ]; then
35     echo "$0: problem parsing the command line arguments"
36     exit 1
37 fi
38 eval set -- "$TEMP"
39
40 usage ()
41 {
42     echo "$0: Run the supernested appliance."
43     echo "  $0 [--smp=N]"
44     echo "Options:"
45     echo "  --smp=N    Use N virtual CPUs (at all levels)."
46 }
47
48 while true ; do
49     case "$1" in
50         --nested)
51             # When the nested guest calls this script, it uses the
52             # --nested flag.  DO NOT ADD THIS FLAG YOURSELF!
53             image=/dev/sda
54             format=raw
55             kernel=/kernel
56             initrd=/initrd
57             smp="$(
58                 grep -Eo 'supernested.smp=[[:digit:]]+' /proc/cmdline |
59                 grep -Eo '[[:digit:]]+'
60                 )"
61             if [ -n "$smp" ]; then smp="-smp $smp"; fi
62             shift
63             ;;
64         --smp)
65             smp="-smp $2"
66             append="$append supernested.smp=$2"
67             shift 2
68             ;;
69         --help)
70             usage
71             exit 0
72             ;;
73         --)
74             shift
75             break
76             ;;
77         *)
78             echo "internal error ($1)"
79             exit 1
80             ;;
81     esac
82 done
83
84 # How much memory to give to the guest?
85 memory="$(
86     grep -Eo '^MemTotal:.*[[:digit:]]+ kB' /proc/meminfo |
87     grep -Eo '[[:digit:]]+'
88     )"
89 if [ -z "memory" ]; then
90     echo "$0: could not get total memory from /proc/meminfo"
91     exit 1
92 fi
93 memory=$((memory / 1024))
94 if [ $memory -lt 2048 ]; then
95     echo "$0: not enough memory left: memory=$memory MB"
96     exit 1
97 fi
98 memory=$((memory - 1500))
99
100 # Get current nesting level from /proc/cmdline, increment it, add it
101 # to the command line.
102 level="$(
103     grep -Eo 'supernested.level=[[:digit:]]+' /proc/cmdline |
104     grep -Eo '[[:digit:]]+'
105     )"
106 if [ -z "$level" ]; then level=0; fi
107 append="$append supernested.level=$((level+1))"
108
109 echo "Running"
110 echo "    qemu = $qemu"
111 echo "    smp = $smp"
112 echo "    memory = $memory"
113 echo "    kernel = $kernel"
114 echo "    initrd = $initrd"
115 echo "    disk = $image"
116 echo "    append = $append"
117
118 # Since this could crash the machine, better to sync ...
119 sync
120
121 exec \
122 "$qemu" \
123     -cpu host \
124     -machine accel=kvm \
125     $smp \
126     -display none \
127     -m $memory \
128     -no-reboot \
129     -rtc driftfix=slew \
130     -no-hpet \
131     -global kvm-pit.lost_tick_policy=discard \
132     -kernel "$kernel" \
133     -initrd "$initrd" \
134     -device virtio-scsi-pci,id=scsi \
135     -drive file="$image",snapshot=on,cache=unsafe,format="$format",id=hd0,if=none \
136     -device scsi-hd,drive=hd0 \
137     -device virtio-serial-pci \
138     -serial stdio \
139     -device sga \
140     -append "$append"