blob: 65b8d6f3328d3a14445456b3c29bcadca631dff9 [file] [log] [blame]
Humera Kouser9c2bd9f2020-09-25 02:41:29 -04001#!/bin/bash
2
3#Copyright 2020-present Open Networking Foundation
4#
5#Licensed under the Apache License, Version 2.0 (the "License");
6#you may not use this file except in compliance with the License.
7#You may obtain a copy of the License at
8#
9#http://www.apache.org/licenses/LICENSE-2.0
10#
11#Unless required by applicable law or agreed to in writing, software
12#distributed under the License is distributed on an "AS IS" BASIS,
13#WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14#See the License for the specific language governing permissions and
15#limitations under the License.
16#
17
18### BEGIN INIT INFO
19# Description:
20# This script swaps the block id labels if expected services
21# like system services onlp, sshd, bal_core_dist, dhclient, physical
22# ,vlan interfaces status and olt services dev_mgmt_daemon, openolt services
23# are UP and RUNNING. If all services are not UP and RUNNING, then roll back
24# to previous state.
25### END INIT INFO
26
27# Labels names
28ONL_ACTIVE_BOOT="ONL-ACTIVE-BOOT"
29ONL_ACTIVE_DATA="ONL-ACTIVE-DATA"
30ONL_STANDBY_BOOT="ONL-STANDBY-BOOT"
31ONL_STANDBY_DATA="ONL-STANDBY-DATA"
32
33RUNNING_ROOT_DEV=
34RUNNING_ROOT_LABEL=
35RUNNING_BOOT_LABEL=
36RUNNING_BOOT_DEV=
37
38STANDBY_BOOT_DEV=
39STANDBY_BOOT_LABEL=
40STANDBY_ROOT_LABEL=
41STANDBY_ROOT_DEV=
42
43# Configuration file about upgrade
44ONL_ROOT="/mnt/onl"
45IMAGES="${ONL_ROOT}/images"
46UPGRADE_PATH="${IMAGES}/upgrade"
47ACTIVE_GRUB_ENV_FILE="${ONL_ROOT}/active_boot/grub/grubenv"
48STANDBY_GRUB_ENV_FILE="${ONL_ROOT}/standby_boot/grub/grubenv"
49GRUB_STAND_PATH="${ONL_ROOT}/standby_boot/grub"
50GRUB_ACTIVE_PATH="${ONL_ROOT}/active_boot/grub"
51STANDBY_GRUB_ACTIVE_PATH="${ONL_ROOT}/standby_boot/grub"
52GRUB_CFG="grub.cfg"
53GRUB_CFG_BACKUP="grub.cfg.back"
54GRUB_CONFIG_FILE="${ONL_ROOT}/config/grub.cfg"
55# Time interval in seconds
56TIME_INTERVAL=5
57
58#in band interface IP
59ASFVOLT16_ETH2_VLAN_INF_IP=
60
61ASGVOLT64_ETH1_VLAN_INF_IP=
62
63ETH3_VLAN_IP=
64
65BRCM_DIR='/broadcom'
66
67# vlan config file
68VLAN_CONFIG_FILE="${BRCM_DIR}/inband.config"
69
70# vlan id for asfvolt16
71ASFVOLT16_VLAN_ID_ETH2=
72
73#vlan id for asgvolt64
74ASGVOLT64_VLAN_ID_ETH1=
75
76OLT_MODEL=$(cat /sys/devices/virtual/dmi/id/board_name)
77ASXvOLT16="ASXvOLT16"
78ASGvOLT64="ASGvOLT64"
79
80VLAN_ID_1=
81VLAN_ID_2=
82
83# interfaces
84ETH1=eth1
85ETH2=eth2
86
87#------------------------------------------------------------------------------
88# Function Name: does_logger_exist
89# Description:
90# This function check if logger exist and executable.
91#
92# Globals:
93# None
94#
95# Arguments:
96# None
97#
98# Returns:
99# returns 0 if exist and executable else 1
100#------------------------------------------------------------------------------
101does_logger_exist()
102{
103cmd=/usr/bin/logger
104if [ -x ${cmd} ]; then
105 return 0
106 else
107 return 1
108 fi
109}
110
111#------------------------------------------------------------------------------
112# Function Name: info_message
113# Description:
114# This function print the info message information to the console
115#
116# Globals:
117# None
118#
119# Arguments:
120# string message
121#
122# Returns:
123# None
124#------------------------------------------------------------------------------
125info_message()
126{
127 echo "INFO: $1"
128 logger -p user.info "$1"
129}
130
131#------------------------------------------------------------------------------
132# Function Name: error_message
133# Description:
134# This function print the error message information to the console
135#
136# Globals:
137# None
138#
139# Arguments:
140# string message
141#
142# Returns:
143# returns 1
144#------------------------------------------------------------------------------
145error_message()
146{
147 echo "ERROR: $1"
148 logger -p user.err "$1"
149 return 1
150}
151
152
153#------------------------------------------------------------------------------
154# Function Name: error_message
155# Description:
156# This function print the error message information to the console
157#
158# Globals:
159# None
160#
161# Arguments:
162# string message
163#
164# Returns:
165# returns 1
166#------------------------------------------------------------------------------
167display_services()
168{
169 echo "Sanity result:
170 Service Name - Status
171 --------------------------------------
172 1. eth2 - UP & RUNNING
173 2. eth1 - UP & RUNNING
174 3. bal_service - RUNNING
175 4. system services - RUNNING"
176
177 if [ "${OLT_MODEL}" = ${ASXvOLT16} ];then
178 echo "
179 5. eth2.${ASFVOLT16_VLAN_ID_ETH2} - UP & RUNNING
180 6. eth2.${ASFVOLT16_VLAN_ID_ETH2} IP - ${ASFVOLT16_ETH2_VLAN_INF_IP}"
181 elif [ "${OLT_MODEL}" = ${ASGvOLT64} ];then
182 echo"
183 5. eth1.${ASGVOLT64_VLAN_ID_ETH1} - UP & RUNNING
184 6. eth1.${ASGVOLT64_VLAN_ID_ETH1} IP - ${ASGVOLT64_ETH1_VLAN_INF_IP}"
185 fi
186}
187
188#------------------------------------------------------------------------------
189# Function Name: is_interface_up_running
190# Description:
191# This function validate the interface whether it is UP and RUNNING or DOWN.
192#
193# Globals:
194# None
195#
196# Arguments:
197# interface name
198#
199# Returns:
200# returns 0 if interface is up and running and returns 1 if interface is down
201#------------------------------------------------------------------------------
202is_interface_up_running()
203{
204 info_message "Validating interface - $1"
205 interface_name=$1
206 ifconfig ${interface_name} | grep -q "UP BROADCAST RUNNING MULTICAST"
207 if [ $? -eq 0 ]; then
208 info_message "${interface_name} UP & RUNNING"
209 echo "---------------------------------------------------------------------"
210 return 0
211 else
212 error_message "${interface_name} is DOWN"
213 echo "---------------------------------------------------------------------"
214 return 1
215 fi
216}
217
218#------------------------------------------------------------------------------
219# Function Name: validate_ip
220# Description:
221# This function get the ip from the interface and validates it.
222#
223# Globals:
224# OLT_MODEL, ASXvOLT16, ASGvOLT64, ASFVOLT16_VLAN_ID_ETH2,
225# ASFVOLT16_VLAN_ID_ETH2, ASGVOLT64_VLAN_ID_ETH1
226#
227#
228# Arguments:
229# interface name
230#
231# Returns:
232# ip address
233#------------------------------------------------------------------------------
234validate_ip()
235{
236 interface_name=$1
237 ip_address=$(ifconfig $interface_name | grep "inet addr" | awk 'BEGIN {FS =":"} {print $2}' | awk '{print $1}')
238
239 if [ "${OLT_MODEL}" = ${ASXvOLT16} ];then
240 if [ ! -z $ip_address ] && [ $interface_name = ${ETH2}.${ASFVOLT16_VLAN_ID_ETH2} ]; then
241 ASFVOLT16_ETH2_VLAN_INF_IP=$ip_address
242 fi
243 elif [ "${OLT_MODEL}" = ${ASGvOLT64} ];then
244 if [ ! -z $ip_address ] && [ $interface_name = ${ETH1}.${ASGVOLT64_VLAN_ID_ETH1} ]; then
245 ASGVOLT64_ETH1_VLAN_INF_IP=$ip_address
246 fi
247 fi
248
249 info_message "Validating $1 ip address - $ip_address"
250 if expr "$ip_address" : '[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*$' >/dev/null; then
251 for i in 1 2 3 4; do
252 if [ $(echo "$ip_address" | cut -d. -f$i) -gt 255 ]; then
253 return 1
254 fi
255 done
256 return 0
257 else
258 return 1
259 fi
260}
261
262#------------------------------------------------------------------------------
263# Function Name : validate_interfaces
264# Description:
265# This function validate interfaces ${ETH2}.${ASFVOLT16_VLAN_ID_ETH2} and
266# or ${ETH1}.${ASGVOLT64_VLAN_ID_ETH1} and based on OLT model.
267# Basically it validates if these interfaces are UP and RUNNING or not
268#
269# Globals:
270# ASFVOLT16_VLAN_ID_ETH2, ASGVOLT64_VLAN_ID_ETH1,
271# ETH1, ETH2, ASFVOLT16_BAL_PACKAGE
272#
273#
274# Arguments:
275# None
276#
277# Returns:
278# return 1 if interface are down else returns 0
279#------------------------------------------------------------------------------
280validate_interfaces()
281{
282 # Validating interfaces whether they are UP and RUNNING or not
283 if [ "${OLT_MODEL}" = ${ASXvOLT16} ];then
284 is_interface_up_running ${ETH2}.${ASFVOLT16_VLAN_ID_ETH2}
285 if [ $? -eq 0 ]; then
286 return 0
287 else
288 return 1
289 fi
290 elif [ "${OLT_MODEL}" = ${ASGvOLT64} ];then
291 is_interface_up_running ${ETH1}.${ASGVOLT64_VLAN_ID_ETH1}
292 if [ $? -eq 0 ]; then
293 return 0
294 else
295 return 1
296 fi
297 fi
298}
299
300#------------------------------------------------------------------------------
301# Function Name: validate_vlan_intf_ip
302# Description:
303# This function validate the inband vlan interfaces ip, checks if DHCP server has
304# assigned proper IP addresses.
305#
306#
307# Globals:
308# ASFVOLT16_VLAN_ID_ETH2, ASGVOLT64_VLAN_ID_ETH1,
309# ETH1, ETH2, OLT_MODEL, ASXvOLT16 and ASGvOLT64
310#
311#
312# Arguments:
313# None
314#
315# Returns:
316# returns 0 if ip adresses are valid else return 1
317#------------------------------------------------------------------------------
318validate_vlan_intf_ip()
319{
320 # Validating vlan interfaces and their IP
321 if [ "${OLT_MODEL}" = ${ASXvOLT16} ];then
322 validate_ip ${ETH2}.${ASFVOLT16_VLAN_ID_ETH2}
323 if [ $? -eq 0 ]; then
324 return 0
325 else
326 return 1
327 fi
328 elif [ "${OLT_MODEL}" = ${ASGvOLT64} ];then
329 validate_ip ${ETH1}.${ASGVOLT64_VLAN_ID_ETH1}
330 if [ $? -eq 0 ]; then
331 return 0
332 else
333 return 1
334 fi
335
336 fi
337 return 0
338}
339
340#------------------------------------------------------------------------------
341# Function Name: validate_system_services
342# Description:
343# This function checks if the below services/processes are running or not.
344# 1. dev_mgmt_daemon
345# 2. openolt
346# 3. onlp
347# 4. sshd
348# 5. dhclient
349# Globals:
350# None
351#
352# Arguments:
353# None
354#
355# Returns:
356# returns 0
357#------------------------------------------------------------------------------
358validate_system_services()
359{
360 echo "---------------------------------------------------------------------"
361 echo "Validating Services"
362 echo "---------------------------------------------------------------------"
363
364 dhclient_interface dhclient_val1 dhclient_val2
365 for service_name in dev_mgmt_daemon openolt onlp sshd ${dhclient_val1} ${dhclient_val2}
366 do
367 echo "---------------------------------------------------------------------"
368 ps -ef | grep -v grep | grep ${service_name}
369 if [ $? -eq 0 ]; then
370 info_message "${service_name} service is running"
371 else
372 error_message "${service_name} is not running"
373 return 1
374 fi
375 echo "---------------------------------------------------------------------"
376 done
377 return 0
378}
379
380#------------------------------------------------------------------------------
381# Function Name: dhclient_interface
382# Description:
383# This function sets values to which tagged interface dhclient service need
384# to be tested based on the OLT model package.
385#
386# Globals:
387# OLT_MODEL, ASFVOLT16_VLAN_ID_ETH2,
388# ASGVOLT64_VLAN_ID_ETH1, ETH1, ETH2,
389# ASGvOLT64, ASXvOLT16.
390#
391# Arguments:
392# None
393#
394# Returns:
395# None
396#------------------------------------------------------------------------------
397dhclient_interface()
398{
399 local value1=$1
400 local value2=$2
401 if [ "${OLT_MODEL}" = ${ASXvOLT16} ];then
402 dhclient1=dhclient.${ETH2}.${ASFVOLT16_VLAN_ID_ETH2}
403 eval $value1="'$dhclient1'"
404 elif [ "${OLT_MODEL}" = ${ASGvOLT64} ];then
405 dhclient3=dhclient.${ETH1}.${ASGVOLT64_VLAN_ID_ETH1}
406 eval $value1="'$dhclient3'"
407 fi
408
409}
410
411#------------------------------------------------------------------------------
412# Function Name: check_services
413# Description:
414# This function check the expected services, like system services for ex:
415# onlp, sshd, bal_core_dist, dhclient and physical and vlan interfaces status.
416#
417# Globals:
418# VLAN_CONFIG_FILE, ASFVOLT16_VLAN_ID_ETH2, ASGVOLT64_VLAN_ID_ETH1
419# TIME_INTERVAL, OLT_MODEL, ASXvOLT16, ASGvOLT64
420#
421# Arguments:
422# None
423#
424# Returns:
425# None
426#------------------------------------------------------------------------------
427check_services()
428{
429 # Let bal services are up and running
430 waiting_time=0
431 while true; do
432 sleep ${TIME_INTERVAL}
433 if [ $waiting_time -eq 70 ]; then
434 break
435 fi
436 waiting_time=$((waiting_time+${TIME_INTERVAL}))
437 done
438
439 if [ -f ${VLAN_CONFIG_FILE} ]; then
440 ASFVOLT16_VLAN_ID_ETH2=$(awk '/asfvolt16_vlan_id_eth2/{print $0}' \
441 ${VLAN_CONFIG_FILE} | awk -F "=" '{print $2}')
442 ASGVOLT64_VLAN_ID_ETH1=$(awk '/asgvolt64_vlan_id_eth1/{print $0}' \
443 ${VLAN_CONFIG_FILE} | awk -F "=" '{print $2}')
444 if [ "${OLT_MODEL}" = ${ASXvOLT16} ];then
445 if [ ${ASFVOLT16_VLAN_ID_ETH2} -gt 4094 ] || [ ${ASFVOLT16_VLAN_ID_ETH2} -lt 1 ]; then
446 error_message "vlan ids not in range"
447 exit 1
448 fi
449 elif [ "${OLT_MODEL}" = ${ASGvOLT64} ];then
450 if [ ${ASGVOLT64_VLAN_ID_ETH1} -gt 4094 ] || [ ${ASGVOLT64_VLAN_ID_ETH1} -lt 1 ]; then
451 error_message "vlan ids not in range"
452 exit 1
453 fi
454 fi
455 else
456 error_message "${VLAN_CONFIG_FILE} does not exist"
457 exit 1
458 fi
459
460 info_message "Sanity check"
461 for func_name in validate_interfaces validate_vlan_intf_ip validate_system_services
462 do
463 counter=0
464 while true; do
465 sleep ${TIME_INTERVAL}
466 $func_name
467 if [ $? -eq 0 ]; then
468 break
469 fi
470 if [ $counter -eq 100 ]; then
471 error_message "Time out ,all services are not up"
472 return 1
473 fi
474 counter=$((counter+${TIME_INTERVAL}))
475 done
476 done
477 display_services
478 return 0
479}
480
481#------------------------------------------------------------------------------
482# Function Name: change_labels
483# Description:
484# This function does the following functions
485# 1) After the upgrade procedure installs the image in stand_by_partition,
486# This function checks for the system services after upgrade,
487# if all ther services are up and running then following steps takes place
488# a) It changes the device labels from Active to Standby and
489# Standby to Active.
490# b) It sets the grub menu entry from 1 to 0 which is default
491# grub entry. After reboot in the grub menu entry will
492# have one entry.
493# c) It will swap the upgrade image to Image directory and vice
494# versa.
495# 2) If system services fails to start after image upgrade,
496# roll-back to previous state of OLT i.e
497# boot the OLT back from the active partition.
498#
499# Globals:
500# RUNNING_BOOT_DEV, STANDBY_BOOT_LABEL, RUNNING_ROOT_DEV, STANDBY_ROOT_LABEL
501# STANDBY_BOOT_DEV, RUNNING_BOOT_LABEL, STANDBY_ROOT_DEV, RUNNING_ROOT_LABEL
502#
503# Arguments:
504# None
505#
506# Returns:
507# None
508#------------------------------------------------------------------------------
509change_labels()
510{
511 grep "data-standby" /proc/cmdline > /dev/null
512 if [ $? -eq 0 ]; then
513 check_services
514 # if the OLT system services fail to start, fall back to previous state
515 if [ $? -eq 1 ]; then
516 reboot -f
517 fi
518 # Change running boot device label to standby boot label
519 e2label $RUNNING_BOOT_DEV $STANDBY_BOOT_LABEL
520 # Change running root device label to standby root label
521 e2label $RUNNING_ROOT_DEV $STANDBY_ROOT_LABEL
522 # Change standby boot device label to active boot label
523 e2label $STANDBY_BOOT_DEV $RUNNING_BOOT_LABEL
524 # Change standby root device label to active root label
525 e2label $STANDBY_ROOT_DEV $RUNNING_ROOT_LABEL
526 partprobe
527
528 if [ -f ${ACTIVE_GRUB_ENV_FILE} ]; then
529 next_entry=$(grub-editenv ${ACTIVE_GRUB_ENV_FILE} list | sed -n 's/^next_entry=//p')
530 saved_entry=$(grub-editenv ${ACTIVE_GRUB_ENV_FILE} list | sed -n 's/^saved_entry=//p')
531 if [ ${next_entry} -eq 1 ] || [ ${saved_entry} -eq 1 ]; then
532 grub-editenv ${ACTIVE_GRUB_ENV_FILE} set next_entry=0
533 grub-editenv ${ACTIVE_GRUB_ENV_FILE} set saved_entry=0
534 fi
535 fi
536
537 if [ -f ${STANDBY_GRUB_ENV_FILE} ]; then
538 next_entry=$(grub-editenv ${STANDBY_GRUB_ENV_FILE} list | sed -n 's/^next_entry=//p')
539 saved_entry=$(grub-editenv ${STANDBY_GRUB_ENV_FILE} list | sed -n 's/^saved_entry=//p')
540 if [ ${next_entry} -eq 1 ] || [ ${saved_entry} -eq 1 ]; then
541 grub-editenv ${STANDBY_GRUB_ENV_FILE} set next_entry=0
542 grub-editenv ${STANDBY_GRUB_ENV_FILE} set saved_entry=0
543 fi
544 fi
545
546 if [ -f ${GRUB_CONFIG_FILE} ]; then
547 if [ -f "${GRUB_ACTIVE_PATH}/${GRUB_CFG}" ]; then
548 cp ${GRUB_CONFIG_FILE} ${GRUB_ACTIVE_PATH}/${GRUB_CFG}
549 fi
550 if [ -f "${GRUB_STAND_PATH}/${GRUB_CFG}" ]; then
551 cp ${GRUB_CONFIG_FILE} ${GRUB_STAND_PATH}/${GRUB_CFG}
552 fi
553 fi
554
555 if [ -d ${UPGRADE_PATH} ]; then
556 cd ${UPGRADE_PATH}
557 upgrade_image=$(ls *.swi)
558 mv $upgrade_image "$upgrade_image.bak"
559 cd ${IMAGES}
560 active_image=$(ls *.swi)
561 mv $active_image "$active_image.bak"
562 mv ${UPGRADE_PATH}/"$upgrade_image.bak" ${IMAGES}/$upgrade_image
563 mv ${IMAGES}/"$active_image.bak" ${UPGRADE_PATH}/$active_image
564 fi
565 fi
566
567}
568
569#------------------------------------------------------------------------------
570# Function Name: get_labels
571# Description:
572# This function get the labels of the devices and also find out the
573# Running and Standby devices.
574#
575# Globals:
576# RUNNING_ROOT_DEV, RUNNING_ROOT_LABEL, RUNNING_BOOT_LABEL, STANDBY_BOOT_LABEL
577# RUNNING_ROOT_LABEL, STANDBY_ROOT_LABEL
578#
579# Arguments:
580# None
581#
582# Returns:
583# None
584#------------------------------------------------------------------------------
585get_labels()
586{
587 RUNNING_ROOT_DEV=$(cat /proc/mounts | grep " \/ ext4" | awk '{print $1}')
588 RUNNING_ROOT_LABEL=$(blkid | grep ${RUNNING_ROOT_DEV} | awk '{print $2}' | cut -c 7- | tr -d \")
589 if [ ${RUNNING_ROOT_LABEL} = "ONL-ACTIVE-DATA" ]; then
590 RUNNING_BOOT_LABEL=${ONL_ACTIVE_BOOT}
591 STANDBY_BOOT_LABEL=${ONL_STANDBY_BOOT}
592 RUNNING_ROOT_LABEL=${ONL_ACTIVE_DATA}
593 STANDBY_ROOT_LABEL=${ONL_STANDBY_DATA}
594 else
595 RUNNING_BOOT_LABEL=${ONL_STANDBY_BOOT}
596 RUNNING_ROOT_LABEL=${ONL_STANDBY_DATA}
597 STANDBY_BOOT_LABEL=${ONL_ACTIVE_BOOT}
598 STANDBY_ROOT_LABEL=${ONL_ACTIVE_DATA}
599 fi
600 STANDBY_ROOT_DEV=$(blkid | grep " LABEL=\"$STANDBY_ROOT_LABEL\"" | awk '{print $1}' | tr -d :)
601 RUNNING_BOOT_DEV=$(blkid | grep " LABEL=\"$RUNNING_BOOT_LABEL\"" | awk '{print $1}' | tr -d :)
602 STANDBY_BOOT_DEV=$(blkid | grep " LABEL=\"$STANDBY_BOOT_LABEL\"" | awk '{print $1}' | tr -d :)
603}
604
605# Starts from here
606does_logger_exist
607if [ $? -eq 0 ]; then
608 get_labels
609 change_labels
610else
611 error_message "logger does not exist"
612 exit 1
613fi