38bbac88a2473fd05c7d829a3ad05318dee3d4b3
[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   libglib2.0-dev \
74   libseccomp-dev
75
76 # Install the Arvados packages we need
77 wait_for_apt_locks && $SUDO DEBIAN_FRONTEND=noninteractive apt-get -qq --yes install \
78   python3-arvados-fuse \
79   arvados-docker-cleaner
80
81 # We want Docker 20.10 or later so that we support glibc 2.33 and up in the container, cf.
82 # https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=1005906
83 dockerversion=5:20.10.13~3-0
84 if [[ "$DIST" =~ ^debian ]]; then
85   family="debian"
86   if [ "$DIST" == "debian10" ]; then
87     distro="buster"
88   elif [ "$DIST" == "debian11" ]; then
89     distro="bullseye"
90   fi
91 elif [[ "$DIST" =~ ^ubuntu ]]; then
92   family="ubuntu"
93   if [ "$DIST" == "ubuntu1804" ]; then
94     distro="bionic"
95   elif [ "$DIST" == "ubuntu2004" ]; then
96     distro="focal"
97   fi
98 else
99   echo "Unsupported distribution $DIST"
100   exit 1
101 fi
102 curl -fsSL https://download.docker.com/linux/$family/gpg | $SUDO gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
103 echo deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/$family/ $distro stable | \
104     $SUDO tee /etc/apt/sources.list.d/docker.list
105 $SUDO apt-get update
106 $SUDO apt-get -yq --no-install-recommends install docker-ce=${dockerversion}~${family}-${distro}
107
108 # Set a higher ulimit and the resolver (if set) for docker
109 $SUDO sed "s/ExecStart=\(.*\)/ExecStart=\1 --default-ulimit nofile=10000:10000 ${SET_RESOLVER}/g" \
110   /lib/systemd/system/docker.service \
111   > /etc/systemd/system/docker.service
112
113 $SUDO systemctl daemon-reload
114
115 # docker should not start on boot: we restart it inside /usr/local/bin/ensure-encrypted-partitions.sh,
116 # and the BootProbeCommand might be "docker ps -q"
117 $SUDO systemctl disable docker
118
119 # Get Go and build singularity
120 mkdir -p /var/lib/arvados
121 rm -rf /var/lib/arvados/go/
122 curl -s https://storage.googleapis.com/golang/go${GOVERSION}.linux-amd64.tar.gz | tar -C /var/lib/arvados -xzf -
123 ln -sf /var/lib/arvados/go/bin/* /usr/local/bin/
124
125 singularityversion=3.10.4
126 curl -Ls https://github.com/sylabs/singularity/archive/refs/tags/v${singularityversion}.tar.gz | tar -C /var/lib/arvados -xzf -
127 cd /var/lib/arvados/singularity-${singularityversion}
128
129 # build dependencies for singularity
130 wait_for_apt_locks && $SUDO DEBIAN_FRONTEND=noninteractive apt-get -qq --yes install \
131   make build-essential libssl-dev uuid-dev cryptsetup
132
133 echo $singularityversion > VERSION
134 ./mconfig --prefix=/var/lib/arvados
135 make -C ./builddir
136 make -C ./builddir install
137 ln -sf /var/lib/arvados/bin/* /usr/local/bin/
138
139 # set `mksquashfs mem` in the singularity config file if it is configured
140 if [ "$MKSQUASHFS_MEM" != "" ]; then
141   echo "mksquashfs mem = ${MKSQUASHFS_MEM}" >> /var/lib/arvados/etc/singularity/singularity.conf
142 fi
143
144 # Print singularity version installed
145 singularity --version
146
147 # Remove unattended-upgrades if it is installed
148 wait_for_apt_locks && $SUDO DEBIAN_FRONTEND=noninteractive apt-get -qq --yes remove unattended-upgrades --purge
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 /tmp/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 /tmp/usr-local-bin-ensure-encrypted-partitions.sh /usr/local/bin/ensure-encrypted-partitions.sh
185 else
186   wait_for_apt_locks && $SUDO DEBIAN_FRONTEND=noninteractive apt-get -qq --yes install jq unzip
187
188   curl -s "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "/tmp/awscliv2.zip"
189   unzip -q /tmp/awscliv2.zip -d /tmp && $SUDO /tmp/aws/install
190   # Pinned to v2.4.5 because we apply a patch below
191   #export EBS_AUTOSCALE_VERSION=$(curl --silent "https://api.github.com/repos/awslabs/amazon-ebs-autoscale/releases/latest" | jq -r .tag_name)
192   export EBS_AUTOSCALE_VERSION="5ca6e24e05787b8ae1184c2a10db80053ddd3038"
193   cd /opt && $SUDO git clone https://github.com/arvados/amazon-ebs-autoscale.git
194   cd /opt/amazon-ebs-autoscale && $SUDO git checkout $EBS_AUTOSCALE_VERSION
195
196   # Set up the cloud-init script that makes use of the AWS EBS autoscaler
197   $SUDO mv /tmp/usr-local-bin-ensure-encrypted-partitions-aws-ebs-autoscale.sh /usr/local/bin/ensure-encrypted-partitions.sh
198 fi
199
200 $SUDO chmod 755 /usr/local/bin/ensure-encrypted-partitions.sh
201 $SUDO chown root:root /usr/local/bin/ensure-encrypted-partitions.sh
202 $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
203 $SUDO chown root:root /etc/cloud/cloud.cfg.d/07_compute_arvados_dispatch_cloud.cfg
204
205 if [ "$NVIDIA_GPU_SUPPORT" == "1" ]; then
206   # We need a kernel and matching headers
207   if [[ "$DIST" =~ ^debian ]]; then
208     $SUDO apt-get -y install linux-image-cloud-amd64 linux-headers-cloud-amd64
209   elif [ "$CLOUD" == "azure" ]; then
210     $SUDO apt-get -y install linux-image-azure linux-headers-azure
211   elif [ "$CLOUD" == "aws" ]; then
212     $SUDO apt-get -y install linux-image-aws linux-headers-aws
213   fi
214
215   # Install CUDA
216   $SUDO apt-key adv --fetch-keys https://developer.download.nvidia.com/compute/cuda/repos/$DIST/x86_64/7fa2af80.pub
217   $SUDO apt-key adv --fetch-keys https://developer.download.nvidia.com/compute/cuda/repos/$DIST/x86_64/3bf863cc.pub
218   $SUDO apt-get -y install software-properties-common
219   $SUDO add-apt-repository "deb https://developer.download.nvidia.com/compute/cuda/repos/$DIST/x86_64/ /"
220   # Ubuntu 18.04's add-apt-repository does not understand 'contrib'
221   $SUDO add-apt-repository contrib || true
222   $SUDO apt-get update
223   $SUDO apt-get -y install cuda
224
225   # Install libnvidia-container, the tooling for Docker/Singularity
226   curl -s -L https://nvidia.github.io/libnvidia-container/gpgkey | \
227     $SUDO apt-key add -
228   if [ "$DIST" == "debian11" ]; then
229     # As of 2021-12-16 libnvidia-container and friends are only available for
230     # Debian 10, not yet Debian 11. Install experimental rc1 package as per this
231     # workaround:
232     # https://github.com/NVIDIA/nvidia-docker/issues/1549#issuecomment-989670662
233     curl -s -L https://nvidia.github.io/libnvidia-container/debian10/libnvidia-container.list | \
234       $SUDO tee /etc/apt/sources.list.d/libnvidia-container.list
235     $SUDO sed -i -e '/experimental/ s/^#//g' /etc/apt/sources.list.d/libnvidia-container.list
236   else
237     # here, $DIST should have a dot if there is one in /etc/os-release (e.g. 18.04)...
238     DIST=$(. /etc/os-release; echo $ID$VERSION_ID)
239     curl -s -L https://nvidia.github.io/libnvidia-container/$DIST/libnvidia-container.list | \
240       $SUDO tee /etc/apt/sources.list.d/libnvidia-container.list
241   fi
242
243   $SUDO apt-get update
244   $SUDO apt-get -y install libnvidia-container1 libnvidia-container-tools nvidia-container-toolkit
245   # This service fails to start when the image is booted without Nvidia GPUs present, which makes
246   # `systemctl is-system-running` respond with "degraded" and since that command is our default
247   # BootProbeCommand, compute nodes never finish booting from Arvados' perspective.
248   # Disable the service to avoid this. This should be fine because crunch-run does its own basic
249   # CUDA initialization.
250   $SUDO systemctl disable nvidia-persistenced.service
251 fi
252
253 $SUDO apt-get clean