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