summaryrefslogtreecommitdiff
path: root/pkg/machine/qemu/machine.go
diff options
context:
space:
mode:
authorAshley Cui <acui@redhat.com>2021-03-30 02:30:28 -0400
committerAshley Cui <acui@redhat.com>2021-03-30 14:56:21 -0400
commitef4e91a59eed957e56bf1536bc03df94cd096576 (patch)
tree4a72e690181c5e0bfada6e67424197b10d459b12 /pkg/machine/qemu/machine.go
parent5e28b35aa511e699dffb2172458fda35ac01a4d2 (diff)
downloadpodman-ef4e91a59eed957e56bf1536bc03df94cd096576.tar.gz
podman-ef4e91a59eed957e56bf1536bc03df94cd096576.tar.bz2
podman-ef4e91a59eed957e56bf1536bc03df94cd096576.zip
Add podman machine list
podman machine list lists all virtual machines & indicates the default VM connection, if it exists. it also can take a --format flag arg as a go template. [NO TESTS NEEDED] Signed-off-by: Ashley Cui <acui@redhat.com>
Diffstat (limited to 'pkg/machine/qemu/machine.go')
-rw-r--r--pkg/machine/qemu/machine.go50
1 files changed, 50 insertions, 0 deletions
diff --git a/pkg/machine/qemu/machine.go b/pkg/machine/qemu/machine.go
index 652a077cc..58486fc73 100644
--- a/pkg/machine/qemu/machine.go
+++ b/pkg/machine/qemu/machine.go
@@ -10,6 +10,7 @@ import (
"os/exec"
"path/filepath"
"strconv"
+ "strings"
"time"
"github.com/containers/podman/v3/pkg/machine"
@@ -426,3 +427,52 @@ func getDiskSize(path string) (uint64, error) {
}
return tmpInfo.VirtualSize, nil
}
+
+// List lists all vm's that use qemu virtualization
+func List(opts machine.ListOptions) ([]*machine.ListResponse, error) {
+ vmConfigDir, err := machine.GetConfDir(vmtype)
+ if err != nil {
+ return nil, err
+ }
+
+ var listed []*machine.ListResponse
+
+ if err = filepath.Walk(vmConfigDir, func(path string, info os.FileInfo, err error) error {
+ vm := new(MachineVM)
+ if strings.HasSuffix(info.Name(), ".json") {
+ fullPath := filepath.Join(vmConfigDir, info.Name())
+ b, err := ioutil.ReadFile(fullPath)
+ if err != nil {
+ return err
+ }
+ err = json.Unmarshal(b, vm)
+ if err != nil {
+ return err
+ }
+ listEntry := new(machine.ListResponse)
+
+ listEntry.Name = vm.Name
+ listEntry.VMType = "qemu"
+ fi, err := os.Stat(fullPath)
+ if err != nil {
+ return err
+ }
+ listEntry.CreatedAt = fi.ModTime()
+
+ fi, err = os.Stat(vm.ImagePath)
+ if err != nil {
+ return err
+ }
+ listEntry.LastUp = fi.ModTime()
+ if vm.isRunning() {
+ listEntry.Running = true
+ }
+
+ listed = append(listed, listEntry)
+ }
+ return nil
+ }); err != nil {
+ return nil, err
+ }
+ return listed, err
+}