Naveen Moses | c8d90aa | 2021-10-07 18:20:33 +0530 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | #BMC set time from host |
| 3 | set -e |
| 4 | |
| 5 | echo "set-bmc-time-from-host is started" |
| 6 | # Sync BMC's date with one of the four servers |
| 7 | |
| 8 | HOST_INSTANCES="HOST_INSTANCES_SED_REPLACEMENT_VALUE" |
| 9 | MAX_RETRY_LIMIT=6 |
| 10 | |
| 11 | check_NTP_status() |
| 12 | { |
| 13 | timedatectl show --property=NTPSynchronized --value |
| 14 | } |
| 15 | |
| 16 | get_single_host_time() |
| 17 | { |
| 18 | for (( retry=1; retry<=5; retry++ )) |
| 19 | do |
| 20 | #request the single host time via ipmb command |
| 21 | # which will be set as bmc time |
| 22 | # 0x01 - me channel | 0x0a - storage net fn | 0x00 - lun |
| 23 | # 0x48 - get SEL time |
| 24 | ipmi_cmd_output=$(busctl call xyz.openbmc_project.Ipmi.Channel.Ipmb \ |
| 25 | "/xyz/openbmc_project/Ipmi/Channel/Ipmb" org.openbmc.Ipmb sendRequest \ |
| 26 | yyyyay 0x01 0x0a 0x00 0x48 0) |
| 27 | ipmb_result=$? |
| 28 | if [ "$ipmb_result" == "0" ];then |
| 29 | sleep 1 |
| 30 | break |
| 31 | fi |
| 32 | done |
| 33 | |
| 34 | if [ "$retry" == "$MAX_RETRY_LIMIT" ];then |
| 35 | exit 1 |
| 36 | fi |
| 37 | |
| 38 | echo "$ipmi_cmd_output" |
| 39 | |
| 40 | } |
| 41 | |
| 42 | get_multi_host_datetime() |
| 43 | { |
| 44 | ipmbAddr=$1 |
| 45 | for (( retry=1; retry<=5; retry++ )) |
| 46 | do |
| 47 | #request the multihost host time via ipmb command |
| 48 | # which will be set as bmc time |
| 49 | # 0x38 - oem net fn | 0x00 - lun | 0x02 - request to bridge ic cmd |
| 50 | # 0x6 - length | IANA id 0x15 0xA0 0x0 |0x48 - get SEL time |
| 51 | |
| 52 | ipmi_cmd_output=$(busctl call xyz.openbmc_project.Ipmi.Channel.Ipmb \ |
| 53 | /xyz/openbmc_project/Ipmi/Channel/Ipmb org.openbmc.Ipmb sendRequest \ |
| 54 | yyyyay "$ipmbAddr" 0x38 0 0x2 6 0x15 0xA0 0x0 0x1 0x28 0x48) |
| 55 | ipmb_result=$? |
| 56 | if [ $ipmb_result == 0 ];then |
| 57 | break |
| 58 | fi |
| 59 | sleep 1 |
| 60 | done |
| 61 | |
| 62 | echo "$ipmi_cmd_output" |
| 63 | } |
| 64 | sync_multi_host_datetime() |
| 65 | { |
| 66 | for index in $HOST_INSTANCES |
| 67 | do |
| 68 | ipmb_addr=$(((index-1)*4)) |
| 69 | # Use standard IPMI command 'SendRequest method' to read RTC time |
| 70 | echo "chosen ipmb addr : "$ipmb_addr |
| 71 | multi_host_time_result=$(get_multi_host_datetime $ipmb_addr) |
| 72 | |
| 73 | if [[ $(echo "$multi_host_time_result" | awk '{ print NF }') -eq 18 ]]; |
| 74 | then |
Andrew Geissler | d96085e | 2023-04-13 15:34:50 -0600 | [diff] [blame] | 75 | echo "syncing up host " "$index" " date time with bmc..." |
Naveen Moses | c8d90aa | 2021-10-07 18:20:33 +0530 | [diff] [blame] | 76 | date -s @$((0x$(echo "$multi_host_time_result" | \ |
| 77 | awk '{printf "%02x%02x%02x%02x",$18,$17,$16,$15}'))) |
| 78 | sync |
| 79 | break |
| 80 | fi |
| 81 | done |
| 82 | |
| 83 | } |
| 84 | sync_single_host_datetime() |
| 85 | { |
| 86 | single_host_time_result=$(get_single_host_time) |
| 87 | |
| 88 | if [[ $(echo "$single_host_time_result" | awk '{ print NF }') -eq 11 ]]; |
| 89 | then |
| 90 | echo "Syncing up host date time with bmc..." |
| 91 | date -s @$((0x$(echo "$single_host_time_result" | \ |
| 92 | awk '{printf "%02x%02x%02x%02x",$11,$10,$9,$8}'))) |
| 93 | sync |
| 94 | fi |
| 95 | |
| 96 | } |
| 97 | |
| 98 | #wait for the NTP server start if available. |
| 99 | sleep 60 |
| 100 | |
| 101 | NTP_STATUS=$(check_NTP_status) |
| 102 | |
| 103 | echo "NTP status :""$NTP_STATUS" |
| 104 | |
| 105 | if [ "$NTP_STATUS" == "yes" ]; then |
| 106 | echo "NTP is running and system clock is in sync.skiping host time sync..." |
| 107 | exit 0 |
| 108 | fi |
| 109 | |
| 110 | if [ "$HOST_INSTANCES" == "0" ]; then |
| 111 | echo "single host instance" |
| 112 | sync_single_host_datetime |
| 113 | else |
| 114 | echo "multiple host instance" |
| 115 | sync_multi_host_datetime |
| 116 | fi |