blob: 69d986397c769bdfe9fc96a4eba4dbc141b1e8d0 [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### BEGIN INIT INFO
18# Description:
19# Upgrade the NOS from Active to Standby partition,
20# This script checks if installed ONL on OLT has active and standby partions
21# if yes, it extracts the ONL package and keep the
22# required files to the Standby partition and start the NOS from Standby partition.
23#
24### END INIT INFO
25
26
27WORKING_DIR=$(pwd)
28# Name of the script
29SCRIPT_NAME="$(basename ${0})"
30UPGRADE=
31ONL_FILE=${1}
32ONL_ROOT="/mnt/onl"
33ACTIVE_BOOTDIR="${ONL_ROOT}/active_boot/"
34STANDBY_BOOTDIR="${ONL_ROOT}/standby_boot/"
35
36# Partition labels
37ONL_ACTIVE_BOOT="ONL-ACTIVE-BOOT"
38ONL_ACTIVE_DATA="ONL-ACTIVE-DATA"
39ONL_STANDBY_BOOT="ONL-STANDBY-BOOT"
40ONL_STANDBY_DATA="ONL-STANDBY-DATA"
41ONL_CONFIG="ONL-CONFIG"
42ONL_IMAGES="ONL-IMAGES"
43
44# Mount paths
45CONFIG="${ONL_ROOT}/config"
46IMAGES="${ONL_ROOT}/images"
47ACTIVE_DATA="${ONL_ROOT}/active_data"
48ACTIVE_BOOT="${ONL_ROOT}/active_boot"
49STANDBY_DATA="${ONL_ROOT}/standby_data"
50STANDBY_BOOT="${ONL_ROOT}/standby_boot"
51GRUB="grub"
52ONL_SOURCE="onl_source"
53
54ACTIVE_DATA_DEV_NAME=
55ACTIVE_DATA_MNT_PATH=
56ACTIVE_BOOT_DEV_NAME=
57ACTIVE_BOOT_MNT_PATH=
58STANDBY_DATA_DEV_NAME=
59STANDBY_DATA_MNT_PATH=
60STANDBY_BOOT_DEV_NAME=
61STANDBY_BOOT_MNT_PATH=
62
63# Configuration file about upgrade
64IMAGES="${ONL_ROOT}/images"
65ACTIVE_GRUB_ENV_FILE="${ONL_ROOT}/active_boot/grub/grubenv"
66STANDBY_GRUB_ENV_FILE="${ONL_ROOT}/active_boot/grub/grubenv"
67GRUB_CONFIG_FILE="${ONL_ROOT}/config/grub.cfg"
68GRUB_CONFIG_FILE_CHANGED="${ONL_ROOT}/config/grub.cfg.changed"
69ASF_VOLT16_VOLTHA_BAL=$(ls /*asfvolt16*.tar.gz 2>/dev/null)
70BROADCOM="/broadcom"
71OPT="/opt/bcm68620"
72DEV_MGMT_DAEMON="dev_mgmt_daemon -d -pcie"
73OPENOLT="openolt"
74#------------------------------------------------------------------------------
75# Function : usage
76#
77# Description:
78# This function says how to use the script
79#
80# Globals:
81# SCRIPT_NAME
82#
83# Arguments:
84# None
85#
86# Returns:
87# None
88#------------------------------------------------------------------------------
89
90usage()
91{
92 echo "${SCRIPT_NAME}:usage:
93 ${SCRIPT_NAME} <ONL_IMAGE_WITH_ABSOLUTE_PATH>
94 "
95 exit 1
96}
97
98#------------------------------------------------------------------------------
99# Function Name: does_logger_exist
100# Description:
101# This function check if logger exist and executable.
102#
103# Globals:
104# None
105#
106# Arguments:
107# None
108#
109# Returns:
110# returns 0 if exist and executable else 1
111#------------------------------------------------------------------------------
112does_logger_exist()
113{
114cmd=/usr/bin/logger
115if [ -x ${cmd} ]; then
116 return 0
117 else
118 return 1
119 fi
120}
121
122#------------------------------------------------------------------------------
123# Function Name: info_message
124# Description:
125# This function print the info message information to the console
126#
127# Globals:
128# None
129#
130# Arguments:
131# string message
132#
133# Returns:
134# None
135#------------------------------------------------------------------------------
136info_message()
137{
138 echo "INFO: $1"
139 logger -p user.info "$1"
140}
141
142#------------------------------------------------------------------------------
143# Function Name: error_message
144# Description:
145# This function print the error message information to the console
146#
147# Globals:
148# None
149#
150# Arguments:
151# string message
152#
153# Returns:
154# returns 1
155#------------------------------------------------------------------------------
156error_message()
157{
158 echo "ERROR: $1"
159 logger -p user.err "$1"
160 return 1
161}
162
163#------------------------------------------------------------------------------
164# Function: clean_up
165#
166# Description:
167# This function delete the existing image in the standby boot partition.
168# In order to copy the the new image. also delete the onl source in the
169# working directory if it is there.
170#
171# Globals:
172# WORKING_DIR, ONL_SOURCE, STANDBY_BOOT_MNT_PATH, ACTIVE_DATA_MNT_PATh
173#
174# Arguments:
175# None
176#
177# Returns:
178# None
179#------------------------------------------------------------------------------
180clean_up()
181{
182 info_message "Deleting onl source"
183 rm -rf ${WORKING_DIR}/${ONL_SOURCE}
184 info_message "Deleting kernel and initrd files"
185 rm -rf ${STANDBY_BOOT_MNT_PATH}/kernel-*
186 rm -rf ${STANDBY_BOOT_MNT_PATH}/x86-64-accton-*
187 echo "INFO: Deleting configuration files"
188 rm -rf /etc/onl/*
189 rm -rf /lib/vendor-config/
190 info_message "Deleting standby rootfs"
191 rm -rf ${STANDBY_DATA_MNT_PATH}/*
192}
193
194#------------------------------------------------------------------------------
195# Function: update_grub_cfg_for_temp_reboot
196#
197# Description:
198# This function updates the grub.cfg file in order to achieve the temporary
199# reboot. by making standby entry in the temporary reboot. once temporary
200# reboot happens successfully grub.cfg file will have only active entry.
201#
202# Globals:
203# GRUB_CONFIG_FILE_CHANGED, ACTIVE_KERNEL_NAME, STANDBY_KERNEL_NAME
204#
205# Arguments:
206# None
207#
208# Returns:
209# None
210#------------------------------------------------------------------------------
211update_grub_cfg_for_temp_reboot()
212{
213 cat > ${GRUB_CONFIG_FILE_CHANGED} <<- EOF
214serial --port=0x3f8 --speed=115200 --word=8 --parity=no --stop=1
215terminal_input serial
216terminal_output serial
217set timeout=5
218
219# Always boot the saved_entry value
220load_env
221if [ \$saved_entry ] ; then
222 set default=\$saved_entry
223fi
224
225menuentry "Open Network Linux" {
226 search --no-floppy --label --set=root ONL-ACTIVE-BOOT
227 # Always return to this entry by default.
228 set saved_entry="0"
229 save_env saved_entry
230 echo 'Loading Open Network Linux ...'
231 insmod gzio
232 insmod part_msdos
233 linux /${ACTIVE_KERNEL_NAME} nopat console=ttyS0,115200n8 tg3.short_preamble=1 tg3.bcm5718s_reset=1 intel_iommu=off onl_platform=x86-64-accton-asxvolt16-r0 data-active
234 initrd /x86-64-accton-asxvolt16-r0.cpio.gz
235}
236
237menuentry "Open Network Linux Standby" {
238 search --no-floppy --label --set=root ONL-STANDBY-BOOT
239 # Always return to this entry by default.
240 set saved_entry="0"
241 save_env saved_entry
242 echo 'Loading Open Network Linux Standby...'
243 insmod gzio
244 insmod part_msdos
245 linux /${STANDBY_KERNEL_NAME} nopat console=ttyS0,115200n8 tg3.short_preamble=1 tg3.bcm5718s_reset=1 intel_iommu=off onl_platform=x86-64-accton-asxvolt16-r0 data-standby
246 initrd /x86-64-accton-asxvolt16-r0.cpio.gz
247}
248
249# Menu entry to chainload ONIE
250menuentry ONIE {
251 search --no-floppy --label --set=root ONIE-BOOT
252 set saved_entry="0"
253 save_env saved_entry
254 echo 'Loading ONIE ...'
255 chainloader +1
256}
257
258
259EOF
260}
261
262#------------------------------------------------------------------------------
263# Function: revert_grub_cfg
264#
265# Description:
266# This function revert the original grub.cfg file which was affected
267# by temporary reboot.
268#
269# Globals:
270# GRUB_CONFIG_FILE, ACTIVE_KERNEL_NAME
271#
272# Arguments:
273# None
274#
275# Returns:
276# None
277#------------------------------------------------------------------------------
278revert_grub_cfg()
279{
280 cat > ${GRUB_CONFIG_FILE} <<- EOF
281serial --port=0x3f8 --speed=115200 --word=8 --parity=no --stop=1
282terminal_input serial
283terminal_output serial
284set timeout=5
285
286# Always boot the saved_entry value
287load_env
288if [ \$saved_entry ] ; then
289 set default=\$saved_entry
290fi
291
292menuentry "Open Network Linux" {
293 search --no-floppy --label --set=root ONL-ACTIVE-BOOT
294 # Always return to this entry by default.
295 set saved_entry="0"
296 save_env saved_entry
297 echo 'Loading Open Network Linux ...'
298 insmod gzio
299 insmod part_msdos
300 linux /${ACTIVE_KERNEL_NAME} nopat console=ttyS0,115200n8 tg3.short_preamble=1 tg3.bcm5718s_reset=1 intel_iommu=off onl_platform=x86-64-accton-asxvolt16-r0 data-active
301 initrd /x86-64-accton-asxvolt16-r0.cpio.gz
302}
303
304menuentry "Open Network Linux Standby" {
305 search --no-floppy --label --set=root ONL-STANDBY-BOOT
306 # Always return to this entry by default.
307 set saved_entry="0"
308 save_env saved_entry
309 echo 'Loading Open Network Linux Standby...'
310 insmod gzio
311 insmod part_msdos
312 linux /${STANDBY_KERNEL_NAME} nopat console=ttyS0,115200n8 tg3.short_preamble=1 tg3.bcm5718s_reset=1 intel_iommu=off onl_platform=x86-64-accton-asxvolt16-r0 data-standby
313 initrd /x86-64-accton-asxvolt16-r0.cpio.gz
314}
315
316# Menu entry to chainload ONIE
317menuentry ONIE {
318 search --no-floppy --label --set=root ONIE-BOOT
319 set saved_entry="0"
320 save_env saved_entry
321 echo 'Loading ONIE ...'
322 chainloader +1
323}
324
325EOF
326}
327#------------------------------------------------------------------------------
328# Function: grub_reboot
329#
330# Description:
331# This function gives a temporary reboot using grub-reboot command, this will
332# set the boot menu entry from active default one to Standby. Now OS will
333# boot up in Standby mode.
334#
335# Globals:
336# ACTIVE_BOOTDIR, STANDBY_BOOTDIR, GRUB_CONFIG_FILE_CHANGED, ACTIVE_BOOT
337# GRUB
338#
339# Arguments:
340# None
341#
342# Returns:
343# None
344#------------------------------------------------------------------------------
345grub_reboot()
346{
347 if [ -f "${ACTIVE_BOOTDIR}/grub/grubenv" ]; then
348 saved_entry=$(grub-editenv ${ACTIVE_BOOTDIR}/grub/grubenv list | sed -n 's/^saved_entry=//p')
349 if [ ${saved_entry} -eq 0 ]; then
350 grub-editenv ${ACTIVE_BOOTDIR}/grub/grubenv set saved_entry=1
351 sed -i '/next_entry=/a\prev_saved_entry=1' ${ACTIVE_BOOTDIR}/grub/grubenv
352 fi
353 fi
354
355 if [ -f "${STANDBY_BOOTDIR}/grub/grubenv" ]; then
356 saved_entry=$(grub-editenv ${STANDBY_BOOTDIR}/grub/grubenv list | sed -n 's/^saved_entry=//p')
357 if [ ${saved_entry} -eq 0 ]; then
358 grub-editenv ${STANDBY_BOOTDIR}/grub/grubenv set saved_entry=1
359 sed -i '/next_entry=/a\prev_saved_entry=1' ${STANDBY_BOOTDIR}/grub/grubenv
360 fi
361 fi
362
363 if [ -f ${GRUB_CONFIG_FILE_CHANGED} ]; then
364 if [ -f "${ACTIVE_BOOT}/${GRUB}/grub.cfg" ]; then
365 cp ${GRUB_CONFIG_FILE_CHANGED} ${ACTIVE_BOOT}/${GRUB}/grub.cfg
366 fi
367 if [ -f "${STANDBY_BOOT}/${GRUB}/grub.cfg" ]; then
368 cp ${GRUB_CONFIG_FILE_CHANGED} ${STANDBY_BOOT}/${GRUB}/grub.cfg
369 fi
370 fi
371 grub-reboot --boot-directory=${ACTIVE_BOOTDIR} 1
372 reboot
373}
374
375#------------------------------------------------------------------------------
376# Function: install_onl
377#
378# Description:
379# This function copy the upgrade image.
380#
381# Globals:
382# WORKING_DIR, ONL_SOURCE, STANDBY_BOOT_MNT_PATH, IMAGES, ACTIVE_BOOT_MNT_PATH
383#
384# Arguments:
385# None
386#
387# Returns:
388# None
389#------------------------------------------------------------------------------
390install_onl()
391{
392 if [ -f ${ONL_FILE} ]; then
393 clean_up
394 mkdir ${WORKING_DIR}/${ONL_SOURCE}
395 cd ${WORKING_DIR}/${ONL_SOURCE}
396 chmod 777 ${ONL_FILE}
397 # Extract onl package
398 unzip ${ONL_FILE}
399 # Copy kernel files to standby boot partition
400 cp kernel-* ${STANDBY_BOOT_MNT_PATH}/
401 # Copy initrd file to standby boot partition
402 cp onl-loader-initrd-amd64.cpio.gz ${STANDBY_BOOT_MNT_PATH}/x86-64-accton-asxvolt16-r0.cpio.gz
403 # Create upgrade directory in the images partition to copy upgrade image
404 if [ -d ${IMAGES}/upgrade ]; then
405 rm -rf ${IMAGES}/upgrade
406 fi
407 info_message "Creating Upgrade Directory"
408 mkdir ${IMAGES}/upgrade
409 info_message "Copying SWI file to Upgrade Directory"
410 cp *.swi ${IMAGES}/upgrade
411
412 # Update the kernel version
413 STANDBY_KERNEL_NAME=$(ls ${STANDBY_BOOT_MNT_PATH} | grep kernel-4.14)
414 ACTIVE_KERNEL_NAME=$(ls ${ACTIVE_BOOT_MNT_PATH} | grep kernel-4.14 )
415 update_grub_cfg_for_temp_reboot
416 revert_grub_cfg
417 else
418 error_message "ONL file not found"
419 exit 1
420 fi
421}
422
423#------------------------------------------------------------------------------
424# Function: upgrade_onl
425#
426# Description:
427# This function get the labels, and install the upgrade NOS.
428#
429# Globals :
430# ACTIVE_DATA_DEV_NAME, ACTIVE_BOOT_DEV_NAME, ACTIVE_BOOT_MNT_PATH,
431# STANDBY_DATA_DEV_NAME, STANDBY_BOOT_MNT_PATH
432#
433# Arguments:
434# None
435#
436# Returns:
437# None
438#------------------------------------------------------------------------------
439upgrade_onl()
440{
441
442 ACTIVE_DATA_DEV_NAME=$(blkid | grep " LABEL=\"ONL-ACTIVE-DATA\"" | awk '{print $1}' | sed 's/://')
443 echo "active data dev - $ACTIVE_DATA_DEV_NAME"
444 ACTIVE_DATA_MNT_PATH=$(mount | grep $ACTIVE_DATA_DEV_NAME | awk '{print $3}')
445 echo $"active data mnt - $ACTIVE_DATA_MNT_PATH"
446 ACTIVE_BOOT_DEV_NAME=$(blkid | grep " LABEL=\"ONL-ACTIVE-BOOT\"" | awk '{print $1}' | sed 's/://')
447 echo "active boot dev $ACTIVE_BOOT_DEV_NAME"
448 ACTIVE_BOOT_MNT_PATH=$(mount | grep $ACTIVE_BOOT_DEV_NAME | awk '{print $3}')
449 echo "active boot mnt - $ACTIVE_BOOT_MNT_PATH"
450 STANDBY_DATA_DEV_NAME=$(blkid | grep " LABEL=\"ONL-STANDBY-DATA\"" | awk '{print $1}' | sed 's/://')
451 echo "standby data dev - $STANDBY_DATA_DEV_NAME"
452 STANDBY_DATA_MNT_PATH=$(mount | grep $STANDBY_DATA_DEV_NAME | awk '{print $3}')
453 echo "standby data mnt - $STANDBY_DATA_MNT_PATH"
454 STANDBY_BOOT_DEV_NAME=$(blkid | grep " LABEL=\"ONL-STANDBY-BOOT\"" | awk '{print $1}' | sed 's/://')
455 echo "standby boot dev - $STANDBY_BOOT_DEV_NAME"
456 STANDBY_BOOT_MNT_PATH=$(mount | grep $STANDBY_BOOT_DEV_NAME | awk '{print $3}')
457 echo "standby boot mnt -$STANDBY_BOOT_MNT_PATH"
458
459 install_onl
460 grub_reboot
461}
462
463#------------------------------------------------------------------------------
464# Function: command_result
465#
466# Description:
467# This function executes the blkid command.
468#
469# Globals:
470# None
471# Arguments:
472# None
473#
474# Returns:
475# None
476#------------------------------------------------------------------------------
477command_result(){
478 blkid | grep $1 > /dev/null
479 if [ $? -eq 0 ]; then
480 return 0
481 else
482 return 1
483 fi
484}
485
486#------------------------------------------------------------------------------
487# Function: is_onl_running
488#
489# Description:
490# This function check the running NOS and also check the Upgrade is required
491# or not.
492#
493# Globals:
494# ONL_ACTIVE_BOOT, ONL_ACTIVE_DATA, ONL_STANDBY_BOOT, ONL_STANDBY_DATA
495# ONL_CONFIG, ONL_IMAGES, IMAGES, ACTIVE_DATA, ACTIVE_BOOT, STANDBY_DATA
496# STANDBY_BOOT
497#
498# Arguments:
499# None
500#
501# Returns:
502# None
503#------------------------------------------------------------------------------
504is_onl_running()
505{
506 info_message "Check all directories exist or not"
507 if command_result "$ONL_ACTIVE_BOOT" && command_result "$ONL_ACTIVE_DATA" && command_result "$ONL_STANDBY_BOOT" && command_result "$ONL_STANDBY_DATA" && command_result "$ONL_CONFIG" && command_result "$ONL_IMAGES" ; then
508 if [ \( -d "$CONFIG" -a -d "$IMAGES" \) -a \( -d "$ACTIVE_DATA" -a -d "$ACTIVE_BOOT" \) -a \( -d "$STANDBY_DATA" -a -d "$STANDBY_BOOT" \) ]; then
509 info_message "All directories exist. Need Upgrade"
510 UPGRADE=1
511 fi
512 else
513 info_message "It is new installation"
514 UPGRADE=0
515 fi
516}
517
518#------------------------------------------------------------------------------
519# Function: main
520#
521# Description:
522# This is the main function. which decides fresh installation is required or
523# upgrade is required.
524#
525# Globals :
526# UPGRADE, ONL_FILE
527# Arguments :
528# None
529#
530# Returns :
531# None
532#------------------------------------------------------------------------------
533main()
534{
535 echo "INFO: Check if image upgrade or new installation"
536 is_onl_running
537 if [ $UPGRADE -eq 0 ]; then
538 error_message "ONL does not have active and stand by partitions \
539 install ONL image with active and standby partitions"
540 exit 1
541 else
542 info_message "Upgrade started"
543 upgrade_onl
544 fi
545}
546
547# main function call
548does_logger_exist
549if [ $? -eq 0 ]; then
550 main
551else
552 error_message "logger does not exist"
553 exit 1
554fi