-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvmwareify.go
112 lines (92 loc) · 3.22 KB
/
vmwareify.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
package vmwareify
import (
"bytes"
"errors"
"io"
"io/ioutil"
"os"
"unicode"
"github.com/stephen-fox/vmwareify/ovf"
)
// BasicConvert converts a non-VMWare .ovf file to a VMWare friendly .ovf
// file. It does the following:
//
// - Removes any IDE controllers
// - Converts any existing SATA controllers to the VMWare kind
// - Set the VMWare compatibility level to vmx-10
// - Disables automatic allocation of CD/DVD drives
func BasicConvert(ovfFilePath string, newFilePath string) error {
if ovfFilePath == newFilePath {
return errors.New("output .ovf file path cannot be the same as the input file path")
}
existing, err := os.Open(ovfFilePath)
if err != nil {
return err
}
defer existing.Close()
buff, err := basicConvert(existing)
if err != nil {
return err
}
info, err := existing.Stat()
if err != nil {
return err
}
err = ioutil.WriteFile(newFilePath, buff.Bytes(), info.Mode())
if err != nil {
return err
}
return nil
}
func basicConvert(existing io.Reader) (*bytes.Buffer, error) {
editScheme := ovf.NewEditScheme().
Propose(SetVirtualSystemTypeFunc("vmx-10"), ovf.VirtualHardwareSystemName).
Propose(RemoveIdeControllersFunc(-1), ovf.VirtualHardwareItemName).
Propose(ConvertSataControllersFunc(), ovf.VirtualHardwareItemName).
Propose(DisableCdromAutomaticAllocationFunc(), ovf.VirtualHardwareItemName)
buff, err := ovf.EditRawOvf(existing, editScheme)
if err != nil {
return bytes.NewBuffer(nil), err
}
return buff, nil
}
// SetVirtualSystemTypeFunc returns an ovf.EditObjectFunc that will set the
// .ovf's VirtualSystemType to the specified value.
func SetVirtualSystemTypeFunc(systemType string) ovf.EditObjectFunc {
return ovf.SetVirtualSystemTypeFunc(systemType)
}
// RemoveIdeControllersFunc returns an ovf.EditObjectFunc that will remove
// the specified number of IDE controllers.
func RemoveIdeControllersFunc(limit int) ovf.EditObjectFunc {
return ovf.DeleteHardwareItemsMatchingFunc("ideController", limit)
}
// ConvertSataControllersFunc returns an ovf.EditObjectFunc that
// will convert an existing SATA controller to a VMWare friendly
// SATA controller.
func ConvertSataControllersFunc() ovf.EditObjectFunc {
modifyFunc := func(sataController ovf.Item) ovf.Item {
sataController.Caption = "SATA Controller"
sataController.Description = "SATAController"
updatedElementNameBuffer := bytes.NewBuffer(nil)
updatedElementNameBuffer.WriteString("SATAController")
for i := range sataController.ElementName {
char := rune(sataController.ElementName[i])
if unicode.IsDigit(char) {
updatedElementNameBuffer.WriteString(string(char))
}
}
sataController.ElementName = updatedElementNameBuffer.String()
sataController.ResourceSubType = "vmware.sata.ahci"
return sataController
}
return ovf.ModifyHardwareItemsOfResourceTypeFunc(ovf.OtherStorageDeviceResourceType, modifyFunc)
}
// DisableCdromAutomaticAllocationFunc returns an ovf.EditObjectFunc that
// will disable AutomaticAllocation for OVF ResourceType 15 devices.
func DisableCdromAutomaticAllocationFunc() ovf.EditObjectFunc {
modifyFunc := func(cdrom ovf.Item) ovf.Item {
cdrom.AutomaticAllocation = false
return cdrom
}
return ovf.ModifyHardwareItemsOfResourceTypeFunc(ovf.CdDriveResourceType, modifyFunc)
}