Skip to content

Commit d0d202f

Browse files
authored
refactor: Refactor GuestOS lifecycle management script (#5177)
Refactor GuestOS lifecycle management script Consolidate start and stop scripts into a single `guestos.sh` for improved lifecycle management. Benefits: - Single script that manages the lifetime of the VM. - The systemd status is tied to the virsh status. If the VM shuts down, systemd is notified right away. - No need for an additional script that keeps restarting the VM (kept monitor-guestos for now just for updating metrics).
1 parent 250941f commit d0d202f

File tree

6 files changed

+56
-76
lines changed

6 files changed

+56
-76
lines changed

ic-os/components/hostos-scripts/guestos/guestos.service

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,12 @@ Requires=generate-guestos-config.service
66
After=generate-guestos-config.service
77

88
[Service]
9-
Type=oneshot
10-
RemainAfterExit=true
9+
Type=notify
1110
ExecStartPre=/opt/ic/bin/detect-first-boot.sh
12-
ExecStart=/opt/ic/bin/start-guestos.sh
11+
ExecStart=/opt/ic/bin/guestos.sh
1312
ExecStartPost=/opt/ic/bin/manageboot.sh hostos confirm
14-
ExecStop=/opt/ic/bin/stop-guestos.sh
15-
Restart=on-failure
16-
RestartSec=300
13+
Restart=always
14+
RestartSec=60
1715

1816
[Install]
1917
WantedBy=multi-user.target

ic-os/components/hostos-scripts/guestos/start-guestos.sh renamed to ic-os/components/hostos-scripts/guestos/guestos.sh

Lines changed: 50 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,29 +9,29 @@ source /opt/ic/bin/metrics.sh
99

1010
SCRIPT="$(basename $0)[$$]"
1111

12-
# Get keyword arguments
12+
CONFIG="/var/lib/libvirt/guestos.xml"
13+
1314
for argument in "${@}"; do
1415
case ${argument} in
1516
-h | --help)
16-
echo 'Usage:
17-
Start GuestOS virtual machine
17+
echo "Usage:
18+
Run GuestOS virtual machine. The script starts the GuestOS virtual machine and
19+
keeps running indefinitely. If the VM is killed, the script exits with a failure.
20+
If the script's process is terminated with SIGTERM, the VM is stopped and the script
21+
exits with a success.
1822
1923
Arguments:
20-
-c=, --config= specify the GuestOS configuration file (Default: /var/lib/libvirt/guestos.xml)
2124
-h, --help show this help message and exit
22-
'
25+
"
2326
exit 1
2427
;;
2528
*)
26-
echo "Error: Argument is not supported."
29+
echo "Error: Argument '${argument}' is not supported."
2730
exit 1
2831
;;
2932
esac
3033
done
3134

32-
# Set arguments if undefined
33-
CONFIG="${CONFIG:=/var/lib/libvirt/guestos.xml}"
34-
3535
write_tty1_log() {
3636
local message=$1
3737

@@ -57,8 +57,31 @@ function define_guestos() {
5757
fi
5858
}
5959

60+
function is_guestos_running() {
61+
# Space around guestos to avoid matching guestos as substring in the VM name
62+
# (e.g. guestos-upgrader)
63+
virsh list --state-running | grep " guestos " >/dev/null
64+
}
65+
66+
function stop_guestos() {
67+
if is_guestos_running; then
68+
virsh destroy --graceful guestos
69+
write_log "Stopping GuestOS virtual machine."
70+
write_metric "hostos_guestos_service_stop" \
71+
"1" \
72+
"GuestOS virtual machine stop state" \
73+
"gauge"
74+
else
75+
write_log "GuestOS virtual machine is already stopped."
76+
write_metric "hostos_guestos_service_stop" \
77+
"0" \
78+
"GuestOS virtual machine stop state" \
79+
"gauge"
80+
fi
81+
}
82+
6083
function start_guestos() {
61-
if [ "$(virsh list --state-running | grep 'guestos')" ]; then
84+
if is_guestos_running; then
6285
write_log "GuestOS virtual machine is already running."
6386
write_metric "hostos_guestos_service_start" \
6487
"0" \
@@ -96,10 +119,12 @@ function start_guestos() {
96119
write_tty1_log "No /var/log/libvirt/qemu/guestos-serial.log file found."
97120
fi
98121

99-
write_tty1_log "Exiting start-guestos.sh so that systemd can restart guestos.service in 5 minutes."
122+
write_tty1_log "Exiting guestos.sh so that systemd can restart guestos.service."
100123
exit 1
101124
fi
102125

126+
trap "stop_guestos; exit 0" SIGTERM
127+
systemd-notify --ready
103128
sleep 10
104129
write_tty1_log ""
105130
write_tty1_log "#################################################"
@@ -117,10 +142,24 @@ function start_guestos() {
117142
fi
118143
}
119144

145+
function wait_for_vm_shutdown() {
146+
while true; do
147+
virsh event guestos lifecycle
148+
# When we terminate normally via systemd stop, the SIGTERM handler will shutdown the VM and this code isn't
149+
# reached.
150+
if ! is_guestos_running; then
151+
write_log "GuestOS VM shut down unexpectedly."
152+
systemd-notify --stopping --status="GuestOS VM shut down unexpectedly."
153+
exit 1
154+
fi
155+
done
156+
}
157+
120158
function main() {
121159
# Establish run order
122160
define_guestos
123161
start_guestos
162+
wait_for_vm_shutdown
124163
}
125164

126165
main

ic-os/components/hostos-scripts/guestos/stop-guestos.sh

Lines changed: 0 additions & 53 deletions
This file was deleted.

ic-os/components/hostos-scripts/monitoring/monitor-guestos.service

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
[Unit]
22
Description=Monitor GuestOS virtual machine
33
After=guestos.service
4-
Wants=guestos.service
54

65
[Service]
76
Type=oneshot

ic-os/components/hostos-scripts/monitoring/monitor-guestos.sh

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,11 @@ function monitor_guestos() {
4444
"GuestOS virtual machine state" \
4545
"gauge"
4646
else
47-
write_log "GuestOS virtual machine is not running, attempting to start."
47+
write_log "GuestOS virtual machine is not running."
4848
write_metric "hostos_guestos_state" \
4949
"2" \
5050
"GuestOS virtual machine state" \
5151
"gauge"
52-
53-
virsh start guestos
5452
fi
5553
}
5654

ic-os/components/hostos.bzl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ component_files = {
77
Label("hostos-scripts/generate-guestos-config/generate-guestos-config.sh"): "/opt/ic/bin/generate-guestos-config.sh",
88
Label("hostos-scripts/generate-guestos-config/generate-guestos-config.service"): "/etc/systemd/system/generate-guestos-config.service",
99
Label("hostos-scripts/guestos/guestos.service"): "/etc/systemd/system/guestos.service",
10-
Label("hostos-scripts/guestos/start-guestos.sh"): "/opt/ic/bin/start-guestos.sh",
11-
Label("hostos-scripts/guestos/stop-guestos.sh"): "/opt/ic/bin/stop-guestos.sh",
10+
Label("hostos-scripts/guestos/guestos.sh"): "/opt/ic/bin/guestos.sh",
1211
Label("hostos-scripts/guestos/guestos.xml.template"): "/opt/ic/share/guestos.xml.template",
1312
Label("hostos-scripts/libvirt/setup-libvirt.sh"): "/opt/ic/bin/setup-libvirt.sh",
1413
Label("hostos-scripts/libvirt/setup-libvirt.service"): "/etc/systemd/system/setup-libvirt.service",

0 commit comments

Comments
 (0)