#!/usr/bin/env bash set -euo pipefail # Script to create a RAID volume on r630-01 using sdc-sdh # For fast access, we'll use RAID 10 (mirrored stripes) for best performance + redundancy # or RAID 0 (striping) for maximum performance (no redundancy) set -u TARGET_NODE="r630-01" TARGET_NODE_IP="192.168.11.11" TARGET_NODE_PASS="password" # Disks to use for RAID DISKS=("sdc" "sdd" "sde" "sdf" "sdg" "sdh") RAID_LEVEL="${1:-10}" # Default to RAID 10, can override with argument (0, 5, 6, 10) # Colors GREEN='\033[0;32m' RED='\033[0;31m' YELLOW='\033[1;33m' BLUE='\033[0;34m' NC='\033[0m' log_info() { echo -e "${BLUE}[INFO]${NC} $1"; } log_success() { echo -e "${GREEN}[✓]${NC} $1"; } log_error() { echo -e "${RED}[✗]${NC} $1"; } log_warn() { echo -e "${YELLOW}[⚠]${NC} $1"; } ssh_r630_01() { sshpass -p "$TARGET_NODE_PASS" ssh -o StrictHostKeyChecking=no -o ConnectTimeout=10 root@"$TARGET_NODE_IP" "$@" 2>&1 } check_prerequisites() { log_info "Checking prerequisites..." # Check if mdadm is installed if ! ssh_r630_01 "which mdadm >/dev/null 2>&1"; then log_warn "mdadm is not installed. Installing..." ssh_r630_01 "apt-get update && apt-get install -y mdadm" || { log_error "Failed to install mdadm" return 1 } fi log_success "mdadm is available" # Check if disks exist for disk in "${DISKS[@]}"; do if ! ssh_r630_01 "test -b /dev/$disk"; then log_error "Disk /dev/$disk does not exist" return 1 fi done log_success "All disks exist" # Check if disks are in use log_info "Checking disk usage..." local in_use_disks=() for disk in "${DISKS[@]}"; do if ssh_r630_01 "pvs 2>/dev/null | grep -q /dev/$disk || mount | grep -q /dev/$disk"; then in_use_disks+=("$disk") fi done if [ ${#in_use_disks[@]} -gt 0 ]; then log_warn "The following disks are currently in use: ${in_use_disks[*]}" log_warn "sdc and sdd are part of the pve volume group" log_warn "You will need to migrate data off these disks before creating RAID" log_warn "This script will NOT proceed automatically to prevent data loss" return 1 fi return 0 } check_disk_status() { log_info "Checking disk status..." for disk in "${DISKS[@]}"; do local size=$(ssh_r630_01 "lsblk -bno SIZE /dev/$disk 2>/dev/null") local fstype=$(ssh_r630_01 "lsblk -no FSTYPE /dev/$disk 2>/dev/null") log_info " /dev/$disk: ${size} bytes, FSTYPE: ${fstype:-none}" done } create_raid() { log_info "Creating RAID $RAID_LEVEL array..." # Prepare disk list local disk_list="" for disk in "${DISKS[@]}"; do disk_list+="/dev/$disk " done log_info "Disks to use: $disk_list" # Create RAID array case "$RAID_LEVEL" in 0) log_info "Creating RAID 0 (striping) - Maximum performance, NO redundancy" log_warn "RAID 0 provides no fault tolerance. One disk failure will lose all data." ssh_r630_01 "mdadm --create /dev/md0 --level=0 --raid-devices=6 $disk_list" || { log_error "Failed to create RAID 0" return 1 } ;; 5) log_info "Creating RAID 5 - Good performance with single disk redundancy" ssh_r630_01 "mdadm --create /dev/md0 --level=5 --raid-devices=6 $disk_list" || { log_error "Failed to create RAID 5" return 1 } ;; 6) log_info "Creating RAID 6 - Good performance with double disk redundancy" ssh_r630_01 "mdadm --create /dev/md0 --level=6 --raid-devices=6 $disk_list" || { log_error "Failed to create RAID 6" return 1 } ;; 10) log_info "Creating RAID 10 (mirrored stripes) - Best performance + redundancy" log_info "RAID 10 requires even number of disks (6 disks = 3x capacity)" ssh_r630_01 "mdadm --create /dev/md0 --level=10 --raid-devices=6 $disk_list" || { log_error "Failed to create RAID 10" return 1 } ;; *) log_error "Invalid RAID level: $RAID_LEVEL" log_info "Supported levels: 0, 5, 6, 10" return 1 ;; esac log_success "RAID array created successfully" # Wait for array to sync log_info "Waiting for RAID array to initialize..." ssh_r630_01 "watch -n 1 'cat /proc/mdstat' &" & local watch_pid=$! # Monitor sync progress while true; do local sync_status=$(ssh_r630_01 "cat /proc/mdstat | grep -A 2 md0 | tail -1") if echo "$sync_status" | grep -q "\[UUUUUU\]\|\[UUUUU_\]"; then log_success "RAID array is fully synchronized" kill $watch_pid 2>/dev/null break elif echo "$sync_status" | grep -q "recovery\|resync"; then local progress=$(echo "$sync_status" | grep -oP '\d+\.\d+%' || echo "in progress") log_info "RAID sync progress: $progress" sleep 10 else sleep 5 fi done # Save RAID configuration log_info "Saving RAID configuration..." ssh_r630_01 "mdadm --detail --scan >> /etc/mdadm/mdadm.conf" || { log_warn "Failed to save to mdadm.conf, but RAID is created" } # Show RAID status log_info "RAID array status:" ssh_r630_01 "mdadm --detail /dev/md0" log_success "RAID array /dev/md0 is ready" } show_raid_info() { log_info "=== RAID Array Information ===" ssh_r630_01 "cat /proc/mdstat" echo "" ssh_r630_01 "mdadm --detail /dev/md0 2>/dev/null || echo 'RAID not created yet'" } main() { echo "" log_info "=== RAID Creation Script for R630-01 ===" log_info "Target: Create RAID $RAID_LEVEL using disks: ${DISKS[*]}" echo "" # Show current disk status check_disk_status echo "" # Check prerequisites if ! check_prerequisites; then log_error "Prerequisites check failed" log_info "Please review the warnings above and ensure disks are available" exit 1 fi # Show RAID info before creation show_raid_info echo "" # Confirm before proceeding log_warn "WARNING: This will destroy all data on the specified disks!" log_warn "Disks: ${DISKS[*]}" log_warn "RAID Level: $RAID_LEVEL" echo "" read -p "Do you want to proceed? (yes/no): " confirm if [ "$confirm" != "yes" ]; then log_info "Operation cancelled" exit 0 fi # Create RAID if create_raid; then log_success "RAID creation completed successfully!" log_info "Next steps:" log_info "1. Create filesystem: mkfs.ext4 /dev/md0 (or mkfs.xfs /dev/md0)" log_info "2. Create mount point: mkdir -p /mnt/raid-fast" log_info "3. Mount: mount /dev/md0 /mnt/raid-fast" log_info "4. Add to /etc/fstab for permanent mounting" log_info "5. Optionally add to Proxmox as storage" else log_error "RAID creation failed" exit 1 fi } main "$@"