mdadm.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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, raidDeviceIds []string, spareDeviceIds []string) error {
  75. //Calculate the size of the raid devices
  76. raidDev := len(raidDeviceIds)
  77. spareDevice := len(spareDeviceIds)
  78. //Validate the number of disk is enough for the raid
  79. if raidLevel == 0 && raidDev < 2 {
  80. return fmt.Errorf("not enough disks for raid0")
  81. } else if raidLevel == 1 && raidDev < 2 {
  82. return fmt.Errorf("not enough disks for raid1")
  83. } else if raidLevel == 5 && raidDev < 3 {
  84. return fmt.Errorf("not enough disk for raid5")
  85. } else if raidLevel == 6 && raidDev < 4 {
  86. return fmt.Errorf("not enough disk for raid6")
  87. }
  88. // Concatenate RAID and spare device arrays
  89. allDeviceIds := append(raidDeviceIds, spareDeviceIds...)
  90. // Build the mdadm command
  91. cmdArgs := []string{"mdadm", "--create", devName, fmt.Sprintf("--level=%d", raidLevel),
  92. fmt.Sprintf("--raid-devices=%d", raidDev), fmt.Sprintf("--spare-devices=%d", spareDevice)}
  93. cmdArgs = append(cmdArgs, allDeviceIds...)
  94. cmd := exec.Command("sudo", cmdArgs...)
  95. cmd.Stdout = os.Stdout
  96. cmd.Stderr = os.Stderr
  97. fmt.Println(cmdArgs)
  98. err := cmd.Run()
  99. if err != nil {
  100. return fmt.Errorf("error running mdadm command: %v", err)
  101. }
  102. return nil
  103. }
  104. // DANGER: Wipe the whole disk given the disk path
  105. func (m *Manager) WipeDisk(diskPath string) error {
  106. // Unmount the disk
  107. isMounted, _ := DeviceIsMounted(diskPath)
  108. if isMounted {
  109. umountCmd := exec.Command("sudo", "umount", diskPath)
  110. if err := umountCmd.Run(); err != nil {
  111. return fmt.Errorf("error unmounting disk %s: %v", diskPath, err)
  112. }
  113. }
  114. // Wipe all filesystem signatures on each of the partitions
  115. wipeCmd1 := exec.Command("sudo", "wipefs", "--all", "--force", diskPath+"?")
  116. if err := wipeCmd1.Run(); err != nil {
  117. return fmt.Errorf("error wiping filesystem signatures on %s?: %v", diskPath, err)
  118. }
  119. // Wipe all filesystem signatures on the entire disk
  120. wipeCmd2 := exec.Command("sudo", "wipefs", "--all", "--force", diskPath)
  121. if err := wipeCmd2.Run(); err != nil {
  122. return fmt.Errorf("error wiping filesystem signatures on %s: %v", diskPath, err)
  123. }
  124. return nil
  125. }