port randomization added for VNC + SSH on run
This commit is contained in:
parent
04ee6698df
commit
1e7135b2d2
3 changed files with 20 additions and 12 deletions
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
|
|
@ -0,0 +1 @@
|
|||
*.pub
|
||||
29
main.go
29
main.go
|
|
@ -4,10 +4,10 @@ import (
|
|||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
|
|
@ -48,7 +48,7 @@ var osImages = map[string]OSImage{
|
|||
},
|
||||
"alpine": {
|
||||
Name: "alpine",
|
||||
URL: "https://dl-cdn.alpinelinux.org/alpine/v3.22/releases/cloud/generic_alpine-3.22.2-x86_64-uefi-cloudinit-r0.qcow2",
|
||||
URL: "https://dl-cdn.alpinelinux.org/alpine/v3.22/releases/cloud/generic_alpine-3.22.2-x86_64-bios-cloudinit-r0.qcow2",
|
||||
Filename: "alpine-3.22.qcow2",
|
||||
User: "alpine",
|
||||
},
|
||||
|
|
@ -121,14 +121,15 @@ Type YES (all caps) to confirm: `, name, dir)
|
|||
}
|
||||
|
||||
|
||||
func sshPort(name string) int {
|
||||
if strings.HasPrefix(name, "vm") {
|
||||
if n, err := strconv.Atoi(strings.TrimPrefix(name, "vm")); err == nil {
|
||||
return 2220 + n
|
||||
func findFreePort() int {
|
||||
l, err := net.Listen("tcp", "127.0.0.1:0")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer l.Close()
|
||||
return l.Addr().(*net.TCPAddr).Port
|
||||
}
|
||||
return 2222
|
||||
}
|
||||
|
||||
|
||||
func parseArg(args []string, key string) string {
|
||||
for i := 0; i < len(args)-1; i++ {
|
||||
|
|
@ -256,7 +257,13 @@ func startVM(name string) {
|
|||
os.Remove(pidFile(name))
|
||||
}
|
||||
|
||||
port := sshPort(name)
|
||||
port := findFreePort()
|
||||
vncPort := findFreePort()
|
||||
vncDisplay := vncPort - 5900
|
||||
if vncDisplay < 0 {
|
||||
panic("invalid VNC port allocation")
|
||||
}
|
||||
|
||||
|
||||
cmd := exec.Command(
|
||||
"qemu-system-x86_64",
|
||||
|
|
@ -267,7 +274,7 @@ func startVM(name string) {
|
|||
"-smp", cpus,
|
||||
|
||||
"-vga", "virtio",
|
||||
"-display", "vnc=0.0.0.0:1",
|
||||
"-display", fmt.Sprintf("vnc=0.0.0.0:%d", vncDisplay),
|
||||
|
||||
"-drive", fmt.Sprintf("file=%s,if=virtio,format=qcow2", diskPath(name)),
|
||||
"-drive", fmt.Sprintf("file=%s,if=virtio,format=raw", seedISOPath(name)),
|
||||
|
|
@ -285,7 +292,7 @@ func startVM(name string) {
|
|||
}
|
||||
|
||||
fmt.Println("VM started:", name)
|
||||
fmt.Println("VNC: localhost:5901")
|
||||
fmt.Printf("VNC: vnc://127.0.0.1:%d\n", vncPort)
|
||||
fmt.Printf("SSH: ssh <user>@localhost -p %d\n", port)
|
||||
}
|
||||
|
||||
|
|
|
|||
BIN
microvm
Executable file
BIN
microvm
Executable file
Binary file not shown.
Loading…
Add table
Add a link
Reference in a new issue