Merge branch '18713-nvidia-persistenced'
[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 SUDO=sudo
8
9 wait_for_apt_locks() {
10   while $SUDO fuser /var/{lib/{dpkg,apt/lists},cache/apt/archives}/lock >/dev/null 2>&1; do
11     echo "APT: Waiting for apt/dpkg locks to be released..."
12     sleep 1
13   done
14 }
15
16 # Run apt-get update
17 $SUDO DEBIAN_FRONTEND=noninteractive apt-get --yes update
18
19 # Install gnupg and dirmgr or gpg key checks will fail
20 wait_for_apt_locks && $SUDO DEBIAN_FRONTEND=noninteractive apt-get -qq --yes install \
21   gnupg \
22   dirmngr \
23   lsb-release
24
25 # For good measure, apt-get upgrade
26 wait_for_apt_locks && $SUDO DEBIAN_FRONTEND=noninteractive apt-get -qq --yes upgrade
27
28 # Make sure cloud-init is installed
29 wait_for_apt_locks && $SUDO DEBIAN_FRONTEND=noninteractive apt-get -qq --yes install cloud-init
30 if [[ ! -d /var/lib/cloud/scripts/per-boot ]]; then
31   mkdir -p /var/lib/cloud/scripts/per-boot
32 fi
33
34 TMP_LSB=`/usr/bin/lsb_release -c -s`
35 LSB_RELEASE_CODENAME=${TMP_LSB//[$'\t\r\n ']}
36
37 # Add the arvados apt repository
38 echo "# apt.arvados.org" |$SUDO tee --append /etc/apt/sources.list.d/apt.arvados.org.list
39 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
40
41 # Add the arvados signing key
42 cat /tmp/1078ECD7.asc | $SUDO apt-key add -
43 # Add the debian keys
44 wait_for_apt_locks && $SUDO DEBIAN_FRONTEND=noninteractive apt-get install --yes debian-keyring debian-archive-keyring
45
46 # Fix locale
47 $SUDO /bin/sed -ri 's/# en_US.UTF-8 UTF-8/en_US.UTF-8 UTF-8/' /etc/locale.gen
48 $SUDO /usr/sbin/locale-gen
49
50 # Install some packages we always need
51 wait_for_apt_locks && $SUDO DEBIAN_FRONTEND=noninteractive apt-get --yes update
52 wait_for_apt_locks && $SUDO DEBIAN_FRONTEND=noninteractive apt-get -qq --yes install \
53   openssh-server \
54   apt-utils \
55   git \
56   curl \
57   libcurl3-gnutls \
58   libcurl4-openssl-dev \
59   lvm2 \
60   cryptsetup \
61   xfsprogs \
62   squashfs-tools
63
64 # Install the Arvados packages we need
65 wait_for_apt_locks && $SUDO DEBIAN_FRONTEND=noninteractive apt-get -qq --yes install \
66   python3-arvados-fuse \
67   arvados-docker-cleaner \
68   docker.io
69
70 # Get Go and build singularity
71 goversion=1.17.1
72 mkdir -p /var/lib/arvados
73 rm -rf /var/lib/arvados/go/
74 curl -s https://storage.googleapis.com/golang/go${goversion}.linux-amd64.tar.gz | tar -C /var/lib/arvados -xzf -
75 ln -sf /var/lib/arvados/go/bin/* /usr/local/bin/
76
77 singularityversion=3.7.4
78 curl -Ls https://github.com/sylabs/singularity/archive/refs/tags/v${singularityversion}.tar.gz | tar -C /var/lib/arvados -xzf -
79 cd /var/lib/arvados/singularity-${singularityversion}
80
81 # build dependencies for singularity
82 wait_for_apt_locks && $SUDO DEBIAN_FRONTEND=noninteractive apt-get -qq --yes install \
83   make build-essential libssl-dev uuid-dev cryptsetup
84
85 echo $singularityversion > VERSION
86 ./mconfig --prefix=/var/lib/arvados
87 make -C ./builddir
88 make -C ./builddir install
89 ln -sf /var/lib/arvados/bin/* /usr/local/bin/
90
91 # set `mksquashfs mem` in the singularity config file if it is configured
92 if [ "$MKSQUASHFS_MEM" != "" ]; then
93   echo "mksquashfs mem = ${MKSQUASHFS_MEM}" >> /var/lib/arvados/etc/singularity/singularity.conf
94 fi
95
96 # Print singularity version installed
97 singularity --version
98
99 # Remove unattended-upgrades if it is installed
100 wait_for_apt_locks && $SUDO DEBIAN_FRONTEND=noninteractive apt-get -qq --yes remove unattended-upgrades --purge
101
102 # Configure arvados-docker-cleaner
103 $SUDO mkdir -p /etc/arvados/docker-cleaner
104 $SUDO echo -e "{\n  \"Quota\": \"10G\",\n  \"RemoveStoppedContainers\": \"always\"\n}" > /etc/arvados/docker-cleaner/docker-cleaner.json
105
106 # Enable cgroup accounting
107 $SUDO sed -i 's/GRUB_CMDLINE_LINUX=""/GRUB_CMDLINE_LINUX="cgroup_enable=memory swapaccount=1"/g' /etc/default/grub
108 $SUDO update-grub
109
110 # Set a higher ulimit and the resolver (if set) for docker
111 if [ "x$RESOLVER" != "x" ]; then
112   SET_RESOLVER="--dns ${RESOLVER}"
113 fi
114
115 $SUDO sed "s/ExecStart=\(.*\)/ExecStart=\1 --default-ulimit nofile=10000:10000 ${SET_RESOLVER}/g" \
116   /lib/systemd/system/docker.service \
117   > /etc/systemd/system/docker.service
118
119 $SUDO systemctl daemon-reload
120
121 # docker should not start on boot: we restart it inside /usr/local/bin/ensure-encrypted-partitions.sh,
122 # and the BootProbeCommand might be "docker ps -q"
123 $SUDO systemctl disable docker
124
125 # Make sure user_allow_other is set in fuse.conf
126 $SUDO sed -i 's/#user_allow_other/user_allow_other/g' /etc/fuse.conf
127
128 # Add crunch user with sudo powers
129 $SUDO adduser --disabled-password --gecos "Crunch user,,,," crunch
130 # Do not require a password to sudo
131 echo -e "# for the crunch user\ncrunch ALL=(ALL) NOPASSWD:ALL" | $SUDO tee /etc/sudoers.d/91-crunch
132
133 # Set up the ssh public key for the crunch user
134 $SUDO mkdir /home/crunch/.ssh
135 $SUDO mv /tmp/crunch-authorized_keys /home/crunch/.ssh/authorized_keys
136 $SUDO chown -R crunch:crunch /home/crunch/.ssh
137 $SUDO chmod 600 /home/crunch/.ssh/authorized_keys
138 $SUDO chmod 700 /home/crunch/.ssh/
139
140 # Make sure we resolve via the provided resolver IP if set. Prepending is good enough because
141 # unless 'rotate' is set, the nameservers are queried in order (cf. man resolv.conf)
142 if [ "x$RESOLVER" != "x" ]; then
143   $SUDO sed -i "s/#prepend domain-name-servers 127.0.0.1;/prepend domain-name-servers ${RESOLVER};/" /etc/dhcp/dhclient.conf
144 fi
145 # Set up the cloud-init script that will ensure encrypted disks
146 $SUDO mv /tmp/usr-local-bin-ensure-encrypted-partitions.sh /usr/local/bin/ensure-encrypted-partitions.sh
147 $SUDO chmod 755 /usr/local/bin/ensure-encrypted-partitions.sh
148 $SUDO chown root:root /usr/local/bin/ensure-encrypted-partitions.sh
149 $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
150 $SUDO chown root:root /etc/cloud/cloud.cfg.d/07_compute_arvados_dispatch_cloud.cfg
151
152 if [ "$NVIDIA_GPU_SUPPORT" == "1" ]; then
153   # $DIST should not have a dot if there is one in /etc/os-release (e.g. 18.04)
154   DIST=$(. /etc/os-release; echo $ID$VERSION_ID | tr -d '.')
155   # We need a kernel and matching headers
156   if [[ "$DIST" =~ ^debian ]]; then
157     $SUDO apt-get -y install linux-image-cloud-amd64 linux-headers-cloud-amd64
158   elif [ "$CLOUD" == "azure" ]; then
159     $SUDO apt-get -y install linux-image-azure linux-headers-azure
160   elif [ "$CLOUD" == "aws" ]; then
161     $SUDO apt-get -y install linux-image-aws linux-headers-aws
162   fi
163
164   # Install CUDA
165   $SUDO apt-key adv --fetch-keys https://developer.download.nvidia.com/compute/cuda/repos/$DIST/x86_64/7fa2af80.pub
166   $SUDO apt-get -y install software-properties-common
167   $SUDO add-apt-repository "deb https://developer.download.nvidia.com/compute/cuda/repos/$DIST/x86_64/ /"
168   $SUDO add-apt-repository contrib
169   $SUDO apt-get update
170   $SUDO apt-get -y install cuda
171
172   # Install libnvidia-container, the tooling for Docker/Singularity
173   curl -s -L https://nvidia.github.io/libnvidia-container/gpgkey | \
174     $SUDO apt-key add -
175   if [ "$DIST" == "debian11" ]; then
176     # As of 2021-12-16 libnvidia-container and friends are only available for
177     # Debian 10, not yet Debian 11. Install experimental rc1 package as per this
178     # workaround:
179     # https://github.com/NVIDIA/nvidia-docker/issues/1549#issuecomment-989670662
180     curl -s -L https://nvidia.github.io/libnvidia-container/debian10/libnvidia-container.list | \
181       $SUDO tee /etc/apt/sources.list.d/libnvidia-container.list
182     $SUDO sed -i -e '/experimental/ s/^#//g' /etc/apt/sources.list.d/libnvidia-container.list
183   else
184     # here, $DIST should have a dot if there is one in /etc/os-release (e.g. 18.04)...
185     DIST=$(. /etc/os-release; echo $ID$VERSION_ID)
186     curl -s -L https://nvidia.github.io/libnvidia-container/$DIST/libnvidia-container.list | \
187       $SUDO tee /etc/apt/sources.list.d/libnvidia-container.list
188   fi
189
190   if [ "$DIST" == "debian10" ]; then
191     # Debian 10 comes with Docker 18.xx, we need 19.03 or later
192     curl -fsSL https://download.docker.com/linux/debian/gpg | $SUDO gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
193     echo deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/debian/ buster stable | \
194       $SUDO tee /etc/apt/sources.list.d/docker.list
195     $SUDO apt-get update
196     $SUDO apt-get -yq --no-install-recommends install docker-ce=5:19.03.15~3-0~debian-buster
197
198     $SUDO sed "s/ExecStart=\(.*\)/ExecStart=\1 --default-ulimit nofile=10000:10000 ${SET_RESOLVER}/g" \
199       /lib/systemd/system/docker.service \
200       > /etc/systemd/system/docker.service
201
202     $SUDO systemctl daemon-reload
203
204     # docker should not start on boot: we restart it inside /usr/local/bin/ensure-encrypted-partitions.sh,
205     # and the BootProbeCommand might be "docker ps -q"
206     $SUDO systemctl disable docker
207   fi
208   $SUDO apt-get update
209   $SUDO apt-get -y install libnvidia-container1 libnvidia-container-tools nvidia-container-toolkit
210   # This service fails to start when the image is booted without Nvidia GPUs present, which makes
211   # `systemctl is-system-running` respond with "degraded" and since that command is our default
212   # BootProbeCommand, compute nodes never finish booting from Arvados' perspective.
213   # Disable the service to avoid this. This should be fine because crunch-run does its own basic
214   # CUDA initialization.
215   $SUDO systemctl disable nvidia-persistenced.service
216 fi
217
218 $SUDO apt-get clean