Merge branch 'feature-74'
All checks were successful
Documentation / publish (push) Successful in 1m32s
All checks were successful
Documentation / publish (push) Successful in 1m32s
This commit is contained in:
commit
91cf5e3323
20 changed files with 470 additions and 156 deletions
|
|
@ -566,8 +566,15 @@ components:
|
|||
example: /var/lib/two/volumes/abc.qcow2
|
||||
dev:
|
||||
type: string
|
||||
description: Device name inside the VM
|
||||
pattern: '^[sv]d[a-z]$'
|
||||
description: >-
|
||||
vda is the virtio-blk root disk, seen as /dev/vda and booted
|
||||
first. xvdX is an additional disk on SCSI target X (xvda → 0),
|
||||
seen by the guest kernel as some /dev/sdY in detection order. The
|
||||
disk serial is set to dev: an image that follows the two image
|
||||
requirements links it to /dev/xvdX, and udev always exposes it as
|
||||
/dev/disk/by-id/scsi-0QEMU_QEMU_HARDDISK_xvdX (virtio-vda for the
|
||||
root disk). Anything else → 400; sdX is no longer accepted.
|
||||
pattern: '^(vda|xvd[a-z])$'
|
||||
example: vda
|
||||
|
||||
VM:
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -39,7 +39,8 @@ Réseau
|
|||
QEMU et VM
|
||||
----------
|
||||
|
||||
* **Un seul disque ``vdX`` par VM** ; les disques additionnels passent par le SCSI (``sdX``). La
|
||||
* **Un seul disque virtio, ``vda``, le disque racine** ; les disques additionnels passent par le SCSI
|
||||
(``xvdX``, cible X). La
|
||||
carte PCI en dépend : NIC en ``0x03``, contrôleur SCSI en ``0x1e``, virtio-blk en ``0x1f``.
|
||||
* ``bus=pci.0`` est explicite sur les trois ``-device`` : un passage de la machine en **q35**
|
||||
casserait le démarrage (``Bus 'pci.0' not found``).
|
||||
|
|
|
|||
|
|
@ -28,7 +28,7 @@ de sérialisation structurée.
|
|||
vm/<name>/nic/<i>/primary → "true" (exactement une interface ; absent sur les autres)
|
||||
vm/<name>/nic/<i>/tap_id → <int> (alloué au premier démarrage, puis conservé)
|
||||
vm/<name>/metadata_port → <port>
|
||||
vm/<name>/disk/<dev> → <path> (une clé par disque : sda, vda, …)
|
||||
vm/<name>/disk/<dev> → <path> (une clé par disque : vda, xvda, xvdb, …)
|
||||
vm/<name>/memory → <int> (Mo)
|
||||
vm/<name>/cpus → <int>
|
||||
vm/<name>/uefi → "true" (absent si SeaBIOS)
|
||||
|
|
@ -55,6 +55,12 @@ clés. Les VM ``deleted`` sont ignorées. Une interface dont la MAC est introuva
|
|||
démarrage en la nommant, et la table reste en place tant que toutes les interfaces ne sont pas
|
||||
migrées.
|
||||
|
||||
**Anciens noms de disques.** Jusqu'en 0.1.0, les disques additionnels s'appelaient ``sdX``. Au
|
||||
démarrage, l'agent renomme chaque clé ``vm/<name>/disk/sdX`` en ``vm/<name>/disk/xvdX`` (même
|
||||
lettre, même cible SCSI). Si ``xvdX`` existe déjà avec un autre chemin, le démarrage échoue en
|
||||
nommant la clé. Le numéro de série du disque, et donc son lien ``by-id``, prend le nouveau nom au
|
||||
prochain lancement de la VM.
|
||||
|
||||
**Les états passent par ``state``.** Toujours ``state.Set`` / ``state.Get`` : ``Set`` refuse une
|
||||
valeur hors énumération, ``Get`` refuse de retourner une valeur non reconnue. ``error`` n'est
|
||||
écrit que par ``Dispatcher.Dispatch``, via ``cmd.Key()``.
|
||||
|
|
|
|||
|
|
@ -94,9 +94,12 @@ suppression, ces ``/32`` sont retirés **avant** l'arrêt de QEMU. Comme un subn
|
|||
supprimé tant qu'une VM l'utilise, le ``/32`` d'une VM est toujours retiré avant le ``/24`` de son
|
||||
subnet — l'ordre qui évite le trou noir de `#41 <https://git.g3e.fr/syonad/two/issues/41>`_.
|
||||
|
||||
**Stockage.** Un seul disque ``vdX`` (virtio-blk) par VM ; les disques supplémentaires passent
|
||||
par le contrôleur SCSI (``sdX``). Cette contrainte vient de la carte PCI figée — voir
|
||||
:doc:`/architecture/contraintes`.
|
||||
**Stockage.** Un seul disque virtio par VM, ``vda``, le disque racine ; les disques
|
||||
supplémentaires passent par le contrôleur SCSI (``xvdX``, cible X). Cette contrainte vient de la
|
||||
carte PCI figée — voir :doc:`/architecture/contraintes`. ``dev`` fixe l'emplacement du disque
|
||||
(cible SCSI, slot PCI) et son numéro de série, pas le nom que lui donne le noyau du guest
|
||||
(``/dev/sdY``, dans l'ordre de détection) : le disque se retrouve par ``/dev/xvdX`` dans une
|
||||
image conforme, et toujours par ``/dev/disk/by-id/…<dev>``.
|
||||
|
||||
Ce qui est stocké, et où
|
||||
------------------------
|
||||
|
|
|
|||
|
|
@ -96,7 +96,7 @@ Là encore, attendez ``running`` :
|
|||
"interfaces": [{"subnet": "sn-000001", "ip": "10.1.1.2", "mac": "52:54:00:01:01:02", "primary": true}],
|
||||
"storage": [{"path": "/var/lib/two/volumes/i-web.qcow2", "dev": "vda"}]}'
|
||||
|
||||
Quatre points qui coûtent du temps quand on les découvre en production :
|
||||
Six points qui coûtent du temps quand on les découvre en production :
|
||||
|
||||
``user_data`` est **encodé en base64**
|
||||
Un base64 invalide est rejeté en 400 plutôt que servi vide. L'agent n'interprète jamais ce
|
||||
|
|
@ -115,10 +115,17 @@ Exactement une interface est ``primary``
|
|||
slot PCI (``0x03 + index``), donc le nom de l'interface dans le guest. Tous les subnets d'une
|
||||
VM doivent appartenir au même VPC.
|
||||
|
||||
Un seul disque ``vdX``
|
||||
Les disques supplémentaires passent par ``sdX``. La carte PCI en dépend — voir
|
||||
``vda`` pour la racine, ``xvdX`` pour les autres disques
|
||||
``vda`` est le disque racine, virtio, démarré en premier. Les disques supplémentaires sont des
|
||||
``xvdX`` (``xvda`` = cible SCSI 0) ; ``sdX`` est refusé (400). La carte PCI en dépend — voir
|
||||
:doc:`/architecture/contraintes`.
|
||||
|
||||
``/dev/xvdX`` dans le guest, jamais ``/dev/sdX``
|
||||
Le noyau du guest nomme les disques SCSI ``sdX`` dans l'ordre de détection, qui change d'un
|
||||
démarrage à l'autre. L'agent donne à chaque disque le numéro de série ``<dev>`` ; une image
|
||||
conforme (:doc:`/deploiement/image-qcow2`) en fait le lien ``/dev/xvdX``. Sans cette règle,
|
||||
le nom stable reste ``/dev/disk/by-id/scsi-0QEMU_QEMU_HARDDISK_xvdX``.
|
||||
|
||||
4. Vérifier
|
||||
-----------
|
||||
|
||||
|
|
|
|||
|
|
@ -284,6 +284,21 @@ interface (``10-netplan-*``). Sur une image démarrée avec ``net.ifnames=0``, v
|
|||
l'image Rocky de cette page, n'est pas encore vérifié. Sans lui, une VM à plusieurs
|
||||
interfaces n'a de bail que sur la première.
|
||||
|
||||
**Noms stables des disques additionnels**
|
||||
|
||||
.. code-block:: bash
|
||||
|
||||
cat << 'ENDFILE' > /mnt/etc/udev/rules.d/90-two-xvd.rules
|
||||
SUBSYSTEM=="block", ENV{DEVTYPE}=="disk", ENV{ID_SERIAL_SHORT}=="xvd[a-z]", SYMLINK+="$env{ID_SERIAL_SHORT}"
|
||||
SUBSYSTEM=="block", ENV{DEVTYPE}=="partition", ENV{ID_SERIAL_SHORT}=="xvd[a-z]", SYMLINK+="$env{ID_SERIAL_SHORT}%n"
|
||||
ENDFILE
|
||||
|
||||
L'agent donne à chaque disque additionnel le numéro de série ``xvdX`` déclaré dans l'API ; ces
|
||||
règles en font ``/dev/xvdX`` (et ``/dev/xvdXN`` pour ses partitions). Le noyau, lui, nomme les
|
||||
disques SCSI ``/dev/sdY`` dans l'ordre de détection, qui change d'un démarrage à l'autre : une
|
||||
``fstab`` ou un script de la VM doit viser ``/dev/xvdX``, jamais ``/dev/sdY``. Le disque racine
|
||||
reste ``/dev/vda``.
|
||||
|
||||
**Services et durcissement**
|
||||
|
||||
.. code-block:: bash
|
||||
|
|
|
|||
|
|
@ -320,9 +320,9 @@ Générée par ``test/e2e/run.sh --matrice`` à partir des en-têtes ; à régé
|
|||
- hv1
|
||||
- #33, #62, #73
|
||||
* - ``26-multi-disque``
|
||||
- VM à plusieurs disques — virtio (vdX) et SCSI (sdX), tailles et bus vus par la VM, démarrage sur vda
|
||||
- VM à plusieurs disques — racine virtio (vda) et SCSI (xvdX), tailles et bus vus par la VM, liens ``/dev/xvdX`` et ``by-id`` par numéro de série, démarrage sur vda
|
||||
- hv1
|
||||
- #32
|
||||
- #32, #74
|
||||
* - ``27-reboot``
|
||||
- redémarrage d'une VM depuis l'invité — même processus QEMU, même adresse, nouveau bail, user_data non rejoué
|
||||
- hv1
|
||||
|
|
@ -412,7 +412,7 @@ Hypothèses vérifiées en lab, à garder en tête en écrivant un test :
|
|||
|
||||
* systemd-networkd **ne réécrit pas** un bail renouvelé à l'identique : un renouvellement se
|
||||
vérifie par le DHCPACK dans le journal du serveur (``lease_renewed``), pas par le fichier de bail ;
|
||||
* l'agent n'accepte **qu'un seul disque virtio** (le disque système) ; les autres sont des ``sdX`` ;
|
||||
* l'agent n'accepte **qu'un seul disque virtio** (le disque racine, ``vda``) ; les autres sont des ``xvdX`` ;
|
||||
* sur Debian 12, ``ebtables -L`` (nft) garde les zéros de tête des MAC ;
|
||||
* ``interface_ip`` est posée en ``/32`` sur le bridge du netns en mode ``bridge`` ;
|
||||
* depuis le netns d'un hôte, une VM d'un autre hôte ne répond pas : la réponse repart vers la
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
112
internal/qemu/args.go
Normal file
112
internal/qemu/args.go
Normal file
|
|
@ -0,0 +1,112 @@
|
|||
package qemu
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const (
|
||||
firstNICSlot = 0x03
|
||||
lastNICSlot = 0x1d
|
||||
maxNICs = lastNICSlot - firstNICSlot + 1
|
||||
)
|
||||
|
||||
func buildArgs(cfg Config) ([]string, error) {
|
||||
memory := cfg.Memory
|
||||
if memory == 0 {
|
||||
memory = 512
|
||||
}
|
||||
|
||||
cpus := cfg.CPUs
|
||||
if cpus == 0 {
|
||||
cpus = 1
|
||||
}
|
||||
|
||||
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",
|
||||
"-cpu", "host",
|
||||
"-m", fmt.Sprintf("%d", memory),
|
||||
"-smp", fmt.Sprintf("%d", cpus),
|
||||
"-serial", fmt.Sprintf("unix:%s,server,nowait", serialSock),
|
||||
"-monitor", fmt.Sprintf("unix:%s,server,nowait", monitorSock),
|
||||
"-qmp", fmt.Sprintf("unix:%s,server,nowait", qmpSock),
|
||||
"-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),
|
||||
)
|
||||
}
|
||||
|
||||
hasScsi := false
|
||||
virtioBlk := 0
|
||||
for _, d := range cfg.Disks {
|
||||
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 xvd* pour les disques additionnels", virtioBlk)
|
||||
}
|
||||
if hasScsi {
|
||||
args = append(args, "-device", "virtio-scsi-pci,id=scsi0,bus=pci.0,addr=0x1e")
|
||||
}
|
||||
|
||||
sorted := make([]DiskConfig, len(cfg.Disks))
|
||||
copy(sorted, cfg.Disks)
|
||||
// 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")
|
||||
jVirtio := strings.HasPrefix(sorted[j].Dev, "vd")
|
||||
if iVirtio != jVirtio {
|
||||
return iVirtio
|
||||
}
|
||||
return sorted[i].Dev < sorted[j].Dev
|
||||
})
|
||||
|
||||
for idx, d := range sorted {
|
||||
bootindex := idx + 1
|
||||
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),
|
||||
)
|
||||
} else {
|
||||
args = append(args,
|
||||
"-drive", fmt.Sprintf("file=%s,if=none,id=%s", d.Path, d.Dev),
|
||||
"-device", fmt.Sprintf("virtio-blk-pci,drive=%s,bus=pci.0,addr=0x1f,bootindex=%d,serial=%s", d.Dev, bootindex, d.Dev),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
// Slots 0x03 à 0x1d réservés au réseau par la carte PCI (#36). Le slot est
|
||||
// dérivé de l'index et non laissé à QEMU : c'est lui qui fixe le nom de
|
||||
// l'interface dans le guest, et un slot flottant la renomme d'un démarrage
|
||||
// à l'autre.
|
||||
if len(cfg.NICs) == 0 {
|
||||
return nil, fmt.Errorf("vm %s has no network interface", cfg.Name)
|
||||
}
|
||||
if len(cfg.NICs) > maxNICs {
|
||||
return nil, fmt.Errorf("vm %s has %d interfaces, the pci map holds %d", cfg.Name, len(cfg.NICs), maxNICs)
|
||||
}
|
||||
for i, n := range cfg.NICs {
|
||||
args = append(args,
|
||||
"-netdev", fmt.Sprintf("tap,id=net%d,ifname=tap%d,script=no,downscript=no", i, n.TapID),
|
||||
"-device", fmt.Sprintf("virtio-net-pci,netdev=net%d,mac=%s,bus=pci.0,addr=0x%02x", i, n.Mac, firstNICSlot+i),
|
||||
)
|
||||
}
|
||||
|
||||
return append(args, "-daemonize"), nil
|
||||
}
|
||||
75
internal/qemu/args_test.go
Normal file
75
internal/qemu/args_test.go
Normal file
|
|
@ -0,0 +1,75 @@
|
|||
package qemu
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func devicesOf(args []string) []string {
|
||||
var devices []string
|
||||
for i := 0; i+1 < len(args); i++ {
|
||||
if args[i] == "-device" {
|
||||
devices = append(devices, args[i+1])
|
||||
}
|
||||
}
|
||||
return devices
|
||||
}
|
||||
|
||||
func TestBuildArgs_DiskSerialIsDeclaredDev(t *testing.T) {
|
||||
args, err := buildArgs(Config{
|
||||
Name: "vm1",
|
||||
NICs: []NICConfig{{TapID: 1, Mac: "52:54:00:00:00:01"}},
|
||||
Disks: []DiskConfig{
|
||||
{Path: "/v/b.qcow2", Dev: "xvdb"},
|
||||
{Path: "/v/root.qcow2", Dev: "vda"},
|
||||
{Path: "/v/a.qcow2", Dev: "xvda"},
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("buildArgs a échoué : %v", err)
|
||||
}
|
||||
|
||||
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=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)
|
||||
if len(got) != len(want) {
|
||||
t.Fatalf("devices = %q, attendu %q", got, want)
|
||||
}
|
||||
for i := range want {
|
||||
if got[i] != want[i] {
|
||||
t.Errorf("device %d = %q, attendu %q", i, got[i], want[i])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestBuildArgs_EndsWithDaemonize(t *testing.T) {
|
||||
args, err := buildArgs(Config{Name: "vm1", NICs: []NICConfig{{TapID: 1, Mac: "52:54:00:00:00:01"}}})
|
||||
if err != nil {
|
||||
t.Fatalf("buildArgs a échoué : %v", err)
|
||||
}
|
||||
if args[len(args)-1] != "-daemonize" {
|
||||
t.Errorf("dernier argument = %q, attendu -daemonize", args[len(args)-1])
|
||||
}
|
||||
}
|
||||
|
||||
func TestBuildArgs_RejectsSecondVirtioDisk(t *testing.T) {
|
||||
_, err := buildArgs(Config{
|
||||
Name: "vm1",
|
||||
NICs: []NICConfig{{TapID: 1, Mac: "52:54:00:00:00:01"}},
|
||||
Disks: []DiskConfig{{Path: "/v/a", Dev: "vda"}, {Path: "/v/b", Dev: "vdb"}},
|
||||
})
|
||||
if err == nil {
|
||||
t.Fatal("buildArgs a accepté deux disques virtio")
|
||||
}
|
||||
}
|
||||
|
||||
func TestBuildArgs_RejectsNoNIC(t *testing.T) {
|
||||
_, err := buildArgs(Config{Name: "vm1"})
|
||||
if err == nil {
|
||||
t.Fatal("buildArgs a accepté une VM sans interface")
|
||||
}
|
||||
}
|
||||
|
|
@ -6,28 +6,10 @@ import (
|
|||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const (
|
||||
firstNICSlot = 0x03
|
||||
lastNICSlot = 0x1d
|
||||
maxNICs = lastNICSlot - firstNICSlot + 1
|
||||
)
|
||||
|
||||
func Start(cfg Config) error {
|
||||
memory := cfg.Memory
|
||||
if memory == 0 {
|
||||
memory = 512
|
||||
}
|
||||
|
||||
cpus := cfg.CPUs
|
||||
if cpus == 0 {
|
||||
cpus = 1
|
||||
}
|
||||
|
||||
for _, dir := range []string{cfg.SerialDir, cfg.MonitorDir, cfg.QMPDir} {
|
||||
if dir != "" {
|
||||
if err := os.MkdirAll(dir, 0755); err != nil {
|
||||
|
|
@ -36,92 +18,11 @@ func Start(cfg Config) error {
|
|||
}
|
||||
}
|
||||
|
||||
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",
|
||||
"-cpu", "host",
|
||||
"-m", fmt.Sprintf("%d", memory),
|
||||
"-smp", fmt.Sprintf("%d", cpus),
|
||||
"-serial", fmt.Sprintf("unix:%s,server,nowait", serialSock),
|
||||
"-monitor", fmt.Sprintf("unix:%s,server,nowait", monitorSock),
|
||||
"-qmp", fmt.Sprintf("unix:%s,server,nowait", qmpSock),
|
||||
"-display", "none",
|
||||
args, err := buildArgs(cfg)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
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),
|
||||
)
|
||||
}
|
||||
|
||||
hasScsi := false
|
||||
virtioBlk := 0
|
||||
for _, d := range cfg.Disks {
|
||||
if strings.HasPrefix(d.Dev, "sd") {
|
||||
hasScsi = true
|
||||
} else {
|
||||
virtioBlk++
|
||||
}
|
||||
}
|
||||
if virtioBlk > 1 {
|
||||
return fmt.Errorf("qemu: %d disques virtio-blk (vd*) déclarés, un seul est supporté — utiliser des disques sd* pour les disques additionnels", virtioBlk)
|
||||
}
|
||||
if hasScsi {
|
||||
args = append(args, "-device", "virtio-scsi-pci,id=scsi0,bus=pci.0,addr=0x1e")
|
||||
}
|
||||
|
||||
sorted := make([]DiskConfig, len(cfg.Disks))
|
||||
copy(sorted, cfg.Disks)
|
||||
// vd* avant sd* : 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")
|
||||
jVirtio := strings.HasPrefix(sorted[j].Dev, "vd")
|
||||
if iVirtio != jVirtio {
|
||||
return iVirtio
|
||||
}
|
||||
return sorted[i].Dev < sorted[j].Dev
|
||||
})
|
||||
|
||||
for idx, d := range sorted {
|
||||
bootindex := idx + 1
|
||||
if strings.HasPrefix(d.Dev, "sd") {
|
||||
scsiID := int(d.Dev[2] - '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", d.Dev, scsiID, bootindex),
|
||||
)
|
||||
} else {
|
||||
args = append(args,
|
||||
"-drive", fmt.Sprintf("file=%s,if=none,id=%s", d.Path, d.Dev),
|
||||
"-device", fmt.Sprintf("virtio-blk-pci,drive=%s,bus=pci.0,addr=0x1f,bootindex=%d", d.Dev, bootindex),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
// Slots 0x03 à 0x1d réservés au réseau par la carte PCI (#36). Le slot est
|
||||
// dérivé de l'index et non laissé à QEMU : c'est lui qui fixe le nom de
|
||||
// l'interface dans le guest, et un slot flottant la renomme d'un démarrage
|
||||
// à l'autre.
|
||||
if len(cfg.NICs) == 0 {
|
||||
return fmt.Errorf("vm %s has no network interface", cfg.Name)
|
||||
}
|
||||
if len(cfg.NICs) > maxNICs {
|
||||
return fmt.Errorf("vm %s has %d interfaces, the pci map holds %d", cfg.Name, len(cfg.NICs), maxNICs)
|
||||
}
|
||||
for i, n := range cfg.NICs {
|
||||
args = append(args,
|
||||
"-netdev", fmt.Sprintf("tap,id=net%d,ifname=tap%d,script=no,downscript=no", i, n.TapID),
|
||||
"-device", fmt.Sprintf("virtio-net-pci,netdev=net%d,mac=%s,bus=pci.0,addr=0x%02x", i, n.Mac, firstNICSlot+i),
|
||||
)
|
||||
}
|
||||
|
||||
args = append(args, "-daemonize")
|
||||
|
||||
scopeArgs := append([]string{
|
||||
"--scope",
|
||||
"--unit=" + ScopeName(cfg.Name),
|
||||
|
|
|
|||
|
|
@ -87,6 +87,18 @@ L3VNI, avec une configuration BGP pilotée par l'agent ; documentation du projet
|
|||
leurs subnets (VM injoignable), et n'était pas tenable en IPv6
|
||||
- Migration au démarrage : les VM existantes gardent leur MAC, recopiée sur chaque interface
|
||||
|
||||
**Disques : `vda` pour la racine, `xvdX` pour les autres (#74)**
|
||||
|
||||
- `dev` accepte désormais `vda`, le disque racine virtio, et `xvda` à `xvdz` pour les disques
|
||||
additionnels, sur la cible SCSI de même rang (`xvda` → 0). Toute autre valeur est refusée (400),
|
||||
`sdX` compris
|
||||
- Chaque disque reçoit le numéro de série `<dev>`. Le noyau de l'invité nomme les disques SCSI
|
||||
`/dev/sdY` dans l'ordre de détection, qui change d'un démarrage à l'autre (mesuré en lab : `sda`
|
||||
et `sdb` inversés 4 démarrages sur 6). Le nom stable est `/dev/xvdX`, créé par une règle udev
|
||||
de l'image (exigence décrite dans la documentation de construction d'image), et toujours
|
||||
`/dev/disk/by-id/scsi-0QEMU_QEMU_HARDDISK_xvdX`
|
||||
- Migration au démarrage : les clés `vm/<name>/disk/sdX` deviennent `vm/<name>/disk/xvdX`
|
||||
|
||||
**Exploitation**
|
||||
|
||||
- Le watchdog contrôle aussi, avec une section `bgp` : les sessions vers les route reflectors ;
|
||||
|
|
@ -164,7 +176,7 @@ bash ./deploy.sh -t 0.2.0
|
|||
```
|
||||
|
||||
Sans `dhcp.backend` ni `bgp` dans `/etc/two/agent.yml`, le comportement est celui de la 0.1.0, à
|
||||
trois exceptions près, qui touchent les appelants de l'API :
|
||||
quatre exceptions près, qui touchent les appelants de l'API :
|
||||
|
||||
- **`interface_ip` ne se fournit plus pour un subnet `vxlan`** : il est refusé (400), l'agent le
|
||||
calcule. Le retirer des appels de création, et ne plus placer de VM sur les deux dernières
|
||||
|
|
@ -172,6 +184,9 @@ trois exceptions près, qui touchent les appelants de l'API :
|
|||
- **un subnet encore utilisé par une VM ne se supprime plus** : supprimer les VM d'abord
|
||||
- **chaque interface d'une VM doit porter sa `mac`** (400 sinon) : l'appelant la choisit et en
|
||||
garantit l'unicité. Les VM existantes gardent la leur, recopiée au premier démarrage de l'agent
|
||||
- **les disques additionnels s'appellent `xvdX`** : `sdX` est refusé (400). Les VM existantes sont
|
||||
migrées ; leurs disques prennent le numéro de série `xvdX` au prochain lancement, et une image
|
||||
qui les montait par `/dev/sdY` doit passer à `/dev/xvdX`
|
||||
|
||||
Les subnets `vxlan` existants gardent leur adressage et le modèle précédent (gateway sur le
|
||||
bridge) jusqu'à leur recréation.
|
||||
|
|
|
|||
|
|
@ -15,6 +15,10 @@ two_image () {
|
|||
> /mnt/two-image/etc/cloud/cloud.cfg.d/99_metadata.cfg
|
||||
printf '%s\n' '[Match]' 'Name=en*' '' '[Network]' 'DHCP=ipv4' \
|
||||
> /mnt/two-image/etc/systemd/network/90-all-en.network
|
||||
printf '%s\n' \
|
||||
'SUBSYSTEM=="block", ENV{DEVTYPE}=="disk", ENV{ID_SERIAL_SHORT}=="xvd[a-z]", SYMLINK+="$env{ID_SERIAL_SHORT}"' \
|
||||
'SUBSYSTEM=="block", ENV{DEVTYPE}=="partition", ENV{ID_SERIAL_SHORT}=="xvd[a-z]", SYMLINK+="$env{ID_SERIAL_SHORT}%n"' \
|
||||
> /mnt/two-image/etc/udev/rules.d/90-two-xvd.rules
|
||||
umount /mnt/two-image
|
||||
qemu-nbd -d /dev/nbd0 >/dev/null
|
||||
mv "${work}/${IMAGE_FILE}" "${TWO_IMAGE}"
|
||||
|
|
|
|||
|
|
@ -361,16 +361,16 @@ test_vm_body_builds_the_create_request () {
|
|||
setup "vm_body : requête de création, disque par défaut, métadonnées et dev"
|
||||
[[ $(node 'vm_body t11-a sn-t11a 10.11.1.10') == '{"name":"t11-a","memory":1024,"cpus":1,"metadata":{},"interfaces":[{"subnet":"sn-t11a","ip":"10.11.1.10","mac":"52:54:00:0b:01:0a","primary":true}],"storage":[{"path":"/var/lib/two/volumes/t11-a.qcow2","dev":"vda"}]}' ]] \
|
||||
|| fail "défaut : $(node 'vm_body t11-a sn-t11a 10.11.1.10')"
|
||||
[[ $(node 'vm_body t11-b sn-t11a 10.11.1.11 /x.qcow2 "{\"user_data\":\"YQ==\"}" sdb | jq -c "[.metadata.user_data, .storage[0].path, .storage[0].dev]"') == '["YQ==","/x.qcow2","sdb"]' ]] \
|
||||
|| fail "options : $(node 'vm_body t11-b sn-t11a 10.11.1.11 /x.qcow2 "{\"user_data\":\"YQ==\"}" sdb')"
|
||||
[[ $(node 'vm_body t11-b sn-t11a 10.11.1.11 /x.qcow2 "{\"user_data\":\"YQ==\"}" xvdb | jq -c "[.metadata.user_data, .storage[0].path, .storage[0].dev]"') == '["YQ==","/x.qcow2","xvdb"]' ]] \
|
||||
|| fail "options : $(node 'vm_body t11-b sn-t11a 10.11.1.11 /x.qcow2 "{\"user_data\":\"YQ==\"}" xvdb')"
|
||||
teardown
|
||||
}
|
||||
|
||||
test_vm_json_builds_nics_disks_metadata_and_uefi () {
|
||||
setup "vm_json : NIC (la première primaire), disques, métadonnées, uefi"
|
||||
local out
|
||||
out=$(node 'vm_json t25-m --disk vda=/v/a.qcow2 --nic sn-a=10.0.1.10 --nic sn-b=10.0.2.10 --disk sda=/v/b.qcow2 --sshkey "ssh-ed25519 AAAA x" --password "\$6\$s\$h" --user-data "I2Nsb3Vk" --uefi')
|
||||
[[ "${out}" == '{"name":"t25-m","memory":1024,"cpus":1,"metadata":{"sshkey":"ssh-ed25519 AAAA x","password":"$6$s$h","user_data":"I2Nsb3Vk"},"interfaces":[{"subnet":"sn-a","ip":"10.0.1.10","mac":"52:54:00:00:01:0a","primary":true},{"subnet":"sn-b","ip":"10.0.2.10","mac":"52:54:00:00:02:0a","primary":false}],"storage":[{"path":"/v/a.qcow2","dev":"vda"},{"path":"/v/b.qcow2","dev":"sda"}],"uefi":true}' ]] \
|
||||
out=$(node 'vm_json t25-m --disk vda=/v/a.qcow2 --nic sn-a=10.0.1.10 --nic sn-b=10.0.2.10 --disk xvda=/v/b.qcow2 --sshkey "ssh-ed25519 AAAA x" --password "\$6\$s\$h" --user-data "I2Nsb3Vk" --uefi')
|
||||
[[ "${out}" == '{"name":"t25-m","memory":1024,"cpus":1,"metadata":{"sshkey":"ssh-ed25519 AAAA x","password":"$6$s$h","user_data":"I2Nsb3Vk"},"interfaces":[{"subnet":"sn-a","ip":"10.0.1.10","mac":"52:54:00:00:01:0a","primary":true},{"subnet":"sn-b","ip":"10.0.2.10","mac":"52:54:00:00:02:0a","primary":false}],"storage":[{"path":"/v/a.qcow2","dev":"vda"},{"path":"/v/b.qcow2","dev":"xvda"}],"uefi":true}' ]] \
|
||||
|| fail "complet : ${out}"
|
||||
out=$(node 'vm_json t21-a --nic sn-a=10.0.1.10')
|
||||
[[ "${out}" == '{"name":"t21-a","memory":1024,"cpus":1,"metadata":{},"interfaces":[{"subnet":"sn-a","ip":"10.0.1.10","mac":"52:54:00:00:01:0a","primary":true}],"storage":[],"uefi":false}' ]] || fail "minimal : ${out}"
|
||||
|
|
@ -384,8 +384,8 @@ test_vm_json_builds_nics_disks_metadata_and_uefi () {
|
|||
test_vm_launch_copies_the_system_disk_first_and_waits () {
|
||||
setup "vm_launch : disque système vda copié en premier, POST, attente de running"
|
||||
local out
|
||||
out=$(node 'VOLUMES=/v; TWO_IMAGE=/img; api_state () { echo; }; mkdir () { :; }; cp () { echo "CP $*"; }; api_post () { echo "POST $1 $(jq -c "[.storage[].dev, .interfaces[0].ip]" <<<"$2")"; }; wait_state () { echo "WAIT $*"; }; vm_launch t26-d --nic sn=10.0.0.10 --disk sda=/v/s.qcow2')
|
||||
[[ "${out}" == $'CP /img /v/t26-d.qcow2\nPOST vms ["vda","sda","10.0.0.10"]\nWAIT vms/t26-d running' ]] || fail "${out}"
|
||||
out=$(node 'VOLUMES=/v; TWO_IMAGE=/img; api_state () { echo; }; mkdir () { :; }; cp () { echo "CP $*"; }; api_post () { echo "POST $1 $(jq -c "[.storage[].dev, .interfaces[0].ip]" <<<"$2")"; }; wait_state () { echo "WAIT $*"; }; vm_launch t26-d --nic sn=10.0.0.10 --disk xvda=/v/s.qcow2')
|
||||
[[ "${out}" == $'CP /img /v/t26-d.qcow2\nPOST vms ["vda","xvda","10.0.0.10"]\nWAIT vms/t26-d running' ]] || fail "${out}"
|
||||
[[ $(node 'api_state () { echo running; }; cp () { echo CP; }; api_post () { echo POST; }; vm_launch t --nic s=1.2.3.4 && echo OK') == OK ]] || fail "VM en running relancée"
|
||||
[[ $(node 'api_state () { echo; }; cp () { echo CP; }; vm_launch t --bad x || echo NON') == *NON ]] || fail "option invalide : disque copié ou POST envoyé"
|
||||
teardown
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
# fonctionnalité: validation de l'API — refus synchrones (400, 409, 422) des VPC, subnets et VM, sans trace en base
|
||||
# hôtes: hv1
|
||||
# tickets: #41, #57, #33, #62
|
||||
# tickets: #41, #57, #33, #62, #74
|
||||
|
||||
on hv1 <<'NODE'
|
||||
check "VPC vp-t11a, l3vni 1100 (témoin)" vpc_create vp-t11a 10.11.0.0/16 1100
|
||||
|
|
@ -40,6 +40,9 @@ done
|
|||
check "VM : subnet inconnu, 422" expect_code 422 api_post_code vms "$(vm_body t11-x sn-t11z 10.11.1.12 "${NONE}")"
|
||||
check "VM : aucune interface primaire, 400" expect_code 400 api_post_code vms "$(vm_body t11-x sn-t11a 10.11.1.12 "${NONE}" | sed 's/"primary":true/"primary":false/')"
|
||||
check "VM : interface sans mac, 400" expect_code 400 api_post_code vms "$(vm_body t11-x sn-t11a 10.11.1.12 "${NONE}" | jq -c 'del(.interfaces[0].mac)')"
|
||||
for dev in sdb vdb xvdaa; do
|
||||
check "VM : disque additionnel ${dev}, 400" expect_code 400 api_post_code vms "$(vm_body t11-x sn-t11a 10.11.1.12 "${NONE}" | jq -c --arg d "${dev}" '.storage += [{"path":"/x.qcow2","dev":$d}]')"
|
||||
done
|
||||
check "VM : user_data en base64 invalide, 400" expect_code 400 api_post_code vms "$(vm_body t11-x sn-t11a 10.11.1.12 "${NONE}" '{"user_data":"pas du base64 !"}')"
|
||||
check "VMs refusées : aucune trace de t11-x" expect_code 404 api_code GET vms/t11-x
|
||||
cp "${TWO_IMAGE}" "${VOLUMES}/t11-ud.qcow2"
|
||||
|
|
|
|||
|
|
@ -1,27 +1,32 @@
|
|||
# fonctionnalité: VM à plusieurs disques — virtio (vdX) et SCSI (sdX), tailles et bus vus par la VM, démarrage sur vda
|
||||
# fonctionnalité: VM à plusieurs disques — racine virtio (vda) et SCSI (xvdX), tailles et bus vus par la VM, liens /dev/xvdX et by-id par numéro de série, démarrage sur vda
|
||||
# hôtes: hv1
|
||||
# tickets: #32
|
||||
# tickets: #32, #74
|
||||
|
||||
on hv1 <<'NODE'
|
||||
KEY=$(vm_key)
|
||||
mkdir -p "${VOLUMES}"
|
||||
check "volume t26-d-sdb 1 Gio" qemu-img create -q -f qcow2 "${VOLUMES}/t26-d-sdb.qcow2" 1G
|
||||
check "volume t26-d-sda 2 Gio" qemu-img create -q -f qcow2 "${VOLUMES}/t26-d-sda.qcow2" 2G
|
||||
check "volume t26-d-xvdb 1 Gio" qemu-img create -q -f qcow2 "${VOLUMES}/t26-d-xvdb.qcow2" 1G
|
||||
check "volume t26-d-xvda 2 Gio" qemu-img create -q -f qcow2 "${VOLUMES}/t26-d-xvda.qcow2" 2G
|
||||
check "VPC vp-t26" vpc_create vp-t26 10.26.0.0/16
|
||||
check "subnet sn-t26" subnet_create sn-t26 vp-t26 2601 10.26.1.0/24
|
||||
check "VM t26-d : vda (système), sdb, sda" vm_launch t26-d --nic sn-t26=10.26.1.10 --sshkey "${KEY}" \
|
||||
--disk "sdb=${VOLUMES}/t26-d-sdb.qcow2" --disk "sda=${VOLUMES}/t26-d-sda.qcow2"
|
||||
check "API : trois disques" test "$(curl -fsS "${API}/vms/t26-d" | jq -r '[.storage[].dev] | sort | join(" ")')" = "sda sdb vda"
|
||||
check "VM t26-d : vda (racine), xvdb, xvda" vm_launch t26-d --nic sn-t26=10.26.1.10 --sshkey "${KEY}" \
|
||||
--disk "xvdb=${VOLUMES}/t26-d-xvdb.qcow2" --disk "xvda=${VOLUMES}/t26-d-xvda.qcow2"
|
||||
check "API : trois disques" test "$(curl -fsS "${API}/vms/t26-d" | jq -r '[.storage[].dev] | sort | join(" ")')" = "vda xvda xvdb"
|
||||
check "t26-d joignable (démarrée sur vda)" vm_wait vp-t26 10.26.1.10
|
||||
check "t26-d : racine sur vda" vm_grep vp-t26 10.26.1.10 'findmnt -n -o SOURCE /' '^/dev/vda'
|
||||
size_is () { local v; v=$(vm_ssh vp-t26 10.26.1.10 "lsblk -b -dn -o SIZE /dev/${1}" 2>&1 | tr -d ' '); [ "${v}" = "${2}" ] || { echo "lu : ${v}"; return 1; }; }
|
||||
target_size () { local v; v=$(vm_ssh vp-t26 10.26.1.10 "lsblk -b -dn -o HCTL,SIZE" 2>&1 | awk -v t="${1}" '{split($1, h, ":")} h[3] == t {print $2}'); [ "${v}" = "${2}" ] || { echo "lu : ${v:-aucun disque sur la cible ${1}}"; return 1; }; }
|
||||
check "t26-d : sda déclaré → cible SCSI 0, 2 Gio" target_size 0 2147483648
|
||||
check "t26-d : sdb déclaré → cible SCSI 1, 1 Gio" target_size 1 1073741824
|
||||
known_flaky "#74" "t26-d : nom dans l'invité = dev déclaré, sdb de 1 Gio" size_is sdb 1073741824
|
||||
known_flaky "#74" "t26-d : nom dans l'invité = dev déclaré, sda de 2 Gio" size_is sda 2147483648
|
||||
check "t26-d : sdb en SCSI" vm_grep vp-t26 10.26.1.10 'readlink -f /sys/block/sdb/device/driver' 'sd$'
|
||||
check "t26-d : sda en SCSI (virtio-scsi)" vm_ssh vp-t26 10.26.1.10 'test -d /sys/block/sda/device/scsi_device && readlink -f /sys/block/sda | grep -q virtio'
|
||||
check "t26-d : xvda déclaré → cible SCSI 0, 2 Gio" target_size 0 2147483648
|
||||
check "t26-d : xvdb déclaré → cible SCSI 1, 1 Gio" target_size 1 1073741824
|
||||
info "t26-d : /dev/disk/by-id : $(vm_ssh vp-t26 10.26.1.10 'ls /dev/disk/by-id' 2>&1 | tr '\n' ' ')"
|
||||
info "t26-d : noms du noyau : $(vm_ssh vp-t26 10.26.1.10 'lsblk -dn -o NAME,HCTL,SIZE' 2>&1 | tr '\n' ' ')"
|
||||
link_size () { local v; v=$(vm_ssh vp-t26 10.26.1.10 "lsblk -b -dn -o SIZE \$(readlink -e ${1})" 2>&1 | tr -d ' '); [ "${v}" = "${2}" ] || { echo "lu : ${v}"; return 1; }; }
|
||||
check "t26-d : /dev/xvda → 2 Gio (règle udev de l'image)" link_size /dev/xvda 2147483648
|
||||
check "t26-d : /dev/xvdb → 1 Gio (règle udev de l'image)" link_size /dev/xvdb 1073741824
|
||||
check "t26-d : by-id scsi-0QEMU_QEMU_HARDDISK_xvda → 2 Gio" link_size /dev/disk/by-id/scsi-0QEMU_QEMU_HARDDISK_xvda 2147483648
|
||||
check "t26-d : by-id scsi-0QEMU_QEMU_HARDDISK_xvdb → 1 Gio" link_size /dev/disk/by-id/scsi-0QEMU_QEMU_HARDDISK_xvdb 1073741824
|
||||
check "t26-d : by-id virtio-vda → /dev/vda" test "$(vm_ssh vp-t26 10.26.1.10 'readlink -e /dev/disk/by-id/virtio-vda')" = /dev/vda
|
||||
check "t26-d : xvda et xvdb en SCSI (virtio-scsi)" vm_ssh vp-t26 10.26.1.10 'for d in xvda xvdb; do k=$(basename $(readlink -e /dev/$d)); test -d /sys/block/$k/device/scsi_device && readlink -f /sys/block/$k | grep -q virtio || exit 1; done'
|
||||
check "t26-d : vda n'est pas en SCSI (témoin)" vm_fails vp-t26 10.26.1.10 'test -d /sys/block/vda/device/scsi_device'
|
||||
check "t26-d : écriture sur sdb et sda" vm_ssh vp-t26 10.26.1.10 'sudo dd if=/dev/zero of=/dev/sdb bs=1M count=8 oflag=direct status=none && sudo dd if=/dev/zero of=/dev/sda bs=1M count=8 oflag=direct status=none'
|
||||
check "t26-d : partition sur xvdb → lien /dev/xvdb1" vm_ssh vp-t26 10.26.1.10 'echo ",,L" | sudo sfdisk -q /dev/xvdb >/dev/null && sudo udevadm settle && test -b "$(readlink -e /dev/xvdb1)"'
|
||||
check "t26-d : écriture sur xvda et xvdb1" vm_ssh vp-t26 10.26.1.10 'sudo dd if=/dev/zero of=/dev/xvda bs=1M count=8 oflag=direct status=none && sudo dd if=/dev/zero of=/dev/xvdb1 bs=1M count=8 oflag=direct status=none'
|
||||
NODE
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue