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