From 0df56cb5a43d8a7faa43d2fdef0b72fa424ebe43 Mon Sep 17 00:00:00 2001 From: modeco80 Date: Sun, 23 Jun 2024 02:40:13 -0400 Subject: [PATCH] qemu: cleanup/fix reset --- qemu/src/QemuVM.ts | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/qemu/src/QemuVM.ts b/qemu/src/QemuVM.ts index 6afdadc..27c6704 100644 --- a/qemu/src/QemuVM.ts +++ b/qemu/src/QemuVM.ts @@ -36,7 +36,6 @@ export class QemuVM extends EventEmitter { private qmpFailCount = 0; private qemuProcess: ExecaChildProcess | null = null; - private qemuRunning = false; private display: QemuDisplay; private definition: QemuVmDefinition; @@ -54,12 +53,12 @@ export class QemuVM extends EventEmitter { async Start() { // Don't start while either trying to start or starting. - if (this.state == VMState.Started || this.state == VMState.Starting) return; + //if (this.state == VMState.Started || this.state == VMState.Starting) return; + if(this.qemuProcess) return; let cmd = this.definition.command; - // build additional command line statements to enable qmp/vnc over unix sockets - // FIXME: Still use TCP if on Windows. + // Build additional command line statements to enable qmp/vnc over unix sockets if (!this.addedAdditionalArguments) { cmd += ' -no-shutdown'; if (this.definition.snapshot) cmd += ' -snapshot'; @@ -101,8 +100,13 @@ export class QemuVM extends EventEmitter { // let code know the VM is going to reset this.emit('reset'); - // Do magic. - await this.StopQemu(); + if(this.qemuProcess !== null) { + // Do magic. + await this.StopQemu(); + } else { + // N.B we always get here when addl. arguments are added + await this.StartQemu(this.definition.command); + } } async QmpCommand(command: string, args: any | null): Promise { @@ -150,9 +154,8 @@ export class QemuVM extends EventEmitter { this.state = state; this.emit('statechange', this.state); - // reset some state when starting the vm back up - // to avoid potentional issues. - if(this.state == VMState.Starting) { + // reset QMP fail count when the VM is (re)starting or stopped + if(this.state == VMState.Stopped || this.state == VMState.Starting) { this.qmpFailCount = 0; } } @@ -177,14 +180,12 @@ export class QemuVM extends EventEmitter { this.qemuProcess.on('spawn', async () => { self.VMLog().Info("QEMU started"); - self.qemuRunning = true; await Shared.Sleep(500); await self.ConnectQmp(); }); this.qemuProcess.on('exit', async (code) => { self.VMLog().Info("QEMU process exited"); - self.qemuRunning = false; // this should be being done anways but it's very clearly not sometimes so @@ -222,7 +223,10 @@ export class QemuVM extends EventEmitter { } private async StopQemu() { - if (this.qemuRunning == true) this.qemuProcess?.kill('SIGTERM'); + if (this.qemuProcess) { + this.qemuProcess?.kill('SIGTERM'); + this.qemuProcess = null; + } } private async ConnectQmp() { @@ -291,7 +295,6 @@ export class QemuVM extends EventEmitter { private async DisconnectDisplay() { try { this.display?.Disconnect(); - //this.display = null; // disassociate with that display object. } catch (err) { // oh well lol }