github.com/sneal/packer@v0.5.2/builder/vmware/common/driver_player5.go (about) 1 package common 2 3 import ( 4 "errors" 5 "fmt" 6 "os" 7 "os/exec" 8 "path/filepath" 9 "strings" 10 11 "github.com/mitchellh/multistep" 12 ) 13 14 // Player5LinuxDriver is a driver that can run VMware Player 5 on Linux. 15 type Player5LinuxDriver struct { 16 AppPath string 17 VdiskManagerPath string 18 QemuImgPath string 19 VmrunPath string 20 21 // SSHConfig are the SSH settings for the Fusion VM 22 SSHConfig *SSHConfig 23 } 24 25 func (d *Player5LinuxDriver) Clone(dst, src string) error { 26 return errors.New("Cloning is not supported with Player 5. Please use Player 6+.") 27 } 28 29 func (d *Player5LinuxDriver) CompactDisk(diskPath string) error { 30 if d.QemuImgPath != "" { 31 return d.qemuCompactDisk(diskPath) 32 } 33 34 defragCmd := exec.Command(d.VdiskManagerPath, "-d", diskPath) 35 if _, _, err := runAndLog(defragCmd); err != nil { 36 return err 37 } 38 39 shrinkCmd := exec.Command(d.VdiskManagerPath, "-k", diskPath) 40 if _, _, err := runAndLog(shrinkCmd); err != nil { 41 return err 42 } 43 44 return nil 45 } 46 47 func (d *Player5LinuxDriver) qemuCompactDisk(diskPath string) error { 48 cmd := exec.Command(d.QemuImgPath, "convert", "-f", "vmdk", "-O", "vmdk", "-o", "compat6", diskPath, diskPath+".new") 49 if _, _, err := runAndLog(cmd); err != nil { 50 return err 51 } 52 53 if err := os.Remove(diskPath); err != nil { 54 return err 55 } 56 57 if err := os.Rename(diskPath+".new", diskPath); err != nil { 58 return err 59 } 60 61 return nil 62 } 63 64 func (d *Player5LinuxDriver) CreateDisk(output string, size string, type_id string) error { 65 var cmd *exec.Cmd 66 if d.QemuImgPath != "" { 67 cmd = exec.Command(d.QemuImgPath, "create", "-f", "vmdk", "-o", "compat6", output, size) 68 } else { 69 cmd = exec.Command(d.VdiskManagerPath, "-c", "-s", size, "-a", "lsilogic", "-t", type_id, output) 70 } 71 if _, _, err := runAndLog(cmd); err != nil { 72 return err 73 } 74 75 return nil 76 } 77 78 func (d *Player5LinuxDriver) IsRunning(vmxPath string) (bool, error) { 79 vmxPath, err := filepath.Abs(vmxPath) 80 if err != nil { 81 return false, err 82 } 83 84 cmd := exec.Command(d.VmrunPath, "-T", "player", "list") 85 stdout, _, err := runAndLog(cmd) 86 if err != nil { 87 return false, err 88 } 89 90 for _, line := range strings.Split(stdout, "\n") { 91 if line == vmxPath { 92 return true, nil 93 } 94 } 95 96 return false, nil 97 } 98 99 func (d *Player5LinuxDriver) SSHAddress(state multistep.StateBag) (string, error) { 100 return SSHAddressFunc(d.SSHConfig)(state) 101 } 102 103 func (d *Player5LinuxDriver) Start(vmxPath string, headless bool) error { 104 guiArgument := "gui" 105 if headless { 106 guiArgument = "nogui" 107 } 108 109 cmd := exec.Command(d.VmrunPath, "-T", "player", "start", vmxPath, guiArgument) 110 if _, _, err := runAndLog(cmd); err != nil { 111 return err 112 } 113 114 return nil 115 } 116 117 func (d *Player5LinuxDriver) Stop(vmxPath string) error { 118 cmd := exec.Command(d.VmrunPath, "-T", "player", "stop", vmxPath, "hard") 119 if _, _, err := runAndLog(cmd); err != nil { 120 return err 121 } 122 123 return nil 124 } 125 126 func (d *Player5LinuxDriver) SuppressMessages(vmxPath string) error { 127 return nil 128 } 129 130 func (d *Player5LinuxDriver) Verify() error { 131 if err := d.findApp(); err != nil { 132 return fmt.Errorf("VMware Player application ('vmplayer') not found in path.") 133 } 134 135 if err := d.findVmrun(); err != nil { 136 return fmt.Errorf("Critical application 'vmrun' not found in path.") 137 } 138 139 if err := d.findVdiskManager(); err != nil { 140 if err := d.findQemuImg(); err != nil { 141 return fmt.Errorf( 142 "Neither 'vmware-vdiskmanager', nor 'qemu-img' found in path.\n" + 143 "One of these is required to configure disks for VMware Player.") 144 } 145 } 146 147 return nil 148 } 149 150 func (d *Player5LinuxDriver) findApp() error { 151 path, err := exec.LookPath("vmplayer") 152 if err != nil { 153 return err 154 } 155 d.AppPath = path 156 return nil 157 } 158 159 func (d *Player5LinuxDriver) findVdiskManager() error { 160 path, err := exec.LookPath("vmware-vdiskmanager") 161 if err != nil { 162 return err 163 } 164 d.VdiskManagerPath = path 165 return nil 166 } 167 168 func (d *Player5LinuxDriver) findQemuImg() error { 169 path, err := exec.LookPath("qemu-img") 170 if err != nil { 171 return err 172 } 173 d.QemuImgPath = path 174 return nil 175 } 176 177 func (d *Player5LinuxDriver) findVmrun() error { 178 path, err := exec.LookPath("vmrun") 179 if err != nil { 180 return err 181 } 182 d.VmrunPath = path 183 return nil 184 } 185 186 func (d *Player5LinuxDriver) ToolsIsoPath(flavor string) string { 187 return "/usr/lib/vmware/isoimages/" + flavor + ".iso" 188 } 189 190 func (d *Player5LinuxDriver) DhcpLeasesPath(device string) string { 191 return "/etc/vmware/" + device + "/dhcpd/dhcpd.leases" 192 }