Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for get disk serial number on darwin #1791

Merged
merged 4 commits into from
Mar 29, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 54 additions & 2 deletions disk/disk_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ package disk

import (
"context"
"encoding/json"
"errors"
"fmt"
"strings"
"unsafe"

"golang.org/x/sys/unix"
Expand Down Expand Up @@ -88,8 +90,58 @@ func getFsType(stat unix.Statfs_t) string {
return common.ByteToString(stat.Fstypename[:])
}

func SerialNumberWithContext(_ context.Context, _ string) (string, error) {
return "", common.ErrNotImplementedError
type spnvmeDataTypeItem struct {
Name string `json:"_name"`
BsdName string `json:"bsd_name"`
DetachableDrive string `json:"detachable_drive"`
DeviceModel string `json:"device_model"`
DeviceRevision string `json:"device_revision"`
DeviceSerial string `json:"device_serial"`
PartitionMapType string `json:"partition_map_type"`
RemovableMedia string `json:"removable_media"`
Size string `json:"size"`
SizeInBytes int64 `json:"size_in_bytes"`
SmartStatus string `json:"smart_status"`
SpnvmeTrimSupport string `json:"spnvme_trim_support"`
Volumes []struct {
Name string `json:"_name"`
BsdName string `json:"bsd_name"`
Iocontent string `json:"iocontent"`
Size string `json:"size"`
SizeInBytes int `json:"size_in_bytes"`
} `json:"volumes"`
}

type spnvmeDataWrapper struct {
SPNVMeDataType []struct {
Items []spnvmeDataTypeItem `json:"_items"`
} `json:"SPNVMeDataType"`
}

func SerialNumberWithContext(ctx context.Context, _ string) (string, error) {
output, err := invoke.CommandWithContext(ctx, "system_profiler", "SPNVMeDataType", "-json")
if err != nil {
return "", err
}

var data spnvmeDataWrapper
if err := json.Unmarshal(output, &data); err != nil {
return "", fmt.Errorf("failed to unmarshal JSON: %w", err)
}

// Extract all serial numbers into a single string
var serialNumbers []string
for _, spnvmeData := range data.SPNVMeDataType {
for _, item := range spnvmeData.Items {
serialNumbers = append(serialNumbers, item.DeviceSerial)
}
}

if len(serialNumbers) == 0 {
return "", errors.New("no serial numbers found")
}

return strings.Join(serialNumbers, ", "), nil
}

func LabelWithContext(_ context.Context, _ string) (string, error) {
Expand Down
Loading