diff --git a/scripts/boot/5g-dial.sh b/scripts/boot/5g-dial.sh index 1801794..714670f 100644 --- a/scripts/boot/5g-dial.sh +++ b/scripts/boot/5g-dial.sh @@ -7,6 +7,40 @@ source "${SCRIPT_DIR}/common.sh" STEP="5g-dial" +disable_interfaces() { + local raw_list="$1" + local iface + local nmcli_available=0 + + if [[ -z "${raw_list}" ]]; then + return 0 + fi + if command -v nmcli >/dev/null 2>&1; then + nmcli_available=1 + fi + + for iface in ${raw_list//,/ }; do + if [[ -z "${iface}" ]]; then + continue + fi + blitz_log "${STEP}" "disable-interface" "start" "iface=${iface}" 0 + if [[ "${nmcli_available}" -eq 1 ]]; then + nmcli device disconnect "${iface}" >/dev/null 2>&1 || true + fi + if ip link show dev "${iface}" >/dev/null 2>&1; then + if ip link set dev "${iface}" down; then + blitz_log "${STEP}" "disable-interface" "success" "iface=${iface}" 0 + else + rc=$? + blitz_log "${STEP}" "disable-interface" "failure" "iface=${iface}" "${rc}" + return "${rc}" + fi + else + blitz_log "${STEP}" "disable-interface" "success" "iface=${iface} not present, skipping" 0 + fi + done +} + wait_for_serial() { local serial_port="$1" local timeout_sec="$2" @@ -31,23 +65,24 @@ wait_for_serial() { wait_for_route() { local target_ip="$1" local timeout_sec="$2" + local expected_interface="${3:-}" local waited=0 local route_output while (( waited < timeout_sec )); do - route_output="$(blitz_route_ready "${target_ip}" || true)" + route_output="$(blitz_route_ready "${target_ip}" "${expected_interface}" || true)" if [[ -n "${route_output}" ]]; then - blitz_log "${STEP}" "route-check" "success" "target_ip=${target_ip} route=${route_output}" 0 + blitz_log "${STEP}" "route-check" "success" "target_ip=${target_ip} interface=${expected_interface:-auto} route=${route_output}" 0 return 0 fi if (( waited == 0 || waited % 5 == 0 )); then - blitz_log "${STEP}" "route-check" "waiting" "target_ip=${target_ip} waited_sec=${waited}" 0 + blitz_log "${STEP}" "route-check" "waiting" "target_ip=${target_ip} interface=${expected_interface:-auto} waited_sec=${waited}" 0 fi sleep 1 waited=$(( waited + 1 )) done - blitz_log "${STEP}" "route-check" "failure" "target_ip=${target_ip} timeout_sec=${timeout_sec}" 1 + blitz_log "${STEP}" "route-check" "failure" "target_ip=${target_ip} interface=${expected_interface:-auto} timeout_sec=${timeout_sec}" 1 return 1 } @@ -61,18 +96,24 @@ if [[ -z "${BLITZ_TIME_SERVER_IP}" ]]; then blitz_log "${STEP}" "precheck" "failure" "BLITZ_TIME_SERVER_IP is empty and no fallback could be derived" 1 exit 1 fi +if [[ -z "${BLITZ_5G_INTERFACE:-}" ]]; then + blitz_log "${STEP}" "precheck" "failure" "BLITZ_5G_INTERFACE must not be empty" 1 + exit 1 +fi -route_output="$(blitz_route_ready "${BLITZ_TIME_SERVER_IP}" || true)" +disable_interfaces "${BLITZ_5G_DISABLE_INTERFACES:-}" + +route_output="$(blitz_route_ready "${BLITZ_TIME_SERVER_IP}" "${BLITZ_5G_INTERFACE}" || true)" if [[ -n "${route_output}" ]]; then - blitz_log "${STEP}" "dial" "already_up" "target_ip=${BLITZ_TIME_SERVER_IP} route=${route_output}" 0 + blitz_log "${STEP}" "dial" "already_up" "target_ip=${BLITZ_TIME_SERVER_IP} interface=${BLITZ_5G_INTERFACE} route=${route_output}" 0 exit 0 fi wait_for_serial "${BLITZ_5G_SERIAL_PORT}" "${BLITZ_5G_SERIAL_WAIT_SEC}" pushd "${BLITZ_5G_DIAL_DIR}" >/dev/null -blitz_run "${STEP}" "dial" python3 rndis_dial.py --serial-port "${BLITZ_5G_SERIAL_PORT}" +blitz_run "${STEP}" "dial" python3 rndis_dial.py --serial-port "${BLITZ_5G_SERIAL_PORT}" --interface "${BLITZ_5G_INTERFACE}" popd >/dev/null -wait_for_route "${BLITZ_TIME_SERVER_IP}" "${BLITZ_5G_ROUTE_WAIT_SEC}" -blitz_log "${STEP}" "complete" "success" "5G dial completed and route is ready" 0 +wait_for_route "${BLITZ_TIME_SERVER_IP}" "${BLITZ_5G_ROUTE_WAIT_SEC}" "${BLITZ_5G_INTERFACE}" +blitz_log "${STEP}" "complete" "success" "5G dial completed and route is ready on ${BLITZ_5G_INTERFACE}" 0 diff --git a/scripts/boot/common.sh b/scripts/boot/common.sh index 5974a96..8caef70 100644 --- a/scripts/boot/common.sh +++ b/scripts/boot/common.sh @@ -54,6 +54,8 @@ blitz_load_boot_env() { export BLITZ_LOG_FILE="${BLITZ_LOG_FILE:-/var/log/blitz-robot/startup.log}" export BLITZ_5G_DIAL_DIR="${BLITZ_5G_DIAL_DIR:-/home/nvidia/5g-test/5G}" export BLITZ_5G_SERIAL_PORT="${BLITZ_5G_SERIAL_PORT:-/dev/ttyUSB7}" + export BLITZ_5G_INTERFACE="${BLITZ_5G_INTERFACE:-eth0}" + export BLITZ_5G_DISABLE_INTERFACES="${BLITZ_5G_DISABLE_INTERFACES:-}" export BLITZ_5G_SERIAL_WAIT_SEC="${BLITZ_5G_SERIAL_WAIT_SEC:-60}" export BLITZ_5G_ROUTE_WAIT_SEC="${BLITZ_5G_ROUTE_WAIT_SEC:-30}" export BLITZ_TIME_SERVER_IP="${BLITZ_TIME_SERVER_IP:-${default_time_server}}" @@ -158,21 +160,24 @@ blitz_require_root() { blitz_run() { local step="$1" local action="$2" + local rc shift 2 blitz_log "${step}" "${action}" "start" "$(blitz_join_cmd "$@")" 0 if "$@"; then blitz_log "${step}" "${action}" "success" "$(blitz_join_cmd "$@")" 0 return 0 + else + rc=$? fi - local rc=$? blitz_log "${step}" "${action}" "failure" "$(blitz_join_cmd "$@")" "${rc}" return "${rc}" } blitz_route_ready() { local target_ip="$1" + local expected_interface="${2:-}" local route_output route_output="$(ip route get "${target_ip}" 2>&1 || true)" @@ -182,6 +187,9 @@ blitz_route_ready() { if [[ "${route_output}" == *"unreachable"* || "${route_output}" == *"prohibit"* ]]; then return 1 fi + if [[ -n "${expected_interface}" && "${route_output}" != *" dev ${expected_interface} "* && "${route_output}" != *" dev ${expected_interface}" ]]; then + return 1 + fi printf '%s\n' "${route_output}" return 0 diff --git a/scripts/boot/robot-boot.env b/scripts/boot/robot-boot.env index 3d3b547..b23d0c1 100644 --- a/scripts/boot/robot-boot.env +++ b/scripts/boot/robot-boot.env @@ -6,9 +6,9 @@ BLITZ_LOG_FILE="/var/log/blitz-robot/startup.log" BLITZ_5G_DIAL_DIR="/home/nvidia/5g-test/5G" BLITZ_5G_SERIAL_PORT="/dev/ttyUSB7" -# 最多等 60 秒让 5G 模块对应的串口设备出现,比如 /dev/ttyUSB7 +BLITZ_5G_INTERFACE="eth0" +BLITZ_5G_DISABLE_INTERFACES="eno3" BLITZ_5G_SERIAL_WAIT_SEC="60" -# 拨号命令执行完以后,最多再等 30 秒,检查到你目标服务器 IP 的路由真的起来 BLITZ_5G_ROUTE_WAIT_SEC="30" # Leave empty to fall back to the host part of ROBOT_SIDE_OMNISOCKET_SERVER_ADDR. diff --git a/scripts/boot/time-sync.sh b/scripts/boot/time-sync.sh index 396d82c..27434ce 100644 --- a/scripts/boot/time-sync.sh +++ b/scripts/boot/time-sync.sh @@ -10,6 +10,7 @@ CHRONY_SOURCES_DIR="/etc/chrony/sources.d" CHRONY_SOURCE_FILE="${CHRONY_SOURCES_DIR}/blitz-robot.sources" CHRONY_MAIN_CONF="/etc/chrony/chrony.conf" CHRONY_MAIN_CONF_BAK="/etc/chrony/chrony.conf.blitz-bak" +CHRONY_BURST_SAMPLES="${CHRONY_BURST_SAMPLES:-1/2}" chrony_unit_name() { if systemctl list-unit-files chrony.service --no-legend 2>/dev/null | grep -q '^chrony\.service'; then @@ -96,7 +97,7 @@ write_chrony_source_file CHRONY_UNIT="$(chrony_unit_name)" blitz_run "${STEP}" "restart-chrony" systemctl restart "${CHRONY_UNIT}" -blitz_run "${STEP}" "burst" chronyc burst +blitz_run "${STEP}" "burst" chronyc burst "${CHRONY_BURST_SAMPLES}" blitz_log "${STEP}" "waitsync" "start" "server=${BLITZ_TIME_SERVER_IP} port=${BLITZ_TIME_SERVER_PORT} wait_sec=${BLITZ_TIME_SYNC_WAIT_SEC} max_offset_sec=${BLITZ_TIME_SYNC_MAX_OFFSET_SEC} interval_sec=${BLITZ_TIME_SYNC_INTERVAL_SEC}" 0 if chronyc waitsync "${BLITZ_TIME_SYNC_WAIT_SEC}" "${BLITZ_TIME_SYNC_MAX_OFFSET_SEC}" 1000 "${BLITZ_TIME_SYNC_INTERVAL_SEC}"; then