Fix arch detection
[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 shift
11 OPTS=$1
12
13 # Location of the runtime files. MANIFEST must exist there, and
14 # describes what else needs to be downloaded: in the format:
15 # <FILENAME>:<MODE>:<BYTES>:<SHA256>
16 # Apart from the MANIFEST itself, all files should be compressed with
17 # gzip. In each case, the script will download $FILENAME.gz,
18 # decompress it and then validate the checksum
19 if [ "$OPTS"x = "localtest"x ]; then
20     RUNTIME_DL=http://www.einval.org/share/arm-security/$RUNTIME/
21 else
22     RUNTIME_DL=https://www.einval.com/arm/training-lab/$RUNTIME/
23 fi
24
25 # Abort on errors
26 set -e
27 # set -x
28
29 # Make sure we have updates applied, and all our needed packages. Kill
30 # inattended-upgrades if it's running, as it will clash here
31 killall unattended-upgrade unattended-upgrades || true
32 apt-get update
33 apt-get dist-upgrade -y
34 apt-get install -y qemu-system-arm gcc-aarch64-linux-gnu
35 echo "Toolchain VM running!"
36
37 # Now grab the emulated runtime and start that
38 cd /vagrant
39 if [ ! -d runtime ]; then
40     mkdir runtime
41 fi
42 cd runtime
43
44 echo "Checking / downloading files needed for the emulated runtime VM"
45 # Grab all the files we need, and check they're valid
46 echo "  Downloading MANIFEST"
47 wget -nv -O MANIFEST $RUNTIME_DL/MANIFEST 
48 for LINE in $(cat MANIFEST); do
49     FILENAME=$(echo $LINE | awk -F: '{print $1}')
50     MODE=$(echo $LINE | awk -F: '{print $2}')
51     BYTES=$(echo $LINE | awk -F: '{print $3}')
52     SHA=$(echo $LINE | awk -F: '{print $4}')
53     DL_NEEDED=1
54     
55     # Quick and dirty - if the file exists and is the right size,
56     # we'll believe it's OK. This will save us downloading a 2G test
57     # image every time, but make sure we delete it if people are
58     # changing tests!
59     echo "  Checking $FILENAME.gz"
60     if [ -f $FILENAME ]; then
61         SIZE=$(stat -c%s $FILENAME)
62         if [ $SIZE = $BYTES ]; then
63             DL_NEEDED=0
64         fi
65     fi
66
67     if [ "$OPTS"x = "nodownload"x ]; then
68         "IGNORING DOWNLOAD FOR $FILENAME"
69         DL_NEEDED=0
70     fi
71     if [ $DL_NEEDED = 1 ]; then
72         # Grab a compressed version of the file, and extract it as we
73         # go
74         echo "  Downloading $FILENAME.gz"
75         wget -nv -O- $RUNTIME_DL/$FILENAME.gz | gzip -cd > $FILENAME
76         SHA_FILE=$(sha256sum $FILENAME | awk '{print $1}')
77         if [ $SHA_FILE != $SHA ]; then
78             echo "Failed to download $FILENAME.gz correctly. Abort"
79             exit 1
80         fi
81     fi
82     chmod $MODE $FILENAME
83 done
84
85 echo "Starting emulated runtime VM next"
86 MACH=$RUNTIME ./start_runtime