From af01ee7dda3c1b644f43230ae466b6dc7ceb97c3 Mon Sep 17 00:00:00 2001 From: Aleksandr Nogikh Date: Fri, 8 Apr 2022 17:28:04 +0000 Subject: pkg/build: mount /etc before copying the sysctl file Assume that /etc can be on a partition different from the one, where the kernel resides. --- pkg/build/linux_linux.go | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) (limited to 'pkg/build/linux_linux.go') diff --git a/pkg/build/linux_linux.go b/pkg/build/linux_linux.go index e0a36f34d..4a8270353 100644 --- a/pkg/build/linux_linux.go +++ b/pkg/build/linux_linux.go @@ -55,7 +55,7 @@ func embedLinuxKernel(params Params, kernelPath string) error { return err } if params.SysctlFile != "" { - if err := osutil.CopyFile(params.SysctlFile, filepath.Join(mountDir, "etc", "sysctl.conf")); err != nil { + if err := copySysctlFile(params.SysctlFile, loopFile, mountDir); err != nil { return err } } @@ -65,6 +65,22 @@ func embedLinuxKernel(params Params, kernelPath string) error { return osutil.CopyFile(imageFile, filepath.Join(params.OutputDir, "image")) } +func copySysctlFile(sysctlFile, loopFile, mountDir string) error { + etcFolder := filepath.Join(mountDir, "etc") + for idx := 2; ; idx++ { + if osutil.IsExist(etcFolder) { + break + } + err := tryMount(fmt.Sprintf("%sp%d", loopFile, idx), mountDir) + if err != nil { + // Most likely we've just run out of partitions. + return fmt.Errorf("didn't find a partition that has /etc") + } + defer unix.Unmount(mountDir, 0) + } + return osutil.CopyFile(sysctlFile, filepath.Join(etcFolder, "sysctl.conf")) +} + func tryMount(device, mountDir string) error { var err error loop: -- cgit mrf-deployment