Skip to content

Commit e536b50

Browse files
committed
Remove pre-existing Elemental initrds
Signed-off-by: David Cassany <[email protected]> (cherry picked from commit 9f2f236)
1 parent 099894e commit e536b50

File tree

4 files changed

+95
-14
lines changed

4 files changed

+95
-14
lines changed

pkg/action/init.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,13 @@ func RunInit(cfg *types.RunConfig, spec *types.InitSpec) error {
7676
}
7777
}
7878

79+
cfg.Config.Logger.Info("Remove any pre-existing initrd")
80+
err = removeElementalInitrds(cfg)
81+
if err != nil {
82+
cfg.Config.Logger.Errorf("failed removing any pre-existing Elemental initrd: %s", err.Error())
83+
return err
84+
}
85+
7986
cfg.Config.Logger.Infof("Generate initrd.")
8087
output, err := cfg.Runner.Run("dracut", "-f", fmt.Sprintf("%s-%s", constants.ElementalInitrd, version), version)
8188
if err != nil {
@@ -99,3 +106,18 @@ func RunInit(cfg *types.RunConfig, spec *types.InitSpec) error {
99106

100107
return err
101108
}
109+
110+
func removeElementalInitrds(cfg *types.RunConfig) error {
111+
initImgs, err := utils.FindFiles(cfg.Fs, "/", fmt.Sprintf("%s-*", constants.ElementalInitrd))
112+
if err != nil {
113+
return err
114+
}
115+
for _, img := range initImgs {
116+
cfg.Config.Logger.Debugf("Removing pre-existing elemental initrd file: %s", img)
117+
err = cfg.Fs.Remove(img)
118+
if err != nil {
119+
return err
120+
}
121+
}
122+
return nil
123+
}

pkg/action/init_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,10 @@ var _ = Describe("Init Action", func() {
111111
Expect(len(enabledUnits)).To(Equal(0))
112112
})
113113
It("Successfully runs init and install files", func() {
114+
preExistingInitrd := "/boot/elemental.initrd-6.2"
115+
_, err := fs.Create(preExistingInitrd)
116+
Expect(err).NotTo(HaveOccurred())
117+
114118
Expect(action.RunInit(cfg, spec)).To(Succeed())
115119

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

131+
exists, _ = utils.Exists(fs, preExistingInitrd)
132+
Expect(exists).To(BeFalse())
133+
127134
// Check expected initrd and kernel files are created
128135
exists, _ = utils.Exists(fs, "/boot/vmlinuz")
129136
Expect(exists).To(BeTrue())

pkg/utils/common.go

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -453,10 +453,15 @@ func FindFile(vfs types.FS, rootDir string, patterns ...string) (string, error)
453453
return found, nil
454454
}
455455

456-
// findFile attempts to find a file from a given pattern on top of a root path.
457-
// Returns empty path if no file is found.
458-
func findFile(vfs types.FS, rootDir, pattern string) (string, error) {
459-
var foundFile string
456+
// FindFiles attempts to find files from a given pattern on top of a root path.
457+
// Returns empty list if no files are found.
458+
func FindFiles(vfs types.FS, rootDir string, pattern string) ([]string, error) {
459+
return findFiles(vfs, rootDir, pattern, false)
460+
}
461+
462+
func findFiles(vfs types.FS, rootDir, pattern string, fristMatchReturn bool) ([]string, error) {
463+
foundFiles := []string{}
464+
460465
base := filepath.Join(rootDir, getBaseDir(pattern))
461466
if ok, _ := Exists(vfs, base); ok {
462467
err := WalkDirFs(vfs, base, func(path string, d fs.DirEntry, err error) error {
@@ -468,19 +473,37 @@ func findFile(vfs types.FS, rootDir, pattern string) (string, error) {
468473
return err
469474
}
470475
if match {
471-
foundFile, err = resolveLink(vfs, path, rootDir, d, constants.MaxLinkDepth)
476+
foundFile, err := resolveLink(vfs, path, rootDir, d, constants.MaxLinkDepth)
472477
if err != nil {
473478
return err
474479
}
475-
return io.EOF
480+
foundFiles = append(foundFiles, foundFile)
481+
if fristMatchReturn {
482+
return io.EOF
483+
}
484+
return nil
476485
}
477486
return nil
478487
})
479488
if err != nil && err != io.EOF {
480-
return "", err
489+
return []string{}, err
481490
}
482491
}
483-
return foundFile, nil
492+
return foundFiles, nil
493+
}
494+
495+
// findFile attempts to find a file from a given pattern on top of a root path.
496+
// Returns empty path if no file is found.
497+
func findFile(vfs types.FS, rootDir, pattern string) (string, error) {
498+
files, err := findFiles(vfs, rootDir, pattern, true)
499+
if err != nil {
500+
return "", err
501+
}
502+
503+
if len(files) > 0 {
504+
return files[0], nil
505+
}
506+
return "", nil
484507
}
485508

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

492515
kernel, err = FindFile(fs, rootDir, constants.GetKernelPatterns()...)
493516
if err != nil {
494-
return "", "", fmt.Errorf("No Kernel file found: %v", err)
517+
return "", "", fmt.Errorf("no Kernel file found: %v", err)
495518
}
496519
files, err := fs.ReadDir(filepath.Join(rootDir, constants.KernelModulesDir))
497520
if err != nil {

pkg/utils/utils_test.go

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -808,11 +808,6 @@ var _ = Describe("Utils", Label("utils"), func() {
808808
Expect(err).ShouldNot(HaveOccurred())
809809
Expect(f).To(Equal(filepath.Join(rootDir, file2)))
810810
})
811-
It("finds a matching file, first match wins file2", func() {
812-
f, err := utils.FindFile(fs, rootDir, "/path/with/*aarch64/find*", "/path/with/*dle*/*me.*")
813-
Expect(err).ShouldNot(HaveOccurred())
814-
Expect(f).To(Equal(filepath.Join(rootDir, file2)))
815-
})
816811
It("finds a matching file and resolves the link", func() {
817812
f, err := utils.FindFile(fs, rootDir, "/path/*/symlink/pointing-to-*", "/path/with/*aarch64/find*")
818813
Expect(err).ShouldNot(HaveOccurred())
@@ -829,6 +824,40 @@ var _ = Describe("Utils", Label("utils"), func() {
829824
Expect(err.Error()).Should(ContainSubstring("syntax error"))
830825
})
831826
})
827+
Describe("FindFiles", func() {
828+
var rootDir, file1, file2, file3, relSymlink string
829+
BeforeEach(func() {
830+
// The root directory
831+
rootDir = "/some/root"
832+
Expect(utils.MkdirAll(fs, rootDir, constants.DirPerm)).To(Succeed())
833+
834+
// Files to find
835+
file1 = "/path/with/needle/findme.extension1"
836+
file2 = "/path/with/needle/findme.extension2"
837+
file3 = "/path/with/needle/hardtofindme"
838+
Expect(utils.MkdirAll(fs, filepath.Join(rootDir, filepath.Dir(file1)), constants.DirPerm)).To(Succeed())
839+
Expect(fs.WriteFile(filepath.Join(rootDir, file1), []byte("file1"), constants.FilePerm)).To(Succeed())
840+
Expect(fs.WriteFile(filepath.Join(rootDir, file2), []byte("file2"), constants.FilePerm)).To(Succeed())
841+
Expect(fs.WriteFile(filepath.Join(rootDir, file3), []byte("file3"), constants.FilePerm)).To(Succeed())
842+
843+
// A symlink pointing to a relative path
844+
relSymlink = "/path/with/needle/findme.symlink"
845+
Expect(fs.Symlink("hardtofindme", filepath.Join(rootDir, relSymlink))).To(Succeed())
846+
})
847+
It("finds all matching files", func() {
848+
f, err := utils.FindFiles(fs, rootDir, "/path/with/*dle*/find*")
849+
Expect(err).ShouldNot(HaveOccurred())
850+
Expect(len(f)).To(Equal(3))
851+
Expect(f).Should(ContainElement(filepath.Join(rootDir, file1)))
852+
Expect(f).Should(ContainElement(filepath.Join(rootDir, file2)))
853+
Expect(f).Should(ContainElement(filepath.Join(rootDir, file3)))
854+
})
855+
It("Returns empty list if there is no match", func() {
856+
f, err := utils.FindFiles(fs, rootDir, "/path/with/needle/notthere*")
857+
Expect(err).ShouldNot(HaveOccurred())
858+
Expect(len(f)).Should(Equal(0))
859+
})
860+
})
832861
Describe("FindKernel", Label("find"), func() {
833862
BeforeEach(func() {
834863
Expect(utils.MkdirAll(fs, "/path/boot", constants.DirPerm)).To(Succeed())

0 commit comments

Comments
 (0)