f-62: vm: MAC fournie à la création par interface, fin de la table préchargée, migration #62

Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
This commit is contained in:
GnomeZworc 2026-10-19 21:25:18 +02:00
commit 077141443e
Signed by: nicolas.boufideline
GPG key ID: 4406BBBF8845D632
20 changed files with 338 additions and 171 deletions

View file

@ -2,7 +2,6 @@ package dhcp
import (
"fmt"
"net"
"os"
"path/filepath"
"strings"
@ -10,9 +9,9 @@ import (
"git.g3e.fr/syonad/two/internal/metadata"
)
func GenerateConfig(c Config) (string, map[string]string, error) {
func GenerateConfig(c Config) (string, error) {
if c.InterfaceIP == nil {
return "", nil, fmt.Errorf("interface ip is required: guests would have no route to the metadata server")
return "", fmt.Errorf("interface ip is required: guests would have no route to the metadata server")
}
mask := fmt.Sprintf("%d.%d.%d.%d", c.Network.Mask[0], c.Network.Mask[1], c.Network.Mask[2], c.Network.Mask[3])
@ -35,26 +34,14 @@ func GenerateConfig(c Config) (string, map[string]string, error) {
fmt.Fprintf(&sb, "dhcp-hostsdir=%s\n", HostsDir(c.ConfDir, c.Name))
fmt.Fprintf(&sb, "dhcp-optsdir=%s\n", OptsDir(c.ConfDir, c.Name))
entries := Entries(c.Network)
for _, dir := range []string{c.ConfDir, HostsDir(c.ConfDir, c.Name), OptsDir(c.ConfDir, c.Name)} {
if err := os.MkdirAll(dir, 0755); err != nil {
return "", nil, fmt.Errorf("create %s: %w", dir, err)
return "", fmt.Errorf("create %s: %w", dir, err)
}
}
outPath := filepath.Join(c.ConfDir, c.Name+".conf")
return outPath, entries, os.WriteFile(outPath, []byte(sb.String()), 0644)
}
func Entries(network *net.IPNet) map[string]string {
entries := make(map[string]string)
i := 0
for ip := cloneIP(network.IP); network.Contains(ip); incrementIP(ip) {
entries[ip.String()] = fmt.Sprintf("00:22:33:%02X:%02X:%02X", (i>>16)&0xFF, (i>>8)&0xFF, i&0xFF)
i++
}
return entries
return outPath, os.WriteFile(outPath, []byte(sb.String()), 0644)
}
func classlessRoutes(c Config) []string {
@ -69,18 +56,3 @@ func classlessRoutes(c Config) []string {
}
return routes
}
func incrementIP(ip net.IP) {
for j := len(ip) - 1; j >= 0; j-- {
ip[j]++
if ip[j] != 0 {
break
}
}
}
func cloneIP(ip net.IP) net.IP {
clone := make(net.IP, len(ip))
copy(clone, ip)
return clone
}