|
@@ -4,6 +4,7 @@ import (
|
|
|
"fmt"
|
|
|
"os"
|
|
|
"os/exec"
|
|
|
+ "regexp"
|
|
|
"strconv"
|
|
|
"strings"
|
|
|
)
|
|
@@ -90,7 +91,7 @@ func (m *Manager) GetDiskUUIDByPath(devicePath string) (string, error) {
|
|
|
}
|
|
|
|
|
|
// CreateRAIDDevice creates a RAID device using the mdadm command.
|
|
|
-func (m *Manager) CreateRAIDDevice(devName string, raidLevel int, raidDeviceIds []string, spareDeviceIds []string) error {
|
|
|
+func (m *Manager) CreateRAIDDevice(devName string, raidName string, raidLevel int, raidDeviceIds []string, spareDeviceIds []string) error {
|
|
|
//Calculate the size of the raid devices
|
|
|
raidDev := len(raidDeviceIds)
|
|
|
spareDevice := len(spareDeviceIds)
|
|
@@ -110,8 +111,8 @@ func (m *Manager) CreateRAIDDevice(devName string, raidLevel int, raidDeviceIds
|
|
|
allDeviceIds := append(raidDeviceIds, spareDeviceIds...)
|
|
|
|
|
|
// Build the mdadm command
|
|
|
- cmd := exec.Command("bash", "-c", fmt.Sprintf("yes | sudo mdadm --create %s --level=%d --raid-devices=%d --spare-devices=%d %s",
|
|
|
- devName, raidLevel, raidDev, spareDevice, strings.Join(allDeviceIds, " ")))
|
|
|
+ cmd := exec.Command("bash", "-c", fmt.Sprintf("yes | sudo mdadm --create %s --name %s --level=%d --raid-devices=%d --spare-devices=%d %s",
|
|
|
+ devName, raidName, raidLevel, raidDev, spareDevice, strings.Join(allDeviceIds, " ")))
|
|
|
|
|
|
cmd.Stdout = os.Stdout
|
|
|
cmd.Stderr = os.Stderr
|
|
@@ -124,22 +125,39 @@ func (m *Manager) CreateRAIDDevice(devName string, raidLevel int, raidDeviceIds
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
-// 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)
|
|
|
- }
|
|
|
+// Updates the mdadm configuration file with the details of RAID arrays
|
|
|
+// so the RAID drive will still be seen after a reboot (hopefully)
|
|
|
+func UpdateMDADMConfig() error {
|
|
|
+ cmdMdadm := exec.Command("sudo", "mdadm", "--detail", "--scan", "--verbose")
|
|
|
+
|
|
|
+ // Run the command and capture its output
|
|
|
+ output, err := cmdMdadm.Output()
|
|
|
+ if err != nil {
|
|
|
+ return fmt.Errorf("error running mdadm command: %v", 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)
|
|
|
+ arrayConfigs := strings.TrimSpace(string(output))
|
|
|
+
|
|
|
+ lines := strings.Split(arrayConfigs, "\n")
|
|
|
+ //TODO: Add line filter for 2nd line appending
|
|
|
+ for _, line := range lines {
|
|
|
+ //For each line, you should have something like this
|
|
|
+ //ARRAY /dev/md0 metadata=1.2 name=debian:0 UUID=cbc11a2b:fbd42653:99c1340b:9c4962fb
|
|
|
+ // devices=/dev/sdb,/dev/sdc
|
|
|
+ fields := strings.Fields(line)
|
|
|
+
|
|
|
}
|
|
|
|
|
|
- return nil
|
|
|
+ //Extract the UUID from the output
|
|
|
+ // Define a regular expression to match the UUID pattern
|
|
|
+ uuidRegex := regexp.MustCompile(`UUID=([0-9a-fA-F:]+)`)
|
|
|
+
|
|
|
+ // Find the first match of the UUID pattern in the output string
|
|
|
+ matches := uuidRegex.FindStringSubmatch(output)
|
|
|
+ if len(matches) < 2 {
|
|
|
+ return "", fmt.Errorf("UUID not found in mdadm output")
|
|
|
+ }
|
|
|
+
|
|
|
+ // Extract the UUID from the matched string
|
|
|
+ uuid := matches[1]
|
|
|
}
|