|
@@ -1,13 +1,14 @@
|
|
|
package raid
|
|
|
|
|
|
import (
|
|
|
- "bufio"
|
|
|
"errors"
|
|
|
"fmt"
|
|
|
"os"
|
|
|
"os/exec"
|
|
|
"path/filepath"
|
|
|
"strings"
|
|
|
+
|
|
|
+ "imuslab.com/arozos/mod/disk/diskfs"
|
|
|
)
|
|
|
|
|
|
// Get the next avaible RAID array name
|
|
@@ -22,29 +23,9 @@ func GetNextAvailableMDDevice() (string, error) {
|
|
|
return "", fmt.Errorf("no available /dev/mdX devices found")
|
|
|
}
|
|
|
|
|
|
-// DANGER: Wipe the whole disk given the disk path
|
|
|
-func (m *Manager) WipeDisk(diskPath string) error {
|
|
|
- // Unmount the disk
|
|
|
- isMounted, _ := DeviceIsMounted(diskPath)
|
|
|
- if isMounted {
|
|
|
- umountCmd := exec.Command("sudo", "umount", diskPath)
|
|
|
- if err := umountCmd.Run(); err != nil {
|
|
|
- return fmt.Errorf("error unmounting disk %s: %v", diskPath, err)
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- // Wipe all filesystem signatures on the entire disk
|
|
|
- wipeCmd := exec.Command("sudo", "wipefs", "--all", "--force", diskPath)
|
|
|
- if err := wipeCmd.Run(); err != nil {
|
|
|
- return fmt.Errorf("error wiping filesystem signatures on %s: %v", diskPath, err)
|
|
|
- }
|
|
|
-
|
|
|
- return nil
|
|
|
-}
|
|
|
-
|
|
|
// ClearSuperblock clears the superblock of the specified disk so it can be used safely
|
|
|
func (m *Manager) ClearSuperblock(devicePath string) error {
|
|
|
- isMounted, err := DeviceIsMounted(devicePath)
|
|
|
+ isMounted, err := diskfs.DeviceIsMounted(devicePath)
|
|
|
if err != nil {
|
|
|
return errors.New("unable to validate if the device is unmounted: " + err.Error())
|
|
|
}
|
|
@@ -61,30 +42,6 @@ func (m *Manager) ClearSuperblock(devicePath string) error {
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
-// Check if a device is mounted given the path name, like /dev/sdc
|
|
|
-func DeviceIsMounted(devicePath string) (bool, error) {
|
|
|
- // Open the mountinfo file
|
|
|
- file, err := os.Open("/proc/mounts")
|
|
|
- if err != nil {
|
|
|
- return false, fmt.Errorf("error opening /proc/mounts: %v", err)
|
|
|
- }
|
|
|
- defer file.Close()
|
|
|
-
|
|
|
- // Scan the mountinfo file line by line
|
|
|
- scanner := bufio.NewScanner(file)
|
|
|
- for scanner.Scan() {
|
|
|
- line := scanner.Text()
|
|
|
- fields := strings.Fields(line)
|
|
|
- if len(fields) >= 2 && fields[0] == devicePath {
|
|
|
- // Device is mounted
|
|
|
- return true, nil
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- // Device is not mounted
|
|
|
- return false, nil
|
|
|
-}
|
|
|
-
|
|
|
// Use to restart any not-removed RAID device
|
|
|
func (m *Manager) RestartRAIDService() error {
|
|
|
cmd := exec.Command("sudo", "mdadm", "--assemble", "--scan")
|
|
@@ -130,21 +87,6 @@ func (m *Manager) RemoveRAIDMember(devicePath string) error {
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
-// UnmountDevice unmounts the specified device.
|
|
|
-// Remember to use full path (e.g. /dev/md0) in the devicePath
|
|
|
-func UnmountDevice(devicePath string) error {
|
|
|
- // Construct the bash command to unmount the device
|
|
|
- cmd := exec.Command("sudo", "bash", "-c", fmt.Sprintf("umount %s", devicePath))
|
|
|
-
|
|
|
- // Run the command
|
|
|
- err := cmd.Run()
|
|
|
- if err != nil {
|
|
|
- return fmt.Errorf("error unmounting device: %v", err)
|
|
|
- }
|
|
|
-
|
|
|
- return nil
|
|
|
-}
|
|
|
-
|
|
|
// IsValidRAIDLevel checks if the given RAID level is valid.
|
|
|
func IsValidRAIDLevel(level string) bool {
|
|
|
// List of valid RAID levels
|