Merge branch '15370-loopback-dispatchcloud'
[arvados.git] / tools / compute-images / scripts / base.sh
1 #!/bin/bash -euxo pipefail
2
3 # Copyright (C) The Arvados Authors. All rights reserved.
4 #
5 # SPDX-License-Identifier: Apache-2.0
6
7 set -eu -o pipefail
8
9 SUDO=sudo
10
11 wait_for_apt_locks() {
12   while $SUDO fuser /var/{lib/{dpkg,apt/lists},cache/apt/archives}/lock >/dev/null 2>&1; do
13     echo "APT: Waiting for apt/dpkg locks to be released..."
14     sleep 1
15   done
16 }
17
18 # $DIST should not have a dot if there is one in /etc/os-release (e.g. 18.04)
19 DIST=$(. /etc/os-release; echo $ID$VERSION_ID | tr -d '.')
20
21 # Run apt-get update
22 $SUDO DEBIAN_FRONTEND=noninteractive apt-get --yes update
23
24 # Install gnupg and dirmgr or gpg key checks will fail
25 wait_for_apt_locks && $SUDO DEBIAN_FRONTEND=noninteractive apt-get -qq --yes install \
26   gnupg \
27   dirmngr \
28   lsb-release
29
30 # For good measure, apt-get upgrade
31 wait_for_apt_locks && $SUDO DEBIAN_FRONTEND=noninteractive apt-get -qq --yes upgrade
32
33 # Make sure cloud-init is installed
34 wait_for_apt_locks && $SUDO DEBIAN_FRONTEND=noninteractive apt-get -qq --yes install cloud-init
35 if [[ ! -d /var/lib/cloud/scripts/per-boot ]]; then
36   mkdir -p /var/lib/cloud/scripts/per-boot
37 fi
38
39 TMP_LSB=`/usr/bin/lsb_release -c -s`
40 LSB_RELEASE_CODENAME=${TMP_LSB//[$'\t\r\n ']}
41
42 SET_RESOLVER=
43 if [ -n "$RESOLVER" ]; then
44   SET_RESOLVER="--dns ${RESOLVER}"
45 fi
46
47 # Add the arvados apt repository
48 echo "# apt.arvados.org" |$SUDO tee --append /etc/apt/sources.list.d/apt.arvados.org.list
49 echo "deb http://apt.arvados.org/$LSB_RELEASE_CODENAME $LSB_RELEASE_CODENAME${REPOSUFFIX} main" |$SUDO tee --append /etc/apt/sources.list.d/apt.arvados.org.list
50
51 # Add the arvados signing key
52 cat /tmp/1078ECD7.asc | $SUDO apt-key add -
53 # Add the debian keys (but don't abort if we can't find them, e.g. on Ubuntu where we don't need them)
54 wait_for_apt_locks && $SUDO DEBIAN_FRONTEND=noninteractive apt-get install --yes debian-keyring debian-archive-keyring 2>/dev/null || true
55
56 # Fix locale
57 $SUDO /bin/sed -ri 's/# en_US.UTF-8 UTF-8/en_US.UTF-8 UTF-8/' /etc/locale.gen
58 $SUDO /usr/sbin/locale-gen
59
60 # Install some packages we always need
61 wait_for_apt_locks && $SUDO DEBIAN_FRONTEND=noninteractive apt-get --yes update
62 wait_for_apt_locks && $SUDO DEBIAN_FRONTEND=noninteractive apt-get -qq --yes install \
63   openssh-server \
64   apt-utils \
65   git \
66   curl \
67   libcurl3-gnutls \
68   libcurl4-openssl-dev \
69   lvm2 \
70   cryptsetup \
71   xfsprogs \
72   squashfs-tools
73
74 # Install the Arvados packages we need
75 wait_for_apt_locks && $SUDO DEBIAN_FRONTEND=noninteractive apt-get -qq --yes install \
76   python3-arvados-fuse \
77   arvados-docker-cleaner
78
79 # We want Docker 20.10 or later so that we support glibc 2.33 and up in the container, cf.
80 # https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=1005906
81 dockerversion=5:20.10.13~3-0
82 if [[ "$DIST" =~ ^debian ]]; then
83   family="debian"
84   if [ "$DIST" == "debian10" ]; then
85     distro="buster"
86   elif [ "$DIST" == "debian11" ]; then
87     distro="bullseye"
88   fi
89 elif [[ "$DIST" =~ ^ubuntu ]]; then
90   family="ubuntu"
91   if [ "$DIST" == "ubuntu1804" ]; then
92     distro="bionic"
93   elif [ "$DIST" == "ubuntu2004" ]; then
94     distro="focal"
95   fi
96 else
97   echo "Unsupported distribution $DIST"
98   exit 1
99 fi
100 curl -fsSL https://download.docker.com/linux/$family/gpg | $SUDO gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
101 echo deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/$family/ $distro stable | \
102     $SUDO tee /etc/apt/sources.list.d/docker.list
103 $SUDO apt-get update
104 $SUDO apt-get -yq --no-install-recommends install docker-ce=${dockerversion}~${family}-${distro}
105
106 # Set a higher ulimit and the resolver (if set) for docker
107 $SUDO sed "s/ExecStart=\(.*\)/ExecStart=\1 --default-ulimit nofile=10000:10000 ${SET_RESOLVER}/g" \
108   /lib/systemd/system/docker.service \
109   > /etc/systemd/system/docker.service
110
111 $SUDO systemctl daemon-reload
112
113 # docker should not start on boot: we restart it inside /usr/local/bin/ensure-encrypted-partitions.sh,
114 # and the BootProbeCommand might be "docker ps -q"
115 $SUDO systemctl disable docker
116
117 # Get Go and build singularity
118 mkdir -p /var/lib/arvados
119 rm -rf /var/lib/arvados/go/
120 curl -s https://storage.googleapis.com/golang/go${GOVERSION}.linux-amd64.tar.gz | tar -C /var/lib/arvados -xzf -
121 ln -sf /var/lib/arvados/go/bin/* /usr/local/bin/
122
123 singularityversion=3.9.9
124 curl -Ls https://github.com/sylabs/singularity/archive/refs/tags/v${singularityversion}.tar.gz | tar -C /var/lib/arvados -xzf -
125 cd /var/lib/arvados/singularity-${singularityversion}
126
127 # build dependencies for singularity
128 wait_for_apt_locks && $SUDO DEBIAN_FRONTEND=noninteractive apt-get -qq --yes install \
129   make build-essential libssl-dev uuid-dev cryptsetup
130
131 echo $singularityversion > VERSION
132 ./mconfig --prefix=/var/lib/arvados
133 make -C ./builddir
134 make -C ./builddir install
135 ln -sf /var/lib/arvados/bin/* /usr/local/bin/
136
137 # set `mksquashfs mem` in the singularity config file if it is configured
138 if [ "$MKSQUASHFS_MEM" != "" ]; then
139   echo "mksquashfs mem = ${MKSQUASHFS_MEM}" >> /var/lib/arvados/etc/singularity/singularity.conf
140 fi
141
142 # Print singularity version installed
143 singularity --version
144
145 # Remove unattended-upgrades if it is installed
146 wait_for_apt_locks && $SUDO DEBIAN_FRONTEND=noninteractive apt-get -qq --yes remove unattended-upgrades --purge
147
148 # Configure arvados-docker-cleaner
149 $SUDO mkdir -p /etc/arvados/docker-cleaner
150 $SUDO echo -e "{\n  \"Quota\": \"10G\",\n  \"RemoveStoppedContainers\": \"always\"\n}" > /etc/arvados/docker-cleaner/docker-cleaner.json
151
152 # Enable cgroup accounting
153 $SUDO sed -i 's/GRUB_CMDLINE_LINUX=""/GRUB_CMDLINE_LINUX="cgroup_enable=memory swapaccount=1"/g' /etc/default/grub
154 $SUDO update-grub
155
156 # Make sure user_allow_other is set in fuse.conf
157 $SUDO sed -i 's/#user_allow_other/user_allow_other/g' /etc/fuse.conf
158
159 # Add crunch user with sudo powers
160 $SUDO adduser --disabled-password --gecos "Crunch user,,,," crunch
161 # Do not require a password to sudo
162 echo -e "# for the crunch user\ncrunch ALL=(ALL) NOPASSWD:ALL" | $SUDO tee /etc/sudoers.d/91-crunch
163
164 # Set up the ssh public key for the crunch user
165 $SUDO mkdir /home/crunch/.ssh
166 $SUDO mv /tmp/crunch-authorized_keys /home/crunch/.ssh/authorized_keys
167 $SUDO chown -R crunch:crunch /home/crunch/.ssh
168 $SUDO chmod 600 /home/crunch/.ssh/authorized_keys
169 $SUDO chmod 700 /home/crunch/.ssh/
170
171 # Make sure we resolve via the provided resolver IP if set. Prepending is good enough because
172 # unless 'rotate' is set, the nameservers are queried in order (cf. man resolv.conf)
173 if [ "x$RESOLVER" != "x" ]; then
174   $SUDO sed -i "s/#prepend domain-name-servers 127.0.0.1;/prepend domain-name-servers ${RESOLVER};/" /etc/dhcp/dhclient.conf
175 fi
176
177 # AWS_EBS_AUTOSCALE is not always set, work around unset variable check
178 EBS_AUTOSCALE=${AWS_EBS_AUTOSCALE:-}
179
180 if [ "$EBS_AUTOSCALE" != "1" ]; then
181   # Set up the cloud-init script that will ensure encrypted disks
182   $SUDO mv /tmp/usr-local-bin-ensure-encrypted-partitions.sh /usr/local/bin/ensure-encrypted-partitions.sh
183 else
184   wait_for_apt_locks && $SUDO DEBIAN_FRONTEND=noninteractive apt-get -qq --yes install jq unzip
185
186   curl -s "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "/tmp/awscliv2.zip"
187   unzip -q /tmp/awscliv2.zip -d /tmp && $SUDO /tmp/aws/install
188   # Pinned to v2.4.5 because we apply a patch below
189   #export EBS_AUTOSCALE_VERSION=$(curl --silent "https://api.github.com/repos/awslabs/amazon-ebs-autoscale/releases/latest" | jq -r .tag_name)
190   export EBS_AUTOSCALE_VERSION="v2.4.5"
191   cd /opt && $SUDO git clone https://github.com/awslabs/amazon-ebs-autoscale.git
192   cd /opt/amazon-ebs-autoscale && $SUDO git checkout $EBS_AUTOSCALE_VERSION
193   $SUDO patch -p1 < /tmp/create-ebs-volume-nvme.patch
194
195   # This script really requires bash and the shebang line is wrong
196   $SUDO sed -i 's|^#!/bin/sh|#!/bin/bash|' /opt/amazon-ebs-autoscale/bin/ebs-autoscale
197
198   # Set up the cloud-init script that makes use of the AWS EBS autoscaler
199   $SUDO mv /tmp/usr-local-bin-ensure-encrypted-partitions-aws-ebs-autoscale.sh /usr/local/bin/ensure-encrypted-partitions.sh
200 fi
201
202 $SUDO chmod 755 /usr/local/bin/ensure-encrypted-partitions.sh
203 $SUDO chown root:root /usr/local/bin/ensure-encrypted-partitions.sh
204 $SUDO mv /tmp/etc-cloud-cloud.cfg.d-07_compute_arvados_dispatch_cloud.cfg /etc/cloud/cloud.cfg.d/07_compute_arvados_dispatch_cloud.cfg
205 $SUDO chown root:root /etc/cloud/cloud.cfg.d/07_compute_arvados_dispatch_cloud.cfg
206
207 if [ "$NVIDIA_GPU_SUPPORT" == "1" ]; then
208   # We need a kernel and matching headers
209   if [[ "$DIST" =~ ^debian ]]; then
210     $SUDO apt-get -y install linux-image-cloud-amd64 linux-headers-cloud-amd64
211   elif [ "$CLOUD" == "azure" ]; then
212     $SUDO apt-get -y install linux-image-azure linux-headers-azure
213   elif [ "$CLOUD" == "aws" ]; then
214     $SUDO apt-get -y install linux-image-aws linux-headers-aws
215   fi
216
217   # Install CUDA
218   $SUDO apt-key adv --fetch-keys https://developer.download.nvidia.com/compute/cuda/repos/$DIST/x86_64/7fa2af80.pub
219   $SUDO apt-key adv --fetch-keys https://developer.download.nvidia.com/compute/cuda/repos/$DIST/x86_64/3bf863cc.pub
220   $SUDO apt-get -y install software-properties-common
221   $SUDO add-apt-repository "deb https://developer.download.nvidia.com/compute/cuda/repos/$DIST/x86_64/ /"
222   # Ubuntu 18.04's add-apt-repository does not understand 'contrib'
223   $SUDO add-apt-repository contrib || true
224   $SUDO apt-get update
225   $SUDO apt-get -y install cuda
226
227   # Install libnvidia-container, the tooling for Docker/Singularity
228   curl -s -L https://nvidia.github.io/libnvidia-container/gpgkey | \
229     $SUDO apt-key add -
230   if [ "$DIST" == "debian11" ]; then
231     # As of 2021-12-16 libnvidia-container and friends are only available for
232     # Debian 10, not yet Debian 11. Install experimental rc1 package as per this
233     # workaround:
234     # https://github.com/NVIDIA/nvidia-docker/issues/1549#issuecomment-989670662
235     curl -s -L https://nvidia.github.io/libnvidia-container/debian10/libnvidia-container.list | \
236       $SUDO tee /etc/apt/sources.list.d/libnvidia-container.list
237     $SUDO sed -i -e '/experimental/ s/^#//g' /etc/apt/sources.list.d/libnvidia-container.list
238   else
239     # here, $DIST should have a dot if there is one in /etc/os-release (e.g. 18.04)...
240     DIST=$(. /etc/os-release; echo $ID$VERSION_ID)
241     curl -s -L https://nvidia.github.io/libnvidia-container/$DIST/libnvidia-container.list | \
242       $SUDO tee /etc/apt/sources.list.d/libnvidia-container.list
243   fi
244
245   $SUDO apt-get update
246   $SUDO apt-get -y install libnvidia-container1 libnvidia-container-tools nvidia-container-toolkit
247   # This service fails to start when the image is booted without Nvidia GPUs present, which makes
248   # `systemctl is-system-running` respond with "degraded" and since that command is our default
249   # BootProbeCommand, compute nodes never finish booting from Arvados' perspective.
250   # Disable the service to avoid this. This should be fine because crunch-run does its own basic
251   # CUDA initialization.
252   $SUDO systemctl disable nvidia-persistenced.service
253 fi
254
255 $SUDO apt-get clean