-
Notifications
You must be signed in to change notification settings - Fork 232
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Switch to golang native error wrapping
We now use the golang error wrapping format specifier `%w` instead of the deprecated github.com/pkg/errors package. Signed-off-by: Sascha Grunert <sgrunert@redhat.com>
- Loading branch information
1 parent
20281e0
commit 3455d12
Showing
42 changed files
with
338 additions
and
345 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,19 @@ | ||
//go:build linux && cgo | ||
// +build linux,cgo | ||
|
||
package devmapper | ||
|
||
import ( | ||
"bufio" | ||
"bytes" | ||
"errors" | ||
"fmt" | ||
"io/ioutil" | ||
"os" | ||
"os/exec" | ||
"path/filepath" | ||
"strings" | ||
|
||
"github.com/pkg/errors" | ||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
|
@@ -59,7 +60,7 @@ func checkDevAvailable(dev string) error { | |
} | ||
|
||
if !bytes.Contains(out, []byte(dev)) { | ||
return errors.Errorf("%s is not available for use with devicemapper", dev) | ||
return fmt.Errorf("%s is not available for use with devicemapper", dev) | ||
} | ||
return nil | ||
} | ||
|
@@ -84,7 +85,7 @@ func checkDevInVG(dev string) error { | |
// got "VG Name" line" | ||
vg := strings.TrimSpace(fields[1]) | ||
if len(vg) > 0 { | ||
return errors.Errorf("%s is already part of a volume group %q: must remove this device from any volume group or provide a different device", dev, vg) | ||
return fmt.Errorf("%s is already part of a volume group %q: must remove this device from any volume group or provide a different device", dev, vg) | ||
} | ||
logrus.Error(fields) | ||
break | ||
|
@@ -112,7 +113,7 @@ func checkDevHasFS(dev string) error { | |
if bytes.Equal(kv[0], []byte("TYPE")) { | ||
v := bytes.Trim(kv[1], "\"") | ||
if len(v) > 0 { | ||
return errors.Errorf("%s has a filesystem already, use dm.directlvm_device_force=true if you want to wipe the device", dev) | ||
return fmt.Errorf("%s has a filesystem already, use dm.directlvm_device_force=true if you want to wipe the device", dev) | ||
} | ||
return nil | ||
} | ||
|
@@ -123,16 +124,16 @@ func checkDevHasFS(dev string) error { | |
func verifyBlockDevice(dev string, force bool) error { | ||
absPath, err := filepath.Abs(dev) | ||
if err != nil { | ||
return errors.Errorf("unable to get absolute path for %s: %s", dev, err) | ||
return fmt.Errorf("unable to get absolute path for %s: %s", dev, err) | ||
} | ||
realPath, err := filepath.EvalSymlinks(absPath) | ||
if err != nil { | ||
return errors.Errorf("failed to canonicalise path for %s: %s", dev, err) | ||
return fmt.Errorf("failed to canonicalise path for %s: %s", dev, err) | ||
} | ||
if err := checkDevAvailable(absPath); err != nil { | ||
logrus.Infof("block device '%s' not available, checking '%s'", absPath, realPath) | ||
if err := checkDevAvailable(realPath); err != nil { | ||
return errors.Errorf("neither '%s' nor '%s' are in the output of lvmdiskscan, can't use device.", absPath, realPath) | ||
return fmt.Errorf("neither '%s' nor '%s' are in the output of lvmdiskscan, can't use device", absPath, realPath) | ||
} | ||
} | ||
if err := checkDevInVG(realPath); err != nil { | ||
|
@@ -158,7 +159,7 @@ func readLVMConfig(root string) (directLVMConfig, error) { | |
if os.IsNotExist(err) { | ||
return cfg, nil | ||
} | ||
return cfg, errors.Wrap(err, "error reading existing setup config") | ||
return cfg, fmt.Errorf("error reading existing setup config: %w", err) | ||
} | ||
|
||
// check if this is just an empty file, no need to produce a json error later if so | ||
|
@@ -167,17 +168,17 @@ func readLVMConfig(root string) (directLVMConfig, error) { | |
} | ||
|
||
err = json.Unmarshal(b, &cfg) | ||
return cfg, errors.Wrap(err, "error unmarshaling previous device setup config") | ||
return cfg, fmt.Errorf("error unmarshaling previous device setup config: %w", err) | ||
} | ||
|
||
func writeLVMConfig(root string, cfg directLVMConfig) error { | ||
p := filepath.Join(root, "setup-config.json") | ||
b, err := json.Marshal(cfg) | ||
if err != nil { | ||
return errors.Wrap(err, "error marshalling direct lvm config") | ||
return fmt.Errorf("error marshalling direct lvm config: %w", err) | ||
} | ||
err = ioutil.WriteFile(p, b, 0600) | ||
return errors.Wrap(err, "error writing direct lvm config to file") | ||
return fmt.Errorf("error writing direct lvm config to file: %w", err) | ||
} | ||
|
||
func setupDirectLVM(cfg directLVMConfig) error { | ||
|
@@ -186,13 +187,13 @@ func setupDirectLVM(cfg directLVMConfig) error { | |
|
||
for _, bin := range binaries { | ||
if _, err := exec.LookPath(bin); err != nil { | ||
return errors.Wrap(err, "error looking up command `"+bin+"` while setting up direct lvm") | ||
return fmt.Errorf("error looking up command `"+bin+"` while setting up direct lvm: %w", err) | ||
} | ||
} | ||
|
||
err := os.MkdirAll(lvmProfileDir, 0755) | ||
if err != nil { | ||
return errors.Wrap(err, "error creating lvm profile directory") | ||
return fmt.Errorf("error creating lvm profile directory: %w", err) | ||
} | ||
|
||
if cfg.AutoExtendPercent == 0 { | ||
|
@@ -215,34 +216,34 @@ func setupDirectLVM(cfg directLVMConfig) error { | |
|
||
out, err := exec.Command("pvcreate", "--metadatasize", cfg.MetaDataSize, "-f", cfg.Device).CombinedOutput() | ||
if err != nil { | ||
return errors.Wrap(err, string(out)) | ||
return fmt.Errorf("%v: %w", string(out), err) | ||
} | ||
|
||
out, err = exec.Command("vgcreate", "storage", cfg.Device).CombinedOutput() | ||
if err != nil { | ||
return errors.Wrap(err, string(out)) | ||
return fmt.Errorf("%v: %w", string(out), err) | ||
} | ||
|
||
out, err = exec.Command("lvcreate", "--wipesignatures", "y", "-n", "thinpool", "storage", "--extents", fmt.Sprintf("%d%%VG", cfg.ThinpPercent)).CombinedOutput() | ||
if err != nil { | ||
return errors.Wrap(err, string(out)) | ||
return fmt.Errorf("%v: %w", string(out), err) | ||
} | ||
out, err = exec.Command("lvcreate", "--wipesignatures", "y", "-n", "thinpoolmeta", "storage", "--extents", fmt.Sprintf("%d%%VG", cfg.ThinpMetaPercent)).CombinedOutput() | ||
if err != nil { | ||
return errors.Wrap(err, string(out)) | ||
return fmt.Errorf("%v: %w", string(out), err) | ||
} | ||
|
||
out, err = exec.Command("lvconvert", "-y", "--zero", "n", "-c", "512K", "--thinpool", "storage/thinpool", "--poolmetadata", "storage/thinpoolmeta").CombinedOutput() | ||
if err != nil { | ||
return errors.Wrap(err, string(out)) | ||
return fmt.Errorf("%v: %w", string(out), err) | ||
} | ||
|
||
profile := fmt.Sprintf("activation{\nthin_pool_autoextend_threshold=%d\nthin_pool_autoextend_percent=%d\n}", cfg.AutoExtendThreshold, cfg.AutoExtendPercent) | ||
err = ioutil.WriteFile(lvmProfileDir+"/storage-thinpool.profile", []byte(profile), 0600) | ||
if err != nil { | ||
return errors.Wrap(err, "error writing storage thinp autoextend profile") | ||
return fmt.Errorf("error writing storage thinp autoextend profile: %w", err) | ||
} | ||
|
||
out, err = exec.Command("lvchange", "--metadataprofile", "storage-thinpool", "storage/thinpool").CombinedOutput() | ||
return errors.Wrap(err, string(out)) | ||
return fmt.Errorf("%v: %w", string(out), err) | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
saschagrunert
Author
Member
|
||
} |
Oops, something went wrong.
this is not equivalent I think.
errors.Wrap
will return nil iferr == nil