f-32: vm: fix incomplete stop vm
All checks were successful
Pre Release Workflow / set-release-target (push) Successful in 1s
Pre Release Workflow / build (agent, amd64, linux) (push) Successful in 1m36s
Pre Release Workflow / build (metadata, amd64, linux) (push) Successful in 2m19s
Pre Release Workflow / upload-scripts (run-dnsmasq-in-netns.sh) (push) Successful in 6s
Pre Release Workflow / prerelease (push) Successful in 11s
All checks were successful
Pre Release Workflow / set-release-target (push) Successful in 1s
Pre Release Workflow / build (agent, amd64, linux) (push) Successful in 1m36s
Pre Release Workflow / build (metadata, amd64, linux) (push) Successful in 2m19s
Pre Release Workflow / upload-scripts (run-dnsmasq-in-netns.sh) (push) Successful in 6s
Pre Release Workflow / prerelease (push) Successful in 11s
Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
This commit is contained in:
parent
c3f26836cd
commit
4ab880a32d
1 changed files with 25 additions and 17 deletions
|
|
@ -33,25 +33,18 @@ func StopVM(db *badger.DB, name string, cfg *configuration.Config) error {
|
|||
|
||||
socketPath := filepath.Join(cfg.QEMU.QMPDir, name+".sock")
|
||||
|
||||
if _, err := qmp.Send(socketPath, []string{`{"execute":"system_powerdown"}`}); err != nil {
|
||||
return fmt.Errorf("qmp system_powerdown: %w", err)
|
||||
if _, err := os.Stat(socketPath); err == nil {
|
||||
// socket présent : tenter l'arrêt gracieux
|
||||
if _, err := qmp.Send(socketPath, []string{`{"execute":"system_powerdown"}`}); err == nil {
|
||||
waitQMPDead(socketPath,
|
||||
time.Duration(cfg.Dispatcher.TimeoutSeconds)*time.Second,
|
||||
time.Duration(cfg.Dispatcher.PollSeconds)*time.Second,
|
||||
)
|
||||
}
|
||||
// connexion QMP échouée : QEMU déjà mort
|
||||
}
|
||||
// socket absent ou QEMU déjà arrêté : cleanup direct
|
||||
|
||||
// attendre l'arrêt effectif de la VM ; forcer via quit après timeout
|
||||
timeout := time.After(time.Duration(cfg.Dispatcher.TimeoutSeconds) * time.Second)
|
||||
poll := time.Duration(cfg.Dispatcher.PollSeconds) * time.Second
|
||||
stopped := false
|
||||
for !stopped {
|
||||
select {
|
||||
case <-timeout:
|
||||
qmp.Send(socketPath, []string{`{"execute":"quit"}`})
|
||||
stopped = true
|
||||
case <-time.After(poll):
|
||||
if _, err := qmp.Send(socketPath, nil); err != nil {
|
||||
stopped = true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if err := netns.Call(d.vpcName, func() error {
|
||||
return iptables.DeleteMetadataRedirect(d.ip, d.interfaceIP, d.metadataPort)
|
||||
|
|
@ -74,3 +67,18 @@ func StopVM(db *badger.DB, name string, cfg *configuration.Config) error {
|
|||
|
||||
return kv.AddInDB(db, "vm/"+name+"/state", "stopped")
|
||||
}
|
||||
|
||||
func waitQMPDead(socketPath string, timeout, poll time.Duration) {
|
||||
timer := time.After(timeout)
|
||||
for {
|
||||
select {
|
||||
case <-timer:
|
||||
qmp.Send(socketPath, []string{`{"execute":"quit"}`})
|
||||
return
|
||||
case <-time.After(poll):
|
||||
if _, err := qmp.Send(socketPath, nil); err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue