This repository has been archived by the owner on Jul 20, 2022. It is now read-only.
forked from hashicorp/packer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
driver_player5.go
174 lines (143 loc) · 3.81 KB
/
driver_player5.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
package vmware
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
)
// Player5LinuxDriver is a driver that can run VMware Player 5 on Linux.
type Player5LinuxDriver struct {
AppPath string
VdiskManagerPath string
QemuImgPath string
VmrunPath string
}
func (d *Player5LinuxDriver) CompactDisk(diskPath string) error {
if d.QemuImgPath != "" {
return d.qemuCompactDisk(diskPath)
}
defragCmd := exec.Command(d.VdiskManagerPath, "-d", diskPath)
if _, _, err := runAndLog(defragCmd); err != nil {
return err
}
shrinkCmd := exec.Command(d.VdiskManagerPath, "-k", diskPath)
if _, _, err := runAndLog(shrinkCmd); err != nil {
return err
}
return nil
}
func (d *Player5LinuxDriver) qemuCompactDisk(diskPath string) error {
cmd := exec.Command(d.QemuImgPath, "convert", "-f", "vmdk", "-O", "vmdk", "-o", "compat6", diskPath, diskPath+".new")
if _, _, err := runAndLog(cmd); err != nil {
return err
}
if err := os.Remove(diskPath); err != nil {
return err
}
if err := os.Rename(diskPath+".new", diskPath); err != nil {
return err
}
return nil
}
func (d *Player5LinuxDriver) CreateDisk(output string, size string, type_id string) error {
var cmd *exec.Cmd
if d.QemuImgPath != "" {
cmd = exec.Command(d.QemuImgPath, "create", "-f", "vmdk", "-o", "compat6", output, size)
} else {
cmd = exec.Command(d.VdiskManagerPath, "-c", "-s", size, "-a", "lsilogic", "-t", type_id, output)
}
if _, _, err := runAndLog(cmd); err != nil {
return err
}
return nil
}
func (d *Player5LinuxDriver) IsRunning(vmxPath string) (bool, error) {
vmxPath, err := filepath.Abs(vmxPath)
if err != nil {
return false, err
}
cmd := exec.Command(d.VmrunPath, "-T", "player", "list")
stdout, _, err := runAndLog(cmd)
if err != nil {
return false, err
}
for _, line := range strings.Split(stdout, "\n") {
if line == vmxPath {
return true, nil
}
}
return false, nil
}
func (d *Player5LinuxDriver) Start(vmxPath string, headless bool) error {
guiArgument := "gui"
if headless {
guiArgument = "nogui"
}
cmd := exec.Command(d.VmrunPath, "-T", "player", "start", vmxPath, guiArgument)
if _, _, err := runAndLog(cmd); err != nil {
return err
}
return nil
}
func (d *Player5LinuxDriver) Stop(vmxPath string) error {
cmd := exec.Command(d.VmrunPath, "-T", "player", "stop", vmxPath, "hard")
if _, _, err := runAndLog(cmd); err != nil {
return err
}
return nil
}
func (d *Player5LinuxDriver) Verify() error {
if err := d.findApp(); err != nil {
return fmt.Errorf("VMware Player application ('vmplayer') not found in path.")
}
if err := d.findVmrun(); err != nil {
return fmt.Errorf("Critical application 'vmrun' not found in path.")
}
if err := d.findVdiskManager(); err != nil {
if err := d.findQemuImg(); err != nil {
return fmt.Errorf(
"Neither 'vmware-vdiskmanager', nor 'qemu-img' found in path.\n" +
"One of these is required to configure disks for VMware Player.")
}
}
return nil
}
func (d *Player5LinuxDriver) findApp() error {
path, err := exec.LookPath("vmplayer")
if err != nil {
return err
}
d.AppPath = path
return nil
}
func (d *Player5LinuxDriver) findVdiskManager() error {
path, err := exec.LookPath("vmware-vdiskmanager")
if err != nil {
return err
}
d.VdiskManagerPath = path
return nil
}
func (d *Player5LinuxDriver) findQemuImg() error {
path, err := exec.LookPath("qemu-img")
if err != nil {
return err
}
d.QemuImgPath = path
return nil
}
func (d *Player5LinuxDriver) findVmrun() error {
path, err := exec.LookPath("vmrun")
if err != nil {
return err
}
d.VmrunPath = path
return nil
}
func (d *Player5LinuxDriver) ToolsIsoPath(flavor string) string {
return "/usr/lib/vmware/isoimages/" + flavor + ".iso"
}
func (d *Player5LinuxDriver) DhcpLeasesPath(device string) string {
return "/etc/vmware/" + device + "/dhcpd/dhcpd.leases"
}