diskfs.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. package diskfs
  2. import (
  3. "bufio"
  4. "errors"
  5. "fmt"
  6. "os"
  7. "os/exec"
  8. "strconv"
  9. "strings"
  10. "imuslab.com/arozos/mod/utils"
  11. )
  12. /*
  13. diskfs.go
  14. This module handle file system creation and formatting
  15. */
  16. // Storage Device meta was generated by lsblk
  17. type StorageDeviceMeta struct {
  18. Name string
  19. Size int64
  20. RO bool
  21. DevType string
  22. MountPoint string
  23. }
  24. // Check if the file format driver is installed on this host
  25. // if a format is supported, mkfs.(format) should be symlinked under /sbin
  26. func FormatPackageInstalled(fsType string) bool {
  27. return utils.FileExists("/sbin/mkfs." + fsType)
  28. }
  29. // Create file system, support ntfs, ext4 and fat32 only
  30. func FormatStorageDevice(fsType string, devicePath string) error {
  31. // Check if the filesystem type is supported
  32. switch fsType {
  33. case "ext4":
  34. // Format the device with the specified filesystem type
  35. cmd := exec.Command("sudo", "mkfs."+fsType, devicePath)
  36. output, err := cmd.CombinedOutput()
  37. if err != nil {
  38. return errors.New("unable to format device: " + string(output))
  39. }
  40. return nil
  41. case "vfat", "fat", "fat32":
  42. //Check if mkfs.fat exists
  43. if !FormatPackageInstalled("vfat") {
  44. return errors.New("unable to format device as fat (vfat). dosfstools not installed?")
  45. }
  46. // Format the device with the specified filesystem type
  47. cmd := exec.Command("sudo", "mkfs.vfat", devicePath)
  48. output, err := cmd.CombinedOutput()
  49. if err != nil {
  50. return errors.New("unable to format device: " + string(output))
  51. }
  52. return nil
  53. case "ntfs":
  54. //Check if ntfs-3g exists
  55. if !FormatPackageInstalled("ntfs") {
  56. return errors.New("unable to format device as ntfs: ntfs-3g not installed?")
  57. }
  58. //Format the drive
  59. cmd := exec.Command("sudo", "mkfs.ntfs", devicePath)
  60. output, err := cmd.CombinedOutput()
  61. if err != nil {
  62. return errors.New("unable to format device: " + string(output))
  63. }
  64. return nil
  65. default:
  66. return fmt.Errorf("unsupported filesystem type: %s", fsType)
  67. }
  68. }
  69. // List all the storage device in the system, set minSize to 0 for no filter
  70. func ListAllStorageDevices(minSize int64) ([]*StorageDeviceMeta, error) {
  71. cmd := exec.Command("sudo", "lsblk", "-b")
  72. output, err := cmd.CombinedOutput()
  73. if err != nil {
  74. return nil, fmt.Errorf("lsblk error: %v", err)
  75. }
  76. // Split the output into lines
  77. lines := strings.Split(string(output), "\n")
  78. var devices []*StorageDeviceMeta
  79. // Parse each line to extract device information
  80. for _, line := range lines[1:] { // Skip the header line
  81. fields := strings.Fields(line)
  82. if len(fields) < 7 {
  83. continue
  84. }
  85. size, err := strconv.ParseInt(fields[3], 10, 64)
  86. if err != nil {
  87. return nil, fmt.Errorf("error parsing device size: %v", err)
  88. }
  89. ro := fields[4] == "1"
  90. device := &StorageDeviceMeta{
  91. Name: fields[0],
  92. Size: size,
  93. RO: ro,
  94. DevType: fields[5],
  95. MountPoint: fields[6],
  96. }
  97. // Filter devices based on minimum size
  98. if size >= minSize {
  99. devices = append(devices, device)
  100. }
  101. }
  102. return devices, nil
  103. }
  104. // Check if a device is mounted given the path name, like /dev/sdc
  105. func DeviceIsMounted(devicePath string) (bool, error) {
  106. // Open the mountinfo file
  107. file, err := os.Open("/proc/mounts")
  108. if err != nil {
  109. return false, fmt.Errorf("error opening /proc/mounts: %v", err)
  110. }
  111. defer file.Close()
  112. // Scan the mountinfo file line by line
  113. scanner := bufio.NewScanner(file)
  114. for scanner.Scan() {
  115. line := scanner.Text()
  116. fields := strings.Fields(line)
  117. if len(fields) >= 2 && fields[0] == devicePath {
  118. // Device is mounted
  119. return true, nil
  120. }
  121. }
  122. // Device is not mounted
  123. return false, nil
  124. }
  125. // UnmountDevice unmounts the specified device.
  126. // Remember to use full path (e.g. /dev/md0) in the devicePath
  127. func UnmountDevice(devicePath string) error {
  128. // Construct the bash command to unmount the device
  129. cmd := exec.Command("sudo", "bash", "-c", fmt.Sprintf("umount %s", devicePath))
  130. // Run the command
  131. err := cmd.Run()
  132. if err != nil {
  133. return fmt.Errorf("error unmounting device: %v", err)
  134. }
  135. return nil
  136. }
  137. // DANGER: Wipe the whole disk given the disk path
  138. func WipeDisk(diskPath string) error {
  139. // Unmount the disk
  140. isMounted, _ := DeviceIsMounted(diskPath)
  141. if isMounted {
  142. umountCmd := exec.Command("sudo", "umount", diskPath)
  143. if err := umountCmd.Run(); err != nil {
  144. return fmt.Errorf("error unmounting disk %s: %v", diskPath, err)
  145. }
  146. }
  147. // Wipe all filesystem signatures on the entire disk
  148. wipeCmd := exec.Command("sudo", "wipefs", "--all", "--force", diskPath)
  149. if err := wipeCmd.Run(); err != nil {
  150. return fmt.Errorf("error wiping filesystem signatures on %s: %v", diskPath, err)
  151. }
  152. return nil
  153. }