raid.go 1.7 KB

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