Selaa lähdekoodia

Added raid config append and remove function

aroz 1 vuosi sitten
vanhempi
commit
6864195195
3 muutettua tiedostoa jossa 57 lisäystä ja 4 poistoa
  1. 39 2
      mod/disk/raid/mdadm.go
  2. 17 1
      mod/disk/raid/raid_test.go
  3. 1 1
      mod/disk/raid/raidutils.go

+ 39 - 2
mod/disk/raid/mdadm.go

@@ -2,6 +2,7 @@ package raid
 
 import (
 	"fmt"
+	"log"
 	"os"
 	"os/exec"
 	"strconv"
@@ -126,6 +127,8 @@ func (m *Manager) CreateRAIDDevice(devName string, raidName string, raidLevel in
 
 // Updates the mdadm configuration file with the details of RAID arrays
 // so the RAID drive will still be seen after a reboot (hopefully)
+// this function will not remove any record from the config file. If you need to remove
+// a RAID volume from config, call to RemoveVolmeFromMDADMConfig()
 func UpdateMDADMConfig() error {
 	cmdMdadm := exec.Command("sudo", "mdadm", "--detail", "--scan", "--verbose")
 
@@ -153,6 +156,9 @@ func UpdateMDADMConfig() error {
 
 		line = strings.ReplaceAll(line, "\n", " ")
 		fields := strings.Fields(line)
+		if len(fields) < 5 {
+			continue
+		}
 		poolUUID := strings.TrimPrefix(fields[3], "UUID=")
 
 		//Check if this uuid already in the config file
@@ -166,8 +172,39 @@ func UpdateMDADMConfig() error {
 
 	}
 
-	fmt.Println(newConfigLines)
+	if len(newConfigLines) == 0 {
+		//Nothing to write
+		log.Println("Nothing to write. Skipping mdadm config update.")
+		return nil
+	}
+
+	// Construct the bash command to append the line to mdadm.conf using echo and tee
+	for _, configLine := range newConfigLines {
+		cmd := exec.Command("bash", "-c", fmt.Sprintf(`echo "%s" | sudo tee -a /etc/mdadm/mdadm.conf`, configLine))
+
+		// Run the command
+		err := cmd.Run()
+		if err != nil {
+			return fmt.Errorf("error injecting line into mdadm.conf: %v", err)
+		}
+	}
+
+	return nil
+}
+
+// Removes a RAID volume from the mdadm configuration file given its volume UUID.
+// Note that this only remove a single line of config. If your line consists of multiple lines
+// you might need to remove it manually
+func RemoveVolumeFromMDADMConfig(volumeUUID string) error {
+	// Construct the sed command to remove the line containing the volume UUID from mdadm.conf
+	sedCommand := fmt.Sprintf(`sudo sed -i '/UUID=%s/d' /etc/mdadm/mdadm.conf`, volumeUUID)
+
+	// Execute the sed command
+	cmd := exec.Command("bash", "-c", sedCommand)
+	err := cmd.Run()
+	if err != nil {
+		return fmt.Errorf("error removing volume from mdadm.conf: %v", err)
+	}
 
-	//TODO: APPEND THE LINES INTO THE CONFIG FILE
 	return nil
 }

+ 17 - 1
mod/disk/raid/raid_test.go

@@ -9,10 +9,26 @@ package raid_test
 
 import (
 	"testing"
+
+	"imuslab.com/arozos/mod/disk/raid"
 )
 
+/*
+func TestRemoveRAIDFromConfig(t *testing.T) {
+	err := raid.RemoveVolumeFromMDADMConfig("cbc11a2b:fbd42653:99c1340b:9c4962fb")
+	if err != nil {
+		t.Errorf("Unexpected error: %v", err)
+		return
+	}
+}
+*/
+
 func TestAddRAIDToConfig(t *testing.T) {
-	//UpdateMDADMConfig()
+	err := raid.UpdateMDADMConfig()
+	if err != nil {
+		t.Errorf("Unexpected error: %v", err)
+		return
+	}
 }
 
 /*

+ 1 - 1
mod/disk/raid/raidutils.go

@@ -41,7 +41,7 @@ func (m *Manager) WipeDisk(diskPath string) error {
 	return nil
 }
 
-// ClearSuperblock clears the superblock of the specified disk.
+// 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)
 	if err != nil {