From 60abdc968e1c264bbb5c3cca0e6e0db30ff88b7b Mon Sep 17 00:00:00 2001
From: Florent Gluck <florent.gluck@hesge.ch>
Date: Thu, 8 Sep 2022 11:38:21 +0200
Subject: [PATCH] Uses guestfish directly instead of using virt-tar-out and
 virt-tar-in wrapper which simplifies the code a bit.

---
 src/exec/Guestfish.go   | 57 +++++++++++++++++++++++++++++++++++++++++
 src/exec/VirtTarIn.go   | 44 -------------------------------
 src/exec/VirtTarOut.go  | 47 ---------------------------------
 src/nexus-server.go     |  5 +---
 src/router/routerVMs.go |  6 -----
 5 files changed, 58 insertions(+), 101 deletions(-)
 create mode 100644 src/exec/Guestfish.go
 delete mode 100644 src/exec/VirtTarIn.go
 delete mode 100644 src/exec/VirtTarOut.go

diff --git a/src/exec/Guestfish.go b/src/exec/Guestfish.go
new file mode 100644
index 0000000..7597494
--- /dev/null
+++ b/src/exec/Guestfish.go
@@ -0,0 +1,57 @@
+package exec
+
+import (
+	"fmt"
+	"os/exec"
+	"strings"
+	"errors"
+)
+
+const (
+	guestfishBinary = "guestfish"
+)
+
+// Checks guestfish is available.
+func CheckGuestfish() error {
+	output, err := exec.Command(guestfishBinary, "--version").Output()
+    if err != nil {
+        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)
+	lines := strings.Split(out, "\n")
+	fields := strings.Split(lines[0], " ")
+	if len(fields) < 2 {
+		return errors.New("Failed extracting "+guestfishBinary+" version number!")
+	}
+	cmd := fields[0]
+	if cmd != guestfishBinary {
+		return errors.New(guestfishBinary+" is required, but not found.")
+	}
+	return nil
+}
+
+// Copies and unarchives a local archive into a directory (vmDir) inside the VM's filesystem.
+func CopyToVM(vmDiskFile, tarFile, vmDir string) error {
+	cmd := exec.Command(guestfishBinary, "--rw", "-i", "tar-in", "-a", vmDiskFile, tarFile, vmDir)
+	stdoutStderr, err := cmd.CombinedOutput()
+	if err != nil {
+		output := fmt.Sprintf("[%s]", stdoutStderr)
+		msg := "Failed writing to \""+vmDir+"\" in qcow ("+vmDiskFile+")"
+		log.Error(msg+": "+output)
+		return errors.New(msg)
+	}
+	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
+}
diff --git a/src/exec/VirtTarIn.go b/src/exec/VirtTarIn.go
deleted file mode 100644
index 733f3af..0000000
--- a/src/exec/VirtTarIn.go
+++ /dev/null
@@ -1,44 +0,0 @@
-package exec
-
-import (
-	"fmt"
-	"os/exec"
-	"strings"
-	"errors"
-)
-
-const (
-	virtTarInBinary = "virt-tar-in"
-)
-
-// Checks virt-tar-in is available.
-func CheckVirtTarIn() error {
-	output, err := exec.Command(virtTarInBinary, "--version").Output()
-    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\".")
-	}
-	out := string(output)
-	lines := strings.Split(out, "\n")
-	fields := strings.Split(lines[0], " ")
-	if len(fields) < 2 {
-		return errors.New("Failed extracting "+virtTarInBinary+" version number!")
-	}
-	cmd := fields[0]
-	if cmd != virtTarInBinary {
-		return errors.New(virtTarInBinary+" is required, but not found.")
-	}
-	return nil
-}
-
-// Runs virt-tar-in which unarchive a local archive into a directory (vmDir) inside the VM disk.
-func CopyToVM(vmDiskFile, tarFile, vmDir string) error {
-	cmd := exec.Command(virtTarInBinary, "-a", vmDiskFile, tarFile, vmDir)
-	stdoutStderr, err := cmd.CombinedOutput()
-	if err != nil {
-		output := fmt.Sprintf("[%s]", stdoutStderr)
-		msg := "Failed writing to \""+vmDir+"\" in qcow ("+vmDiskFile+")"
-		log.Error(msg+": "+output)
-		return errors.New(msg)
-	}
-	return nil
-}
diff --git a/src/exec/VirtTarOut.go b/src/exec/VirtTarOut.go
deleted file mode 100644
index b8eaac7..0000000
--- a/src/exec/VirtTarOut.go
+++ /dev/null
@@ -1,47 +0,0 @@
-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
-}
diff --git a/src/nexus-server.go b/src/nexus-server.go
index e40c13a..279e399 100644
--- a/src/nexus-server.go
+++ b/src/nexus-server.go
@@ -34,12 +34,9 @@ func main() {
 	if err := exec.CheckQemuImg(); err != nil {
 		log.Fatal(err)
 	}
-	if err := exec.CheckVirtCopyOut(); err != nil {
+	if err := exec.CheckGuestfish(); err != nil {
 		log.Fatal(err)
 	}
-	// if err := exec.CheckVirtCopyIn(); err != nil {
-	// 	log.Fatal(err)
-	// }
 
 	usage := func() {
 		fmt.Println("Usage of "+path.Base(os.Args[0])+":")
diff --git a/src/router/routerVMs.go b/src/router/routerVMs.go
index 6a30eff..5aa34e2 100644
--- a/src/router/routerVMs.go
+++ b/src/router/routerVMs.go
@@ -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>"
 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 {
-		// 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)
 		if err != nil {
 			return echo.NewHTTPError(http.StatusBadRequest, err.Error())
-- 
GitLab