Skip to content
Snippets Groups Projects
Commit 60abdc96 authored by Florent Gluck's avatar Florent Gluck
Browse files

Uses guestfish directly instead of using virt-tar-out and virt-tar-in wrapper...

Uses guestfish directly instead of using virt-tar-out and virt-tar-in wrapper which simplifies the code a bit.
parent 7d713866
No related branches found
No related tags found
No related merge requests found
...@@ -8,31 +8,31 @@ import ( ...@@ -8,31 +8,31 @@ import (
) )
const ( const (
virtTarInBinary = "virt-tar-in" guestfishBinary = "guestfish"
) )
// Checks virt-tar-in is available. // Checks guestfish is available.
func CheckVirtTarIn() error { func CheckGuestfish() error {
output, err := exec.Command(virtTarInBinary, "--version").Output() output, err := exec.Command(guestfishBinary, "--version").Output()
if err != nil { if err != nil {
return errors.New(virtTarInBinary+" is required but not found. On Ubuntu/Debian, it can be installed with \"sudo apt-get install guestfish\".") return errors.New(guestfishBinary+" is required but not found. On Ubuntu/Debian, it can be installed with \"sudo apt-get install guestfish\".")
} }
out := string(output) out := string(output)
lines := strings.Split(out, "\n") lines := strings.Split(out, "\n")
fields := strings.Split(lines[0], " ") fields := strings.Split(lines[0], " ")
if len(fields) < 2 { if len(fields) < 2 {
return errors.New("Failed extracting "+virtTarInBinary+" version number!") return errors.New("Failed extracting "+guestfishBinary+" version number!")
} }
cmd := fields[0] cmd := fields[0]
if cmd != virtTarInBinary { if cmd != guestfishBinary {
return errors.New(virtTarInBinary+" is required, but not found.") return errors.New(guestfishBinary+" is required, but not found.")
} }
return nil return nil
} }
// Runs virt-tar-in which unarchive a local archive into a directory (vmDir) inside the VM disk. // Copies and unarchives a local archive into a directory (vmDir) inside the VM's filesystem.
func CopyToVM(vmDiskFile, tarFile, vmDir string) error { func CopyToVM(vmDiskFile, tarFile, vmDir string) error {
cmd := exec.Command(virtTarInBinary, "-a", vmDiskFile, tarFile, vmDir) cmd := exec.Command(guestfishBinary, "--rw", "-i", "tar-in", "-a", vmDiskFile, tarFile, vmDir)
stdoutStderr, err := cmd.CombinedOutput() stdoutStderr, err := cmd.CombinedOutput()
if err != nil { if err != nil {
output := fmt.Sprintf("[%s]", stdoutStderr) output := fmt.Sprintf("[%s]", stdoutStderr)
...@@ -42,3 +42,16 @@ func CopyToVM(vmDiskFile, tarFile, vmDir string) error { ...@@ -42,3 +42,16 @@ func CopyToVM(vmDiskFile, tarFile, vmDir string) error {
} }
return nil return nil
} }
// Recursively copies a directory in the VM's filesystem (vmDir) into a tar archive.
func CopyFromVM(vmDiskFile, vmDir, tarFile string) error {
cmd := exec.Command(guestfishBinary, "--ro", "-i", "tar-out", "-a", vmDiskFile, vmDir, tarFile)
stdoutStderr, err := cmd.CombinedOutput()
if err != nil {
output := fmt.Sprintf("[%s]", stdoutStderr)
msg := "Failed reading \""+vmDir+"\" in qcow ("+vmDiskFile+"): "+output
log.Error(msg)
return errors.New(msg)
}
return nil
}
package exec
import (
"fmt"
"os/exec"
"strings"
"errors"
)
const (
virtTarOutBinary = "virt-tar-out"
)
// Checks virt-tar-out is available.
func CheckVirtCopyOut() error {
output, err := exec.Command(virtTarOutBinary, "--version").Output()
if err != nil {
return errors.New(virtTarOutBinary+" is required but not found. On Ubuntu/Debian, it can be installed with \"sudo apt-get install guestfish\".")
}
out := string(output)
lines := strings.Split(out, "\n")
fields := strings.Split(lines[0], " ")
if len(fields) < 2 {
return errors.New("Failed extracting "+virtTarOutBinary+" version number!")
}
cmd := fields[0]
if cmd != virtTarOutBinary {
return errors.New(virtTarOutBinary+" is required, but not found.")
}
return nil
}
// Runs virt-tar-in which unarchive a local archive into a directory (vmDir) inside the VM disk.
// Runs virt-copy-out which recursively extract a directory from the VM's
// filesystem (vmDir) into a tar archive.
func CopyFromVM(vmDiskFile, vmDir, tarFile string) error {
cmd := exec.Command(virtTarOutBinary, "-a", vmDiskFile, vmDir, tarFile)
stdoutStderr, err := cmd.CombinedOutput()
if err != nil {
output := fmt.Sprintf("[%s]", stdoutStderr)
msg := "Failed reading \""+vmDir+"\" in qcow ("+vmDiskFile+"): "+output
log.Error(msg)
return errors.New(msg)
}
return nil
}
...@@ -34,12 +34,9 @@ func main() { ...@@ -34,12 +34,9 @@ func main() {
if err := exec.CheckQemuImg(); err != nil { if err := exec.CheckQemuImg(); err != nil {
log.Fatal(err) log.Fatal(err)
} }
if err := exec.CheckVirtCopyOut(); err != nil { if err := exec.CheckGuestfish(); err != nil {
log.Fatal(err) log.Fatal(err)
} }
// if err := exec.CheckVirtCopyIn(); err != nil {
// log.Fatal(err)
// }
usage := func() { usage := func() {
fmt.Println("Usage of "+path.Base(os.Args[0])+":") fmt.Println("Usage of "+path.Base(os.Args[0])+":")
......
...@@ -208,12 +208,6 @@ func (r *RouterVMs)DeleteVMByID(c echo.Context) error { ...@@ -208,12 +208,6 @@ func (r *RouterVMs)DeleteVMByID(c echo.Context) error {
// curl --cacert ca.pem -X PUT https://localhost:1077/vms/e41f3556-ca24-4658-bd79-8c85bd6bff59/start -H "Authorization: Bearer <AccessToken>" // curl --cacert ca.pem -X PUT https://localhost:1077/vms/e41f3556-ca24-4658-bd79-8c85bd6bff59/start -H "Authorization: Bearer <AccessToken>"
func (r *RouterVMs)StartVM(c echo.Context) error { func (r *RouterVMs)StartVM(c echo.Context) error {
return r.performVMAction(c, caps.CAP_VM_START_ANY, caps.CAP_VM_START, func(c echo.Context, vm *vms.VM) error { return r.performVMAction(c, caps.CAP_VM_START_ANY, caps.CAP_VM_START, func(c echo.Context, vm *vms.VM) error {
// port, pwd, err := r.vms.StartVM(vm.ID)
// if err != nil {
// return echo.NewHTTPError(http.StatusNotFound, err.Error())
// }
// return c.JSONPretty(http.StatusOK, echo.Map{"port": port, "pwd": pwd}, " ")
_, _, err := r.vms.StartVM(vm.ID) _, _, err := r.vms.StartVM(vm.ID)
if err != nil { if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, err.Error()) return echo.NewHTTPError(http.StatusBadRequest, err.Error())
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment