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