Add intel_iommu to kernel cmdline
[icn.git] / deploy / metal3 / scripts / 01_metal3.sh
1 #!/usr/bin/env bash
2 set -eu -o pipefail
3
4 LIBDIR="$(dirname "$(dirname "$(dirname "$PWD")")")"
5
6 eval "$(go env)"
7
8 source $LIBDIR/env/lib/common.sh
9
10 if [[ $EUID -ne 0 ]]; then
11     echo "This script must be run as root"
12     exit 1
13 fi
14
15 IMAGE_URL=http://172.22.0.1/images/${BM_IMAGE}
16 IMAGE_CHECKSUM=http://172.22.0.1/images/${BM_IMAGE}.md5sum
17
18 function clone_repos {
19     mkdir -p "${M3PATH}"
20     if [[ -d ${BMOPATH} && "${FORCE_REPO_UPDATE}" == "true" ]]; then
21       rm -rf "${BMOPATH}"
22     fi
23     if [ ! -d "${BMOPATH}" ] ; then
24         pushd "${M3PATH}"
25         git clone "${BMOREPO}"
26         popd
27     fi
28     pushd "${BMOPATH}"
29     git checkout "${BMOBRANCH}"
30     git pull -r || true
31     popd
32 }
33
34 function get_default_interface_ipaddress {
35     local _ip=$1
36     local _default_interface=$(awk '$2 == 00000000 { print $1 }' /proc/net/route)
37     local _ipv4address=$(ip addr show dev $_default_interface | awk '$1 == "inet" { sub("/.*", "", $2); print $2 }')
38     eval $_ip="'$_ipv4address'"
39 }
40
41 function create_ssh_key {
42     #ssh key for compute node to communicate back to bootstrap server
43     mkdir -p $BUILD_DIR/ssh_key
44     ssh-keygen -C "compute.icn.akraino.lfedge.org" -f $BUILD_DIR/ssh_key/id_rsa
45     cat $BUILD_DIR/ssh_key/id_rsa.pub >> $HOME/.ssh/authorized_keys
46 }
47
48 function set_compute_key {
49     _SSH_LOCAL_KEY=$(cat $BUILD_DIR/ssh_key/id_rsa)
50     cat << EOF
51 write_files:
52 - path: /opt/ssh_id_rsa
53     owner: root:root
54     permissions: '0600'
55     content: |
56     $_SSH_LOCAL_KEY
57 EOF
58 }
59
60 function deprovision_compute_node {
61     name="$1"
62     kubectl patch baremetalhost $name -n metal3 --type merge \
63     -p '{"spec":{"image":{"url":"","checksum":""}}}'
64 }
65
66 function set_compute_ssh_config {
67     get_default_interface_ipaddress default_addr
68     cat << EOF
69 - path: /root/.ssh/config
70     owner: root:root
71     permissions: '0600'
72     content: |
73     Host bootstrapmachine $default_addr
74     HostName $default_addr
75     IdentityFile /opt/ssh_id_rsa
76     User $USER
77 - path: /etc/apt/sources.list
78     owner: root:root
79     permissions: '0665'
80     content: |
81     deb [trusted=yes] ssh://$USER@$default_addr:$LOCAL_APT_REPO ./
82 EOF
83 }
84
85 # documentation for the values below may be found at
86 # https://cloudinit.readthedocs.io/en/latest/topics/modules.html
87 function create_userdata {
88     name="$1"
89     username="$2"
90     password="$3"
91     COMPUTE_NODE_FQDN="$name.akraino.icn.org"
92
93     # validate that the user isn't expecting the deprecated
94     # COMPUTE_NODE_PASSWORD to be used
95     if [ "$password" != "${COMPUTE_NODE_PASSWORD:-$password}" ]; then
96         cat <<EOF
97 COMPUTE_NODE_PASSWORD "$COMPUTE_NODE_PASSWORD" not equal to nodes.json $name password "$password".
98 Unset COMPUTE_NODE_PASSWORD and retry.
99 EOF
100         exit 1
101     fi
102
103     printf "#cloud-config\n" >  $name-userdata.yaml
104     if [ -n "$password" ]; then
105         if [ -n "$username" ]; then
106             passwd=$(mkpasswd --method=SHA-512 --rounds 4096 "$password")
107             printf "users:" >>  $name-userdata.yaml
108             printf "\n  - name: ""%s" "$username" >>  $name-userdata.yaml
109             printf "\n    lock_passwd: False" >>  $name-userdata.yaml # necessary to allow password login
110             printf "\n    passwd: ""%s" "$passwd" >>  $name-userdata.yaml
111             printf "\n    sudo: \"ALL=(ALL) NOPASSWD:ALL\"" >>  $name-userdata.yaml
112         else
113             printf "password: ""%s" "$password" >>  $name-userdata.yaml
114         fi
115         printf "\nchpasswd: {expire: False}\n" >>  $name-userdata.yaml
116         printf "ssh_pwauth: True\n" >>  $name-userdata.yaml
117     fi
118
119     if [ -n "$COMPUTE_NODE_FQDN" ]; then
120         printf "fqdn: ""%s" "$COMPUTE_NODE_FQDN" >>  $name-userdata.yaml
121         printf "\n" >>  $name-userdata.yaml
122     fi
123     printf "disable_root: false\n" >>  $name-userdata.yaml
124     printf "ssh_authorized_keys:\n  - " >>  $name-userdata.yaml
125
126     if [ ! -f $HOME/.ssh/id_rsa.pub ]; then
127         yes y | ssh-keygen -t rsa -N "" -f $HOME/.ssh/id_rsa
128     fi
129
130     cat $HOME/.ssh/id_rsa.pub >>  $name-userdata.yaml
131     cloud_init_scripts >> $name-userdata.yaml
132     printf "\n" >>  $name-userdata.yaml
133 }
134
135 function launch_baremetal_operator {
136     docker pull $IRONIC_BAREMETAL_IMAGE
137     kubectl apply -f bmo/namespace/namespace.yaml
138     kubectl apply -f bmo/rbac/service_account.yaml -n metal3
139     kubectl apply -f bmo/rbac/role.yaml -n metal3
140     kubectl apply -f bmo/rbac/role_binding.yaml
141     kubectl apply -f bmo/crds/metal3.io_baremetalhosts_crd.yaml
142     kubectl apply -f bmo/operator/no_ironic/operator.yaml -n metal3
143 }
144
145 function remove_baremetal_operator {
146     kubectl delete -f bmo/operator/no_ironic/operator.yaml -n metal3
147     kubectl delete -f bmo/crds/metal3.io_baremetalhosts_crd.yaml
148     kubectl delete -f bmo/rbac/role_binding.yaml
149     kubectl delete -f bmo/rbac/role.yaml -n metal3
150     kubectl delete -f bmo/rbac/service_account.yaml -n metal3
151     kubectl delete -f bmo/namespace/namespace.yaml
152 }
153
154 function cloud_init_scripts {
155     # The "intel_iommu=on iommu=pt" kernel command line is necessary
156     # for QAT support.
157     cat << 'EOF'
158 write_files:
159 - path: /var/lib/cloud/scripts/per-instance/set_kernel_cmdline.sh
160   owner: root:root
161   permissions: '0777'
162   content: |
163     #!/usr/bin/env bash
164     set -eux -o pipefail
165     grub_file=${1:-"/etc/default/grub"}
166     kernel_parameters="intel_iommu=on iommu=pt"
167     sed -i~ "/^GRUB_CMDLINE_LINUX=/{h;s/\(=\".*\)\"/\1 ${kernel_parameters}\"/};\${x;/^$/{s//GRUB_CMDLINE_LINUX=\"${kernel_parameters}\"/;H};x}" "$grub_file"
168     update-grub
169     reboot
170 EOF
171     cat << 'EOF'
172 - path: /var/lib/cloud/scripts/per-boot/run_dhclient.sh
173   owner: root:root
174   permissions: '0777'
175   content: |
176     #!/usr/bin/env bash
177     set -xe
178     for intf in /sys/class/net/*; do
179         sudo ifconfig `basename $intf` up
180         sudo dhclient -nw `basename $intf`
181     done
182 EOF
183     cat << EOF
184 - path: /var/lib/cloud/scripts/per-boot/set_provider_network.sh
185   owner: root:root
186   permissions: '0777'
187   content: |
188     #!/usr/bin/env bash
189     set -xe
190     route add default gw $PROVIDER_NETWORK_GATEWAY
191     sed -i -e 's/^#DNS=.*/DNS=$PROVIDER_NETWORK_DNS/g' /etc/systemd/resolved.conf
192     systemctl daemon-reload
193     systemctl restart systemd-resolved
194 EOF
195 }
196
197 function apply_userdata_credential {
198     name="$1"
199     cat <<EOF > ./$name-user-data-credential.yaml
200 apiVersion: v1
201 data:
202   userData: $(base64 -w 0 $name-userdata.yaml)
203 kind: Secret
204 metadata:
205   name: $name-user-data
206   namespace: metal3
207 type: Opaque
208 EOF
209     kubectl apply -n metal3 -f $name-user-data-credential.yaml
210 }
211
212 function make_bm_hosts {
213     while IFS=',' read -r name ipmi_username ipmi_password ipmi_address os_username os_password os_image_name; do
214         create_userdata $name $os_username $os_password
215         apply_userdata_credential $name
216
217         go run $GOPATH/src/github.com/metal3-io/baremetal-operator/cmd/make-bm-worker/main.go \
218            -address "ipmi://$ipmi_address" \
219            -password "$ipmi_password" \
220            -user "$ipmi_username" \
221            "$name" > $name-bm-node.yaml
222
223         printf "  image:" >> $name-bm-node.yaml
224         printf "\n    url: ""%s" "$IMAGE_URL" >> $name-bm-node.yaml
225         printf "\n    checksum: ""%s" "$IMAGE_CHECKSUM" >> $name-bm-node.yaml
226         printf "\n  userData:" >> $name-bm-node.yaml
227         printf "\n    name: ""%s" "$name""-user-data" >> $name-bm-node.yaml
228         printf "\n    namespace: metal3" >> $name-bm-node.yaml
229         printf "\n  rootDeviceHints:" >> $name-bm-node.yaml
230         printf "\n    minSizeGigabytes: 48\n" >> $name-bm-node.yaml
231         kubectl apply -f $name-bm-node.yaml -n metal3
232     done
233 }
234
235 function configure_nodes {
236     if [ ! -d $IRONIC_DATA_DIR ]; then
237         mkdir -p $IRONIC_DATA_DIR
238     fi
239
240     #make sure nodes.json file in /opt/ironic/ are configured
241     if [ ! -f $IRONIC_DATA_DIR/nodes.json ]; then
242         cp $PWD/nodes.json.sample $IRONIC_DATA_DIR/nodes.json
243     fi
244 }
245
246 function remove_bm_hosts {
247     while IFS=',' read -r name ipmi_username ipmi_password ipmi_address os_username os_password os_image_name; do
248         deprovision_compute_node $name
249     done
250 }
251
252 function cleanup {
253     while IFS=',' read -r name ipmi_username ipmi_password ipmi_address os_username os_password os_image_name; do
254         kubectl delete bmh $name -n metal3
255         kubectl delete secrets $name-bmc-secret -n metal3
256         kubectl delete secrets $name-user-data -n metal3
257         if [ -f $name-bm-node.yaml ]; then
258             rm -rf $name-bm-node.yaml
259         fi
260
261         if [ -f $name-user-data-credential.yaml ]; then
262             rm -rf $name-user-data-credential.yaml
263         fi
264
265         if [ -f $name-userdata.yaml ]; then
266             rm -rf $name-userdata.yaml
267         fi
268     done
269 }
270
271 function clean_all {
272     list_nodes | cleanup
273     if [ -f $IRONIC_DATA_DIR/nodes.json ]; then
274         rm -rf $IRONIC_DATA_DIR/nodes.json
275     fi
276 }
277
278 function apply_bm_hosts {
279     list_nodes | make_bm_hosts
280 }
281
282 function deprovision_all_hosts {
283     list_nodes | remove_bm_hosts
284 }
285
286 if [ "$1" == "launch" ]; then
287     clone_repos
288     launch_baremetal_operator
289     exit 0
290 fi
291
292 if [ "$1" == "deprovision" ]; then
293     configure_nodes
294     deprovision_all_hosts
295     exit 0
296 fi
297
298 if [ "$1" == "provision" ]; then
299     configure_nodes
300     apply_bm_hosts
301     exit 0
302 fi
303
304 if [ "$1" == "clean" ]; then
305     configure_nodes
306     clean_all
307     exit 0
308 fi
309
310 if [ "$1" == "remove" ]; then
311     remove_baremetal_operator
312     exit 0
313 fi
314
315 echo "Usage: metal3.sh"
316 echo "launch      - Launch the metal3 operator"
317 echo "provision   - provision baremetal node as specified in common.sh"
318 echo "deprovision - deprovision baremetal node as specified in common.sh"
319 echo "clean       - clean all the bmh resources"
320 echo "remove      - remove baremetal operator"
321 exit 1
322
323 #Following code is tested for the offline mode
324 #Will be intergrated for the offline mode for ICNi v.0.1.0 beta
325 #create_ssh_key
326 #create_userdata
327 #set_compute_key
328 #set_compute_ssh_config