github.com/daniellockard/packer@v0.7.6-0.20141210173435-5a9390934716/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) Iso() (string, error) { 44 var stdout bytes.Buffer 45 46 cmd := exec.Command(d.VBoxManagePath, "list", "systemproperties") 47 cmd.Stdout = &stdout 48 if err := cmd.Run(); err != nil { 49 return "", err 50 } 51 52 DefaultGuestAdditionsRe := regexp.MustCompile("Default Guest Additions ISO:(.+)") 53 54 for _, line := range strings.Split(stdout.String(), "\n") { 55 // Need to trim off CR character when running in windows 56 // Trimming whitespaces at this point helps to filter out empty value 57 line = strings.TrimRight(line, " \r") 58 59 matches := DefaultGuestAdditionsRe.FindStringSubmatch(line) 60 if matches == nil { 61 continue 62 } 63 64 isoname := strings.Trim(matches[1], " \r\n") 65 log.Printf("Found Default Guest Additions ISO: %s", isoname) 66 67 return isoname, nil 68 } 69 70 return "", fmt.Errorf("Cannot find \"Default Guest Additions ISO\" in vboxmanage output (or it is empty)") 71 } 72 73 func (d *VBox42Driver) Import(name string, path string, flags []string) error { 74 args := []string{ 75 "import", path, 76 "--vsys", "0", 77 "--vmname", name, 78 } 79 args = append(args, flags...) 80 81 return d.VBoxManage(args...) 82 } 83 84 func (d *VBox42Driver) IsRunning(name string) (bool, error) { 85 var stdout bytes.Buffer 86 87 cmd := exec.Command(d.VBoxManagePath, "showvminfo", name, "--machinereadable") 88 cmd.Stdout = &stdout 89 if err := cmd.Run(); err != nil { 90 return false, err 91 } 92 93 for _, line := range strings.Split(stdout.String(), "\n") { 94 // Need to trim off CR character when running in windows 95 line = strings.TrimRight(line, "\r") 96 97 if line == `VMState="running"` { 98 return true, nil 99 } 100 101 // We consider "stopping" to still be running. We wait for it to 102 // be completely stopped or some other state. 103 if line == `VMState="stopping"` { 104 return true, nil 105 } 106 107 // We consider "paused" to still be running. We wait for it to 108 // be completely stopped or some other state. 109 if line == `VMState="paused"` { 110 return true, nil 111 } 112 } 113 114 return false, nil 115 } 116 117 func (d *VBox42Driver) Stop(name string) error { 118 if err := d.VBoxManage("controlvm", name, "poweroff"); err != nil { 119 return err 120 } 121 122 // We sleep here for a little bit to let the session "unlock" 123 time.Sleep(2 * time.Second) 124 125 return nil 126 } 127 128 func (d *VBox42Driver) SuppressMessages() error { 129 extraData := map[string]string{ 130 "GUI/RegistrationData": "triesLeft=0", 131 "GUI/SuppressMessages": "confirmInputCapture,remindAboutAutoCapture,remindAboutMouseIntegrationOff,remindAboutMouseIntegrationOn,remindAboutWrongColorDepth", 132 "GUI/UpdateDate": fmt.Sprintf("1 d, %d-01-01, stable", time.Now().Year()+1), 133 "GUI/UpdateCheckCount": "60", 134 } 135 136 for k, v := range extraData { 137 if err := d.VBoxManage("setextradata", "global", k, v); err != nil { 138 return err 139 } 140 } 141 142 return nil 143 } 144 145 func (d *VBox42Driver) VBoxManage(args ...string) error { 146 var stdout, stderr bytes.Buffer 147 148 log.Printf("Executing VBoxManage: %#v", args) 149 cmd := exec.Command(d.VBoxManagePath, args...) 150 cmd.Stdout = &stdout 151 cmd.Stderr = &stderr 152 err := cmd.Run() 153 154 stdoutString := strings.TrimSpace(stdout.String()) 155 stderrString := strings.TrimSpace(stderr.String()) 156 157 if _, ok := err.(*exec.ExitError); ok { 158 err = fmt.Errorf("VBoxManage error: %s", stderrString) 159 } 160 161 if err == nil { 162 // Sometimes VBoxManage gives us an error with a zero exit code, 163 // so we also regexp match an error string. 164 m, _ := regexp.MatchString("VBoxManage([.a-z]+?): error:", stderrString) 165 if m { 166 err = fmt.Errorf("VBoxManage error: %s", stderrString) 167 } 168 } 169 170 log.Printf("stdout: %s", stdoutString) 171 log.Printf("stderr: %s", stderrString) 172 173 return err 174 } 175 176 func (d *VBox42Driver) Verify() error { 177 return nil 178 } 179 180 func (d *VBox42Driver) Version() (string, error) { 181 var stdout bytes.Buffer 182 183 cmd := exec.Command(d.VBoxManagePath, "--version") 184 cmd.Stdout = &stdout 185 if err := cmd.Run(); err != nil { 186 return "", err 187 } 188 189 versionOutput := strings.TrimSpace(stdout.String()) 190 log.Printf("VBoxManage --version output: %s", versionOutput) 191 192 // If the "--version" output contains vboxdrv, then this is indicative 193 // of problems with the VirtualBox setup and we shouldn't really continue, 194 // whether or not we can read the version. 195 if strings.Contains(versionOutput, "vboxdrv") { 196 return "", fmt.Errorf("VirtualBox is not properly setup: %s", versionOutput) 197 } 198 199 versionRe := regexp.MustCompile("^([.0-9]+)(?:_(?:RC|OSEr)[0-9]+)?") 200 matches := versionRe.FindAllStringSubmatch(versionOutput, 1) 201 if matches == nil || len(matches[0]) != 2 { 202 return "", fmt.Errorf("No version found: %s", versionOutput) 203 } 204 205 log.Printf("VirtualBox version: %s", matches[0][1]) 206 return matches[0][1], nil 207 }