Skip to content

Commit c0e6b65

Browse files
authored
Minor improvements on error logging and returning cmd outputs. (#138)
1 parent b0af4c2 commit c0e6b65

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

pkg/lvm/lvm.go

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ func MountLV(log *slog.Logger, lvname, mountPath string, vgName string, fsType s
8080
cmd = exec.Command(fmt.Sprintf("mkfs.%s", fsType), formatArgs...) //nolint:gosec
8181
out, err = cmd.CombinedOutput()
8282
if err != nil {
83-
return string(out), fmt.Errorf("unable to format lv:%s err:%w", lvname, err)
83+
return string(out), fmt.Errorf("unable to format lv %q: %w (%s)", lvname, err, string(out))
8484
}
8585
}
8686

@@ -97,7 +97,7 @@ func MountLV(log *slog.Logger, lvname, mountPath string, vgName string, fsType s
9797
if err != nil {
9898
mountOutput := string(out)
9999
if !strings.Contains(mountOutput, "already mounted") {
100-
return string(out), fmt.Errorf("unable to mount %s to %s err:%w output:%s", lvPath, mountPath, err, out)
100+
return string(out), fmt.Errorf("unable to mount %q to %q: %w (%s)", lvPath, mountPath, err, string(out))
101101
}
102102
}
103103
err = os.Chmod(mountPath, 0777|os.ModeSetgid)
@@ -124,7 +124,7 @@ func BindMountLV(log *slog.Logger, lvname, mountPath string, vgName string) (str
124124
if err != nil {
125125
mountOutput := string(out)
126126
if !strings.Contains(mountOutput, "already mounted") {
127-
return string(out), fmt.Errorf("unable to mount %s to %s err:%w output:%s", lvPath, mountPath, err, out)
127+
return string(out), fmt.Errorf("unable to mount %q to %s: %w (%s)", lvPath, mountPath, err, string(out))
128128
}
129129
}
130130
err = os.Chmod(mountPath, 0777|os.ModeSetgid)
@@ -140,7 +140,7 @@ func UmountLV(log *slog.Logger, targetPath string) {
140140
out, err := cmd.CombinedOutput()
141141
if err != nil {
142142
//RETURN err ?
143-
log.Error("unable to umount", "targetPath", targetPath, "output", out, "error", err)
143+
log.Error("unable to umount", "targetPath", targetPath, "output", string(out), "error", err)
144144
}
145145
}
146146

@@ -149,24 +149,28 @@ func VgExists(log *slog.Logger, vgname string) bool {
149149
cmd := exec.Command("vgs", vgname, "--noheadings", "-o", "vg_name")
150150
out, err := cmd.CombinedOutput()
151151
if err != nil {
152-
log.Debug("unable to list existing volumegroups", "error", err)
152+
log.Debug("unable to list existing volumegroups", "error", err, "output", string(out))
153153
return false
154154
}
155155
return vgname == strings.TrimSpace(string(out))
156156
}
157157

158158
// VgActivate execute vgchange -ay to activate all volumes of the volume group
159159
func VgActivate(log *slog.Logger) {
160+
// TODO: this function is kind of best effort and does not return any errors and it's not clear if it worked or not
161+
// can we turn this into something idempotent and more concrete?
162+
160163
// scan for vgs and activate if any
161164
cmd := exec.Command("vgscan")
162165
out, err := cmd.CombinedOutput()
163166
if err != nil {
164-
log.Debug("unable to scan for volumegroups", "output", out, "error", err)
167+
log.Debug("unable to scan for volumegroups", "output", string(out), "error", err)
165168
}
169+
166170
cmd = exec.Command("vgchange", "-ay")
167171
_, err = cmd.CombinedOutput()
168172
if err != nil {
169-
log.Debug("unable to activate volumegroups", "output", out, "error", err)
173+
log.Debug("unable to activate volumegroups", "output", string(out), "error", err)
170174
}
171175
}
172176

@@ -289,7 +293,7 @@ func LvExists(log *slog.Logger, vg string, name string) bool {
289293

290294
out, err := cmd.CombinedOutput()
291295
if err != nil {
292-
log.Error("unable to list existing volumes", "error", err)
296+
log.Error("unable to list existing volumes", "error", err, "output", string(out))
293297
return false
294298
}
295299

@@ -357,7 +361,7 @@ func VgStats(log *slog.Logger, vgName string) (int64, error) {
357361
cmd := exec.Command("vgs", args...) //nolint:gosec
358362
out, err := cmd.CombinedOutput()
359363
if err != nil {
360-
return 0, fmt.Errorf("unable to get vg stats of %s: %w", vgName, err)
364+
return 0, fmt.Errorf("unable to get vg stats of %q: %w (%s)", vgName, err, string(out))
361365
}
362366

363367
pvReport := vgReport{}

0 commit comments

Comments
 (0)