-
Notifications
You must be signed in to change notification settings - Fork 18
/
vm.go
2070 lines (1833 loc) · 64.9 KB
/
vm.go
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package vix
/*
#include "vix.h"
#include "helper.h"
*/
import "C"
import (
"fmt"
"math"
"os"
"runtime"
"strconv"
"time"
"unsafe"
"github.com/hooklift/govmx"
)
// VM represents a virtual machine.
type VM struct {
// Internal VIX handle
handle C.VixHandle
vmxfile *VMXFile
}
// NewVirtualMachine creates a new VM instance.
func NewVirtualMachine(handle C.VixHandle, vmxpath string) (*VM, error) {
vmxfile := &VMXFile{
path: vmxpath,
}
// Loads VMX file in memory
err := vmxfile.Read()
if err != nil {
return nil, err
}
vm := &VM{
handle: handle,
vmxfile: vmxfile,
}
runtime.SetFinalizer(vm, cleanupVM)
return vm, nil
}
// Vcpus returns number of virtual CPUs configured for the virtual machine.
func (v *VM) Vcpus() (uint8, error) {
var err C.VixError = C.VIX_OK
vcpus := C.VIX_PROPERTY_NONE
err = C.get_property(v.handle,
C.VIX_PROPERTY_VM_NUM_VCPUS,
unsafe.Pointer(&vcpus))
if C.VIX_OK != err {
return 0, &Error{
Operation: "vm.Vcpus",
Code: int(err & 0xFFFF),
Text: C.GoString(C.Vix_GetErrorText(err, nil)),
}
}
return uint8(vcpus), nil
}
// VmxPath returns path to the virtual machine configuration file.
func (v *VM) VmxPath() (string, error) {
var err C.VixError = C.VIX_OK
var path *C.char
err = C.get_property(v.handle,
C.VIX_PROPERTY_VM_VMX_PATHNAME,
unsafe.Pointer(&path))
defer C.Vix_FreeBuffer(unsafe.Pointer(path))
if C.VIX_OK != err {
return "", &Error{
Operation: "vm.VmxPath",
Code: int(err & 0xFFFF),
Text: C.GoString(C.Vix_GetErrorText(err, nil)),
}
}
return C.GoString(path), nil
}
// VMTeamPath returns path to the virtual machine team.
func (v *VM) VMTeamPath() (string, error) {
var err C.VixError = C.VIX_OK
var path *C.char
err = C.get_property(v.handle,
C.VIX_PROPERTY_VM_VMTEAM_PATHNAME,
unsafe.Pointer(&path))
defer C.Vix_FreeBuffer(unsafe.Pointer(path))
if C.VIX_OK != err {
return "", &Error{
Operation: "vm.VMTeamPath",
Code: int(err & 0xFFFF),
Text: C.GoString(C.Vix_GetErrorText(err, nil)),
}
}
return C.GoString(path), nil
}
// MemorySize returns memory size of the virtual machine.
func (v *VM) MemorySize() (uint, error) {
var err C.VixError = C.VIX_OK
memsize := C.VIX_PROPERTY_NONE
err = C.get_property(v.handle,
C.VIX_PROPERTY_VM_MEMORY_SIZE,
unsafe.Pointer(&memsize))
if C.VIX_OK != err {
return 0, &Error{
Operation: "vm.MemorySize",
Code: int(err & 0xFFFF),
Text: C.GoString(C.Vix_GetErrorText(err, nil)),
}
}
return uint(memsize), nil
}
// ReadOnly tells whether or not the VM is read-only.
func (v *VM) ReadOnly() (bool, error) {
var err C.VixError = C.VIX_OK
readonly := C.VIX_PROPERTY_NONE
err = C.get_property(v.handle,
C.VIX_PROPERTY_VM_READ_ONLY,
unsafe.Pointer(&readonly))
if C.VIX_OK != err {
return false, &Error{
Operation: "vm.ReadOnly",
Code: int(err & 0xFFFF),
Text: C.GoString(C.Vix_GetErrorText(err, nil)),
}
}
if readonly == 0 {
return false, nil
}
return true, nil
}
// InVMTeam returns whether the virtual machine is a member of a team.
func (v *VM) InVMTeam() (bool, error) {
var err C.VixError = C.VIX_OK
inTeam := C.VIX_PROPERTY_NONE
err = C.get_property(v.handle,
C.VIX_PROPERTY_VM_IN_VMTEAM,
unsafe.Pointer(&inTeam))
if C.VIX_OK != err {
return false, &Error{
Operation: "vm.InVmTeam",
Code: int(err & 0xFFFF),
Text: C.GoString(C.Vix_GetErrorText(err, nil)),
}
}
if inTeam == 0 {
return false, nil
}
return true, nil
}
// PowerState returns power state of the virtual machine.
func (v *VM) PowerState() (VMPowerState, error) {
var err C.VixError = C.VIX_OK
var state C.VixPowerState = 0x0
err = C.get_property(v.handle,
C.VIX_PROPERTY_VM_POWER_STATE,
unsafe.Pointer(&state))
if C.VIX_OK != err {
return VMPowerState(0x0), &Error{
Operation: "vm.PowerState",
Code: int(err & 0xFFFF),
Text: C.GoString(C.Vix_GetErrorText(err, nil)),
}
}
return VMPowerState(state), nil
}
// ToolsState returns state of the VMware Tools suite in the guest.
func (v *VM) ToolsState() (GuestToolsState, error) {
var err C.VixError = C.VIX_OK
state := C.VIX_TOOLSSTATE_UNKNOWN
err = C.get_property(v.handle,
C.VIX_PROPERTY_VM_TOOLS_STATE,
unsafe.Pointer(&state))
if C.VIX_OK != err {
return TOOLSSTATE_UNKNOWN, &Error{
Operation: "vm.ToolsState",
Code: int(err & 0xFFFF),
Text: C.GoString(C.Vix_GetErrorText(err, nil)),
}
}
return GuestToolsState(state), nil
}
// IsRunning returns whether the virtual machine is running.
func (v *VM) IsRunning() (bool, error) {
var err C.VixError = C.VIX_OK
running := C.VIX_PROPERTY_NONE
err = C.get_property(v.handle,
C.VIX_PROPERTY_VM_IS_RUNNING,
unsafe.Pointer(&running))
if C.VIX_OK != err {
return false, &Error{
Operation: "vm.IsRunning",
Code: int(err & 0xFFFF),
Text: C.GoString(C.Vix_GetErrorText(err, nil)),
}
}
if running == 0 {
return false, nil
}
return true, nil
}
// GuestOS returns the guest os.
func (v *VM) GuestOS() (string, error) {
var err C.VixError = C.VIX_OK
var os *C.char
err = C.get_property(v.handle,
C.VIX_PROPERTY_VM_GUESTOS,
unsafe.Pointer(&os))
defer C.Vix_FreeBuffer(unsafe.Pointer(os))
if C.VIX_OK != err {
return "", &Error{
Operation: "vm.GuestOS",
Code: int(err & 0xFFFF),
Text: C.GoString(C.Vix_GetErrorText(err, nil)),
}
}
return C.GoString(os), nil
}
// Features returns VM supported features.
// func (v *VM) Features() (string, error) {
// var err C.VixError = C.VIX_OK
// var features *C.char
// err = C.get_property(v.handle,
// C.VIX_PROPERTY_VM_SUPPORTED_FEATURES,
// unsafe.Pointer(&features))
// defer C.Vix_FreeBuffer(unsafe.Pointer(features))
// if C.VIX_OK != err {
// return "", &VixError{
// code: int(err & 0xFFFF),
// text: C.GoString(C.Vix_GetErrorText(err, nil)),
// }
// }
// return C.GoString(features), nil
// }
// EnableSharedFolders enables or disables all shared folders as a feature for a
// virtual machine.
//
// Remarks:
//
// * This function enables/disables all shared folders as a feature on a
// virtual machine. In order to access shared folders on a guest, the
// feature has to be enabled, and in addition, the individual shared folder
// has to be enabled.
//
// * It is not necessary to call VM.LoginInGuest() before calling this function.
//
// * In this release, this function requires the virtual machine to be powered
// on with VMware Tools installed.
//
// * Shared folders are not supported for the following guest operating systems:
// Windows ME, Windows 98, Windows 95, Windows 3.x, and DOS.
//
// * On Linux virtual machines, calling this function will automatically mount
// shared folder(s) in the guest.
//
// Since VMware Workstation 6.0, not available on Server 2.0.
// Minimum Supported Guest OS: Microsoft Windows NT Series, Linux
//
func (v *VM) EnableSharedFolders(enabled bool) error {
var jobHandle C.VixHandle = C.VIX_INVALID_HANDLE
var err C.VixError = C.VIX_OK
var share C.Bool = C.FALSE
if enabled {
share = C.TRUE
}
jobHandle = C.VixVM_EnableSharedFolders(v.handle,
share,
0,
nil,
nil)
defer C.Vix_ReleaseHandle(jobHandle)
err = C.vix_job_wait(jobHandle)
if C.VIX_OK != err {
return &Error{
Operation: "vm.EnableSharedFolders",
Code: int(err & 0xFFFF),
Text: C.GoString(C.Vix_GetErrorText(err, nil)),
}
}
return nil
}
// AddSharedFolder mounts a new shared folder in the virtual machine.
//
// Parameters:
//
// guestpath: Specifies the guest path name of the new shared folder.
// hostpath: Specifies the host path of the shared folder.
// flags: The folder options.
//
// Remarks:
//
// * This function creates a local mount point in the guest file system and
// mounts a shared folder exported by the host.
//
// * Shared folders will only be accessible inside the guest operating system
// if shared folders are enabled for the virtual machine.
// See the documentation for VM.EnableSharedFolders().
//
// * The folder options include: SHAREDFOLDER_WRITE_ACCESS - Allow write access.
//
// * Only absolute paths should be used for files in the guest; the resolution
// of relative paths is not specified.
//
// * The hostpath argument must specify a path to a directory that exists on the
// host, or an error will result.
//
// * If a shared folder with the same name exists before calling this function,
// the job handle returned by this function will return VIX_E_ALREADY_EXISTS.
//
// * It is not necessary to call VM.LoginInGuest() before calling this function.
//
// * When creating shared folders in a Windows guest, there might be a delay
// before contents of a shared folder are visible to functions such as
// Guest.IsFile() and Guest.RunProgram().
//
// * Shared folders are not supported for the following guest operating
// systems: Windows ME, Windows 98, Windows 95, Windows 3.x, and DOS.
//
// * In this release, this function requires the virtual machine to be powered
// on with VMware Tools installed.
//
// * To determine in which directory in the guest the shared folder will be,
// query Guest.SharedFoldersParentDir(). When the virtual machine is powered
// on and the VMware Tools are running, this property will contain the path to
// the parent directory of the shared folders for that virtual machine.
//
// Since VMware Workstation 6.0, not available on Server 2.0.
//
func (v *VM) AddSharedFolder(guestpath, hostpath string, flags SharedFolderOption) error {
var jobHandle C.VixHandle = C.VIX_INVALID_HANDLE
var err C.VixError = C.VIX_OK
gpath := C.CString(guestpath)
hpath := C.CString(hostpath)
defer C.free(unsafe.Pointer(gpath))
defer C.free(unsafe.Pointer(hpath))
jobHandle = C.VixVM_AddSharedFolder(v.handle,
gpath,
hpath,
C.VixMsgSharedFolderOptions(flags),
nil, nil)
defer C.Vix_ReleaseHandle(jobHandle)
err = C.vix_job_wait(jobHandle)
if C.VIX_OK != err {
return &Error{
Operation: "vm.AddSharedFolder",
Code: int(err & 0xFFFF),
Text: C.GoString(C.Vix_GetErrorText(err, nil)),
}
}
return nil
}
// RemoveSharedFolder removes a shared folder in the virtual machine.
//
// Parameters:
//
// guestpath: Specifies the guest pathname of the shared folder to delete.
//
// Remarks:
//
// * This function removes a shared folder in the virtual machine referenced by
// the VM object
//
// * It is not necessary to call VM.LoginInGuest() before calling this function.
//
// * Shared folders are not supported for the following guest operating
// systems: Windows ME, Windows 98, Windows 95, Windows 3.x, and DOS.
//
// * In this release, this function requires the virtual machine to be powered
// on with VMware Tools installed.
//
// * Depending on the behavior of the guest operating system, when removing
// shared folders, there might be a delay before the shared folder is no
// longer visible to programs running within the guest operating system and
// to functions such as Guest.IsFile()
//
// Since VMware Workstation 6.0
//
func (v *VM) RemoveSharedFolder(guestpath string) error {
var jobHandle C.VixHandle = C.VIX_INVALID_HANDLE
var err C.VixError = C.VIX_OK
gpath := C.CString(guestpath)
defer C.free(unsafe.Pointer(gpath))
jobHandle = C.VixVM_RemoveSharedFolder(v.handle,
gpath,
0,
nil, nil)
defer C.Vix_ReleaseHandle(jobHandle)
err = C.vix_job_wait(jobHandle)
if C.VIX_OK != err {
return &Error{
Operation: "vm.RemoveSharedFolder",
Code: int(err & 0xFFFF),
Text: C.GoString(C.Vix_GetErrorText(err, nil)),
}
}
return nil
}
// Screenshot captures the screen of the guest operating system.
//
// Remarks:
//
// * This function captures the current screen image and returns it as a
// []byte result.
//
// * For security reasons, this function requires a successful call to
// VM.LoginInGuest() must be made.
//
// Since VMware Workstation 6.5
// Minimum Supported Guest OS: Microsoft Windows NT Series, Linux
//
func (v *VM) Screenshot() ([]byte, error) {
var jobHandle C.VixHandle = C.VIX_INVALID_HANDLE
var err C.VixError = C.VIX_OK
var byteCount C.int
var screenBits C.char
jobHandle = C.VixVM_CaptureScreenImage(v.handle,
C.VIX_CAPTURESCREENFORMAT_PNG,
C.VIX_INVALID_HANDLE,
nil,
nil)
defer C.Vix_ReleaseHandle(jobHandle)
err = C.get_screenshot_bytes(jobHandle, &byteCount, &screenBits)
defer C.Vix_FreeBuffer(unsafe.Pointer(&screenBits))
if C.VIX_OK != err {
return nil, &Error{
Operation: "vm.Screenshot",
Code: int(err & 0xFFFF),
Text: C.GoString(C.Vix_GetErrorText(err, nil)),
}
}
return C.GoBytes(unsafe.Pointer(&screenBits), byteCount), nil
}
// Clone creates a copy of the virtual machine specified by the current VM instance.
//
// Parameters:
//
// cloneType: Must be either CLONETYPE_FULL or CLONETYPE_LINKED.
// * CLONETYPE_FULL: Creates a full, independent clone of the virtual machine.
// * CLONETYPE_LINKED: Creates a linked clone, which is a copy of a virtual
// machine that shares virtual disks with the parent
// virtual machine in an ongoing manner.
// This conserves disk space as long as the parent and
// clone do not change too much from their original state.
//
// destVmxFile: The path name of the virtual machine configuration file that will
// be created for the virtual machine clone produced by this operation.
// This should be a full absolute path name, with directory names delineated
// according to host system convention: \ for Windows and / for Linux.
//
// Remarks:
//
// * The function returns a new VM instance which is a clone of its parent VM.
//
// * It is not possible to create a full clone of a powered on virtual machine.
// You must power off or suspend a virtual machine before creating a full
// clone of that machine.
//
// * With a suspended virtual machine, requesting a linked clone results in
// error 3007 VIX_E_VM_IS_RUNNING.
// Suspended virtual machines retain memory state, so proceeding with a
// linked clone could cause loss of data.
//
// * A linked clone must have access to the parent's virtual disks. Without
// such access, you cannot use a linked clone
// at all because its file system will likely be incomplete or corrupt.
//
// * Deleting a virtual machine that is the parent of a linked clone renders
// the linked clone useless.
//
// * Because a full clone does not share virtual disks with the parent virtual
// machine, full clones generally perform better than linked clones.
// However, full clones take longer to create than linked clones. Creating a
// full clone can take several minutes if the files involved are large.
//
// * This function is not supported when using the VMWARE_PLAYER provider.
//
func (v *VM) Clone(cloneType CloneType, destVmxFile string) (*VM, error) {
var jobHandle C.VixHandle = C.VIX_INVALID_HANDLE
var clonedHandle C.VixHandle = C.VIX_INVALID_HANDLE
var err C.VixError = C.VIX_OK
dstVmxFile := C.CString(destVmxFile)
defer C.free(unsafe.Pointer(dstVmxFile))
jobHandle = C.VixVM_Clone(v.handle,
C.VIX_INVALID_HANDLE, // snapshotHandle
C.VixCloneType(cloneType), // cloneType
dstVmxFile, // destConfigPathName
0, // options,
C.VIX_INVALID_HANDLE, // propertyListHandle
nil, // callbackProc
nil) // clientData
defer C.Vix_ReleaseHandle(jobHandle)
err = C.get_vix_handle(jobHandle,
C.VIX_PROPERTY_JOB_RESULT_HANDLE,
&clonedHandle,
C.VIX_PROPERTY_NONE)
if C.VIX_OK != err {
return nil, &Error{
Operation: "vm.Clone",
Code: int(err & 0xFFFF),
Text: C.GoString(C.Vix_GetErrorText(err, nil)),
}
}
return NewVirtualMachine(clonedHandle, destVmxFile)
}
// cleanupVM cleans up VM VIX handle.
func cleanupVM(v *VM) {
if v.handle != C.VIX_INVALID_HANDLE {
C.Vix_ReleaseHandle(v.handle)
v.handle = C.VIX_INVALID_HANDLE
}
}
// CreateSnapshot saves a copy of the virtual machine state as a snapshot object.
//
// Parameters:
//
// name: A user-defined name for the snapshot; need not be unique.
//
// description: A user-defined description for the snapshot.
//
// options: Flags to specify how the snapshot should be created. Any combination of the
// following or 0 to exclude memory:
// * SNAPSHOT_INCLUDE_MEMORY: Captures the full state of a running virtual
// machine, including the memory.
//
// Remarks:
//
// * This function creates a child snapshot of the current snapshot.
//
// * If a virtual machine is suspended, you cannot snapshot it more than once.
//
// * If a powered-on virtual machine gets a snapshot created with option 0
// (exclude memory), the power state is not saved, so reverting to the
// snapshot sets powered-off state.
//
// * The 'name' and 'description' parameters can be set but not retrieved
// using the VIX API.
//
// * VMware Server supports only a single snapshot for each virtual machine.
// The following considerations apply to VMware Server:
// * If you call this function a second time for the same virtual machine
// without first deleting the snapshot,
// the second call will overwrite the previous snapshot.
// * A virtual machine imported to VMware Server from another VMware product
// might have more than one snapshot at the time it is imported. In that
// case, you can use this function to add a new snapshot to the series.
//
// * Starting in VMware Workstation 6.5, snapshot operations are allowed on
// virtual machines that are part of a team.
// Previously, this operation failed with error code
// VIX_PROPERTY_VM_IN_VMTEAM. Team members snapshot independently so they can
// have different and inconsistent snapshot states.
//
// * This function is not supported when using the VMWARE_PLAYER provider.
//
// * If the virtual machine is open and powered off in the UI, this function now
// closes the virtual machine in the UI before creating the snapshot.
//
// Since VMware Workstation 6.0
//
func (v *VM) CreateSnapshot(name, description string, options CreateSnapshotOption) (*Snapshot, error) {
var jobHandle C.VixHandle = C.VIX_INVALID_HANDLE
var snapshotHandle C.VixHandle = C.VIX_INVALID_HANDLE
var err C.VixError = C.VIX_OK
sname := C.CString(name)
sdesc := C.CString(description)
defer C.free(unsafe.Pointer(sname))
defer C.free(unsafe.Pointer(sdesc))
jobHandle = C.VixVM_CreateSnapshot(v.handle,
sname, // name
sdesc, // description
C.VixCreateSnapshotOptions(options), // options
C.VIX_INVALID_HANDLE, // propertyListHandle
nil, // callbackProc
nil) // clientData
err = C.get_vix_handle(jobHandle,
C.VIX_PROPERTY_JOB_RESULT_HANDLE,
&snapshotHandle,
C.VIX_PROPERTY_NONE)
if C.VIX_OK != err {
return nil, &Error{
Operation: "vm.CreateSnapshot",
Code: int(err & 0xFFFF),
Text: C.GoString(C.Vix_GetErrorText(err, nil)),
}
}
snapshot := &Snapshot{
handle: snapshotHandle,
}
runtime.SetFinalizer(snapshot, cleanupSnapshot)
return snapshot, nil
}
// RemoveSnapshot deletes all saved states for the snapshot.
//
// Parameters:
//
// snapshot: A Snapshot instance. Call VM.RootSnapshot() to get a snapshot instance.
//
// Remarks:
//
// * This function deletes all saved states for the specified snapshot. If the
// snapshot was based on another snapshot, the base snapshot becomes the new
// root snapshot.
//
// * The VMware Server release of the VIX API can manage only a single snapshot
// for each virtual machine. A virtual machine imported from another VMware
// product can have more than one snapshot at the time it is imported. In that
// case, you can delete only a snapshot subsequently added using the VIX API.
//
// * Starting in VMware Workstation 6.5, snapshot operations are allowed on
// virtual machines that are part of a team. Previously, this operation
// failed with error code VIX_PROPERTY_VM_IN_VMTEAM. Team members snapshot
// independently so they can have different and inconsistent snapshot states.
//
// * This function is not supported when using the VMWARE_PLAYER provider
//
// * If the virtual machine is open and powered off in the UI, this function may
// close the virtual machine in the UI before deleting the snapshot.
//
// Since VMware Server 1.0
//
func (v *VM) RemoveSnapshot(snapshot *Snapshot, options RemoveSnapshotOption) error {
var jobHandle C.VixHandle = C.VIX_INVALID_HANDLE
var err C.VixError = C.VIX_OK
jobHandle = C.VixVM_RemoveSnapshot(v.handle, //vmHandle
snapshot.handle, //snapshotHandle
C.VixRemoveSnapshotOptions(options), //options
nil, //callbackProc
nil) //clientData
defer C.Vix_ReleaseHandle(jobHandle)
err = C.vix_job_wait(jobHandle)
if C.VIX_OK != err {
return &Error{
Operation: "vm.RemoveSnapshot",
Code: int(err & 0xFFFF),
Text: C.GoString(C.Vix_GetErrorText(err, nil)),
}
}
return nil
}
// Delete permanently deletes a virtual machine from your host system.
//
// Parameters:
//
// options: For VMware Server 2.0 and ESX, this value must be VMDELETE_DISK_FILES.
// For all other versions it can be either VMDELETE_KEEP_FILES or VMDELETE_DISK_FILES.
// When option is VMDELETE_DISK_FILES, it deletes all associated files.
// When option is VMDELETE_KEEP_FILES, does not delete *.vmdk virtual disk file(s).
//
// Remarks:
//
// * This function permanently deletes a virtual machine from your host system.
// You can accomplish the same effect by deleting all virtual machine files
// using the host file system. This function simplifies the task by deleting
// all VMware files in a single function call.
// If a deleteOptions value of VMDELETE_KEEP_FILES is specified, the virtual disk (vmdk) files
// will not be deleted.
// This function does not delete other user files in the virtual machine
// folder.
//
// * This function is successful only if the virtual machine is powered off or
// suspended.
//
// * Deleting a virtual machine that is the parent of a linked clone renders
// the linked clone useless.
//
// * If the machine was powered on with GUI enabled, this function is going to
// return error VIX_E_FILE_ALREADY_LOCKED if the VM tab is open. If you want
// to force the removal despite this, pass the option VMDELETE_FORCE.
//
// since VMware Server 1.0
//
func (v *VM) Delete(options VmDeleteOption) error {
var jobHandle C.VixHandle = C.VIX_INVALID_HANDLE
var err C.VixError = C.VIX_OK
if (options & VMDELETE_FORCE) != 0 {
vmxPath, err := v.VmxPath()
if err != nil {
return err
}
err = os.RemoveAll(vmxPath + ".lck")
if err != nil {
return err
}
}
jobHandle = C.VixVM_Delete(v.handle, C.VixVMDeleteOptions(options), nil, nil)
defer C.Vix_ReleaseHandle(jobHandle)
err = C.vix_job_wait(jobHandle)
if C.VIX_OK != err {
return &Error{
Operation: "vm.Delete",
Code: int(err & 0xFFFF),
Text: C.GoString(C.Vix_GetErrorText(err, nil)),
}
}
C.Vix_ReleaseHandle(v.handle)
return nil
}
// CurrentSnapshot returns the handle of the current active snapshot belonging to
// the virtual machine.
//
// Remarks:
//
// * This function returns the handle of the current active snapshot belonging
// to the virtual machine.
//
// * This function is not supported when using the VMWARE_PLAYER provider
//
// Since VMware Workstation 6.0
//
func (v *VM) CurrentSnapshot() (*Snapshot, error) {
var snapshotHandle C.VixHandle = C.VIX_INVALID_HANDLE
var err C.VixError = C.VIX_OK
err = C.VixVM_GetCurrentSnapshot(v.handle, &snapshotHandle)
if C.VIX_OK != err {
return nil, &Error{
Operation: "vm.CurrentSnapshot",
Code: int(err & 0xFFFF),
Text: C.GoString(C.Vix_GetErrorText(err, nil)),
}
}
snapshot := &Snapshot{handle: snapshotHandle}
runtime.SetFinalizer(snapshot, cleanupSnapshot)
return snapshot, nil
}
// SnapshotByName returns a Snapshot object matching the given name.
//
// Parameters:
//
// name: Identifies a snapshot name.
//
// Remarks:
//
// * When the snapshot name is a duplicate, it returns error 13017
// VIX_E_SNAPSHOT_NONUNIQUE_NAME.
//
// * When there are multiple snapshots with the same name, or the same path to
// that name, you cannot specify a unique name, but you can to use the UI to
// rename duplicates.
//
// * You can specify the snapshot name as a path using '/' or '\\' as path
// separators, including snapshots in the tree above the named snapshot,
// for example 'a/b/c' or 'x/x'. Do not mix '/' and '\\' in the same path
// expression.
//
// * This function is not supported when using the VMWARE_PLAYER provider.
//
// Since VMware Workstation 6.0
//
func (v *VM) SnapshotByName(name string) (*Snapshot, error) {
var snapshotHandle C.VixHandle = C.VIX_INVALID_HANDLE
var err C.VixError = C.VIX_OK
sname := C.CString(name)
defer C.free(unsafe.Pointer(sname))
err = C.VixVM_GetNamedSnapshot(v.handle, sname, &snapshotHandle)
if C.VIX_OK != err {
return nil, &Error{
Operation: "vm.SnapshotByName",
Code: int(err & 0xFFFF),
Text: C.GoString(C.Vix_GetErrorText(err, nil)),
}
}
snapshot := &Snapshot{handle: snapshotHandle}
// FIXME(c4milo): Replace with cleanUp function as we've done for VM and Host.
runtime.SetFinalizer(snapshot, cleanupSnapshot)
return snapshot, nil
}
// TotalRootSnapshots returns the number of top-level (root) snapshots belonging to a
// virtual machine.
//
// Remarks:
//
// * This function returns the number of top-level (root) snapshots belonging to
// a virtual machine.
// A top-level snapshot is one that is not based on any previous snapshot.
// If the virtual machine has more than one snapshot, the snapshots can be a
// sequence in which each snapshot is based on the previous one, leaving only
// a single top-level snapshot.
// However, if applications create branched trees of snapshots, a single
// virtual machine can have several top-level snapshots.
//
// * VMware Server can manage only a single snapshot for each virtual machine.
// All other snapshots in a sequence are ignored. The return value is always
// 0 or 1.
//
// * This function is not supported when using the VMWARE_PLAYER provider
//
// Since VMware Workstation 6.0
//
func (v *VM) TotalRootSnapshots() (int, error) {
var result C.int
var err C.VixError = C.VIX_OK
err = C.VixVM_GetNumRootSnapshots(v.handle, &result)
if C.VIX_OK != err {
return 0, &Error{
Operation: "vm.TotalRootSnapshots",
Code: int(err & 0xFFFF),
Text: C.GoString(C.Vix_GetErrorText(err, nil)),
}
}
return int(result), nil
}
// TotalSharedFolders returns the number of shared folders mounted in the virtual
// machine.
//
// Remarks:
//
// * This function returns the number of shared folders mounted in the virtual
// machine.
//
// * It is not necessary to call VM.LoginInGuest() before calling this function.
//
// * Shared folders are not supported for the following guest operating systems:
// Windows ME, Windows 98, Windows 95, Windows 3.x, and DOS.
//
// * In this release, this function requires the virtual machine to be powered
// on with VMware Tools installed.
//
// Since VMware Workstation 6.0
//
func (v *VM) TotalSharedFolders() (int, error) {
var jobHandle C.VixHandle = C.VIX_INVALID_HANDLE
var err C.VixError = C.VIX_OK
var numSharedFolders C.int
jobHandle = C.VixVM_GetNumSharedFolders(v.handle, nil, nil)
defer C.Vix_ReleaseHandle(jobHandle)
err = C.get_num_shared_folders(jobHandle, &numSharedFolders)
if C.VIX_OK != err {
return 0, &Error{
Operation: "vm.TotalSharedFolders",
Code: int(err & 0xFFFF),
Text: C.GoString(C.Vix_GetErrorText(err, nil)),
}
}
return int(numSharedFolders), nil
}
// RootSnapshot returns a root Snapshot instance belonging to the current virtual machine.
//
// Parameters:
//
// index: Identifies a root snapshot. See below for range of values.
//
// Remarks:
//
// * Snapshots are indexed from 0 to n-1, where n is the number of root
// snapshots. Use the function VM.TotalRootSnapshots to get the value of n.
//
// * VMware Server can manage only a single snapshot for each virtual machine.
// The value of index can only be 0.
//
// * This function is not supported when using the VMWARE_PLAYER provider
//
// Since VMware Server 1.0
//
func (v *VM) RootSnapshot(index int) (*Snapshot, error) {
var snapshotHandle C.VixHandle = C.VIX_INVALID_HANDLE
var err C.VixError = C.VIX_OK
err = C.VixVM_GetRootSnapshot(v.handle,
C.int(index),
&snapshotHandle)
if C.VIX_OK != err {
return nil, &Error{
Operation: "vm.RootSnapshot",
Code: int(err & 0xFFFF),
Text: C.GoString(C.Vix_GetErrorText(err, nil)),
}
}
snapshot := &Snapshot{handle: snapshotHandle}
runtime.SetFinalizer(snapshot, cleanupSnapshot)