github.com/alouche/packer@v0.3.7/builder/vmware/driver_player5.go (about)

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