blob: e032fba50f23b582e638fe495cb1cc922bee256e [file] [log] [blame]
Gerrit MCPbbf9d5d2019-12-19 13:47:41 +01001#!/bin/bash
2set -x
Deniscaa72dd2021-06-04 17:13:05 +03003set -e
Dennis Dmitrievb12d2342020-03-12 19:36:24 +02004# allow access to the local variables from prepare-metadata.py
5set -a
Gerrit MCPbbf9d5d2019-12-19 13:47:41 +01006
Vasyl Saienko9824df42021-03-17 15:43:02 +02007# ensure we don't re-source this in the same environment
Vasyl Saienko4a2832d2024-05-16 09:00:03 +03008[[ -z "$_FUNCTIONS_SCRIPT" ]] || return 0
9declare -r -g _FUNCTIONS_SCRIPT=1
Vasyl Saienko9824df42021-03-17 15:43:02 +020010
Oleh Hryhorovca772092020-02-13 11:15:17 +020011PUBLIC_INTERFACE_NETMASK=$(echo ${PUBLIC_INTERFACE_CIDR} | cut -d'/' -f2)
Oleh Hryhorovdccb1cd2020-03-04 15:52:55 +020012STORAGE_BACKEND_NETWORK_NETMASK=$(echo ${STORAGE_BACKEND_NETWORK} | cut -d'/' -f2)
Oleh Hryhorovdccb1cd2020-03-04 15:52:55 +020013STORAGE_FRONTEND_NETWORK_NETMASK=$(echo ${STORAGE_FRONTEND_NETWORK} | cut -d'/' -f2)
Vasyl Saienko552250d2021-03-19 12:07:23 +020014IRONIC_BAREMETAL_INTERFACE_IP=${IRONIC_BAREMETAL_INTERFACE_IP:-$ironic_baremetal_interface_ip}
Vasyl Saienko4c468192020-05-19 11:51:13 +030015IRONIC_BAREMETAL_NETWORK_NETMASK=$(echo ${IRONIC_BAREMETAL_NETWORK} | cut -d'/' -f2)
Vasyl Saienkod3964202023-09-24 15:09:48 +000016IRONIC_BAREMETAL_INTERFACE=$(ip route |grep ${IRONIC_BAREMETAL_NETWORK} | awk '/ src / {print $3}')
Vasyl Saienko3dd0c272020-11-16 17:20:07 +020017TUNNEL_INTERFACE_NETWORK_NETMASK=$(ip -o addr show |grep -w ${TUNNEL_INTERFACE_IP} | awk '{print $4}' |awk -F '/' '{print $2}')
18TUNNEL_INTERFACE=$(ip -o addr show |grep -w ${TUNNEL_INTERFACE_IP}/${TUNNEL_INTERFACE_NETWORK_NETMASK} | awk '{print $2}')
Michael Polenchuk486cadf2020-08-24 16:25:51 +040019IRONIC_BAREMETAL_NETWORK_PREFIX=$(sed 's/[0-9]*\/[0-9]*$//' <<< $IRONIC_BAREMETAL_NETWORK)
20IRONIC_BAREMETAL_TUNNEL_NETWORK_PREFIX=$(sed 's/[0-9]*\/[0-9]*$//' <<< $IRONIC_BAREMETAL_TUNNEL_NETWORK)
Gerrit MCPbbf9d5d2019-12-19 13:47:41 +010021DOCKER_DEFAULT_ADDRESS_POOL=${DOCKER_DEFAULT_ADDRESS_POOL:-10.10.1.0/16}
22# DOCKER_DEFAULT_ADDRESS_SIZE have to be less then netmask in DOCKER_DEFAULT_ADDRESS_POOL because
23# to the fact that actual netmask for docker_gwbridge is given from it
24DOCKER_DEFAULT_ADDRESS_SIZE=${DOCKER_DEFAULT_ADDRESS_SIZE:-24}
Mykyta Karpin5ed5fd82020-01-27 18:23:29 +020025DOCKER_EE_RELEASE=${DOCKER_EE_RELEASE:-stable-19.03}
Vasyl Saienkofa714102024-03-22 09:30:25 +020026DOCKER_EE_PACKAGES=${DOCKER_EE_PACKAGES:-'docker-ee'}
Vasyl Saienkoa94bf182023-04-01 10:42:17 +000027BINARY_BASE_URL=${BINARY_BASE_URL:-"http://binary.mirantis.com"}
dbiletskiy090662a2024-07-08 14:27:26 +020028CODENAME=$(cat /etc/*release | awk -F '=' '/_CODENAME/ {print $2}' | head -1)
Vasyl Saienko7c595922021-03-17 13:58:13 +020029
30### COMMON FUNCTIONS ###
31function get_interface_prefix {
32 local interface=$1
33 local interface_ip
34 local interface_mask
35
36 interface_ip=$(ip addr show dev ${interface} | grep -Po 'inet \K[\d.]+' | egrep -v "127.0.|172.17")
37 interface_mask=$(ip addr show dev ${interface} | grep -Po 'inet \K[\d.]+\/[\d]+' | egrep -v "127.0.|172.17" | cut -d'/' -f2)
38 echo "${interface_ip}/${interface_mask}"
39}
40### END COMMON FUNCTIONS ###
41
Oleh Hryhorovf4677af2021-04-26 18:00:15 +030042DEFAULT_INTERFACE=${DEFAULT_INTERFACE:-$(ip route show |awk '/default/ {print $5}')}
Vasyl Saienko7c595922021-03-17 13:58:13 +020043
Vasyl Saienko390c53a2021-03-18 12:45:11 +020044if [[ -n ${CONTROL_NETWORK_CIDR} ]]; then
Vasyl Saienkod3964202023-09-24 15:09:48 +000045 CONTROL_IP_ADDRESS=$(ip route |grep ${CONTROL_NETWORK_CIDR} | head -n1 | fgrep -v ' via ' | awk '/ src / {print $9}')
Vasyl Saienko7c595922021-03-17 13:58:13 +020046else
47 CONTROL_IP_ADDRESS=$(get_interface_prefix ${DEFAULT_INTERFACE} | awk -F '/' '{print $1}')
Vasyl Saienkoe9677062021-03-19 13:31:32 +020048 CONTROL_NETWORK_CIDR=$(ip route show dev ${DEFAULT_INTERFACE} | awk '/kernel/ {print $1}')
Vasyl Saienko7c595922021-03-17 13:58:13 +020049fi
50
51
Oleh Hryhorovb58460e2020-02-05 15:53:48 +020052PUBLIC_INTERFACE=${PUBLIC_INTERFACE:-ens4}
Gerrit MCPbbf9d5d2019-12-19 13:47:41 +010053UCP_USERNAME=${UCP_USERNAME:-admin}
54UCP_PASSWORD=${UCP_PASSWORD:-administrator}
55OS_CODENAME=$(lsb_release -c -s)
Vasyl Saienko3ab29c92020-01-03 10:39:03 +020056NODE_DEPLOYMENT_RETRIES=${NODE_DEPLOYMENT_RETRIES:-15}
Vasyl Saienko7d7acea2020-01-03 16:59:05 +020057FLOATING_NETWORK_PREFIXES=${FLOATING_NETWORK_PREFIXES:-10.11.12.0/24}
Vasyl Saienko0fa6f192020-03-06 16:08:51 +020058UCP_MASTER_HOST=${UCP_MASTER_HOST:-${CONTROL_IP_ADDRESS}}
59UCP_IP_ADDRESS=${UCP_IP_ADDRESS:-$CONTROL_IP_ADDRESS}
Mykyta Karpin9df830a2024-09-12 14:25:01 +020060UCP_AUDIT_LOG_LEVEL=${UCP_AUDIT_LOG_LEVEL:-''}
Vasyl Saienkocfbedc72021-02-15 18:02:56 +020061# Change default VXLAN port, need for https://mirantis.jira.com/browse/PRODX-11679
62UCP_DOCKER_SWARM_DATA_PORT=${UCP_DOCKER_SWARM_DATA_PORT:-4789}
Vasyl Saienko06c7afe2022-07-01 17:53:56 +000063UCP_DOCKER_CALICO_VXLAN_PORT=${UCP_DOCKER_CALICO_VXLAN_PORT:-${UCP_DOCKER_SWARM_DATA_PORT}}
Vasyl Saienko08166342020-02-28 12:20:51 +020064NTP_SERVERS=${NTP_SERVERS:-"ldap.scc.mirantis.net ldap.bud.mirantis.net"}
Michael Polenchuk486cadf2020-08-24 16:25:51 +040065IRONIC_BAREMETAL_VXLAN_INTERFACE='vxlan10'
Mykyta Karpin6a153c52020-11-03 15:21:38 +020066HUGE_PAGES_1G_SIZE=$(echo "${HUGE_PAGES}" | awk -F ',' '{print $1}')
67HUGE_PAGES_2Mb_SIZE=$(echo "${HUGE_PAGES}" | awk -F ',' '{print $2}')
68CONFIGURE_HUGE_PAGES=false
Vasyl Saienko3dd0c272020-11-16 17:20:07 +020069TUNNEL_INTERFACE_NETPLAN_MANAGE=true
Anton Samoylov1d140ee2020-11-23 22:40:13 +040070TUNGSTENFABRIC_ENABLED=${TUNGSTENFABRIC_ENABLED:-false}
Mykyta Karpin6a153c52020-11-03 15:21:38 +020071if [[ "${HUGE_PAGES_1G_SIZE}" -gt 0 ]] || [[ "${HUGE_PAGES_2Mb_SIZE}" -gt 0 ]]; then
72 CONFIGURE_HUGE_PAGES=true
Anton Samoylov1d140ee2020-11-23 22:40:13 +040073 if [[ "${TUNGSTENFABRIC_ENABLED,,}" == false ]]; then
74 TUNNEL_INTERFACE_NETPLAN_MANAGE=false
75 fi
Mykyta Karpin6a153c52020-11-03 15:21:38 +020076fi
dyakovlev9af15002024-11-19 13:56:48 +040077
78if [[ "${TUNGSTENFABRIC_ENABLED,,}" == true ]]; then
79 NTP_SERVERS="172.19.124.2 172.16.243.0"
80
81fi
Oleh Hryhorov43bbc8c2020-12-03 15:30:09 +020082SINGLE_NODE=${SINGLE_NODE:-true}
Gerrit MCPbbf9d5d2019-12-19 13:47:41 +010083
Vasyl Saienko25bb75b2021-02-15 12:22:33 +020084#FRR Options
85FRR_BGP_AS_NUMBER=${FRR_BGP_AS_NUMBER:-64512}
86FRR_ROUTER_ID=${TUNNEL_INTERFACE_IP}
Vasyl Saienko846c6342021-02-24 17:01:36 +020087FRR_BGP_SOURCE_ADDRESS=${TUNNEL_INTERFACE_IP}
88FRR_BGP_NEIGHBORS=${FRR_BGP_NEIGHBORS:-}
Vasyl Saienko5f953782021-02-25 10:30:32 +020089FRR_EVPN_TUNNELS_RANGE=${FRR_EVPN_TUNNELS_RANGE:-'20 50'}
Vasyl Saienkob3b8c4e2021-02-26 10:54:27 +020090FRR_EVPN_VXLAN_DST_PORT=${FRR_EVPN_VXLAN_DST_PORT:-4790}
Vasyl Saienko25bb75b2021-02-15 12:22:33 +020091
Oleksii Grudev4e3c8b42021-03-02 19:02:45 +020092LVM_LOOP_DEVICE_SIZE=${LVM_LOOP_DEVICE_SIZE:-0}
Vasyl Saienkocd302062021-04-21 22:25:18 +030093CINDER_LVM_LOOP_DEVICE_SIZE=${CINDER_LVM_LOOP_DEVICE_SIZE:-0}
Oleksii Grudev4e3c8b42021-03-02 19:02:45 +020094
Vasyl Saienko8d34ad02021-07-26 11:55:23 +030095_KUBECTL_CMD="kubectl --kubeconfig /etc/kubernetes/admin.conf "
96
Vasyl Saienkoa51d1ca2020-01-02 15:59:16 +020097function retry {
98 local retries=$1
99 shift
100 local msg="$1"
101 shift
102
103 local count=0
104 until "$@"; do
105 exit=$?
106 wait=$((2 ** $count))
107 count=$(($count + 1))
108 if [ $count -lt $retries ]; then
109 echo "Retry $count/$retries exited $exit, retrying in $wait seconds..."
110 sleep $wait
111 else
112 echo "Retry $count/$retries exited $exit, no more retries left."
113 echo "$msg"
114 return $exit
115 fi
116 done
117 return 0
118}
119
Vasyl Saienko300bb0b2023-04-19 06:52:53 +0000120function install_pkgs {
121 local pkgs="$@"
122 function _retry_install {
Vasyl Saienko92133c02024-05-16 10:18:51 +0300123 apt update && DEBIAN_FRONTEND=noninteractive apt install -o DPkg::Options::=--force-confdef -y $pkgs
Vasyl Saienko300bb0b2023-04-19 06:52:53 +0000124 }
125 retry 10 "Labeling node failed" _retry_install
126}
127
Vasyl Saienko806d5dc2022-02-01 14:18:09 +0200128function setup_bind_mounts {
129 mkdir -p /var/lib/persistant-data
Vasyl Saienkoe3107252023-06-06 07:02:58 +0000130 for component in nova glance cinder docker; do
Vasyl Saienko3cc01ca2023-09-25 12:23:43 +0000131 if [[ -d /var/lib/${component} ]]; then
132 continue
133 fi
Vasyl Saienko806d5dc2022-02-01 14:18:09 +0200134 mkdir -p /var/lib/data/${component} /var/lib/${component}
Vasyl Saienko3cc01ca2023-09-25 12:23:43 +0000135 echo "/var/lib/data/${component} /var/lib/${component} none bind 0 0" >> /etc/fstab
Vasyl Saienko806d5dc2022-02-01 14:18:09 +0200136 done
Vasyl Saienkoe3107252023-06-06 07:02:58 +0000137
138 if [[ "${KUBERNETES_INSTALLER}" == "k0s" ]]; then
139 mkdir -p /var/lib/k0s/kubelet
140 mkdir -p /var/lib/kubelet
141 echo "/var/lib/k0s/kubelet /var/lib/kubelet none bind 0 0" >> /etc/fstab
142 fi
143
Vasyl Saienko806d5dc2022-02-01 14:18:09 +0200144 mount -a
145}
146
Vasyl Saienko392dc712020-04-07 15:56:10 +0300147function configure_atop {
148 sed -i 's/INTERVAL=600/INTERVAL=60/' /usr/share/atop/atop.daily
149 systemctl restart atop
150}
151
dbiletskiy70af8192024-06-19 09:27:10 +0200152function disable_unattended_upgr {
Vasyl Saienkoaadd2f82020-08-07 09:35:03 +0300153 # Disable unattended upgrades
154 sed -i 's/1/0/' /etc/apt/apt.conf.d/20auto-upgrades
Oleh Hryhorovbe7bdf72021-04-05 18:21:22 +0300155 sed -i 's/APT::Periodic::Update-Package-Lists "1"/APT::Periodic::Update-Package-Lists "0"/g' /etc/apt/apt.conf.d/*
dbiletskiy70af8192024-06-19 09:27:10 +0200156}
157
Vasyl Saienkof9813512024-07-02 12:30:25 +0300158function configure_logind_conf {
159
160 cat <<EOF >/etc/systemd/logind.conf
161[Login]
162#NAutoVTs=6
163#ReserveVT=6
164#KillUserProcesses=no
165#KillOnlyUsers=
166#KillExcludeUsers=root
167InhibitDelayMaxSec=600
168#UserStopDelaySec=10
169#HandlePowerKey=poweroff
170#HandleSuspendKey=suspend
171#HandleHibernateKey=hibernate
172#HandleLidSwitch=suspend
173#HandleLidSwitchExternalPower=suspend
174#HandleLidSwitchDocked=ignore
175#HandleRebootKey=reboot
176#PowerKeyIgnoreInhibited=no
177#SuspendKeyIgnoreInhibited=no
178#HibernateKeyIgnoreInhibited=no
179#LidSwitchIgnoreInhibited=yes
180#RebootKeyIgnoreInhibited=no
181#HoldoffTimeoutSec=30s
182#IdleAction=ignore
183#IdleActionSec=30min
184#RuntimeDirectorySize=10%
185#RuntimeDirectoryInodesMax=400k
186#RemoveIPC=yes
187#InhibitorsMax=8192
188#SessionsMax=8192
189
190EOF
191
192 systemctl daemon-reload
193
194}
195
dbiletskiy70af8192024-06-19 09:27:10 +0200196function install_required_packages {
Mykyta Karpind6cde5e2022-07-05 12:06:06 +0300197 local pkg_list="apt-transport-https ca-certificates curl software-properties-common jq unzip atop iptables-persistent socat ntp rng-tools open-iscsi nfs-common"
Vasyl Saienkoaadd2f82020-08-07 09:35:03 +0300198
Vasyl Saienkoa7cfc322020-11-06 16:52:22 +0200199 if [[ "${CONFIGURE_HUGE_PAGES}" == true ]]; then
dbiletskiy090662a2024-07-08 14:27:26 +0200200 # Since jammy the igb_uio kernel module contains in the dpdk-kmods-dkms package
201 if [[ ${CODENAME} == "bionic" ]] || [[ ${CODENAME} == "focal" ]]; then
202 pkg_list="$pkg_list dpdk-igb-uio-dkms"
203 else
204 pkg_list="$pkg_list dpdk-kmods-dkms"
205 fi
Anton Samoylovec019d52020-11-19 18:55:47 +0400206 # linux-image-extra-$(uname -r) provides uio_pci_generic module, used by TF DPDK as default driver
207 if [[ "${TUNGSTENFABRIC_ENABLED,,}" == true ]]; then
208 pkg_list="$pkg_list linux-modules-extra-$(uname -r)"
209 fi
Vasyl Saienkoa7cfc322020-11-06 16:52:22 +0200210 fi
Vasyl Saienko300bb0b2023-04-19 06:52:53 +0000211 install_pkgs $pkg_list
212 # Since version 4 yq uses another syntax
Oleksandr Kononenko231933b2024-02-05 16:46:35 +0200213 if [[ ! -f /usr/bin/yq ]]; then
214 curl --retry 6 --retry-delay 5 -L ${BINARY_BASE_URL}/openstack/bin/utils/yq/yq-v3.3.2 -o /usr/bin/yq
215 chmod +x /usr/bin/yq
216 fi
Mykyta Karpin5ed5fd82020-01-27 18:23:29 +0200217}
218
Oleksii Grudev4399e232021-02-24 19:08:25 +0200219function enable_iscsi {
220 systemctl enable iscsid
221 systemctl restart iscsid
222}
Mykyta Karpin5ed5fd82020-01-27 18:23:29 +0200223
Oleksii Grudev4e3c8b42021-03-02 19:02:45 +0200224function configure_lvm {
Vasyl Saienkocd302062021-04-21 22:25:18 +0300225 function _setup_lvm {
226 local device_num=$1
227 local device_size=$2
228 local vg_name=$3
Denis Biletskiy3b21a822022-05-10 10:29:29 +0300229 if vgdisplay | grep ${vg_name}; then
230 echo "Volume group ${vg_name} is already initialize"
Deniscaa72dd2021-06-04 17:13:05 +0300231 else
232 #configure lvm only on compute nodes
233 if [[ ${NODE_METADATA} == *"openstack-compute-node"* ]]; then
234 truncate --size ${device_size}G /srv/disk${device_num}
235 cat <<EOF > /etc/systemd/system/setup-loopback-loop${device_num}.service
Oleksii Grudev4e3c8b42021-03-02 19:02:45 +0200236[Unit]
Vasyl Saienkocd302062021-04-21 22:25:18 +0300237Description=Setup loop${device_num} device
Oleksii Grudev4e3c8b42021-03-02 19:02:45 +0200238DefaultDependencies=no
239After=systemd-udev-settle.service
240Before=lvm2-activation-early.service
241Wants=systemd-udev-settle.service
242
243[Service]
Vasyl Saienkocd302062021-04-21 22:25:18 +0300244ExecStart=-/sbin/losetup /dev/loop${device_num} /srv/disk${device_num}
Oleksii Grudev4e3c8b42021-03-02 19:02:45 +0200245RemainAfterExit=true
246Type=oneshot
247
248[Install]
249WantedBy=local-fs.target
250EOF
Deniscaa72dd2021-06-04 17:13:05 +0300251 systemctl enable setup-loopback-loop${device_num}
252 systemctl start setup-loopback-loop${device_num}
253 #adding short sleep to give time for service to start
254 sleep 3
255 pvcreate /dev/loop${device_num}
256 vgcreate ${vg_name} /dev/loop${device_num}
257 fi
Vasyl Saienkocd302062021-04-21 22:25:18 +0300258 fi
259 }
260
261 if [[ "${LVM_LOOP_DEVICE_SIZE}" -gt 0 ]]; then
Denis Biletskiy3b21a822022-05-10 10:29:29 +0300262 _setup_lvm 10 ${LVM_LOOP_DEVICE_SIZE} nova-vol
Oleksii Grudev4e3c8b42021-03-02 19:02:45 +0200263 fi
Vasyl Saienkocd302062021-04-21 22:25:18 +0300264 if [[ "${CINDER_LVM_LOOP_DEVICE_SIZE}" -gt 0 ]]; then
Denis Biletskiy3b21a822022-05-10 10:29:29 +0300265 _setup_lvm 11 ${CINDER_LVM_LOOP_DEVICE_SIZE} cinder-vol
Vasyl Saienkocd302062021-04-21 22:25:18 +0300266 fi
267
Oleksii Grudev4e3c8b42021-03-02 19:02:45 +0200268}
269
Mykyta Karpin5ed5fd82020-01-27 18:23:29 +0200270function install_docker {
271 function install_retry {
Vasyl Saienkofa714102024-03-22 09:30:25 +0200272 curl --retry 6 --retry-delay 5 -fsSL "${DOCKER_EE_URL}/gpg" | sudo apt-key add -
273 add-apt-repository "deb [arch=amd64] ${DOCKER_EE_URL}/ ${OS_CODENAME} ${DOCKER_EE_RELEASE}"
274 install_pkgs ${DOCKER_EE_PACKAGES}
Vasyl Saienko92133c02024-05-16 10:18:51 +0300275 if [[ $KUBERNETES_CONTAINER_RUNTIME == "crio" ]]; then
276 NERDCTL_VERSION=1.7.6
277 curl --retry 6 --retry-delay 5 -fsSL https://github.com/containerd/nerdctl/releases/download/v${NERDCTL_VERSION}/nerdctl-${NERDCTL_VERSION}-linux-amd64.tar.gz --output nerdctl-${NERDCTL_VERSION}-linux-amd64.tar.gz
278 tar zxvf nerdctl-${NERDCTL_VERSION}-linux-amd64.tar.gz -C /usr/local/bin
279 fi
Mykyta Karpin5ed5fd82020-01-27 18:23:29 +0200280 }
281 retry 10 "Failed to install docker" install_retry
Gerrit MCPbbf9d5d2019-12-19 13:47:41 +0100282}
283
Mykyta Karpin7abaea02020-07-27 15:43:07 +0300284function prepare_docker_config {
Gerrit MCPbbf9d5d2019-12-19 13:47:41 +0100285 mkdir -p /etc/docker
286 cat <<EOF > /etc/docker/daemon.json
287{
Vasyl Saienkoa37de442024-05-02 17:15:47 +0300288 "default-cgroupns-mode": "host",
Gerrit MCPbbf9d5d2019-12-19 13:47:41 +0100289 "default-address-pools": [
290 { "base": "${DOCKER_DEFAULT_ADDRESS_POOL}", "size": ${DOCKER_DEFAULT_ADDRESS_SIZE} }
Mykyta Karpin7abaea02020-07-27 15:43:07 +0300291 ],
292 "log-driver": "json-file",
293 "log-opts": {
294 "max-size": "1g",
295 "max-file": "3"
296 }
Gerrit MCPbbf9d5d2019-12-19 13:47:41 +0100297}
298EOF
299
Vasyl Saienko92133c02024-05-16 10:18:51 +0300300 if [[ $KUBERNETES_CONTAINER_RUNTIME == "crio" ]]; then
301 mkdir -p /etc/containerd/
302cat <<EOF> /etc/containerd/config.toml
Vasyl Saienko05edc402024-06-11 11:17:21 +0300303version = 2
304[plugins]
305[plugins."io.containerd.grpc.v1.cri".containerd.runtimes]
306 [plugins."io.containerd.grpc.v1.cri".containerd.runtimes.runc]
307 runtime_type = "io.containerd.runc.v2"
308 [plugins."io.containerd.grpc.v1.cri".containerd.runtimes.runc.options]
309 SystemdCgroup = true
Vasyl Saienko92133c02024-05-16 10:18:51 +0300310EOF
Vasyl Saienko05edc402024-06-11 11:17:21 +0300311 systemctl restart containerd || /bin/true
Vasyl Saienko92133c02024-05-16 10:18:51 +0300312 fi
313
Gerrit MCPbbf9d5d2019-12-19 13:47:41 +0100314}
315
Vasyl Saienkoe32453e2020-09-16 09:30:34 +0300316function pull_image {
317 local image="$1"
318 function pull_image_retry {
Vasyl Saienko92133c02024-05-16 10:18:51 +0300319 if [[ $KUBERNETES_CONTAINER_RUNTIME == "crio" ]]; then
320 nerdctl --address unix:///run/containerd/containerd.sock -n k8s.io pull $image
321 fi
Vasyl Saienkoe32453e2020-09-16 09:30:34 +0300322 docker pull ${image}
323 }
324 retry 10 "Can't pool docker image $image" pull_image_retry
325}
326
Vasyl Saienko84ee4fd2020-09-02 10:00:18 +0300327function cache_images {
328
Vasyl Saienkoe32453e2020-09-16 09:30:34 +0300329 # Cache UCP image with retries
330 pull_image "${DOCKER_UCP_IMAGE}"
Vasyl Saienko84ee4fd2020-09-02 10:00:18 +0300331
Vasyl Saienkoe32453e2020-09-16 09:30:34 +0300332 local image_prefix=${DOCKER_UCP_IMAGE%/*}
Oleksandr Kononenkobf4ecf82024-02-01 11:02:53 +0200333 for image in $(docker container run --rm --name ucp -v /var/run/docker.sock:/var/run/docker.sock ${DOCKER_UCP_IMAGE} images --list); do
334 pull_image "${image_prefix}/${image##*/}"
Vasyl Saienko92133c02024-05-16 10:18:51 +0300335 if [[ $KUBERNETES_CONTAINER_RUNTIME == "crio" ]]; then
336 nerdctl --address unix:///run/containerd/containerd.sock -n k8s.io tag ${image_prefix}/${image##*/} mirantis/${image##*/}
337 fi
Oleksandr Kononenkobf4ecf82024-02-01 11:02:53 +0200338 docker tag ${image_prefix}/${image##*/} mirantis/${image##*/}
339 done
Vasyl Saienko84ee4fd2020-09-02 10:00:18 +0300340}
341
Gerrit MCPbbf9d5d2019-12-19 13:47:41 +0100342function install_ucp {
Deniscaa72dd2021-06-04 17:13:05 +0300343 if docker ps --all | grep ucp-controller; then
344 echo "Docker UCP container is running"
345 else
346 local tmpd
347 tmpd=$(mktemp -d)
348 function docker_run_retry {
349 docker container run --rm --name ucp \
350 -v /var/run/docker.sock:/var/run/docker.sock \
351 ${DOCKER_UCP_IMAGE} install \
352 --host-address $UCP_IP_ADDRESS \
353 --admin-username $UCP_USERNAME \
354 --admin-password $UCP_PASSWORD \
355 --existing-config
356 }
357 retry 10 "Can't bring up docker UCP container" docker_run_retry
358 fi
Gerrit MCPbbf9d5d2019-12-19 13:47:41 +0100359}
360
Vasyl Saienko8d34ad02021-07-26 11:55:23 +0300361function download_k0s {
Vasyl Saienko3e8ba732024-09-09 14:07:49 +0300362 curl -sSLf https://get.k0s.sh -o /tmp/get.k0s.sh
363 sh /tmp/get.k0s.sh
Vasyl Saienko8d34ad02021-07-26 11:55:23 +0300364}
365
366function install_k0s {
Vasyl Saienko59e5b2a2024-09-09 08:57:18 +0300367 local install_flags="--enable-worker"
368 if [[ "${SINGLE_NODE}" == true ]]; then
369 install_flags="$install_flags --single"
370 fi
Vasyl Saienkoe3107252023-06-06 07:02:58 +0000371 mkdir -p /etc/k0s
372 k0s config create > /etc/k0s/k0s.yaml
Vasyl Saienko59e5b2a2024-09-09 08:57:18 +0300373 k0s install controller -c /etc/k0s/k0s.yaml $install_flags
Vasyl Saienko8d34ad02021-07-26 11:55:23 +0300374 k0s start
375
Vasyl Saienkoe3107252023-06-06 07:02:58 +0000376 sleep 60
Vasyl Saienko8d34ad02021-07-26 11:55:23 +0300377
Vasyl Saienkoe2c68dd2022-01-27 17:10:12 +0200378 mkdir -p /etc/kubernetes /root/.kube/
Vasyl Saienko8d34ad02021-07-26 11:55:23 +0300379 k0s kubeconfig admin > /etc/kubernetes/admin.conf
380 cp /etc/kubernetes/admin.conf /root/.kube/config
381
382 mkdir -p /etc/k0s
383 k0s kubeconfig admin > /etc/k0s/admin.conf
Vasyl Saienko59e5b2a2024-09-09 08:57:18 +0300384 if [[ "${SINGLE_NODE}" != true ]]; then
385 k0s token create --role=worker > /etc/k0s/worker_token.yaml
386 k0s token create --role=controller > /etc/k0s/controller_token.yaml
387 fi
Vasyl Saienko8d34ad02021-07-26 11:55:23 +0300388
Vasyl Saienko300bb0b2023-04-19 06:52:53 +0000389 install_pkgs nginx
Vasyl Saienko8d34ad02021-07-26 11:55:23 +0300390 rm -f /etc/nginx/sites-enabled/default
391 ln -s /etc/k0s/ /var/www/k0s
392 cat << EOF > /etc/nginx/sites-enabled/k0s
393server {
394 listen *:80;
395 root /var/www;
396
397 location /k0s {
398 autoindex on;
399 }
400}
401EOF
402 systemctl restart nginx
403}
404
Oleh Hryhorovcd1a3bd2020-12-04 13:21:06 +0200405function get_authtoken_retry {
406# Download the bundle https://docs.docker.com/ee/ucp/user-access/cli/
407# Create an environment variable with the user security token
408 AUTHTOKEN=$(curl --retry 6 --retry-delay 5 -sk -d '{"username":"'$UCP_USERNAME'","password":"'$UCP_PASSWORD'"}' https://${UCP_MASTER_HOST}/auth/login | jq -r .auth_token)
409 if [ -z ${AUTHTOKEN} ]; then
410 return -1
411 fi
412}
413
Gerrit MCPbbf9d5d2019-12-19 13:47:41 +0100414function download_bundles {
415 local tmpd
416 tmpd=$(mktemp -d)
Oleh Hryhorova0754872020-01-24 16:45:05 +0200417
418 function download_bundles_retry {
419 # Download the client certificate bundle
Vasyl Saienkof3dba8c2020-02-14 10:07:23 +0200420 curl --retry 6 --retry-delay 5 -k -H "Authorization: Bearer $AUTHTOKEN" https://${UCP_MASTER_HOST}/api/clientbundle -o ${tmpd}/bundle.zip
Oleh Hryhorova0754872020-01-24 16:45:05 +0200421 }
Oleh Hryhorovb58460e2020-02-05 15:53:48 +0200422
Vasyl Saienko9b688992020-02-13 13:50:53 +0200423 retry 10 "Can't get AUTHTOKEN from master." get_authtoken_retry
424 retry 10 "Can't download bundle file from master." download_bundles_retry
Gerrit MCPbbf9d5d2019-12-19 13:47:41 +0100425
426 pushd $tmpd
427 # Unzip the bundle.
428 unzip bundle.zip
429
430 # Run the utility script.
431 eval "$(<env.sh)"
Vasyl Saienkof9f668c2020-01-04 11:36:35 +0200432 mkdir -p /etc/kubernetes /root/.kube/
Vasyl Saienko5b379422020-01-04 12:24:08 +0200433 cp kube.yml /etc/kubernetes/admin.conf
434 cp kube.yml /root/.kube/config
Gerrit MCPbbf9d5d2019-12-19 13:47:41 +0100435 popd
436}
437
Vasyl Saienko3ab29c92020-01-03 10:39:03 +0200438function wait_for_node {
439 function retry_wait {
Vasyl Saienko8d34ad02021-07-26 11:55:23 +0300440 ${_KUBECTL_CMD} get nodes |grep -w Ready |awk '{print $1}' |grep -q $(hostname)
Vasyl Saienko3ab29c92020-01-03 10:39:03 +0200441 }
442 retry $NODE_DEPLOYMENT_RETRIES "The node didn't come up." retry_wait
443}
444
Vasyl Saienko8d34ad02021-07-26 11:55:23 +0300445function download_k8s_metadata {
446 mkdir -p /etc/k0s/
447 mkdir -p /etc/kubernetes
448 for f in worker_token.yaml controller_token.yaml admin.conf; do
449 curl --retry 6 --retry-delay 5 -L http://${UCP_MASTER_HOST}/k0s/${f} -o /etc/k0s/${f}
450 done
451 cp /etc/k0s/admin.conf /etc/kubernetes/admin.conf
452 mkdir -p /root/.kube/
453 cp /etc/k0s/admin.conf /root/.kube/config
454}
455
Gerrit MCPbbf9d5d2019-12-19 13:47:41 +0100456function join_node {
Deniscaa72dd2021-06-04 17:13:05 +0300457 if kubectl --kubeconfig /etc/kubernetes/admin.conf get nodes |grep -w Ready |awk '{print $1}' |grep -q $(hostname); then
458 echo "This node is already join"
459 else
460 local type=${1}
461 function retry_join_node {
462 env -i $(docker swarm join-token $type |grep 'docker swarm join' | xargs)
Mykyta Karpine2d2fda2023-11-23 10:41:51 +0200463 sleep 20 && systemctl restart docker && sleep 20
Deniscaa72dd2021-06-04 17:13:05 +0300464 }
465 retry 10 "Failed to join node to swarm" retry_join_node
466 fi
Gerrit MCPbbf9d5d2019-12-19 13:47:41 +0100467}
468
Vasyl Saienko8d34ad02021-07-26 11:55:23 +0300469function join_k0s_node {
470 k0s install worker --token-file /etc/k0s/worker_token.yaml
471 k0s start
472}
473
Gerrit MCPbbf9d5d2019-12-19 13:47:41 +0100474function create_ucp_config {
Oleh Hryhorovcd1a3bd2020-12-04 13:21:06 +0200475 if [[ "${SINGLE_NODE}" == true ]]; then
476 max_pods="kubelet_max_pods = 220"
477 fi
Michael Polenchuk06bfa152022-02-15 17:50:04 +0400478 if [[ "${SECURE_OVERLAY_ENABLED,,}" == true ]]; then
479 secure_overlay="secure_overlay = true
480 calico_vxlan = false"
481 fi
Vasyl Saienko92133c02024-05-16 10:18:51 +0300482 kubelet_custom_flags=""
483 if [[ $KUBERNETES_CONTAINER_RUNTIME == "crio" ]]; then
Vasyl Saienko05edc402024-06-11 11:17:21 +0300484 kubelet_custom_flags='custom_kubelet_flags=["--cgroup-driver=systemd", "--container-runtime-endpoint=unix:///run/containerd/containerd.sock"]'
Vasyl Saienko92133c02024-05-16 10:18:51 +0300485 fi
Deniscaa72dd2021-06-04 17:13:05 +0300486 if docker config ls | grep com.docker.ucp.config ; then
487 echo "Config com.docker.ucp.config already exists"
488 else
489 echo "
Vasyl Saienko2fcc70b2020-01-02 10:09:08 +0200490[scheduling_configuration]
Gerrit MCPbbf9d5d2019-12-19 13:47:41 +0100491 enable_admin_ucp_scheduling = true
Vasyl Saienko2fcc70b2020-01-02 10:09:08 +0200492 default_node_orchestrator = \"kubernetes\"
Vasyl Saienkoccd3a752020-01-02 10:21:54 +0200493[cluster_config]
494 dns = [\"172.18.208.44\"]
Vasyl Saienko06c7afe2022-07-01 17:53:56 +0000495 calico_vxlan_port = \"$UCP_DOCKER_CALICO_VXLAN_PORT\"
Oleh Hryhorovcd1a3bd2020-12-04 13:21:06 +0200496 ${max_pods}
Michael Polenchuk06bfa152022-02-15 17:50:04 +0400497 ${secure_overlay}
Vasyl Saienko92133c02024-05-16 10:18:51 +0300498 ${kubelet_custom_flags}
Mykyta Karpin9df830a2024-09-12 14:25:01 +0200499[audit_log_configuration]
500 level = \"${UCP_AUDIT_LOG_LEVEL}\"
501 support_dump_include_audit_logs = false
Vasyl Saienkoccd3a752020-01-02 10:21:54 +0200502" | docker config create com.docker.ucp.config -
Deniscaa72dd2021-06-04 17:13:05 +0300503 fi
Gerrit MCPbbf9d5d2019-12-19 13:47:41 +0100504}
505
506function swarm_init {
Deniscaa72dd2021-06-04 17:13:05 +0300507 if docker node ls | grep $HOSTNAME; then
508 echo "This node is already part of a swarm"
509 else
510 docker swarm init --advertise-addr ${UCP_IP_ADDRESS} --data-path-addr ${UCP_IP_ADDRESS} --listen-addr ${UCP_IP_ADDRESS} --data-path-port ${UCP_DOCKER_SWARM_DATA_PORT}
Vasyl Saienko80b4c592023-10-13 14:24:00 +0000511 sleep 5 && systemctl restart docker && sleep 5
Deniscaa72dd2021-06-04 17:13:05 +0300512 fi
Gerrit MCPbbf9d5d2019-12-19 13:47:41 +0100513}
514
515function rm_ucp_config {
516 docker config rm com.docker.ucp.config
517}
518
Vasyl Saienkob58c7a52020-01-02 09:14:40 +0200519function install_kubectl {
Michael Polenchuk92ca1b32020-11-13 15:58:38 +0400520 local kubectl=/usr/local/bin/kubectl
Vasyl Saienkoa94bf182023-04-01 10:42:17 +0000521 curl --retry 6 --retry-delay 5 -L ${BINARY_BASE_URL}/openstack/bin/utils/kubectl/kubectl-${KUBECTL_VERSION}-linux -o ${kubectl}
Michael Polenchuk92ca1b32020-11-13 15:58:38 +0400522 chmod +x ${kubectl}
Ilya Menkov2d22bd02020-02-14 15:01:29 +0400523cat << EOF >> ~/.bashrc
524source /usr/share/bash-completion/bash_completion
525source <(kubectl completion bash)
526EOF
Vasyl Saienkob58c7a52020-01-02 09:14:40 +0200527}
528
Vasyl Saienko08166342020-02-28 12:20:51 +0200529function configure_ntp {
Vasyl Saienkoe2c68dd2022-01-27 17:10:12 +0200530 echo "" > /etc/ntp.conf
Vasyl Saienko08166342020-02-28 12:20:51 +0200531
Anton Samoylov6d2cf7b2020-09-01 11:35:11 +0400532 for server in $NTP_SERVERS; do
533cat << EOF >> /etc/ntp.conf
534server ${server} iburst
535EOF
536 done
537
538 cat << EOF >> /etc/ntp.conf
539# Set general access to this service
540restrict -4 default kod nomodify notrap nopeer noquery
541restrict -6 default kod nomodify notrap nopeer noquery
542
543# Allow access from localhost
544restrict 127.0.0.1
545restrict ::1
546
547# Do not listen on any interface address by default
548interface ignore wildcard
549interface listen lo
550interface listen ${DEFAULT_INTERFACE}
551
552# Location of drift file
553driftfile /var/lib/ntp/ntp.drift
554EOF
555
ibumarskov35370042022-04-19 14:27:02 +0400556 systemctl disable systemd-timesyncd
557 systemctl enable ntp
Anton Samoylov6d2cf7b2020-09-01 11:35:11 +0400558 systemctl restart ntp
Vasyl Saienko08166342020-02-28 12:20:51 +0200559}
560
Vasyl Saienko5af97c32022-01-25 09:45:45 +0200561function wait_for_external_network {
562 function _check_access {
563 curl --connect-timeout 10 ${DOCKER_UCP_IMAGE%%/*} || (sleep 1; /bin/false)
564 }
565 retry 180 "Failed to wait for external networks reachable." _check_access
566}
567
Vasyl Saienko2fcc70b2020-01-02 10:09:08 +0200568function prepare_network {
Dennis Dmitrievea894212020-03-12 21:36:43 +0200569 if [ -z "${CONTROL_IP_ADDRESS}" ]; then
Vasyl Saienko7c595922021-03-17 13:58:13 +0200570 wait_condition_send "FAILURE" "CONTROL_IP_ADDRESS is not found."
Dennis Dmitrievea894212020-03-12 21:36:43 +0200571 exit 1
572 fi
573
Vasyl Saienko2fcc70b2020-01-02 10:09:08 +0200574 systemctl restart systemd-resolved
Vasyl Saienkoc14c43d2020-01-08 13:06:18 +0200575 # Make sure local hostname is present in /etc/hosts
Vasyl Saienko8d34ad02021-07-26 11:55:23 +0300576 sed -i "s/127.0.0.1 localhost/127.0.0.1 localhost\n${CONTROL_IP_ADDRESS} $(hostname -s).cluster.local $(hostname -s)/" /etc/hosts
Vasyl Saienko2fcc70b2020-01-02 10:09:08 +0200577}
578
Vasyl Saienko7d7acea2020-01-03 16:59:05 +0200579function workaround_default_forward_policy {
Vasyl Saienkod67e35e2023-09-27 14:44:01 +0000580 local should_run=0
581 for net in $FLOATING_NETWORK_PREFIXES; do
582 if ! iptables -nvL DOCKER-USER |grep $net; then
583 should_run=1
584 break
585 fi
586 done
587 if [[ $should_run == 0 ]]; then
588 echo "Iptables is already configured"
Deniscaa72dd2021-06-04 17:13:05 +0300589 else
590 cat << EOF > /etc/iptables/rules.v4
Vasyl Saienkobd34d2a2020-03-10 16:25:17 +0200591*filter
592:DOCKER-USER - [0:0]
593EOF
Deniscaa72dd2021-06-04 17:13:05 +0300594 for net in $FLOATING_NETWORK_PREFIXES; do
Vasyl Saienkobd34d2a2020-03-10 16:25:17 +0200595cat << EOF >> /etc/iptables/rules.v4
596-A DOCKER-USER -d ${net} -j ACCEPT
597-A DOCKER-USER -s ${net} -j ACCEPT
Vasyl Saienkobd34d2a2020-03-10 16:25:17 +0200598EOF
Deniscaa72dd2021-06-04 17:13:05 +0300599 done
Vasyl Saienkobd34d2a2020-03-10 16:25:17 +0200600
601cat << EOF >> /etc/iptables/rules.v4
602-A DOCKER-USER -j RETURN
Vasyl Saienko1785c352020-03-11 11:39:40 +0200603COMMIT
Vasyl Saienkobd34d2a2020-03-10 16:25:17 +0200604EOF
Vasyl Saienko02df39a2020-05-20 16:39:35 +0300605
606#Allow access to Internet from VMs for virtualized environment.
607cat << EOF >> /etc/iptables/rules.v4
608*nat
609:POSTROUTING ACCEPT - [0:0]
610EOF
Deniscaa72dd2021-06-04 17:13:05 +0300611 for net in $FLOATING_NETWORK_PREFIXES; do
Vasyl Saienko02df39a2020-05-20 16:39:35 +0300612cat << EOF >> /etc/iptables/rules.v4
613-A POSTROUTING -s ${net} -o ${DEFAULT_INTERFACE} -j MASQUERADE
614EOF
Deniscaa72dd2021-06-04 17:13:05 +0300615 done
Vasyl Saienko02df39a2020-05-20 16:39:35 +0300616
617cat << EOF >> /etc/iptables/rules.v4
618COMMIT
619EOF
Deniscaa72dd2021-06-04 17:13:05 +0300620 sudo netfilter-persistent reload
621 fi
Vasyl Saienko7d7acea2020-01-03 16:59:05 +0200622}
623
Dennis Dmitrievea894212020-03-12 21:36:43 +0200624function disable_rp_filter {
625 # Run this func before "network_config" to create new interfaces with the default rp_filter value
626 cat << EOF > /etc/sysctl.d/99-disable-rp-filter.conf
627net.ipv4.conf.all.rp_filter=0
628net.ipv4.conf.default.rp_filter=0
629EOF
Vasyl Saienkoe61a98c2021-10-19 21:42:19 +0300630 for iface in $(find /proc/sys/net/ipv4/conf/ -name "enp*" -o -name "ens*" -o -name "eth*"); do
631 echo 0 > $iface/rp_filter
Oleh Hryhorov207137d2021-04-13 12:03:39 +0300632 done
633
Dennis Dmitrievea894212020-03-12 21:36:43 +0200634 sysctl -p /etc/sysctl.d/99-disable-rp-filter.conf
635}
636
Vasyl Saienko45280eb2024-06-24 11:45:13 +0000637function configure_sysctl_limits {
638 cat << EOF > /etc/sysctl.d/99-limits.conf
639fs.inotify.max_user_instances = 8192
640EOF
641 sysctl -p /etc/sysctl.d/99-limits.conf
642}
643
Vasyl Saienko83f309e2020-05-06 11:14:28 +0300644function configure_contrack {
645 cat << EOF > /etc/sysctl.d/100-contrackd.conf
646net.netfilter.nf_conntrack_log_invalid=255
647net.netfilter.nf_conntrack_tcp_be_liberal=1
648EOF
649
650 sysctl -p /etc/sysctl.d/100-contrackd.conf
651}
652
Vasyl Saienkob377de02020-05-25 14:51:31 +0300653# NOTE(vsaienko): disable calling iptables when sending packets via bridge.
654function disable_iptables_for_bridges {
655 cat << EOF > /etc/sysctl.d/101-bridge-nf-call-iptables.conf
656net.bridge.bridge-nf-call-ip6tables=0
657net.bridge.bridge-nf-call-iptables=0
658net.bridge.bridge-nf-call-arptables=0
659EOF
660
661 sysctl -p /etc/sysctl.d/101-bridge-nf-call-iptables.conf
662}
663
Oleh Hryhorovb58460e2020-02-05 15:53:48 +0200664function network_config {
Vasyl Saienko9b688992020-02-13 13:50:53 +0200665 PUBLIC_NODE_IP_ADDRESS=${PUBLIC_INTERFACE_IP:-$(ip addr show dev ${PUBLIC_INTERFACE} | grep -Po 'inet \K[\d.]+' | egrep -v "127.0.|172.17")}
666 PUBLIC_NODE_IP_NETMASK=${PUBLIC_INTERFACE_NETMASK:-$(ip addr show dev ${PUBLIC_INTERFACE} | grep -Po 'inet \K[\d.]+\/[\d]+' | egrep -v "127.0.|172.17" | cut -d'/' -f2)}
667
Oleh Hryhorov404e9572020-01-21 18:57:22 +0200668 local public_interface=${1:-${PUBLIC_INTERFACE}}
669 local cloud_netplan_cfg="/etc/netplan/50-cloud-init.yaml"
670 local match_ip_line
Vasyl Saienko9b688992020-02-13 13:50:53 +0200671
Vasyl Saienko300bb0b2023-04-19 06:52:53 +0000672 install_pkgs bridge-utils
Mykyta Karpin5ed5fd82020-01-27 18:23:29 +0200673
Oleh Hryhorov404e9572020-01-21 18:57:22 +0200674cat << EOF > /etc/systemd/network/10-veth-phy-br.netdev
675[NetDev]
676Name=veth-phy
677Kind=veth
678[Peer]
679Name=veth-br
680EOF
Vasyl Saienko9b688992020-02-13 13:50:53 +0200681
Vasyl Saienko66522982020-05-25 09:34:43 +0300682if [[ -n ${IRONIC_BAREMETAL_INTERFACE} ]]; then
683
Vasyl Saienko4c468192020-05-19 11:51:13 +0300684cat << EOF > /etc/systemd/network/11-veth-bm.netdev
685[NetDev]
686Name=veth-bm
687Kind=veth
688[Peer]
689Name=veth-bm-br
690EOF
691
Vasyl Saienko1233afd2020-05-21 14:55:21 +0300692cat << EOF > /etc/systemd/network/12-veth-vbmc.netdev
693[NetDev]
694Name=veth-vbmc
695Kind=veth
696[Peer]
697Name=veth-vbmc-br
698EOF
Vasyl Saienko66522982020-05-25 09:34:43 +0300699fi
Vasyl Saienko1233afd2020-05-21 14:55:21 +0300700
Oleh Hryhorov404e9572020-01-21 18:57:22 +0200701 sed -i 's/.*ethernets:.*/&\n veth-phy: {}/' ${cloud_netplan_cfg}
702 sed -i 's/.*ethernets:.*/&\n veth-br: {}/' ${cloud_netplan_cfg}
Vasyl Saienko66522982020-05-25 09:34:43 +0300703if [[ -n ${IRONIC_BAREMETAL_INTERFACE} ]]; then
Vasyl Saienko4c468192020-05-19 11:51:13 +0300704 sed -i 's/.*ethernets:.*/&\n veth-bm: {}/' ${cloud_netplan_cfg}
705 sed -i 's/.*ethernets:.*/&\n veth-bm-br: {}/' ${cloud_netplan_cfg}
Vasyl Saienko1233afd2020-05-21 14:55:21 +0300706 sed -i 's/.*ethernets:.*/&\n veth-vbmc: {}/' ${cloud_netplan_cfg}
707 sed -i 's/.*ethernets:.*/&\n veth-vbmc-br: {}/' ${cloud_netplan_cfg}
Oleh Hryhorov404e9572020-01-21 18:57:22 +0200708
Michael Polenchuk486cadf2020-08-24 16:25:51 +0400709 if [[ "${IRONIC_MT_ENABLED,,}" == true ]] ; then
710 sed -i "s/.*ethernets:.*/&\n ${IRONIC_BAREMETAL_VXLAN_INTERFACE}: {}/" ${cloud_netplan_cfg}
711 sed -i 's/.*ethernets:.*/&\n veth-pxe: {}/' ${cloud_netplan_cfg}
712 sed -i 's/.*ethernets:.*/&\n veth-pxe-br: {}/' ${cloud_netplan_cfg}
713cat << EOF > /etc/systemd/network/13-veth-pxe.netdev
714[NetDev]
715Name=veth-pxe
716Kind=veth
717[Peer]
718Name=veth-pxe-br
719EOF
720 sed -i "s/- ${IRONIC_BAREMETAL_NETWORK_PREFIX}\([0-9]*\)/- ${IRONIC_BAREMETAL_TUNNEL_NETWORK_PREFIX}\1/" ${cloud_netplan_cfg}
721cat << EOF >> ${cloud_netplan_cfg}
722 vlans:
723 pxe.1000:
724 id: 1000
725 link: veth-pxe
726 addresses:
727 - ${IRONIC_BAREMETAL_INTERFACE_IP}/${IRONIC_BAREMETAL_NETWORK_NETMASK}
728EOF
729 else
730 ironic_baremetal_address_match_ip_line=$(grep -nm1 "${IRONIC_BAREMETAL_INTERFACE_IP}/${IRONIC_BAREMETAL_NETWORK_NETMASK}" ${cloud_netplan_cfg} | cut -d: -f1)
731 sed -i "$((${ironic_baremetal_address_match_ip_line}-1)),$((${ironic_baremetal_address_match_ip_line}))d" ${cloud_netplan_cfg}
732 fi
Vasyl Saienko66522982020-05-25 09:34:43 +0300733fi
Vasyl Saienko4c468192020-05-19 11:51:13 +0300734
Mykyta Karpinb3a7f292024-10-18 15:44:36 +0200735public_address_match_ip_line=$(grep -nm1 "${PUBLIC_NODE_IP_ADDRESS}/${PUBLIC_NODE_IP_NETMASK}" ${cloud_netplan_cfg} | cut -d: -f1)
736if [ -n "${public_address_match_ip_line}" ] ; then
737 sed -i "$((${public_address_match_ip_line}-1)),$((${public_address_match_ip_line}))d" ${cloud_netplan_cfg}
738fi
Oleh Hryhorov404e9572020-01-21 18:57:22 +0200739
740cat << EOF >> ${cloud_netplan_cfg}
741 bridges:
742 br-public:
743 dhcp4: false
744 interfaces:
745 - ${PUBLIC_INTERFACE}
746 - veth-br
Mykyta Karpinb3a7f292024-10-18 15:44:36 +0200747EOF
748if [[ ${NODE_METADATA} == *"tempest"* ]]; then
749cat << EOF >> ${cloud_netplan_cfg}
Oleh Hryhorov404e9572020-01-21 18:57:22 +0200750 addresses:
751 - ${PUBLIC_NODE_IP_ADDRESS}/${PUBLIC_NODE_IP_NETMASK}
Vasyl Saienko66522982020-05-25 09:34:43 +0300752EOF
Vasyl Saienko5b6d70a2023-04-10 13:55:31 +0000753# Assign more ips for neutron dynamic routing PRODX-31417
Vasyl Saienko5b6d70a2023-04-10 13:55:31 +0000754 for i in {71..76}; do
755cat << EOF >> ${cloud_netplan_cfg}
Vasyl Saienkoc7639002023-04-11 07:40:29 +0000756 - ${PUBLIC_NODE_IP_ADDRESS%.*}.${i}/${PUBLIC_NODE_IP_NETMASK}
Vasyl Saienko5b6d70a2023-04-10 13:55:31 +0000757EOF
758 done
759fi
Vasyl Saienko66522982020-05-25 09:34:43 +0300760
Vasyl Saienkof79d0942020-11-17 14:33:55 +0200761# Remove Tunnel interface from netplan
762if [[ $TUNNEL_INTERFACE_NETPLAN_MANAGE == false ]]; then
763 sed -i "/ ${TUNNEL_INTERFACE}/,/ set-name: ${TUNNEL_INTERFACE}/d" ${cloud_netplan_cfg}
764fi
765
Vasyl Saienko66522982020-05-25 09:34:43 +0300766if [[ -n ${IRONIC_BAREMETAL_INTERFACE} ]]; then
767cat << EOF >> ${cloud_netplan_cfg}
Vasyl Saienko4c468192020-05-19 11:51:13 +0300768 br-baremetal:
769 dhcp4: false
770 interfaces:
Vasyl Saienko4c468192020-05-19 11:51:13 +0300771 - veth-bm-br
Vasyl Saienko1233afd2020-05-21 14:55:21 +0300772 - veth-vbmc-br
Michael Polenchuk486cadf2020-08-24 16:25:51 +0400773EOF
774 if [[ "${IRONIC_MT_ENABLED,,}" != true ]] ; then
775cat << EOF >> ${cloud_netplan_cfg}
776 - ${IRONIC_BAREMETAL_INTERFACE}
Vasyl Saienko4c468192020-05-19 11:51:13 +0300777 addresses:
778 - ${IRONIC_BAREMETAL_INTERFACE_IP}/${IRONIC_BAREMETAL_NETWORK_NETMASK}
Oleh Hryhorov404e9572020-01-21 18:57:22 +0200779EOF
Michael Polenchuk486cadf2020-08-24 16:25:51 +0400780 else
781cat << EOF >> ${cloud_netplan_cfg}
782 - ${IRONIC_BAREMETAL_VXLAN_INTERFACE}
783 - veth-pxe-br
784EOF
Vasyl Saienko3dd0c272020-11-16 17:20:07 +0200785
Michael Polenchuk486cadf2020-08-24 16:25:51 +0400786cat << EOF > /etc/systemd/system/ironic-vxlan-tunnel.service
787[Unit]
788Description=Ironic VXLAN tunnel
789After=network.target
790
791[Service]
792Type=oneshot
793RemainAfterExit=true
794ExecStart=/sbin/ip link add ${IRONIC_BAREMETAL_VXLAN_INTERFACE} type vxlan id 10 group 239.1.1.10 dstport 0 dev ${IRONIC_BAREMETAL_INTERFACE}
795ExecStart=/sbin/ip link set ${IRONIC_BAREMETAL_VXLAN_INTERFACE} up
796
797[Install]
798WantedBy=multi-user.target
799EOF
800
801 systemctl enable ironic-vxlan-tunnel.service
802 systemctl start ironic-vxlan-tunnel.service
803 fi
Vasyl Saienko66522982020-05-25 09:34:43 +0300804fi
Oleh Hryhorov404e9572020-01-21 18:57:22 +0200805 netplan --debug apply
Vasyl Saienko9b688992020-02-13 13:50:53 +0200806
807 # NOTE(vsaienko): give some time to apply changes
808 sleep 15
Vasyl Saienko16c81c02020-11-17 17:10:55 +0200809
810# Remove Tunnel interface from netplan
811if [[ $TUNNEL_INTERFACE_NETPLAN_MANAGE == false ]]; then
812 ip addr flush ${TUNNEL_INTERFACE}
813 ip link set ${TUNNEL_INTERFACE} up
814fi
815
Oleh Hryhorov404e9572020-01-21 18:57:22 +0200816}
817
Oleh Hryhorovb58460e2020-02-05 15:53:48 +0200818$functions_override
819
Oleh Hryhorov404e9572020-01-21 18:57:22 +0200820function set_node_labels {
Vasyl Saienko2ae2eec2020-02-20 10:11:32 +0200821 function set_node_labels_retry {
Vasyl Saienko8d34ad02021-07-26 11:55:23 +0300822 ${_KUBECTL_CMD} patch node $(hostname) -p "{\"metadata\": $(echo $NODE_METADATA | jq -c ".")}"
Vasyl Saienko2ae2eec2020-02-20 10:11:32 +0200823 }
Vasyl Saienko08024582020-02-20 11:45:34 +0200824 retry 10 "Labeling node failed" set_node_labels_retry
Oleh Hryhorov404e9572020-01-21 18:57:22 +0200825}
Gerrit MCPbbf9d5d2019-12-19 13:47:41 +0100826
Vasyl Saienko1eb06d62020-11-16 10:16:22 +0200827HW_METADATA='{}'
Vasyl Saienkof9ee1582020-03-02 16:53:41 +0200828function prepare_metadata_files {
Dennis Dmitrievea894212020-03-12 21:36:43 +0200829 /usr/sbin/prepare-metadata.py --metadata-file /usr/share/metadata/lab-metadata.yaml
830}
831
832function collect_ceph_metadata {
Oleh Hryhorovf14fc922020-03-10 12:11:10 +0200833 local ceph_osd_node
Vasyl Saienko8d34ad02021-07-26 11:55:23 +0300834 ceph_osd_node=$(${_KUBECTL_CMD} get nodes -l role=ceph-osd-node -o jsonpath={.items[?\(@.metadata.name==\"$(hostname)\"\)].metadata.name})
Oleh Hryhorovf14fc922020-03-10 12:11:10 +0200835
Oleh Hryhorovf14fc922020-03-10 12:11:10 +0200836 if [[ -f /usr/share/metadata/ceph.yaml && ${ceph_osd_node} ]]; then
Vasyl Saienkof9ee1582020-03-02 16:53:41 +0200837 HW_METADATA="{\"ceph\": {\"$(hostname)\": \"$(base64 -w 0 /usr/share/metadata/ceph.yaml)\"}}"
Oleh Hryhorov39fbfe52020-03-12 15:26:53 +0200838 ceph_store_drive=$(cat /usr/share/metadata/ceph.yaml | egrep '\- name\: vd?' | awk '{print $3}')
839 if [[ -b /dev/${ceph_store_drive} ]]; then
840 sgdisk --zap-all /dev/${ceph_store_drive}
841 fi
Vasyl Saienkof9ee1582020-03-02 16:53:41 +0200842 fi
843}
844
Oleh Hryhorove7feab42020-11-11 18:00:29 +0200845function collect_interfaces_metadata {
Vasyl Saienko3dd0c272020-11-16 17:20:07 +0200846
Oleh Hryhorove7feab42020-11-11 18:00:29 +0200847 local if_metadata_file="/usr/share/metadata/interfaces.yaml"
Denisd2635bf2021-06-15 13:35:36 +0300848 if [ -z "${TUNNEL_INTERFACE}" ] ; then
849 echo "Tunnel interface is empty"
850 else
851 pci_id=$(ethtool -i ${TUNNEL_INTERFACE} |grep bus-info | awk '{print $2}')
852 mac=$(cat $(find /sys/bus/pci/devices/${pci_id}/ -name net)/${TUNNEL_INTERFACE}/address)
853 ip=${TUNNEL_INTERFACE_IP}/${TUNNEL_INTERFACE_NETWORK_NETMASK}
Oleh Hryhorove7feab42020-11-11 18:00:29 +0200854
Denisd2635bf2021-06-15 13:35:36 +0300855 cat << EOF > ${if_metadata_file}
Vasyl Saienko592730c2020-11-17 11:36:46 +0200856${TUNNEL_INTERFACE}:
Vasyl Saienko1152e982020-11-16 16:23:32 +0200857 pci_id: "${pci_id}"
858 ip_addr: "${ip}"
859 mac: "${mac}"
Oleh Hryhorove7feab42020-11-11 18:00:29 +0200860EOF
861
Denisd2635bf2021-06-15 13:35:36 +0300862 fi
Oleh Hryhorove7feab42020-11-11 18:00:29 +0200863 HW_METADATA=$(echo ${HW_METADATA} | jq -cr ". += {\"interfaces\": {\"$(hostname)\": \"$(base64 -w 0 ${if_metadata_file})\"}}")
864
865}
866
Oleh Hryhorovab3793f2020-06-04 18:37:22 +0300867function nested_virt_config {
868
Vasyl Saienkoe61a98c2021-10-19 21:42:19 +0300869if grep --color vmx /proc/cpuinfo; then
870 cat << EOF > /etc/modprobe.d/qemu-system-x86.conf
Oleh Hryhorovab3793f2020-06-04 18:37:22 +0300871options kvm_intel nested=1
872options kvm_intel enable_apicv=1
873options kvm_intel ept=1
874EOF
875
876 modprobe -r kvm_intel || true
877 modprobe kvm_intel nested=1
Vasyl Saienkoe61a98c2021-10-19 21:42:19 +0300878else
879 echo "Nested virtualization is not supported"
880fi
Oleh Hryhorovab3793f2020-06-04 18:37:22 +0300881
882}
Vasyl Saienkof9ee1582020-03-02 16:53:41 +0200883
Mykyta Karpin6a153c52020-11-03 15:21:38 +0200884function configure_huge_pages {
885 local nr_2Mb
886 local nr_1G
887 local apply_grub=false
888
889 if [[ ! $(cat /etc/default/grub | grep -E ^GRUB_CMDLINE_LINUX=".*hugepagesz=2M.*") ]] && [[ "${HUGE_PAGES_2Mb_SIZE}" -gt 0 ]]; then
890 nr_2Mb=$(( "${HUGE_PAGES_2Mb_SIZE}" / 2 ))
891 echo "Configure GRUB kernel cmd to allocate ${nr_2Mb} 2Mb huge pages"
892 echo "GRUB_CMDLINE_LINUX=\"\$GRUB_CMDLINE_LINUX hugepagesz=2M hugepages=$nr_2Mb\"" >> /etc/default/grub
893 apply_grub=true
894 fi
895
896 if [[ ! $(cat /etc/default/grub | grep -E ^GRUB_CMDLINE_LINUX=".*hugepagesz=1G.*") ]] && [[ "${HUGE_PAGES_1G_SIZE}" -gt 0 ]]; then
897 echo "Configure GRUB kernel cmd to allocate ${HUGE_PAGES_1G_SIZE} 1G huge pages"
898 echo "GRUB_CMDLINE_LINUX=\"\$GRUB_CMDLINE_LINUX hugepagesz=1G hugepages=${HUGE_PAGES_1G_SIZE}\"" >> /etc/default/grub
899 apply_grub=true
900 fi
901
902 if [[ "${apply_grub}" == true ]]; then
903 update-grub
904 cloud-init clean -r
905 else
906 echo "GRUB already configured with huge pages"
907 fi
908}
909
Oleh Hryhorov43bbc8c2020-12-03 15:30:09 +0200910function disable_master_taint {
911
Oleh Hryhorovcd1a3bd2020-12-04 13:21:06 +0200912 function disable_master_taint_retry {
913 # Enable "Allow all authenticated users, including service accounts to schedule on all nodes, including UCP managers."
914 curl --retry 6 --retry-delay 5 -k -H "Authorization: Bearer $AUTHTOKEN" -k -X PUT https://${UCP_MASTER_HOST}/collectionGrants/authenticated/swarm/scheduler
915 }
916
917 retry 10 "Can't enable permission to schedule all pods on UCP master." disable_master_taint_retry
Oleh Hryhorov43bbc8c2020-12-03 15:30:09 +0200918
919}
920
Vasyl Saienko94843cb2021-02-26 16:43:34 +0200921function disable_ipv6 {
922 cat << EOF > /etc/sysctl.d/11-ipv6-disable.conf
923net.ipv6.conf.all.disable_ipv6 = 1
924net.ipv6.conf.default.disable_ipv6 = 1
925net.ipv6.conf.lo.disable_ipv6 = 1
926EOF
927 sysctl -p /etc/sysctl.d/11-ipv6-disable.conf
928}
929
Vasyl Saienko846c6342021-02-24 17:01:36 +0200930function setup_evpn {
931
932cat << EOF > /etc/netplan/60-evpn.yaml
933network:
934 version: 2
935 ethernets:
Vasyl Saienko5f953782021-02-25 10:30:32 +0200936EOF
937for vni in $(seq ${FRR_EVPN_TUNNELS_RANGE}); do
938cat << EOF >> /etc/netplan/60-evpn.yaml
939 vxlan$vni: {}
940EOF
941done
942cat << EOF >> /etc/netplan/60-evpn.yaml
Vasyl Saienko846c6342021-02-24 17:01:36 +0200943 bridges:
Vasyl Saienko5f953782021-02-25 10:30:32 +0200944EOF
945for vni in $(seq ${FRR_EVPN_TUNNELS_RANGE}); do
946cat << EOF >> /etc/netplan/60-evpn.yaml
947 br$vni:
Vasyl Saienko846c6342021-02-24 17:01:36 +0200948 dhcp4: false
Vasyl Saienkof18b4782021-03-03 14:15:40 +0200949 link-local: []
Vasyl Saienko846c6342021-02-24 17:01:36 +0200950 parameters:
951 stp: false
Vasyl Saienko5f953782021-02-25 10:30:32 +0200952 interfaces:
953 - vxlan$vni
Vasyl Saienko846c6342021-02-24 17:01:36 +0200954 addresses:
Vasyl Saienko5f953782021-02-25 10:30:32 +0200955 - 192.168.$vni.254/24
Vasyl Saienko846c6342021-02-24 17:01:36 +0200956EOF
Vasyl Saienko5f953782021-02-25 10:30:32 +0200957done
Vasyl Saienko846c6342021-02-24 17:01:36 +0200958
Vasyl Saienko5f953782021-02-25 10:30:32 +0200959for vni in $(seq ${FRR_EVPN_TUNNELS_RANGE}); do
960cat << EOF > /etc/systemd/system/evpn-vxlan-tunnel-${vni}.service
Vasyl Saienko846c6342021-02-24 17:01:36 +0200961[Unit]
Vasyl Saienko5f953782021-02-25 10:30:32 +0200962Description=EVPN tunnel ${vni}
Vasyl Saienko846c6342021-02-24 17:01:36 +0200963After=network.target
964
965[Service]
966Type=oneshot
967RemainAfterExit=true
Vasyl Saienkob3b8c4e2021-02-26 10:54:27 +0200968ExecStart=/sbin/ip link add vxlan${vni} type vxlan id ${vni} local ${TUNNEL_INTERFACE_IP} nolearning dstport ${FRR_EVPN_VXLAN_DST_PORT}
Vasyl Saienko5f953782021-02-25 10:30:32 +0200969ExecStart=/sbin/ip link set vxlan${vni} up
Vasyl Saienko846c6342021-02-24 17:01:36 +0200970
971[Install]
972WantedBy=multi-user.target
973EOF
Vasyl Saienko5f953782021-02-25 10:30:32 +0200974 systemctl enable evpn-vxlan-tunnel-${vni}.service
975 systemctl start evpn-vxlan-tunnel-${vni}.service
976done
Vasyl Saienko846c6342021-02-24 17:01:36 +0200977
978 netplan --debug apply
979 # NOTE(vsaienko): give some time to apply changes
980 sleep 15
Mykyta Karpinb3a7f292024-10-18 15:44:36 +0200981 # workaround for https://github.com/systemd/systemd/issues/13432
982 systemctl restart systemd-resolved
Vasyl Saienko846c6342021-02-24 17:01:36 +0200983}
984
Vasyl Saienko25bb75b2021-02-15 12:22:33 +0200985function install_frr {
986 function _install_frr_packages {
Vasyl Saienko25bb75b2021-02-15 12:22:33 +0200987 FRRVER="frr-stable"
Oleh Hryhorov6243c9f2023-09-21 09:53:09 +0000988 curl -s https://mirror.mirantis.com/.snapshots/frr-stable-latest/keys.asc | sudo apt-key add -
Oleh Hryhorov2ae1db82023-09-21 07:39:50 +0000989 add-apt-repository "deb [arch=amd64] https://mirror.mirantis.com/.snapshots/frr-stable-latest/ ${OS_CODENAME} ${FRRVER}"
Vasyl Saienko300bb0b2023-04-19 06:52:53 +0000990 install_pkgs frr frr-pythontools
Vasyl Saienko25bb75b2021-02-15 12:22:33 +0200991 }
992 retry 3 "Can't install frr packages" _install_frr_packages
993
994 sed -i 's/^bgpd=no/bgpd=yes/g' /etc/frr/daemons
995
996 cat << EOF > /etc/frr/frr.conf
997log syslog informational
998no ipv6 forwarding
999service integrated-vtysh-config
1000!
1001router bgp ${FRR_BGP_AS_NUMBER}
1002 bgp router-id ${FRR_ROUTER_ID}
1003 no bgp default ipv4-unicast
1004 bgp cluster-id ${FRR_ROUTER_ID}
1005 coalesce-time 1000
Vasyl Saienko846c6342021-02-24 17:01:36 +02001006 neighbor rr peer-group
Vasyl Saienkob3b8c4e2021-02-26 10:54:27 +02001007 neighbor rr capability extended-nexthop
1008 neighbor rr remote-as ${FRR_BGP_AS_NUMBER}
Vasyl Saienko846c6342021-02-24 17:01:36 +02001009 neighbor rr update-source ${FRR_BGP_SOURCE_ADDRESS}
1010EOF
1011
1012for neighbor_ip in $(echo $FRR_BGP_NEIGHBORS | tr "," "\n"); do
1013 cat << EOF >> /etc/frr/frr.conf
1014 neighbor $neighbor_ip peer-group rr
1015EOF
1016
1017done
1018 cat << EOF >> /etc/frr/frr.conf
Vasyl Saienko25bb75b2021-02-15 12:22:33 +02001019 !
1020 address-family ipv4 unicast
Vasyl Saienko846c6342021-02-24 17:01:36 +02001021 neighbor rr activate
Vasyl Saienko25bb75b2021-02-15 12:22:33 +02001022 exit-address-family
1023 !
1024 address-family ipv4 vpn
Vasyl Saienko846c6342021-02-24 17:01:36 +02001025 neighbor rr activate
Vasyl Saienko25bb75b2021-02-15 12:22:33 +02001026 exit-address-family
1027 !
1028 address-family l2vpn evpn
Vasyl Saienko846c6342021-02-24 17:01:36 +02001029 neighbor rr activate
Vasyl Saienko25bb75b2021-02-15 12:22:33 +02001030 advertise-all-vni
1031 advertise-default-gw
1032 exit-address-family
1033!
1034line vty
1035!
1036EOF
1037
1038 systemctl restart frr
Vasyl Saienko846c6342021-02-24 17:01:36 +02001039
Vasyl Saienko94843cb2021-02-26 16:43:34 +02001040 disable_ipv6
Vasyl Saienko846c6342021-02-24 17:01:36 +02001041 setup_evpn
Vasyl Saienko25bb75b2021-02-15 12:22:33 +02001042}
1043
Mykyta Karpinbac78b42021-12-07 11:25:03 +02001044function cron_disable_calico_offloading {
Denis Biletskiy49b91ea2022-06-09 11:41:39 +02001045 # The workaround is to disable offloading on vxlan interface
1046 if [ -f /etc/cron.d/disable_calico_offloading ]; then
1047 echo "Cronjob for disable callico offloading already exists"
1048 else
1049 script_path="/usr/sbin/calico_disable_offloading.sh"
1050 cat << 'EOF' >> $script_path
1051#!/usr/bin/env bash
1052if /sbin/ethtool --show-offload vxlan.calico | grep -E '^.x-checksumming:' | grep -q ': on'; then
1053 /sbin/ethtool --offload vxlan.calico rx off tx off > /dev/null
1054fi
Mykyta Karpinbac78b42021-12-07 11:25:03 +02001055EOF
Denis Biletskiy49b91ea2022-06-09 11:41:39 +02001056 chmod +x $script_path
1057 cat << EOF >> /etc/cron.d/disable_calico_offloading
1058* * * * * root $script_path 2>&1 | /usr/bin/logger -t calico_disable_offloading
1059EOF
1060 fi
Mykyta Karpinbac78b42021-12-07 11:25:03 +02001061}
1062
Vasyl Saienko114333f2022-05-24 09:34:51 +00001063function increase_iscsi_timeout {
1064 cat <<EOF > /etc/udev/rules.d/50-iscsi-timeout.rules
1065ACTION=="add", SUBSYSTEM=="scsi" , SYSFS{type}=="0|7|14",
1066RUN+="/bin/sh -c 'echo 120 > /sys/block/%k/device/timeout'"
1067EOF
1068 udevadm control --reload-rules && udevadm trigger
1069}