Files
collabvm-1.2.ts/cvmts/src/vm/qemu.ts
modeco80 e839f7f5aa better display/vm stuff
- moved superqemu's "QemuDisplay" here; the VNC VM and Qemu both share it (and it has been renamed to a less goofy dumb name)

- VNC VM has been heavily refactored to just use the VNC display we have (this means only one source of truth, less bugs, and it's generally just Better to share the code imho). this means that future plans to abstract this further (or implement the client in cvm-rs in general) won't cause any explosions, or require duplicate effort

- vms are now in src/vm/... just better organization

- superqemu doesn't manage a display anymore (or care about it, other than making sure the socket is unlinked on stop). Instead now it provides info for us to setup our own VNC client. This is also why we provide our own shim interface

This currently relies on a alpha version of superqemu.

Before this is merged into cvmts main I will publish a stable tag and point cvmts to that new version
2024-08-23 07:26:23 -04:00

90 lines
1.8 KiB
TypeScript

import EventEmitter from 'events';
import VM from './interface.js';
import { QemuVM, QemuVmDefinition, VMState } from '@computernewb/superqemu';
import { VMDisplay } from '../display/interface.js';
import { VncDisplay } from '../display/vnc.js';
import pino from 'pino';
// shim over superqemu because it diverges from the VM interface
export class QemuVMShim implements VM {
private vm;
private display: VncDisplay | null = null;
private logger;
constructor(def: QemuVmDefinition) {
this.vm = new QemuVM(def);
this.logger = pino({ name: `CVMTS.QemuVMShim/${def.id}` });
}
Start(): Promise<void> {
return this.vm.Start();
}
async Stop(): Promise<void> {
await this.vm.Stop();
this.display?.Disconnect();
this.display = null;
}
Reboot(): Promise<void> {
return this.vm.Reboot();
}
Reset(): Promise<void> {
return this.vm.Reset();
}
MonitorCommand(command: string): Promise<any> {
return this.vm.MonitorCommand(command);
}
StartDisplay(): void {
// boot it up
let info = this.vm.GetDisplayInfo();
if (info == null) throw new Error('its dead jim');
switch (info.type) {
case 'vnc-tcp':
this.display = new VncDisplay({
host: info.host || '127.0.0.1',
port: info.port || 5900,
path: null
});
break;
case 'vnc-uds':
this.display = new VncDisplay({
path: info.path
});
break;
}
let self = this;
this.display?.on('connected', () => {
// The VM can now be considered started
self.logger.info('Display connected');
});
// now that QMP has connected, connect to the display
self.display?.Connect();
}
GetDisplay(): VMDisplay | null {
return this.display;
}
GetState(): VMState {
return this.vm.GetState();
}
SnapshotsSupported(): boolean {
return this.vm.SnapshotsSupported();
}
Events(): EventEmitter {
return this.vm;
}
}