FreeVM-Server-KVM/start-linux.js

34 lines
1.4 KiB
JavaScript
Raw Permalink Normal View History

2024-09-20 15:26:05 -04:00
const http = require('http');
const child = require("child_process");
2024-09-20 15:32:13 -04:00
let makedisk = "qemu-img create vm.img 30G" // make a 30GB disk
2024-09-20 15:53:03 -04:00
let qemucmd = "qemu-system-x86_64 -vnc :1 -m 1G -hda vm.img -accel kvm"
2024-09-20 15:32:13 -04:00
child.execSync(makedisk)
2024-09-20 15:29:30 -04:00
child.execSync(qemucmd)
2024-09-20 16:58:39 -04:00
const server = http.createServer((req, res) => {
// Check if the request is a POST to /stop
if (req.method === 'POST' && req.url === '/stop') {
// Execute the pkill command to stop Node.js processes
exec('pkill qemu*', (error, stdout, stderr) => {
if (error) {
console.error(`Error executing command: ${error.message}`);
res.writeHead(500);
res.end(JSON.stringify({ message: 'Failed to stop the server' }));
return;
}
console.log('QEMU processes terminated');
res.writeHead(200);
res.end(JSON.stringify({ message: 'Server is stopping' }));
});
} else {
// Handle 404 for other routes
res.writeHead(404);
res.end(JSON.stringify({ message: 'Not Found' }));
}
});
// Start the server on port 3000
const PORT = 8080;
server.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
console.log("QEMU and FreeVM are running. VNC into it using 127.0.0.1 to VNC into it and go to localhost:8080/stop to stop the VM.")
});