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