Tweaks to support 32- and 64-bit VMs better
[training-lab.git] / scripts / start_runtime
1 #!/bin/sh
2
3 SSH_PORT=2222
4 SHARE=/vagrant
5 MEM=512
6 TIMEOUT=600 # 10 minutes
7
8 # Are we native, or running under full emulation?
9 MACHINE=$(uname -m)
10 case $MACHINE in
11     aarch64)
12         if [ "$MACH"x = ""x ] || \
13                [ "$MACH"x = "aarch64"x ] || \
14                [ "$MACH"x = "arm64" ] ; then
15             MACH=aarch64
16             QEMU=qemu-system-aarch64
17             CPU_OPTS="-cpu host --enable-kvm"
18             MACH_OPTS="-machine virt,gic-version=host,kernel_irqchip=on"
19         elif [ "$MACH"x = "arm"x ]; then
20             MACH=arm
21             QEMU=qemu-system-aarch64
22             CPU_OPTS="-cpu host,aarch64=off --enable-kvm"
23             MACH_OPTS="-machine virt,gic-version=host,kernel_irqchip=on"
24         else
25             echo "Unknown MACH $MACH; abort"
26             exit 1
27         fi
28         ;;
29     *)
30         if [ "$MACH"x = ""x ] || \
31                [ "$MACH"x = "aarch64"x ] || \
32                [ "$MACH"x = "arm64" ] ; then
33             MACH=aarch64
34             QEMU=qemu-system-aarch64
35             CPU_OPTS="-cpu cortex-a57"
36             MACH_OPTS="-M virt"
37         elif [ "$MACH"x = "arm"x ]; then
38             MACH=arm
39             QEMU=qemu-system-arm
40             CPU_OPTS="-cpu cortex-a15"
41             MACH_OPTS="-M virt"
42         else
43             echo "Unknown MACH $MACH; abort"
44             exit 1
45         fi
46         ;;
47 esac
48
49 if [ "$DISK"x = ""x ] ; then
50     DISK=runtime-hd.img
51 fi
52
53 #DISPLAY="-vga none -nographic"
54 DISPLAY="-daemonize -vnc :1"
55 PFLASH="-pflash $MACH-OVMF.fd -pflash $MACH-storage.fd"
56
57 if [ "$CD"x != ""x ]; then
58     CD="-device virtio-scsi-pci,id=scsi -drive if=none,id=cd,file=$CD,readonly -device scsi-cd,drive=cd,bootindex=0"
59 fi
60
61 if [ "$NOSHARE"x = ""x ]; then
62     P9="-virtfs local,path=$SHARE/,mount_tag=host0,security_model=passthrough,id=host0"
63     if [ -f ~/.ssh/authorized_keys ]; then
64         cp ~/.ssh/authorized_keys $SHARE/runtime/vagrant-pub-key
65     fi
66     touch $SHARE/runtime/waiting_runtime_vm
67 fi
68
69 NET="-netdev user"
70 NET="$NET,id=eth0"
71 NET="$NET,hostfwd=tcp:127.0.0.1:${SSH_PORT}-10.0.2.15:22"
72 NET="$NET -device virtio-net-device,netdev=eth0"
73
74 CMD="$QEMU -m $MEM \
75     $DISPLAY \
76     $MACH_OPTS $CPU_OPTS \
77     $PFLASH \
78     -drive file=$DISK,format=raw,if=virtio \
79     $NET \
80     $CD \
81     $P9"
82
83 echo $CMD
84 $CMD
85
86 i=0
87 while true; do
88     echo "Waiting for qemu VM to come ready ($i seconds)"
89     if [ ! -f $SHARE/runtime/waiting_runtime_vm ]; then
90         echo "ready after $i seconds"
91         exit 0
92     fi
93     sleep 1
94     i=$(($i + 1))
95 done
96
97 # else the VM timed out
98 echo "qemu VM startup took too long: $i seconds. Abort"
99 exit 1