-
Notifications
You must be signed in to change notification settings - Fork 0
/
lv_attr.go
305 lines (268 loc) · 11.7 KB
/
lv_attr.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
/*
Copyright 2024 The lvm2go Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package lvm2go
import (
"errors"
"fmt"
"strings"
)
type VolumeType rune
var (
ErrPartialActivation = errors.New("found partial activation of physical volumes, one or more physical volumes are setup incorrectly")
ErrUnknownVolumeHealth = errors.New("unknown volume health reported, verification on the host system is required")
ErrWriteCacheError = errors.New("write cache error signifies that dm-writecache reports an error")
ErrThinPoolFailed = errors.New("thin pool encounters serious failures and hence no further I/O is permitted at all")
ErrThinPoolOutOfDataSpace = errors.New("thin pool is out of data space, no further data can be written to the thin pool without extension")
ErrThinPoolMetadataReadOnly = errors.New("metadata read only signifies that thin pool encounters certain types of failures, but it's still possible to do data reads. However, no metadata changes are allowed")
ErrThinVolumeFailed = errors.New("the underlying thin pool entered a failed state and no further I/O is permitted")
ErrRAIDRefreshNeeded = errors.New("RAID volume requires a refresh, one or more Physical Volumes have suffered a write error. This could be due to temporary failure of the Physical Volume or an indication it is failing. The device should be refreshed or replaced")
ErrRAIDMismatchesExist = errors.New("RAID volume has portions of the array that are not coherent. Inconsistencies are detected by initiating a check RAID logical volume. The scrubbing operations, \"check\" and \"repair\", can be performed on a RAID volume via the \"lvchange\" command")
ErrRAIDReshaping = errors.New("RAID volume is currently reshaping. Reshaping signifies a RAID Logical Volume is either undergoing a stripe addition/removal, a stripe size or RAID algorithm change")
ErrRAIDReshapeRemoved = errors.New("RAID volume signifies freed raid images after reshaping")
ErrRAIDWriteMostly = errors.New("RAID volume is marked as write-mostly. this signifies the devices in a RAID 1 logical volume have been marked write-mostly. This means that reading from this device will be avoided, and other devices will be preferred for reading (unless no other devices are available). This minimizes the I/O to the specified device")
ErrLogicalVolumeSuspended = errors.New("logical volume is in a suspended state, no I/O is permitted")
ErrInvalidSnapshot = errors.New("logical volume is an invalid snapshot, no I/O is permitted")
ErrSnapshotMergeFailed = errors.New("snapshot merge failed, no I/O is permitted")
ErrMappedDevicePresentWithInactiveTables = errors.New("mapped device present with inactive tables, no I/O is permitted")
ErrMappedDevicePresentWithoutTables = errors.New("mapped device present without tables, no I/O is permitted")
ErrThinPoolCheckNeeded = errors.New("a thin pool check is needed")
ErrUnknownVolumeState = errors.New("unknown volume state, verification on the host system is required")
ErrHistoricalVolumeState = errors.New("historical volume state (volume no longer exists but is kept around in logs), verification on the host system is required")
ErrLogicalVolumeUnderlyingDeviceStateUnknown = errors.New("logical volume underlying device state is unknown, verification on the host system is required")
)
const (
VolumeTypeMirrored VolumeType = 'm'
VolumeTypeMirroredNoInitialSync VolumeType = 'M'
VolumeTypeOrigin VolumeType = 'o'
VolumeTypeOriginWithMergingSnapshot VolumeType = 'O'
VolumeTypeRAID VolumeType = 'r'
VolumeTypeRAIDNoInitialSync VolumeType = 'R'
VolumeTypeSnapshot VolumeType = 's'
VolumeTypeMergingSnapshot VolumeType = 'S'
VolumeTypePVMove VolumeType = 'p'
VolumeTypeVirtual VolumeType = 'v'
VolumeTypeMirrorOrRAIDImage VolumeType = 'i'
VolumeTypeMirrorOrRAIDImageOutOfSync VolumeType = 'I'
VolumeTypeMirrorLogDevice VolumeType = 'l'
VolumeTypeUnderConversion VolumeType = 'c'
VolumeTypeThinVolume VolumeType = 'V'
VolumeTypeThinPool VolumeType = 't'
VolumeTypeThinPoolData VolumeType = 'T'
VolumeTypeThinPoolMetadata VolumeType = 'e'
VolumeTypeNone VolumeType = '-'
)
type LVPermissions rune
const (
LVPermissionsWriteable LVPermissions = 'w'
LVPermissionsReadOnly LVPermissions = 'r'
LVPermissionsReadOnlyActivationOfNonReadOnlyVolume LVPermissions = 'R'
LVPermissionsNone LVPermissions = '-'
)
type LVAllocationPolicyAttr rune
const (
LVAllocationPolicyAttrAnywhere LVAllocationPolicyAttr = 'a'
LVAllocationPolicyAttrAnywhereLocked LVAllocationPolicyAttr = 'A'
LVAllocationPolicyAttrContiguous LVAllocationPolicyAttr = 'c'
LVAllocationPolicyAttrContiguousLocked LVAllocationPolicyAttr = 'C'
LVAllocationPolicyAttrInherited LVAllocationPolicyAttr = 'i'
LVAllocationPolicyAttrInheritedLocked LVAllocationPolicyAttr = 'I'
LVAllocationPolicyAttrCling LVAllocationPolicyAttr = 'l'
LVAllocationPolicyAttrClingLocked LVAllocationPolicyAttr = 'L'
LVAllocationPolicyAttrNormal LVAllocationPolicyAttr = 'n'
LVAllocationPolicyAttrNormalLocked LVAllocationPolicyAttr = 'N'
LVAllocationPolicyAttrNone = '-'
)
type Minor rune
const (
MinorTrue Minor = 'm'
MinorFalse Minor = '-'
)
type State rune
const (
StateActive State = 'a'
StateSuspended State = 's'
StateInvalidSnapshot State = 'I'
StateSuspendedSnapshot State = 'S'
StateSnapshotMergeFailed State = 'm'
StateSuspendedSnapshotMergeFailed State = 'M'
StateMappedDevicePresentWithoutTables State = 'd'
StateMappedDevicePresentWithInactiveTables State = 'i'
StateNone State = '-'
StateHistorical State = 'h'
StateThinPoolCheckNeeded State = 'c'
StateSuspendedThinPoolCheckNeeded State = 'C'
StateUnknown State = 'X'
)
type Open rune
const (
OpenTrue Open = 'o'
OpenFalse Open = '-'
OpenUnknown Open = 'X'
)
type OpenTarget rune
const (
OpenTargetMirror = 'm'
OpenTargetRaid = 'r'
OpenTargetSnapshot = 's'
OpenTargetThin = 't'
OpenTargetUnknown = 'u'
OpenTargetVirtual = 'v'
)
type ZeroAttr rune
const (
ZeroAttrTrue ZeroAttr = 'z'
ZeroAttrFalse ZeroAttr = '-'
)
type VolumeHealth rune
const (
VolumeHealthPartialActivation = 'p'
VolumeHealthUnknown = 'X'
VolumeHealthOK = '-'
VolumeHealthRAIDRefreshNeeded = 'r'
VolumeHealthRAIDMismatchesExist = 'm'
VolumeHealthRAIDWriteMostly = 'w'
VolumeHealthRAIDReshaping = 's'
VolumeHealthRAIDReshapeRemoved = 'R'
VolumeHealthThinFailed = 'F'
VolumeHealthThinPoolOutOfDataSpace = 'D'
VolumeHealthThinPoolMetadataReadOnly = 'M'
VolumeHealthWriteCacheError = 'E'
)
type SkipActivation rune
const (
SkipActivationTrue SkipActivation = 'k'
SkipActivationFalse SkipActivation = '-'
)
// LVAttributes has mapped lv_attr information, see https://linux.die.net/man/8/lvs
// It is a complete parsing of the entire attribute byte flags that is attached to each LV.
// This is useful when attaching logic to the state of an LV as the state of an LV can be determined
// from the Attributes, e.g. for determining whether an LV is considered a Thin-Pool or not.
type LVAttributes struct {
VolumeType
LVPermissions
LVAllocationPolicyAttr
Minor
State
Open
OpenTarget
ZeroAttr
VolumeHealth
SkipActivation
}
func ParseLVAttributes(raw string) (LVAttributes, error) {
if len(raw) != 10 {
return LVAttributes{}, fmt.Errorf("%s is an invalid length lv_attr", raw)
}
return LVAttributes{
VolumeType(raw[0]),
LVPermissions(raw[1]),
LVAllocationPolicyAttr(raw[2]),
Minor(raw[3]),
State(raw[4]),
Open(raw[5]),
OpenTarget(raw[6]),
ZeroAttr(raw[7]),
VolumeHealth(raw[8]),
SkipActivation(raw[9]),
}, nil
}
func (attr LVAttributes) String() string {
var builder strings.Builder
fields := []rune{
rune(attr.VolumeType),
rune(attr.LVPermissions),
rune(attr.LVAllocationPolicyAttr),
rune(attr.Minor),
rune(attr.State),
rune(attr.Open),
rune(attr.OpenTarget),
rune(attr.ZeroAttr),
rune(attr.VolumeHealth),
rune(attr.SkipActivation),
}
builder.Grow(len(fields))
for _, r := range fields {
builder.WriteRune(r)
}
return builder.String()
}
func (attr LVAttributes) MarshalText() ([]byte, error) {
return []byte(attr.String()), nil
}
// VerifyHealth checks the health of the logical volume based on the attributes, mainly
// bit 9 (volume health indicator) based on bit 1 (volume type indicator)
// All failed known states are reported with an error message.
func (attr LVAttributes) VerifyHealth() error {
if attr.VolumeHealth == VolumeHealthPartialActivation {
return ErrPartialActivation
}
if attr.VolumeHealth == VolumeHealthUnknown {
return ErrUnknownVolumeHealth
}
if attr.VolumeHealth == VolumeHealthWriteCacheError {
return ErrWriteCacheError
}
if attr.VolumeType == VolumeTypeThinPool {
switch attr.VolumeHealth {
case VolumeHealthThinFailed:
return ErrThinPoolFailed
case VolumeHealthThinPoolOutOfDataSpace:
return ErrThinPoolOutOfDataSpace
case VolumeHealthThinPoolMetadataReadOnly:
return ErrThinPoolMetadataReadOnly
}
}
if attr.VolumeType == VolumeTypeThinVolume {
switch attr.VolumeHealth {
case VolumeHealthThinFailed:
return ErrThinVolumeFailed
}
}
if attr.VolumeType == VolumeTypeRAID || attr.VolumeType == VolumeTypeRAIDNoInitialSync {
switch attr.VolumeHealth {
case VolumeHealthRAIDRefreshNeeded:
return ErrRAIDRefreshNeeded
case VolumeHealthRAIDMismatchesExist:
return ErrRAIDMismatchesExist
case VolumeHealthRAIDReshaping:
return ErrRAIDReshaping
case VolumeHealthRAIDReshapeRemoved:
return ErrRAIDReshapeRemoved
case VolumeHealthRAIDWriteMostly:
return ErrRAIDWriteMostly
}
}
switch attr.State {
case StateSuspended, StateSuspendedSnapshot:
return ErrLogicalVolumeSuspended
case StateInvalidSnapshot:
return ErrInvalidSnapshot
case StateSnapshotMergeFailed, StateSuspendedSnapshotMergeFailed:
return ErrSnapshotMergeFailed
case StateMappedDevicePresentWithInactiveTables:
return ErrMappedDevicePresentWithInactiveTables
case StateMappedDevicePresentWithoutTables:
return ErrMappedDevicePresentWithoutTables
case StateThinPoolCheckNeeded, StateSuspendedThinPoolCheckNeeded:
return ErrThinPoolCheckNeeded
case StateUnknown:
return ErrUnknownVolumeState
case StateHistorical:
return ErrHistoricalVolumeState
}
switch attr.Open {
case OpenUnknown:
return ErrLogicalVolumeUnderlyingDeviceStateUnknown
}
return nil
}