2013-12-25 17:50:12 -05:00
|
|
|
package common
|
2013-07-12 19:03:32 -04:00
|
|
|
|
|
|
|
import (
|
2013-12-26 16:39:41 -05:00
|
|
|
"errors"
|
2013-07-12 19:03:32 -04:00
|
|
|
"fmt"
|
2014-05-12 21:35:37 -04:00
|
|
|
"log"
|
2013-07-12 19:03:32 -04:00
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
2013-12-24 13:31:57 -05:00
|
|
|
|
2018-01-19 19:18:44 -05:00
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
2013-07-12 19:03:32 -04:00
|
|
|
)
|
|
|
|
|
2014-05-12 21:35:37 -04:00
|
|
|
// Player5Driver is a driver that can run VMware Player 5 on Linux.
|
|
|
|
type Player5Driver struct {
|
2015-11-11 07:39:06 -05:00
|
|
|
VmwareDriver
|
|
|
|
|
2013-07-12 19:03:32 -04:00
|
|
|
AppPath string
|
|
|
|
VdiskManagerPath string
|
|
|
|
QemuImgPath string
|
|
|
|
VmrunPath string
|
2013-12-24 13:31:57 -05:00
|
|
|
|
|
|
|
// SSHConfig are the SSH settings for the Fusion VM
|
2013-12-25 17:50:12 -05:00
|
|
|
SSHConfig *SSHConfig
|
2013-07-12 19:03:32 -04:00
|
|
|
}
|
|
|
|
|
2014-05-12 21:35:37 -04:00
|
|
|
func (d *Player5Driver) Clone(dst, src string) error {
|
|
|
|
return errors.New("Cloning is not supported with VMWare Player version 5. Please use VMWare Player version 6, or greater.")
|
2013-12-26 16:39:41 -05:00
|
|
|
}
|
|
|
|
|
2014-05-12 21:35:37 -04:00
|
|
|
func (d *Player5Driver) CompactDisk(diskPath string) error {
|
2013-07-12 19:03:32 -04:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2014-05-12 21:35:37 -04:00
|
|
|
func (d *Player5Driver) qemuCompactDisk(diskPath string) error {
|
2013-07-16 18:02:27 -04:00
|
|
|
cmd := exec.Command(d.QemuImgPath, "convert", "-f", "vmdk", "-O", "vmdk", "-o", "compat6", diskPath, diskPath+".new")
|
2013-07-12 19:03:32 -04:00
|
|
|
if _, _, err := runAndLog(cmd); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := os.Remove(diskPath); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2013-07-16 18:02:27 -04:00
|
|
|
if err := os.Rename(diskPath+".new", diskPath); err != nil {
|
2013-07-12 19:03:32 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-05-12 21:35:37 -04:00
|
|
|
func (d *Player5Driver) CreateDisk(output string, size string, type_id string) error {
|
2013-07-12 19:03:32 -04:00
|
|
|
var cmd *exec.Cmd
|
|
|
|
if d.QemuImgPath != "" {
|
|
|
|
cmd = exec.Command(d.QemuImgPath, "create", "-f", "vmdk", "-o", "compat6", output, size)
|
|
|
|
} else {
|
2013-08-22 14:40:56 -04:00
|
|
|
cmd = exec.Command(d.VdiskManagerPath, "-c", "-s", size, "-a", "lsilogic", "-t", type_id, output)
|
2013-07-12 19:03:32 -04:00
|
|
|
}
|
|
|
|
if _, _, err := runAndLog(cmd); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-05-12 21:35:37 -04:00
|
|
|
func (d *Player5Driver) IsRunning(vmxPath string) (bool, error) {
|
2013-07-12 19:03:32 -04:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2015-06-13 19:23:33 -04:00
|
|
|
func (d *Player5Driver) CommHost(state multistep.StateBag) (string, error) {
|
|
|
|
return CommHost(d.SSHConfig)(state)
|
2013-11-07 15:00:27 -05:00
|
|
|
}
|
|
|
|
|
2014-05-12 21:35:37 -04:00
|
|
|
func (d *Player5Driver) Start(vmxPath string, headless bool) error {
|
2013-07-12 19:03:32 -04:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2014-05-12 21:35:37 -04:00
|
|
|
func (d *Player5Driver) Stop(vmxPath string) error {
|
2013-07-12 19:03:32 -04:00
|
|
|
cmd := exec.Command(d.VmrunPath, "-T", "player", "stop", vmxPath, "hard")
|
|
|
|
if _, _, err := runAndLog(cmd); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-05-12 21:35:37 -04:00
|
|
|
func (d *Player5Driver) SuppressMessages(vmxPath string) error {
|
2013-11-08 15:19:09 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-05-12 21:35:37 -04:00
|
|
|
func (d *Player5Driver) Verify() error {
|
|
|
|
var err error
|
|
|
|
if d.AppPath == "" {
|
|
|
|
if d.AppPath, err = playerFindVMware(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-07-12 19:03:32 -04:00
|
|
|
}
|
|
|
|
|
2014-05-12 21:35:37 -04:00
|
|
|
if d.VmrunPath == "" {
|
|
|
|
if d.VmrunPath, err = playerFindVmrun(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-07-12 19:03:32 -04:00
|
|
|
}
|
|
|
|
|
2014-05-12 21:35:37 -04:00
|
|
|
if d.VdiskManagerPath == "" {
|
|
|
|
d.VdiskManagerPath, err = playerFindVdiskManager()
|
2013-07-12 19:03:32 -04:00
|
|
|
}
|
|
|
|
|
2014-05-12 21:35:37 -04:00
|
|
|
if d.VdiskManagerPath == "" && d.QemuImgPath == "" {
|
|
|
|
d.QemuImgPath, err = playerFindQemuImg()
|
|
|
|
}
|
2013-07-12 19:03:32 -04:00
|
|
|
|
|
|
|
if err != nil {
|
2014-05-12 21:35:37 -04:00
|
|
|
return fmt.Errorf(
|
|
|
|
"Neither 'vmware-vdiskmanager', nor 'qemu-img' found in path.\n" +
|
|
|
|
"One of these is required to configure disks for VMware Player.")
|
2013-07-12 19:03:32 -04:00
|
|
|
}
|
|
|
|
|
2014-05-12 21:35:37 -04:00
|
|
|
log.Printf("VMware app path: %s", d.AppPath)
|
|
|
|
log.Printf("vmrun path: %s", d.VmrunPath)
|
|
|
|
log.Printf("vdisk-manager path: %s", d.VdiskManagerPath)
|
|
|
|
log.Printf("qemu-img path: %s", d.QemuImgPath)
|
|
|
|
|
|
|
|
if _, err := os.Stat(d.AppPath); err != nil {
|
|
|
|
return fmt.Errorf("VMware application not found: %s", d.AppPath)
|
2013-07-12 19:03:32 -04:00
|
|
|
}
|
|
|
|
|
2014-05-12 21:35:37 -04:00
|
|
|
if _, err := os.Stat(d.VmrunPath); err != nil {
|
|
|
|
return fmt.Errorf("'vmrun' application not found: %s", d.VmrunPath)
|
|
|
|
}
|
|
|
|
|
|
|
|
if d.VdiskManagerPath != "" {
|
|
|
|
_, err = os.Stat(d.VdiskManagerPath)
|
|
|
|
} else {
|
|
|
|
_, err = os.Stat(d.QemuImgPath)
|
2013-07-12 19:03:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
2014-05-12 21:35:37 -04:00
|
|
|
return fmt.Errorf(
|
|
|
|
"Neither 'vmware-vdiskmanager', nor 'qemu-img' found in path.\n" +
|
|
|
|
"One of these is required to configure disks for VMware Player.")
|
2013-07-12 19:03:32 -04:00
|
|
|
}
|
2014-05-12 21:35:37 -04:00
|
|
|
|
2015-11-11 07:39:06 -05:00
|
|
|
// Assigning the path callbacks to VmwareDriver
|
|
|
|
d.VmwareDriver.DhcpLeasesPath = func(device string) string {
|
|
|
|
return playerDhcpLeasesPath(device)
|
|
|
|
}
|
2016-04-05 21:45:58 -04:00
|
|
|
|
|
|
|
d.VmwareDriver.DhcpConfPath = func(device string) string {
|
|
|
|
return playerVmDhcpConfPath(device)
|
2015-11-11 07:39:06 -05:00
|
|
|
}
|
2016-04-05 21:45:58 -04:00
|
|
|
|
|
|
|
d.VmwareDriver.VmnetnatConfPath = func(device string) string {
|
|
|
|
return playerVmnetnatConfPath(device)
|
2015-11-11 07:39:06 -05:00
|
|
|
}
|
2016-04-05 21:45:58 -04:00
|
|
|
|
2015-11-11 07:39:06 -05:00
|
|
|
d.VmwareDriver.NetmapConfPath = func() string {
|
|
|
|
return playerNetmapConfPath()
|
|
|
|
}
|
2013-07-12 19:03:32 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-05-12 21:35:37 -04:00
|
|
|
func (d *Player5Driver) ToolsIsoPath(flavor string) string {
|
|
|
|
return playerToolsIsoPath(flavor)
|
2013-07-12 19:03:32 -04:00
|
|
|
}
|
|
|
|
|
2014-05-12 21:35:37 -04:00
|
|
|
func (d *Player5Driver) ToolsInstall() error {
|
2014-04-21 11:25:32 -04:00
|
|
|
return nil
|
|
|
|
}
|
2017-04-13 18:39:55 -04:00
|
|
|
|
|
|
|
func (d *Player5Driver) GetVmwareDriver() VmwareDriver {
|
|
|
|
return d.VmwareDriver
|
|
|
|
}
|