My approach, a simple program in golang. It can be run by a script at Unraid startup.
./vmhook 192.168.1.2:8151 windows10
At http://192.168.1.2:8151 there will be a “start” button, below is the code and the compiled version of the program. You can add other behavior or other buttons and build the program yourself.
// Command to build program:
// go build -trimpath -ldflags "-s -w" -o vmhook main.go
package main
import (
"fmt"
"net/http"
"os"
"os/exec"
)
func main() {
if len(os.Args) < 3 {
fmt.Println("Usage: ./vmhook <address>:<port> <VM_name>")
return
}
addr := os.Args[1]
vmName := os.Args[2]
// Handler for the index page
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
// Serve a simple HTML page with a button
html := `
<html>
<head>
<title>Start Windows10 VM</title>
</head>
<body>
<form action="/start" method="post">
<input type="submit" value="Start">
</form>
</body>
</html>
`
fmt.Fprintf(w, html)
})
// Handler for the start button
http.HandleFunc("/start", func(w http.ResponseWriter, r *http.Request) {
// Execute the command "virsh start windows10"
cmd := exec.Command("virsh", "start", vmName)
err := cmd.Run()
if err != nil {
fmt.Fprintf(w, "Error starting VM: %v", err)
return
}
fmt.Fprintf(w, vmName+" VM has been started successfully")
})
// Start the HTTP server
fmt.Printf("Server is starting on %s\n", addr)
err := http.ListenAndServe(addr, nil)
if err != nil {
fmt.Printf("Server failed to start: %v\n", err)
}
}
vmhook