Skip to content

Commit

Permalink
Merge pull request containerd#10209 from austinvazquez/cherry-pick-a7…
Browse files Browse the repository at this point in the history
…82fd6da2fa9fa350ef754b68b5e80261cfeddc-to-release-1.7

[release/1.7] Use LOOP_CONFIGURE when creating loop devices
  • Loading branch information
dmcgowan authored May 16, 2024
2 parents 62af107 + 803aaa6 commit 2441c2d
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions mount/losetup_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"strings"
"time"

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

fiveDotEight := kernel.KernelVersion{Kernel: 5, Major: 8}
if ok, err := kernel.GreaterEqualThan(fiveDotEight); err == nil && ok {
config := unix.LoopConfig{
Fd: uint32(back.Fd()),
}

copy(config.Info.File_name[:], backingFile)
if param.Readonly {
config.Info.Flags |= unix.LO_FLAGS_READ_ONLY
}

if param.Autoclear {
config.Info.Flags |= unix.LO_FLAGS_AUTOCLEAR
}

if param.Direct {
config.Info.Flags |= unix.LO_FLAGS_DIRECT_IO
}

if err := unix.IoctlLoopConfigure(int(loop.Fd()), &config); err != nil {
return nil, fmt.Errorf("failed to configure loop device: %s: %w", loopDev, err)
}

return loop, nil
}

// 2. Set FD
if err := unix.IoctlSetInt(int(loop.Fd()), unix.LOOP_SET_FD, int(back.Fd())); err != nil {
return nil, fmt.Errorf("could not set loop fd for device: %s: %w", loopDev, err)
Expand Down

0 comments on commit 2441c2d

Please sign in to comment.