builder/qemu: log VM output
This commit is contained in:
parent
b75a2935f3
commit
07e5a85bab
|
@ -1,15 +1,18 @@
|
||||||
package qemu
|
package qemu
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bufio"
|
||||||
"bytes"
|
"bytes"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/mitchellh/multistep"
|
"github.com/mitchellh/multistep"
|
||||||
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
"unicode"
|
||||||
)
|
)
|
||||||
|
|
||||||
type DriverCancelCallback func(state multistep.StateBag) bool
|
type DriverCancelCallback func(state multistep.StateBag) bool
|
||||||
|
@ -92,20 +95,24 @@ func (d *QemuDriver) Stop(name string) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *QemuDriver) Qemu(vmName string, qemuArgs ...string) error {
|
func (d *QemuDriver) Qemu(vmName string, qemuArgs ...string) error {
|
||||||
var stdout, stderr bytes.Buffer
|
stdout_r, stdout_w := io.Pipe()
|
||||||
|
stderr_r, stderr_w := io.Pipe()
|
||||||
|
|
||||||
log.Printf("Executing %s: %#v", d.qemuPath, qemuArgs)
|
log.Printf("Executing %s: %#v", d.qemuPath, qemuArgs)
|
||||||
ds := d.getDriverState(vmName)
|
ds := d.getDriverState(vmName)
|
||||||
ds.cmd = exec.Command(d.qemuPath, qemuArgs...)
|
ds.cmd = exec.Command(d.qemuPath, qemuArgs...)
|
||||||
ds.cmd.Stdout = &stdout
|
ds.cmd.Stdout = stdout_w
|
||||||
ds.cmd.Stderr = &stderr
|
ds.cmd.Stderr = stderr_w
|
||||||
|
|
||||||
|
go logReader("Qemu stdout", stdout_r)
|
||||||
|
go logReader("Qemu stderr", stderr_r)
|
||||||
|
|
||||||
err := ds.cmd.Start()
|
err := ds.cmd.Start()
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
err = fmt.Errorf("Error starting VM: %s", err)
|
err = fmt.Errorf("Error starting VM: %s", err)
|
||||||
} else {
|
} else {
|
||||||
log.Printf("---- Started Qemu ------- PID = ", ds.cmd.Process.Pid)
|
log.Printf("---- Started Qemu ------- PID = %d", ds.cmd.Process.Pid)
|
||||||
|
|
||||||
ds.cancelChan = make(chan struct{})
|
ds.cancelChan = make(chan struct{})
|
||||||
|
|
||||||
|
@ -114,6 +121,8 @@ func (d *QemuDriver) Qemu(vmName string, qemuArgs ...string) error {
|
||||||
|
|
||||||
// start the virtual machine in the background
|
// start the virtual machine in the background
|
||||||
go func() {
|
go func() {
|
||||||
|
defer stderr_w.Close()
|
||||||
|
defer stdout_w.Close()
|
||||||
ds.waitDone <- ds.cmd.Wait()
|
ds.waitDone <- ds.cmd.Wait()
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
|
@ -242,3 +251,18 @@ func (d *QemuDriver) Version() (string, error) {
|
||||||
log.Printf("Qemu version: %s", matches[0])
|
log.Printf("Qemu version: %s", matches[0])
|
||||||
return matches[0], nil
|
return matches[0], nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue