From 29ad905596a05582026661d19c2db8fddd6994d4 Mon Sep 17 00:00:00 2001 From: Gavin Zhao Date: Fri, 25 Aug 2023 18:46:37 -0400 Subject: [PATCH 1/7] Refactor build cache and drop build cache support for legacy XML builds Signed-off-by: Gavin Zhao --- builder/build.go | 109 +++++++++++--------------------------------- builder/cache.go | 24 ++++++++++ builder/main.go | 21 ++++----- cli/delete_cache.go | 18 ++++---- 4 files changed, 70 insertions(+), 102 deletions(-) create mode 100644 builder/cache.go diff --git a/builder/build.go b/builder/build.go index 038f53d..990d93a 100644 --- a/builder/build.go +++ b/builder/build.go @@ -31,8 +31,6 @@ func (p *Package) CreateDirs(o *Overlay) error { dirs := []string{ p.GetWorkDir(o), p.GetSourceDir(o), - p.GetCcacheDir(o), - p.GetSccacheDir(o), } for _, p := range dirs { if err := os.MkdirAll(p, 0o0755); err != nil { @@ -40,32 +38,15 @@ func (p *Package) CreateDirs(o *Overlay) error { } } - // Fix up the ccache directories - if p.Type == PackageTypeXML { - // Ensure we have root owned ccache/sccache - if err := os.MkdirAll(LegacyCcacheDirectory, 0o0755); err != nil { - return fmt.Errorf("Failed to create ccache directory %+v, reason: %w\n", p, err) - } - - if err := os.MkdirAll(LegacySccacheDirectory, 0o0755); err != nil { - return fmt.Errorf("Failed to create sccache directory %+v, reason: %w\n", p, err) - } - } else { - // Ensure we have root owned ccache/sccache - if err := os.MkdirAll(CcacheDirectory, 0o0755); err != nil { - return fmt.Errorf("Failed to create ccache directory %+v, reason: %w\n", p, err) - } - - if err := os.Chown(CcacheDirectory, BuildUserID, BuildUserGID); err != nil { - return fmt.Errorf("Failed to chown ccache directory %+v, reason: %w\n", p, err) - } - - if err := os.MkdirAll(SccacheDirectory, 0o0755); err != nil { - return fmt.Errorf("Failed to create sccache directory %+v, reason: %w\n", p, err) - } - - if err := os.Chown(SccacheDirectory, BuildUserID, BuildUserGID); err != nil { - return fmt.Errorf("Failed to chown sccache directory %+v, reason: %w\n", p, err) + // Ensure we have root owned cache directories. + // + // Currently we are only using build caches for YPKG builds to reduce + // maintenance burden. + if p.Type == PackageTypeYpkg { + for _, cache := range Caches { + if err := os.MkdirAll(cache.CacheDir, 0o0755); err != nil { + return fmt.Errorf("Failed to create cache directory %s for %s, reason: %w\n", cache.CacheDir, cache.Name, err) + } } } @@ -134,51 +115,28 @@ func (p *Package) BindSources(o *Overlay) error { return nil } -// BindCcache will make the ccache directory available to the build. -func (p *Package) BindCcache(o *Overlay) error { +// BindCache will make all cache defined in [caches] available to the build +func (p *Package) BindCaches(o *Overlay) error { mountMan := disk.GetMountManager() - ccacheDir := p.GetCcacheDir(o) - - var ccacheSource string - if p.Type == PackageTypeXML { - ccacheSource = LegacyCcacheDirectory - } else { - ccacheSource = CcacheDirectory - } - - slog.Debug("Exposing ccache to build", "dir", ccacheDir) - - // Bind mount local ccache into chroot - if err := mountMan.BindMount(ccacheSource, ccacheDir); err != nil { - return fmt.Errorf("Failed to bind mount ccache %s, reason: %w\n", ccacheDir, err) - } - o.ExtraMounts = append(o.ExtraMounts, ccacheDir) + for _, c := range Caches { + var cacheSource string + var cacheDir string - return nil -} - -// BindSccache will make the sccache directory available to the build. -func (p *Package) BindSccache(o *Overlay) error { - mountMan := disk.GetMountManager() - sccacheDir := p.GetSccacheDir(o) - - var sccacheSource string - if p.Type == PackageTypeXML { - sccacheSource = LegacySccacheDirectory - } else { - sccacheSource = SccacheDirectory - } + if p.Type == PackageTypeYpkg { + cacheSource = filepath.Join(CacheDirectory, c.Name, "ypkg") + cacheDir = filepath.Join(o.MountPoint, c.CacheDir[1:]) + } - slog.Debug("Exposing sccache to build", "dir", sccacheDir) + log.Debugf("Exposing %s to build %s\n", c.Name, cacheDir) - // Bind mount local sccache into chroot - if err := mountMan.BindMount(sccacheSource, sccacheDir); err != nil { - return fmt.Errorf("Failed to bind mount sccache %s, reason: %w\n", sccacheDir, err) + // Bind mount local ccache into chroot + if err := mountMan.BindMount(cacheSource, cacheDir); err != nil { + return fmt.Errorf("Failed to bind mount %s %s, reason: %s\n", c.Name, cacheDir, err) + } + o.ExtraMounts = append(o.ExtraMounts, cacheDir) } - o.ExtraMounts = append(o.ExtraMounts, sccacheDir) - return nil } @@ -371,13 +329,8 @@ func (p *Package) BuildYpkg(notif PidNotifier, usr *UserInfo, pman *EopkgManager return err } - // Ensure we have ccache available - if err := p.BindCcache(overlay); err != nil { - return err - } - - // Ensure we have sccache available - if err := p.BindSccache(overlay); err != nil { + // Ensure we have build caches available + if err := p.BindCaches(overlay); err != nil { return err } @@ -434,16 +387,6 @@ func (p *Package) BuildXML(notif PidNotifier, pman *EopkgManager, overlay *Overl return fmt.Errorf("Cannot continue without sources.\n") } - // Ensure we have ccache available - if err := p.BindCcache(overlay); err != nil { - return err - } - - // Ensure we have ccache available - if err := p.BindSccache(overlay); err != nil { - return err - } - // Now recopy the assets prior to build if err := pman.CopyAssets(); err != nil { return err diff --git a/builder/cache.go b/builder/cache.go new file mode 100644 index 0000000..a00fb26 --- /dev/null +++ b/builder/cache.go @@ -0,0 +1,24 @@ +package builder + +import ( + "path" +) + +var ( + Ccache = Cache{ + Name: "ccache", + CacheDir: path.Join(BuildUserHome, ".ccache"), + } + + Sccache = Cache{ + Name: "sccache", + CacheDir: path.Join(BuildUserHome, ".cache", "sccache"), + } + + Caches = []Cache{Ccache, Sccache} +) + +type Cache struct { + Name string + CacheDir string // CacheDir is the chroot-internal cache directory. +} diff --git a/builder/main.go b/builder/main.go index 93ad482..6931fa0 100644 --- a/builder/main.go +++ b/builder/main.go @@ -51,17 +51,16 @@ const ( // PackageCacheDirectory is where we share packages between all builders. PackageCacheDirectory = "/var/lib/solbuild/packages" - // CcacheDirectory is the system wide ccache directory. - CcacheDirectory = "/var/lib/solbuild/ccache/ypkg" - - // LegacyCcacheDirectory is the root owned ccache directory for pspec.xml. - LegacyCcacheDirectory = "/var/lib/solbuild/ccache/legacy" - - // SccacheDirectory is the root owned sccache directory. - SccacheDirectory = "/var/lib/solbuild/sccache/ypkg" - - // LegacySccacheDirectory is the root owned ccache directory for pspec.xml. - LegacySccacheDirectory = "/var/lib/solbuild/sccache/legacy" + // CacheDirectory is where packages' build cache are stored + CacheDirectory = "/var/lib/solbuild/cache" + + // Obsolete cache directories. These are only still specified so that the + // `delete-cache -a` subcommand will remove them. In the future they will + // be removed. + ObsoleteCcacheDirectory = "/var/lib/solbuild/ccache/ypkg" + ObsoleteLegacyCcacheDirectory = "/var/lib/solbuild/ccache/legacy" + ObsoleteSccacheDirectory = "/var/lib/solbuild/sccache/ypkg" + ObsoleteLegacySccacheDirectory = "/var/lib/solbuild/sccache/legacy" ) const ( diff --git a/cli/delete_cache.go b/cli/delete_cache.go index b085c91..58a2482 100644 --- a/cli/delete_cache.go +++ b/cli/delete_cache.go @@ -76,10 +76,11 @@ func DeleteCacheRun(r *cmd.Root, s *cmd.Sub) { if sFlags.Sizes { sizeDirs := []string{ manager.Config.OverlayRootDir, - builder.CcacheDirectory, - builder.LegacyCcacheDirectory, - builder.SccacheDirectory, - builder.LegacySccacheDirectory, + builder.CacheDirectory, + builder.ObsoleteCcacheDirectory, + builder.ObsoleteSccacheDirectory, + builder.ObsoleteLegacyCcacheDirectory, + builder.ObsoleteLegacySccacheDirectory, builder.PackageCacheDirectory, source.SourceDir, } @@ -108,10 +109,11 @@ func DeleteCacheRun(r *cmd.Root, s *cmd.Sub) { } if sFlags.All { nukeDirs = append(nukeDirs, []string{ - builder.CcacheDirectory, - builder.LegacyCcacheDirectory, - builder.SccacheDirectory, - builder.LegacySccacheDirectory, + builder.CacheDirectory, + builder.ObsoleteCcacheDirectory, + builder.ObsoleteSccacheDirectory, + builder.ObsoleteLegacyCcacheDirectory, + builder.ObsoleteLegacySccacheDirectory, builder.PackageCacheDirectory, source.SourceDir, }...) From b3f9274b1820aea1a6891f74993425418d193e8b Mon Sep 17 00:00:00 2001 From: Gavin Zhao Date: Wed, 20 Sep 2023 19:49:52 -0400 Subject: [PATCH 2/7] Be friends with linters Signed-off-by: Gavin Zhao --- builder/build.go | 11 +++++++---- builder/main.go | 2 +- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/builder/build.go b/builder/build.go index 990d93a..6fe2bbc 100644 --- a/builder/build.go +++ b/builder/build.go @@ -115,13 +115,15 @@ func (p *Package) BindSources(o *Overlay) error { return nil } -// BindCache will make all cache defined in [caches] available to the build +// BindCache will make all cache defined in [caches] available to the build. func (p *Package) BindCaches(o *Overlay) error { mountMan := disk.GetMountManager() for _, c := range Caches { - var cacheSource string - var cacheDir string + var ( + cacheSource string + cacheDir string + ) if p.Type == PackageTypeYpkg { cacheSource = filepath.Join(CacheDirectory, c.Name, "ypkg") @@ -132,8 +134,9 @@ func (p *Package) BindCaches(o *Overlay) error { // Bind mount local ccache into chroot if err := mountMan.BindMount(cacheSource, cacheDir); err != nil { - return fmt.Errorf("Failed to bind mount %s %s, reason: %s\n", c.Name, cacheDir, err) + return fmt.Errorf("Failed to bind mount %s %s, reason: %w\n", c.Name, cacheDir, err) } + o.ExtraMounts = append(o.ExtraMounts, cacheDir) } diff --git a/builder/main.go b/builder/main.go index 6931fa0..cd04d16 100644 --- a/builder/main.go +++ b/builder/main.go @@ -51,7 +51,7 @@ const ( // PackageCacheDirectory is where we share packages between all builders. PackageCacheDirectory = "/var/lib/solbuild/packages" - // CacheDirectory is where packages' build cache are stored + // CacheDirectory is where packages' build cache are stored. CacheDirectory = "/var/lib/solbuild/cache" // Obsolete cache directories. These are only still specified so that the From e21d19df44fea129d8546a47b11eb3727d2da701 Mon Sep 17 00:00:00 2001 From: Gavin Zhao Date: Wed, 20 Sep 2023 19:56:57 -0400 Subject: [PATCH 3/7] Ensure caches are only bound for YPKG builds Signed-off-by: Gavin Zhao --- builder/build.go | 45 ++++++--------------------------------------- 1 file changed, 6 insertions(+), 39 deletions(-) diff --git a/builder/build.go b/builder/build.go index 6fe2bbc..5618c3f 100644 --- a/builder/build.go +++ b/builder/build.go @@ -117,18 +117,15 @@ func (p *Package) BindSources(o *Overlay) error { // BindCache will make all cache defined in [caches] available to the build. func (p *Package) BindCaches(o *Overlay) error { + if p.Type == PackageTypeXML { + return fmt.Errorf("Failed to bind caches, reason: not YPKG build") + } + mountMan := disk.GetMountManager() for _, c := range Caches { - var ( - cacheSource string - cacheDir string - ) - - if p.Type == PackageTypeYpkg { - cacheSource = filepath.Join(CacheDirectory, c.Name, "ypkg") - cacheDir = filepath.Join(o.MountPoint, c.CacheDir[1:]) - } + cacheSource := filepath.Join(CacheDirectory, c.Name, "ypkg") + cacheDir := filepath.Join(o.MountPoint, c.CacheDir[1:]) log.Debugf("Exposing %s to build %s\n", c.Name, cacheDir) @@ -173,36 +170,6 @@ func (p *Package) GetSourceDirInternal() string { return filepath.Join(BuildUserHome, "YPKG", "sources") } -// GetCcacheDir will return the externally visible ccache directory. -func (p *Package) GetCcacheDir(o *Overlay) string { - return filepath.Join(o.MountPoint, p.GetCcacheDirInternal()[1:]) -} - -// GetCcacheDirInternal will return the chroot-internal ccache directory -// for the given build type. -func (p *Package) GetCcacheDirInternal() string { - if p.Type == PackageTypeXML { - return "/root/.ccache" - } - - return filepath.Join(BuildUserHome, ".ccache") -} - -// GetSccacheDir will return the externally visible sccache directory. -func (p *Package) GetSccacheDir(o *Overlay) string { - return filepath.Join(o.MountPoint, p.GetSccacheDirInternal()[1:]) -} - -// GetSccacheDirInternal will return the chroot-internal sccache -// directory for the given build type. -func (p *Package) GetSccacheDirInternal() string { - if p.Type == PackageTypeXML { - return "/root/.cache/sccache" - } - - return filepath.Join(BuildUserHome, ".cache", "sccache") -} - // CopyAssets will copy all of the required assets into the builder root. func (p *Package) CopyAssets(h *PackageHistory, o *Overlay) error { baseDir := filepath.Dir(p.Path) From 2862830e330637f6836d0e8f1d4176c9980bc4e4 Mon Sep 17 00:00:00 2001 From: Gavin Zhao Date: Sat, 11 Nov 2023 12:38:24 -0500 Subject: [PATCH 4/7] Keep up with changes Signed-off-by: Gavin Zhao --- builder/build.go | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/builder/build.go b/builder/build.go index 5618c3f..a34758c 100644 --- a/builder/build.go +++ b/builder/build.go @@ -32,6 +32,12 @@ func (p *Package) CreateDirs(o *Overlay) error { p.GetWorkDir(o), p.GetSourceDir(o), } + if p.Type == PackageTypeYpkg { + for _, cache := range Caches { + dirs = append(dirs, filepath.Join(o.MountPoint, cache.CacheDir[1:])) + } + } + for _, p := range dirs { if err := os.MkdirAll(p, 0o0755); err != nil { return fmt.Errorf("Failed to create required directory %s. Reason: %w\n", p, err) @@ -44,7 +50,7 @@ func (p *Package) CreateDirs(o *Overlay) error { // maintenance burden. if p.Type == PackageTypeYpkg { for _, cache := range Caches { - if err := os.MkdirAll(cache.CacheDir, 0o0755); err != nil { + if err := os.MkdirAll(filepath.Join(CacheDirectory, cache.Name), 0o0755); err != nil { return fmt.Errorf("Failed to create cache directory %s for %s, reason: %w\n", cache.CacheDir, cache.Name, err) } } @@ -87,7 +93,7 @@ func (p *Package) BindSources(o *Overlay) error { } // Find the target path in the chroot - slog.Debug("Exposing source to container", "target", bindConfig.BindTarget) + slog.Debug("Exposing source to container", "source", bindConfig.BindSource, "target", bindConfig.BindTarget) if st, err := os.Stat(bindConfig.BindSource); err == nil && st != nil { if st.IsDir() { @@ -124,10 +130,10 @@ func (p *Package) BindCaches(o *Overlay) error { mountMan := disk.GetMountManager() for _, c := range Caches { - cacheSource := filepath.Join(CacheDirectory, c.Name, "ypkg") + cacheSource := filepath.Join(CacheDirectory, c.Name) cacheDir := filepath.Join(o.MountPoint, c.CacheDir[1:]) - log.Debugf("Exposing %s to build %s\n", c.Name, cacheDir) + slog.Debug("Exposing cache to build", "cache", c.Name, "source", cacheSource, "target", cacheDir) // Bind mount local ccache into chroot if err := mountMan.BindMount(cacheSource, cacheDir); err != nil { From a9bf4a4feccd4204581a3445ed0d67f85ffa0203 Mon Sep 17 00:00:00 2001 From: Gavin Zhao Date: Sat, 11 Nov 2023 12:52:43 -0500 Subject: [PATCH 5/7] Addresss linter Signed-off-by: Gavin Zhao --- builder/build.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/builder/build.go b/builder/build.go index a34758c..192ef2c 100644 --- a/builder/build.go +++ b/builder/build.go @@ -32,6 +32,8 @@ func (p *Package) CreateDirs(o *Overlay) error { p.GetWorkDir(o), p.GetSourceDir(o), } + + // Add cache directories. if p.Type == PackageTypeYpkg { for _, cache := range Caches { dirs = append(dirs, filepath.Join(o.MountPoint, cache.CacheDir[1:])) From f0c16eb94a67a3e563a77ed66fb8f31319e87149 Mon Sep 17 00:00:00 2001 From: Gavin Zhao Date: Sat, 11 Nov 2023 13:59:30 -0500 Subject: [PATCH 6/7] Fix cache dir writing permissions Signed-off-by: Gavin Zhao --- builder/build.go | 34 ++++++++++++++++++++-------------- builder/cache.go | 12 +++++++++++- 2 files changed, 31 insertions(+), 15 deletions(-) diff --git a/builder/build.go b/builder/build.go index 192ef2c..3ea40d9 100644 --- a/builder/build.go +++ b/builder/build.go @@ -33,28 +33,34 @@ func (p *Package) CreateDirs(o *Overlay) error { p.GetSourceDir(o), } - // Add cache directories. - if p.Type == PackageTypeYpkg { - for _, cache := range Caches { - dirs = append(dirs, filepath.Join(o.MountPoint, cache.CacheDir[1:])) - } - } - for _, p := range dirs { if err := os.MkdirAll(p, 0o0755); err != nil { - return fmt.Errorf("Failed to create required directory %s. Reason: %w\n", p, err) + return fmt.Errorf("Failed to create required directory %s. Reason: %w", p, err) } } - // Ensure we have root owned cache directories. - // - // Currently we are only using build caches for YPKG builds to reduce - // maintenance burden. + // Create cache directories if p.Type == PackageTypeYpkg { for _, cache := range Caches { - if err := os.MkdirAll(filepath.Join(CacheDirectory, cache.Name), 0o0755); err != nil { - return fmt.Errorf("Failed to create cache directory %s for %s, reason: %w\n", cache.CacheDir, cache.Name, err) + inRootCacheDir := filepath.Join(o.MountPoint, cache.CacheDir[1:]) + hostCacheDir := filepath.Join(CacheDirectory, cache.Name) + + // Cache directories in build root. + if err := os.MkdirAll(inRootCacheDir, 0o0755); err != nil { + return fmt.Errorf("Failed to create cache directory %s in build root, reason: %w", inRootCacheDir, err) } + + // Cache directory in host. + // Ensure we have root owned cache directories. + if err := os.MkdirAll(hostCacheDir, 0o0755); err != nil { + return fmt.Errorf("Failed to create cache directory %s for %s, reason: %w", cache.CacheDir, cache.Name, err) + } + + // Ensure the build user can write to the cache directories. + if err := os.Chown(hostCacheDir, BuildUserID, BuildUserGID); err != nil { + return fmt.Errorf("Failed to chown cache directory %s in build root, reason: %w", inRootCacheDir, err) + } + } } diff --git a/builder/cache.go b/builder/cache.go index a00fb26..e8389e4 100644 --- a/builder/cache.go +++ b/builder/cache.go @@ -15,7 +15,17 @@ var ( CacheDir: path.Join(BuildUserHome, ".cache", "sccache"), } - Caches = []Cache{Ccache, Sccache} + Bazel = Cache{ + Name: "bazel", + CacheDir: path.Join(BuildUserHome, ".cache", "bazel"), + } + + GoBuild = Cache{ + Name: "go-build", + CacheDir: path.Join(BuildUserHome, ".cache", "go-build"), + } + + Caches = []Cache{Bazel, Ccache, GoBuild, Sccache} ) type Cache struct { From 3cbe634cac95bd6ca97ad0d0250a138c531960ed Mon Sep 17 00:00:00 2001 From: Gavin Zhao Date: Sat, 11 Nov 2023 18:50:54 -0500 Subject: [PATCH 7/7] Make linter happy Signed-off-by: Gavin Zhao --- builder/build.go | 1 - 1 file changed, 1 deletion(-) diff --git a/builder/build.go b/builder/build.go index 3ea40d9..4e3dacd 100644 --- a/builder/build.go +++ b/builder/build.go @@ -60,7 +60,6 @@ func (p *Package) CreateDirs(o *Overlay) error { if err := os.Chown(hostCacheDir, BuildUserID, BuildUserGID); err != nil { return fmt.Errorf("Failed to chown cache directory %s in build root, reason: %w", inRootCacheDir, err) } - } }