This repository has been archived by the owner on Oct 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 55
/
pmd-lvm.go
217 lines (191 loc) · 6.36 KB
/
pmd-lvm.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
package pmdmanager
import (
"fmt"
"strconv"
"strings"
"github.com/golang/glog"
"github.com/intel/csi-pmem/pkg/ndctl"
pmemexec "github.com/intel/csi-pmem/pkg/pmem-exec"
)
type pmemLvm struct {
volumeGroups []string
}
var _ PmemDeviceManager = &pmemLvm{}
var lvsArgs = []string{"--noheadings", "-o", "lv_name,lv_path,lv_size", "--units", "B"}
var vgsArgs = []string{"--noheadings", "--nosuffix", "-o", "vg_name,vg_size,vg_free,vg_tags", "--units", "B"}
// NewPmemDeviceManagerLVM Instantiates a new LVM based pmem device manager
// The pre-requisite for this manager is that all the pmem regions which should be managed by
// this LMV manager are devided into namespaces and grouped as volume groups.
func NewPmemDeviceManagerLVM() (PmemDeviceManager, error) {
ctx, err := ndctl.NewContext()
if err != nil {
return nil, fmt.Errorf("Failed to initialize pmem context: %s", err.Error())
}
volumeGroups := []string{}
for _, bus := range ctx.GetBuses() {
glog.Infof("NewPmemDeviceManagerLVM: Bus: %v", bus.DeviceName())
for _, r := range bus.ActiveRegions() {
glog.Infof("NewPmemDeviceManagerLVM: Region: %v", r.DeviceName())
nsmodes := []string{"fsdax", "sector"}
for _, nsmod := range nsmodes {
vgname := vgName(bus, r, nsmod)
if _, err := pmemexec.RunCommand("vgs", vgname); err != nil {
glog.Infof("NewPmemDeviceManagerLVM: VG %v non-existent, skip", vgname)
} else {
volumeGroups = append(volumeGroups, vgname)
glog.Infof("NewPmemDeviceManagerLVM: NsMode: %v", nsmod)
}
}
}
}
ctx.Free()
return &pmemLvm{
volumeGroups: volumeGroups,
}, nil
}
type vgInfo struct {
name string
size uint64
free uint64
tag string
}
func (lvm *pmemLvm) GetCapacity() (uint64, error) {
// TODO: this is hard-coded to fsdax. should add another loop over nsmode=sector ?
vgs, err := getVolumeGroups(lvm.volumeGroups, "fsdax")
if err != nil {
return 0, err
}
var capacity uint64
for _, vg := range vgs {
if vg.free > capacity {
capacity = vg.free
}
}
return capacity, nil
}
// nsmode is "fsdax" or "sector"
func (lvm *pmemLvm) CreateDevice(name string, size uint64, nsmode string) error {
if nsmode != "fsdax" && nsmode != "sector" {
return fmt.Errorf("Unknown nsmode(%v)", nsmode)
}
// pick a region, few possible strategies:
// 1. pick first with enough available space: simplest, regions get filled in order;
// 2. pick first with largest available space: regions get used round-robin, i.e. load-balanced, but does not leave large unused;
// 3. pick first with smallest available which satisfies the request: ordered initially, but later leaves bigger free available;
// Let's implement strategy 1 for now, simplest to code as no need to compare sizes in all regions
// NOTE: We walk buses and regions in ndctl context, but avail.size we check in LV context
vgs, err := getVolumeGroups(lvm.volumeGroups, nsmode)
if err != nil {
return err
}
// lvcreate takes size in MBytes if no unit.
// We use MBytes here to avoid problems with byte-granularity, as lvcreate
// may refuse to create some arbitrary sizes.
// Division by 1M should not result in smaller-than-asked here
// as lvcreate will round up to next 4MB boundary.
sizeM := int(size / (1024 * 1024))
strSz := strconv.Itoa(sizeM)
for _, vg := range vgs {
if vg.free >= size {
// lvcreate takes size in MBytes if no unit
if _, err := pmemexec.RunCommand("lvcreate", "-L", strSz, "-n", name, vg.name); err != nil {
glog.Infof("lvcreate failed with error: %v, trying for next free region", err)
} else {
return nil
}
}
}
return fmt.Errorf("No region is having enough space required(%v)", size)
}
func (lvm *pmemLvm) DeleteDevice(name string, flush bool) error {
device, err := lvm.GetDevice(name)
if err != nil {
return err
}
glog.Infof("DeleteDevice: Matching LVpath: %v erase:%v", device.Path, flush)
if flush {
flushDevice(device)
}
_, err = pmemexec.RunCommand("lvremove", "-fy", device.Path)
return err
}
func (lvm *pmemLvm) FlushDeviceData(name string) error {
device, err := lvm.GetDevice(name)
if err != nil {
return err
}
return flushDevice(device)
}
func (lvm *pmemLvm) GetDevice(id string) (PmemDeviceInfo, error) {
devices, err := lvm.ListDevices()
if err != nil {
return PmemDeviceInfo{}, err
}
for _, dev := range devices {
if dev.Name == id {
return dev, nil
}
}
return PmemDeviceInfo{}, fmt.Errorf("Device not found with name %s", id)
}
func (lvm *pmemLvm) ListDevices() ([]PmemDeviceInfo, error) {
args := append(lvsArgs, lvm.volumeGroups...)
output, err := pmemexec.RunCommand("lvs", args...)
if err != nil {
return nil, fmt.Errorf("list volumes failed : %s(lvs output: %s)", err.Error(), output)
}
return parseLVSOuput(output)
}
func vgName(bus *ndctl.Bus, region *ndctl.Region, nsmode string) string {
return bus.DeviceName() + region.DeviceName() + nsmode
}
func flushDevice(dev PmemDeviceInfo) error {
// erase data on block device, if not disabled by driver option
// use one iteration instead of shred's default=3 for speed
glog.Infof("Wiping data on: %s", dev.Path)
if _, err := pmemexec.RunCommand("shred", "-n", "1", dev.Path); err != nil {
return fmt.Errorf("device flush failure: %v", err.Error())
}
return nil
}
//lvs options "lv_name,lv_path,lv_size,lv_free"
func parseLVSOuput(output string) ([]PmemDeviceInfo, error) {
devices := []PmemDeviceInfo{}
lines := strings.Split(string(output), "\n")
for _, line := range lines {
fields := strings.Fields(strings.TrimSpace(line))
if len(fields) != 3 {
continue
}
dev := PmemDeviceInfo{}
dev.Name = fields[0]
dev.Path = fields[1]
dev.Size, _ = strconv.ParseUint(fields[2], 10, 64)
devices = append(devices, dev)
}
return devices, nil
}
func getVolumeGroups(groups []string, wantedTag string) ([]vgInfo, error) {
vgs := []vgInfo{}
args := append(vgsArgs, groups...)
output, err := pmemexec.RunCommand("vgs", args...)
if err != nil {
return vgs, fmt.Errorf("vgs failure: %s", err.Error())
}
for _, line := range strings.SplitN(output, "\n", len(groups)) {
fields := strings.Fields(strings.TrimSpace(line))
if len(fields) != 4 {
return vgs, fmt.Errorf("Failed to parse vgs output line: %s", line)
}
tag := fields[3]
if tag == wantedTag {
vg := vgInfo{}
vg.name = fields[0]
vg.size, _ = strconv.ParseUint(fields[1], 10, 64)
vg.free, _ = strconv.ParseUint(fields[2], 10, 64)
vg.tag = tag
vgs = append(vgs, vg)
}
}
return vgs, nil
}