-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpv_header.go
164 lines (138 loc) · 4.45 KB
/
pv_header.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package lvm
import (
"bytes"
"encoding/binary"
"fmt"
"io"
"log"
"github.com/Soulou/go-lvm/crc32"
"github.com/pkg/errors"
)
const (
LabelID = "LABELONE"
// Label can be in any of the first 4 sectors
LabelScanSectors = 4
LabelScanSize = LabelScanSectors * SectorSize
LabelHeaderSize = 32
PhysicalVolumeIDLength = 32
)
type LabelHeader struct {
ID [8]byte /* LABELONE */
Sector uint64 /* Sector number of this label */
CRC uint32 /* From next field to end of sector */
Offset uint32 /* Offset from start of struct to contents */
Typename [8]byte /* LVM2 001 */
}
func (h LabelHeader) String() string {
return fmt.Sprintf(
"LabelHeader[ID:%s Sector:%d CRC:%d Offset:%d Typename:%s]",
string(h.ID[:]), h.Sector, h.CRC, h.Offset, string(h.Typename[:]),
)
}
func (h LabelHeader) CheckCRC32(sector []byte) (uint32, bool) {
crc32checksum := crc32.Calc(InitialCRC, sector[20:SectorSize])
return crc32checksum, crc32checksum == h.CRC
}
type StaticPhysicalVolumeHeader struct {
UUID [PhysicalVolumeIDLength]byte
/* This size can be overridden if PV belongs to a VG */
DeviceSize uint64 /* Bytes */
}
type PhysicalVolumeHeader struct {
StaticPhysicalVolumeHeader
/* NULL-terminated list of data areas followed by */
/* NULL-terminated list of metadata area headers */
DiskAreas []DataAreaDescriptor
MetadataAreas []DataAreaDescriptor
}
func (h PhysicalVolumeHeader) UUIDToString() string {
buf := bytes.NewBuffer(make([]byte, 0, 38))
buf.Write(h.UUID[0:6])
buf.Write([]byte{'-'})
buf.Write(h.UUID[6:10])
buf.Write([]byte{'-'})
buf.Write(h.UUID[14:18])
buf.Write([]byte{'-'})
buf.Write(h.UUID[18:22])
buf.Write([]byte{'-'})
buf.Write(h.UUID[22:26])
buf.Write([]byte{'-'})
buf.Write(h.UUID[26:32])
return buf.String()
}
func (h PhysicalVolumeHeader) String() string {
return fmt.Sprintf(
"PVHeader[ID:%s Size:%d DiskAreas:%d MetadataAreas:%d]",
h.UUIDToString(), h.DeviceSize, len(h.DiskAreas), len(h.MetadataAreas),
)
}
type StaticPhysicalVolumeHeaderExtension struct {
Version uint32
Flags uint32
}
type PhysicalVolumeHeaderExtension struct {
StaticPhysicalVolumeHeaderExtension
/* NULL-terminated list of bootloader areas */
BooloaderAreas []DataAreaDescriptor
}
func (ext PhysicalVolumeHeaderExtension) String() string {
return fmt.Sprintf("Ext[Version:%d Flags:%d, BootloaderAreas:%d]", ext.Version, ext.Flags, len(ext.BooloaderAreas))
}
func (pv *PhysicalVolume) readHeaderExt(reader io.Reader) (PhysicalVolumeHeaderExtension, error) {
var ext PhysicalVolumeHeaderExtension
err := binary.Read(reader, binary.LittleEndian, &(ext.StaticPhysicalVolumeHeaderExtension))
if err != nil {
return ext, errors.Wrapf(err, "fail to parse pv header extension")
}
areas, err := readDataAreaDescriptorList(reader)
if err != nil {
return ext, errors.Wrapf(err, "fail to parse data area descriptors for bootloader areas")
}
ext.BooloaderAreas = areas
return ext, nil
}
func (pv *PhysicalVolume) readHeader(reader io.Reader) (PhysicalVolumeHeader, error) {
var header PhysicalVolumeHeader
err := binary.Read(reader, binary.LittleEndian, &(header.StaticPhysicalVolumeHeader))
if err != nil {
return header, errors.Wrapf(err, "fail to parse pv header")
}
areas, err := readDataAreaDescriptorList(reader)
if err != nil {
return header, errors.Wrapf(err, "fail to parse data area descriptors for disk areas")
}
header.DiskAreas = areas
areas, err = readDataAreaDescriptorList(reader)
if err != nil {
return header, errors.Wrapf(err, "fail to parse data area descriptors for metadata areas")
}
header.MetadataAreas = areas
return header, nil
}
// It is expected to get the first block (block n°0) of the device
func (pv *PhysicalVolume) readLabelHeader(reader io.Reader) (LabelHeader, error) {
var header LabelHeader
for sector := 0; sector < LabelScanSectors; sector++ {
err := binary.Read(reader, binary.LittleEndian, &header)
if err != nil {
log.Println("error reading error: ", err, "continuing..")
continue
}
if string(header.ID[:]) == LabelID {
if header.Sector != uint64(sector) {
return header, errors.Errorf("header sector does not match: (%v, expected: %v)", header.Sector, sector)
}
break
} else {
toNextSector := make([]byte, SectorSize-LabelHeaderSize)
_, err := reader.Read(toNextSector)
if err != nil {
return header, errors.Wrapf(err, "fail to read to next sector")
}
}
}
if header.Sector == 0 {
return header, errors.New("label header not found")
}
return header, nil
}