Skip to content

Commit

Permalink
Remove unused passive symlinks for loopdevice
Browse files Browse the repository at this point in the history
Current loopdevice snapshotter does not require anymore
having symlinks to list passive snapshots. Current grub2
configuration configures the passive snapshot path based on
the snapshot ID.

Signed-off-by: David Cassany <dcassany@suse.com>
  • Loading branch information
davidcassany committed Feb 19, 2024
1 parent 2d01d9a commit 1733a87
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 88 deletions.
1 change: 0 additions & 1 deletion pkg/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,6 @@ const (
LoopDeviceSnapshotterType = "loopdevice"
BtrfsSnapshotterType = "btrfs"
ActiveSnapshot = "active"
PassiveSnapshot = "passive_%d"

// Legacy paths
LegacyImagesPath = "cOS"
Expand Down
7 changes: 0 additions & 7 deletions pkg/mocks/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,5 @@ func FakeLoopDeviceSnapshotsStatus(fs v1.FS, rootDir string, snapsCount int) err
if err != nil {
return err
}
for i = 1; i <= snapsCount-1; i++ {
snapshotFile = filepath.Join("..", strconv.Itoa(i), "snapshot.img")
err = fs.Symlink(snapshotFile, filepath.Join(passivesPath, fmt.Sprintf(constants.PassiveSnapshot, i)))
if err != nil {
return err
}
}
return err
}
133 changes: 53 additions & 80 deletions pkg/snapshotter/loopdevice.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ const (
loopDeviceImgName = "snapshot.img"
loopDeviceWorkDir = "snapshot.workDir"
loopDeviceLabelPattern = "EL_SNAP%d"
loopDevicePassivePath = loopDeviceSnapsPath + "/passives"
)

var _ v1.Snapshotter = (*LoopDevice)(nil)
Expand Down Expand Up @@ -218,18 +217,19 @@ func (l *LoopDevice) CloseTransactionOnError(snapshot *v1.Snapshot) error {
// CloseTransaction closes the transaction for the given snapshot. This is the responsible of setting new active and
// passive snapshots.
func (l *LoopDevice) CloseTransaction(snapshot *v1.Snapshot) (err error) {
var linkDst, newPassive, activeSnap string
var linkDst, activeSnap string

if !snapshot.InProgress {
l.cfg.Logger.Debugf("No transaction to close for snapshot %d workdir", snapshot.ID)
return fmt.Errorf("given snapshot is not in progress")
}
defer func() {
if err != nil {
_ = l.CloseTransactionOnError(snapshot)
}
}()

if !snapshot.InProgress {
l.cfg.Logger.Debugf("No transaction to close for snapshot %d workdir", snapshot.ID)
return fmt.Errorf("given snapshot is not in progress")
}

l.cfg.Logger.Infof("Closing transaction for snapshot %d workdir", snapshot.ID)
l.cfg.Logger.Debugf("Unmount %s", snapshot.MountPoint)
err = l.cfg.Mounter.Unmount(snapshot.MountPoint)
Expand All @@ -249,19 +249,6 @@ func (l *LoopDevice) CloseTransaction(snapshot *v1.Snapshot) (err error) {
return err
}

// Create fallback link for current active snapshot
newPassive = filepath.Join(l.rootDir, loopDevicePassivePath, fmt.Sprintf(constants.PassiveSnapshot, l.activeSnapshotID))
if l.activeSnapshotID > 0 {
linkDst = fmt.Sprintf("../%d/%s", l.activeSnapshotID, loopDeviceImgName)
l.cfg.Logger.Debugf("creating symlink %s to %s", newPassive, linkDst)
err = l.cfg.Fs.Symlink(linkDst, newPassive)
if err != nil {
l.cfg.Logger.Errorf("failed creating the new passive link: %v", err)
return err
}
l.cfg.Logger.Infof("New passive snapshot fallback from active (%d) created", l.activeSnapshotID)
}

// Remove old symlink and create a new one
activeSnap = filepath.Join(l.rootDir, loopDeviceSnapsPath, constants.ActiveSnapshot)
linkDst = fmt.Sprintf("%d/%s", snapshot.ID, loopDeviceImgName)
Expand All @@ -270,7 +257,6 @@ func (l *LoopDevice) CloseTransaction(snapshot *v1.Snapshot) (err error) {
err = l.cfg.Fs.Symlink(linkDst, activeSnap)
if err != nil {
l.cfg.Logger.Errorf("failed default snapshot image for snapshot %d: %v", snapshot.ID, err)
_ = l.cfg.Fs.Remove(newPassive)
sErr := l.cfg.Fs.Symlink(fmt.Sprintf("%d/%s", l.activeSnapshotID, loopDeviceImgName), activeSnap)
if sErr != nil {
l.cfg.Logger.Warnf("could not restore previous active link")
Expand All @@ -290,7 +276,6 @@ func (l *LoopDevice) CloseTransaction(snapshot *v1.Snapshot) (err error) {
// DeleteSnapshot deletes the snapshot of the given ID. It cannot delete an snapshot that is actually booted.
func (l *LoopDevice) DeleteSnapshot(id int) error {
var err error
var snapLink string

l.cfg.Logger.Infof("Deleting snapshot %d", id)
inUse, err := l.isSnapshotInUse(id)
Expand Down Expand Up @@ -321,15 +306,12 @@ func (l *LoopDevice) DeleteSnapshot(id int) error {
}

if l.activeSnapshotID == id {
snapLink = filepath.Join(l.rootDir, loopDeviceSnapsPath, constants.ActiveSnapshot)
} else {
snapLink = filepath.Join(l.rootDir, loopDevicePassivePath, fmt.Sprintf(constants.PassiveSnapshot, id))
}

err = l.cfg.Fs.Remove(snapLink)
if err != nil {
l.cfg.Logger.Errorf("failed removing snapshot link %s: %v", snapLink, err)
return err
snapLink := filepath.Join(l.rootDir, loopDeviceSnapsPath, constants.ActiveSnapshot)
err = l.cfg.Fs.Remove(snapLink)
if err != nil {
l.cfg.Logger.Errorf("failed removing snapshot link %s: %v", snapLink, err)
return err
}
}

snapDir := filepath.Join(l.rootDir, loopDeviceSnapsPath, strconv.Itoa(id))
Expand All @@ -344,20 +326,30 @@ func (l *LoopDevice) DeleteSnapshot(id int) error {
func (l *LoopDevice) GetSnapshots() ([]int, error) {
var ids []int

ids, err := l.getPassiveSnapshots()
if err != nil {
return nil, err
}

id, err := l.getActiveSnapshot()
if err != nil {
return nil, err
}

if id > 0 {
return append(ids, id), nil
snapsPath := filepath.Join(l.rootDir, loopDeviceSnapsPath)
r := regexp.MustCompile(`^\d+$`)
if ok, _ := utils.Exists(l.cfg.Fs, snapsPath); ok {
dirs, err := l.cfg.Fs.ReadDir(snapsPath)
if err != nil {
l.cfg.Logger.Errorf("failed reading %s contents", snapsPath)
return ids, err
}
for _, dir := range dirs {
// Find snapshots based numeric directory names
if !r.MatchString(dir.Name()) {
continue
}
id, err := strconv.Atoi(dir.Name())
if err != nil {
continue
}
ids = append(ids, id)
}
l.cfg.Logger.Debugf("Found snapshots: %v", ids)
return ids, nil
}
return ids, nil
l.cfg.Logger.Errorf("path %s does not exist", snapsPath)
return ids, fmt.Errorf("cannot determine snapshots, initate snapshotter first")
}

// SnapshotImageToSource converts the given snapshot into an ImageSource. This is useful to deploy a system
Expand Down Expand Up @@ -518,38 +510,27 @@ func (l *LoopDevice) setBootloader() error {

// getPassiveSnapshots returns a list of available passive snapshots
func (l *LoopDevice) getPassiveSnapshots() ([]int, error) {
var ids []int
allIDs, err := l.GetSnapshots()
if err != nil {
l.cfg.Logger.Errorf("failed processing available snapshots list: %s", err.Error())
return nil, err
}

snapsPath := filepath.Join(l.rootDir, loopDevicePassivePath)
r := regexp.MustCompile(`passive_(\d+)`)
if ok, _ := utils.Exists(l.cfg.Fs, snapsPath); ok {
links, err := l.cfg.Fs.ReadDir(snapsPath)
if err != nil {
l.cfg.Logger.Errorf("failed reading %s contents", snapsPath)
return ids, err
}
for _, link := range links {
// Find snapshots based numeric directory names
if !r.MatchString(link.Name()) {
continue
}
matches := r.FindStringSubmatch(link.Name())
id, err := strconv.Atoi(matches[1])
if err != nil {
continue
}
linkPath := filepath.Join(snapsPath, link.Name())
if exists, _ := utils.Exists(l.cfg.Fs, linkPath); exists {
ids = append(ids, id)
} else {
l.cfg.Logger.Warnf("image for snapshot %d doesn't exist", id)
}
activeID, err := l.getActiveSnapshot()
if err != nil {
l.cfg.Logger.Errorf("failed processing current active snapshot: %s", err.Error())
return nil, err
}

passiveIDs := []int{}
for _, id := range allIDs {
if id == activeID {
continue
}
l.cfg.Logger.Debugf("Passive snapshots: %v", ids)
return ids, nil
passiveIDs = append(passiveIDs, id)
}
l.cfg.Logger.Errorf("path %s does not exist", snapsPath)
return ids, fmt.Errorf("cannot determine passive snapshots, initate snapshotter first")

return passiveIDs, nil
}

// legacyImageToSnapshot is method to migrate existing legacy passive.img or active.img as a new loop device snapshot
Expand All @@ -573,14 +554,6 @@ func (l *LoopDevice) legacyImageToSnapsot(image string) error {
l.cfg.Logger.Errorf("failed copying legacy image as snapshot: %v", err)
return err
}

passiveLink := fmt.Sprintf("../%d/%s", id, loopDeviceImgName)
newPassive := filepath.Join(l.rootDir, loopDevicePassivePath, fmt.Sprintf(constants.PassiveSnapshot, id))
err = l.cfg.Fs.Symlink(passiveLink, newPassive)
if err != nil {
l.cfg.Logger.Errorf("failed creating the passive link for legacy images: %v", err)
return err
}
}
return nil
}
Expand Down

0 comments on commit 1733a87

Please sign in to comment.