Browse Source

Added wip mdadm config

Toby Chui 1 year ago
parent
commit
077622e806
3 changed files with 64 additions and 19 deletions
  1. 35 17
      mod/disk/raid/mdadm.go
  2. 7 2
      mod/disk/raid/raid_test.go
  3. 22 0
      mod/disk/raid/raidutils.go

+ 35 - 17
mod/disk/raid/mdadm.go

@@ -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]
 }

+ 7 - 2
mod/disk/raid/raid_test.go

@@ -9,10 +9,13 @@ package raid_test
 
 import (
 	"testing"
-
-	"imuslab.com/arozos/mod/disk/raid"
 )
 
+func TestAddRAIDToConfig(t *testing.T) {
+	//UpdateMDADMConfig()
+}
+
+/*
 func TestReadRAIDInfo(t *testing.T) {
 	raidInfo, err := raid.GetRAIDInfo("/dev/md0")
 	if err != nil {
@@ -24,6 +27,8 @@ func TestReadRAIDInfo(t *testing.T) {
 	raidInfo.PrettyPrintRAIDInfo()
 }
 
+*/
+
 /*
 func TestCreateRAIDDevice(t *testing.T) {
 	//Create an empty Manager

+ 22 - 0
mod/disk/raid/raidutils.go

@@ -4,6 +4,7 @@ import (
 	"bufio"
 	"fmt"
 	"os"
+	"os/exec"
 	"strings"
 )
 
@@ -19,6 +20,27 @@ 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
+}
+
+// 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")