github.com/kaixiang/packer@v0.5.2-0.20140114230416-1f5786b0d7f1/builder/virtualbox/common/driver_4_2.go (about) 1 package common 2 3 import ( 4 "bytes" 5 "fmt" 6 "log" 7 "os/exec" 8 "regexp" 9 "strings" 10 "time" 11 ) 12 13 type VBox42Driver struct { 14 // This is the path to the "VBoxManage" application. 15 VBoxManagePath string 16 } 17 18 func (d *VBox42Driver) CreateSATAController(vmName string, name string) error { 19 version, err := d.Version() 20 if err != nil { 21 return err 22 } 23 24 portCountArg := "--sataportcount" 25 if strings.HasPrefix(version, "4.3") { 26 portCountArg = "--portcount" 27 } 28 29 command := []string{ 30 "storagectl", vmName, 31 "--name", name, 32 "--add", "sata", 33 portCountArg, "1", 34 } 35 36 return d.VBoxManage(command...) 37 } 38 39 func (d *VBox42Driver) Delete(name string) error { 40 return d.VBoxManage("unregistervm", name, "--delete") 41 } 42 43 func (d *VBox42Driver) Import(name, path string) error { 44 args := []string{ 45 "import", path, 46 "--vsys", "0", 47 "--vmname", name, 48 } 49 50 return d.VBoxManage(args...) 51 } 52 53 func (d *VBox42Driver) IsRunning(name string) (bool, error) { 54 var stdout bytes.Buffer 55 56 cmd := exec.Command(d.VBoxManagePath, "showvminfo", name, "--machinereadable") 57 cmd.Stdout = &stdout 58 if err := cmd.Run(); err != nil { 59 return false, err 60 } 61 62 for _, line := range strings.Split(stdout.String(), "\n") { 63 // Need to trim off CR character when running in windows 64 line = strings.TrimRight(line, "\r") 65 66 if line == `VMState="running"` { 67 return true, nil 68 } 69 70 // We consider "stopping" to still be running. We wait for it to 71 // be completely stopped or some other state. 72 if line == `VMState="stopping"` { 73 return true, nil 74 } 75 76 // We consider "paused" to still be running. We wait for it to 77 // be completely stopped or some other state. 78 if line == `VMState="paused"` { 79 return true, nil 80 } 81 } 82 83 return false, nil 84 } 85 86 func (d *VBox42Driver) Stop(name string) error { 87 if err := d.VBoxManage("controlvm", name, "poweroff"); err != nil { 88 return err 89 } 90 91 // We sleep here for a little bit to let the session "unlock" 92 time.Sleep(2 * time.Second) 93 94 return nil 95 } 96 97 func (d *VBox42Driver) SuppressMessages() error { 98 extraData := map[string]string{ 99 "GUI/RegistrationData": "triesLeft=0", 100 "GUI/SuppressMessages": "confirmInputCapture,remindAboutAutoCapture,remindAboutMouseIntegrationOff,remindAboutMouseIntegrationOn,remindAboutWrongColorDepth", 101 "GUI/UpdateDate": fmt.Sprintf("1 d, %d-01-01, stable", time.Now().Year()+1), 102 "GUI/UpdateCheckCount": "60", 103 } 104 105 for k, v := range extraData { 106 if err := d.VBoxManage("setextradata", "global", k, v); err != nil { 107 return err 108 } 109 } 110 111 return nil 112 } 113 114 func (d *VBox42Driver) VBoxManage(args ...string) error { 115 var stdout, stderr bytes.Buffer 116 117 log.Printf("Executing VBoxManage: %#v", args) 118 cmd := exec.Command(d.VBoxManagePath, args...) 119 cmd.Stdout = &stdout 120 cmd.Stderr = &stderr 121 err := cmd.Run() 122 123 stdoutString := strings.TrimSpace(stdout.String()) 124 stderrString := strings.TrimSpace(stderr.String()) 125 126 if _, ok := err.(*exec.ExitError); ok { 127 err = fmt.Errorf("VBoxManage error: %s", stderrString) 128 } 129 130 log.Printf("stdout: %s", stdoutString) 131 log.Printf("stderr: %s", stderrString) 132 133 return err 134 } 135 136 func (d *VBox42Driver) Verify() error { 137 return nil 138 } 139 140 func (d *VBox42Driver) Version() (string, error) { 141 var stdout bytes.Buffer 142 143 cmd := exec.Command(d.VBoxManagePath, "--version") 144 cmd.Stdout = &stdout 145 if err := cmd.Run(); err != nil { 146 return "", err 147 } 148 149 versionOutput := strings.TrimSpace(stdout.String()) 150 log.Printf("VBoxManage --version output: %s", versionOutput) 151 152 // If the "--version" output contains vboxdrv, then this is indicative 153 // of problems with the VirtualBox setup and we shouldn't really continue, 154 // whether or not we can read the version. 155 if strings.Contains(versionOutput, "vboxdrv") { 156 return "", fmt.Errorf("VirtualBox is not properly setup: %s", versionOutput) 157 } 158 159 versionRe := regexp.MustCompile("[^.0-9]") 160 matches := versionRe.Split(versionOutput, 2) 161 if len(matches) == 0 || matches[0] == "" { 162 return "", fmt.Errorf("No version found: %s", versionOutput) 163 } 164 165 log.Printf("VirtualBox version: %s", matches[0]) 166 return matches[0], nil 167 }