1 #!/bin/bash -euxo pipefail
3 # Copyright (C) The Arvados Authors. All rights reserved.
5 # SPDX-License-Identifier: Apache-2.0
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..."
22 $SUDO DEBIAN_FRONTEND=noninteractive apt-get --yes update
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 \
30 # For good measure, apt-get upgrade
31 wait_for_apt_locks && $SUDO DEBIAN_FRONTEND=noninteractive apt-get -qq --yes upgrade
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
40 if [ -n "$RESOLVER" ]; then
41 SET_RESOLVER="--dns ${RESOLVER}"
44 # Add the arvados apt repository
45 echo "# apt.arvados.org" |$SUDO tee --append /etc/apt/sources.list.d/apt.arvados.org.list
46 echo "deb http://apt.arvados.org/$VERSION_CODENAME $VERSION_CODENAME${REPOSUFFIX} main" |$SUDO tee --append /etc/apt/sources.list.d/apt.arvados.org.list
48 # Add the arvados signing key
49 cat /tmp/1078ECD7.asc | $SUDO apt-key add -
50 # Add the debian keys (but don't abort if we can't find them, e.g. on Ubuntu where we don't need them)
51 wait_for_apt_locks && $SUDO DEBIAN_FRONTEND=noninteractive apt-get install --yes debian-keyring debian-archive-keyring 2>/dev/null || true
54 $SUDO /bin/sed -ri 's/# en_US.UTF-8 UTF-8/en_US.UTF-8 UTF-8/' /etc/locale.gen
55 $SUDO /usr/sbin/locale-gen
57 # Install some packages we always need
58 wait_for_apt_locks && $SUDO DEBIAN_FRONTEND=noninteractive apt-get --yes update
59 wait_for_apt_locks && $SUDO DEBIAN_FRONTEND=noninteractive apt-get -qq --yes install \
65 libcurl4-openssl-dev \
70 # Install the Arvados packages we need
71 wait_for_apt_locks && $SUDO DEBIAN_FRONTEND=noninteractive apt-get -qq --yes install \
72 python3-arvados-fuse \
73 arvados-docker-cleaner
75 DOCKER_URL="https://download.docker.com/linux/$DISTRO_ID"
76 curl -fsSL "$DOCKER_URL/gpg" | $SUDO gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
77 echo "deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] $DOCKER_URL/ $VERSION_CODENAME stable" | \
78 $SUDO tee /etc/apt/sources.list.d/docker.list
80 $SUDO apt-get -yq --no-install-recommends install docker-ce
82 # Set a higher ulimit and the resolver (if set) for docker
83 $SUDO sed "s/ExecStart=\(.*\)/ExecStart=\1 --default-ulimit nofile=10000:10000 ${SET_RESOLVER}/g" \
84 /lib/systemd/system/docker.service \
85 > /etc/systemd/system/docker.service
87 $SUDO systemctl daemon-reload
89 # docker should not start on boot: we restart it inside /usr/local/bin/ensure-encrypted-partitions.sh,
90 # and the BootProbeCommand might be "docker ps -q"
91 $SUDO systemctl disable docker
93 # Remove unattended-upgrades if it is installed
94 wait_for_apt_locks && $SUDO DEBIAN_FRONTEND=noninteractive apt-get -qq --yes remove unattended-upgrades --purge
96 # Configure arvados-docker-cleaner
97 $SUDO mkdir -p /etc/arvados/docker-cleaner
98 $SUDO echo -e "{\n \"Quota\": \"10G\",\n \"RemoveStoppedContainers\": \"always\"\n}" > /etc/arvados/docker-cleaner/docker-cleaner.json
100 # Enable cgroup accounting (forcing cgroups v1)
101 $SUDO echo 'GRUB_CMDLINE_LINUX="$GRUB_CMDLINE_LINUX cgroup_enable=memory swapaccount=1 systemd.unified_cgroup_hierarchy=0"' >> /etc/default/grub
104 # Make sure user_allow_other is set in fuse.conf
105 $SUDO sed -i 's/#user_allow_other/user_allow_other/g' /etc/fuse.conf
107 # Add crunch user with sudo powers
108 $SUDO adduser --disabled-password --gecos "Crunch user,,,," crunch
109 # Do not require a password to sudo
110 echo -e "# for the crunch user\ncrunch ALL=(ALL) NOPASSWD:ALL" | $SUDO tee /etc/sudoers.d/91-crunch
112 # Set up the ssh public key for the crunch user
113 $SUDO mkdir /home/crunch/.ssh
114 $SUDO mv /tmp/crunch-authorized_keys /home/crunch/.ssh/authorized_keys
115 $SUDO chown -R crunch:crunch /home/crunch/.ssh
116 $SUDO chmod 600 /home/crunch/.ssh/authorized_keys
117 $SUDO chmod 700 /home/crunch/.ssh/
119 # Make sure we resolve via the provided resolver IP if set. Prepending is good enough because
120 # unless 'rotate' is set, the nameservers are queried in order (cf. man resolv.conf)
121 if [ "x$RESOLVER" != "x" ]; then
122 $SUDO sed -i "s/#prepend domain-name-servers 127.0.0.1;/prepend domain-name-servers ${RESOLVER};/" /etc/dhcp/dhclient.conf
125 # AWS_EBS_AUTOSCALE is not always set, work around unset variable check
126 EBS_AUTOSCALE=${AWS_EBS_AUTOSCALE:-}
128 if [ "$EBS_AUTOSCALE" != "1" ]; then
129 # Set up the cloud-init script that will ensure encrypted disks
130 $SUDO mv /tmp/usr-local-bin-ensure-encrypted-partitions.sh /usr/local/bin/ensure-encrypted-partitions.sh
132 wait_for_apt_locks && $SUDO DEBIAN_FRONTEND=noninteractive apt-get -qq --yes install jq unzip
134 curl -s "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "/tmp/awscliv2.zip"
135 unzip -q /tmp/awscliv2.zip -d /tmp && $SUDO /tmp/aws/install
136 # Pinned to v2.4.5 because we apply a patch below
137 #export EBS_AUTOSCALE_VERSION=$(curl --silent "https://api.github.com/repos/awslabs/amazon-ebs-autoscale/releases/latest" | jq -r .tag_name)
138 export EBS_AUTOSCALE_VERSION="ee323f0751c2b6f733692e805b51b9bf3c251bac"
139 cd /opt && $SUDO git clone https://github.com/arvados/amazon-ebs-autoscale.git
140 cd /opt/amazon-ebs-autoscale && $SUDO git checkout $EBS_AUTOSCALE_VERSION
142 # Set up the cloud-init script that makes use of the AWS EBS autoscaler
143 $SUDO mv /tmp/usr-local-bin-ensure-encrypted-partitions-aws-ebs-autoscale.sh /usr/local/bin/ensure-encrypted-partitions.sh
146 $SUDO chmod 755 /usr/local/bin/ensure-encrypted-partitions.sh
147 $SUDO chown root:root /usr/local/bin/ensure-encrypted-partitions.sh
148 $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
149 $SUDO chown root:root /etc/cloud/cloud.cfg.d/07_compute_arvados_dispatch_cloud.cfg
151 if [ "$NVIDIA_GPU_SUPPORT" == "1" ]; then
152 # We need a kernel and matching headers
153 if [[ "$DISTRO_ID" == debian ]]; then
154 $SUDO apt-get -y install linux-image-cloud-amd64 linux-headers-cloud-amd64
155 elif [ "$CLOUD" == "azure" ]; then
156 $SUDO apt-get -y install linux-image-azure linux-headers-azure
157 elif [ "$CLOUD" == "aws" ]; then
158 $SUDO apt-get -y install linux-image-aws linux-headers-aws
162 NVIDIA_URL="https://developer.download.nvidia.com/compute/cuda/repos/$(echo "$DISTRO_ID$VERSION_ID" | tr -d .)/x86_64"
163 $SUDO apt-key adv --fetch-keys "$NVIDIA_URL/7fa2af80.pub"
164 $SUDO apt-key adv --fetch-keys "$NVIDIA_URL/3bf863cc.pub"
165 $SUDO apt-get -y install software-properties-common
166 $SUDO add-apt-repository "deb $NVIDIA_URL/ /"
167 $SUDO add-apt-repository contrib
169 $SUDO apt-get -y install cuda
171 # Install libnvidia-container, the tooling for Docker/Singularity
172 curl -s -L https://nvidia.github.io/libnvidia-container/gpgkey | \
174 if [[ "$VERSION_CODENAME" == bullseye ]]; then
175 # As of 2021-12-16 libnvidia-container and friends are only available for
176 # Debian 10, not yet Debian 11. Install experimental rc1 package as per this
178 # https://github.com/NVIDIA/nvidia-docker/issues/1549#issuecomment-989670662
179 curl -s -L https://nvidia.github.io/libnvidia-container/debian10/libnvidia-container.list | \
180 $SUDO tee /etc/apt/sources.list.d/libnvidia-container.list
181 $SUDO sed -i -e '/experimental/ s/^#//g' /etc/apt/sources.list.d/libnvidia-container.list
183 curl -s -L "https://nvidia.github.io/libnvidia-container/$DISTRO_ID$VERSION_ID/libnvidia-container.list" | \
184 $SUDO tee /etc/apt/sources.list.d/libnvidia-container.list
188 $SUDO apt-get -y install libnvidia-container1 libnvidia-container-tools nvidia-container-toolkit
189 # This service fails to start when the image is booted without Nvidia GPUs present, which makes
190 # `systemctl is-system-running` respond with "degraded" and since that command is our default
191 # BootProbeCommand, compute nodes never finish booting from Arvados' perspective.
192 # Disable the service to avoid this. This should be fine because crunch-run does its own basic
193 # CUDA initialization.
194 $SUDO systemctl disable nvidia-persistenced.service
197 # Get Go and build singularity
198 mkdir -p /var/lib/arvados
199 rm -rf /var/lib/arvados/go/
200 curl -s https://storage.googleapis.com/golang/go${GOVERSION}.linux-amd64.tar.gz | tar -C /var/lib/arvados -xzf -
201 ln -sf /var/lib/arvados/go/bin/* /usr/local/bin/
203 singularityversion=3.10.4
205 git clone --recurse-submodules https://github.com/sylabs/singularity
207 git checkout v${singularityversion}
209 # build dependencies for singularity
210 wait_for_apt_locks && $SUDO DEBIAN_FRONTEND=noninteractive apt-get -qq --yes install \
211 make build-essential libssl-dev uuid-dev cryptsetup \
212 squashfs-tools libglib2.0-dev libseccomp-dev
215 echo $singularityversion > VERSION
216 ./mconfig --prefix=/var/lib/arvados
218 make -C ./builddir install
219 ln -sf /var/lib/arvados/bin/* /usr/local/bin/
221 # set `mksquashfs mem` in the singularity config file if it is configured
222 if [ "$MKSQUASHFS_MEM" != "" ]; then
223 echo "mksquashfs mem = ${MKSQUASHFS_MEM}" >> /var/lib/arvados/etc/singularity/singularity.conf
226 # Print singularity version installed
227 singularity --version