From 5ca4d58dc9a2477db44d34c9f30ed21b0c3d2131 Mon Sep 17 00:00:00 2001 From: Andrey Smirnov Date: Tue, 19 Sep 2023 22:37:40 +0400 Subject: [PATCH] fix: generate of modules.dep when on the machine When running on the machine, the extensionTreePath is not writeable, so create and clean up a temporary directory to host `modules.dep` extension. Signed-off-by: Andrey Smirnov --- internal/pkg/extensions/kernel_modules.go | 4 ++-- pkg/imager/extensions/extensions.go | 14 +++++++++++++- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/internal/pkg/extensions/kernel_modules.go b/internal/pkg/extensions/kernel_modules.go index 2d4f7eaa40..1a0af71a17 100644 --- a/internal/pkg/extensions/kernel_modules.go +++ b/internal/pkg/extensions/kernel_modules.go @@ -39,7 +39,7 @@ func (ext *Extension) KernelModuleDirectory() string { // GenerateKernelModuleDependencyTreeExtension generates a kernel module dependency tree extension. // //nolint:gocyclo -func GenerateKernelModuleDependencyTreeExtension(extensionPathsWithKernelModules []string, initramfsPath, extensionTreePath string, printFunc func(format string, v ...any)) (*Extension, error) { +func GenerateKernelModuleDependencyTreeExtension(extensionPathsWithKernelModules []string, initramfsPath, scratchPath string, printFunc func(format string, v ...any)) (*Extension, error) { printFunc("preparing to run depmod to generate kernel modules dependency tree") tempDir, err := os.MkdirTemp("", "ext-modules") @@ -106,7 +106,7 @@ func GenerateKernelModuleDependencyTreeExtension(extensionPathsWithKernelModules } // we want the extension to survive this function, so not storing in a temporary directory - kernelModulesDependencyTreeStagingDir := filepath.Join(extensionTreePath, "modules.dep") + kernelModulesDependencyTreeStagingDir := filepath.Join(scratchPath, "modules.dep") // we want to make sure the root directory has the right permissions. if err := os.MkdirAll(kernelModulesDependencyTreeStagingDir, 0o755); err != nil { diff --git a/pkg/imager/extensions/extensions.go b/pkg/imager/extensions/extensions.go index 1efc312101..1ec898f296 100644 --- a/pkg/imager/extensions/extensions.go +++ b/pkg/imager/extensions/extensions.go @@ -28,6 +28,8 @@ type Builder struct { } // Build rebuilds the initramfs.xz with extensions. +// +//nolint:gocyclo func (builder *Builder) Build() error { extensionsList, err := extensions.List(builder.ExtensionTreePath) if err != nil { @@ -49,7 +51,17 @@ func (builder *Builder) Build() error { extensionPathsWithKernelModules := findExtensionsWithKernelModules(extensionsList) if len(extensionPathsWithKernelModules) > 0 { - kernelModuleDepExtension, genErr := extensions.GenerateKernelModuleDependencyTreeExtension(extensionPathsWithKernelModules, builder.InitramfsPath, builder.ExtensionTreePath, builder.Printf) + var scratchPath string + + // create a temporary directory to store 'modules.dep' extension + scratchPath, err = os.MkdirTemp("", "ext-modules") + if err != nil { + return err + } + + defer os.RemoveAll(scratchPath) //nolint:errcheck + + kernelModuleDepExtension, genErr := extensions.GenerateKernelModuleDependencyTreeExtension(extensionPathsWithKernelModules, builder.InitramfsPath, scratchPath, builder.Printf) if genErr != nil { return genErr }