Compare commits
4 commits
732a293857
...
2a5473eb22
| Author | SHA1 | Date | |
|---|---|---|---|
|
2a5473eb22 |
|||
|
ec613996a0 |
|||
|
325f1acff5 |
|||
|
c602725de9 |
12 changed files with 141 additions and 14 deletions
|
|
@ -430,6 +430,10 @@ components:
|
||||||
minItems: 1
|
minItems: 1
|
||||||
items:
|
items:
|
||||||
$ref: "#/components/schemas/VMStorage"
|
$ref: "#/components/schemas/VMStorage"
|
||||||
|
uefi:
|
||||||
|
type: boolean
|
||||||
|
description: Boot with UEFI firmware (OVMF). Defaults to false (SeaBIOS).
|
||||||
|
example: false
|
||||||
|
|
||||||
VMInterface:
|
VMInterface:
|
||||||
type: object
|
type: object
|
||||||
|
|
@ -487,6 +491,9 @@ components:
|
||||||
type: array
|
type: array
|
||||||
items:
|
items:
|
||||||
$ref: "#/components/schemas/VMStorage"
|
$ref: "#/components/schemas/VMStorage"
|
||||||
|
uefi:
|
||||||
|
type: boolean
|
||||||
|
example: false
|
||||||
|
|
||||||
Error:
|
Error:
|
||||||
type: object
|
type: object
|
||||||
|
|
|
||||||
|
|
@ -39,6 +39,18 @@ interfaces:
|
||||||
metadata:
|
metadata:
|
||||||
run_dir: "/run/two/metadata"
|
run_dir: "/run/two/metadata"
|
||||||
|
|
||||||
|
# QEMU runtime paths
|
||||||
|
qemu:
|
||||||
|
# UEFI firmware (requires apt install ovmf on Debian/Ubuntu)
|
||||||
|
ovmf_code_path: "/usr/share/OVMF/OVMF_CODE.fd"
|
||||||
|
ovmf_vars_template: "/usr/share/OVMF/OVMF_VARS.fd"
|
||||||
|
# Per-VM UEFI variable store (writable copy, created at start / deleted at stop)
|
||||||
|
uefi_vars_dir: "/run/two/vms/efi"
|
||||||
|
# QEMU Unix socket directories
|
||||||
|
serial_dir: "/run/two/vms/serial"
|
||||||
|
monitor_dir: "/run/two/vms/monitor"
|
||||||
|
qmp_dir: "/run/two/vms/qmp"
|
||||||
|
|
||||||
# Admin API (read-only DB inspection, loopback only)
|
# Admin API (read-only DB inspection, loopback only)
|
||||||
admin:
|
admin:
|
||||||
enabled: false
|
enabled: false
|
||||||
|
|
|
||||||
|
|
@ -49,6 +49,7 @@ type VMCreateRequest struct {
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
Memory int `json:"memory"`
|
Memory int `json:"memory"`
|
||||||
CPUs int `json:"cpus"`
|
CPUs int `json:"cpus"`
|
||||||
|
UEFI bool `json:"uefi"`
|
||||||
Password string `json:"password"`
|
Password string `json:"password"`
|
||||||
SSHKey string `json:"sshkey"`
|
SSHKey string `json:"sshkey"`
|
||||||
Interfaces []VMInterface `json:"interfaces"`
|
Interfaces []VMInterface `json:"interfaces"`
|
||||||
|
|
@ -61,6 +62,7 @@ type VM struct {
|
||||||
MetadataPort string `json:"metadata_port"`
|
MetadataPort string `json:"metadata_port"`
|
||||||
Memory int `json:"memory"`
|
Memory int `json:"memory"`
|
||||||
CPUs int `json:"cpus"`
|
CPUs int `json:"cpus"`
|
||||||
|
UEFI bool `json:"uefi"`
|
||||||
Interfaces []VMInterface `json:"interfaces"`
|
Interfaces []VMInterface `json:"interfaces"`
|
||||||
Storage []VMStorage `json:"storage"`
|
Storage []VMStorage `json:"storage"`
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -79,6 +79,7 @@ func vmFromDB(name string, entries map[string]string) (VM, error) {
|
||||||
vm.MetadataPort = entries[prefix+"metadata_port"]
|
vm.MetadataPort = entries[prefix+"metadata_port"]
|
||||||
vm.Memory, _ = strconv.Atoi(entries[prefix+"memory"])
|
vm.Memory, _ = strconv.Atoi(entries[prefix+"memory"])
|
||||||
vm.CPUs, _ = strconv.Atoi(entries[prefix+"cpus"])
|
vm.CPUs, _ = strconv.Atoi(entries[prefix+"cpus"])
|
||||||
|
vm.UEFI = entries[prefix+"uefi"] == "true"
|
||||||
|
|
||||||
subnet := entries[prefix+"subnet"]
|
subnet := entries[prefix+"subnet"]
|
||||||
ip := entries[prefix+"ip"]
|
ip := entries[prefix+"ip"]
|
||||||
|
|
|
||||||
|
|
@ -84,6 +84,7 @@ func (s *Server) startVM(w http.ResponseWriter, r *http.Request) {
|
||||||
VolumePath: req.Storage[0].Path,
|
VolumePath: req.Storage[0].Path,
|
||||||
Memory: req.Memory,
|
Memory: req.Memory,
|
||||||
CPUs: req.CPUs,
|
CPUs: req.CPUs,
|
||||||
|
UEFI: req.UEFI,
|
||||||
Password: req.Password,
|
Password: req.Password,
|
||||||
SSHKey: req.SSHKey,
|
SSHKey: req.SSHKey,
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -36,6 +36,14 @@ type Config struct {
|
||||||
Address string `mapstructure:"address"`
|
Address string `mapstructure:"address"`
|
||||||
Port int `mapstructure:"port"`
|
Port int `mapstructure:"port"`
|
||||||
} `mapstructure:"admin"`
|
} `mapstructure:"admin"`
|
||||||
|
QEMU struct {
|
||||||
|
OVMFCodePath string `mapstructure:"ovmf_code_path"`
|
||||||
|
OVMFVarsTemplate string `mapstructure:"ovmf_vars_template"`
|
||||||
|
UEFIVarsDir string `mapstructure:"uefi_vars_dir"`
|
||||||
|
SerialDir string `mapstructure:"serial_dir"`
|
||||||
|
MonitorDir string `mapstructure:"monitor_dir"`
|
||||||
|
QMPDir string `mapstructure:"qmp_dir"`
|
||||||
|
} `mapstructure:"qemu"`
|
||||||
DefaultInterface string `mapstructure:"default_interface"`
|
DefaultInterface string `mapstructure:"default_interface"`
|
||||||
Interfaces map[string]string `mapstructure:"interfaces"`
|
Interfaces map[string]string `mapstructure:"interfaces"`
|
||||||
}
|
}
|
||||||
|
|
@ -55,6 +63,12 @@ func LoadConfig(path string) (*Config, error) {
|
||||||
v.SetDefault("dispatcher.timeout_seconds", 300)
|
v.SetDefault("dispatcher.timeout_seconds", 300)
|
||||||
v.SetDefault("dispatcher.poll_seconds", 2)
|
v.SetDefault("dispatcher.poll_seconds", 2)
|
||||||
v.SetDefault("metadata.run_dir", "/run/two/metadata")
|
v.SetDefault("metadata.run_dir", "/run/two/metadata")
|
||||||
|
v.SetDefault("qemu.ovmf_code_path", "/usr/share/OVMF/OVMF_CODE.fd")
|
||||||
|
v.SetDefault("qemu.ovmf_vars_template", "/usr/share/OVMF/OVMF_VARS.fd")
|
||||||
|
v.SetDefault("qemu.uefi_vars_dir", "/run/two/vms/uefi")
|
||||||
|
v.SetDefault("qemu.serial_dir", "/run/two/vms/serial")
|
||||||
|
v.SetDefault("qemu.monitor_dir", "/run/two/vms/monitor")
|
||||||
|
v.SetDefault("qemu.qmp_dir", "/run/two/vms/qmp")
|
||||||
v.SetDefault("admin.enabled", false)
|
v.SetDefault("admin.enabled", false)
|
||||||
v.SetDefault("admin.address", "127.0.0.1")
|
v.SetDefault("admin.address", "127.0.0.1")
|
||||||
v.SetDefault("admin.port", 9091)
|
v.SetDefault("admin.port", 9091)
|
||||||
|
|
|
||||||
|
|
@ -20,6 +20,7 @@ type StartVMCommand struct {
|
||||||
VolumePath string
|
VolumePath string
|
||||||
Memory int
|
Memory int
|
||||||
CPUs int
|
CPUs int
|
||||||
|
UEFI bool
|
||||||
Password string
|
Password string
|
||||||
SSHKey string
|
SSHKey string
|
||||||
}
|
}
|
||||||
|
|
@ -46,6 +47,9 @@ func (c StartVMCommand) Prepare(db *badger.DB, _ *configuration.Config) error {
|
||||||
kv.AddInDB(db, "vm/"+c.Name+"/volume_path", c.VolumePath)
|
kv.AddInDB(db, "vm/"+c.Name+"/volume_path", c.VolumePath)
|
||||||
kv.AddInDB(db, "vm/"+c.Name+"/memory", strconv.Itoa(c.Memory))
|
kv.AddInDB(db, "vm/"+c.Name+"/memory", strconv.Itoa(c.Memory))
|
||||||
kv.AddInDB(db, "vm/"+c.Name+"/cpus", strconv.Itoa(c.CPUs))
|
kv.AddInDB(db, "vm/"+c.Name+"/cpus", strconv.Itoa(c.CPUs))
|
||||||
|
if c.UEFI {
|
||||||
|
kv.AddInDB(db, "vm/"+c.Name+"/uefi", "true")
|
||||||
|
}
|
||||||
if c.Password != "" {
|
if c.Password != "" {
|
||||||
kv.AddInDB(db, "vm/"+c.Name+"/password", c.Password)
|
kv.AddInDB(db, "vm/"+c.Name+"/password", c.Password)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,9 @@ package qemu
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
|
"path/filepath"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Config struct {
|
type Config struct {
|
||||||
|
|
@ -14,6 +16,11 @@ type Config struct {
|
||||||
VolumePath string
|
VolumePath string
|
||||||
Memory int
|
Memory int
|
||||||
CPUs int
|
CPUs int
|
||||||
|
UEFICodePath string
|
||||||
|
UEFIVarsPath string
|
||||||
|
SerialDir string
|
||||||
|
MonitorDir string
|
||||||
|
QMPDir string
|
||||||
}
|
}
|
||||||
|
|
||||||
func Start(cfg Config) error {
|
func Start(cfg Config) error {
|
||||||
|
|
@ -27,20 +34,44 @@ func Start(cfg Config) error {
|
||||||
cpus = 1
|
cpus = 1
|
||||||
}
|
}
|
||||||
|
|
||||||
cmd := exec.Command("qemu-system-x86_64",
|
for _, dir := range []string{cfg.SerialDir, cfg.MonitorDir, cfg.QMPDir} {
|
||||||
|
if dir != "" {
|
||||||
|
if err := os.MkdirAll(dir, 0755); err != nil {
|
||||||
|
return fmt.Errorf("mkdir %s: %w", dir, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
serialSock := filepath.Join(cfg.SerialDir, cfg.Name+".sock")
|
||||||
|
monitorSock := filepath.Join(cfg.MonitorDir, cfg.Name+".sock")
|
||||||
|
qmpSock := filepath.Join(cfg.QMPDir, cfg.Name+".sock")
|
||||||
|
|
||||||
|
args := []string{
|
||||||
"-enable-kvm",
|
"-enable-kvm",
|
||||||
"-cpu", "host",
|
"-cpu", "host",
|
||||||
"-m", fmt.Sprintf("%d", memory),
|
"-m", fmt.Sprintf("%d", memory),
|
||||||
"-smp", fmt.Sprintf("%d", cpus),
|
"-smp", fmt.Sprintf("%d", cpus),
|
||||||
"-serial", fmt.Sprintf("unix:/tmp/%s.sock,server,nowait", cfg.Name),
|
"-serial", fmt.Sprintf("unix:%s,server,nowait", serialSock),
|
||||||
"-monitor", fmt.Sprintf("unix:/tmp/%s.mon-sock,server,nowait", cfg.Name),
|
"-monitor", fmt.Sprintf("unix:%s,server,nowait", monitorSock),
|
||||||
"-qmp", fmt.Sprintf("unix:/tmp/%s.qmp-sock,server,nowait", cfg.Name),
|
"-qmp", fmt.Sprintf("unix:%s,server,nowait", qmpSock),
|
||||||
"-display", "none",
|
"-display", "none",
|
||||||
|
}
|
||||||
|
|
||||||
|
if cfg.UEFICodePath != "" && cfg.UEFIVarsPath != "" {
|
||||||
|
args = append(args,
|
||||||
|
"-drive", fmt.Sprintf("if=pflash,format=raw,readonly=on,file=%s", cfg.UEFICodePath),
|
||||||
|
"-drive", fmt.Sprintf("if=pflash,format=raw,file=%s", cfg.UEFIVarsPath),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
args = append(args,
|
||||||
"-drive", fmt.Sprintf("file=%s,if=virtio", cfg.VolumePath),
|
"-drive", fmt.Sprintf("file=%s,if=virtio", cfg.VolumePath),
|
||||||
"-netdev", fmt.Sprintf("tap,id=net0,ifname=tap%d,script=no,downscript=no", cfg.TapID),
|
"-netdev", fmt.Sprintf("tap,id=net0,ifname=tap%d,script=no,downscript=no", cfg.TapID),
|
||||||
"-device", fmt.Sprintf("virtio-net-pci,netdev=net0,mac=%s", cfg.Mac),
|
"-device", fmt.Sprintf("virtio-net-pci,netdev=net0,mac=%s", cfg.Mac),
|
||||||
"-daemonize",
|
"-daemonize",
|
||||||
)
|
)
|
||||||
|
|
||||||
|
cmd := exec.Command("qemu-system-x86_64", args...)
|
||||||
if err := cmd.Run(); err != nil {
|
if err := cmd.Run(); err != nil {
|
||||||
return fmt.Errorf("qemu-system-x86_64: %w", err)
|
return fmt.Errorf("qemu-system-x86_64: %w", err)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -7,6 +7,11 @@ import "errors"
|
||||||
type Config struct {
|
type Config struct {
|
||||||
Name, Mac, VolumePath string
|
Name, Mac, VolumePath string
|
||||||
TapID, Memory, CPUs int
|
TapID, Memory, CPUs int
|
||||||
|
UEFICodePath string
|
||||||
|
UEFIVarsPath string
|
||||||
|
SerialDir string
|
||||||
|
MonitorDir string
|
||||||
|
QMPDir string
|
||||||
}
|
}
|
||||||
|
|
||||||
func Start(_ Config) error {
|
func Start(_ Config) error {
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,9 @@ package vm
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
|
||||||
configuration "git.g3e.fr/syonad/two/internal/config/agent"
|
configuration "git.g3e.fr/syonad/two/internal/config/agent"
|
||||||
"git.g3e.fr/syonad/two/internal/iptables"
|
"git.g3e.fr/syonad/two/internal/iptables"
|
||||||
|
|
@ -49,18 +52,54 @@ func StartVM(db *badger.DB, name string, cfg *configuration.Config) error {
|
||||||
return fmt.Errorf("start metadata: %w", err)
|
return fmt.Errorf("start metadata: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := netns.Call(d.vpcName, func() error {
|
qcfg := qemu.Config{
|
||||||
return qemu.Start(qemu.Config{
|
|
||||||
Name: name,
|
Name: name,
|
||||||
TapID: d.tapID,
|
TapID: d.tapID,
|
||||||
Mac: d.mac,
|
Mac: d.mac,
|
||||||
VolumePath: d.volumePath,
|
VolumePath: d.volumePath,
|
||||||
Memory: d.memory,
|
Memory: d.memory,
|
||||||
CPUs: d.cpus,
|
CPUs: d.cpus,
|
||||||
})
|
SerialDir: cfg.QEMU.SerialDir,
|
||||||
|
MonitorDir: cfg.QEMU.MonitorDir,
|
||||||
|
QMPDir: cfg.QEMU.QMPDir,
|
||||||
|
}
|
||||||
|
|
||||||
|
if d.uefi {
|
||||||
|
varsPath := filepath.Join(cfg.QEMU.UEFIVarsDir, name+"-uefi-vars.fd")
|
||||||
|
if err := copyFile(cfg.QEMU.OVMFVarsTemplate, varsPath); err != nil {
|
||||||
|
return fmt.Errorf("copy uefi vars: %w", err)
|
||||||
|
}
|
||||||
|
qcfg.UEFICodePath = cfg.QEMU.OVMFCodePath
|
||||||
|
qcfg.UEFIVarsPath = varsPath
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := netns.Call(d.vpcName, func() error {
|
||||||
|
return qemu.Start(qcfg)
|
||||||
}); err != nil {
|
}); err != nil {
|
||||||
return fmt.Errorf("start qemu: %w", err)
|
return fmt.Errorf("start qemu: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return kv.AddInDB(db, "vm/"+name+"/state", "started")
|
return kv.AddInDB(db, "vm/"+name+"/state", "started")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func copyFile(src, dst string) error {
|
||||||
|
if err := os.MkdirAll(filepath.Dir(dst), 0755); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
in, err := os.Open(src)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer in.Close()
|
||||||
|
|
||||||
|
out, err := os.Create(dst)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer out.Close()
|
||||||
|
|
||||||
|
if _, err := io.Copy(out, in); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return out.Sync()
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -23,6 +23,7 @@ type vmData struct {
|
||||||
volumePath string
|
volumePath string
|
||||||
memory int
|
memory int
|
||||||
cpus int
|
cpus int
|
||||||
|
uefi bool
|
||||||
password string
|
password string
|
||||||
sshkey string
|
sshkey string
|
||||||
}
|
}
|
||||||
|
|
@ -105,6 +106,9 @@ func loadVM(db *badger.DB, name string) (vmData, error) {
|
||||||
return d, fmt.Errorf("parse cpus: %w", err)
|
return d, fmt.Errorf("parse cpus: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if v, _ := kv.GetFromDB(db, "vm/"+name+"/uefi"); v == "true" {
|
||||||
|
d.uefi = true
|
||||||
|
}
|
||||||
d.password, _ = kv.GetFromDB(db, "vm/"+name+"/password")
|
d.password, _ = kv.GetFromDB(db, "vm/"+name+"/password")
|
||||||
d.sshkey, _ = kv.GetFromDB(db, "vm/"+name+"/sshkey")
|
d.sshkey, _ = kv.GetFromDB(db, "vm/"+name+"/sshkey")
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,8 @@ package vm
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
configuration "git.g3e.fr/syonad/two/internal/config/agent"
|
configuration "git.g3e.fr/syonad/two/internal/config/agent"
|
||||||
|
|
@ -29,7 +31,7 @@ func StopVM(db *badger.DB, name string, cfg *configuration.Config) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
socketPath := fmt.Sprintf("/tmp/%s.qmp-sock", name)
|
socketPath := filepath.Join(cfg.QEMU.QMPDir, name+".sock")
|
||||||
|
|
||||||
if _, err := qmp.Send(socketPath, []string{`{"execute":"system_powerdown"}`}); err != nil {
|
if _, err := qmp.Send(socketPath, []string{`{"execute":"system_powerdown"}`}); err != nil {
|
||||||
return fmt.Errorf("qmp system_powerdown: %w", err)
|
return fmt.Errorf("qmp system_powerdown: %w", err)
|
||||||
|
|
@ -65,5 +67,10 @@ func StopVM(db *badger.DB, name string, cfg *configuration.Config) error {
|
||||||
return fmt.Errorf("delete tap: %w", err)
|
return fmt.Errorf("delete tap: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if d.uefi {
|
||||||
|
varsPath := filepath.Join(cfg.QEMU.UEFIVarsDir, name+"-uefi-vars.fd")
|
||||||
|
os.Remove(varsPath)
|
||||||
|
}
|
||||||
|
|
||||||
return kv.AddInDB(db, "vm/"+name+"/state", "stopped")
|
return kv.AddInDB(db, "vm/"+name+"/state", "stopped")
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue