34 lines
1.3 KiB
JavaScript
34 lines
1.3 KiB
JavaScript
const http = require('http');
|
|
const child = require("child_process");
|
|
let makedisk = "qemu-img create vm.img 30G" // make a 30GB disk
|
|
let qemucmd = "qemu-system-x86_64 -vnc :1 -m 1G -hda vm.img"
|
|
child.execSync(makedisk)
|
|
child.execSync(qemucmd)
|
|
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.")
|
|
}); |