Skip to content

Commit

Permalink
Allow passing arguments to report functions, and use where possible.
Browse files Browse the repository at this point in the history
This adds passing arguments to
 getPvReport
 getLvReport
 getVgReport

And then uses those to not get full lvm reports when only one device
is expected.

The big win is that the CreateLV, CreatePV, CreateVG functions
no longer do a scan of all  of lvm after doing their job.  They
specifically ask about the device they know they created.
  • Loading branch information
Scott Moser authored and rchamarthy committed Apr 9, 2020
1 parent 5e5fc2e commit d2eda4f
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 32 deletions.
45 changes: 22 additions & 23 deletions linux/lvm.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,13 @@ type linuxLVM struct {
}

func (ls *linuxLVM) ScanPVs(filter disko.PVFilter) (disko.PVSet, error) {
return ls.scanPVs(filter)
}

func (ls *linuxLVM) scanPVs(filter disko.PVFilter, scanArgs ...string) (disko.PVSet, error) {
pvs := disko.PVSet{}

pvdatum, err := getPvReport()
pvdatum, err := getPvReport(scanArgs...)
if err != nil {
return pvs, err
}
Expand All @@ -36,11 +40,15 @@ func (ls *linuxLVM) ScanPVs(filter disko.PVFilter) (disko.PVSet, error) {
}

func (ls *linuxLVM) ScanVGs(filter disko.VGFilter) (disko.VGSet, error) {
return ls.scanVGs(filter)
}

func (ls *linuxLVM) scanVGs(filter disko.VGFilter, scanArgs ...string) (disko.VGSet, error) {
var vgdatum []lvmVGData
var vgs = disko.VGSet{}
var err error

vgdatum, err = getVgReport()
vgdatum, err = getVgReport(scanArgs...)
if err != nil {
return vgs, err
}
Expand All @@ -63,8 +71,7 @@ func (ls *linuxLVM) ScanVGs(filter disko.VGFilter) (disko.VGSet, error) {
return vgs, err
}

lvs, err := ls.ScanLVs(
func(d disko.LV) bool { return d.VGName == name })
lvs, err := ls.scanLVs(func(d disko.LV) bool { return true }, name)
if err != nil {
return vgs, err
}
Expand All @@ -79,11 +86,15 @@ func (ls *linuxLVM) ScanVGs(filter disko.VGFilter) (disko.VGSet, error) {
}

func (ls *linuxLVM) ScanLVs(filter disko.LVFilter) (disko.LVSet, error) {
return ls.scanLVs(filter)
}

func (ls *linuxLVM) scanLVs(filter disko.LVFilter, scanArgs ...string) (disko.LVSet, error) {
var lvdatum []lvmLVData
var lvs = disko.LVSet{}
var err error

lvdatum, err = getLvReport()
lvdatum, err = getLvReport(scanArgs...)
if err != nil {
return lvs, err
}
Expand Down Expand Up @@ -130,7 +141,7 @@ func (ls *linuxLVM) CreatePV(name string) (disko.PV, error) {
return nilPV, err
}

pvs, err := ls.ScanPVs(getPVFilterByName(kname))
pvs, err := ls.scanPVs(func(d disko.PV) bool { return true }, path)
if err != nil {
return nilPV, err
}
Expand All @@ -148,7 +159,7 @@ func (ls *linuxLVM) DeletePV(pv disko.PV) error {
}

func (ls *linuxLVM) HasPV(name string) bool {
pvs, err := ls.ScanPVs(getPVFilterByName(name))
pvs, err := ls.scanPVs(func(d disko.PV) bool { return true }, getPathForKname(name))
if err != nil {
return false
}
Expand All @@ -167,7 +178,7 @@ func (ls *linuxLVM) CreateVG(name string, pvs ...disko.PV) (disko.VG, error) {
return disko.VG{}, nil
}

vgSet, err := ls.ScanVGs(getVGFilterByName(name))
vgSet, err := ls.scanVGs(func(d disko.VG) bool { return true }, name)

if err != nil {
return disko.VG{}, nil
Expand Down Expand Up @@ -195,7 +206,7 @@ func (ls *linuxLVM) RemoveVG(vgName string) error {
}

func (ls *linuxLVM) HasVG(vgName string) bool {
vgs, err := ls.ScanVGs(getVGFilterByName(vgName))
vgs, err := ls.scanVGs(func(d disko.VG) bool { return true }, vgName)
if err != nil {
return false
}
Expand Down Expand Up @@ -244,7 +255,7 @@ func (ls *linuxLVM) CreateLV(vgName string, name string, size uint64,
return nilLV, err
}

lvs, err := ls.ScanLVs(getLVFilterByName(vgName, name))
lvs, err := ls.scanLVs(func(d disko.LV) bool { return true }, vgLv(vgName, name))

if err != nil {
return nilLV, err
Expand Down Expand Up @@ -278,26 +289,14 @@ func (ls *linuxLVM) ExtendLV(vgName string, lvName string,
}

func (ls *linuxLVM) HasLV(vgName string, name string) bool {
lvs, err := ls.ScanLVs(getLVFilterByName(vgName, name))
lvs, err := ls.scanLVs(func(d disko.LV) bool { return true }, vgLv(vgName, name))
if err != nil {
log.Panicf("Failed to scan logical volumes: %s", err)
}

return len(lvs) != 0
}

func getVGFilterByName(name string) disko.VGFilter {
return func(d disko.VG) bool { return d.Name == name }
}

func getPVFilterByName(name string) disko.PVFilter {
return func(d disko.PV) bool { return d.Name == name }
}

func getLVFilterByName(vgName string, name string) disko.LVFilter {
return func(d disko.LV) bool { return d.Name == name && d.VGName == vgName }
}

func isRoundExtent(size uint64) error {
if size%disko.ExtentSize == 0 {
return nil
Expand Down
22 changes: 13 additions & 9 deletions linux/lvmdump.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,11 @@ func parsePvReport(report []byte) ([]lvmPVData, error) {
return d["report"][0]["pv"], nil
}

func getPvReport() ([]lvmPVData, error) {
out, stderr, rc := runCommandWithOutputErrorRc(
"lvm", "pvs", "--options=pv_all,vg_name", "--report-format=json", "--unit=B")
func getPvReport(args ...string) ([]lvmPVData, error) {
cmd := []string{"lvm", "pvs", "--options=pv_all,vg_name", "--report-format=json", "--unit=B"}
cmd = append(cmd, args...)
out, stderr, rc := runCommandWithOutputErrorRc(cmd...)

if rc != 0 {
return []lvmPVData{},
fmt.Errorf("failed lvm pvs [%d]: %s\n%s", rc, out, stderr)
Expand Down Expand Up @@ -108,9 +110,10 @@ func parseVgReport(report []byte) ([]lvmVGData, error) {
return d["report"][0]["vg"], nil
}

func getVgReport() ([]lvmVGData, error) {
out, stderr, rc := runCommandWithOutputErrorRc(
"lvm", "vgs", "--options=vg_all", "--report-format=json", "--unit=B")
func getVgReport(args ...string) ([]lvmVGData, error) {
cmd := []string{"lvm", "vgs", "--options=vg_all", "--report-format=json", "--unit=B"}
cmd = append(cmd, args...)
out, stderr, rc := runCommandWithOutputErrorRc(cmd...)

if rc != 0 {
return []lvmVGData{},
Expand Down Expand Up @@ -162,9 +165,10 @@ func parseLvReport(report []byte) ([]lvmLVData, error) {
return d["report"][0]["lv"], nil
}

func getLvReport() ([]lvmLVData, error) {
out, stderr, rc := runCommandWithOutputErrorRc(
"lvm", "lvs", "--options=lv_all,vg_name", "--report-format=json", "--unit=B")
func getLvReport(args ...string) ([]lvmLVData, error) {
cmd := []string{"lvm", "lvs", "--options=lv_all,vg_name", "--report-format=json", "--unit=B"}
cmd = append(cmd, args...)
out, stderr, rc := runCommandWithOutputErrorRc(cmd...)

if rc != 0 {
return []lvmLVData{},
Expand Down

0 comments on commit d2eda4f

Please sign in to comment.