Skip to content

Commit 1860f4b

Browse files
authored
Improve OpenWrt VM boot and readiness check (#7669)
* Improve OpenWrt VM boot and readiness check Replaces the fixed sleep and repeated configuration attempts with a loop that waits for the VM to be ready using 'qm agent ping'. Configuration commands are now sent only after OpenWrt is confirmed to be ready, improving reliability and startup efficiency. * Update openwrt-vm.sh * Add VM running check before configuring OpenWrt network Wrapped network configuration commands in a check to ensure the VM is running before sending commands. This prevents errors if the VM is not active and provides appropriate success or error messages. * Enable and update VM cleanup logic in openwrt-vm.sh Uncommented and updated the cleanup_vmid function to stop and destroy the VM if it exists. Also enabled its invocation in the error handler to ensure proper resource cleanup on errors.
1 parent bf0bac1 commit 1860f4b

File tree

1 file changed

+24
-11
lines changed

1 file changed

+24
-11
lines changed

vm/openwrt-vm.sh

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ function error_handler() {
7777
post_update_to_api "failed" "$command"
7878
local error_message="${RD}[ERROR]${CL} in line ${RD}$line_number${CL}: exit code ${RD}$exit_code${CL}: while executing command ${YW}$command${CL}"
7979
echo -e "\n$error_message\n"
80-
#cleanup_vmid
80+
cleanup_vmid
8181
}
8282

8383
function get_valid_nextid() {
@@ -97,12 +97,12 @@ function get_valid_nextid() {
9797
echo "$try_id"
9898
}
9999

100-
# function cleanup_vmid() {
101-
# if qm status $VMID &>/dev/null; then
102-
# qm stop $VMID &>/dev/null
103-
# #qm destroy $VMID &>/dev/null
104-
# fi
105-
# }
100+
function cleanup_vmid() {
101+
if qm status $VMID &>/dev/null; then
102+
qm stop $VMID &>/dev/null
103+
qm destroy $VMID &>/dev/null
104+
fi
105+
}
106106

107107
function cleanup() {
108108
popd >/dev/null
@@ -588,9 +588,19 @@ msg_ok "Created OpenWrt VM ${CL}${BL}(${HN})"
588588
msg_info "OpenWrt is being started in order to configure the network interfaces."
589589
qm start $VMID
590590
sleep 15
591+
msg_info "Waiting for OpenWrt to boot..."
592+
for i in {1..30}; do
593+
if qm status "$VMID" | grep -q "running"; then
594+
sleep 5
595+
msg_ok "OpenWrt is running"
596+
break
597+
fi
598+
sleep 1
599+
done
600+
591601
msg_ok "Network interfaces are being configured as OpenWrt initiates."
592-
for _ in {1..30}; do
593-
if qm status "$VMID" | grep -q "stopped"; then break; fi
602+
603+
if qm status "$VMID" | grep -q "running"; then
594604
send_line_to_vm ""
595605
send_line_to_vm "uci delete network.@device[0]"
596606
send_line_to_vm "uci set network.wan=interface"
@@ -604,8 +614,11 @@ for _ in {1..30}; do
604614
send_line_to_vm "uci set network.lan.netmask=${LAN_NETMASK}"
605615
send_line_to_vm "uci commit"
606616
send_line_to_vm "halt"
607-
done
608-
msg_ok "Network interfaces configured in OpenWrt"
617+
msg_ok "Network interfaces configured in OpenWrt"
618+
else
619+
msg_error "VM is not running"
620+
exit 1
621+
fi
609622

610623
msg_info "Waiting for OpenWrt to shut down..."
611624
until qm status "$VMID" | grep -q "stopped"; do

0 commit comments

Comments
 (0)