From 6fcd19857bec575bf0d057829218be3f223604f7 Mon Sep 17 00:00:00 2001 From: Roland Kammerer Date: Tue, 7 Jul 2020 11:02:07 +0200 Subject: [PATCH] boot volume: use size of backing volume So far this was hard coded to 10G, which for example did not allow to boot bigger images like - haha - "Micro"OS. --- internal/virter/libvirt_test.go | 9 +++++++++ internal/virter/libvirtxml.go | 4 ++-- internal/virter/virter.go | 1 + internal/virter/vm.go | 12 +++++++++++- 4 files changed, 23 insertions(+), 3 deletions(-) diff --git a/internal/virter/libvirt_test.go b/internal/virter/libvirt_test.go index 69cc9ff..52299fa 100644 --- a/internal/virter/libvirt_test.go +++ b/internal/virter/libvirt_test.go @@ -160,6 +160,15 @@ func (l *FakeLibvirtConnection) StorageVolDownload(Vol libvirt.StorageVol, inStr return nil } +func (l *FakeLibvirtConnection) StorageVolGetInfo(Vol libvirt.StorageVol) (rType int8, rCapacity uint64, rAllocation uint64, err error) { + _, ok := l.vols[Vol.Name] + if !ok { + return 0, 0, 0, mockLibvirtError(errNoStorageVol) + } + + return 0, 42, 23, nil +} + func (l *FakeLibvirtConnection) NetworkLookupByName(Name string) (rNet libvirt.Network, err error) { if Name != networkName { return libvirt.Network{}, errors.New("unknown network") diff --git a/internal/virter/libvirtxml.go b/internal/virter/libvirtxml.go index 0848c4f..43daaa6 100644 --- a/internal/virter/libvirtxml.go +++ b/internal/virter/libvirtxml.go @@ -197,8 +197,8 @@ func (v *Virter) ciDataVolumeXML(name string) (string, error) { return v.diskVolumeXML(name, 0, "B", "raw") } -func (v *Virter) vmVolumeXML(name string, backingPath string) (string, error) { - volume := v.diskVolume(name, 10, "GiB", "qcow2") +func (v *Virter) vmVolumeXML(name string, backingPath string, sizeB uint64) (string, error) { + volume := v.diskVolume(name, sizeB, "B", "qcow2") volume.BackingStore = &lx.StorageVolumeBackingStore{ Path: backingPath, Format: &lx.StorageVolumeTargetFormat{Type: "qcow2"}, diff --git a/internal/virter/virter.go b/internal/virter/virter.go index 411842f..5c6dd22 100644 --- a/internal/virter/virter.go +++ b/internal/virter/virter.go @@ -24,6 +24,7 @@ type LibvirtConnection interface { StorageVolGetXMLDesc(Vol libvirt.StorageVol, Flags uint32) (rXML string, err error) StorageVolCreateXMLFrom(Pool libvirt.StoragePool, XML string, Clonevol libvirt.StorageVol, Flags libvirt.StorageVolCreateFlags) (rVol libvirt.StorageVol, err error) StorageVolDownload(Vol libvirt.StorageVol, inStream io.Writer, Offset uint64, Length uint64, Flags libvirt.StorageVolDownloadFlags) (err error) + StorageVolGetInfo(Vol libvirt.StorageVol) (rType int8, rCapacity uint64, rAllocation uint64, err error) NetworkLookupByName(Name string) (rNet libvirt.Network, err error) NetworkGetXMLDesc(Net libvirt.Network, Flags uint32) (rXML string, err error) NetworkUpdate(Net libvirt.Network, Command uint32, Section uint32, ParentIndex int32, XML string, Flags libvirt.NetworkUpdateFlags) (err error) diff --git a/internal/virter/vm.go b/internal/virter/vm.go index 5c8550e..b7d10ed 100644 --- a/internal/virter/vm.go +++ b/internal/virter/vm.go @@ -9,6 +9,7 @@ import ( "time" "github.com/LINBIT/virter/pkg/netcopy" + "github.com/rck/unit" log "github.com/sirupsen/logrus" "golang.org/x/crypto/ssh" @@ -142,7 +143,16 @@ func (v *Virter) createVMVolume(sp libvirt.StoragePool, vmConfig VMConfig) error return fmt.Errorf("could not get backing image path: %w", err) } - xml, err := v.vmVolumeXML(vmName, backingPath) + _, sizeB, _, err := v.libvirt.StorageVolGetInfo(backingVolume) + if err != nil { + return fmt.Errorf("could not get backing image info: %w", err) + } + minSize := uint64(10 * unit.G) + if sizeB < minSize { + sizeB = minSize + } + + xml, err := v.vmVolumeXML(vmName, backingPath, sizeB) if err != nil { return err }