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 /boot/efi /mnt/image/boot/efi 48sudo mount --bind /dev/ /mnt/image/dev 49sudo mount --bind /dev/pts /mnt/image/dev/pts 50sudo mount --bind /run /mnt/image/run 51# resolv.conf is needed on Debian but not Ubuntu 52sudo cp /etc/resolv.conf /mnt/image/etc/ 53sudo chroot /mnt/image /usr/bin/apt update 54sudo chroot /mnt/image /usr/bin/apt install -y "${tmp_debs[@]}" 55# install tools dependencies 56sudo chroot /mnt/image /usr/bin/apt install -y openjdk-11-jre 57sudo chroot /mnt/image /usr/bin/apt install -y unzip bzip2 lzop 58sudo chroot /mnt/image /usr/bin/apt install -y aapt 59sudo chroot /mnt/image /usr/bin/apt install -y screen # needed by tradefed 60 61sudo chroot /mnt/image /usr/bin/find /home -ls 62 63 64# Install GPU driver dependencies 65sudo chroot /mnt/image /usr/bin/apt install -y gcc 66sudo chroot /mnt/image /usr/bin/apt install -y linux-source 67sudo chroot /mnt/image /usr/bin/apt install -y linux-headers-`uname -r` 68sudo chroot /mnt/image /usr/bin/apt install -y make 69 70# Download the latest GPU driver installer 71gsutil cp \ 72 $(gsutil ls gs://nvidia-drivers-us-public/GRID/GRID*/*-Linux-x86_64-*.run \ 73 | sort \ 74 | tail -n 1) \ 75 /mnt/image/tmp/nvidia-driver-installer.run 76 77# Make GPU driver installer executable 78chmod +x /mnt/image/tmp/nvidia-driver-installer.run 79 80# Install the latest GPU driver with default options and the dispatch libs 81sudo chroot /mnt/image /tmp/nvidia-driver-installer.run \ 82 --silent \ 83 --install-libglvnd 84 85# Cleanup after install 86rm /mnt/image/tmp/nvidia-driver-installer.run 87 88# Verify 89query_nvidia() { 90 sudo chroot /mnt/image nvidia-smi --format=csv,noheader --query-gpu="$@" 91} 92 93if [[ $(query_nvidia "count") != "1" ]]; then 94 echo "Failed to detect GPU." 95 exit 1 96fi 97 98if [[ $(query_nvidia "driver_version") == "" ]]; then 99 echo "Failed to detect GPU driver." 100 exit 1 101fi 102 103# Vulkan loader 104sudo chroot /mnt/image /usr/bin/apt install -y libvulkan1 105 106# Wayland-server needed to have Nvidia driver fail gracefully when attemping to 107# use the EGL API on GCE instances without a GPU. 108sudo chroot /mnt/image /usr/bin/apt install -y libwayland-server0 109 110# Clean up the builder's version of resolv.conf 111sudo rm /mnt/image/etc/resolv.conf 112 113# Make sure the image has /var/empty, and allow unprivileged_userns_clone for 114# minijail process sandboxing 115sudo chroot /mnt/image /usr/bin/mkdir -p /var/empty 116sudo tee /mnt/image/etc/sysctl.d/80-nsjail.conf >/dev/null <<EOF 117kernel.unprivileged_userns_clone=1 118EOF 119 120# Skip unmounting: 121# Sometimes systemd starts, making it hard to unmount 122# In any case we'll unmount cleanly when the instance shuts down 123 124echo IMAGE_WAS_CREATED 125