summaryrefslogtreecommitdiff
path: root/pkg/machine/config.go
diff options
context:
space:
mode:
authorAshley Cui <acui@redhat.com>2022-07-08 20:10:25 -0400
committerMatthew Heon <matthew.heon@pm.me>2022-07-26 13:32:33 -0400
commit17dbce2fb060b4803b2dae4eb6b78fdebea5b61f (patch)
treed573c5222cd767a14e1b8bc0519e4ccc23bc6663 /pkg/machine/config.go
parente473c5e4b741cef2c1174cb4ec51000f443e6877 (diff)
downloadpodman-17dbce2fb060b4803b2dae4eb6b78fdebea5b61f.tar.gz
podman-17dbce2fb060b4803b2dae4eb6b78fdebea5b61f.tar.bz2
podman-17dbce2fb060b4803b2dae4eb6b78fdebea5b61f.zip
Clean up cached machine images
When initing machines, we download a machine image, and uncompress and copy the image for the actual vm image. When a user constantly pulls new machines, there may be a buildup of old, unused machine images. This commit cleans ups the unused cached images. Changes: - If the machine is pulled from a URL or from the FCOS releases, we pull them into XDG_DATA_HOME/containers/podman/machine/vmType/cache - Cache cleanups only happen if there is a cache miss, and we need to pull a new image - For Fedora and FCOS, we actually use the cache, so we go through the cache dir and remove any images older than 2 weeks (FCOS's release cycle), on a cache miss. - For generic files pulled from a URL, we don't actually cache, so we delete the pulled file immediately after creating a machine image - For generic files from a local path, the original file will never be cleaned up Note that because we cache in a different dir, this will not clean up old images pulled before this commit. [NO NEW TESTS NEEDED] Signed-off-by: Ashley Cui <acui@redhat.com>
Diffstat (limited to 'pkg/machine/config.go')
-rw-r--r--pkg/machine/config.go19
1 files changed, 17 insertions, 2 deletions
diff --git a/pkg/machine/config.go b/pkg/machine/config.go
index 29cd7bc00..253601dad 100644
--- a/pkg/machine/config.go
+++ b/pkg/machine/config.go
@@ -73,6 +73,7 @@ type Download struct {
Arch string
Artifact string
CompressionType string
+ CacheDir string
Format string
ImageName string
LocalPath string
@@ -139,6 +140,7 @@ type VM interface {
type DistributionDownload interface {
HasUsableCache() (bool, error)
Get() *Download
+ CleanCache() error
}
type InspectInfo struct {
ConfigPath VMFile
@@ -172,6 +174,19 @@ func (rc RemoteConnectionType) MakeSSHURL(host, path, port, userName string) url
return uri
}
+// GetCacheDir returns the dir where VM images are downladed into when pulled
+func GetCacheDir(vmType string) (string, error) {
+ dataDir, err := GetDataDir(vmType)
+ if err != nil {
+ return "", err
+ }
+ cacheDir := filepath.Join(dataDir, "cache")
+ if _, err := os.Stat(cacheDir); !errors.Is(err, os.ErrNotExist) {
+ return cacheDir, nil
+ }
+ return cacheDir, os.MkdirAll(cacheDir, 0755)
+}
+
// GetDataDir returns the filepath where vm images should
// live for podman-machine.
func GetDataDir(vmType string) (string, error) {
@@ -180,7 +195,7 @@ func GetDataDir(vmType string) (string, error) {
return "", err
}
dataDir := filepath.Join(dataDirPrefix, vmType)
- if _, err := os.Stat(dataDir); !os.IsNotExist(err) {
+ if _, err := os.Stat(dataDir); !errors.Is(err, os.ErrNotExist) {
return dataDir, nil
}
mkdirErr := os.MkdirAll(dataDir, 0755)
@@ -205,7 +220,7 @@ func GetConfDir(vmType string) (string, error) {
return "", err
}
confDir := filepath.Join(confDirPrefix, vmType)
- if _, err := os.Stat(confDir); !os.IsNotExist(err) {
+ if _, err := os.Stat(confDir); !errors.Is(err, os.ErrNotExist) {
return confDir, nil
}
mkdirErr := os.MkdirAll(confDir, 0755)