fwvalidate 3.0 KB

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