diff --git a/qemu.go b/qemu.go new file mode 100644 index 000000000..a98928cd3 --- /dev/null +++ b/qemu.go @@ -0,0 +1,62 @@ +/* +// Copyright (c) 2016 Intel Corporation +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +*/ + +package qemu + +import ( + "bytes" + "os" + "os/exec" + + "golang.org/x/net/context" +) + +// LaunchQemu can be used to launch a new qemu instance by invoking the +// qemu-system-x86_64 binary. +// +// The ctx parameter is not currently used but has been added so that the +// signature of this function will not need to change when launch cancellation +// is implemented. +// +// params is a slice of options to pass to qemu-system-x86_64 and fds is a +// list of open file descriptors that are to be passed to the spawned qemu +// process. +// +// This function writes its log output via logger parameter. +// +// The function will block until the launched qemu process exits. "", nil +// will be returned if the launch succeeds. Otherwise a string containing +// the contents of stderr + a Go error object will be returned. +func LaunchQemu(ctx context.Context, params []string, fds []*os.File, logger QMPLog) (string, error) { + errStr := "" + cmd := exec.Command("qemu-system-x86_64", params...) + if fds != nil { + logger.Infof("Adding extra file %v", fds) + cmd.ExtraFiles = fds + } + + var stderr bytes.Buffer + cmd.Stderr = &stderr + logger.Infof("launching qemu with: %v", params) + + err := cmd.Run() + if err != nil { + logger.Errorf("Unable to launch qemu: %v", err) + errStr = stderr.String() + logger.Errorf("%s", errStr) + } + return errStr, err +} diff --git a/qmp.go b/qmp.go index 11ca1ebbd..c95cdc35f 100644 --- a/qmp.go +++ b/qmp.go @@ -29,9 +29,6 @@ import ( "golang.org/x/net/context" ) -// Code to launch qemu -// move to package and document - // QMPLog is a logging interface used by the qemu package to log various // interesting pieces of information. Rather than introduce a dependency // on a given logging package, qemu presents this interface that allows