Skip to content

Commit

Permalink
Merge pull request #9211 from UiPath/use-loop-configure
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelkarp committed Oct 17, 2023
2 parents aef2ebc + a782fd6 commit 423c7ad
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions mount/losetup_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,11 @@ import (
"fmt"
"os"
"strings"
"syscall"
"time"
"unsafe"

kernel "github.com/containerd/containerd/contrib/seccomp/kernelversion"
"github.com/containerd/containerd/pkg/randutil"
"golang.org/x/sys/unix"
)
Expand All @@ -32,8 +35,26 @@ const (
loopDevFormat = "/dev/loop%d"

ebusyString = "device or resource busy"

loopConfigureIoctl = 0x4c0a
)

type LoopConfig struct {
Fd uint32
BlockSize uint32
Info unix.LoopInfo64
Reserved [8]uint64
}

func ioctlConfigure(fd int, value *LoopConfig) error {
_, _, err := syscall.Syscall(syscall.SYS_IOCTL, uintptr(fd), uintptr(loopConfigureIoctl), uintptr(unsafe.Pointer(value)))
if err == 0 {
return nil
}

return err
}

// LoopParams parameters to control loop device setup
type LoopParams struct {
// Loop device should forbid write
Expand Down Expand Up @@ -84,6 +105,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 := 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 := ioctlConfigure(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 423c7ad

Please sign in to comment.