diff options
author | Ashley Cui <acui@redhat.com> | 2022-04-27 16:56:48 -0400 |
---|---|---|
committer | openshift-cherrypick-robot <> | 2022-05-04 18:06:23 +0000 |
commit | d3f406e9f6fe3c33228d0f384f986982640d2f43 (patch) | |
tree | a1eee345deaeeca0efedbde48a8ce4d6fe32af16 /pkg | |
parent | c4357f0f7b7a032908abfab358616d82163eaf9e (diff) | |
download | podman-d3f406e9f6fe3c33228d0f384f986982640d2f43.tar.gz podman-d3f406e9f6fe3c33228d0f384f986982640d2f43.tar.bz2 podman-d3f406e9f6fe3c33228d0f384f986982640d2f43.zip |
podman system reset removed machines incorrectly
podman system reset did not clean up machines fully, leaving some config
files, and breaking machines. Now it removes all machines files fully.
Signed-off-by: Ashley Cui <acui@redhat.com>
Diffstat (limited to 'pkg')
-rw-r--r-- | pkg/machine/config.go | 29 | ||||
-rw-r--r-- | pkg/machine/qemu/machine.go | 76 | ||||
-rw-r--r-- | pkg/machine/wsl/machine.go | 76 |
3 files changed, 177 insertions, 4 deletions
diff --git a/pkg/machine/config.go b/pkg/machine/config.go index 9a0ce757a..d34776714 100644 --- a/pkg/machine/config.go +++ b/pkg/machine/config.go @@ -52,6 +52,7 @@ type Provider interface { List(opts ListOptions) ([]*ListResponse, error) IsValidVMName(name string) (bool, error) CheckExclusiveActiveVM() (bool, string, error) + RemoveAndCleanMachines() error } type RemoteConnectionType string @@ -170,11 +171,11 @@ func (rc RemoteConnectionType) MakeSSHURL(host, path, port, userName string) url // GetDataDir returns the filepath where vm images should // live for podman-machine. func GetDataDir(vmType string) (string, error) { - data, err := homedir.GetDataHome() + dataDirPrefix, err := DataDirPrefix() if err != nil { return "", err } - dataDir := filepath.Join(data, "containers", "podman", "machine", vmType) + dataDir := filepath.Join(dataDirPrefix, vmType) if _, err := os.Stat(dataDir); !os.IsNotExist(err) { return dataDir, nil } @@ -182,14 +183,24 @@ func GetDataDir(vmType string) (string, error) { return dataDir, mkdirErr } +// DataDirPrefix returns the path prefix for all machine data files +func DataDirPrefix() (string, error) { + data, err := homedir.GetDataHome() + if err != nil { + return "", err + } + dataDir := filepath.Join(data, "containers", "podman", "machine") + return dataDir, nil +} + // GetConfigDir returns the filepath to where configuration // files for podman-machine should live func GetConfDir(vmType string) (string, error) { - conf, err := homedir.GetConfigHome() + confDirPrefix, err := ConfDirPrefix() if err != nil { return "", err } - confDir := filepath.Join(conf, "containers", "podman", "machine", vmType) + confDir := filepath.Join(confDirPrefix, vmType) if _, err := os.Stat(confDir); !os.IsNotExist(err) { return confDir, nil } @@ -197,6 +208,16 @@ func GetConfDir(vmType string) (string, error) { return confDir, mkdirErr } +// ConfDirPrefix returns the path prefix for all machine config files +func ConfDirPrefix() (string, error) { + conf, err := homedir.GetConfigHome() + if err != nil { + return "", err + } + confDir := filepath.Join(conf, "containers", "podman", "machine") + return confDir, nil +} + // ResourceConfig describes physical attributes of the machine type ResourceConfig struct { // CPUs to be assigned to the VM diff --git a/pkg/machine/qemu/machine.go b/pkg/machine/qemu/machine.go index fe33e580c..6e36b0886 100644 --- a/pkg/machine/qemu/machine.go +++ b/pkg/machine/qemu/machine.go @@ -1544,3 +1544,79 @@ func (v *MachineVM) editCmdLine(flag string, value string) { v.CmdLine = append(v.CmdLine, []string{flag, value}...) } } + +// RemoveAndCleanMachines removes all machine and cleans up any other files associatied with podman machine +func (p *Provider) RemoveAndCleanMachines() error { + var ( + vm machine.VM + listResponse []*machine.ListResponse + opts machine.ListOptions + destroyOptions machine.RemoveOptions + ) + destroyOptions.Force = true + var prevErr error + + listResponse, err := p.List(opts) + if err != nil { + return err + } + + for _, mach := range listResponse { + vm, err = p.LoadVMByName(mach.Name) + if err != nil { + if prevErr != nil { + logrus.Error(prevErr) + } + prevErr = err + } + _, remove, err := vm.Remove(mach.Name, destroyOptions) + if err != nil { + if prevErr != nil { + logrus.Error(prevErr) + } + prevErr = err + } else { + if err := remove(); err != nil { + if prevErr != nil { + logrus.Error(prevErr) + } + prevErr = err + } + } + } + + // Clean leftover files in data dir + dataDir, err := machine.DataDirPrefix() + if err != nil { + if prevErr != nil { + logrus.Error(prevErr) + } + prevErr = err + } else { + err := os.RemoveAll(dataDir) + if err != nil { + if prevErr != nil { + logrus.Error(prevErr) + } + prevErr = err + } + } + + // Clean leftover files in conf dir + confDir, err := machine.ConfDirPrefix() + if err != nil { + if prevErr != nil { + logrus.Error(prevErr) + } + prevErr = err + } else { + err := os.RemoveAll(confDir) + if err != nil { + if prevErr != nil { + logrus.Error(prevErr) + } + prevErr = err + } + } + return prevErr +} diff --git a/pkg/machine/wsl/machine.go b/pkg/machine/wsl/machine.go index bc2ac7864..57fb36fc9 100644 --- a/pkg/machine/wsl/machine.go +++ b/pkg/machine/wsl/machine.go @@ -1442,3 +1442,79 @@ func (v *MachineVM) getResources() (resources machine.ResourceConfig) { resources.DiskSize = getDiskSize(v) return } + +// RemoveAndCleanMachines removes all machine and cleans up any other files associatied with podman machine +func (p *Provider) RemoveAndCleanMachines() error { + var ( + vm machine.VM + listResponse []*machine.ListResponse + opts machine.ListOptions + destroyOptions machine.RemoveOptions + ) + destroyOptions.Force = true + var prevErr error + + listResponse, err := p.List(opts) + if err != nil { + return err + } + + for _, mach := range listResponse { + vm, err = p.LoadVMByName(mach.Name) + if err != nil { + if prevErr != nil { + logrus.Error(prevErr) + } + prevErr = err + } + _, remove, err := vm.Remove(mach.Name, destroyOptions) + if err != nil { + if prevErr != nil { + logrus.Error(prevErr) + } + prevErr = err + } else { + if err := remove(); err != nil { + if prevErr != nil { + logrus.Error(prevErr) + } + prevErr = err + } + } + } + + // Clean leftover files in data dir + dataDir, err := machine.DataDirPrefix() + if err != nil { + if prevErr != nil { + logrus.Error(prevErr) + } + prevErr = err + } else { + err := os.RemoveAll(dataDir) + if err != nil { + if prevErr != nil { + logrus.Error(prevErr) + } + prevErr = err + } + } + + // Clean leftover files in conf dir + confDir, err := machine.ConfDirPrefix() + if err != nil { + if prevErr != nil { + logrus.Error(prevErr) + } + prevErr = err + } else { + err := os.RemoveAll(confDir) + if err != nil { + if prevErr != nil { + logrus.Error(prevErr) + } + prevErr = err + } + } + return prevErr +} |