260c5d47ee32c8a84af34c6490bcd0a7fa247c00
[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
54 wait_for_apt_locks && $SUDO DEBIAN_FRONTEND=noninteractive apt-get install --yes debian-keyring debian-archive-keyring
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 goversion=1.17.1
119 mkdir -p /var/lib/arvados
120 rm -rf /var/lib/arvados/go/
121 curl -s https://storage.googleapis.com/golang/go${goversion}.linux-amd64.tar.gz | tar -C /var/lib/arvados -xzf -
122 ln -sf /var/lib/arvados/go/bin/* /usr/local/bin/
123
124 singularityversion=3.7.4
125 curl -Ls https://github.com/sylabs/singularity/archive/refs/tags/v${singularityversion}.tar.gz | tar -C /var/lib/arvados -xzf -
126 cd /var/lib/arvados/singularity-${singularityversion}
127
128 # build dependencies for singularity
129 wait_for_apt_locks && $SUDO DEBIAN_FRONTEND=noninteractive apt-get -qq --yes install \
130   make build-essential libssl-dev uuid-dev cryptsetup
131
132 echo $singularityversion > VERSION
133 ./mconfig --prefix=/var/lib/arvados
134 make -C ./builddir
135 make -C ./builddir install
136 ln -sf /var/lib/arvados/bin/* /usr/local/bin/
137
138 # set `mksquashfs mem` in the singularity config file if it is configured
139 if [ "$MKSQUASHFS_MEM" != "" ]; then
140   echo "mksquashfs mem = ${MKSQUASHFS_MEM}" >> /var/lib/arvados/etc/singularity/singularity.conf
141 fi
142
143 # Print singularity version installed
144 singularity --version
145
146 # Remove unattended-upgrades if it is installed
147 wait_for_apt_locks && $SUDO DEBIAN_FRONTEND=noninteractive apt-get -qq --yes remove unattended-upgrades --purge
148
149 # Configure arvados-docker-cleaner
150 $SUDO mkdir -p /etc/arvados/docker-cleaner
151 $SUDO echo -e "{\n  \"Quota\": \"10G\",\n  \"RemoveStoppedContainers\": \"always\"\n}" > /etc/arvados/docker-cleaner/docker-cleaner.json
152
153 # Enable cgroup accounting
154 $SUDO sed -i 's/GRUB_CMDLINE_LINUX=""/GRUB_CMDLINE_LINUX="cgroup_enable=memory swapaccount=1"/g' /etc/default/grub
155 $SUDO update-grub
156
157 # Make sure user_allow_other is set in fuse.conf
158 $SUDO sed -i 's/#user_allow_other/user_allow_other/g' /etc/fuse.conf
159
160 # Add crunch user with sudo powers
161 $SUDO adduser --disabled-password --gecos "Crunch user,,,," crunch
162 # Do not require a password to sudo
163 echo -e "# for the crunch user\ncrunch ALL=(ALL) NOPASSWD:ALL" | $SUDO tee /etc/sudoers.d/91-crunch
164
165 # Set up the ssh public key for the crunch user
166 $SUDO mkdir /home/crunch/.ssh
167 $SUDO mv /tmp/crunch-authorized_keys /home/crunch/.ssh/authorized_keys
168 $SUDO chown -R crunch:crunch /home/crunch/.ssh
169 $SUDO chmod 600 /home/crunch/.ssh/authorized_keys
170 $SUDO chmod 700 /home/crunch/.ssh/
171
172 # Make sure we resolve via the provided resolver IP if set. Prepending is good enough because
173 # unless 'rotate' is set, the nameservers are queried in order (cf. man resolv.conf)
174 if [ "x$RESOLVER" != "x" ]; then
175   $SUDO sed -i "s/#prepend domain-name-servers 127.0.0.1;/prepend domain-name-servers ${RESOLVER};/" /etc/dhcp/dhclient.conf
176 fi
177
178 # AWS_EBS_AUTOSCALE is not always set, work around unset variable check
179 EBS_AUTOSCALE=${AWS_EBS_AUTOSCALE:-}
180
181 if [ "$EBS_AUTOSCALE" != "1" ]; then
182   # Set up the cloud-init script that will ensure encrypted disks
183   $SUDO mv /tmp/usr-local-bin-ensure-encrypted-partitions.sh /usr/local/bin/ensure-encrypted-partitions.sh
184 else
185   wait_for_apt_locks && $SUDO DEBIAN_FRONTEND=noninteractive apt-get -qq --yes install jq unzip
186
187   curl -s "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "/tmp/awscliv2.zip"
188   unzip -q /tmp/awscliv2.zip -d /tmp && $SUDO /tmp/aws/install
189   # Pinned to v2.4.5 because we apply a patch below
190   #export EBS_AUTOSCALE_VERSION=$(curl --silent "https://api.github.com/repos/awslabs/amazon-ebs-autoscale/releases/latest" | jq -r .tag_name)
191   export EBS_AUTOSCALE_VERSION="v2.4.5"
192   cd /opt && $SUDO git clone https://github.com/awslabs/amazon-ebs-autoscale.git
193   cd /opt/amazon-ebs-autoscale && $SUDO git checkout $EBS_AUTOSCALE_VERSION
194   $SUDO patch -p1 < /tmp/create-ebs-volume-nvme.patch
195
196   # This script really requires bash and the shebang line is wrong
197   $SUDO sed -i 's|^#!/bin/sh|#!/bin/bash|' /opt/amazon-ebs-autoscale/bin/ebs-autoscale
198
199   # Set up the cloud-init script that makes use of the AWS EBS autoscaler
200   $SUDO mv /tmp/usr-local-bin-ensure-encrypted-partitions-aws-ebs-autoscale.sh /usr/local/bin/ensure-encrypted-partitions.sh
201 fi
202
203 $SUDO chmod 755 /usr/local/bin/ensure-encrypted-partitions.sh
204 $SUDO chown root:root /usr/local/bin/ensure-encrypted-partitions.sh
205 $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
206 $SUDO chown root:root /etc/cloud/cloud.cfg.d/07_compute_arvados_dispatch_cloud.cfg
207
208 if [ "$NVIDIA_GPU_SUPPORT" == "1" ]; then
209   # We need a kernel and matching headers
210   if [[ "$DIST" =~ ^debian ]]; then
211     $SUDO apt-get -y install linux-image-cloud-amd64 linux-headers-cloud-amd64
212   elif [ "$CLOUD" == "azure" ]; then
213     $SUDO apt-get -y install linux-image-azure linux-headers-azure
214   elif [ "$CLOUD" == "aws" ]; then
215     $SUDO apt-get -y install linux-image-aws linux-headers-aws
216   fi
217
218   # Install CUDA
219   $SUDO apt-key adv --fetch-keys https://developer.download.nvidia.com/compute/cuda/repos/$DIST/x86_64/7fa2af80.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