2013-09-02 23:23:52 -04:00
|
|
|
package qemu
|
|
|
|
|
|
|
|
import (
|
2013-11-05 20:59:09 -05:00
|
|
|
"bufio"
|
2013-09-02 23:23:52 -04:00
|
|
|
"bytes"
|
|
|
|
"fmt"
|
2013-11-05 20:59:09 -05:00
|
|
|
"io"
|
2013-09-02 23:23:52 -04:00
|
|
|
"log"
|
2020-09-16 14:52:19 -04:00
|
|
|
"os"
|
2013-09-02 23:23:52 -04:00
|
|
|
"os/exec"
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
2013-11-06 00:40:49 -05:00
|
|
|
"sync"
|
|
|
|
"syscall"
|
2013-11-06 01:03:43 -05:00
|
|
|
"time"
|
2013-11-05 20:59:09 -05:00
|
|
|
"unicode"
|
2015-09-08 06:40:23 -04:00
|
|
|
|
2018-01-19 19:18:44 -05:00
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
2013-09-02 23:23:52 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type DriverCancelCallback func(state multistep.StateBag) bool
|
|
|
|
|
2013-09-03 19:13:05 -04:00
|
|
|
// A driver is able to talk to qemu-system-x86_64 and perform certain
|
2013-09-02 23:23:52 -04:00
|
|
|
// operations with it.
|
|
|
|
type Driver interface {
|
2020-09-16 14:52:19 -04:00
|
|
|
// Copy bypasses qemu-img convert and directly copies an image
|
|
|
|
// that doesn't need converting.
|
|
|
|
Copy(string, string) error
|
|
|
|
|
2013-09-02 23:23:52 -04:00
|
|
|
// Stop stops a running machine, forcefully.
|
2013-11-06 00:40:49 -05:00
|
|
|
Stop() error
|
2013-09-02 23:23:52 -04:00
|
|
|
|
|
|
|
// Qemu executes the given command via qemu-system-x86_64
|
2013-11-06 00:40:49 -05:00
|
|
|
Qemu(qemuArgs ...string) error
|
2013-09-02 23:23:52 -04:00
|
|
|
|
|
|
|
// wait on shutdown of the VM with option to cancel
|
2013-11-06 00:40:49 -05:00
|
|
|
WaitForShutdown(<-chan struct{}) bool
|
2013-09-02 23:23:52 -04:00
|
|
|
|
|
|
|
// Qemu executes the given command via qemu-img
|
|
|
|
QemuImg(...string) error
|
|
|
|
|
|
|
|
// Verify checks to make sure that this driver should function
|
|
|
|
// properly. If there is any indication the driver can't function,
|
|
|
|
// this will return an error.
|
|
|
|
Verify() error
|
|
|
|
|
2013-09-03 19:13:05 -04:00
|
|
|
// Version reads the version of Qemu that is installed.
|
2013-09-02 23:23:52 -04:00
|
|
|
Version() (string, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
type QemuDriver struct {
|
2013-11-06 00:43:20 -05:00
|
|
|
QemuPath string
|
|
|
|
QemuImgPath string
|
2013-09-02 23:23:52 -04:00
|
|
|
|
2013-11-06 00:40:49 -05:00
|
|
|
vmCmd *exec.Cmd
|
|
|
|
vmEndCh <-chan int
|
|
|
|
lock sync.Mutex
|
2013-09-02 23:23:52 -04:00
|
|
|
}
|
|
|
|
|
2013-11-06 00:40:49 -05:00
|
|
|
func (d *QemuDriver) Stop() error {
|
|
|
|
d.lock.Lock()
|
|
|
|
defer d.lock.Unlock()
|
2013-09-02 23:23:52 -04:00
|
|
|
|
2013-11-06 00:40:49 -05:00
|
|
|
if d.vmCmd != nil {
|
|
|
|
if err := d.vmCmd.Process.Kill(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-09-02 23:23:52 -04:00
|
|
|
}
|
2013-11-06 00:40:49 -05:00
|
|
|
|
2013-09-02 23:23:52 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-09-16 14:52:19 -04:00
|
|
|
func (d *QemuDriver) Copy(sourceName, targetName string) error {
|
|
|
|
source, err := os.Open(sourceName)
|
|
|
|
if err != nil {
|
|
|
|
err = fmt.Errorf("Error opening iso for copy: %s", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer source.Close()
|
|
|
|
|
|
|
|
// Create will truncate an existing file
|
|
|
|
target, err := os.Create(targetName)
|
|
|
|
if err != nil {
|
|
|
|
err = fmt.Errorf("Error creating hard drive in output dir: %s", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer target.Close()
|
|
|
|
|
|
|
|
log.Printf("Copying %s to %s", source.Name(), target.Name())
|
|
|
|
bytes, err := io.Copy(target, source)
|
|
|
|
if err != nil {
|
|
|
|
err = fmt.Errorf("Error copying iso to output dir: %s", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
log.Printf(fmt.Sprintf("Copied %d bytes", bytes))
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-11-06 00:40:49 -05:00
|
|
|
func (d *QemuDriver) Qemu(qemuArgs ...string) error {
|
|
|
|
d.lock.Lock()
|
|
|
|
defer d.lock.Unlock()
|
|
|
|
|
|
|
|
if d.vmCmd != nil {
|
|
|
|
panic("Existing VM state found")
|
|
|
|
}
|
|
|
|
|
2013-11-05 20:59:09 -05:00
|
|
|
stdout_r, stdout_w := io.Pipe()
|
|
|
|
stderr_r, stderr_w := io.Pipe()
|
2013-09-02 23:23:52 -04:00
|
|
|
|
2013-11-06 00:43:20 -05:00
|
|
|
log.Printf("Executing %s: %#v", d.QemuPath, qemuArgs)
|
|
|
|
cmd := exec.Command(d.QemuPath, qemuArgs...)
|
2013-11-06 00:40:49 -05:00
|
|
|
cmd.Stdout = stdout_w
|
|
|
|
cmd.Stderr = stderr_w
|
|
|
|
|
|
|
|
err := cmd.Start()
|
|
|
|
if err != nil {
|
|
|
|
err = fmt.Errorf("Error starting VM: %s", err)
|
|
|
|
return err
|
|
|
|
}
|
2013-11-05 20:59:09 -05:00
|
|
|
|
|
|
|
go logReader("Qemu stdout", stdout_r)
|
|
|
|
go logReader("Qemu stderr", stderr_r)
|
2013-09-02 23:23:52 -04:00
|
|
|
|
2013-11-06 00:40:49 -05:00
|
|
|
log.Printf("Started Qemu. Pid: %d", cmd.Process.Pid)
|
|
|
|
|
|
|
|
// Wait for Qemu to complete in the background, and mark when its done
|
|
|
|
endCh := make(chan int, 1)
|
|
|
|
go func() {
|
|
|
|
defer stderr_w.Close()
|
|
|
|
defer stdout_w.Close()
|
|
|
|
|
|
|
|
var exitCode int = 0
|
|
|
|
if err := cmd.Wait(); err != nil {
|
|
|
|
if exiterr, ok := err.(*exec.ExitError); ok {
|
|
|
|
// The program has exited with an exit code != 0
|
|
|
|
if status, ok := exiterr.Sys().(syscall.WaitStatus); ok {
|
|
|
|
exitCode = status.ExitStatus()
|
|
|
|
} else {
|
|
|
|
exitCode = 254
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-09-02 23:23:52 -04:00
|
|
|
|
2013-11-06 00:40:49 -05:00
|
|
|
endCh <- exitCode
|
2013-09-02 23:23:52 -04:00
|
|
|
|
2013-11-06 00:40:49 -05:00
|
|
|
d.lock.Lock()
|
|
|
|
defer d.lock.Unlock()
|
|
|
|
d.vmCmd = nil
|
|
|
|
d.vmEndCh = nil
|
|
|
|
}()
|
2013-09-02 23:23:52 -04:00
|
|
|
|
2013-11-06 01:03:43 -05:00
|
|
|
// Wait at least a couple seconds for an early fail from Qemu so
|
|
|
|
// we can report that.
|
|
|
|
select {
|
|
|
|
case exit := <-endCh:
|
|
|
|
if exit != 0 {
|
2016-05-20 17:25:52 -04:00
|
|
|
return fmt.Errorf("Qemu failed to start. Please run with PACKER_LOG=1 to get more info.")
|
2013-11-06 01:03:43 -05:00
|
|
|
}
|
|
|
|
case <-time.After(2 * time.Second):
|
|
|
|
}
|
|
|
|
|
2013-11-06 00:40:49 -05:00
|
|
|
// Setup our state so we know we are running
|
|
|
|
d.vmCmd = cmd
|
|
|
|
d.vmEndCh = endCh
|
2013-09-02 23:23:52 -04:00
|
|
|
|
2013-11-06 00:40:49 -05:00
|
|
|
return nil
|
2013-09-02 23:23:52 -04:00
|
|
|
}
|
|
|
|
|
2013-11-06 00:40:49 -05:00
|
|
|
func (d *QemuDriver) WaitForShutdown(cancelCh <-chan struct{}) bool {
|
|
|
|
d.lock.Lock()
|
|
|
|
endCh := d.vmEndCh
|
|
|
|
d.lock.Unlock()
|
2013-09-02 23:23:52 -04:00
|
|
|
|
2013-11-06 00:40:49 -05:00
|
|
|
if endCh == nil {
|
|
|
|
return true
|
2013-09-02 23:23:52 -04:00
|
|
|
}
|
|
|
|
|
2013-11-06 00:40:49 -05:00
|
|
|
select {
|
|
|
|
case <-endCh:
|
|
|
|
return true
|
|
|
|
case <-cancelCh:
|
|
|
|
return false
|
|
|
|
}
|
2013-09-02 23:23:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *QemuDriver) QemuImg(args ...string) error {
|
|
|
|
var stdout, stderr bytes.Buffer
|
|
|
|
|
|
|
|
log.Printf("Executing qemu-img: %#v", args)
|
2013-11-06 00:43:20 -05:00
|
|
|
cmd := exec.Command(d.QemuImgPath, args...)
|
2013-09-02 23:23:52 -04:00
|
|
|
cmd.Stdout = &stdout
|
|
|
|
cmd.Stderr = &stderr
|
|
|
|
err := cmd.Run()
|
|
|
|
|
|
|
|
stdoutString := strings.TrimSpace(stdout.String())
|
|
|
|
stderrString := strings.TrimSpace(stderr.String())
|
|
|
|
|
|
|
|
if _, ok := err.(*exec.ExitError); ok {
|
|
|
|
err = fmt.Errorf("QemuImg error: %s", stderrString)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("stdout: %s", stdoutString)
|
|
|
|
log.Printf("stderr: %s", stderrString)
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *QemuDriver) Verify() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *QemuDriver) Version() (string, error) {
|
|
|
|
var stdout bytes.Buffer
|
|
|
|
|
2013-11-06 00:43:20 -05:00
|
|
|
cmd := exec.Command(d.QemuPath, "-version")
|
2013-09-02 23:23:52 -04:00
|
|
|
cmd.Stdout = &stdout
|
|
|
|
if err := cmd.Run(); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
versionOutput := strings.TrimSpace(stdout.String())
|
|
|
|
log.Printf("Qemu --version output: %s", versionOutput)
|
2020-03-11 05:29:22 -04:00
|
|
|
versionRe := regexp.MustCompile(`[\.[0-9]+]*`)
|
2015-09-08 06:40:23 -04:00
|
|
|
matches := versionRe.FindStringSubmatch(versionOutput)
|
2013-09-02 23:23:52 -04:00
|
|
|
if len(matches) == 0 {
|
|
|
|
return "", fmt.Errorf("No version found: %s", versionOutput)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("Qemu version: %s", matches[0])
|
|
|
|
return matches[0], nil
|
|
|
|
}
|
2013-11-05 20:59:09 -05:00
|
|
|
|
|
|
|
func logReader(name string, r io.Reader) {
|
|
|
|
bufR := bufio.NewReader(r)
|
|
|
|
for {
|
|
|
|
line, err := bufR.ReadString('\n')
|
|
|
|
if line != "" {
|
|
|
|
line = strings.TrimRightFunc(line, unicode.IsSpace)
|
|
|
|
log.Printf("%s: %s", name, line)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err == io.EOF {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|