22217: Modernize NVIDIA repository setup
[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 safe_apt() {
19     wait_for_apt_locks &&
20         $SUDO env DEBIAN_FRONTEND=noninteractive apt-get -qq --yes "$@"
21 }
22
23 download_and_install() {
24     local url="$1"; shift
25     local dest="$1"; shift
26     curl -fsSL "$url" | $SUDO install "$@" /dev/stdin "$dest"
27 }
28
29 . /etc/os-release
30 DISTRO_ID="$ID"
31 echo "Working directory is '${WORKDIR}'"
32
33 ### 1. Configure apt preferences
34
35 if [[ "${PIN_PACKAGES:-true}" != false ]]; then
36     $SUDO install -d /etc/apt/preferences.d
37     $SUDO install -m 0644 \
38           "$WORKDIR/etc-apt-preferences.d-arvados.pref" \
39           /etc/apt/preferences.d/arvados.pref
40 fi
41
42 ### 2. Install all base packages we need
43
44 safe_apt update
45 # Add the debian keys (but don't abort if we can't find them, e.g. on Ubuntu where we don't need them)
46 safe_apt install debian-keyring debian-archive-keyring 2>/dev/null || true
47 safe_apt upgrade
48 # Install gnupg and dirmgr or gpg key checks will fail
49 safe_apt install \
50   gnupg \
51   dirmngr \
52   lsb-release \
53   cloud-init \
54   openssh-server \
55   apt-utils \
56   git \
57   curl \
58   libcurl3-gnutls \
59   libcurl4-openssl-dev \
60   lvm2 \
61   cryptsetup \
62   xfsprogs \
63   jq \
64   unzip \
65   make \
66   build-essential \
67   libssl-dev \
68   uuid-dev \
69   squashfs-tools \
70   libglib2.0-dev \
71   libseccomp-dev \
72   software-properties-common
73
74 safe_apt remove --purge unattended-upgrades
75
76 ### 3. Set up third-party apt repositories and install packages we need from them
77
78 $SUDO add-apt-repository contrib
79 $SUDO install -d /etc/apt/keyrings
80
81 # Add the Arvados apt source
82 download_and_install https://apt.arvados.org/pubkey.gpg /etc/apt/keyrings/arvados.asc
83 $SUDO install -m 644 /dev/stdin /etc/apt/sources.list.d/arvados.sources <<EOF
84 Types: deb
85 URIs: https://apt.arvados.org/$VERSION_CODENAME
86 Suites: $VERSION_CODENAME${REPOSUFFIX:-}
87 Components: main
88 Signed-by: /etc/apt/keyrings/arvados.asc
89 EOF
90
91 # Add the Docker apt source
92 DOCKER_URL="https://download.docker.com/linux/$DISTRO_ID"
93 curl -fsSL "$DOCKER_URL/gpg" | $SUDO gpg --dearmor -o /etc/apt/keyrings/docker.gpg
94 $SUDO install -m 644 /dev/stdin /etc/apt/sources.list.d/docker.sources <<EOF
95 Types: deb
96 URIs: $DOCKER_URL/
97 Suites: $VERSION_CODENAME
98 Components: stable
99 Signed-by: /etc/apt/keyrings/docker.gpg
100 EOF
101
102 # Add the NVIDIA CUDA apt source
103 # Note that the "keyring" package also installs the apt source
104 NVIDIA_URL="https://developer.download.nvidia.com/compute/cuda/repos/$(echo "$DISTRO_ID$VERSION_ID" | tr -d .)/x86_64"
105 CUDA_KEYRING_DEB=cuda-keyring_1.1-1_all.deb
106 curl -fsSL -o "$WORKDIR/$CUDA_KEYRING_DEB" "$NVIDIA_URL/$CUDA_KEYRING_DEB"
107 wait_for_apt_locks && $SUDO dpkg -i "$WORKDIR/$CUDA_KEYRING_DEB"
108
109 # Add the NVIDIA container toolkit apt source
110 download_and_install \
111     https://nvidia.github.io/libnvidia-container/gpgkey \
112     /etc/apt/keyrings/nvidia-container-toolkit.asc
113 download_and_install \
114     https://nvidia.github.io/libnvidia-container/stable/deb/nvidia-container-toolkit.list \
115     /etc/apt/sources.list.d/nvidia-container-toolkit.list
116 $SUDO sed -i 's@^deb http@deb [signed-by=/etc/apt/keyrings/nvidia-container-toolkit.asc] http@' \
117       /etc/apt/sources.list.d/nvidia-container-toolkit.list
118
119 safe_apt update
120 safe_apt install python3-arvados-fuse arvados-docker-cleaner
121 safe_apt install --no-install-recommends docker-ce
122
123 ### 4. Compute node system configuration
124
125 mkdir -p /var/lib/cloud/scripts/per-boot
126
127 # Fix locale
128 $SUDO /bin/sed -ri 's/# en_US.UTF-8 UTF-8/en_US.UTF-8 UTF-8/' /etc/locale.gen
129 $SUDO /usr/sbin/locale-gen
130
131 # Set a higher ulimit and the resolver (if set) for docker
132 $SUDO install -d /etc/docker
133 $SUDO install -m 644 /dev/stdin /etc/docker/daemon.json <<EOFDOCKER
134 {
135   "default-ulimits": {
136     "nofile": {
137       "Hard": 10000,
138       "Name": "nofile",
139       "Soft": 10000
140     }
141   }
142   ${RESOLVER:+ , \"dns\": \"$RESOLVER\"}
143 }
144 EOFDOCKER
145
146 # docker should not start on boot: we restart it inside /usr/local/bin/ensure-encrypted-partitions.sh,
147 # and the BootProbeCommand might be "docker ps -q"
148 $SUDO systemctl disable docker
149
150 # Configure arvados-docker-cleaner
151 $SUDO mkdir -p /etc/arvados/docker-cleaner
152 $SUDO echo -e "{\n  \"Quota\": \"10G\",\n  \"RemoveStoppedContainers\": \"always\"\n}" > /etc/arvados/docker-cleaner/docker-cleaner.json
153
154 # Enable cgroup accounting (forcing cgroups v1)
155 $SUDO echo 'GRUB_CMDLINE_LINUX="$GRUB_CMDLINE_LINUX cgroup_enable=memory swapaccount=1 systemd.unified_cgroup_hierarchy=0"' >> /etc/default/grub
156 $SUDO update-grub
157
158 # Make sure user_allow_other is set in fuse.conf
159 $SUDO sed -i 's/#user_allow_other/user_allow_other/g' /etc/fuse.conf
160
161 # Add crunch user with sudo powers
162 $SUDO adduser --disabled-password --gecos "Crunch user,,,," crunch
163 # Do not require a password to sudo
164 echo -e "# for the crunch user\ncrunch ALL=(ALL) NOPASSWD:ALL" | $SUDO tee /etc/sudoers.d/91-crunch
165
166 # Set up the ssh public key for the crunch user
167 $SUDO mkdir /home/crunch/.ssh
168 $SUDO mv ${WORKDIR}/crunch-authorized_keys /home/crunch/.ssh/authorized_keys
169 $SUDO chown -R crunch:crunch /home/crunch/.ssh
170 $SUDO chmod 600 /home/crunch/.ssh/authorized_keys
171 $SUDO chmod 700 /home/crunch/.ssh/
172
173 # Make sure we resolve via the provided resolver IP if set. Prepending is good enough because
174 # unless 'rotate' is set, the nameservers are queried in order (cf. man resolv.conf)
175 if [ "x$RESOLVER" != "x" ]; then
176   $SUDO sed -i "s/#prepend domain-name-servers 127.0.0.1;/prepend domain-name-servers ${RESOLVER};/" /etc/dhcp/dhclient.conf
177 fi
178
179 # AWS_EBS_AUTOSCALE is not always set, work around unset variable check
180 EBS_AUTOSCALE=${AWS_EBS_AUTOSCALE:-}
181
182 if [ "$EBS_AUTOSCALE" != "1" ]; then
183   # Set up the cloud-init script that will ensure encrypted disks
184   $SUDO mv ${WORKDIR}/usr-local-bin-ensure-encrypted-partitions.sh /usr/local/bin/ensure-encrypted-partitions.sh
185 else
186   download_and_install "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" "${WORKDIR}/awscliv2.zip"
187   unzip -q ${WORKDIR}/awscliv2.zip -d ${WORKDIR} && $SUDO ${WORKDIR}/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="ee323f0751c2b6f733692e805b51b9bf3c251bac"
191   cd /opt && $SUDO git clone https://github.com/arvados/amazon-ebs-autoscale.git
192   cd /opt/amazon-ebs-autoscale && $SUDO git checkout $EBS_AUTOSCALE_VERSION
193
194   # Set up the cloud-init script that makes use of the AWS EBS autoscaler
195   $SUDO mv ${WORKDIR}/usr-local-bin-ensure-encrypted-partitions-aws-ebs-autoscale.sh /usr/local/bin/ensure-encrypted-partitions.sh
196 fi
197
198 $SUDO chmod 755 /usr/local/bin/ensure-encrypted-partitions.sh
199 $SUDO chown root:root /usr/local/bin/ensure-encrypted-partitions.sh
200 $SUDO mv ${WORKDIR}/etc-cloud-cloud.cfg.d-07_compute_arvados_dispatch_cloud.cfg /etc/cloud/cloud.cfg.d/07_compute_arvados_dispatch_cloud.cfg
201 $SUDO chown root:root /etc/cloud/cloud.cfg.d/07_compute_arvados_dispatch_cloud.cfg
202
203 if [ "$NVIDIA_GPU_SUPPORT" == "1" ]; then
204   # We need a kernel and matching headers
205   if [[ "$DISTRO_ID" == debian ]]; then
206     safe_apt install linux-image-cloud-amd64 linux-headers-cloud-amd64
207   elif [ "$CLOUD" == "azure" ]; then
208     safe_apt install linux-image-azure linux-headers-azure
209   elif [ "$CLOUD" == "aws" ]; then
210     safe_apt install linux-image-aws linux-headers-aws
211   fi
212   safe_apt install cuda libnvidia-container1 libnvidia-container-tools nvidia-container-toolkit
213
214   # Various components fail to start, and cause systemd to boot in degraded
215   # state, if the system does not actually have an NVIDIA GPU. Configure the
216   # image to adapt at boot time.
217
218   # Don't load modules unconditionally.
219   # Instead load them if hardware is detected.
220   if [[ -f /etc/modules-load.d/nvidia.conf ]]; then
221       $SUDO mv /etc/modules-load.d/nvidia.conf /etc/modules-load.d/nvidia.avail
222   fi
223   $SUDO install "$WORKDIR/usr-local-bin-detect-gpu.sh" /usr/local/bin/detect-gpu.sh
224   $SUDO install -d /etc/systemd/system/systemd-modules-load.service.d
225   $SUDO install -m 0644 \
226         "$WORKDIR/etc-systemd-system-systemd-modules-load.service.d-detect-gpu.conf" \
227         /etc/systemd/system/systemd-modules-load.service.d/detect-gpu.conf
228
229   # Don't start the persistence daemon.
230   # Instead rely on crunch-run's CUDA initialization.
231   $SUDO systemctl disable nvidia-persistenced.service
232 fi
233
234 # Get Go and build singularity
235 mkdir -p /var/lib/arvados
236 rm -rf /var/lib/arvados/go/
237 curl -fsSL https://storage.googleapis.com/golang/go${GOVERSION}.linux-amd64.tar.gz |
238     tar -C /var/lib/arvados -xz
239 ln -sf /var/lib/arvados/go/bin/* /usr/local/bin/
240
241 singularityversion=3.10.4
242 cd /var/lib/arvados
243 git clone --recurse-submodules https://github.com/sylabs/singularity
244 cd singularity
245 git checkout v${singularityversion}
246 echo $singularityversion > VERSION
247 ./mconfig --prefix=/var/lib/arvados
248 make -C ./builddir
249 make -C ./builddir install
250 ln -sf /var/lib/arvados/bin/* /usr/local/bin/
251
252 # set `mksquashfs mem` in the singularity config file if it is configured
253 if [ "$MKSQUASHFS_MEM" != "" ]; then
254   echo "mksquashfs mem = ${MKSQUASHFS_MEM}" >> /var/lib/arvados/etc/singularity/singularity.conf
255 fi
256
257 # Print singularity version installed
258 singularity --version
259
260 safe_apt clean