Files
podman/vendor/github.com/containers/libhvee/pkg/hypervctl/kvp.go
Brent Baude 0dac214f56 basic hypverv machine implementation
with libhvee, we are able to do the basics of podman machine management
on hyperv.  The basic functions like init, rm, stop, and start are all
functional.  Start and stop will periodically throw a benign error
processing the hyperv message being returned from the action.  The error
is described in the todo's below.

notable items:

* no podman commands will work (like ps, images, etc)
* the machine must be initialized with --image-path and fed a custom image.
* disk size is set to 100GB statically.
* the vm joins the default hyperv network which is TCP/IP network based.
* podman machine ssh does not work
* podman machine set does not work
* you can grab the ip address from hyperv and fake a machine connection
  with `podman system connection`.
* when booting, use the hyperv console to know the boot is complete.

TODOs:
* podman machine ssh
* podman machine set
* podman machine rm needs force bool
* disk size in NewMachine is set to 100GB
* podman start needs to wait until fully booted
* establish a boot complete signal from guest
* implement gvproxy like user networking
* fix benign failures in stop/start -> Error: error 2147749890 (FormatMessage failed with: The system cannot find message text for message number 0x%1 in the message file for %2.)

[NO NEW TESTS NEEDED]

Signed-off-by: Brent Baude <bbaude@redhat.com>
2023-03-17 16:02:28 -05:00

139 lines
3.0 KiB
Go

//go:build windows
// +build windows
package hypervctl
import (
"encoding/xml"
"fmt"
"io"
"strings"
"github.com/containers/libhvee/pkg/wmiext"
)
const (
KvpOperationFailed = 32768
KvpAccessDenied = 32769
KvpNotSupported = 32770
KvpStatusUnknown = 32771
KvpTimeoutOccurred = 32772
KvpIllegalArgument = 32773
KvpSystemInUse = 32774
KvpInvalidState = 32775
KvpIncorrectDataType = 32776
KvpSystemNotAvailable = 32777
KvpOutOfMemory = 32778
KvpNotFound = 32779
KvpExchangeDataItemName = "Msvm_KvpExchangeDataItem"
MemorySettingDataName = "Msvm_MemorySettingData"
)
type CimKvpItems struct {
Instances []CimKvpItem `xml:"INSTANCE"`
}
type CimKvpItem struct {
Properties []CimKvpItemProperty `xml:"PROPERTY"`
}
type CimKvpItemProperty struct {
Name string `xml:"NAME,attr"`
Value string `xml:"VALUE"`
}
type KvpError struct {
ErrorCode int
message string
}
func (k *KvpError) Error() string {
return fmt.Sprintf("%s (%d)", k.message, k.ErrorCode)
}
func createKvpItem(service *wmiext.Service, key string, value string) (string, error) {
item, err := service.SpawnInstance(KvpExchangeDataItemName)
if err != nil {
return "", err
}
defer item.Close()
_ = item.Put("Name", key)
_ = item.Put("Data", value)
_ = item.Put("Source", 0)
itemStr := item.GetCimText()
return itemStr, nil
}
func parseKvpMapXml(kvpXml string) (map[string]string, error) {
// Workaround XML decoder's inability to handle multiple root elements
r := io.MultiReader(
strings.NewReader("<root>"),
strings.NewReader(kvpXml),
strings.NewReader("</root>"),
)
var items CimKvpItems
if err := xml.NewDecoder(r).Decode(&items); err != nil {
return nil, err
}
ret := make(map[string]string)
for _, item := range items.Instances {
var key, value string
for _, prop := range item.Properties {
if strings.EqualFold(prop.Name, "Name") {
key = prop.Value
} else if strings.EqualFold(prop.Name, "Data") {
value = prop.Value
}
}
if len(key) > 0 {
ret[key] = value
}
}
return ret, nil
}
func translateKvpError(source error, illegalSuggestion string) error {
j, ok := source.(*wmiext.JobError)
if !ok {
return source
}
var message string
switch j.ErrorCode {
case KvpOperationFailed:
message = "Operation failed"
case KvpAccessDenied:
message = "Access denied"
case KvpNotSupported:
message = "Not supported"
case KvpStatusUnknown:
message = "Status is unknown"
case KvpTimeoutOccurred:
message = "Timeout occurred"
case KvpIllegalArgument:
message = "Illegal argument (" + illegalSuggestion + ")"
case KvpSystemInUse:
message = "System is in use"
case KvpInvalidState:
message = "Invalid state for this operation"
case KvpIncorrectDataType:
message = "Incorrect data type"
case KvpSystemNotAvailable:
message = "System is not available"
case KvpOutOfMemory:
message = "Out of memory"
case KvpNotFound:
message = "Not found"
default:
return source
}
return &KvpError{j.ErrorCode, message}
}