Skip to content

Commit f29ac26

Browse files
frelondavidcassany
authored andcommitted
Avoid panic when MaxSnaps is set to 0
When the MaxSnaps configuration option is set to 0 the following panic occurs during installation: ``` [PANICKED] Test Panicked In [It] at: /usr/lib64/go/1.24/src/runtime/panic.go:115 @ 07/01/25 10:04:57.47 runtime error: index out of range [0] with length 0 Full Stack Trace github.com/rancher/elemental-toolkit/v2/pkg/snapshotter.(*LoopDevice).cleanOldSnapshots(0xc00025a300) /home/frelon/src/elemental-toolkit/pkg/snapshotter/loopdevice.go:468 +0x3e7 github.com/rancher/elemental-toolkit/v2/pkg/snapshotter.(*LoopDevice).CloseTransaction(0xc00025a300, 0xc000234a00) /home/frelon/src/elemental-toolkit/pkg/snapshotter/loopdevice.go:271 +0x11d5 github.com/rancher/elemental-toolkit/v2/pkg/snapshotter_test.init.func3.12() /home/frelon/src/elemental-toolkit/pkg/snapshotter/loopdevice_test.go:204 +0x38e ``` This commit checks that we don't try to delete old snapshots in case there are none. Signed-off-by: Fredrik Lönnegren <[email protected]> (cherry picked from commit d8450e0)
1 parent 46dcd19 commit f29ac26

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

pkg/snapshotter/loopdevice.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,7 @@ func (l *LoopDevice) cleanOldSnapshots() error {
464464
}
465465

466466
sort.Ints(ids)
467-
for len(ids) > l.snapshotterCfg.MaxSnaps-1 {
467+
for len(ids) > l.snapshotterCfg.MaxSnaps-1 && len(ids) > 0 {
468468
err = l.DeleteSnapshot(ids[0])
469469
if err != nil {
470470
l.cfg.Logger.Warnf("could not delete snapshot %d", ids[0])

pkg/snapshotter/loopdevice_test.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,15 @@ import (
2222

2323
. "github.com/onsi/ginkgo/v2"
2424
. "github.com/onsi/gomega"
25+
"github.com/twpayne/go-vfs/v4"
26+
"github.com/twpayne/go-vfs/v4/vfst"
27+
2528
conf "github.com/rancher/elemental-toolkit/v2/pkg/config"
2629
"github.com/rancher/elemental-toolkit/v2/pkg/constants"
2730
"github.com/rancher/elemental-toolkit/v2/pkg/mocks"
2831
"github.com/rancher/elemental-toolkit/v2/pkg/snapshotter"
2932
"github.com/rancher/elemental-toolkit/v2/pkg/types"
3033
"github.com/rancher/elemental-toolkit/v2/pkg/utils"
31-
"github.com/twpayne/go-vfs/v4"
32-
"github.com/twpayne/go-vfs/v4/vfst"
3334
)
3435

3536
var _ = Describe("LoopDevice", Label("snapshotter", "loopdevice"), func() {
@@ -192,6 +193,17 @@ var _ = Describe("LoopDevice", Label("snapshotter", "loopdevice"), func() {
192193
Expect(lp.GetSnapshots()).Error().To(HaveOccurred())
193194
})
194195

196+
It("closes a transaction with 0 MaxSnaps", func() {
197+
snapCfg.MaxSnaps = 0
198+
lp, err := snapshotter.NewSnapshotter(cfg, snapCfg, bootloader)
199+
Expect(err).NotTo(HaveOccurred())
200+
Expect(lp.InitSnapshotter(statePart, efiDir)).To(Succeed())
201+
202+
snap, err := lp.StartTransaction()
203+
Expect(err).ToNot(HaveOccurred())
204+
Expect(lp.CloseTransaction(snap)).Error().ToNot(HaveOccurred())
205+
})
206+
195207
Describe("using loopdevice on sixth snapshot", func() {
196208
var err error
197209
var lp types.Snapshotter

0 commit comments

Comments
 (0)