Repository navigation
Expand file tree
/
Copy pathnbd_notification_example_test.go
More file actions
137 lines (128 loc) · 3.3 KB
/
Copy pathnbd_notification_example_test.go
File metadata and controls
137 lines (128 loc) · 3.3 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"
"runtime"
"time"
"github.com/Code-Hex/vz/v4"
)
// This example requires macOS 14 or newer, the network client entitlement, and an
// NBD server at 127.0.0.1:10809 serving a disk named "export".
func ExampleNetworkBlockDeviceStorageDeviceAttachment_WasConnectedNotify() {
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
}
attachment, err := vz.NewNetworkBlockDeviceStorageDeviceAttachment(
"nbd://127.0.0.1:10809/export", 5*time.Second, true, vz.DiskSynchronizationModeFull,
)
if err != nil {
fmt.Println(err)
return
}
defer runtime.KeepAlive(attachment)
disk, err := vz.NewVirtioBlockDeviceConfiguration(attachment)
if err != nil {
fmt.Println(err)
return
}
config.SetStorageDevicesVirtualMachineConfiguration([]vz.StorageDeviceConfiguration{disk})
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 struct{}, 16)
stop, err := attachment.WasConnectedNotify(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 <-events:
fmt.Println("NBD connected or reconnected")
case <-ctx.Done():
fmt.Println(ctx.Err())
}
}
// Use a running NBD server at 127.0.0.1:10809 with a disk named "export".
// This example requires macOS 14 or newer and the network client entitlement.
// An error event means the NBD client can no longer function.
func ExampleNetworkBlockDeviceStorageDeviceAttachment_DidEncounterErrorNotify() {
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
}
attachment, err := vz.NewNetworkBlockDeviceStorageDeviceAttachment(
"nbd://127.0.0.1:10809/export", 5*time.Second, true, vz.DiskSynchronizationModeFull,
)
if err != nil {
fmt.Println(err)
return
}
defer runtime.KeepAlive(attachment)
disk, err := vz.NewVirtioBlockDeviceConfiguration(attachment)
if err != nil {
fmt.Println(err)
return
}
config.SetStorageDevicesVirtualMachineConfiguration([]vz.StorageDeviceConfiguration{disk})
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 error, 16)
stop, err := attachment.DidEncounterErrorNotify(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 err := <-events:
fmt.Println("unrecoverable NBD error:", err)
case <-ctx.Done():
fmt.Println(ctx.Err())
}
}