1#!/bin/bash
2
3set -x
4set -o errexit
5
6sudo apt-get update
7
8# Stuff we need to get build support
9
10sudo apt install -y debhelper ubuntu-dev-tools equivs "${extra_packages[@]}"
11
12# Install the cuttlefish build deps
13
14for dsc in *.dsc; do
15  yes | sudo mk-build-deps -i "${dsc}" -t apt-get
16done
17
18# Installing the build dependencies left some .deb files around. Remove them
19# to keep them from landing on the image.
20yes | rm -f *.deb
21
22for dsc in *.dsc; do
23  # Unpack the source and build it
24
25  dpkg-source -x "${dsc}"
26  dir="$(basename "${dsc}" .dsc)"
27  dir="${dir/_/-}"
28  pushd "${dir}/"
29  debuild -uc -us
30  popd
31done
32
33# Now gather all of the *.deb files to copy them into the image
34debs=(*.deb)
35
36tmp_debs=()
37for i in "${debs[@]}"; do
38  tmp_debs+=(/tmp/"$(basename "$i")")
39done
40
41# Now install the packages on the disk
42sudo mkdir /mnt/image
43sudo mount /dev/sdb1 /mnt/image
44cp "${debs[@]}" /mnt/image/tmp
45sudo mount -t sysfs none /mnt/image/sys
46sudo mount -t proc none /mnt/image/proc
47sudo mount --bind /dev/ /mnt/image/dev
48sudo mount --bind /dev/pts /mnt/image/dev/pts
49sudo mount --bind /run /mnt/image/run
50# resolv.conf is needed on Debian but not Ubuntu
51sudo cp /etc/resolv.conf /mnt/image/etc/
52sudo chroot /mnt/image /usr/bin/apt update
53sudo chroot /mnt/image /usr/bin/apt install -y "${tmp_debs[@]}"
54# install tools dependencies
55sudo chroot /mnt/image /usr/bin/apt install -y openjdk-11-jre
56sudo chroot /mnt/image /usr/bin/apt install -y unzip bzip2 lzop
57sudo chroot /mnt/image /usr/bin/apt install -y aapt
58sudo chroot /mnt/image /usr/bin/apt install -y screen # needed by tradefed
59
60sudo chroot /mnt/image /usr/bin/find /home -ls
61
62
63# Install GPU driver dependencies
64sudo chroot /mnt/image /usr/bin/apt install -y gcc
65sudo chroot /mnt/image /usr/bin/apt install -y linux-source
66sudo chroot /mnt/image /usr/bin/apt install -y linux-headers-`uname -r`
67sudo chroot /mnt/image /usr/bin/apt install -y make
68
69# Download the latest GPU driver installer
70gsutil cp \
71  $(gsutil ls gs://nvidia-drivers-us-public/GRID/GRID*/*-Linux-x86_64-*.run \
72    | sort \
73    | tail -n 1) \
74  /mnt/image/tmp/nvidia-driver-installer.run
75
76# Make GPU driver installer executable
77chmod +x /mnt/image/tmp/nvidia-driver-installer.run
78
79# Install the latest GPU driver with default options and the dispatch libs
80sudo chroot /mnt/image /tmp/nvidia-driver-installer.run \
81  --silent \
82  --install-libglvnd
83
84# Cleanup after install
85rm /mnt/image/tmp/nvidia-driver-installer.run
86
87# Verify
88query_nvidia() {
89  sudo chroot /mnt/image nvidia-smi --format=csv,noheader --query-gpu="$@"
90}
91
92if [[ $(query_nvidia "count") != "1" ]]; then
93  echo "Failed to detect GPU."
94  exit 1
95fi
96
97if [[ $(query_nvidia "driver_version") == "" ]]; then
98  echo "Failed to detect GPU driver."
99  exit 1
100fi
101
102
103# Clean up the builder's version of resolv.conf
104sudo rm /mnt/image/etc/resolv.conf
105
106# Skip unmounting:
107#  Sometimes systemd starts, making it hard to unmount
108#  In any case we'll unmount cleanly when the instance shuts down
109
110echo IMAGE_WAS_CREATED
111