Skip to content

Commit

Permalink
Fix stackable sources for build-iso (rancher#2061)
Browse files Browse the repository at this point in the history
* Fix stackable sources for build-iso
* Move syncFunc as a parameter of DumpSource

Signed-off-by: David Cassany <dcassany@suse.com>
  • Loading branch information
davidcassany authored May 2, 2024
1 parent 786cfa6 commit 22baf14
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 198 deletions.
4 changes: 2 additions & 2 deletions pkg/action/build-disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ func (b *BuildDiskAction) BuildDiskRun() (err error) { //nolint:gocyclo
recRoot := filepath.Join(workdir, filepath.Base(b.spec.RecoverySystem.File)+rootSuffix)

// Create recovery root
err = elemental.DumpSource(b.cfg.Config, recRoot, b.spec.RecoverySystem.Source)
err = elemental.MirrorRoot(b.cfg.Config, recRoot, b.spec.RecoverySystem.Source)
if err != nil {
b.cfg.Logger.Errorf("failed loading recovery image source tree: %s", err.Error())
return err
Expand Down Expand Up @@ -425,7 +425,7 @@ func (b *BuildDiskAction) createStatePartitionImage() (*types.Image, error) {
}

// Deploy system image
err = elemental.DumpSource(b.cfg.Config, b.snapshot.WorkDir, system)
err = elemental.MirrorRoot(b.cfg.Config, b.snapshot.WorkDir, system)
if err != nil {
_ = b.snapshotter.CloseTransactionOnError(b.snapshot)
b.cfg.Logger.Errorf("failed deploying source: %s", system.String())
Expand Down
2 changes: 1 addition & 1 deletion pkg/action/build-iso.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ func (b BuildISOAction) burnISO(root, efiImg string) error {

func (b BuildISOAction) applySources(target string, sources ...*types.ImageSource) error {
for _, src := range sources {
err := elemental.DumpSource(b.cfg.Config, target, src)
err := elemental.DumpSource(b.cfg.Config, target, src, utils.SyncData)
if err != nil {
return elementalError.NewFromError(err, elementalError.DumpSource)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/action/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ func (i InstallAction) Run() (err error) {
cleanup.PushErrorOnly(func() error { return i.snapshotter.CloseTransactionOnError(i.snapshot) })

// Deploy system image
err = elemental.DumpSource(i.cfg.Config, i.snapshot.WorkDir, i.spec.System)
err = elemental.MirrorRoot(i.cfg.Config, i.snapshot.WorkDir, i.spec.System)
if err != nil {
i.cfg.Logger.Errorf("failed deploying source: %s", i.spec.System.String())
return elementalError.NewFromError(err, elementalError.DumpSource)
Expand Down
2 changes: 1 addition & 1 deletion pkg/action/reset.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ func (r ResetAction) Run() (err error) {
cleanup.PushErrorOnly(func() error { return r.snapshotter.CloseTransactionOnError(r.snapshot) })

// Deploy system image
err = elemental.DumpSource(r.cfg.Config, r.snapshot.WorkDir, r.spec.System)
err = elemental.MirrorRoot(r.cfg.Config, r.snapshot.WorkDir, r.spec.System)
if err != nil {
r.cfg.Logger.Errorf("failed deploying source: %s", r.spec.System.String())
return elementalError.NewFromError(err, elementalError.DumpSource)
Expand Down
2 changes: 1 addition & 1 deletion pkg/action/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ func (u *UpgradeAction) Run() (err error) {
cleanup.PushErrorOnly(func() error { return u.snapshotter.CloseTransactionOnError(u.snapshot) })

// Deploy system image
err = elemental.DumpSource(u.cfg.Config, u.snapshot.WorkDir, u.spec.System)
err = elemental.MirrorRoot(u.cfg.Config, u.snapshot.WorkDir, u.spec.System)
if err != nil {
u.cfg.Logger.Errorf("failed deploying source: %s", u.spec.System.String())
return elementalError.NewFromError(err, elementalError.DumpSource)
Expand Down
79 changes: 26 additions & 53 deletions pkg/elemental/elemental.go
Original file line number Diff line number Diff line change
Expand Up @@ -438,49 +438,6 @@ func CopyFileImg(c types.Config, img *types.Image) error {
return err
}

// DeployImage will deploy the given image into the target. This method
// creates the filesystem image file and fills it with the correspondant data
func DeployImage(c types.Config, img *types.Image) error {
var err error
var cleaner func() error

c.Logger.Infof("Deploying image: %s", img.File)
transientTree := strings.TrimSuffix(img.File, filepath.Ext(img.File)) + ".imgTree"
if img.Source.IsDir() {
transientTree = img.Source.Value()
} else if img.Source.IsFile() {
srcImg := &types.Image{
File: img.Source.Value(),
MountPoint: transientTree,
}
err := MountFileSystemImage(c, srcImg)
if err != nil {
c.Logger.Errorf("failed mounting image tree: %v", err)
return err
}
cleaner = func() error {
err := UnmountFileSystemImage(c, srcImg)
if err != nil {
return err
}
return c.Fs.RemoveAll(transientTree)
}
} else if img.Source.IsImage() {
err = DumpSource(c, transientTree, img.Source)
if err != nil {
c.Logger.Errorf("failed dumping image tree: %v", err)
return err
}
cleaner = func() error { return c.Fs.RemoveAll(transientTree) }
}
err = CreateImageFromTree(c, img, transientTree, false, cleaner)
if err != nil {
c.Logger.Errorf("failed creating image from image tree: %v", err)
return err
}
return nil
}

// DeployRecoverySystem deploys the rootfs image from the img parameter and
// extracts kernel+initrd to the same directory.
// This can be used for both ISO (all artifacts in same output dir) and raw
Expand Down Expand Up @@ -518,7 +475,7 @@ func DeployRecoverySystem(cfg types.Config, img *types.Image) error {
return cfg.Fs.RemoveAll(transientTree)
}
} else if img.Source.IsImage() {
err = DumpSource(cfg, transientTree, img.Source)
err = MirrorRoot(cfg, transientTree, img.Source)
if err != nil {
cfg.Logger.Errorf("failed dumping image tree: %v", err)
return err
Expand Down Expand Up @@ -585,11 +542,21 @@ func DeployRecoverySystem(cfg types.Config, img *types.Image) error {
return nil
}

// DumpSource sets the image data according to the image source type
func DumpSource(c types.Config, target string, imgSrc *types.ImageSource) error { // nolint:gocyclo
// DumpSource dumps the imgSrc data to target. SyncFunc argument is the function used to synchronize file or directory
// sources (unused for contaier images), defaults to utils.SyncData if nil provided.
func DumpSource(
c types.Config, target string, imgSrc *types.ImageSource,
syncFunc func(
l types.Logger, r types.Runner, f types.FS, src string, dst string, excl ...string,
) error,
) error { // nolint:gocyclo
var err error
var digest string

if syncFunc == nil {
syncFunc = utils.SyncData
}

c.Logger.Infof("Copying %s source...", imgSrc.Value())

err = utils.MkdirAll(c.Fs, target, cnst.DirPerm)
Expand Down Expand Up @@ -618,7 +585,7 @@ func DumpSource(c types.Config, target string, imgSrc *types.ImageSource) error
imgSrc.SetDigest(digest)
} else if imgSrc.IsDir() {
excludes := cnst.GetDefaultSystemExcludes(imgSrc.Value())
err = utils.MirrorData(c.Logger, c.Runner, c.Fs, imgSrc.Value(), target, excludes...)
err = syncFunc(c.Logger, c.Runner, c.Fs, imgSrc.Value(), target, excludes...)
if err != nil {
return err
}
Expand All @@ -633,22 +600,28 @@ func DumpSource(c types.Config, target string, imgSrc *types.ImageSource) error
return err
}
defer UnmountFileSystemImage(c, img) // nolint:errcheck
err = utils.MirrorData(c.Logger, c.Runner, c.Fs, cnst.ImgSrcDir, target)
err = syncFunc(c.Logger, c.Runner, c.Fs, cnst.ImgSrcDir, target)
if err != nil {
return err
}
} else {
return fmt.Errorf("unknown image source type")
}
// Create essential directories such as /tmp, /dev, etc.
err = utils.CreateDirStructure(c.Fs, target)
if err != nil {
return err
}

c.Logger.Infof("Finished copying %s into %s", imgSrc.Value(), target)
return nil
}

// MirrorRoot mirrors image source contents to target. Any preexisting data in target is going to be overwritten or
// deleted to perfectly match image source contents.
func MirrorRoot(c types.Config, target string, imgSrc *types.ImageSource) error {
err := DumpSource(c, target, imgSrc, utils.MirrorData)
if err != nil {
return nil
}
return utils.CreateDirStructure(c.Fs, target)
}

// CopyCloudConfig will check if there is a cloud init in the config and store it on the target
func CopyCloudConfig(c types.Config, path string, cloudInit []string) (err error) {
if path == "" {
Expand Down
Loading

0 comments on commit 22baf14

Please sign in to comment.