Skip to content

Commit

Permalink
Few small changes to facilitate testing
Browse files Browse the repository at this point in the history
Signed-off-by: David Cassany <dcassany@suse.com>
  • Loading branch information
davidcassany committed Oct 31, 2024
1 parent 4029d6c commit 90fd77c
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 50 deletions.
43 changes: 21 additions & 22 deletions pkg/snapshotter/btrfs-backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ func (d *Date) UnmarshalXML(dec *xml.Decoder, start xml.StartElement) error {

type btrfsBackend struct {
cfg *types.Config
stat backendStat
currentID int
activeID int
maxSnapshots int
}

Expand All @@ -119,15 +120,11 @@ func newBtrfsBackend(cfg *types.Config, maxSnapshots int) *btrfsBackend {
// Probe tests the given device and returns the found state as a backendStat struct
func (b *btrfsBackend) Probe(device string, mountpoint string) (backendStat, error) {
var rootVolume, snapshotsVolume bool

// Do not compute it again if it was already done in advance
if b.stat.rootDir != "" && b.stat.stateMount != "" {
return b.stat, nil
}
var stat backendStat

volumes, err := b.getSubvolumes(mountpoint)
if err != nil {
return b.stat, err
return stat, err
}

b.cfg.Logger.Debugf(
Expand All @@ -145,10 +142,10 @@ func (b *btrfsBackend) Probe(device string, mountpoint string) (backendStat, err
if rootVolume && snapshotsVolume {
id, err := b.getActiveSnapshot(mountpoint)
if err != nil {
return b.stat, err
return stat, err
}
if id > 0 {
b.stat.activeID = id
b.activeID = id
}
}

Expand All @@ -157,17 +154,19 @@ func (b *btrfsBackend) Probe(device string, mountpoint string) (backendStat, err
if elemental.IsPassiveMode(*b.cfg) || elemental.IsActiveMode(*b.cfg) {
rootDir, stateMount, currentID, err := b.findStateMount(device)
if err != nil {
return b.stat, err
return stat, err
}
b.stat.rootDir = rootDir
b.stat.stateMount = stateMount
b.stat.currentID = currentID
return b.stat, nil
stat.RootDir = rootDir
stat.StateMount = stateMount
stat.CurrentID, b.currentID = currentID, currentID
stat.ActiveID = b.activeID
return stat, nil
}

b.stat.rootDir = mountpoint
b.stat.stateMount = mountpoint
return b.stat, nil
stat.RootDir = mountpoint
stat.StateMount = mountpoint
stat.ActiveID = b.activeID
return stat, nil
}

// InitBrfsPartition is the method required to create snapshots structure on just formated partition
Expand Down Expand Up @@ -305,8 +304,8 @@ func (b btrfsBackend) ListSnapshots(rootDir string) (snapshotsList, error) {
}

snaps.IDs = subvolumesListToSnapshotsIDs(list)
snaps.activeID = activeID
b.stat.activeID = activeID
snaps.ActiveID = activeID
b.activeID = activeID
return snaps, nil
}

Expand All @@ -315,7 +314,7 @@ func (b btrfsBackend) DeleteSnapshot(rootDir string, id int) error {
if id <= 0 {
return fmt.Errorf("invalid id, should be higher than zero")
}
if id == b.stat.currentID {
if id == b.currentID {
return fmt.Errorf("invalid id, cannot delete current snapshot")
}
cmdOut, err := b.cfg.Runner.Run("btrfs", "subvolume", "delete", filepath.Join(rootDir, fmt.Sprintf(snapshotPathTmpl, id)))
Expand Down Expand Up @@ -344,7 +343,7 @@ func (b btrfsBackend) SnapshotsCleanup(rootDir string) error {
if snapsToDelete > 0 {
slices.Sort(list.IDs)
for i := range snapsToDelete {
if list.IDs[i] == b.stat.currentID {
if list.IDs[i] == b.currentID {
b.cfg.Logger.Warnf("current snapshot '%d' can't be cleaned up, stopping", list.IDs[i])
break
}
Expand Down Expand Up @@ -499,7 +498,7 @@ func (b btrfsBackend) clearInProgressMetadata(rootDir string, id int) error {

// computeNewID defines the next available snapshot ID
func (b btrfsBackend) computeNewID(rootDir string) (int, error) {
if b.stat.activeID == 0 {
if b.activeID == 0 {
// If there is no active snapshot we assume this will be the first one
return 1, nil
}
Expand Down
38 changes: 20 additions & 18 deletions pkg/snapshotter/btrfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,14 @@ type subvolumeBackend interface {

type snapshotsList struct {
IDs []int
activeID int
ActiveID int
}

type backendStat struct {
activeID int
currentID int
rootDir string
stateMount string
ActiveID int
CurrentID int
RootDir string
StateMount string
}

type Btrfs struct {
Expand Down Expand Up @@ -105,21 +105,23 @@ func newBtrfsSnapshotter(cfg types.Config, snapCfg types.SnapshotterConfig, boot
return nil, fmt.Errorf("%s", msg)
}
}
var backend subvolumeBackend
if btrfsCfg.Snapper {
backend = newSnapperBackend(&cfg, snapCfg.MaxSnaps)
} else {
backend = newBtrfsBackend(&cfg, snapCfg.MaxSnaps)
}
return &Btrfs{
cfg: cfg, snapshotterCfg: snapCfg,
btrfsCfg: *btrfsCfg, bootloader: bootloader,
snapshotsUmount: func() error { return nil },
snapshotsMount: func() error { return nil },
backend: backend,
backend: NewSubvolumeBackend(&cfg, *btrfsCfg, snapCfg.MaxSnaps),
}, nil
}

// NewSubvolumeBackend returns an instance of a subvolume backend
func NewSubvolumeBackend(cfg *types.Config, bCfg types.BtrfsConfig, maxSnaps int) subvolumeBackend {
if bCfg.Snapper {
return newSnapperBackend(cfg, maxSnaps)
}
return newBtrfsBackend(cfg, maxSnaps)
}

// InitSnapshotter initiates the snapshotter to the given root directory. This method includes the logic to create
// required subvolmes to handle snapshots as snapper does.
func (b *Btrfs) InitSnapshotter(state *types.Partition, efiDir string) error {
Expand Down Expand Up @@ -314,7 +316,7 @@ func (b *Btrfs) GetSnapshots() (snapshots []int, err error) {
if err != nil {
return nil, err
}
b.activeSnapshotID = snapList.activeID
b.activeSnapshotID = snapList.ActiveID
return snapList.IDs, err
}

Expand Down Expand Up @@ -352,10 +354,10 @@ func (b *Btrfs) isInitiated(state *types.Partition) (bool, error) {
if err != nil {
return false, err
}
b.activeSnapshotID = bStat.activeID
b.rootDir = bStat.rootDir
state.MountPoint = bStat.stateMount
return bStat.activeID > 0, nil
b.activeSnapshotID = bStat.ActiveID
b.rootDir = bStat.RootDir
state.MountPoint = bStat.StateMount
return bStat.ActiveID > 0, nil
}

// getPassiveSnapshots returns a list of the available snapshots
Expand Down Expand Up @@ -437,7 +439,7 @@ func (b *Btrfs) remountStatePartition(state *types.Partition) error {
return err
}

// mountSnapshotsSubvolumeInSnapshot mounts the snapshots subvolume inside the active snapshot tree
// mountSnapshotsSubvolumeInSnapshot mounts the snapshots subvolume inside the given snapshot tree
func (b *Btrfs) mountSnapshotsSubvolumeInSnapshot(root, device string, snapshotID int) error {
var mountpoint, subvol string

Expand Down
21 changes: 11 additions & 10 deletions pkg/snapshotter/snapper-backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,15 @@ var _ subvolumeBackend = (*snapperBackend)(nil)

type snapperBackend struct {
cfg *types.Config
stat backendStat
currentID int
activeID int
btrfs *btrfsBackend
maxSnapshots int
}

// newSnapperBackend creates a new instance for of the snapper backend
func newSnapperBackend(cfg *types.Config, maxSnapshots int) *snapperBackend {
// snapper backend embeds an instance of a btrfs backend to handle fill the gap for the
// snapper backend embeds an instance of a btrfs backend to fill the gap for the
// operatons that snapper can't entirely handle.
return &snapperBackend{cfg: cfg, maxSnapshots: maxSnapshots, btrfs: newBtrfsBackend(cfg, maxSnapshots)}
}
Expand All @@ -54,9 +55,9 @@ func newSnapperBackend(cfg *types.Config, maxSnapshots int) *snapperBackend {
func (s *snapperBackend) Probe(device, mountpoint string) (backendStat, error) {
stat, err := s.btrfs.Probe(device, mountpoint)
if err != nil {
return s.stat, err
return stat, err
}
s.stat = stat
s.activeID, s.currentID = stat.ActiveID, stat.CurrentID
return stat, nil
}

Expand Down Expand Up @@ -116,7 +117,7 @@ func (s snapperBackend) CommitSnapshot(rootDir string, snapshot *types.Snapshot)
return err
}

if s.stat.activeID == 0 && s.stat.currentID == 0 {
if s.activeID == 0 && s.currentID == 0 {
// Snapper does not support modifying a snapshot from a host not having a configured snapper
// and this is the case for the installation media
return s.btrfs.CommitSnapshot(rootDir, snapshot)
Expand Down Expand Up @@ -162,18 +163,18 @@ func (s snapperBackend) ListSnapshots(rootDir string) (snapshotsList, error) {
}
ids = append(ids, id)
if match[2] == "yes" {
sl.activeID = id
sl.ActiveID = id
}
}
}
sl.IDs = ids
s.stat.activeID = sl.activeID
s.activeID = sl.ActiveID
return sl, nil
}

// DeleteSnapshot deletes the given snapshot
func (s snapperBackend) DeleteSnapshot(rootDir string, id int) error {
if s.stat.activeID == 0 && s.stat.currentID == 0 {
if s.activeID == 0 && s.currentID == 0 {
// With snapper is not possible to delete any snapshot without an active one
return s.btrfs.DeleteSnapshot(rootDir, id)
}
Expand Down Expand Up @@ -202,8 +203,8 @@ func (s snapperBackend) SnapshotsCleanup(rootDir string) error {
// over the actual "/" root
func (s snapperBackend) rootArgs(rootDir string) []string {
args := []string{}
if rootDir != "/" && s.stat.currentID == 0 {
args = []string{"--no-dbus", "--root", filepath.Join(rootDir, fmt.Sprintf(snapshotPathTmpl, s.stat.activeID))}
if rootDir != "/" && s.currentID == 0 && s.activeID > 0 {
args = []string{"--no-dbus", "--root", filepath.Join(rootDir, fmt.Sprintf(snapshotPathTmpl, s.activeID))}
} else if rootDir != "/" {
args = []string{"--no-dbus", "--root", rootDir}
}
Expand Down

0 comments on commit 90fd77c

Please sign in to comment.