Esempio n. 1
0
func LaunchVM(c *VMConfig) (*exec.Cmd, error) {
	exists, err := vmExists(c.Name)
	if err != nil {
		return nil, err
	}
	if exists {
		err := DeleteVM(c)
		if err != nil {
			return nil, err
		}
	}
	err = vmCreate(c)
	if err != nil {
		return nil, err
	}
	cmd, err := VBoxHeadless("--startvm", c.Name)
	if err != nil {
		return nil, err
	}

	var conn net.Conn
	for i := 0; i < 5; i++ {
		conn, err = util.Connect(c.sockPath())
		if err == nil {
			break
		}
		time.Sleep(500 * time.Millisecond)
	}
	if err != nil {
		return nil, err
	}
	go io.Copy(conn, os.Stdin)
	go io.Copy(os.Stdout, conn)
	return cmd, nil
}
Esempio n. 2
0
func LaunchVM(c *VMConfig) (*exec.Cmd, error) {
	dir := c.InstanceDir
	err := os.MkdirAll(dir, 0777)
	if err != nil {
		fmt.Printf("mkdir failed: %s", dir)
		return nil, err
	}
	var cmd *exec.Cmd
	if runtime.GOOS == "windows" {
		cmd = exec.Command("cmd.exe", "/c", "copy", c.OriginalVMDK, c.Image)
	} else {
		cmd = exec.Command("cp", c.OriginalVMDK, c.Image)
	}
	_, err = cmd.Output()
	if err != nil {
		fmt.Printf("cp failed: %s", c.OriginalVMDK)
		return nil, err
	}
	err = vmCreateVMXFile(c)
	if err != nil {
		fmt.Printf("Create VMXFile failed: %s", c.VMXFile)
		return nil, err
	}
	cmd, err = vmxRun("-T", "ws", "start", c.VMXFile, "nogui")
	if err != nil {
		return nil, err
	}

	var conn net.Conn
	for i := 0; i < 5; i++ {
		conn, err = util.Connect(c.sockPath())
		if err == nil {
			break
		}
		time.Sleep(500 * time.Millisecond)
	}
	if err != nil {
		fmt.Println("err socket")
		return nil, err
	}

	done := make(chan bool)
	go io.Copy(conn, os.Stdin)
	go doRead(done, conn)

	// Wait until the serial connection is disconnected
	<-done

	return cmd, nil
}