Merge pull request #1256 from scop/feat/solaris-disk-serialnumber

[disk][solaris] implement SerialNumberWithContext
This commit is contained in:
shirou 2022-02-25 22:26:39 +09:00 committed by GitHub
commit 2fa880a4ad
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 31 additions and 1 deletions

View File

@ -5,10 +5,12 @@ package disk
import (
"bufio"
"bytes"
"context"
"fmt"
"math"
"os"
"os/exec"
"strings"
"github.com/shirou/gopsutil/v3/internal/common"
@ -114,7 +116,35 @@ func UsageWithContext(ctx context.Context, path string) (*UsageStat, error) {
}
func SerialNumberWithContext(ctx context.Context, name string) (string, error) {
return "", common.ErrNotImplementedError
cfgadm, err := exec.LookPath("cfgadm")
if err != nil {
return "", fmt.Errorf("find cfgadm: %w", err)
}
out, err := invoke.CommandWithContext(ctx, cfgadm, "-ls", "select=type(disk),cols=ap_id:info,cols2=,noheadings")
if err != nil {
return "", fmt.Errorf("exec cfgadm: %w", err)
}
suf := "::" + strings.TrimPrefix(name, "/dev/")
s := bufio.NewScanner(bytes.NewReader(out))
for s.Scan() {
flds := strings.Fields(s.Text())
if strings.HasSuffix(flds[0], suf) {
flen := len(flds)
if flen >= 3 {
for i, f := range flds {
if i > 0 && i < flen-1 && f == "SN:" {
return flds[i+1], nil
}
}
}
return "", nil
}
}
if err := s.Err(); err != nil {
return "", err
}
return "", nil
}
func LabelWithContext(ctx context.Context, name string) (string, error) {