1
From: Arno van Amersfoort <arnova@rocky.eld.leidenuniv.nl>
2
Subject: Add contributed mdadd script
4
The mdadd.sh script adds a harddisk to multiple MD-arrays, using an existing
5
harddisk as a template.
7
Origin: http://rocky.eld.leidenuniv.nl/scripts/mdadd.sh
8
Updated: Fri, 29 Jul 2011 15:30:45 +0200
9
Signed-off-by: martin f. krafft <madduck@debian.org>
12
contrib/mdadd.sh | 318 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
13
1 files changed, 318 insertions(+), 0 deletions(-)
15
diff --git a/contrib/mdadd.sh b/contrib/mdadd.sh
17
index 0000000..8613c11
19
+++ b/contrib/mdadd.sh
24
+# ----------------------------------------------------------------------------------------------------------------------
25
+# Linux MD (Soft)RAID Add Script - Add a (new) harddisk to another multi MD-array harddisk
26
+# Last update: March 9, 2010
27
+# (C) Copyright 2005-2010 by Arno van Amersfoort
28
+# Homepage : http://rocky.eld.leidenuniv.nl/
29
+# 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
30
+# (note: you must remove all spaces and substitute the @ and the . at the proper locations!)
31
+# ----------------------------------------------------------------------------------------------------------------------
32
+# This program is free software; you can redistribute it and/or
33
+# modify it under the terms of the GNU General Public License
34
+# version 2 as published by the Free Software Foundation.
36
+# This program is distributed in the hope that it will be useful,
37
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
38
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
39
+# GNU General Public License for more details.
41
+# You should have received a copy of the GNU General Public License
42
+# along with this program; if not, write to the Free Software
43
+# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
44
+# ----------------------------------------------------------------------------------------------------------------------
51
+ echo "Bad or missing parameter(s)"
52
+ echo "Usage: $(basename $0) [ options ] [ source_disk ] [ target_disk ]"
54
+ echo "--force = Even proceed if target device does not appear empty"
55
+ echo "--noptupdate = Do NOT update the partition table on the target device (EXPERT!)"
56
+ echo "--nombrupdate = Do NOT update the MBR boot-loader on the target device (EXPERT!)"
62
+ cat /proc/partitions |awk '{ print $NF }' |sed -e '1,2d' -e 's,^/dev/,,'
68
+ if ! which "$1" >/dev/null 2>&1; then
69
+ printf "\033[40m\033[1;31mERROR: Binary \"$1\" does not exist or is not executable!\033[0m\n" >&2
70
+ printf "\033[40m\033[1;31m Please, make sure that it is (properly) installed!\033[0m\n" >&2
78
+ if [ "$UID" != "0" ]; then
79
+ printf "\033[40m\033[1;31mERROR: Root check FAILED (you MUST be root to use this script)! Quitting...\n\033[0m"
92
+ if [ -z "$SOURCE" ] || [ -z "$TARGET" ]; then
93
+ echo "ERROR: Bad or missing argument(s)"
98
+ if ! echo "$SOURCE" |grep -q '^/dev/'; then
99
+ printf "\033[40m\033[1;31mERROR: Source device $SOURCE does not start with /dev/! Quitting...\n\033[0m"
103
+ if ! echo "$TARGET" |grep -q '^/dev/'; then
104
+ printf "\033[40m\033[1;31mERROR: Target device $TARGET does not start with /dev/! Quitting...\n\033[0m"
108
+ if echo "$SOURCE" |grep -q 'md[0-9]'; then
109
+ printf "\033[40m\033[1;31mERROR: The source device specified is an md-device! Quitting...\n\033[0m"
110
+ echo "A physical drive (part of the md-array('s)) is required as source device (eg. /dev/sda)!"
114
+ # We also want variables without /dev/ :
115
+ SOURCE_NODEV="$(echo "$SOURCE" |sed 's,^/dev/,,')"
116
+ TARGET_NODEV="$(echo "$TARGET" |sed 's,^/dev/,,')"
118
+ if ! get_partitions |grep -E -q -x "${SOURCE_NODEV}p?[0-9]+"; then
119
+ printf "\033[40m\033[1;31mERROR: Source device $SOURCE does not contain any partitions!? Quitting...\n\033[0m"
123
+ if get_partitions |grep -E -q -x "${TARGET_NODEV}p?[0-9]+" && [ "$FORCE" != "1" ]; then
125
+ printf "\033[40m\033[1;31mERROR: Target device $TARGET already contains partitions! Use --force to override. Quitting...\n\033[0m"
129
+ echo "--> Saving mdadm detail scan to /tmp/mdadm-detail-scan..."
130
+ mdadm --detail --scan --verbose >/tmp/mdadm-detail-scan
132
+ if [ "$retval" != "0" ]; then
133
+ printf "\033[40m\033[1;31mERROR: mdadm returned an error($retval) while determining detail information!\n\033[0m"
137
+ echo "--> Saving partition table of target device $TARGET to /tmp/partitions.target..."
138
+ sfdisk -q -d "$TARGET" >"/tmp/partitions.target"
140
+ if [ "$retval" != "0" ]; then
141
+ printf "\033[40m\033[1;31mERROR: sfdisk returned an error($retval) while reading the partition table on $TARGET!\n\033[0m"
145
+ echo "--> Saving partition table of source device $SOURCE to /tmp/partitions.source..."
146
+ sfdisk -d "$SOURCE" >"/tmp/partitions.source"
148
+ if [ "$retval" != "0" ]; then
149
+ printf "\033[40m\033[1;31mERROR: sfdisk returned an error($retval) while reading the partition table on $SOURCE!\n\033[0m"
155
+ for MDSTAT_LINE in `cat /proc/mdstat`; do
156
+ if echo "$MDSTAT_LINE" |grep -q '^md'; then
157
+ MD_DEV_LINE="$MDSTAT_LINE"
158
+ MD_DEV="$(echo "$MDSTAT_LINE" |awk '{ print $1 }')"
161
+ for part_nodev in `cat "/tmp/partitions.target" |grep '^/dev/' |grep -v 'Id= 0' |awk '{ print $1 }' |sed 's,^/dev/,,'`; do
162
+ if echo "$MD_DEV_LINE" |grep -E -q "[[:blank:]]$part_nodev\["; then
163
+ 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"
169
+ if echo "$MDSTAT_LINE" |grep -E -q '[[:blank:]]blocks[[:blank:]]' && ! echo "$MDSTAT_LINE" |grep -q '_'; then
170
+ # This array is NOT degraded so now check whether we want to add devices to it:
174
+ for part_nodev in `cat "/tmp/partitions.source" |grep '^/dev/' |grep -v 'Id= 0' |awk '{ print $1 }' |sed 's,^/dev/,,'`; do
175
+ if echo "$MD_DEV_LINE" |grep -E -q "[[:blank:]]$part_nodev\["; then
176
+ 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"
185
+# Program entry point
186
+echo "MDadd for SoftRAID-MDADM v$MY_VERSION"
187
+echo "Written by Arno van Amersfoort"
188
+echo "--------------------------------"
190
+# Set environment variables to default
200
+ ARGNAME="$(echo "$arg" |cut -d= -f1)"
201
+ ARGVAL="$(echo "$arg" |cut -d= -f2)"
203
+ if ! echo "$ARGNAME" |grep -q '^-'; then
204
+ if [ -z "$SOURCE" ]; then
207
+ if [ -z "$TARGET" ]; then
216
+ --force|-force|-f) FORCE=1;;
217
+ --noptupdate|-noptupdate|--noptu|-noptu) NOPTUPDATE=1;;
218
+ --nombrupdate|-nombrupdate|--nombru|nombru) NOMBRUPDATE=1;;
221
+ *) echo "ERROR: Bad argument: $ARGNAME";
228
+# Make sure everything is sane:
231
+# Disable all swaps on target disk
232
+echo "--> Disabling any swap partitions on target device $TARGET"
234
+for SWAP in `grep -E "^${TARGET}p?[0-9]+" /proc/swaps |awk '{ print $1 }'`; do
235
+ swapoff $SWAP >/dev/null 2>&1
238
+# Update track0 on target disk
239
+if [ "$NOMBRUPDATE" != "1" ]; then
240
+ echo "--> Copying track0(containing MBR) from $SOURCE to $TARGET..."
241
+ dd if="$SOURCE" of="$TARGET" bs=65536 count=1
243
+ if [ "$retval" != "0" ]; then
244
+ printf "\033[40m\033[1;31mERROR: dd returned an error($retval) while copying track0!\n\033[0m"
249
+if [ "$NOPTUPDATE" != "1" ]; then
250
+ echo "--> Restoring partition table from /tmp/partitions.source to $TARGET..."
251
+ cat "/tmp/partitions.source" |sfdisk --force "$TARGET"
253
+ if [ "$retval" != "0" ]; then
254
+ printf "\033[40m\033[1;31mERROR: sfdisk returned an error($retval) while writing the partition table!\n\033[0m"
258
+ echo "--> Restoring partition table from /tmp/partitions.target to $TARGET..."
259
+ cat "/tmp/partitions.target" |sfdisk --force "$TARGET"
261
+ if [ "$retval" != "0" ]; then
262
+ printf "\033[40m\033[1;31mERROR: sfdisk returned an error($retval) while writing the partition table!\n\033[0m"
268
+# Copy/build all md devices that exist on the source drive:
272
+for LINE in `cat /tmp/mdadm-detail-scan`; do
273
+ if echo "$LINE" |grep -E -q '^ARRAY[[:blank:]]'; then
274
+ MD_DEV=$(echo "$LINE" |awk '{ print $2 }')
277
+ if echo "$LINE" |grep -E -q "devices=.*${SOURCE}p?[0-9]+"; then
280
+ for item in `echo "$LINE" |sed -e "s:.*devices=::"`; do
281
+ if echo "$item" |grep -E -q -x "${SOURCE}p?[0-9]+"; then
282
+ PARTITION_NR=`echo "$item" |sed s:"$SOURCE"::`
287
+ if [ -z "$PARTITION_NR" ]; then
288
+ printf "\033[40m\033[1;31mERROR: Unable to retrieve detail information for $SOURCE from $MD_DEV!\n\033[0m"
292
+ # Check whether we're a root or boot partition
293
+ if grep -E -q -e "^$MD_DEV[[:blank:]]*/boot[[:blank:]]" -e "$MD_DEV[[:blank:]]*/[[:blank:]]" /etc/fstab; then
299
+ echo "--> Adding ${TARGET}${PARTITION_NR} to RAID array $MD_DEV:"
300
+ printf "\033[40m\033[1;31m"
301
+ mdadm --add "$MD_DEV" "${TARGET}${PARTITION_NR}"
303
+ if [ "$retval" != "0" ]; then
304
+ printf "\033[40m\033[1;31mERROR: mdadm returned an error($retval) while adding device!\n\033[0m"
313
+# Create swapspace on partitions with ID=82
314
+echo "--> Creating swapspace on target device (if any swap partitions exist)..."
316
+for SWAP_DEVICE in `sfdisk -d "$TARGET" |grep -i 'Id=82' |awk '{ print $1 }'`; do
317
+ mkswap "$SWAP_DEVICE"
318
+ swapon "$SWAP_DEVICE"
320
+ if ! grep -E -q "^$SWAP_DEVICE[[:blank:]]*none[[:blank:]]*swap[[:blank:]]" /etc/fstab; then
321
+ printf "\033[40m\033[1;31mWARNING: /etc/fstab does NOT contain a (valid) swap entry for $SWAP_DEVICE\n\033[0m"
325
+# Wait a bit for mdstat to settle
328
+echo "--> Showing current /proc/mdstat (you may need to update your mdadm.conf (manually)..."
332
+if [ "$NO_ADD" = "1" ]; then
333
+ printf "\033[40m\033[1;31mWARNING: No mdadm --add actions were performed, please investigate!\n\033[0m"
336
+if [ "$BOOT" = "1" ]; then
337
+ printf "\033[40m\033[1;31mNOTE: Boot and/or root partition detected.\n Please check your bootloader & active partitions!\n\033[0m"
339
tg: (d28c1a7..) contrib/scripts/mdadd (depends on: mdadm-3.0.3)