Skip to content

Commit 2441c2d

Browse files
authored
Merge pull request #10209 from austinvazquez/cherry-pick-a782fd6da2fa9fa350ef754b68b5e80261cfeddc-to-release-1.7
[release/1.7] Use LOOP_CONFIGURE when creating loop devices
2 parents 62af107 + 803aaa6 commit 2441c2d

1 file changed

Lines changed: 27 additions & 0 deletions

File tree

mount/losetup_linux.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"strings"
2424
"time"
2525

26+
kernel "github.com/containerd/containerd/contrib/seccomp/kernelversion"
2627
"github.com/containerd/containerd/pkg/randutil"
2728
"golang.org/x/sys/unix"
2829
)
@@ -84,6 +85,32 @@ func setupLoopDev(backingFile, loopDev string, param LoopParams) (_ *os.File, re
8485
}
8586
}()
8687

88+
fiveDotEight := kernel.KernelVersion{Kernel: 5, Major: 8}
89+
if ok, err := kernel.GreaterEqualThan(fiveDotEight); err == nil && ok {
90+
config := unix.LoopConfig{
91+
Fd: uint32(back.Fd()),
92+
}
93+
94+
copy(config.Info.File_name[:], backingFile)
95+
if param.Readonly {
96+
config.Info.Flags |= unix.LO_FLAGS_READ_ONLY
97+
}
98+
99+
if param.Autoclear {
100+
config.Info.Flags |= unix.LO_FLAGS_AUTOCLEAR
101+
}
102+
103+
if param.Direct {
104+
config.Info.Flags |= unix.LO_FLAGS_DIRECT_IO
105+
}
106+
107+
if err := unix.IoctlLoopConfigure(int(loop.Fd()), &config); err != nil {
108+
return nil, fmt.Errorf("failed to configure loop device: %s: %w", loopDev, err)
109+
}
110+
111+
return loop, nil
112+
}
113+
87114
// 2. Set FD
88115
if err := unix.IoctlSetInt(int(loop.Fd()), unix.LOOP_SET_FD, int(back.Fd())); err != nil {
89116
return nil, fmt.Errorf("could not set loop fd for device: %s: %w", loopDev, err)

0 commit comments

Comments
 (0)