Skip to content

Commit ee07a43

Browse files
peterfangacrnsi-robot
authored andcommitted
misc: add the s5_trigger.sh script
Tracked-On: #5411 Signed-off-by: Peter Fang <[email protected]>
1 parent ec8af42 commit ee07a43

File tree

1 file changed

+216
-0
lines changed

1 file changed

+216
-0
lines changed

misc/life_mngr/s5_trigger.sh

Lines changed: 216 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,216 @@
1+
#! /bin/bash
2+
# Copyright (C) 2020 Intel Corporation.
3+
# SPDX-License-Identifier: BSD-3-Clause
4+
5+
MAX_ARRAY=6
6+
7+
started="started"
8+
stopped="stopped"
9+
SOS="sos"
10+
UOS="uos"
11+
SHUTDOWN="shutdown"
12+
ACKED="acked"
13+
14+
#config
15+
IP_ADDR="127.0.0.1"
16+
SOCKET_PORT="8193"
17+
18+
#send message by socket
19+
send_message()
20+
{
21+
message="$1"
22+
echo "Sending: $message"
23+
echo -ne "$message" >&6 &
24+
}
25+
26+
#read message by socket
27+
read_message()
28+
{
29+
read -r -d $'\0' ret_msg <&6
30+
}
31+
32+
#send message by tty
33+
send_pre_message()
34+
{
35+
message="$1"
36+
echo "Sending: $message"
37+
echo -ne "$message" > /dev/ttyS1
38+
}
39+
40+
#read message by tty
41+
read_pre_message()
42+
{
43+
read -r -d $'\0' ret_msg < /dev/ttyS1
44+
}
45+
46+
power_off_post_vms() {
47+
vm_list=$(acrnctl list)
48+
echo $vm_list
49+
50+
array=($vm_list)
51+
num=${#array[@]}
52+
echo "Number of VMs: " $num
53+
54+
if [ $num -gt $MAX_ARRAY ]; then
55+
echo "There are no VMs running or acrnctl encountered an internal error."
56+
return 0
57+
fi
58+
59+
#shut down post-launched VMs
60+
for ((i=0; i<$num; i+=2))
61+
do
62+
if [ ${array[$i+1]} == $started ]; then
63+
echo "Shutting down: " ${array[$i]}
64+
acrnctl stop ${array[$i]}
65+
sleep 5s
66+
fi
67+
done
68+
69+
return 1
70+
}
71+
72+
check_post_vms_status() {
73+
#check post-launched VM status for some time
74+
check_times=5
75+
while [[ $check_times > 0 ]]
76+
do
77+
vm_list=$(acrnctl list)
78+
array=($vm_list)
79+
num=${#array[@]}
80+
echo "VM status: " $vm_list
81+
82+
if [ $num -gt $MAX_ARRAY ]; then
83+
sleep 5s
84+
let check_times--
85+
echo "Check #" $check_times
86+
continue;
87+
fi
88+
89+
flag=0
90+
91+
for ((i=0; i<$num; i+=2))
92+
do
93+
if [ ${array[$i+1]} != $stopped ]; then
94+
flag=1
95+
break;
96+
fi
97+
done
98+
99+
if [ $flag -eq 1 ]; then
100+
sleep 5s
101+
let check_times--
102+
echo "Check #" $check_times
103+
else
104+
echo "VM status: " $vm_list
105+
break;
106+
fi
107+
108+
if [ $check_times -eq 0 ]; then
109+
echo "Timed out waiting for VMs..."
110+
break;
111+
fi
112+
done
113+
114+
if [ $check_times -gt 0 ]; then
115+
return 1;
116+
else
117+
return 0;
118+
fi
119+
}
120+
121+
check_post_vms_alive() {
122+
#check if there is any post-launched VM alive, and return if there is
123+
vm_list=$(acrnctl list)
124+
array=($vm_list)
125+
num=${#array[@]}
126+
127+
for ((i=0; i<$num; i+=2))
128+
do
129+
if [ ${array[$i+1]} == $started ]; then
130+
echo $vm_list " VM alive!"
131+
return 1
132+
fi
133+
done
134+
135+
echo "No VM alive: " $vm_list
136+
return 0
137+
}
138+
139+
if [ "$1" = "$SOS" ]; then
140+
try_times=2
141+
142+
#shut down post-launched VMs
143+
while [[ $try_times -gt 0 ]]
144+
do
145+
echo "Checking whether post-launched VMs are alive..."
146+
check_post_vms_alive
147+
if [ $? -eq 0 ]; then
148+
try_times=1
149+
break
150+
fi
151+
152+
echo "Powering off VMs..."
153+
power_off_post_vms
154+
if [ $? -eq 0 ]; then
155+
break
156+
fi
157+
158+
echo "Checking the status of post-launched VMs..."
159+
check_post_vms_status
160+
if [ $? -eq 1 ]; then
161+
break
162+
fi
163+
164+
let try_times--
165+
done
166+
167+
if [ $try_times -eq 0 ]; then
168+
echo "S5 failed!"
169+
exit
170+
fi
171+
172+
echo $(acrnctl list)
173+
174+
#send shutdown message to the pre-launched VM
175+
send_pre_message $SHUTDOWN
176+
#read ack message from the pre-launched VM
177+
read_pre_message
178+
echo "ret_msg: $ret_msg"
179+
180+
#check the ack message
181+
if [ "$ret_msg" = "$ACKED" ]; then
182+
echo "Received ACK message"
183+
else
184+
echo "Could not receive ACK message from the pre-launched User VM"
185+
exit
186+
fi
187+
188+
#all pre-launched and post-launched VMs have shut down
189+
echo "Shutting down the Service VM itself..."
190+
sleep 3s
191+
poweroff
192+
193+
elif [ "$1" = "$UOS" ]; then
194+
echo "Trying to open socket..."
195+
if ! exec 6<>/dev/tcp/$IP_ADDR/$SOCKET_PORT
196+
then
197+
echo "Failed to open socket"
198+
exit 1
199+
fi
200+
echo "Socket opened"
201+
202+
#send shutdown message
203+
send_message $SHUTDOWN
204+
#read ack message
205+
read_message
206+
echo "ret_msg: $ret_msg"
207+
208+
#check the ack message
209+
if [ "$ret_msg" = "$ACKED" ]; then
210+
echo "Received ACK message"
211+
else
212+
echo "Could not receive ACK message"
213+
fi
214+
else
215+
echo "Error! Please use: ./s5_trigger.sh sos OR ./s5_trigger.sh uos"
216+
fi

0 commit comments

Comments
 (0)