@@ -3,8 +3,11 @@ package main
3
3
import (
4
4
"context"
5
5
"fmt"
6
+ "log"
6
7
"os"
8
+ "os/signal"
7
9
"runtime"
10
+ "syscall"
8
11
"time"
9
12
10
13
"github.com/Code-Hex/vz/v2"
@@ -35,95 +38,55 @@ func main() {
35
38
}
36
39
37
40
func run (ctx context.Context ) error {
38
- defer time .Sleep (time .Second )
39
- return installMacOS (ctx )
40
- }
41
-
42
- func installMacOS (ctx context.Context ) error {
43
- restoreImagePath := GetRestoreImagePath ()
44
- restoreImage , err := vz .LoadMacOSRestoreImageFromPath (restoreImagePath )
45
- if err != nil {
46
- return fmt .Errorf ("failed to load restore image: %w" , err )
47
- }
48
- configurationRequirements := restoreImage .MostFeaturefulSupportedConfiguration ()
49
- config , err := setupVirtualMachineWithMacOSConfigurationRequirements (
50
- configurationRequirements ,
51
- )
52
- if err != nil {
53
- return fmt .Errorf ("failed to setup config: %w" , err )
41
+ if false {
42
+ defer time .Sleep (time .Second )
43
+ return installMacOS (ctx )
54
44
}
55
- vm := vz .NewVirtualMachine (config )
56
-
57
- installer := vz .NewMacOSInstaller (vm , restoreImagePath )
58
-
59
- ctx , cancel := context .WithCancel (ctx )
60
- defer cancel ()
61
-
62
- go func () {
63
- ticker := time .NewTicker (500 * time .Millisecond )
64
- defer ticker .Stop ()
65
- for {
66
- select {
67
- case <- ctx .Done ():
68
- fmt .Println ("install has been cancelled" )
69
- return
70
- case <- installer .Done ():
71
- fmt .Println ("install has been completed" )
72
- return
73
- case <- ticker .C :
74
- fmt .Printf ("install: %d\r " , int (installer .FractionCompleted ()* 100 ))
75
- }
76
- }
77
- }()
78
-
79
- return installer .Install (ctx )
45
+ return runVM (ctx )
80
46
}
81
47
82
- func setupVirtualMachineWithMacOSConfigurationRequirements (macOSConfiguration * vz.MacOSConfigurationRequirements ) (* vz.VirtualMachineConfiguration , error ) {
83
- config := vz .NewVirtualMachineConfiguration (
84
- vz .NewMacOSBootLoader (),
85
- computeCPUCount (),
86
- computeMemorySize (),
87
- )
88
- platformConfig , err := createMacPlatformConfiguration (macOSConfiguration )
48
+ func runVM (ctx context.Context ) error {
49
+ platformConfig , err := createMacPlatformConfiguration ()
89
50
if err != nil {
90
- return nil , fmt . Errorf ( "failed to create mac platform config: %w" , err )
51
+ return err
91
52
}
92
- config .SetPlatformVirtualMachineConfiguration (platformConfig )
93
- config .SetGraphicsDevicesVirtualMachineConfiguration ([]vz.GraphicsDeviceConfiguration {
94
- createGraphicsDeviceConfiguration (),
95
- })
96
- blockDeviceConfig , err := createBlockDeviceConfiguration (GetDiskImagePath ())
53
+ config , err := setupVMConfiguration (platformConfig )
97
54
if err != nil {
98
- return nil , fmt . Errorf ( "failed to create block device configuration: %w" , err )
55
+ return err
99
56
}
100
- config .SetStorageDevicesVirtualMachineConfiguration ([]vz.StorageDeviceConfiguration {blockDeviceConfig })
101
-
102
- config .SetNetworkDevicesVirtualMachineConfiguration ([]* vz.VirtioNetworkDeviceConfiguration {
103
- createNetworkDeviceConfiguration (),
104
- })
57
+ vm := vz .NewVirtualMachine (config )
105
58
106
- config .SetPointingDevicesVirtualMachineConfiguration ([]vz.PointingDeviceConfiguration {
107
- createPointingDeviceConfiguration (),
108
- })
59
+ signalCh := make (chan os.Signal , 1 )
60
+ signal .Notify (signalCh , syscall .SIGTERM )
109
61
110
- config .SetKeyboardsVirtualMachineConfiguration ([]vz.KeyboardConfiguration {
111
- createKeyboardConfiguration (),
112
- })
62
+ errCh := make (chan error , 1 )
113
63
114
- config .SetAudioDevicesVirtualMachineConfiguration ([]vz.AudioDeviceConfiguration {
115
- createAudioDeviceConfiguration (),
64
+ vm .Start (func (err error ) {
65
+ if err != nil {
66
+ errCh <- err
67
+ }
116
68
})
117
69
118
- validated , err := config .Validate ()
119
- if err != nil {
120
- return nil , fmt .Errorf ("failed to validate configuration: %w" , err )
121
- }
122
- if ! validated {
123
- return nil , fmt .Errorf ("invalid configuration" )
70
+ for {
71
+ select {
72
+ case <- signalCh :
73
+ result , err := vm .RequestStop ()
74
+ if err != nil {
75
+ return err
76
+ }
77
+ log .Println ("recieved signal" , result )
78
+ case newState := <- vm .StateChangedNotify ():
79
+ if newState == vz .VirtualMachineStateRunning {
80
+ log .Println ("start VM is running" )
81
+ }
82
+ if newState == vz .VirtualMachineStateStopped {
83
+ log .Println ("stopped successfully" )
84
+ return nil
85
+ }
86
+ case err := <- errCh :
87
+ return fmt .Errorf ("failed to start vm: %w" , err )
88
+ }
124
89
}
125
-
126
- return config , nil
127
90
}
128
91
129
92
func computeCPUCount () uint {
@@ -161,8 +124,11 @@ func computeMemorySize() uint64 {
161
124
func createBlockDeviceConfiguration (diskPath string ) (* vz.VirtioBlockDeviceConfiguration , error ) {
162
125
// create disk image with 64 GiB
163
126
if err := vz .CreateDiskImage (diskPath , 64 * 1024 * 1024 * 1024 ); err != nil {
164
- return nil , fmt .Errorf ("failed to create disk image: %w" , err )
127
+ if ! os .IsExist (err ) {
128
+ return nil , fmt .Errorf ("failed to create disk image: %w" , err )
129
+ }
165
130
}
131
+
166
132
diskImageAttachment , err := vz .NewDiskImageStorageDeviceAttachment (
167
133
diskPath ,
168
134
false ,
@@ -174,37 +140,6 @@ func createBlockDeviceConfiguration(diskPath string) (*vz.VirtioBlockDeviceConfi
174
140
return storageDeviceConfig , nil
175
141
}
176
142
177
- func createMacPlatformConfiguration (macOSConfiguration * vz.MacOSConfigurationRequirements ) (* vz.MacPlatformConfiguration , error ) {
178
- hardwareModel := macOSConfiguration .HardwareModel ()
179
- if err := CreateFileAndWriteTo (
180
- hardwareModel .DataRepresentation (),
181
- GetHardwareModelPath (),
182
- ); err != nil {
183
- return nil , fmt .Errorf ("failed to write hardware model data: %w" , err )
184
- }
185
-
186
- machineIdentifier := vz .NewMacMachineIdentifier ()
187
- if err := CreateFileAndWriteTo (
188
- machineIdentifier .DataRepresentation (),
189
- GetMachineIdentifierPath (),
190
- ); err != nil {
191
- return nil , fmt .Errorf ("failed to write machine identifier data: %w" , err )
192
- }
193
-
194
- auxiliaryStorage , err := vz .NewMacAuxiliaryStorage (
195
- GetAuxiliaryStoragePath (),
196
- vz .WithCreating (hardwareModel ),
197
- )
198
- if err != nil {
199
- return nil , fmt .Errorf ("failed to create a new mac auxiliary storage: %w" , err )
200
- }
201
- return vz .NewMacPlatformConfiguration (
202
- vz .WithAuxiliaryStorage (auxiliaryStorage ),
203
- vz .WithHardwareModel (hardwareModel ),
204
- vz .WithMachineIdentifier (machineIdentifier ),
205
- ), nil
206
- }
207
-
208
143
func createGraphicsDeviceConfiguration () * vz.MacGraphicsDeviceConfiguration {
209
144
graphicDeviceConfig := vz .NewMacGraphicsDeviceConfiguration ()
210
145
graphicDeviceConfig .SetDisplays (
@@ -237,3 +172,70 @@ func createAudioDeviceConfiguration() *vz.VirtioSoundDeviceConfiguration {
237
172
)
238
173
return audioConfig
239
174
}
175
+
176
+ func createMacPlatformConfiguration () (* vz.MacPlatformConfiguration , error ) {
177
+ auxiliaryStorage , err := vz .NewMacAuxiliaryStorage (GetAuxiliaryStoragePath ())
178
+ if err != nil {
179
+ return nil , fmt .Errorf ("failed to create a new mac auxiliary storage: %w" , err )
180
+ }
181
+ hardwareModel , err := vz .NewMacHardwareModelWithDataPath (
182
+ GetHardwareModelPath (),
183
+ )
184
+ if err != nil {
185
+ return nil , fmt .Errorf ("failed to create a new hardware model: %w" , err )
186
+ }
187
+ machineIdentifier , err := vz .NewMacMachineIdentifierWithDataPath (
188
+ GetMachineIdentifierPath (),
189
+ )
190
+ if err != nil {
191
+ return nil , fmt .Errorf ("failed to create a new machine identifier: %w" , err )
192
+ }
193
+ return vz .NewMacPlatformConfiguration (
194
+ vz .WithAuxiliaryStorage (auxiliaryStorage ),
195
+ vz .WithHardwareModel (hardwareModel ),
196
+ vz .WithMachineIdentifier (machineIdentifier ),
197
+ ), nil
198
+ }
199
+
200
+ func setupVMConfiguration (platformConfig vz.PlatformConfiguration ) (* vz.VirtualMachineConfiguration , error ) {
201
+ config := vz .NewVirtualMachineConfiguration (
202
+ vz .NewMacOSBootLoader (),
203
+ computeCPUCount (),
204
+ computeMemorySize (),
205
+ )
206
+ config .SetPlatformVirtualMachineConfiguration (platformConfig )
207
+ config .SetGraphicsDevicesVirtualMachineConfiguration ([]vz.GraphicsDeviceConfiguration {
208
+ createGraphicsDeviceConfiguration (),
209
+ })
210
+ blockDeviceConfig , err := createBlockDeviceConfiguration (GetDiskImagePath ())
211
+ if err != nil {
212
+ return nil , fmt .Errorf ("failed to create block device configuration: %w" , err )
213
+ }
214
+ config .SetStorageDevicesVirtualMachineConfiguration ([]vz.StorageDeviceConfiguration {blockDeviceConfig })
215
+
216
+ config .SetNetworkDevicesVirtualMachineConfiguration ([]* vz.VirtioNetworkDeviceConfiguration {
217
+ createNetworkDeviceConfiguration (),
218
+ })
219
+
220
+ config .SetPointingDevicesVirtualMachineConfiguration ([]vz.PointingDeviceConfiguration {
221
+ createPointingDeviceConfiguration (),
222
+ })
223
+
224
+ config .SetKeyboardsVirtualMachineConfiguration ([]vz.KeyboardConfiguration {
225
+ createKeyboardConfiguration (),
226
+ })
227
+
228
+ config .SetAudioDevicesVirtualMachineConfiguration ([]vz.AudioDeviceConfiguration {
229
+ createAudioDeviceConfiguration (),
230
+ })
231
+
232
+ validated , err := config .Validate ()
233
+ if err != nil {
234
+ return nil , fmt .Errorf ("failed to validate configuration: %w" , err )
235
+ }
236
+ if ! validated {
237
+ return nil , fmt .Errorf ("invalid configuration" )
238
+ }
239
+
240
+ return config , nil
241
+ }
0 commit comments