Skip to content

Commit 074c531

Browse files
committed
Use PowerShell command to interact with Hyper-V
Replace wmi calls with powershell commands. This should improve error messages and remove need of executing this code with escalating privileges. The workflow should be the same, the most changes is in 'MemorySettings' memory field names and types, it should be set in bytes. And 'ProcessorSettings' has other name for processors count field. Signed-off-by: Yevhen Vydolob <[email protected]> Signed-off-by: Yevhen Vydolob <[email protected]>
1 parent cc07f05 commit 074c531

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+2405
-4239
lines changed

cmd/createvm/main.go

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"path/filepath"
1010

1111
"github.com/containers/libhvee/pkg/hypervctl"
12+
"github.com/containers/libhvee/pkg/powershell"
1213
)
1314

1415
func main() {
@@ -20,6 +21,14 @@ func main() {
2021
return
2122
}
2223

24+
if err := powershell.HypervAvailable(); err != nil {
25+
panic(err)
26+
}
27+
28+
if !powershell.IsHypervAdministrator() {
29+
panic(powershell.ErrNotAdministrator)
30+
}
31+
2332
vmName := os.Args[1]
2433
vhdxFile := abs(os.Args[2])
2534
isoFile := abs(os.Args[3])
@@ -37,21 +46,28 @@ func main() {
3746
}
3847
}
3948

49+
if exists, err := vmm.Exists(vmName); err != nil {
50+
panic(err)
51+
} else if exists {
52+
panic(fmt.Errorf("machine %s already exists", vmName))
53+
}
54+
4055
// System
4156
systemSettings, err := hypervctl.NewSystemSettingsBuilder().
4257
PrepareSystemSettings(vmName, nil).
4358
PrepareMemorySettings(func(ms *hypervctl.MemorySettings) {
4459
ms.DynamicMemoryEnabled = true
45-
ms.VirtualQuantity = 8192 // Startup memory
46-
ms.Reservation = 1024 // min
47-
ms.Limit = 16384 // max
60+
ms.StartupBytes = 8192 * 1024 * 1024 // 8GB
61+
ms.MinimumBytes = 1024 * 1024 * 1024 // 1GB
62+
ms.MaximumBytes = 16384 * 1024 * 1024 // 16GB
4863
}).
4964
PrepareProcessorSettings(func(ps *hypervctl.ProcessorSettings) {
50-
ps.VirtualQuantity = 4 // 4 cores
65+
ps.Count = 4 // 4 cores
5166
}).
5267
Build()
5368

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

@@ -60,7 +76,7 @@ func main() {
6076
err = hypervctl.NewDriveSettingsBuilder(systemSettings).
6177
AddScsiController().
6278
AddSyntheticDiskDrive(0).
63-
DefineVirtualHardDisk(vhdxFile, func(vhdss *hypervctl.VirtualHardDiskStorageSettings) {
79+
DefineVirtualHardDisk(vhdxFile, func(vhdss *hypervctl.HardDiskDriveSettings) {
6480
// set extra params like
6581
// vhdss.IOPSLimit = 5000
6682
}).
@@ -94,11 +110,8 @@ func main() {
94110
panic(err)
95111
}
96112

97-
if err = vm.AddKeyValuePair("fun", "pair"); err != nil {
98-
panic(err)
99-
}
100113

101-
fmt.Println(vm.Path())
114+
fmt.Println(vm.Name)
102115

103116
fmt.Println("Done!")
104117
}

cmd/dumpvms/main.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"os"
1010

1111
"github.com/containers/libhvee/pkg/hypervctl"
12+
"github.com/containers/libhvee/pkg/powershell"
1213
)
1314

1415
const (
@@ -31,7 +32,7 @@ func getVms() (string, error) {
3132

3233
func dumpSummary() (string, error) {
3334
vmms := hypervctl.VirtualMachineManager{}
34-
summs, err := vmms.GetSummaryInformation(hypervctl.SummaryRequestNearAll)
35+
summs, err := vmms.GetSummaryInformation()
3536
if err != nil {
3637
return "", fmt.Errorf("Could not retrieve virtual machine summaries: %v\n", err) // nolint:staticcheck
3738
}
@@ -51,6 +52,14 @@ func main() {
5152
err error
5253
result string
5354
)
55+
if err := powershell.HypervAvailable(); err != nil {
56+
panic(err)
57+
}
58+
59+
if !powershell.IsHypervAdministrator() {
60+
panic(powershell.ErrNotAdministrator)
61+
}
62+
5463
args := os.Args
5564
if len(args) != 2 {
5665
printHelp()

cmd/kvpctl/main.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212

1313
"github.com/containers/libhvee/pkg/hypervctl"
1414
"github.com/containers/libhvee/pkg/kvp/ginsu"
15+
"github.com/containers/libhvee/pkg/powershell"
1516
"github.com/sirupsen/logrus"
1617
"golang.org/x/sys/windows"
1718
)
@@ -68,6 +69,14 @@ func main() {
6869
printHelp()
6970
}
7071

72+
if err := powershell.HypervAvailable(); err != nil {
73+
panic(err)
74+
}
75+
76+
if !powershell.IsHypervAdministrator() {
77+
panic(powershell.ErrNotAdministrator)
78+
}
79+
7180
subCmd := getSubCommand(os.Args[2])
7281
if subCmd == unknown {
7382
fmt.Printf("error: unknown command %s\n", os.Args[2])

cmd/managevm/main.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"os"
6+
7+
"github.com/containers/libhvee/pkg/hypervctl"
8+
"github.com/containers/libhvee/pkg/powershell"
9+
)
10+
11+
func main() {
12+
13+
if err := powershell.HypervAvailable(); err != nil {
14+
panic(err)
15+
}
16+
17+
if !powershell.IsHypervAdministrator() {
18+
panic(powershell.ErrNotAdministrator)
19+
}
20+
21+
if len(os.Args) < 2 {
22+
fmt.Printf("Usage: %s <vm name> <start|stop|restart|status|remove> \n\n", os.Args[0])
23+
return
24+
}
25+
26+
vmName := os.Args[1]
27+
action := os.Args[2]
28+
29+
vmm := hypervctl.VirtualMachineManager{}
30+
31+
vm, err := vmm.GetMachine(vmName)
32+
if err != nil {
33+
panic(err)
34+
}
35+
36+
switch action {
37+
case "remove":
38+
err = vm.Remove(vm.HardDrives[0].Path)
39+
if err != nil {
40+
fmt.Println(err)
41+
panic(err)
42+
}
43+
case "start":
44+
err = vm.Start()
45+
case "stop":
46+
err = vm.Stop()
47+
case "restart":
48+
err = vm.Stop()
49+
if err != nil {
50+
panic(err)
51+
}
52+
err = vm.Start()
53+
if err != nil {
54+
panic(err)
55+
}
56+
case "status":
57+
fmt.Println(vm.GetState())
58+
}
59+
if err != nil {
60+
panic(err)
61+
}
62+
}

cmd/updatevm/main.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,19 @@ import (
99
"strconv"
1010

1111
"github.com/containers/libhvee/pkg/hypervctl"
12+
"github.com/containers/libhvee/pkg/powershell"
1213
)
1314

1415
func main() {
1516

17+
if err := powershell.HypervAvailable(); err != nil {
18+
panic(err)
19+
}
20+
21+
if !powershell.IsHypervAdministrator() {
22+
panic(powershell.ErrNotAdministrator)
23+
}
24+
1625
if len(os.Args) < 4 {
1726
fmt.Printf("Usage: %s <vm name> <cores> <static mem>\n\n", os.Args[0])
1827

@@ -37,12 +46,12 @@ func main() {
3746
}
3847

3948
err = vm.UpdateProcessorMemSettings(func(ps *hypervctl.ProcessorSettings) {
40-
ps.VirtualQuantity = cores
49+
ps.Count = int64(cores)
4150
}, func(ms *hypervctl.MemorySettings) {
4251
ms.DynamicMemoryEnabled = false
43-
ms.VirtualQuantity = mem
44-
ms.Limit = mem
45-
ms.Reservation = mem
52+
ms.StartupBytes = mem
53+
ms.MaximumBytes = mem
54+
ms.MinimumBytes = mem
4655
})
4756
if err != nil {
4857
panic(err)

0 commit comments

Comments
 (0)