diskfs.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. package diskfs
  2. import (
  3. "errors"
  4. "fmt"
  5. "os/exec"
  6. "strconv"
  7. "strings"
  8. "imuslab.com/arozos/mod/utils"
  9. )
  10. /*
  11. diskfs.go
  12. This module handle file system creation and formatting
  13. */
  14. // Storage Device meta was generated by lsblk
  15. type StorageDeviceMeta struct {
  16. Name string
  17. Size int64
  18. RO bool
  19. DevType string
  20. MountPoint string
  21. }
  22. // Check if the file format driver is installed on this host
  23. // if a format is supported, mkfs.(format) should be symlinked under /sbin
  24. func FormatPackageInstalled(fsType string) bool {
  25. return utils.FileExists("/sbin/mkfs." + fsType)
  26. }
  27. // Create file system, support ntfs, ext4 and fat32 only
  28. func FormatStorageDevice(fsType string, devicePath string) error {
  29. // Check if the filesystem type is supported
  30. switch fsType {
  31. case "ext4":
  32. // Format the device with the specified filesystem type
  33. cmd := exec.Command("sudo", "mkfs."+fsType, devicePath)
  34. output, err := cmd.CombinedOutput()
  35. if err != nil {
  36. return errors.New("unable to format device: " + string(output))
  37. }
  38. return nil
  39. case "vfat", "fat", "fat32":
  40. //Check if mkfs.fat exists
  41. if !FormatPackageInstalled("vfat") {
  42. return errors.New("unable to format device as fat (vfat). dosfstools not installed?")
  43. }
  44. // Format the device with the specified filesystem type
  45. cmd := exec.Command("sudo", "mkfs.vfat", devicePath)
  46. output, err := cmd.CombinedOutput()
  47. if err != nil {
  48. return errors.New("unable to format device: " + string(output))
  49. }
  50. return nil
  51. case "ntfs":
  52. //Check if ntfs-3g exists
  53. if !FormatPackageInstalled("ntfs") {
  54. return errors.New("unable to format device as ntfs: ntfs-3g not installed?")
  55. }
  56. //Format the drive
  57. cmd := exec.Command("sudo", "mkfs.ntfs", devicePath)
  58. output, err := cmd.CombinedOutput()
  59. if err != nil {
  60. return errors.New("unable to format device: " + string(output))
  61. }
  62. return nil
  63. default:
  64. return fmt.Errorf("unsupported filesystem type: %s", fsType)
  65. }
  66. }
  67. // List all the storage device in the system, set minSize to 0 for no filter
  68. func ListAllStorageDevices(minSize int64) ([]*StorageDeviceMeta, error) {
  69. cmd := exec.Command("sudo", "lsblk", "-b")
  70. output, err := cmd.CombinedOutput()
  71. if err != nil {
  72. return nil, fmt.Errorf("lsblk error: %v", err)
  73. }
  74. // Split the output into lines
  75. lines := strings.Split(string(output), "\n")
  76. var devices []*StorageDeviceMeta
  77. // Parse each line to extract device information
  78. for _, line := range lines[1:] { // Skip the header line
  79. fields := strings.Fields(line)
  80. if len(fields) < 7 {
  81. continue
  82. }
  83. size, err := strconv.ParseInt(fields[3], 10, 64)
  84. if err != nil {
  85. return nil, fmt.Errorf("error parsing device size: %v", err)
  86. }
  87. ro := fields[4] == "1"
  88. device := &StorageDeviceMeta{
  89. Name: fields[0],
  90. Size: size,
  91. RO: ro,
  92. DevType: fields[5],
  93. MountPoint: fields[6],
  94. }
  95. // Filter devices based on minimum size
  96. if size >= minSize {
  97. devices = append(devices, device)
  98. }
  99. }
  100. return devices, nil
  101. }