raidutils.go 949 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. package raid
  2. import (
  3. "bufio"
  4. "fmt"
  5. "os"
  6. "strings"
  7. )
  8. // Get the next avaible RAID array name
  9. func GetNextAvailableMDDevice() (string, error) {
  10. for i := 0; i < 100; i++ {
  11. mdDevice := fmt.Sprintf("/dev/md%d", i)
  12. if _, err := os.Stat(mdDevice); os.IsNotExist(err) {
  13. return mdDevice, nil
  14. }
  15. }
  16. return "", fmt.Errorf("no available /dev/mdX devices found")
  17. }
  18. func DeviceIsMounted(devicePath string) (bool, error) {
  19. // Open the mountinfo file
  20. file, err := os.Open("/proc/mounts")
  21. if err != nil {
  22. return false, fmt.Errorf("error opening /proc/mounts: %v", err)
  23. }
  24. defer file.Close()
  25. // Scan the mountinfo file line by line
  26. scanner := bufio.NewScanner(file)
  27. for scanner.Scan() {
  28. line := scanner.Text()
  29. fields := strings.Fields(line)
  30. if len(fields) >= 2 && fields[0] == devicePath {
  31. // Device is mounted
  32. return true, nil
  33. }
  34. }
  35. // Device is not mounted
  36. return false, nil
  37. }