raidutils.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. package raid
  2. import (
  3. "errors"
  4. "fmt"
  5. "os"
  6. "os/exec"
  7. "path/filepath"
  8. "strings"
  9. "imuslab.com/arozos/mod/disk/diskfs"
  10. )
  11. // Get the next avaible RAID array name
  12. func GetNextAvailableMDDevice() (string, error) {
  13. for i := 0; i < 100; i++ {
  14. mdDevice := fmt.Sprintf("/dev/md%d", i)
  15. if _, err := os.Stat(mdDevice); os.IsNotExist(err) {
  16. return mdDevice, nil
  17. }
  18. }
  19. return "", fmt.Errorf("no available /dev/mdX devices found")
  20. }
  21. // Check if the given device is safe to remove from the array without losing data
  22. func (m *Manager) IsSafeToRemove(mdDev string, sdXDev string) bool {
  23. targetRAIDVol, err := m.GetRAIDDeviceByDevicePath(mdDev)
  24. if err != nil {
  25. return false
  26. }
  27. //Trim off the /dev/ part if exists
  28. sdXDev = filepath.Base(sdXDev)
  29. //Check how many members left if this is removed
  30. remainingMemebers := 0
  31. for _, member := range targetRAIDVol.Members {
  32. if member.Name != sdXDev {
  33. remainingMemebers++
  34. }
  35. }
  36. //Check if removal of sdX will cause data loss
  37. if strings.EqualFold(targetRAIDVol.Level, "raid0") {
  38. return false
  39. } else if strings.EqualFold(targetRAIDVol.Level, "raid1") {
  40. //In raid1, you need at least 1 disk to hold data
  41. return remainingMemebers >= 1
  42. } else if strings.EqualFold(targetRAIDVol.Level, "raid5") {
  43. //In raid 5, at least 2 disk is needed before data loss
  44. return remainingMemebers >= 2
  45. } else if strings.EqualFold(targetRAIDVol.Level, "raid6") {
  46. //In raid 6, you need 6 disks with max loss = 2 disks
  47. return remainingMemebers >= 2
  48. }
  49. return true
  50. }
  51. // Check if the given disk (sdX) is currently used in any volume
  52. func (m *Manager) DiskIsUsedInAnotherRAIDVol(sdXDev string) (bool, error) {
  53. raidPools, err := m.GetRAIDDevicesFromProcMDStat()
  54. if err != nil {
  55. return false, errors.New("unable to access RAID controller state")
  56. }
  57. for _, md := range raidPools {
  58. for _, member := range md.Members {
  59. if member.Name == filepath.Base(sdXDev) {
  60. return true, nil
  61. }
  62. }
  63. }
  64. return false, nil
  65. }
  66. // ClearSuperblock clears the superblock of the specified disk so it can be used safely
  67. func (m *Manager) ClearSuperblock(devicePath string) error {
  68. isMounted, err := diskfs.DeviceIsMounted(devicePath)
  69. if err != nil {
  70. return errors.New("unable to validate if the device is unmounted: " + err.Error())
  71. }
  72. if isMounted {
  73. return errors.New("target device is mounted. Make sure it is unmounted before clearing")
  74. }
  75. cmd := exec.Command("sudo", "mdadm", "--zero-superblock", devicePath)
  76. err = cmd.Run()
  77. if err != nil {
  78. return fmt.Errorf("error clearing superblock: %v", err)
  79. }
  80. return nil
  81. }
  82. // Use to restart any not-removed RAID device
  83. func (m *Manager) RestartRAIDService() error {
  84. cmd := exec.Command("sudo", "mdadm", "--assemble", "--scan")
  85. // Run the command
  86. output, err := cmd.CombinedOutput()
  87. if err != nil {
  88. if string(output) == "" {
  89. //Nothing updated in config.
  90. return nil
  91. }
  92. return fmt.Errorf("error restarting RAID device: %s", strings.TrimSpace(string(output)))
  93. }
  94. return nil
  95. }
  96. // Stop RAID device with given path
  97. func (m *Manager) StopRAIDDevice(devicePath string) error {
  98. cmd := exec.Command("sudo", "mdadm", "--stop", devicePath)
  99. // Run the command
  100. err := cmd.Run()
  101. if err != nil {
  102. return fmt.Errorf("error stopping RAID device: %v", err)
  103. }
  104. return nil
  105. }
  106. // RemoveRAIDDevice removes the specified RAID device member (disk).
  107. func (m *Manager) RemoveRAIDMember(devicePath string) error {
  108. // Construct the mdadm command to remove the RAID device
  109. cmd := exec.Command("sudo", "mdadm", "--remove", devicePath)
  110. // Run the command
  111. output, err := cmd.CombinedOutput()
  112. if err != nil {
  113. // If there was an error, return the combined output and the error message
  114. return fmt.Errorf("error removing RAID device: %s", strings.TrimSpace(string(output)))
  115. }
  116. return nil
  117. }
  118. // IsValidRAIDLevel checks if the given RAID level is valid.
  119. func IsValidRAIDLevel(level string) bool {
  120. // List of valid RAID levels
  121. validLevels := []string{"raid1", "raid0", "raid6", "raid5", "raid4", "raid10"}
  122. // Convert the RAID level to lowercase and remove any surrounding whitespace
  123. level = strings.TrimSpace(strings.ToLower(level))
  124. // Check if the level exists in the list of valid levels
  125. for _, validLevel := range validLevels {
  126. if level == validLevel {
  127. return true
  128. }
  129. }
  130. // Return false if the level is not found in the list of valid levels
  131. return false
  132. }
  133. // Get RAID device info from device path
  134. func (m *Manager) GetRAIDDeviceByDevicePath(devicePath string) (*RAIDDevice, error) {
  135. //Strip the /dev/ part if it was accidentally passed in
  136. devicePath = filepath.Base(devicePath)
  137. //Get all the raid devices
  138. rdevs, err := m.GetRAIDDevicesFromProcMDStat()
  139. if err != nil {
  140. return nil, err
  141. }
  142. //Check for match
  143. for _, rdev := range rdevs {
  144. if rdev.Name == devicePath {
  145. return &rdev, nil
  146. }
  147. }
  148. return nil, errors.New("target RAID device not found")
  149. }
  150. // Check if a RAID device exists, e.g. md0
  151. func (m *Manager) RAIDDeviceExists(devicePath string) bool {
  152. _, err := m.GetRAIDDeviceByDevicePath(devicePath)
  153. return err == nil
  154. }