Skip to content

Commit

Permalink
Be more smart about upgrade dir (#104)
Browse files Browse the repository at this point in the history
  • Loading branch information
Itxaka authored Feb 14, 2022
1 parent 7d40dd0 commit 8aeb051
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 12 deletions.
3 changes: 2 additions & 1 deletion pkg/action/action_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/rancher-sandbox/elemental/pkg/action"
"github.com/rancher-sandbox/elemental/pkg/constants"
"github.com/rancher-sandbox/elemental/pkg/types/v1"
"github.com/rancher-sandbox/elemental/pkg/utils"
v1mock "github.com/rancher-sandbox/elemental/tests/mocks"
"github.com/sirupsen/logrus"
"github.com/spf13/afero"
Expand Down Expand Up @@ -562,7 +563,7 @@ var _ = Describe("Actions", func() {
config.RecoveryImage = "system/cos-config"
config.ImgSize = 10
// Create fake /etc/os-release
_ = afero.WriteFile(fs, filepath.Join(constants.UpgradeTempDir, "etc", "os-release"), []byte("GRUB_ENTRY_NAME=TESTOS"), os.ModePerm)
_ = afero.WriteFile(fs, filepath.Join(utils.GetUpgradeTempDir(config), "etc", "os-release"), []byte("GRUB_ENTRY_NAME=TESTOS"), os.ModePerm)
})
It("Fails if some hook fails and strict is set", func() {
runner = v1mock.NewTestRunnerV2()
Expand Down
19 changes: 10 additions & 9 deletions pkg/action/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ func (u *UpgradeAction) Run() (err error) {
upgradeStateDir := constants.RunningStateDir
// When booting from recovery the label can be the recovery or the system, depending on the recovery img type (squshs/non-squash)
bootedFromRecovery := utils.BootedFrom(u.Config.Runner, u.Config.RecoveryLabel) || utils.BootedFrom(u.Config.Runner, u.Config.SystemLabel)
upgradeTempDir := utils.GetUpgradeTempDir(u.Config)

cleanup := utils.NewCleanStack()
defer func() { err = cleanup.Cleanup(err) }()
Expand All @@ -87,12 +88,12 @@ func (u *UpgradeAction) Run() (err error) {

u.Config.Logger.Infof("Upgrading %s partition", upgradeTarget)

err = u.Config.Fs.MkdirAll(constants.UpgradeTempDir, os.ModeDir)
err = u.Config.Fs.MkdirAll(upgradeTempDir, os.ModeDir)
if err != nil {
u.Error("Error creating target dir %s: %s", constants.UpgradeTempDir, err)
u.Error("Error creating target dir %s: %s", upgradeTempDir, err)
return err
}
cleanup.Push(func() error { return u.remove(constants.UpgradeTempDir) })
cleanup.Push(func() error { return u.remove(upgradeTempDir) })

if u.Config.RecoveryUpgrade {
statePart, err = utils.GetFullDeviceByLabel(u.Config.Runner, u.Config.RecoveryLabel, 5)
Expand Down Expand Up @@ -189,7 +190,7 @@ func (u *UpgradeAction) Run() (err error) {
Size: u.Config.ImgSize,
Label: u.Config.ActiveLabel,
FS: constants.LinuxImgFs,
MountPoint: constants.UpgradeTempDir,
MountPoint: upgradeTempDir,
Source: upgradeSource, // if source is a dir it will copy from here, if it's a docker img it uses Config.DockerImg IN THAT ORDER!
}

Expand All @@ -213,7 +214,7 @@ func (u *UpgradeAction) Run() (err error) {
}

for _, d := range []string{"proc", "boot", "dev", "sys", "tmp", "usr/local", "oem"} {
_ = u.Config.Fs.MkdirAll(filepath.Join(constants.UpgradeTempDir, d), os.ModeDir)
_ = u.Config.Fs.MkdirAll(filepath.Join(upgradeTempDir, d), os.ModeDir)
}

err = upgradeHook(u.Config, "before-upgrade", false)
Expand All @@ -230,8 +231,8 @@ func (u *UpgradeAction) Run() (err error) {
}
// Selinux relabel
// In the original script, any errors are ignored
_, _ = u.Config.Runner.Run("chmod", "755", constants.UpgradeTempDir)
_ = ele.SelinuxRelabel(constants.UpgradeTempDir, false)
_, _ = u.Config.Runner.Run("chmod", "755", upgradeTempDir)
_ = ele.SelinuxRelabel(upgradeTempDir, false)

// Only run rebrand on non recovery+squash
err = upgradeHook(u.Config, "after-upgrade-chroot", true)
Expand All @@ -241,7 +242,7 @@ func (u *UpgradeAction) Run() (err error) {
}

// Load the os-release file from the new upgraded system
osRelease, err := utils.LoadEnvFile(u.Config.Fs, filepath.Join(constants.UpgradeTempDir, "etc", "os-release"))
osRelease, err := utils.LoadEnvFile(u.Config.Fs, filepath.Join(upgradeTempDir, "etc", "os-release"))
// override grub vars with the new system vars
u.Config.GrubDefEntry = osRelease["GRUB_ENTRY_NAME"]

Expand Down Expand Up @@ -296,7 +297,7 @@ func (u *UpgradeAction) Run() (err error) {
finalDestination = filepath.Join(upgradeStateDir, "cOS", constants.RecoverySquashFile)
options := constants.GetDefaultSquashfsOptions()
u.Info("Creating %s", constants.RecoverySquashFile)
err = utils.CreateSquashFS(u.Config.Runner, u.Config.Logger, constants.UpgradeTempDir, transitionImg, options)
err = utils.CreateSquashFS(u.Config.Runner, u.Config.Logger, upgradeTempDir, transitionImg, options)
if err != nil {
return err
}
Expand Down
5 changes: 3 additions & 2 deletions pkg/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ limitations under the License.

package constants

import "runtime"
import (
"runtime"
)

const (
GrubConf = "/etc/cos/grub.cfg"
Expand Down Expand Up @@ -74,7 +76,6 @@ const (
UpgradeActive = "active"
UpgradeRecovery = "recovery"
UpgradeSource = "system/cos"
UpgradeTempDir = "/tmp/upgrade"
UpgradeRecoveryDir = "/run/initramfs/live"
TransitionImgFile = "transition.img"
TransitionSquashFile = "transition.squashfs"
Expand Down
18 changes: 18 additions & 0 deletions pkg/utils/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"io"
"os"
"os/exec"
"path/filepath"
"strings"
"time"
)
Expand Down Expand Up @@ -228,3 +229,20 @@ func LoadEnvFile(fs afero.Fs, file string) (map[string]string, error) {

return envMap, err
}

// GetUpgradeTempDir returns the dir for storing upgrade related temporal files
// It will respect TMPDIR and use that if exists, fallback to try the persistent partition if its mounted
// and finally the default /tmp/ dir
func GetUpgradeTempDir(config *v1.RunConfig) string {
// if we got a TMPDIR var, respect and use that
dir := os.Getenv("TMPDIR")
if dir != "" {
return filepath.Join(dir, "elemental-upgrade")
}
// Check persistent and if its mounted
persistent, err := GetFullDeviceByLabel(config.Runner, config.PersistentLabel, 5)
if err == nil && persistent.MountPoint != "" {
return filepath.Join(persistent.MountPoint, "elemental-upgrade")
}
return filepath.Join("/", "tmp", "elemental-upgrade")
}

0 comments on commit 8aeb051

Please sign in to comment.