Repository navigation
Expand file tree
/
Copy pathdevice_notification_example_test.go
More file actions
137 lines (128 loc) · 3.49 KB
/
Copy pathdevice_notification_example_test.go
File metadata and controls
137 lines (128 loc) · 3.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
//go:build darwin
package vz_test
import (
"context"
"fmt"
"time"
"github.com/Code-Hex/vz/v4"
)
// The guest must open or close the virtio port named "control" to produce an event.
// This example requires macOS 13 or newer and a guest with virtio console support.
func ExampleVirtualMachine_ConsolePortChangedNotify() {
loader, err := vz.NewLinuxBootLoader("testdata/Image", vz.WithInitrd("testdata/initramfs.cpio.gz"))
if err != nil {
fmt.Println(err)
return
}
config, err := vz.NewVirtualMachineConfiguration(loader, 1, 512*1024*1024)
if err != nil {
fmt.Println(err)
return
}
console, err := vz.NewVirtioConsoleDeviceConfiguration()
if err != nil {
fmt.Println(err)
return
}
port, err := vz.NewVirtioConsolePortConfiguration(vz.WithVirtioConsolePortConfigurationName("control"))
if err != nil {
fmt.Println(err)
return
}
console.SetVirtioConsolePortConfiguration(1, port)
config.SetConsoleDevicesVirtualMachineConfiguration([]vz.ConsoleDeviceConfiguration{console})
machine, err := vz.NewVirtualMachine(config)
if err != nil {
fmt.Println(err)
return
}
defer func() {
if machine.CanStop() {
if err := machine.Stop(); err != nil {
fmt.Println(err)
}
}
}()
events := make(chan vz.ConsolePortEvent, 16)
stop, err := machine.ConsolePortChangedNotify(events)
if err != nil {
fmt.Println(err)
return
}
defer stop()
if err := machine.Start(); err != nil {
fmt.Println(err)
return
}
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
select {
case event := <-events:
fmt.Printf("console %d port %d open=%t\n", event.DeviceIndex, event.PortIndex, event.Open)
// Before talking to a new guest process, process or flush pending attachment data.
case <-ctx.Done():
fmt.Println(ctx.Err())
}
}
// This example requires macOS 14 or newer and a guest with virtio graphics support.
func ExampleVirtualMachine_GraphicsDisplayChangedNotify() {
loader, err := vz.NewLinuxBootLoader("testdata/Image", vz.WithInitrd("testdata/initramfs.cpio.gz"))
if err != nil {
fmt.Println(err)
return
}
config, err := vz.NewVirtualMachineConfiguration(loader, 1, 512*1024*1024)
if err != nil {
fmt.Println(err)
return
}
graphics, err := vz.NewVirtioGraphicsDeviceConfiguration()
if err != nil {
fmt.Println(err)
return
}
scanout, err := vz.NewVirtioGraphicsScanoutConfiguration(800, 600)
if err != nil {
fmt.Println(err)
return
}
graphics.SetScanouts(scanout)
config.SetGraphicsDevicesVirtualMachineConfiguration([]vz.GraphicsDeviceConfiguration{graphics})
machine, err := vz.NewVirtualMachine(config)
if err != nil {
fmt.Println(err)
return
}
defer func() {
if machine.CanStop() {
if err := machine.Stop(); err != nil {
fmt.Println(err)
}
}
}()
events := make(chan vz.GraphicsDisplayEvent, 16)
stop, err := machine.GraphicsDisplayChangedNotify(events)
if err != nil {
fmt.Println(err)
return
}
defer stop()
if err := machine.Start(); err != nil {
fmt.Println(err)
return
}
display := machine.GraphicsDevices()[0].Displays()[0]
if err := display.ReconfigureWithSizeInPixels(1024, 768); err != nil {
fmt.Println(err)
return
}
// The guest may ignore the request, and either reconfiguration event may be dropped.
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
select {
case event := <-events:
fmt.Printf("graphics %d display %d reconfiguring=%t\n", event.DeviceIndex, event.DisplayIndex, event.Reconfiguring)
case <-ctx.Done():
fmt.Println(ctx.Err())
}
}