Add support for a runtime argument to choose the runtime type
[training-lab.git] / bootstrap-toolchain-vm.sh
1 #!/bin/sh
2 #
3 # Runtime provisioning script for Vagrant-based training lab setup
4
5 # runtime type - arm32 (default) or arm64
6 RUNTIME=$1
7 if [ "$RUNTIME"x = ""x ]; then
8     RUNTIME=arm32
9 fi
10
11 # Location of the runtime files. MANIFEST must exist there, and
12 # describes what else needs to be downloaded: in the format:
13 # <FILENAME>:<MODE>:<BYTES>:<SHA256>
14 # Apart from the MANIFEST itself, all files should be compressed with
15 # gzip. In each case, the script will download $FILENAME.gz,
16 # decompress it and then validate the checksum
17 RUNTIME_DL=https://www.einval.com/arm/training-lab/$RUNTIME/
18
19 # Abort on errors
20 set -e
21 # set -x
22
23 # Make sure we have updates applied, and all our needed packages. Kill
24 # inattended-upgrades if it's running, as it will clash here
25 killall unattended-upgrade unattended-upgrades
26 apt-get update
27 apt-get dist-upgrade -y
28 apt-get install -y qemu-system-arm gcc-aarch64-linux-gnu
29 echo "Toolchain VM running!"
30
31 # Now grab the emulated runtime and start that
32 cd /vagrant
33 if [ ! -d runtime ]; then
34     mkdir runtime
35 fi
36 cd runtime
37
38 echo "Checking / downloading files needed for the emulated runtime VM"
39 # Grab all the files we need, and check they're valid
40 echo "  Downloading MANIFEST"
41 wget -nv -O MANIFEST $RUNTIME_DL/MANIFEST 
42 for LINE in $(cat MANIFEST); do
43     FILENAME=$(echo $LINE | awk -F: '{print $1}')
44     MODE=$(echo $LINE | awk -F: '{print $2}')
45     BYTES=$(echo $LINE | awk -F: '{print $3}')
46     SHA=$(echo $LINE | awk -F: '{print $4}')
47     DL_NEEDED=1
48     
49     # Quick and dirty - if the file exists and is the right size,
50     # we'll believe it
51     echo "  Checking $FILENAME.gz"
52     if [ -f $FILENAME ]; then
53         SIZE=$(stat -c%s $FILENAME)
54         if [ $SIZE = $BYTES ]; then
55             DL_NEEDED=0
56         fi
57     fi
58
59     if [ $DL_NEEDED = 1 ]; then
60         # Grab a compressed version of the file, and extract it as we
61         # go
62         echo "  Downloading $FILENAME.gz"
63         wget -nv -O- $RUNTIME_DL/$FILENAME.gz | gzip -cd > $FILENAME
64         SHA_FILE=$(sha256sum $FILENAME | awk '{print $1}')
65         if [ $SHA_FILE != $SHA ]; then
66             echo "Failed to download $FILENAME.gz correctly. Abort"
67             exit 1
68         fi
69     fi
70     chmod $MODE $FILENAME
71 done
72
73 echo "Starting emulated runtime VM next"
74 MACH=$RUNTIME ./start_runtime