1#!/bin/bash 2 3# Copyright 2018 Google Inc. All rights reserved. 4 5# Licensed under the Apache License, Version 2.0 (the "License"); 6# you may not use this file except in compliance with the License. 7# You may obtain a copy of the License at 8 9# http://www.apache.org/licenses/LICENSE-2.0 10 11# Unless required by applicable law or agreed to in writing, software 12# distributed under the License is distributed on an "AS IS" BASIS, 13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14# See the License for the specific language governing permissions and 15# limitations under the License. 16 17set -x 18set -o errexit 19shopt -s extglob 20 21# If "true" install host orchestration capabilities. 22host_orchestration_flag="false" 23 24while getopts ":o" flag; do 25 case "${flag}" in 26 o) host_orchestration_flag="true";; 27 esac 28done 29 30sudo apt-get update 31 32# Stuff we need to get build support 33 34sudo apt install -y debhelper ubuntu-dev-tools equivs "${extra_packages[@]}" 35 36# Resize 37sudo apt install -y cloud-utils 38sudo apt install -y cloud-guest-utils 39sudo apt install -y fdisk 40sudo growpart /dev/sdb 1 41sudo e2fsck -f -y /dev/sdb1 42sudo resize2fs /dev/sdb1 43 44# Install the cuttlefish build deps 45 46for dsc in *.dsc; do 47 yes | sudo mk-build-deps -i "${dsc}" -t apt-get 48done 49 50# Installing the build dependencies left some .deb files around. Remove them 51# to keep them from landing on the image. 52yes | rm -f *.deb 53 54for dsc in *.dsc; do 55 # Unpack the source and build it 56 57 dpkg-source -x "${dsc}" 58 dir="$(basename "${dsc}" .dsc)" 59 dir="${dir/_/-}" 60 pushd "${dir}/" 61 debuild -uc -us 62 popd 63done 64 65# Now gather all of the relevant .deb files to copy them into the image 66debs=() 67if [[ "${host_orchestration_flag}" == "true" ]]; then 68 debs=(!(cuttlefish-@(common|user)*).deb) 69else 70 debs=(!(cuttlefish-orchestration*).deb) 71fi 72 73tmp_debs=() 74for i in "${debs[@]}"; do 75 tmp_debs+=(/tmp/"$(basename "$i")") 76done 77 78# Now install the packages on the disk 79sudo mkdir -p /mnt/image 80sudo mount /dev/sdb1 /mnt/image 81cp "${debs[@]}" /mnt/image/tmp 82sudo mount -t sysfs none /mnt/image/sys 83sudo mount -t proc none /mnt/image/proc 84sudo mount --bind /boot/efi /mnt/image/boot/efi 85sudo mount --bind /dev/ /mnt/image/dev 86sudo mount --bind /dev/pts /mnt/image/dev/pts 87sudo mount --bind /run /mnt/image/run 88# resolv.conf is needed on Debian but not Ubuntu 89sudo cp /etc/resolv.conf /mnt/image/etc/ 90sudo chroot /mnt/image /usr/bin/apt update 91sudo chroot /mnt/image /usr/bin/apt install -y "${tmp_debs[@]}" 92# install tools dependencies 93sudo chroot /mnt/image /usr/bin/apt install -y openjdk-17-jre 94sudo chroot /mnt/image /usr/bin/apt install -y unzip bzip2 lzop 95sudo chroot /mnt/image /usr/bin/apt install -y aapt 96sudo chroot /mnt/image /usr/bin/apt install -y screen # needed by tradefed 97 98sudo chroot /mnt/image /usr/bin/find /home -ls 99sudo chroot /mnt/image /usr/bin/apt install -t bullseye-backports -y linux-image-cloud-amd64 100 101# update QEMU version to most recent backport 102sudo chroot /mnt/image /usr/bin/apt install -y --only-upgrade qemu-system-x86 -t bullseye-backports 103sudo chroot /mnt/image /usr/bin/apt install -y --only-upgrade qemu-system-arm -t bullseye-backports 104 105# Install GPU driver dependencies 106sudo cp install_nvidia.sh /mnt/image/ 107sudo chroot /mnt/image /usr/bin/bash install_nvidia.sh 108sudo rm /mnt/image/install_nvidia.sh 109 110# Verify 111query_nvidia() { 112 sudo chroot /mnt/image nvidia-smi --format=csv,noheader --query-gpu="$@" 113} 114 115if [[ $(query_nvidia "count") != "1" ]]; then 116 echo "Failed to detect GPU." 117 exit 1 118fi 119 120if [[ $(query_nvidia "driver_version") == "" ]]; then 121 echo "Failed to detect GPU driver." 122 exit 1 123fi 124 125# Vulkan loader 126sudo chroot /mnt/image /usr/bin/apt install -y libvulkan1 -t bullseye-backports 127 128# Wayland-server needed to have Nvidia driver fail gracefully when attemping to 129# use the EGL API on GCE instances without a GPU. 130sudo chroot /mnt/image /usr/bin/apt install -y libwayland-server0 -t bullseye-backports 131 132# Clean up the builder's version of resolv.conf 133sudo rm /mnt/image/etc/resolv.conf 134 135# Make sure the image has /var/empty, and allow unprivileged_userns_clone for 136# minijail process sandboxing 137sudo chroot /mnt/image /usr/bin/mkdir -p /var/empty 138sudo tee /mnt/image/etc/sysctl.d/80-nsjail.conf >/dev/null <<EOF 139kernel.unprivileged_userns_clone=1 140EOF 141 142# Skip unmounting: 143# Sometimes systemd starts, making it hard to unmount 144# In any case we'll unmount cleanly when the instance shuts down 145 146echo IMAGE_WAS_CREATED 147