Sfoglia il codice sorgente

Added mounting check for disk wipe

Toby Chui 1 anno fa
parent
commit
60a0a03e7a
3 ha cambiato i file con 33 aggiunte e 3 eliminazioni
  1. 6 3
      mod/disk/raid/mdadm.go
  2. 2 0
      mod/disk/raid/raid_test.go
  3. 25 0
      mod/disk/raid/raidutils.go

+ 6 - 3
mod/disk/raid/mdadm.go

@@ -131,9 +131,12 @@ func (m *Manager) CreateRAIDDevice(devName string, raidLevel int, raidDeviceIds
 // DANGER: Wipe the whole disk given the disk path
 func (m *Manager) WipeDisk(diskPath string) error {
 	// Unmount the disk
-	umountCmd := exec.Command("sudo", "umount", diskPath)
-	if err := umountCmd.Run(); err != nil {
-		return fmt.Errorf("error unmounting disk %s: %v", diskPath, err)
+	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 each of the partitions

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

@@ -23,6 +23,7 @@ func TestCreateRAIDDevice(t *testing.T) {
 		err := manager.WipeDisk(partion)
 		if err != nil {
 			t.Errorf("Disk wipe error: %v", err)
+			return
 		}
 	}
 
@@ -30,6 +31,7 @@ func TestCreateRAIDDevice(t *testing.T) {
 	err := manager.CreateRAIDDevice(devName, raidLevel, raidDeviceIds, spareDeviceIds)
 	if err != nil {
 		t.Errorf("Unexpected error: %v", err)
+		return
 	}
 
 	fmt.Println("RAID array created")

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

@@ -1,8 +1,10 @@
 package raid
 
 import (
+	"bufio"
 	"fmt"
 	"os"
+	"strings"
 )
 
 // Get the next avaible RAID array name
@@ -16,3 +18,26 @@ func GetNextAvailableMDDevice() (string, error) {
 
 	return "", fmt.Errorf("no available /dev/mdX devices found")
 }
+
+func DeviceIsMounted(devicePath string) (bool, error) {
+	// Open the mountinfo file
+	file, err := os.Open("/proc/mounts")
+	if err != nil {
+		return false, fmt.Errorf("error opening /proc/mounts: %v", err)
+	}
+	defer file.Close()
+
+	// Scan the mountinfo file line by line
+	scanner := bufio.NewScanner(file)
+	for scanner.Scan() {
+		line := scanner.Text()
+		fields := strings.Fields(line)
+		if len(fields) >= 2 && fields[0] == devicePath {
+			// Device is mounted
+			return true, nil
+		}
+	}
+
+	// Device is not mounted
+	return false, nil
+}