github.com/hpcng/singularity@v3.1.1+incompatible/pkg/ocibundle/tools/loop.go (about) 1 // Copyright (c) 2019, Sylabs Inc. All rights reserved. 2 // This software is licensed under a 3-clause BSD license. Please consult the 3 // LICENSE.md file distributed with the sources of this project regarding your 4 // rights to use or distribute this software. 5 6 package tools 7 8 import ( 9 "fmt" 10 "os" 11 12 "github.com/sylabs/singularity/pkg/util/loop" 13 ) 14 15 // CreateLoop associates a file to loop device and returns 16 // path of loop device used 17 func CreateLoop(file *os.File, offset, size uint64) (string, error) { 18 loopDev := &loop.Device{ 19 MaxLoopDevices: 256, 20 Shared: true, 21 Info: &loop.Info64{ 22 SizeLimit: size, 23 Offset: offset, 24 Flags: loop.FlagsAutoClear, 25 }, 26 } 27 idx := 0 28 if err := loopDev.AttachFromFile(file, os.O_RDONLY, &idx); err != nil { 29 return "", fmt.Errorf("failed to attach image %s: %s", file.Name(), err) 30 } 31 return fmt.Sprintf("/dev/loop%d", idx), nil 32 }