fwvalidate 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. #!/bin/sh
  2. # This file is part of the OpenADK project.
  3. # Validate update.
  4. GRUB=$(which grub-reboot)
  5. if [ "${GRUB}" = "/usr/sbin/grub-reboot" ]; then
  6. REVERSE=0
  7. else
  8. REVERSE=1
  9. fi
  10. DISK=@@DISK@@
  11. if [ $REVERSE -eq 1 ]; then
  12. PART0="/dev/${DISK}p1"
  13. PART1="/dev/${DISK}p2"
  14. else
  15. PART0="/dev/${DISK}2"
  16. PART1="/dev/${DISK}3"
  17. fi
  18. APPLIANCE_NAME=OpenADK
  19. BOOT0_NAME="OpenADK1"
  20. BOOT1_NAME="OpenADK2"
  21. CURRENT_SYS="$(rdev /|awk '{ print $1 }' )"
  22. TIMEOUT=45
  23. STAT_FILE="/tmp/update_status"
  24. SSH_KEY_FOLDER=/etc/dropbear/
  25. SSH_KEYS=("dropbear_dss_host_key" "dropbear_ecdsa_host_key" "dropbear_rsa_host_key")
  26. DEBUG=1
  27. if [ "x$1" == "xtest" ];then
  28. TIMEOUT=1
  29. fi
  30. get_interface(){
  31. ip route list | grep '^default' | cut -d\ -f 5
  32. }
  33. get_nw_mask(){
  34. # This function will get the NW Mask in the form /x e.g. /16
  35. local BIT=$(ip a s $(get_interface)| grep inet\ | cut -d/ -f2| cut -d\ -f1)
  36. echo $BIT
  37. }
  38. getip() {
  39. DEFDEVICE=$(ip route list | grep ^default | cut -d\ -f5)
  40. IPADDR=$(ip a s $(ip route list | grep ^default | cut -d\ -f5) | grep inet\ | grep 'inet' | cut -d\ -f 6 | cut -d/ -f1)
  41. echo $IPADDR
  42. }
  43. chk_initial_save(){
  44. if [ $(cfgfs status | wc -l) -gt 0 ];then
  45. echo "please save configuration"
  46. fi
  47. }
  48. updatebootflag(){
  49. case "$CURRENT_SYS" in
  50. "$PART1")
  51. sfdisk -A /dev/$DISK 1
  52. ;;
  53. "$PART0")
  54. sfdisk -A /dev/$DISK 2
  55. ;;
  56. *)
  57. echo "Current partition $CURRENT_SYS not recognized"
  58. exit 1
  59. ;;
  60. esac
  61. }
  62. updategrub(){
  63. mount -o remount,rw /boot
  64. case "$CURRENT_SYS" in
  65. "$PART1")
  66. grub-set-default OpenADK2
  67. ;;
  68. "$PART0")
  69. grub-set-default OpenADK1
  70. ;;
  71. *)
  72. echo "Current partition $CURRENT_SYS not recognized"
  73. exit 1
  74. ;;
  75. esac
  76. sync
  77. mount -o remount,ro /boot
  78. }
  79. base_check() {
  80. NET_PROGS="$(netstat -tulpn 2>/dev/null)"
  81. TESTS=0
  82. TESTSUM=0
  83. #test start: check if dropbear is running
  84. T_NAME=dropbear
  85. if [[ $NET_PROGS = *"/dropbear"* ]];then
  86. logger -t update "check $T_NAME OK"
  87. TESTS=$(( $TESTS + 1 ))
  88. else
  89. logger -t update "check $T_NAME FAILURE"
  90. fi
  91. ((TESTSUM = TESTSUM +1))
  92. #test end
  93. }
  94. if [ -f /installation_date.txt ];then
  95. echo "Update was applied at:" > $STAT_FILE
  96. echo "$(head -n1 /installation_date.txt)" >> $STAT_FILE
  97. else
  98. rm -f $STAT_FILE
  99. fi
  100. # Do some checks before setting the new partiton as default boot partition.
  101. if ( [ -f /firmware_check ] || [ "x$1" = "xtest" ] );then
  102. logger -t update "check now!"
  103. base_check
  104. i=0
  105. while [ $TESTS -lt $TESTSUM ];do
  106. base_check
  107. [ $DEBUG -gt 0 ] && echo "$i Only $TESTS from $TESTSUM are passed wait until $TIMEOUT"
  108. sleep 1
  109. i=$(( $i + 1 ))
  110. if [ $i -ge $TIMEOUT ];then
  111. break
  112. fi
  113. done
  114. else
  115. logger -t update "$APPLIANCE_NAME validate nothing to do..."
  116. if [ -f $STAT_FILE ];then
  117. echo "Last update was successful" >> $STAT_FILE
  118. else
  119. echo "Firmware check was successful" >> $STAT_FILE
  120. fi
  121. n=0
  122. chk_initial_save
  123. exit 0
  124. fi
  125. if [ $TESTS -eq $TESTSUM ]; then
  126. logger -t update "All Tests passed."
  127. if [ "x$1" = "x" ]; then
  128. logger -t update "Set default boot partition for bootloader."
  129. mount -o remount,rw /
  130. rm /firmware_check
  131. mount -o remount,ro /
  132. echo "System check was successful" >> $STAT_FILE
  133. if [ $REVERSE -eq 1 ]; then
  134. echo "Nothing todo. All fine."
  135. logger -t update "Nothing todo. All fine."
  136. else
  137. updategrub
  138. fi
  139. fi
  140. else
  141. if [ $REVERSE -eq 1 ]; then
  142. updatebootflag
  143. fi
  144. logger -t update "Not all tests passed. The default system remains on the current partition."
  145. logger -t update "Please try to reboot the system and repeat the update."
  146. echo "ERROR last system update failed, please reboot and try again." >> $STAT_FILE
  147. exit 1
  148. fi