mdadm.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. package raid
  2. import (
  3. "fmt"
  4. "os"
  5. "os/exec"
  6. "strconv"
  7. "strings"
  8. )
  9. /*
  10. mdadm manager
  11. This script handles the interaction with mdadm
  12. */
  13. type StorageDeviceMeta struct {
  14. Name string
  15. Size int64
  16. RO bool
  17. DevType string
  18. MountPoint string
  19. }
  20. // List all the storage device in the system, set minSize to 0 for no filter
  21. func (m *Manager) ListAllStorageDevices(minSize int64) ([]*StorageDeviceMeta, error) {
  22. cmd := exec.Command("sudo", "lsblk", "-b")
  23. output, err := cmd.CombinedOutput()
  24. if err != nil {
  25. return nil, fmt.Errorf("lsblk error: %v", err)
  26. }
  27. // Split the output into lines
  28. lines := strings.Split(string(output), "\n")
  29. var devices []*StorageDeviceMeta
  30. // Parse each line to extract device information
  31. for _, line := range lines[1:] { // Skip the header line
  32. fields := strings.Fields(line)
  33. if len(fields) < 7 {
  34. continue
  35. }
  36. size, err := strconv.ParseInt(fields[3], 10, 64)
  37. if err != nil {
  38. return nil, fmt.Errorf("error parsing device size: %v", err)
  39. }
  40. ro := fields[4] == "1"
  41. device := &StorageDeviceMeta{
  42. Name: fields[0],
  43. Size: size,
  44. RO: ro,
  45. DevType: fields[5],
  46. MountPoint: fields[6],
  47. }
  48. // Filter devices based on minimum size
  49. if size >= minSize {
  50. devices = append(devices, device)
  51. }
  52. }
  53. return devices, nil
  54. }
  55. // Return the uuid of the disk by its path name (e.g. /dev/sda)
  56. func (m *Manager) GetDiskUUIDByPath(devicePath string) (string, error) {
  57. cmd := exec.Command("sudo", "blkid", devicePath)
  58. output, err := cmd.CombinedOutput()
  59. if err != nil {
  60. return "", fmt.Errorf("blkid error: %v", err)
  61. }
  62. // Parse the output to extract the UUID
  63. fields := strings.Fields(string(output))
  64. for _, field := range fields {
  65. if strings.HasPrefix(field, "UUID=") {
  66. uuid := strings.TrimPrefix(field, "UUID=\"")
  67. uuid = strings.TrimSuffix(uuid, "\"")
  68. return uuid, nil
  69. }
  70. }
  71. return "", fmt.Errorf("UUID not found for device %s", devicePath)
  72. }
  73. // CreateRAIDDevice creates a RAID device using the mdadm command.
  74. func (m *Manager) CreateRAIDDevice(devName string, raidLevel int, raidDev int, spareDevice int, raidDeviceIds []string, spareDeviceIds []string) error {
  75. if len(raidDeviceIds) < raidDev {
  76. return fmt.Errorf("insufficient RAID devices specified")
  77. }
  78. if len(spareDeviceIds) < spareDevice {
  79. return fmt.Errorf("insufficient spare devices specified")
  80. }
  81. // Concatenate RAID and spare device arrays
  82. allDeviceIds := append(raidDeviceIds, spareDeviceIds...)
  83. // Build the mdadm command
  84. cmdArgs := []string{"mdadm", "--create", devName, fmt.Sprintf("--level=%d", raidLevel),
  85. fmt.Sprintf("--raid-devices=%d", raidDev), fmt.Sprintf("--spare-devices=%d", spareDevice)}
  86. cmdArgs = append(cmdArgs, allDeviceIds...)
  87. cmd := exec.Command("sudo", cmdArgs...)
  88. cmd.Stdout = os.Stdout
  89. cmd.Stderr = os.Stderr
  90. err := cmd.Run()
  91. if err != nil {
  92. return fmt.Errorf("error running mdadm command: %v", err)
  93. }
  94. return nil
  95. }