Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 22 additions & 9 deletions cmd/createvm/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"path/filepath"

"github.com/containers/libhvee/pkg/hypervctl"
"github.com/containers/libhvee/pkg/powershell"
)

func main() {
Expand All @@ -20,6 +21,14 @@ func main() {
return
}

if err := powershell.HypervAvailable(); err != nil {
panic(err)
}

if !powershell.IsHypervAdministrator() {
panic(powershell.ErrNotAdministrator)
}

vmName := os.Args[1]
vhdxFile := abs(os.Args[2])
isoFile := abs(os.Args[3])
Expand All @@ -37,21 +46,28 @@ func main() {
}
}

if exists, err := vmm.Exists(vmName); err != nil {
panic(err)
} else if exists {
panic(fmt.Errorf("machine %s already exists", vmName))
}

// System
systemSettings, err := hypervctl.NewSystemSettingsBuilder().
PrepareSystemSettings(vmName, nil).
PrepareMemorySettings(func(ms *hypervctl.MemorySettings) {
ms.DynamicMemoryEnabled = true
ms.VirtualQuantity = 8192 // Startup memory
ms.Reservation = 1024 // min
ms.Limit = 16384 // max
ms.StartupBytes = 8192 * 1024 * 1024 // 8GB
ms.MinimumBytes = 1024 * 1024 * 1024 // 1GB
ms.MaximumBytes = 16384 * 1024 * 1024 // 16GB
}).
PrepareProcessorSettings(func(ps *hypervctl.ProcessorSettings) {
ps.VirtualQuantity = 4 // 4 cores
ps.Count = 4 // 4 cores
}).
Build()

if err != nil {
fmt.Fprintf(os.Stderr, "error building system settings: %s\n", err)
panic(err)
}

Expand All @@ -60,7 +76,7 @@ func main() {
err = hypervctl.NewDriveSettingsBuilder(systemSettings).
AddScsiController().
AddSyntheticDiskDrive(0).
DefineVirtualHardDisk(vhdxFile, func(vhdss *hypervctl.VirtualHardDiskStorageSettings) {
DefineVirtualHardDisk(vhdxFile, func(vhdss *hypervctl.HardDiskDriveSettings) {
// set extra params like
// vhdss.IOPSLimit = 5000
}).
Expand Down Expand Up @@ -94,11 +110,8 @@ func main() {
panic(err)
}

if err = vm.AddKeyValuePair("fun", "pair"); err != nil {
panic(err)
}

fmt.Println(vm.Path())
fmt.Println(vm.Name)

fmt.Println("Done!")
}
Expand Down
11 changes: 10 additions & 1 deletion cmd/dumpvms/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"os"

"github.com/containers/libhvee/pkg/hypervctl"
"github.com/containers/libhvee/pkg/powershell"
)

const (
Expand All @@ -31,7 +32,7 @@ func getVms() (string, error) {

func dumpSummary() (string, error) {
vmms := hypervctl.VirtualMachineManager{}
summs, err := vmms.GetSummaryInformation(hypervctl.SummaryRequestNearAll)
summs, err := vmms.GetSummaryInformation()
if err != nil {
return "", fmt.Errorf("Could not retrieve virtual machine summaries: %v\n", err) // nolint:staticcheck
}
Expand All @@ -51,6 +52,14 @@ func main() {
err error
result string
)
if err := powershell.HypervAvailable(); err != nil {
panic(err)
}

if !powershell.IsHypervAdministrator() {
panic(powershell.ErrNotAdministrator)
}

args := os.Args
if len(args) != 2 {
printHelp()
Expand Down
9 changes: 9 additions & 0 deletions cmd/kvpctl/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"github.com/containers/libhvee/pkg/hypervctl"
"github.com/containers/libhvee/pkg/kvp/ginsu"
"github.com/containers/libhvee/pkg/powershell"
"github.com/sirupsen/logrus"
"golang.org/x/sys/windows"
)
Expand Down Expand Up @@ -68,6 +69,14 @@ func main() {
printHelp()
}

if err := powershell.HypervAvailable(); err != nil {
panic(err)
}

if !powershell.IsHypervAdministrator() {
panic(powershell.ErrNotAdministrator)
}

subCmd := getSubCommand(os.Args[2])
if subCmd == unknown {
fmt.Printf("error: unknown command %s\n", os.Args[2])
Expand Down
70 changes: 70 additions & 0 deletions cmd/managevm/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package main

import (
"fmt"
"os"

"github.com/containers/libhvee/pkg/hypervctl"
"github.com/containers/libhvee/pkg/powershell"
)

func main() {

if err := powershell.HypervAvailable(); err != nil {
panic(err)
}

if !powershell.IsHypervAdministrator() {
panic(powershell.ErrNotAdministrator)
}

if len(os.Args) < 2 {
fmt.Printf("Usage: %s <vm name> <start|stop|restart|status|remove> \n\n", os.Args[0])
return
}

vmName := os.Args[1]
action := os.Args[2]

vmm := hypervctl.VirtualMachineManager{}

exists, _, err := vmm.GetMachineExists(vmName)
if err != nil {
panic(err)
}
if !exists {
panic(fmt.Errorf("VM %s does not exist", vmName))
}

vm, err := vmm.GetMachine(vmName)
if err != nil {
panic(err)
}

switch action {
case "remove":
err = vm.Remove(vm.HardDrives[0].Path)
if err != nil {
fmt.Println(err)
panic(err)
}
case "start":
err = vm.Start()
case "stop":
err = vm.Stop()
case "restart":
err = vm.Stop()
if err != nil {
panic(err)
}
err = vm.Start()
if err != nil {
panic(err)
}
case "status":
fmt.Println(vm.GetState())
}
if err != nil {
panic(err)
}
}
17 changes: 13 additions & 4 deletions cmd/updatevm/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,19 @@ import (
"strconv"

"github.com/containers/libhvee/pkg/hypervctl"
"github.com/containers/libhvee/pkg/powershell"
)

func main() {

if err := powershell.HypervAvailable(); err != nil {
panic(err)
}

if !powershell.IsHypervAdministrator() {
panic(powershell.ErrNotAdministrator)
}

if len(os.Args) < 4 {
fmt.Printf("Usage: %s <vm name> <cores> <static mem>\n\n", os.Args[0])

Expand All @@ -37,12 +46,12 @@ func main() {
}

err = vm.UpdateProcessorMemSettings(func(ps *hypervctl.ProcessorSettings) {
ps.VirtualQuantity = cores
ps.Count = int64(cores)
}, func(ms *hypervctl.MemorySettings) {
ms.DynamicMemoryEnabled = false
ms.VirtualQuantity = mem
ms.Limit = mem
ms.Reservation = mem
ms.StartupBytes = mem
ms.MaximumBytes = mem
ms.MinimumBytes = mem
})
if err != nil {
panic(err)
Expand Down
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ go 1.23.3
toolchain go1.23.9

require (
github.com/go-ole/go-ole v1.3.0
github.com/onsi/ginkgo/v2 v2.27.1
github.com/onsi/gomega v1.38.2
github.com/schollz/progressbar/v3 v3.18.0
Expand Down
3 changes: 0 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ github.com/gkampitakis/go-snaps v0.5.15 h1:amyJrvM1D33cPHwVrjo9jQxX8g/7E2wYdZ+01
github.com/gkampitakis/go-snaps v0.5.15/go.mod h1:HNpx/9GoKisdhw9AFOBT1N7DBs9DiHo/hGheFGBZ+mc=
github.com/go-logr/logr v1.4.3 h1:CjnDlHq8ikf6E492q6eKboGOC0T8CDaOvkHCIg8idEI=
github.com/go-logr/logr v1.4.3/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
github.com/go-ole/go-ole v1.3.0 h1:Dt6ye7+vXGIKZ7Xtk4s6/xVdGDQynvom7xCFEdWr6uE=
github.com/go-ole/go-ole v1.3.0/go.mod h1:5LS6F96DhAwUc7C+1HLexzMXY1xGRSryjyPPKW6zv78=
github.com/go-task/slim-sprig/v3 v3.0.0 h1:sUs3vkvUymDpBKi3qH1YSqBQk9+9D/8M2mN1vB6EwHI=
github.com/go-task/slim-sprig/v3 v3.0.0/go.mod h1:W848ghGpv3Qj3dhTPRyJypKRiqCdHZiAzKg9hl15HA8=
github.com/goccy/go-yaml v1.18.0 h1:8W7wMFS12Pcas7KU+VVkaiCng+kG8QiFeFwzFb+rwuw=
Expand Down Expand Up @@ -84,7 +82,6 @@ golang.org/x/net v0.43.0/go.mod h1:vhO1fvI4dGsIjh73sWfUVjj3N7CA9WkKJNQm2svM6Jg=
golang.org/x/sync v0.16.0 h1:ycBJEhp9p4vXvUZNszeOq0kGTPghopOL8q0fq3vstxw=
golang.org/x/sync v0.16.0/go.mod h1:1dzgHSNfp02xaA81J2MS99Qcpr2w7fw1gpm99rleRqA=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.35.0 h1:vz1N37gP5bs89s7He8XuIYXpyY0+QlsKmzipCbUtyxI=
golang.org/x/sys v0.35.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
golang.org/x/term v0.34.0 h1:O/2T7POpk0ZZ7MAzMeWFSg6S5IpWd/RXDlM9hgM3DR4=
Expand Down
Loading