Skip to content

Commit

Permalink
Remove pre-existing Elemental initrds
Browse files Browse the repository at this point in the history
Signed-off-by: David Cassany <dcassany@suse.com>
(cherry picked from commit 9f2f236)
  • Loading branch information
davidcassany committed Jun 10, 2024
1 parent 099894e commit e536b50
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 14 deletions.
22 changes: 22 additions & 0 deletions pkg/action/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,13 @@ func RunInit(cfg *types.RunConfig, spec *types.InitSpec) error {
}
}

cfg.Config.Logger.Info("Remove any pre-existing initrd")
err = removeElementalInitrds(cfg)
if err != nil {
cfg.Config.Logger.Errorf("failed removing any pre-existing Elemental initrd: %s", err.Error())
return err
}

cfg.Config.Logger.Infof("Generate initrd.")
output, err := cfg.Runner.Run("dracut", "-f", fmt.Sprintf("%s-%s", constants.ElementalInitrd, version), version)
if err != nil {
Expand All @@ -99,3 +106,18 @@ func RunInit(cfg *types.RunConfig, spec *types.InitSpec) error {

return err
}

func removeElementalInitrds(cfg *types.RunConfig) error {
initImgs, err := utils.FindFiles(cfg.Fs, "/", fmt.Sprintf("%s-*", constants.ElementalInitrd))
if err != nil {
return err
}
for _, img := range initImgs {
cfg.Config.Logger.Debugf("Removing pre-existing elemental initrd file: %s", img)
err = cfg.Fs.Remove(img)
if err != nil {
return err
}
}
return nil
}
7 changes: 7 additions & 0 deletions pkg/action/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ var _ = Describe("Init Action", func() {
Expect(len(enabledUnits)).To(Equal(0))
})
It("Successfully runs init and install files", func() {
preExistingInitrd := "/boot/elemental.initrd-6.2"
_, err := fs.Create(preExistingInitrd)
Expect(err).NotTo(HaveOccurred())

Expect(action.RunInit(cfg, spec)).To(Succeed())

Expect(len(enabledUnits)).To(Equal(expectedNumUnits))
Expand All @@ -124,6 +128,9 @@ var _ = Describe("Init Action", func() {
exists, _ := utils.Exists(fs, "/boot/elemental.initrd-6.4")
Expect(exists).To(BeTrue())

exists, _ = utils.Exists(fs, preExistingInitrd)
Expect(exists).To(BeFalse())

// Check expected initrd and kernel files are created
exists, _ = utils.Exists(fs, "/boot/vmlinuz")
Expect(exists).To(BeTrue())
Expand Down
41 changes: 32 additions & 9 deletions pkg/utils/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -453,10 +453,15 @@ func FindFile(vfs types.FS, rootDir string, patterns ...string) (string, error)
return found, nil
}

// findFile attempts to find a file from a given pattern on top of a root path.
// Returns empty path if no file is found.
func findFile(vfs types.FS, rootDir, pattern string) (string, error) {
var foundFile string
// FindFiles attempts to find files from a given pattern on top of a root path.
// Returns empty list if no files are found.
func FindFiles(vfs types.FS, rootDir string, pattern string) ([]string, error) {
return findFiles(vfs, rootDir, pattern, false)
}

func findFiles(vfs types.FS, rootDir, pattern string, fristMatchReturn bool) ([]string, error) {
foundFiles := []string{}

base := filepath.Join(rootDir, getBaseDir(pattern))
if ok, _ := Exists(vfs, base); ok {
err := WalkDirFs(vfs, base, func(path string, d fs.DirEntry, err error) error {
Expand All @@ -468,19 +473,37 @@ func findFile(vfs types.FS, rootDir, pattern string) (string, error) {
return err
}
if match {
foundFile, err = resolveLink(vfs, path, rootDir, d, constants.MaxLinkDepth)
foundFile, err := resolveLink(vfs, path, rootDir, d, constants.MaxLinkDepth)
if err != nil {
return err
}
return io.EOF
foundFiles = append(foundFiles, foundFile)
if fristMatchReturn {
return io.EOF
}
return nil
}
return nil
})
if err != nil && err != io.EOF {
return "", err
return []string{}, err
}
}
return foundFile, nil
return foundFiles, nil
}

// findFile attempts to find a file from a given pattern on top of a root path.
// Returns empty path if no file is found.
func findFile(vfs types.FS, rootDir, pattern string) (string, error) {
files, err := findFiles(vfs, rootDir, pattern, true)
if err != nil {
return "", err
}

if len(files) > 0 {
return files[0], nil
}
return "", nil
}

// FindKernel finds for kernel files inside a given root tree path.
Expand All @@ -491,7 +514,7 @@ func FindKernel(fs types.FS, rootDir string) (string, string, error) {

kernel, err = FindFile(fs, rootDir, constants.GetKernelPatterns()...)
if err != nil {
return "", "", fmt.Errorf("No Kernel file found: %v", err)
return "", "", fmt.Errorf("no Kernel file found: %v", err)
}
files, err := fs.ReadDir(filepath.Join(rootDir, constants.KernelModulesDir))
if err != nil {
Expand Down
39 changes: 34 additions & 5 deletions pkg/utils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -808,11 +808,6 @@ var _ = Describe("Utils", Label("utils"), func() {
Expect(err).ShouldNot(HaveOccurred())
Expect(f).To(Equal(filepath.Join(rootDir, file2)))
})
It("finds a matching file, first match wins file2", func() {
f, err := utils.FindFile(fs, rootDir, "/path/with/*aarch64/find*", "/path/with/*dle*/*me.*")
Expect(err).ShouldNot(HaveOccurred())
Expect(f).To(Equal(filepath.Join(rootDir, file2)))
})
It("finds a matching file and resolves the link", func() {
f, err := utils.FindFile(fs, rootDir, "/path/*/symlink/pointing-to-*", "/path/with/*aarch64/find*")
Expect(err).ShouldNot(HaveOccurred())
Expand All @@ -829,6 +824,40 @@ var _ = Describe("Utils", Label("utils"), func() {
Expect(err.Error()).Should(ContainSubstring("syntax error"))
})
})
Describe("FindFiles", func() {
var rootDir, file1, file2, file3, relSymlink string
BeforeEach(func() {
// The root directory
rootDir = "/some/root"
Expect(utils.MkdirAll(fs, rootDir, constants.DirPerm)).To(Succeed())

// Files to find
file1 = "/path/with/needle/findme.extension1"
file2 = "/path/with/needle/findme.extension2"
file3 = "/path/with/needle/hardtofindme"
Expect(utils.MkdirAll(fs, filepath.Join(rootDir, filepath.Dir(file1)), constants.DirPerm)).To(Succeed())
Expect(fs.WriteFile(filepath.Join(rootDir, file1), []byte("file1"), constants.FilePerm)).To(Succeed())
Expect(fs.WriteFile(filepath.Join(rootDir, file2), []byte("file2"), constants.FilePerm)).To(Succeed())
Expect(fs.WriteFile(filepath.Join(rootDir, file3), []byte("file3"), constants.FilePerm)).To(Succeed())

// A symlink pointing to a relative path
relSymlink = "/path/with/needle/findme.symlink"
Expect(fs.Symlink("hardtofindme", filepath.Join(rootDir, relSymlink))).To(Succeed())
})
It("finds all matching files", func() {
f, err := utils.FindFiles(fs, rootDir, "/path/with/*dle*/find*")
Expect(err).ShouldNot(HaveOccurred())
Expect(len(f)).To(Equal(3))
Expect(f).Should(ContainElement(filepath.Join(rootDir, file1)))
Expect(f).Should(ContainElement(filepath.Join(rootDir, file2)))
Expect(f).Should(ContainElement(filepath.Join(rootDir, file3)))
})
It("Returns empty list if there is no match", func() {
f, err := utils.FindFiles(fs, rootDir, "/path/with/needle/notthere*")
Expect(err).ShouldNot(HaveOccurred())
Expect(len(f)).Should(Equal(0))
})
})
Describe("FindKernel", Label("find"), func() {
BeforeEach(func() {
Expect(utils.MkdirAll(fs, "/path/boot", constants.DirPerm)).To(Succeed())
Expand Down

0 comments on commit e536b50

Please sign in to comment.