raid.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. package raid
  2. import (
  3. "errors"
  4. "fmt"
  5. "os"
  6. "os/exec"
  7. "path/filepath"
  8. "runtime"
  9. "imuslab.com/arozos/mod/apt"
  10. "imuslab.com/arozos/mod/utils"
  11. )
  12. /*
  13. RAID management package for handling RAID and Virtual Image Creation
  14. for Linux with mdadm installed
  15. */
  16. type Options struct {
  17. }
  18. type Manager struct {
  19. Options *Options
  20. }
  21. // Create a new raid manager
  22. func NewRaidManager(options Options) (*Manager, error) {
  23. //Check if platform is supported
  24. if runtime.GOOS != "linux" {
  25. return nil, errors.New("ArozOS do not support RAID management on this platform")
  26. }
  27. //Check if mdadm exists
  28. mdadmExists, err := apt.PackageExists("mdadm")
  29. if err != nil || !mdadmExists {
  30. return nil, errors.New("mdadm not installed on this host")
  31. }
  32. return &Manager{
  33. Options: &options,
  34. }, nil
  35. }
  36. // Create a virtual image partition at given path with given size
  37. func CreateVirtualPartition(imagePath string, totalSize int64) error {
  38. cmd := exec.Command("sudo", "dd", "if=/dev/zero", "of="+imagePath, "bs=4M", "count="+fmt.Sprintf("%dM", totalSize/(4*1024*1024)))
  39. cmd.Stdout = os.Stdout
  40. cmd.Stderr = os.Stderr
  41. err := cmd.Run()
  42. if err != nil {
  43. return fmt.Errorf("dd error: %v", err)
  44. }
  45. return nil
  46. }
  47. // Format the given image file
  48. func FormatVirtualPartition(imagePath string) error {
  49. //Check if image actually exists
  50. if !utils.FileExists(imagePath) {
  51. return errors.New("image file not exists")
  52. }
  53. if filepath.Ext(imagePath) != ".img" {
  54. return errors.New("given file is not an image path")
  55. }
  56. cmd := exec.Command("sudo", "mkfs.ext4", imagePath)
  57. cmd.Stdout = os.Stdout
  58. cmd.Stderr = os.Stderr
  59. err := cmd.Run()
  60. if err != nil {
  61. return fmt.Errorf("error running mkfs.ext4 command: %v", err)
  62. }
  63. return nil
  64. }