diff --git a/linux/lvm.go b/linux/lvm.go index 4ef698e..8829c0d 100644 --- a/linux/lvm.go +++ b/linux/lvm.go @@ -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 } @@ -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 } @@ -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 } @@ -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 } @@ -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 } @@ -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 } @@ -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 @@ -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 } @@ -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 @@ -278,7 +289,7 @@ 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) } @@ -286,18 +297,6 @@ func (ls *linuxLVM) HasLV(vgName string, name string) bool { 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 diff --git a/linux/lvmdump.go b/linux/lvmdump.go index 74c054f..d228368 100644 --- a/linux/lvmdump.go +++ b/linux/lvmdump.go @@ -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) @@ -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{}, @@ -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{},