Skip to content

Commit e2121b8

Browse files
committed
tarfs: replace go-losetup with the version from containerd
Replace go-losetup with the version from containerd, to support auto-clean unused loop devices. Signed-off-by: Jiang Liu <[email protected]>
1 parent 751bc86 commit e2121b8

File tree

2 files changed

+21
-11
lines changed

2 files changed

+21
-11
lines changed

pkg/tarfs/losetup_linux.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@
2020
package tarfs
2121

2222
import (
23-
"errors"
2423
"fmt"
2524
"math/rand"
2625
"os"
2726
"strings"
2827
"time"
2928

29+
"github.com/pkg/errors"
3030
"golang.org/x/sys/unix"
3131
)
3232

@@ -166,6 +166,14 @@ func setupLoop(backingFile string, param LoopParams) (*os.File, error) {
166166
return nil, errors.New("timeout creating new loopback device")
167167
}
168168

169+
func deleteLoop(file *os.File) error {
170+
err := unix.IoctlSetInt(int(file.Fd()), unix.LOOP_CLR_FD, 0)
171+
if err != nil {
172+
return errors.Wrapf(err, "delete loopdev %s", file.Name())
173+
}
174+
return file.Close()
175+
}
176+
169177
func removeLoop(loopdev string) error {
170178
file, err := os.Open(loopdev)
171179
if err != nil {

pkg/tarfs/tarfs.go

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ import (
3232
"github.com/containerd/nydus-snapshotter/pkg/rafs"
3333
"github.com/containerd/nydus-snapshotter/pkg/remote"
3434
"github.com/containerd/nydus-snapshotter/pkg/remote/remotes"
35-
losetup "github.com/freddierice/go-losetup"
3635
"github.com/moby/sys/mountinfo"
3736
"github.com/opencontainers/go-digest"
3837
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
@@ -78,8 +77,8 @@ type snapshotStatus struct {
7877
status int
7978
blobID string
8079
erofsMountPoint string
81-
dataLoopdev *losetup.Device
82-
metaLoopdev *losetup.Device
80+
dataLoopdev *os.File
81+
metaLoopdev *os.File
8382
wg *sync.WaitGroup
8483
cancel context.CancelFunc
8584
ctx context.Context
@@ -664,7 +663,7 @@ func (t *Manager) MountTarErofs(snapshotID string, s *storage.Snapshot, labels m
664663
}
665664
st.dataLoopdev = loopdev
666665
}
667-
devices = append(devices, "device="+st.dataLoopdev.Path())
666+
devices = append(devices, "device="+st.dataLoopdev.Name())
668667
}
669668

670669
st.mutex.Unlock()
@@ -693,7 +692,7 @@ func (t *Manager) MountTarErofs(snapshotID string, s *storage.Snapshot, labels m
693692
}
694693
st.metaLoopdev = loopdev
695694
}
696-
devName := st.metaLoopdev.Path()
695+
devName := st.metaLoopdev.Name()
697696

698697
if err = os.MkdirAll(mountPoint, 0750); err != nil {
699698
return errors.Wrapf(err, "create tarfs mount dir %s", mountPoint)
@@ -742,7 +741,7 @@ func (t *Manager) DetachLayer(snapshotID string) error {
742741
}
743742

744743
if st.metaLoopdev != nil {
745-
err := st.metaLoopdev.Detach()
744+
err := deleteLoop(st.metaLoopdev)
746745
if err != nil {
747746
st.mutex.Unlock()
748747
return errors.Wrapf(err, "detach merged bootstrap loopdev for tarfs snapshot %s", snapshotID)
@@ -751,7 +750,7 @@ func (t *Manager) DetachLayer(snapshotID string) error {
751750
}
752751

753752
if st.dataLoopdev != nil {
754-
err := st.dataLoopdev.Detach()
753+
err := deleteLoop(st.dataLoopdev)
755754
if err != nil {
756755
st.mutex.Unlock()
757756
return errors.Wrapf(err, "detach layer bootstrap loopdev for tarfs snapshot %s", snapshotID)
@@ -863,12 +862,15 @@ func (t *Manager) waitLayerReady(snapshotID string, lock bool) (*snapshotStatus,
863862
return st, nil
864863
}
865864

866-
func (t *Manager) attachLoopdev(blob string) (*losetup.Device, error) {
865+
func (t *Manager) attachLoopdev(blob string) (*os.File, error) {
867866
// losetup.Attach() is not thread-safe hold lock here
868867
t.mutexLoopDev.Lock()
869868
defer t.mutexLoopDev.Unlock()
870-
dev, err := losetup.Attach(blob, 0, false)
871-
return &dev, err
869+
param := LoopParams{
870+
Readonly: true,
871+
Autoclear: true,
872+
}
873+
return setupLoop(blob, param)
872874
}
873875

874876
func (t *Manager) CheckTarfsHintAnnotation(ctx context.Context, ref string, manifestDigest digest.Digest) (bool, error) {

0 commit comments

Comments
 (0)