-
Notifications
You must be signed in to change notification settings - Fork 10
/
info.go
71 lines (60 loc) · 1.7 KB
/
info.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package losetup
import (
"fmt"
"unsafe"
"golang.org/x/sys/unix"
)
// Info is a datastructure that holds relevant information about a file backed
// loopback device.
type Info struct {
Device uint64
INode uint64
RDevice uint64
Offset uint64
SizeLimit uint64
Number uint32
EncryptType uint32
EncryptKeySize uint32
Flags uint32
FileName [NameSize]byte
CryptName [NameSize]byte
EncryptKey [KeySize]byte
Init [2]uint64
}
// GetInfo returns information about a loop device
func (device Device) GetInfo() (Info, error) {
f, err := device.open()
if err != nil {
return Info{}, fmt.Errorf("could not open %v: %v", device, err)
}
defer f.Close()
return getInfo(f.Fd())
}
func getInfo(fd uintptr) (Info, error) {
retInfo := Info{}
_, _, errno := unix.Syscall(unix.SYS_IOCTL, fd, GetStatus64, uintptr(unsafe.Pointer(&retInfo)))
if errno == unix.ENXIO {
return Info{}, fmt.Errorf("device not backed by a file")
} else if errno != 0 {
return Info{}, fmt.Errorf("could not get info about %v (err: %d): %v", fd, errno, errno)
}
return retInfo, nil
}
// SetInfo sets options in the loop device.
func (device Device) SetInfo(info Info) error {
f, err := device.open()
if err != nil {
return fmt.Errorf("could not open %v: %v", device, err)
}
defer f.Close()
return setInfo(f.Fd(), info)
}
func setInfo(fd uintptr, info Info) error {
_, _, errno := unix.Syscall(unix.SYS_IOCTL, fd, SetStatus64, uintptr(unsafe.Pointer(&info)))
if errno == unix.ENXIO {
return fmt.Errorf("device not backed by a file")
} else if errno != 0 {
return fmt.Errorf("could not get info about %v (err: %d): %v", fd, errno, errno)
}
return nil
}