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