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