Merge branch '18321-gpu-instancetype' refs #18321
[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   crunch-run \
68   arvados-docker-cleaner \
69   docker.io
70
71 # Get Go and build singularity
72 goversion=1.17.1
73 mkdir -p /var/lib/arvados
74 rm -rf /var/lib/arvados/go/
75 curl -s https://storage.googleapis.com/golang/go${goversion}.linux-amd64.tar.gz | tar -C /var/lib/arvados -xzf -
76 ln -sf /var/lib/arvados/go/bin/* /usr/local/bin/
77
78 singularityversion=3.7.4
79 curl -Ls https://github.com/sylabs/singularity/archive/refs/tags/v${singularityversion}.tar.gz | tar -C /var/lib/arvados -xzf -
80 cd /var/lib/arvados/singularity-${singularityversion}
81
82 # build dependencies for singularity
83 wait_for_apt_locks && $SUDO DEBIAN_FRONTEND=noninteractive apt-get -qq --yes install \
84   make build-essential libssl-dev uuid-dev cryptsetup
85
86 echo $singularityversion > VERSION
87 ./mconfig --prefix=/var/lib/arvados
88 make -C ./builddir
89 make -C ./builddir install
90 ln -sf /var/lib/arvados/bin/* /usr/local/bin/
91
92 # set `mksquashfs mem` in the singularity config file if it is configured
93 if [ "$MKSQUASHFS_MEM" != "" ]; then
94   echo "mksquashfs mem = ${MKSQUASHFS_MEM}" >> /var/lib/arvados/etc/singularity/singularity.conf
95 fi
96
97 # Print singularity version installed
98 singularity --version
99
100 # Remove unattended-upgrades if it is installed
101 wait_for_apt_locks && $SUDO DEBIAN_FRONTEND=noninteractive apt-get -qq --yes remove unattended-upgrades --purge
102
103 # Configure arvados-docker-cleaner
104 $SUDO mkdir -p /etc/arvados/docker-cleaner
105 $SUDO echo -e "{\n  \"Quota\": \"10G\",\n  \"RemoveStoppedContainers\": \"always\"\n}" > /etc/arvados/docker-cleaner/docker-cleaner.json
106
107 # Enable cgroup accounting
108 $SUDO sed -i 's/GRUB_CMDLINE_LINUX=""/GRUB_CMDLINE_LINUX="cgroup_enable=memory swapaccount=1"/g' /etc/default/grub
109 $SUDO update-grub
110
111 # Set a higher ulimit and the resolver (if set) for docker
112 if [ "x$RESOLVER" != "x" ]; then
113   SET_RESOLVER="--dns ${RESOLVER}"
114 fi
115
116 $SUDO sed "s/ExecStart=\(.*\)/ExecStart=\1 --default-ulimit nofile=10000:10000 ${SET_RESOLVER}/g" \
117   /lib/systemd/system/docker.service \
118   > /etc/systemd/system/docker.service
119
120 $SUDO systemctl daemon-reload
121
122 # docker should not start on boot: we restart it inside /usr/local/bin/ensure-encrypted-partitions.sh,
123 # and the BootProbeCommand might be "docker ps -q"
124 $SUDO systemctl disable docker
125
126 # Make sure user_allow_other is set in fuse.conf
127 $SUDO sed -i 's/#user_allow_other/user_allow_other/g' /etc/fuse.conf
128
129 # Add crunch user with sudo powers
130 $SUDO adduser --disabled-password --gecos "Crunch user,,,," crunch
131 # Do not require a password to sudo
132 echo -e "# for the crunch user\ncrunch ALL=(ALL) NOPASSWD:ALL" | $SUDO tee /etc/sudoers.d/91-crunch
133
134 # Set up the ssh public key for the crunch user
135 $SUDO mkdir /home/crunch/.ssh
136 $SUDO mv /tmp/crunch-authorized_keys /home/crunch/.ssh/authorized_keys
137 $SUDO chown -R crunch:crunch /home/crunch/.ssh
138 $SUDO chmod 600 /home/crunch/.ssh/authorized_keys
139 $SUDO chmod 700 /home/crunch/.ssh/
140
141 # Make sure we resolve via the provided resolver IP if set. Prepending is good enough because
142 # unless 'rotate' is set, the nameservers are queried in order (cf. man resolv.conf)
143 if [ "x$RESOLVER" != "x" ]; then
144   $SUDO sed -i "s/#prepend domain-name-servers 127.0.0.1;/prepend domain-name-servers ${RESOLVER};/" /etc/dhcp/dhclient.conf
145 fi
146 # Set up the cloud-init script that will ensure encrypted disks
147 $SUDO mv /tmp/usr-local-bin-ensure-encrypted-partitions.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 /tmp/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
152
153 if [ "$NVIDIA_GPU_SUPPORT" == "1" ]; then
154   # $DIST should not have a dot if there is one in /etc/os-release (e.g. 18.04)
155   DIST=$(. /etc/os-release; echo $ID$VERSION_ID | tr -d '.')
156   # We need a kernel and matching headers
157   if [[ "$DIST" =~ ^debian ]]; then
158     $SUDO apt-get -y install linux-image-cloud-amd64 linux-headers-cloud-amd64
159   elif [ "$CLOUD" == "azure" ]; then
160     $SUDO apt-get -y install linux-image-azure linux-headers-azure
161   elif [ "$CLOUD" == "aws" ]; then
162     $SUDO apt-get -y install linux-image-aws linux-headers-aws
163   fi
164
165   # Install CUDA
166   $SUDO apt-key adv --fetch-keys https://developer.download.nvidia.com/compute/cuda/repos/$DIST/x86_64/7fa2af80.pub
167   $SUDO apt-get -y install software-properties-common
168   $SUDO add-apt-repository "deb https://developer.download.nvidia.com/compute/cuda/repos/$DIST/x86_64/ /"
169   $SUDO add-apt-repository contrib
170   $SUDO apt-get update
171   $SUDO apt-get -y install cuda
172
173   # Install libnvidia-container, the tooling for Docker/Singularity
174   curl -s -L https://nvidia.github.io/libnvidia-container/gpgkey | \
175     $SUDO apt-key add -
176   if [ "$DIST" == "debian11" ]; 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
179     # workaround:
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
184   else
185     # here, $DIST should have a dot if there is one in /etc/os-release (e.g. 18.04)...
186     DIST=$(. /etc/os-release; echo $ID$VERSION_ID)
187     curl -s -L https://nvidia.github.io/libnvidia-container/$DIST/libnvidia-container.list | \
188       $SUDO tee /etc/apt/sources.list.d/libnvidia-container.list
189   fi
190
191   if [ "$DIST" == "debian10" ]; then
192     # Debian 10 comes with Docker 18.xx, we need 19.03 or later
193     curl -fsSL https://download.docker.com/linux/debian/gpg | $SUDO gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
194     echo deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/debian/ buster stable | \
195       $SUDO tee /etc/apt/sources.list.d/docker.list
196     $SUDO apt-get update
197     $SUDO apt-get -yq --no-install-recommends install docker-ce=5:19.03.15~3-0~debian-buster
198
199     $SUDO sed "s/ExecStart=\(.*\)/ExecStart=\1 --default-ulimit nofile=10000:10000 ${SET_RESOLVER}/g" \
200       /lib/systemd/system/docker.service \
201       > /etc/systemd/system/docker.service
202
203     $SUDO systemctl daemon-reload
204
205     # docker should not start on boot: we restart it inside /usr/local/bin/ensure-encrypted-partitions.sh,
206     # and the BootProbeCommand might be "docker ps -q"
207     $SUDO systemctl disable docker
208   fi
209   $SUDO apt-get update
210   $SUDO apt-get -y install libnvidia-container1 libnvidia-container-tools nvidia-container-toolkit
211 fi
212
213 $SUDO apt-get clean