Skip to content

Commit

Permalink
Set working directory for non chrooted build hooks
Browse files Browse the repository at this point in the history
This commit sets the working directory of build hooks relative to
output flag. This way it is possible to add hooks that not dependent
of the ouput path flag.

In addition this commit also adds a new embedded feature to copy
Raspberry PI firmware to the expected location.

Signed-off-by: David Cassany <dcassany@suse.com>
  • Loading branch information
davidcassany committed Sep 23, 2024
1 parent b4765b9 commit eb83a89
Show file tree
Hide file tree
Showing 10 changed files with 68 additions and 25 deletions.
7 changes: 0 additions & 7 deletions examples/green-rpi/01_rpi-firmware.yaml

This file was deleted.

7 changes: 1 addition & 6 deletions examples/green-rpi/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,7 @@ COPY --from=TOOLKIT /usr/bin/elemental /usr/bin/elemental
RUN systemctl enable NetworkManager.service

# Generate initrd with required elemental services
RUN elemental init -f && \
kernel=$(ls /boot/Image-* | head -n1) && \
if [ -e "$kernel" ]; then ln -sf "${kernel#/boot/}" /boot/vmlinuz; fi && \
rm -rf /var/log/update* && \
>/var/log/lastlog && \
rm -rf /boot/vmlinux*
RUN elemental --debug init --force

# Update os-release file with some metadata
RUN echo IMAGE_REPO=\"${REPO}\" >> /etc/os-release && \
Expand Down
10 changes: 5 additions & 5 deletions pkg/action/build-disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ func WithDiskBootloader(bootloader types.Bootloader) BuildDiskActionOption {
}
}

func (b *BuildDiskAction) buildDiskHook(hook string) error {
return Hook(&b.cfg.Config, hook, b.cfg.Strict, b.cfg.CloudInitPaths...)
func (b *BuildDiskAction) buildDiskHook(hook string, workdir string) error {
return Hook(&b.cfg.Config, hook, b.cfg.Strict, workdir, b.cfg.CloudInitPaths...)
}

func (b *BuildDiskAction) buildDiskChrootHook(hook string, root string) error {
Expand Down Expand Up @@ -149,7 +149,7 @@ func (b *BuildDiskAction) BuildDiskRun() (err error) { //nolint:gocyclo
rawImg = filepath.Join(b.cfg.OutDir, rawImg)

// Before disk hook happens before doing anything
err = b.buildDiskHook(constants.BeforeDiskHook)
err = b.buildDiskHook(constants.BeforeDiskHook, workdir)
if err != nil {
return elementalError.NewFromError(err, elementalError.HookBeforeDisk)
}
Expand Down Expand Up @@ -226,7 +226,7 @@ func (b *BuildDiskAction) BuildDiskRun() (err error) { //nolint:gocyclo
return elementalError.NewFromError(err, elementalError.HookAfterDiskChroot)
}
}
err = b.buildDiskHook(constants.AfterDiskHook)
err = b.buildDiskHook(constants.AfterDiskHook, workdir)
if err != nil {
return elementalError.NewFromError(err, elementalError.HookAfterDisk)
}
Expand Down Expand Up @@ -257,7 +257,7 @@ func (b *BuildDiskAction) BuildDiskRun() (err error) { //nolint:gocyclo
return err
}

err = b.buildDiskHook(constants.PostDiskHook)
err = b.buildDiskHook(constants.PostDiskHook, workdir)
if err != nil {
return elementalError.NewFromError(err, elementalError.HookPostDisk)
}
Expand Down
30 changes: 26 additions & 4 deletions pkg/action/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ limitations under the License.
package action

import (
"os"

"github.com/sirupsen/logrus"

elementalError "github.com/rancher/elemental-toolkit/v2/pkg/error"
Expand All @@ -25,12 +27,32 @@ import (
)

// Hook is RunStage wrapper that only adds logic to ignore errors
// in case types.RunConfig.Strict is set to false
func Hook(config *types.Config, hook string, strict bool, cloudInitPaths ...string) error {
// in case types.RunConfig.Strict is set to false and changes the default working directory
// to the given one. Previous working directory is restored after the method execution.
func Hook(config *types.Config, hook string, strict bool, workdir string, cloudInitPaths ...string) (err error) {
var currentPath string

config.Logger.Infof("Running %s hook", hook)
if workdir != "" {
currentPath, err = os.Getwd()
if err != nil {
return err
}
err = utils.Chdir(config.Fs, workdir)
if err != nil {
return err
}
defer func() {
// We can't use utils.Chdir to recover as Getwd is not aware of the given FS
nErr := os.Chdir(currentPath)
if err == nil && nErr != nil {
err = nErr
}
}()
}
oldLevel := config.Logger.GetLevel()
config.Logger.SetLevel(logrus.ErrorLevel)
err := utils.RunStage(config, hook, strict, cloudInitPaths...)
err = utils.RunStage(config, hook, strict, cloudInitPaths...)
config.Logger.SetLevel(oldLevel)
if !strict {
err = nil
Expand All @@ -41,7 +63,7 @@ func Hook(config *types.Config, hook string, strict bool, cloudInitPaths ...stri
// ChrootHook executes Hook inside a chroot environment
func ChrootHook(config *types.Config, hook string, strict bool, chrootDir string, bindMounts map[string]string, cloudInitPaths ...string) (err error) {
callback := func() error {
return Hook(config, hook, strict, cloudInitPaths...)
return Hook(config, hook, strict, "", cloudInitPaths...)
}
return utils.ChrootedCallback(config, chrootDir, bindMounts, callback)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/action/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func NewInstallAction(cfg *types.RunConfig, spec *types.InstallSpec, opts ...Ins
}

func (i *InstallAction) installHook(hook string) error {
return Hook(&i.cfg.Config, hook, i.cfg.Strict, i.cfg.CloudInitPaths...)
return Hook(&i.cfg.Config, hook, i.cfg.Strict, "", i.cfg.CloudInitPaths...)
}

func (i *InstallAction) installChrootHook(hook string, root string) error {
Expand Down
2 changes: 1 addition & 1 deletion pkg/action/reset.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import (
)

func (r *ResetAction) resetHook(hook string) error {
return Hook(&r.cfg.Config, hook, r.cfg.Strict, r.cfg.CloudInitPaths...)
return Hook(&r.cfg.Config, hook, r.cfg.Strict, "", r.cfg.CloudInitPaths...)
}

func (r *ResetAction) resetChrootHook(hook string, root string) error {
Expand Down
2 changes: 1 addition & 1 deletion pkg/action/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func (u UpgradeAction) Error(s string, args ...interface{}) {

func (u UpgradeAction) upgradeHook(hook string) error {
u.Info("Applying '%s' hook", hook)
return Hook(&u.cfg.Config, hook, u.cfg.Strict, u.cfg.CloudInitPaths...)
return Hook(&u.cfg.Config, hook, u.cfg.Strict, "", u.cfg.CloudInitPaths...)
}

func (u UpgradeAction) upgradeChrootHook(hook string, root string) error {
Expand Down
20 changes: 20 additions & 0 deletions pkg/features/embedded/arm-firmware/system/oem/00_armfirmware.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: "Set ARM Firmware"
stages:
after-install-chroot:
- &pifirmware
name: Raspberry PI post hook
if: '[ -d "/boot/vc" ]'
commands:
- cp -rf /boot/vc/* /run/elemental/efi/

after-upgrade-chroot:
- <<: *pifirmware

after-reset-chroot:
- <<: *pifirmware

after-disk:
- name: Raspberry PI post hook
if: '[ -d "./recovery.img.root/boot/vc" ]'
commands:
- cp -rf ./recovery.img.root/boot/vc/* ./efi/
5 changes: 5 additions & 0 deletions pkg/features/features.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ const (
FeatureCloudConfigEssentials = "cloud-config-essentials"
FeatureBootAssessment = "boot-assessment"
FeatureAutologin = "autologin"
FeatureArmFirmware = "arm-firmware"
)

var (
Expand All @@ -67,6 +68,7 @@ var (
FeatureCloudConfigDefaults,
FeatureCloudConfigEssentials,
FeatureBootAssessment,
FeatureArmFirmware,
}

Default = []string{
Expand All @@ -79,6 +81,7 @@ var (
FeatureCloudConfigDefaults,
FeatureCloudConfigEssentials,
FeatureBootAssessment,
FeatureArmFirmware,
}
)

Expand Down Expand Up @@ -171,6 +174,8 @@ func Get(names []string) ([]*Feature, error) {
features = append(features, New(name, units))
case FeatureAutologin:
features = append(features, New(name, nil))
case FeatureArmFirmware:
features = append(features, New(name, nil))
default:
notFound = append(notFound, name)
}
Expand Down
8 changes: 8 additions & 0 deletions pkg/utils/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,14 @@ func MkdirAll(fs types.FS, name string, mode os.FileMode) (err error) {
return os.MkdirAll(name, mode)
}

// Chdir changes the current directory supporting the given filesystem
func Chdir(fs types.FS, path string) (err error) {
if path, err = fs.RawPath(path); err != nil {
return &os.PathError{Op: "chdir", Path: path, Err: err}
}
return os.Chdir(path)
}

// readlink calls fs.Readlink but trims temporary prefix on Readlink result
func readlink(fs types.FS, name string) (string, error) {
res, err := fs.Readlink(name)
Expand Down

0 comments on commit eb83a89

Please sign in to comment.