This commit is contained in:
despiegk 2025-06-15 17:21:36 +02:00
parent c84c5007eb
commit 6767e2b4e4

View File

@ -82,6 +82,113 @@ list_ssd_disks() {
AVAILABLE_DISKS=("${disks[@]}")
}
# Check for RAID arrays and stop them
check_and_stop_raid() {
log "Checking for active RAID arrays..."
if [[ ! -f /proc/mdstat ]]; then
log "No RAID support detected"
return 0
fi
# Get list of active RAID devices
local raid_devices=()
while IFS= read -r line; do
if [[ "$line" =~ ^(md[0-9]+) ]]; then
raid_devices+=("/dev/${BASH_REMATCH[1]}")
fi
done < /proc/mdstat
if [[ ${#raid_devices[@]} -eq 0 ]]; then
log "No active RAID arrays found"
return 0
fi
log "Found active RAID arrays:"
for raid in "${raid_devices[@]}"; do
echo " - $raid"
done
# Check if any RAID devices are mounted
log "Checking for mounted RAID filesystems..."
for raid in "${raid_devices[@]}"; do
if mountpoint -q "$raid" 2>/dev/null; then
log "Unmounting $raid"
umount "$raid" 2>/dev/null || warn "Failed to unmount $raid"
fi
done
# Stop RAID arrays
log "Stopping RAID arrays..."
for raid in "${raid_devices[@]}"; do
log "Stopping $raid"
if mdadm --stop "$raid" 2>/dev/null; then
log "Successfully stopped $raid"
else
warn "Failed to stop $raid"
fi
done
# Wait a moment for arrays to fully stop
sleep 2
# Verify arrays are stopped
local remaining_arrays=()
while IFS= read -r line; do
if [[ "$line" =~ ^(md[0-9]+) ]]; then
remaining_arrays+=("/dev/${BASH_REMATCH[1]}")
fi
done < /proc/mdstat
if [[ ${#remaining_arrays[@]} -gt 0 ]]; then
warn "Some RAID arrays are still active:"
for raid in "${remaining_arrays[@]}"; do
echo " - $raid"
done
return 1
else
log "All RAID arrays successfully stopped"
return 0
fi
}
# Remove RAID superblocks from disk partitions
remove_raid_superblocks() {
local disk="$1"
log "Checking for RAID superblocks on $disk..."
# Get all partitions on this disk
local partitions=()
while IFS= read -r partition; do
if [[ -n "$partition" ]]; then
partitions+=("/dev/$partition")
fi
done < <(lsblk -ln -o NAME "$disk" | tail -n +2)
# Check each partition for RAID superblocks
for partition in "${partitions[@]}"; do
if mdadm --examine "$partition" &>/dev/null; then
log "Found RAID superblock on $partition, removing..."
if mdadm --zero-superblock "$partition" 2>/dev/null; then
log "Successfully removed RAID superblock from $partition"
else
warn "Failed to remove RAID superblock from $partition"
fi
fi
done
# Also check the whole disk for superblocks
if mdadm --examine "$disk" &>/dev/null; then
log "Found RAID superblock on $disk, removing..."
if mdadm --zero-superblock "$disk" 2>/dev/null; then
log "Successfully removed RAID superblock from $disk"
else
warn "Failed to remove RAID superblock from $disk"
fi
fi
}
# Function to erase a single disk
erase_disk() {
local disk="$1"
@ -93,6 +200,14 @@ erase_disk() {
log "Starting erasure of $disk..."
# Check if disk is part of any active RAID arrays and stop them if needed
local disk_basename=$(basename "$disk")
if grep -q "$disk_basename" /proc/mdstat 2>/dev/null; then
warn "Disk $disk appears to be part of an active RAID array"
log "Stopping RAID arrays that use this disk..."
check_and_stop_raid
fi
# Unmount any mounted partitions on this disk
log "Unmounting any mounted partitions on $disk..."
for partition in $(lsblk -ln -o NAME "$disk" | tail -n +2); do
@ -103,11 +218,19 @@ erase_disk() {
fi
done
# Remove RAID superblocks if present
remove_raid_superblocks "$disk"
# Get disk size for progress indication
local disk_size_bytes=$(blockdev --getsize64 "$disk" 2>/dev/null || echo "0")
local disk_size_gb=$((disk_size_bytes / 1024 / 1024 / 1024))
log "Disk size: ${disk_size_gb}GB"
# Wipe partition table first
log "Wiping partition table on $disk..."
wipefs -af "$disk" 2>/dev/null || warn "Failed to wipe filesystem signatures"
log "Erasing first 1GB of $disk (this will destroy the partition table and filesystem headers)..."
# Use dd to zero out the first 1GB of the disk
@ -157,6 +280,10 @@ erase_all_disks() {
log "Starting disk erasure process..."
# First, stop any RAID arrays that might be using these disks
log "Checking and stopping RAID arrays before disk erasure..."
check_and_stop_raid
local success_count=0
local total_count=${#AVAILABLE_DISKS[@]}