4
# ----------------------------------------------------------------------------------------------------------------------
5
# Linux MD (Soft)RAID Add Script - Add a (new) harddisk to another multi MD-array harddisk
6
# Last update: January 10, 2012
7
# (C) Copyright 2005-2012 by Arno van Amersfoort
8
# Homepage : http://rocky.eld.leidenuniv.nl/
9
# Email : a r n o v a AT r o c k y DOT e l d DOT l e i d e n u n i v DOT n l
10
# (note: you must remove all spaces and substitute the @ and the . at the proper locations!)
11
# ----------------------------------------------------------------------------------------------------------------------
12
# This program is free software; you can redistribute it and/or
13
# modify it under the terms of the GNU General Public License
14
# version 2 as published by the Free Software Foundation.
16
# This program is distributed in the hope that it will be useful,
17
# but WITHOUT ANY WARRANTY; without even the implied warranty of
18
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19
# GNU General Public License for more details.
21
# You should have received a copy of the GNU General Public License
22
# along with this program; if not, write to the Free Software
23
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24
# ----------------------------------------------------------------------------------------------------------------------
31
echo "Bad or missing parameter(s)"
32
echo "Usage: $(basename $0) [ options ] [ source_disk ] [ target_disk ]"
34
echo "--force = Even proceed if target device does not appear empty"
35
echo "--noptupdate = Do NOT update the partition table on the target device (EXPERT!)"
36
echo "--nombrupdate = Do NOT update the MBR boot-loader on the target device (EXPERT!)"
42
local DEVICE="$(echo "$1" |sed s,'^/dev/',, )"
44
if [ -z "$DEVICE" ]; then
45
cat /proc/partitions |sed -e '1,2d' -e s,' /dev/',,
47
cat /proc/partitions |sed -e '1,2d' -e s,' /dev/',, |grep -E " ${DEVICE}p?[0-9]+$"
53
get_partitions |grep -E " ${1}$" |awk '{ print $3 }'
58
if ! which "$1" >/dev/null 2>&1; then
59
printf "\033[40m\033[1;31mERROR: Binary \"$1\" does not exist or is not executable!\033[0m\n" >&2
60
printf "\033[40m\033[1;31m Please, make sure that it is (properly) installed!\033[0m\n" >&2
68
if [ "$(id -u)" != "0" ]; then
69
printf "\033[40m\033[1;31mERROR: Root check FAILED (you MUST be root to use this script)! Quitting...\n\033[0m" >&2
81
if [ -z "$SOURCE" ] || [ -z "$TARGET" ]; then
82
echo "ERROR: Bad or missing argument(s)" >&2
87
if ! echo "$SOURCE" |grep -q '^/dev/'; then
88
printf "\033[40m\033[1;31mERROR: Source device $SOURCE does not start with /dev/! Quitting...\n\033[0m" >&2
92
if ! echo "$TARGET" |grep -q '^/dev/'; then
93
printf "\033[40m\033[1;31mERROR: Target device $TARGET does not start with /dev/! Quitting...\n\033[0m" >&2
97
if echo "$SOURCE" |grep -q 'md[0-9]'; then
98
printf "\033[40m\033[1;31mERROR: The source device specified is an md-device! Quitting...\n\033[0m" >&2
99
echo "A physical drive (part of the md-array(s)) is required as source device (eg. /dev/sda)!" >&2
103
# We also want variables without /dev/ :
104
SOURCE_NODEV="$(echo "$SOURCE" |sed 's,^/dev/,,')"
105
TARGET_NODEV="$(echo "$TARGET" |sed 's,^/dev/,,')"
107
if [ -z "$(get_partitions ${SOURCE_NODEV})" ]; then
108
printf "\033[40m\033[1;31mERROR: Source device $SOURCE does not contain any partitions!? Quitting...\n\033[0m" >&2
112
if [ -n "$(get_partitions ${TARGET_NODEV})" ] && [ $FORCE -ne 1 ]; then
114
printf "\033[40m\033[1;31mERROR: Target device $TARGET already contains partitions! Use --force to override. Quitting...\n\033[0m" >&2
118
SOURCE_SIZE="$(get_part_size $SOURCE_NODEV)"
119
TARGET_SIZE="$(get_part_size $TARGET_NODEV)"
120
if [ $SOURCE_SIZE -gt $TARGET_SIZE ]; then
121
printf "\033[40m\033[1;31mWARNING: Target device $TARGET ($TARGET_SIZE blocks) is smaller than source device $SOURCE ($SOURCE_SIZE blocks)\nPress enter to continue or CTRL-C to abort...\n\033[0m" >&2
125
echo "--> Saving mdadm detail scan to /tmp/mdadm-detail-scan..."
126
mdadm --detail --scan --verbose >|/tmp/mdadm-detail-scan
128
if [ $retval -ne 0 ]; then
129
printf "\033[40m\033[1;31mERROR: mdadm returned an error($retval) while determining detail information!\n\033[0m" >&2
133
echo "--> Saving partition table of target device $TARGET to /tmp/partitions.target..."
134
sfdisk -d "$TARGET" >|"/tmp/partitions.target" 2>/dev/null
136
if [ $retval -ne 0 ]; then
137
echo "NOTE: sfdisk returned an error($retval) while reading the partition table on $TARGET"
140
echo "--> Saving partition table of source device $SOURCE to /tmp/partitions.source..."
141
sfdisk -d "$SOURCE" >|"/tmp/partitions.source"
143
if [ $retval -ne 0 ]; then
144
printf "\033[40m\033[1;31mERROR: sfdisk returned an error($retval) while reading the partition table on $SOURCE!\n\033[0m" >&2
148
echo "--> Checking status of running MDs..."
151
for MDSTAT_LINE in `cat /proc/mdstat`; do
152
if echo "$MDSTAT_LINE" |grep -q '^md'; then
153
MD_DEV_LINE="$MDSTAT_LINE"
154
MD_DEV="$(echo "$MDSTAT_LINE" |awk '{ print $1 }')"
157
for part_nodev in `cat "/tmp/partitions.target" |grep '^/dev/' |grep -i -v 'Id= 0' |awk '{ print $1 }' |sed 's,^/dev/,,'`; do
158
if echo "$MD_DEV_LINE" |grep -E -q "[[:blank:]]$part_nodev\["; then
159
printf "\033[40m\033[1;31mERROR: Partition /dev/$part_nodev on target device is already in use by array /dev/$MD_DEV!\n\033[0m" >&2
165
if echo "$MDSTAT_LINE" |grep -E -q '[[:blank:]]blocks[[:blank:]]' && ! echo "$MDSTAT_LINE" |grep -q '_'; then
166
# This array is NOT degraded so now check whether we want to add devices to it:
169
for part_nodev in `cat "/tmp/partitions.source" |grep '^/dev/' |grep -i -v 'Id= 0' |awk '{ print $1 }' |sed 's,^/dev/,,'`; do
170
if echo "$MD_DEV_LINE" |grep -E -q "[[:blank:]]$part_nodev\["; then
171
printf "$MD_DEV_LINE\n$MDSTAT_LINE\n"
172
printf "\033[40m\033[1;31mWARNING: Array $MD_DEV is NOT degraded, target device ${TARGET}$(echo "$part_nodev" |sed "s,$SOURCE_NODEV,,") will become a hotspare!\nPress enter to continue or CTRL-C to abort...\n\033[0m" >&2
181
# Wrapper for partprobe (call when performing a partition table update with eg. fdisk/sfdisk).
182
# $1 = Device to re-read
188
printf "(Re)reading partition table on $DEVICE"
190
# Retry several times since some daemons can block the re-reread for a while (like dm/lvm or blkid)
191
for x in `seq 1 10`; do
193
result=`sfdisk -R "$DEVICE" 2>&1`
195
# Wait a bit for things to settle
198
if [ -z "$result" ]; then
205
if [ -n "$result" ]; then
213
# Program entry point
214
echo "MDadd for SoftRAID-MDADM v$MY_VERSION"
215
echo "Written by Arno van Amersfoort"
216
echo "--------------------------------"
218
# Set environment variables to default
228
ARGNAME="$(echo "$arg" |cut -d= -f1)"
229
ARGVAL="$(echo "$arg" |cut -d= -f2)"
231
if ! echo "$ARGNAME" |grep -q '^-'; then
232
if [ -z "$SOURCE" ]; then
235
if [ -z "$TARGET" ]; then
244
--force|-force|-f) FORCE=1;;
245
--noptupdate|-noptupdate|--noptu|-noptu) NOPTUPDATE=1;;
246
--nombrupdate|-nombrupdate|--nombru|nombru) NOMBRUPDATE=1;;
249
*) echo "ERROR: Bad argument: $ARGNAME";
256
# Make sure everything is sane:
259
# Disable all swaps on target disk
260
echo "--> Disabling any swap partitions on target device $TARGET"
262
for SWAP in `grep -E "^${TARGET}p?[0-9]+" /proc/swaps |awk '{ print $1 }'`; do
263
swapoff $SWAP >/dev/null 2>&1
266
# Update track0 on target disk
267
if [ $NOMBRUPDATE -ne 1 ]; then
268
echo "--> Copying track0(containing MBR) from $SOURCE to $TARGET..."
269
dd if="$SOURCE" of="$TARGET" bs=32768 count=1
271
if [ $retval -ne 0 ]; then
272
printf "\033[40m\033[1;31mERROR: dd returned an error($retval) while copying track0!\n\033[0m" >&2
277
PT_FILE="/tmp/partitions.source"
278
if [ $NOPTUPDATE -eq 1 ]; then
279
PT_FILE="/tmp/partitions.target"
282
echo "--> Restoring partition table from $PT_FILE to $TARGET..."
283
sfdisk --no-reread --force "$TARGET" < "$PT_FILE"
285
if [ $retval -ne 0 ]; then
286
printf "\033[40m\033[1;31mERROR: sfdisk returned an error($retval) while writing the partition table!\n\033[0m" >&2
292
if [ $NOPTUPDATE -ne 1 ]; then
293
# Re-read partition table
296
if [ $retval -ne 0 ]; then
297
printf "\033[40m\033[1;31mERROR: (Re)reading the partition table failed($retval)!\n\033[0m" >&2
302
# Copy/build all md devices that exist on the source drive:
306
for LINE in `cat /tmp/mdadm-detail-scan`; do
307
if echo "$LINE" |grep -E -q '^ARRAY[[:blank:]]'; then
308
MD_DEV=$(echo "$LINE" |awk '{ print $2 }')
311
if echo "$LINE" |grep -E -q "devices=.*${SOURCE}p?[0-9]+"; then
314
for item in `echo "$LINE" |sed -e "s:.*devices=::"`; do
315
if echo "$item" |grep -E -q -x "${SOURCE}p?[0-9]+"; then
316
PARTITION_NR=`echo "$item" |sed s:"$SOURCE"::`
321
if [ -z "$PARTITION_NR" ]; then
322
printf "\033[40m\033[1;31mERROR: Unable to retrieve detail information for $SOURCE from $MD_DEV!\n\033[0m" >&2
326
# Check whether we're a root or boot partition
327
if grep -E -q -e "^$MD_DEV[[:blank:]]*/boot[[:blank:]]" -e "$MD_DEV[[:blank:]]*/[[:blank:]]" /etc/fstab; then
333
echo "--> Adding ${TARGET}${PARTITION_NR} to RAID array $MD_DEV:"
334
printf "\033[40m\033[1;31m"
335
mdadm --add "$MD_DEV" "${TARGET}${PARTITION_NR}"
337
if [ $retval -ne 0 ]; then
338
printf "\033[40m\033[1;31mERROR: mdadm returned an error($retval) while adding device!\n\033[0m" >&2
347
# Create swapspace on partitions with ID=82
348
echo "--> Creating swapspace on target device (if any swap partitions exist)..."
350
for SWAP_DEVICE in `sfdisk -d "$TARGET" 2>/dev/null |grep -i 'Id=82$' |awk '{ print $1 }'`; do
351
if ! mkswap "$SWAP_DEVICE"; then
352
printf "\033[40m\033[1;31mWARNING: mkswap failed for $SWAP_DEVICE\n\033[0m" >&2
354
swapon "$SWAP_DEVICE"
357
if ! grep -E -q "^$SWAP_DEVICE[[:blank:]]*none[[:blank:]]*swap[[:blank:]]" /etc/fstab; then
358
printf "\033[40m\033[1;31mWARNING: /etc/fstab does NOT contain a (valid) swap entry for $SWAP_DEVICE\n\033[0m" >&2
362
# Wait a bit for mdstat to settle
365
echo "--> Showing current /proc/mdstat (you may need to update your mdadm.conf (manually)..."
369
if [ $NO_ADD -eq 1 ]; then
370
printf "\033[40m\033[1;31mWARNING: No mdadm --add actions were performed, please investigate!\n\033[0m" >&2
373
if [ $BOOT -eq 1 ]; then
374
printf "\033[40m\033[1;31mNOTE: Boot and/or root partition detected.\n Please check your bootloader & active partitions!\n\033[0m"