f-74: api: disques additionnels xvdX sur la cible SCSI de même rang, sdX refusé, migration des clés disk/sdX #74
Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
This commit is contained in:
parent
9b5b4a6e62
commit
e16edb00bf
7 changed files with 187 additions and 27 deletions
|
|
@ -68,6 +68,11 @@ func run(confFile string) int {
|
|||
return 1
|
||||
}
|
||||
|
||||
if err := migration.MigrateVMDisks(db, log.With(slog.String("component", "migration"))); err != nil {
|
||||
log.Error("vm disk migration failed", "error", err)
|
||||
return 1
|
||||
}
|
||||
|
||||
if cfg.BGP.Enabled() {
|
||||
if err := applyBGPBase(cfg, frr.NewClient()); err != nil {
|
||||
log.Error("failed to apply the bgp base configuration", "error", err)
|
||||
|
|
|
|||
|
|
@ -24,7 +24,7 @@ func TestVmFromDB_SingleDisk(t *testing.T) {
|
|||
"vm/vm-1/metadata_port": "1234",
|
||||
"vm/vm-1/memory": "512",
|
||||
"vm/vm-1/cpus": "1",
|
||||
"vm/vm-1/disk/sda": "/data/root.qcow2",
|
||||
"vm/vm-1/disk/xvda": "/data/root.qcow2",
|
||||
}
|
||||
vm, err := vmFromDB("vm-1", entries)
|
||||
if err != nil {
|
||||
|
|
@ -33,7 +33,7 @@ func TestVmFromDB_SingleDisk(t *testing.T) {
|
|||
if len(vm.Storage) != 1 {
|
||||
t.Fatalf("attendu 1 disque, obtenu %d", len(vm.Storage))
|
||||
}
|
||||
if vm.Storage[0].Dev != "sda" || vm.Storage[0].Path != "/data/root.qcow2" {
|
||||
if vm.Storage[0].Dev != "xvda" || vm.Storage[0].Path != "/data/root.qcow2" {
|
||||
t.Errorf("disque inattendu : %+v", vm.Storage[0])
|
||||
}
|
||||
}
|
||||
|
|
@ -46,8 +46,8 @@ func TestVmFromDB_MultiDisk(t *testing.T) {
|
|||
"vm/vm-2/metadata_port": "1235",
|
||||
"vm/vm-2/memory": "1024",
|
||||
"vm/vm-2/cpus": "2",
|
||||
"vm/vm-2/disk/sda": "/data/root.qcow2",
|
||||
"vm/vm-2/disk/sdb": "/data/data.qcow2",
|
||||
"vm/vm-2/disk/xvda": "/data/root.qcow2",
|
||||
"vm/vm-2/disk/xvdb": "/data/data.qcow2",
|
||||
}
|
||||
vm, err := vmFromDB("vm-2", entries)
|
||||
if err != nil {
|
||||
|
|
@ -57,13 +57,13 @@ func TestVmFromDB_MultiDisk(t *testing.T) {
|
|||
t.Fatalf("attendu 2 disques, obtenu %d", len(vm.Storage))
|
||||
}
|
||||
sort.Slice(vm.Storage, func(i, j int) bool { return vm.Storage[i].Dev < vm.Storage[j].Dev })
|
||||
if vm.Storage[0].Dev != "sda" || vm.Storage[1].Dev != "sdb" {
|
||||
t.Errorf("devs attendus [sda sdb], obtenus [%s %s]", vm.Storage[0].Dev, vm.Storage[1].Dev)
|
||||
if vm.Storage[0].Dev != "xvda" || vm.Storage[1].Dev != "xvdb" {
|
||||
t.Errorf("devs attendus [xvda xvdb], obtenus [%s %s]", vm.Storage[0].Dev, vm.Storage[1].Dev)
|
||||
}
|
||||
}
|
||||
|
||||
func TestVmFromDB_SlotGap(t *testing.T) {
|
||||
// sdb absent — sda et sdc seulement
|
||||
// xvdb absent — xvda et xvdc seulement
|
||||
entries := map[string]string{
|
||||
"vm/vm-3/state": "running",
|
||||
"vm/vm-3/subnet": "sn-1",
|
||||
|
|
@ -71,8 +71,8 @@ func TestVmFromDB_SlotGap(t *testing.T) {
|
|||
"vm/vm-3/metadata_port": "1236",
|
||||
"vm/vm-3/memory": "512",
|
||||
"vm/vm-3/cpus": "1",
|
||||
"vm/vm-3/disk/sda": "/data/root.qcow2",
|
||||
"vm/vm-3/disk/sdc": "/data/extra.qcow2",
|
||||
"vm/vm-3/disk/xvda": "/data/root.qcow2",
|
||||
"vm/vm-3/disk/xvdc": "/data/extra.qcow2",
|
||||
}
|
||||
vm, err := vmFromDB("vm-3", entries)
|
||||
if err != nil {
|
||||
|
|
@ -85,11 +85,11 @@ func TestVmFromDB_SlotGap(t *testing.T) {
|
|||
for _, s := range vm.Storage {
|
||||
devs[s.Dev] = true
|
||||
}
|
||||
if !devs["sda"] || !devs["sdc"] {
|
||||
t.Errorf("attendu sda et sdc, obtenus %v", devs)
|
||||
if !devs["xvda"] || !devs["xvdc"] {
|
||||
t.Errorf("attendu xvda et xvdc, obtenus %v", devs)
|
||||
}
|
||||
if devs["sdb"] {
|
||||
t.Error("sdb ne devrait pas apparaître")
|
||||
if devs["xvdb"] {
|
||||
t.Error("xvdb ne devrait pas apparaître")
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -107,8 +107,8 @@ func TestStartVM_MultiDisk(t *testing.T) {
|
|||
{Subnet: "sn-1", IP: "10.0.0.10", MAC: "52:54:00:00:00:05", Primary: true},
|
||||
},
|
||||
Storage: []VMStorage{
|
||||
{Path: "/data/root.qcow2", Dev: "sda"},
|
||||
{Path: "/data/data.qcow2", Dev: "sdb"},
|
||||
{Path: "/data/root.qcow2", Dev: "vda"},
|
||||
{Path: "/data/data.qcow2", Dev: "xvda"},
|
||||
},
|
||||
Memory: 1024,
|
||||
CPUs: 2,
|
||||
|
|
@ -120,7 +120,7 @@ func TestStartVM_MultiDisk(t *testing.T) {
|
|||
t.Fatalf("attendu 202, obtenu %d : %s", w.Code, w.Body.String())
|
||||
}
|
||||
|
||||
for _, dev := range []string{"sda", "sdb"} {
|
||||
for _, dev := range []string{"vda", "xvda"} {
|
||||
if _, err := kv.GetFromDB(db, "vm/vm-10/disk/"+dev); err != nil {
|
||||
t.Errorf("disk/%s absent en DB après création", dev)
|
||||
}
|
||||
|
|
@ -142,7 +142,7 @@ func TestStartVM_StorageReturnedInResponse(t *testing.T) {
|
|||
{Subnet: "sn-1", IP: "10.0.0.11", MAC: "52:54:00:00:00:05", Primary: true},
|
||||
},
|
||||
Storage: []VMStorage{
|
||||
{Path: "/data/root.qcow2", Dev: "sda"},
|
||||
{Path: "/data/root.qcow2", Dev: "xvda"},
|
||||
},
|
||||
})
|
||||
|
||||
|
|
@ -157,11 +157,38 @@ func TestStartVM_StorageReturnedInResponse(t *testing.T) {
|
|||
if len(vm.Storage) != 1 {
|
||||
t.Fatalf("attendu 1 disque dans la réponse, obtenu %d", len(vm.Storage))
|
||||
}
|
||||
if vm.Storage[0].Dev != "sda" || vm.Storage[0].Path != "/data/root.qcow2" {
|
||||
if vm.Storage[0].Dev != "xvda" || vm.Storage[0].Path != "/data/root.qcow2" {
|
||||
t.Errorf("disque inattendu dans la réponse : %+v", vm.Storage[0])
|
||||
}
|
||||
}
|
||||
|
||||
func TestStartVM_RejectsInvalidDiskDev(t *testing.T) {
|
||||
for _, dev := range []string{"sda", "sdb", "vdb", "xvd", "xvdA", "xvdaa", ""} {
|
||||
s, db := newTestServer(t)
|
||||
kv.AddInDB(db, "subnet/sn-1/state", "running")
|
||||
kv.AddInDB(db, "subnet/sn-1/vpc", "vpc-1")
|
||||
kv.AddInDB(db, "subnet/sn-1/cidr", "10.0.0.0/24")
|
||||
|
||||
body, _ := json.Marshal(VMCreateRequest{
|
||||
Name: "vm-12",
|
||||
Interfaces: []VMInterface{{Subnet: "sn-1", IP: "10.0.0.12", MAC: "52:54:00:00:00:05", Primary: true}},
|
||||
Storage: []VMStorage{{Path: "/data/root.qcow2", Dev: "vda"}, {Path: "/data/data.qcow2", Dev: dev}},
|
||||
})
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
s.VmsHandler(w, httptest.NewRequest(http.MethodPost, "/vms", bytes.NewReader(body)))
|
||||
if w.Code != http.StatusBadRequest {
|
||||
t.Errorf("dev %q : attendu 400, obtenu %d", dev, w.Code)
|
||||
}
|
||||
if !strings.Contains(w.Body.String(), "storage 1: dev") {
|
||||
t.Errorf("dev %q : message inattendu %s", dev, w.Body.String())
|
||||
}
|
||||
if _, err := kv.GetFromDB(db, "vm/vm-12/state"); err == nil {
|
||||
t.Errorf("dev %q : la VM ne doit pas être enregistrée", dev)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// --- POST /vms : objet metadata ---
|
||||
|
||||
func postVM(t *testing.T, name string, meta VMMetadata) (*httptest.ResponseRecorder, *badger.DB) {
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@ import (
|
|||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
dispatcher "git.g3e.fr/syonad/two/internal/dispatcher/agent"
|
||||
|
|
@ -12,6 +13,8 @@ import (
|
|||
"git.g3e.fr/syonad/two/pkg/db/kv"
|
||||
)
|
||||
|
||||
var diskDev = regexp.MustCompile(`^(vda|xvd[a-z])$`)
|
||||
|
||||
func (s *Server) VmsHandler(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
switch r.Method {
|
||||
|
|
@ -88,6 +91,11 @@ func (s *Server) startVM(w http.ResponseWriter, r *http.Request) {
|
|||
|
||||
disks := make([]dispatcher.VMDisk, len(req.Storage))
|
||||
for i, s := range req.Storage {
|
||||
if !diskDev.MatchString(s.Dev) {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
json.NewEncoder(w).Encode(ErrorResponse{Error: fmt.Sprintf("storage %d: dev %q must be vda (root disk) or xvd[a-z]", i, s.Dev)})
|
||||
return
|
||||
}
|
||||
disks[i] = dispatcher.VMDisk{Path: s.Path, Dev: s.Dev}
|
||||
}
|
||||
|
||||
|
|
|
|||
40
internal/migration/disk.go
Normal file
40
internal/migration/disk.go
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
package migration
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"git.g3e.fr/syonad/two/pkg/db/kv"
|
||||
|
||||
"github.com/dgraph-io/badger/v4"
|
||||
)
|
||||
|
||||
var legacyDiskKey = regexp.MustCompile(`^vm/([^/]+)/disk/sd([a-z])$`)
|
||||
|
||||
func MigrateVMDisks(db *badger.DB, log *slog.Logger) error {
|
||||
entries, err := kv.ListByPrefix(db, "vm/")
|
||||
if err != nil {
|
||||
return fmt.Errorf("list vm/: %w", err)
|
||||
}
|
||||
|
||||
for key, path := range entries {
|
||||
m := legacyDiskKey.FindStringSubmatch(key)
|
||||
if m == nil {
|
||||
continue
|
||||
}
|
||||
target := strings.TrimSuffix(key, "sd"+m[2]) + "xvd" + m[2]
|
||||
if existing, ok := entries[target]; ok && existing != path {
|
||||
return fmt.Errorf("migrate %s: %s already holds %q", key, target, existing)
|
||||
}
|
||||
if err := kv.AddInDB(db, target, path); err != nil {
|
||||
return fmt.Errorf("migrate %s: %w", key, err)
|
||||
}
|
||||
if err := kv.DeleteInDB(db, key); err != nil {
|
||||
return fmt.Errorf("delete legacy %s: %w", key, err)
|
||||
}
|
||||
log.Info("vm disk migrated", "resource", "vm/"+m[1], "from", "sd"+m[2], "to", "xvd"+m[2])
|
||||
}
|
||||
return nil
|
||||
}
|
||||
80
internal/migration/disk_test.go
Normal file
80
internal/migration/disk_test.go
Normal file
|
|
@ -0,0 +1,80 @@
|
|||
package migration
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"git.g3e.fr/syonad/two/pkg/db/kv"
|
||||
)
|
||||
|
||||
func TestMigrateVMDisks_RenamesSCSIDisks(t *testing.T) {
|
||||
db := newNICDB(t)
|
||||
kv.AddInDB(db, "vm/i-test1/disk/vda", "/v/root.qcow2")
|
||||
kv.AddInDB(db, "vm/i-test1/disk/sda", "/v/a.qcow2")
|
||||
kv.AddInDB(db, "vm/i-test1/disk/sdc", "/v/c.qcow2")
|
||||
kv.AddInDB(db, "vm/i-test2/disk/sdb", "/v/b.qcow2")
|
||||
|
||||
if err := MigrateVMDisks(db, quietLog()); err != nil {
|
||||
t.Fatalf("MigrateVMDisks : %v", err)
|
||||
}
|
||||
|
||||
got, err := kv.ListByPrefix(db, "vm/")
|
||||
if err != nil {
|
||||
t.Fatalf("ListByPrefix : %v", err)
|
||||
}
|
||||
want := map[string]string{
|
||||
"vm/i-test1/disk/vda": "/v/root.qcow2",
|
||||
"vm/i-test1/disk/xvda": "/v/a.qcow2",
|
||||
"vm/i-test1/disk/xvdc": "/v/c.qcow2",
|
||||
"vm/i-test2/disk/xvdb": "/v/b.qcow2",
|
||||
}
|
||||
if len(got) != len(want) {
|
||||
t.Errorf("clés après migration : %v, attendu %v", got, want)
|
||||
}
|
||||
for key, value := range want {
|
||||
if got[key] != value {
|
||||
t.Errorf("%s = %q, attendu %q", key, got[key], value)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestMigrateVMDisks_Idempotent(t *testing.T) {
|
||||
db := newNICDB(t)
|
||||
kv.AddInDB(db, "vm/i-test1/disk/sda", "/v/a.qcow2")
|
||||
|
||||
for i := 0; i < 2; i++ {
|
||||
if err := MigrateVMDisks(db, quietLog()); err != nil {
|
||||
t.Fatalf("passe %d : %v", i, err)
|
||||
}
|
||||
}
|
||||
if got, _ := kv.GetFromDB(db, "vm/i-test1/disk/xvda"); got != "/v/a.qcow2" {
|
||||
t.Errorf("xvda = %q après deux passes", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMigrateVMDisks_LeavesOtherKeys(t *testing.T) {
|
||||
db := newNICDB(t)
|
||||
kv.AddInDB(db, "vm/i-test1/nic/0/sda", "x")
|
||||
kv.AddInDB(db, "vm/i-test1/disk/sdaa", "/v/x.qcow2")
|
||||
|
||||
if err := MigrateVMDisks(db, quietLog()); err != nil {
|
||||
t.Fatalf("MigrateVMDisks : %v", err)
|
||||
}
|
||||
for _, key := range []string{"vm/i-test1/nic/0/sda", "vm/i-test1/disk/sdaa"} {
|
||||
if _, err := kv.GetFromDB(db, key); err != nil {
|
||||
t.Errorf("%s ne devait pas être touchée : %v", key, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestMigrateVMDisks_RefusesConflict(t *testing.T) {
|
||||
db := newNICDB(t)
|
||||
kv.AddInDB(db, "vm/i-test1/disk/sda", "/v/a.qcow2")
|
||||
kv.AddInDB(db, "vm/i-test1/disk/xvda", "/v/other.qcow2")
|
||||
|
||||
if err := MigrateVMDisks(db, quietLog()); err == nil {
|
||||
t.Fatal("un conflit sda/xvda doit faire échouer la migration")
|
||||
}
|
||||
if got, _ := kv.GetFromDB(db, "vm/i-test1/disk/sda"); got != "/v/a.qcow2" {
|
||||
t.Errorf("sda ne doit pas être supprimée en cas de conflit : %q", got)
|
||||
}
|
||||
}
|
||||
|
|
@ -49,14 +49,14 @@ func buildArgs(cfg Config) ([]string, error) {
|
|||
hasScsi := false
|
||||
virtioBlk := 0
|
||||
for _, d := range cfg.Disks {
|
||||
if strings.HasPrefix(d.Dev, "sd") {
|
||||
if strings.HasPrefix(d.Dev, "xvd") {
|
||||
hasScsi = true
|
||||
} else {
|
||||
virtioBlk++
|
||||
}
|
||||
}
|
||||
if virtioBlk > 1 {
|
||||
return nil, fmt.Errorf("qemu: %d disques virtio-blk (vd*) déclarés, un seul est supporté — utiliser des disques sd* pour les disques additionnels", virtioBlk)
|
||||
return nil, fmt.Errorf("qemu: %d disques virtio-blk (vd*) déclarés, un seul est supporté — utiliser des disques xvd* pour les disques additionnels", virtioBlk)
|
||||
}
|
||||
if hasScsi {
|
||||
args = append(args, "-device", "virtio-scsi-pci,id=scsi0,bus=pci.0,addr=0x1e")
|
||||
|
|
@ -64,7 +64,7 @@ func buildArgs(cfg Config) ([]string, error) {
|
|||
|
||||
sorted := make([]DiskConfig, len(cfg.Disks))
|
||||
copy(sorted, cfg.Disks)
|
||||
// vd* avant sd* : les disques virtio-blk bootent en premier.
|
||||
// vd* avant xvd* : les disques virtio-blk bootent en premier.
|
||||
// À lettre égale de type, ordre alphabétique.
|
||||
sort.Slice(sorted, func(i, j int) bool {
|
||||
iVirtio := strings.HasPrefix(sorted[i].Dev, "vd")
|
||||
|
|
@ -77,8 +77,8 @@ func buildArgs(cfg Config) ([]string, error) {
|
|||
|
||||
for idx, d := range sorted {
|
||||
bootindex := idx + 1
|
||||
if strings.HasPrefix(d.Dev, "sd") {
|
||||
scsiID := int(d.Dev[2] - 'a')
|
||||
if strings.HasPrefix(d.Dev, "xvd") {
|
||||
scsiID := int(d.Dev[3] - 'a')
|
||||
args = append(args,
|
||||
"-drive", fmt.Sprintf("file=%s,if=none,id=%s", d.Path, d.Dev),
|
||||
"-device", fmt.Sprintf("scsi-hd,drive=%s,bus=scsi0.0,scsi-id=%d,bootindex=%d,serial=%s", d.Dev, scsiID, bootindex, d.Dev),
|
||||
|
|
|
|||
|
|
@ -19,9 +19,9 @@ func TestBuildArgs_DiskSerialIsDeclaredDev(t *testing.T) {
|
|||
Name: "vm1",
|
||||
NICs: []NICConfig{{TapID: 1, Mac: "52:54:00:00:00:01"}},
|
||||
Disks: []DiskConfig{
|
||||
{Path: "/v/b.qcow2", Dev: "sdb"},
|
||||
{Path: "/v/b.qcow2", Dev: "xvdb"},
|
||||
{Path: "/v/root.qcow2", Dev: "vda"},
|
||||
{Path: "/v/a.qcow2", Dev: "sda"},
|
||||
{Path: "/v/a.qcow2", Dev: "xvda"},
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
|
|
@ -31,8 +31,8 @@ func TestBuildArgs_DiskSerialIsDeclaredDev(t *testing.T) {
|
|||
want := []string{
|
||||
"virtio-scsi-pci,id=scsi0,bus=pci.0,addr=0x1e",
|
||||
"virtio-blk-pci,drive=vda,bus=pci.0,addr=0x1f,bootindex=1,serial=vda",
|
||||
"scsi-hd,drive=sda,bus=scsi0.0,scsi-id=0,bootindex=2,serial=sda",
|
||||
"scsi-hd,drive=sdb,bus=scsi0.0,scsi-id=1,bootindex=3,serial=sdb",
|
||||
"scsi-hd,drive=xvda,bus=scsi0.0,scsi-id=0,bootindex=2,serial=xvda",
|
||||
"scsi-hd,drive=xvdb,bus=scsi0.0,scsi-id=1,bootindex=3,serial=xvdb",
|
||||
"virtio-net-pci,netdev=net0,mac=52:54:00:00:00:01,bus=pci.0,addr=0x03",
|
||||
}
|
||||
got := devicesOf(args)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue