fix(deps): update module github.com/containers/libhvee to v0.6.0

Signed-off-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
This commit is contained in:
renovate[bot]
2023-12-14 10:55:38 +00:00
committed by GitHub
parent b82e336751
commit 8d94fc1564
12 changed files with 238 additions and 31 deletions

View File

@@ -0,0 +1,108 @@
package hypervctl
import (
"encoding/xml"
"fmt"
"strconv"
"strings"
"github.com/containers/libhvee/pkg/wmiext"
"github.com/containers/podman/v4/pkg/strongunits"
)
// ResizeDisk takes a diskPath and strongly typed new size and uses powershell
// to change its size. There is no error protection for trying to size a disk
// smaller than the current size.
func ResizeDisk(diskPath string, newSize strongunits.GiB) error {
var (
service *wmiext.Service
err error
job *wmiext.Instance
ret int32
)
if service, err = NewLocalHyperVService(); err != nil {
return err
}
defer service.Close()
imms, err := service.GetSingletonInstance("Msvm_ImageManagementService")
if err != nil {
return err
}
defer imms.Close()
err = imms.BeginInvoke("ResizeVirtualHardDisk").
In("Path", diskPath).
In("MaxInternalSize", int64(newSize.ToBytes())).
Execute().
Out("Job", &job).
Out("ReturnValue", &ret).
End()
if err != nil {
return fmt.Errorf("failed to resize disk: %w", err)
}
return waitVMResult(ret, service, job, "failed to resize disk", nil)
}
func GetDiskSize(diskPath string) (strongunits.B, error) {
var (
service *wmiext.Service
err error
job *wmiext.Instance
ret int32
results string
)
if service, err = NewLocalHyperVService(); err != nil {
return 0, err
}
defer service.Close()
imms, err := service.GetSingletonInstance("Msvm_ImageManagementService")
if err != nil {
return 0, err
}
defer imms.Close()
inv := imms.BeginInvoke("GetVirtualHardDiskSettingData").
In("Path", diskPath).
Execute().
Out("Job", &job).
Out("ReturnValue", &ret)
if err := inv.Error(); err != nil {
return 0, fmt.Errorf("failed to get setting data for disk %s: %q", diskPath, err)
}
if err := waitVMResult(ret, service, job, "failure waiting on result from disk settings", nil); err != nil {
return 0, err
}
err = inv.Out("SettingData", &results).End()
if err != nil {
return 0, fmt.Errorf("failed to retrieve setting object payload for disk: %q", err)
}
type CimSingleInstance struct {
XMLName xml.Name `xml:"INSTANCE"`
Properties []CimKvpItemProperty `xml:"PROPERTY"`
}
diskSettings := CimSingleInstance{}
if err := xml.Unmarshal([]byte(results), &diskSettings); err != nil {
return 0, fmt.Errorf("unable to parse disk settings xml: %q", err)
}
for _, prop := range diskSettings.Properties {
if strings.EqualFold(prop.Name, "MaxInternalSize") {
size, err := strconv.ParseUint(prop.Value, 10, 64)
if err != nil {
return 0, fmt.Errorf("unable to parse size in disk settings")
}
return strongunits.B(size), nil
}
}
return 0, fmt.Errorf("disk settings was missing a size value")
}

View File

@@ -4,6 +4,7 @@
package hypervctl
import (
"errors"
"fmt"
"github.com/containers/libhvee/pkg/wmiext"
@@ -88,7 +89,25 @@ func (vmm *VirtualMachineManager) Exists(name string) (bool, error) {
return false, nil
}
// GetMachine is a stub to lookup and get settings for a VM
func (vmm *VirtualMachineManager) GetMachine(name string) (*VirtualMachine, error) {
return vmm.getMachine(name)
}
// GetMachineExists looks for a machine defined in hyperv and returns it if it exists
func (vmm *VirtualMachineManager) GetMachineExists(name string) (bool, *VirtualMachine, error) {
vm, err := vmm.getMachine(name)
if err != nil {
if errors.Is(err, wmiext.ErrNoResults) {
return false, nil, nil
}
return false, nil, err
}
return true, vm, nil
}
// getMachine looks up a single VM by name
func (vmm *VirtualMachineManager) getMachine(name string) (*VirtualMachine, error) {
const wql = "Select * From Msvm_VirtualSystemSettingData Where VirtualSystemType = 'Microsoft:Hyper-V:System:Realized' And ElementName='%s'"
vm := &VirtualMachine{}
@@ -108,6 +127,9 @@ func (vmm *VirtualMachineManager) GetMachine(name string) (*VirtualMachine, erro
settings, err := service.FindFirstInstance(fmt.Sprintf(wql, name))
if err != nil {
if errors.Is(err, wmiext.ErrNoResults) {
return nil, err
}
return vm, fmt.Errorf("could not find virtual machine %q: %w", name, err)
}
defer settings.Close()

View File

@@ -1,6 +1,7 @@
package wmiext
import (
"errors"
"fmt"
"os"
"strings"
@@ -158,6 +159,11 @@ var (
wmiModule syscall.Handle
)
// VM Lookup errors
var (
ErrNoResults = errors.New("no results found")
)
func init() {
file := os.ExpandEnv("${windir}\\system32\\wbem\\wmiutils.dll")
wmiModule, _ = syscall.LoadLibrary(file)

View File

@@ -299,7 +299,7 @@ func (s *Service) FindFirstInstance(wql string) (*Instance, error) {
}
if instance == nil {
return nil, errors.New("no results found")
return nil, ErrNoResults
}
return instance, nil