Forráskód Böngészése

Added script to remove the device hardcode from mdadm conf

aroz 1 éve
szülő
commit
b28aca0294
2 módosított fájl, 38 hozzáadás és 1 törlés
  1. 14 0
      mod/disk/diskfs/diskfs.go
  2. 24 1
      mod/disk/raid/mdadmConf.go

+ 14 - 0
mod/disk/diskfs/diskfs.go

@@ -2,6 +2,7 @@ package diskfs
 
 import (
 	"bufio"
+	"bytes"
 	"encoding/json"
 	"errors"
 	"fmt"
@@ -145,6 +146,19 @@ func GetBlockDeviceMeta(devicePath string) (*BlockDeviceMeta, error) {
 	return nil, errors.New("target block device not found")
 }
 
+// Get the disk UUID by current device path (e.g. /dev/sda)
+func GetDiskUUID(devicePath string) (string, error) {
+	cmd := exec.Command("sudo", "blkid", "-s", "UUID", "-o", "value", devicePath)
+	var out bytes.Buffer
+	cmd.Stdout = &out
+	err := cmd.Run()
+	if err != nil {
+		return "", err
+	}
+	uuid := strings.TrimSpace(out.String())
+	return uuid, nil
+}
+
 // Get partition information (e.g. /dev/sdX1)
 func GetPartitionMeta(devicePath string) (*PartitionMeta, error) {
 	//Trim the /dev/ part of the device path

+ 24 - 1
mod/disk/raid/mdadmConf.go

@@ -62,6 +62,26 @@ func (m *Manager) FlushReload() error {
 	return nil
 }
 
+// removeDevicesEntry remove device hardcode from mdadm config file
+func removeDevicesEntry(configLine string) string {
+	// Split the config line by space character
+	tokens := strings.Fields(configLine)
+
+	// Iterate through the tokens to find and remove the devices=* part
+	for i, token := range tokens {
+		if strings.HasPrefix(token, "devices=") {
+			// Remove the devices=* part from the slice
+			tokens = append(tokens[:i], tokens[i+1:]...)
+			break
+		}
+	}
+
+	// Join the tokens back into a single string
+	updatedConfigLine := strings.Join(tokens, " ")
+
+	return updatedConfigLine
+}
+
 // Updates the mdadm configuration file with the details of RAID arrays
 // so the RAID drive will still be seen after a reboot (hopefully)
 // this will automatically add / remove config base on current runtime setup
@@ -105,8 +125,11 @@ func (m *Manager) UpdateMDADMConfig() error {
 		}
 
 		//This config not exists in the settings. Add it to append lines
-		log.Println("[RAID] Adding " + fields[0] + " (UUID=" + poolUUID + ") into mdadm config")
+		m.Options.Logger.PrintAndLog("RAID", "Adding "+fields[0]+" (UUID="+poolUUID+") into mdadm config", nil)
 		settingLine := "ARRAY " + strings.Join(fields, " ")
+
+		//Remove the device specific names
+		settingLine = removeDevicesEntry(settingLine)
 		newConfigLines = append(newConfigLines, settingLine)
 	}