1212# - Detects and reports existing RAID configurations
1313# - Thoroughly cleans target drives of any existing data/configurations
1414# - Creates GPT partition tables with RAID-type partitions
15- # - Establishes RAID-1 array (/dev/md0 ) for data redundancy
15+ # - Establishes RAID-1 array (${RAID_DEVICE} ) for data redundancy
1616# - Formats the array with XFS filesystem for performance
1717# - Automatically mounts at /nsm and configures for boot persistence
1818# - Provides monitoring information for resync operations
3030#
3131# WARNING: This script will DESTROY all data on the target drives!
3232#
33- # USAGE: sudo ./raid_setup .sh
33+ # USAGE: sudo ./so-nvme-raid1 .sh
3434#
3535# ################################################################
3636
3737# Exit on any error
3838set -e
3939
40+ # Configuration variables
41+ RAID_ARRAY_NAME=" md0"
42+ RAID_DEVICE=" /dev/${RAID_ARRAY_NAME} "
43+ MOUNT_POINT=" /nsm"
44+
4045# Function to log messages
4146log () {
4247 echo " [$( date ' +%Y-%m-%d %H:%M:%S' ) ] $1 "
@@ -50,50 +55,159 @@ check_root() {
5055 fi
5156}
5257
58+ # Function to find MD arrays using specific devices
59+ find_md_arrays_using_devices () {
60+ local target_devices=(" $@ " )
61+ local found_arrays=()
62+
63+ # Parse /proc/mdstat to find arrays using our target devices
64+ if [ -f " /proc/mdstat" ]; then
65+ while IFS= read -r line; do
66+ if [[ $line =~ ^(md[0-9]+) ]]; then
67+ local array_name=" ${BASH_REMATCH[1]} "
68+ local array_path=" /dev/$array_name "
69+
70+ # Check if this array uses any of our target devices
71+ for device in " ${target_devices[@]} " ; do
72+ if echo " $line " | grep -q " ${device##*/ } " ; then
73+ found_arrays+=(" $array_path " )
74+ break
75+ fi
76+ done
77+ fi
78+ done < /proc/mdstat
79+ fi
80+
81+ printf ' %s\n' " ${found_arrays[@]} "
82+ }
83+
5384# Function to check if RAID is already set up
5485check_existing_raid () {
55- if [ -e " /dev/md0" ]; then
56- if mdadm --detail /dev/md0 & > /dev/null; then
57- local raid_state=$( mdadm --detail /dev/md0 | grep " State" | awk ' {print $3}' )
58- local mount_point=" /nsm"
59-
60- log " Found existing RAID array /dev/md0 (State: $raid_state )"
61-
62- if mountpoint -q " $mount_point " ; then
63- log " RAID is already mounted at $mount_point "
64- log " Current RAID details:"
65- mdadm --detail /dev/md0
86+ local target_devices=(" /dev/nvme0n1p1" " /dev/nvme1n1p1" )
87+ local found_arrays=($( find_md_arrays_using_devices " ${target_devices[@]} " ) )
88+
89+ # Check if we found any arrays using our target devices
90+ if [ ${# found_arrays[@]} -gt 0 ]; then
91+ for array_path in " ${found_arrays[@]} " ; do
92+ if mdadm --detail " $array_path " & > /dev/null; then
93+ local raid_state=$( mdadm --detail " $array_path " | grep " State" | awk ' {print $3}' )
94+ local mount_point=" /nsm"
6695
67- # Check if resyncing
68- if grep -q " resync" /proc/mdstat; then
69- log " RAID is currently resyncing:"
70- grep resync /proc/mdstat
71- log " You can monitor progress with: watch -n 60 cat /proc/mdstat"
72- else
73- log " RAID is fully synced and operational"
74- fi
96+ log " Found existing RAID array $array_path (State: $raid_state )"
7597
76- # Show disk usage
77- log " Current disk usage:"
78- df -h " $mount_point "
98+ # Check what's currently mounted at /nsm
99+ local current_mount=$( findmnt -n -o SOURCE " $mount_point " 2> /dev/null || echo " " )
79100
80- exit 0
101+ if [ -n " $current_mount " ]; then
102+ if [ " $current_mount " = " $array_path " ]; then
103+ log " RAID array $array_path is already correctly mounted at $mount_point "
104+ log " Current RAID details:"
105+ mdadm --detail " $array_path "
106+
107+ # Check if resyncing
108+ if grep -q " resync" /proc/mdstat; then
109+ log " RAID is currently resyncing:"
110+ grep resync /proc/mdstat
111+ log " You can monitor progress with: watch -n 60 cat /proc/mdstat"
112+ else
113+ log " RAID is fully synced and operational"
114+ fi
115+
116+ # Show disk usage
117+ log " Current disk usage:"
118+ df -h " $mount_point "
119+
120+ exit 0
121+ else
122+ log " Found $mount_point mounted on $current_mount , but RAID array $array_path exists"
123+ log " Will unmount current filesystem and remount on RAID array"
124+
125+ # Unmount current filesystem
126+ log " Unmounting $mount_point "
127+ umount " $mount_point "
128+
129+ # Remove old fstab entry
130+ log " Removing old fstab entry for $current_mount "
131+ sed -i " \|$current_mount |d" /etc/fstab
132+
133+ # Mount the RAID array
134+ log " Mounting RAID array $array_path at $mount_point "
135+ mount " $array_path " " $mount_point "
136+
137+ # Update fstab
138+ log " Updating fstab for RAID array"
139+ sed -i " \|${array_path} |d" /etc/fstab
140+ echo " ${array_path} ${mount_point} xfs defaults,nofail 0 0" >> /etc/fstab
141+
142+ log " RAID array is now mounted at $mount_point "
143+ log " Current RAID details:"
144+ mdadm --detail " $array_path "
145+
146+ # Check if resyncing
147+ if grep -q " resync" /proc/mdstat; then
148+ log " RAID is currently resyncing:"
149+ grep resync /proc/mdstat
150+ log " You can monitor progress with: watch -n 60 cat /proc/mdstat"
151+ else
152+ log " RAID is fully synced and operational"
153+ fi
154+
155+ # Show disk usage
156+ log " Current disk usage:"
157+ df -h " $mount_point "
158+
159+ exit 0
160+ fi
161+ else
162+ # /nsm not mounted, mount the RAID array
163+ log " Mounting RAID array $array_path at $mount_point "
164+ mount " $array_path " " $mount_point "
165+
166+ # Update fstab
167+ log " Updating fstab for RAID array"
168+ sed -i " \|${array_path} |d" /etc/fstab
169+ echo " ${array_path} ${mount_point} xfs defaults,nofail 0 0" >> /etc/fstab
170+
171+ log " RAID array is now mounted at $mount_point "
172+ log " Current RAID details:"
173+ mdadm --detail " $array_path "
174+
175+ # Show disk usage
176+ log " Current disk usage:"
177+ df -h " $mount_point "
178+
179+ exit 0
180+ fi
81181 fi
82- fi
182+ done
83183 fi
84184
85185 # Check if any of the target devices are in use
86- for device in " /dev/nvme0n1" " /dev/nvme1n1" ; do
87- if lsblk -o NAME,MOUNTPOINT " $device " | grep -q " nsm" ; then
88- log " Error: $device is already mounted at /nsm"
89- exit 1
90- fi
91-
186+ for device in " /dev/nvme0n1" " /dev/nvme1n1" ; do
92187 if mdadm --examine " $device " & > /dev/null || mdadm --examine " ${device} p1" & > /dev/null; then
188+ # Find the actual array name for this device
189+ local device_arrays=($( find_md_arrays_using_devices " ${device} p1" ) )
190+ local array_name=" "
191+
192+ if [ ${# device_arrays[@]} -gt 0 ]; then
193+ array_name=" ${device_arrays[0]} "
194+ else
195+ # Fallback: try to find array name from /proc/mdstat
196+ local partition_name=" ${device##*/ } p1"
197+ array_name=$( grep -l " $partition_name " /proc/mdstat 2> /dev/null | head -1)
198+ if [ -n " $array_name " ]; then
199+ array_name=$( grep " ^md[0-9]" /proc/mdstat | grep " $partition_name " | awk ' {print "/dev/" $1}' | head -1)
200+ fi
201+ # Final fallback
202+ if [ -z " $array_name " ]; then
203+ array_name=" $RAID_DEVICE "
204+ fi
205+ fi
206+
93207 log " Error: $device appears to be part of an existing RAID array"
94208 log " To reuse this device, you must first:"
95209 log " 1. Unmount any filesystems"
96- log " 2. Stop the RAID array: mdadm --stop /dev/md0 "
210+ log " 2. Stop the RAID array: mdadm --stop $array_name "
97211 log " 3. Zero the superblock: mdadm --zero-superblock ${device} p1"
98212 exit 1
99213 fi
@@ -183,20 +297,20 @@ main() {
183297 fi
184298
185299 log " Creating RAID array"
186- mdadm --create /dev/md0 --level=1 --raid-devices=2 \
300+ mdadm --create " $RAID_DEVICE " --level=1 --raid-devices=2 \
187301 --metadata=1.2 \
188302 /dev/nvme0n1p1 /dev/nvme1n1p1 \
189303 --force --run
190304
191305 log " Creating XFS filesystem"
192- mkfs.xfs -f /dev/md0
306+ mkfs.xfs -f " $RAID_DEVICE "
193307
194308 log " Creating mount point"
195309 mkdir -p /nsm
196310
197311 log " Updating fstab"
198- sed -i ' /\/dev\/md0/d ' /etc/fstab
199- echo " /dev/md0 /nsm xfs defaults,nofail 0 0" >> /etc/fstab
312+ sed -i " \| ${RAID_DEVICE} |d " /etc/fstab
313+ echo " ${RAID_DEVICE} ${MOUNT_POINT} xfs defaults,nofail 0 0" >> /etc/fstab
200314
201315 log " Reloading systemd daemon"
202316 systemctl daemon-reload
@@ -209,7 +323,7 @@ main() {
209323
210324 log " RAID setup complete"
211325 log " RAID array details:"
212- mdadm --detail /dev/md0
326+ mdadm --detail " $RAID_DEVICE "
213327
214328 if grep -q " resync" /proc/mdstat; then
215329 log " RAID is currently resyncing. You can monitor progress with:"
0 commit comments