4
# ----------------------------------------------------------------------------------------------------------------------
5
# Linux MD (Soft)RAID Add Script - Add a (new) harddisk to another multi MD-array harddisk
6
# Last update: June 9, 2009
7
# (C) Copyright 2005-2009 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
# ----------------------------------------------------------------------------------------------------------------------
32
echo "Bad or missing parameter(s)"
33
echo "Usage: $(basename $0) [ source_disk ] [ target_disk ] [ options ]"
35
echo "--force = Even proceed if target device does not appear empty"
36
echo "--noptupdate = Do NOT update the partition table on the target device (EXPERT!)"
37
echo "--nombrupdate = Do NOT update the MBR boot-loader on the target device (EXPERT!)"
43
cat /proc/partitions |awk '{ print $NF }' |sed -e '1,2d' -e 's,^/dev/,,'
49
if ! which "$1" >/dev/null 2>&1; then
50
printf "\033[40m\033[1;31mERROR: Binary \"$1\" does not exist or is not executable!\033[0m\n" >&2
51
printf "\033[40m\033[1;31m Please, make sure that it is (properly) installed!\033[0m\n" >&2
59
if [ "$UID" != "0" ]; then
60
printf "\033[40m\033[1;31mERROR: Root check FAILED (you MUST be root to use this script)! Quitting...\n\033[0m"
73
if [ -z "$SOURCE" ] || [ -z "$TARGET" ]; then
74
echo "ERROR: Bad or missing argument(s)"
79
if ! echo "$SOURCE" |grep -q '^/dev/'; then
80
printf "\033[40m\033[1;31mERROR: Source device $SOURCE does not start with /dev/! Quitting...\n\033[0m"
84
if ! echo "$TARGET" |grep -q '^/dev/'; then
85
printf "\033[40m\033[1;31mERROR: Target device $TARGET does not start with /dev/! Quitting...\n\033[0m"
89
if echo "$SOURCE" |grep -q 'md[0-9]'; then
90
printf "\033[40m\033[1;31mERROR: The source device specified is an md-device! Quitting...\n\033[0m"
91
echo "A physical drive (part of the md-array('s)) is required as source device (ie. /dev/hda)!"
95
# We also want variables without /dev/ :
96
SOURCE_NODEV="$(echo "$SOURCE" |sed 's,^/dev/,,')"
97
TARGET_NODEV="$(echo "$TARGET" |sed 's,^/dev/,,')"
99
if ! get_partitions |grep -E -q -x "$SOURCE_NODEV""p?[0-9]+"; then
100
printf "\033[40m\033[1;31mERROR: Source device $SOURCE does not contain any partitions!? Quitting...\n\033[0m"
104
if get_partitions |grep -E -q -x "$TARGET_NODEV""p?[0-9]+" && [ "$FORCE" != "1" ]; then
105
printf "\033[40m\033[1;31mERROR: Target device $TARGET is NOT empty! Use --force to override. Quitting...\n\033[0m"
109
echo "--> Saving mdadm detail scan to /tmp/mdadm-detail-scan.txt..."
110
mdadm --detail --scan --verbose >/tmp/mdadm-detail-scan.txt
112
if [ "$retval" != "0" ]; then
113
printf "\033[40m\033[1;31mERROR: mdadm returned an error($retval) while determining detail information!\n\033[0m"
117
echo "--> Saving partition table of target device $TARGET to /tmp/partitions.$TARGET_NODEV..."
118
sfdisk -d "$TARGET" >"/tmp/partitions.$TARGET_NODEV"
120
if [ "$retval" != "0" ]; then
121
printf "\033[40m\033[1;31mERROR: sfdisk returned an error($retval) while reading the partition table!\n\033[0m"
125
echo "--> Saving partition table of source device $SOURCE to /tmp/partitions.$SOURCE_NODEV..."
126
sfdisk -d "$SOURCE" >"/tmp/partitions.$SOURCE_NODEV"
128
if [ "$retval" != "0" ]; then
129
printf "\033[40m\033[1;31mERROR: sfdisk returned an error($retval) while reading the partition table!\n\033[0m"
135
for MDSTAT_LINE in `cat /proc/mdstat`; do
136
if echo "$MDSTAT_LINE" |grep -q '^md'; then
137
MD_DEV_LINE="$MDSTAT_LINE"
138
MD_DEV="$(echo "$MDSTAT_LINE" |awk '{ print $1 }')"
141
for part_nodev in `cat "/tmp/partitions.$TARGET_NODEV" |grep '^/dev/' |grep -v 'Id= 0' |awk '{ print $1 }' |sed 's,^/dev/,,'`; do
142
if echo "$MD_DEV_LINE" |grep -E -q "[[:blank:]]$part_nodev\["; then
143
printf "\033[40m\033[1;31mWARNING: Partition /dev/$part_nodev on target device is already in use by array /dev/$MD_DEV!\nPress enter to continue or CTRL-C to abort...\n\033[0m"
149
if echo "$MDSTAT_LINE" |grep -E -q '[[:blank:]]blocks[[:blank:]]' && ! echo "$MDSTAT_LINE" |grep -q '_'; then
150
# This array is NOT degraded so now check whether we want to add devices to it:
153
for part_nodev in `cat "/tmp/partitions.$SOURCE_NODEV" |grep '^/dev/' |grep -v 'Id= 0' |awk '{ print $1 }' |sed 's,^/dev/,,'`; do
154
if echo "$MD_DEV_LINE" |grep -E -q "[[:blank:]]$part_nodev\["; then
155
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" echo "WARNING: Array is not degraded: $LINE"
164
# Program entry point
165
echo "MDadd for SoftRAID-MDADM v$MY_VERSION"
166
echo "Written by Arno van Amersfoort"
167
echo "--------------------------------"
169
# Set environment variables to default
178
ARGNAME="$(echo "$arg" |cut -d= -f1)"
179
ARGVAL="$(echo "$arg" |cut -d= -f2)"
181
if ! echo "$ARGNAME" |grep -q '^-'; then
182
if [ -z "$SOURCE" ]; then
185
if [ -z "$TARGET" ]; then
194
--force|-force|-f) FORCE=1;;
195
--noptupdate|-noptupdate|--noptu|-noptu) NOPTUPDATE=1;;
196
--nombrupdate|-nombrupdate|--nombru|nombru) NOMBRUPDATE=1;;
199
*) echo "ERROR: Bad argument: $ARGNAME";
206
# Make sure everything is sane:
209
# Disable all swaps on target disk
210
echo "--> Disabling any swap partitions on target device $TARGET"
212
for SWAP in `grep -E "^$TARGET""p?[0-9]+" /proc/swaps |awk '{ print $1 }'`; do
213
swapoff $SWAP >/dev/null 2>&1
216
# Update track0 on target disk
217
if [ "$NOMBRUPDATE" != "1" ]; then
218
echo "--> Copying track0(containing MBR) from $SOURCE to $TARGET..."
219
dd if="$SOURCE" of="$TARGET" bs=65536 count=1
221
if [ "$retval" != "0" ]; then
222
printf "\033[40m\033[1;31mERROR: dd returned an error($retval) while copying track0!\n\033[0m"
227
if [ "$NOPTUPDATE" != "1" ]; then
228
echo "--> Restoring partition table from /tmp/partitions.$SOURCE_NODEV to $TARGET..."
229
cat "/tmp/partitions.$SOURCE_NODEV" |sfdisk --force "$TARGET"
231
if [ "$retval" != "0" ]; then
232
printf "\033[40m\033[1;31mERROR: sfdisk returned an error($retval) while writing the partition table!\n\033[0m"
236
echo "--> Restoring partition table from /tmp/partitions.$TARGET_NODEV to $TARGET..."
237
cat "/tmp/partitions.$TARGET_NODEV" |sfdisk --force "$TARGET"
239
if [ "$retval" != "0" ]; then
240
printf "\033[40m\033[1;31mERROR: sfdisk returned an error($retval) while writing the partition table!\n\033[0m"
246
# Copy/build all md devices that exist on the source drive:
250
for LINE in `cat /tmp/mdadm-detail-scan.txt`; do
251
if echo "$LINE" |grep -E -q '^ARRAY[[:blank:]]'; then
252
MD_DEV=$(echo "$LINE" |awk '{ print $2 }')
255
if echo "$LINE" |grep -q "devices=.*$SOURCE"; then
257
PARTITION_NR="$(echo "$LINE" |sed -e "s:.*devices=.*$SOURCE::" -e "s:,.*::")"
259
if [ -z "$PARTITION_NR" ]; then
260
printf "\033[40m\033[1;31mERROR: Unable to retrieve detail information for $SOURCE from $MD_DEV!\n\033[0m"
264
# Check whether we're a root or boot partition
265
if grep -E -q -e "^$MD_DEV[[:blank:]]*/boot[[:blank:]]" -e "$MD_DEV[[:blank:]]*/[[:blank:]]" /etc/fstab; then
270
echo "--> Adding $TARGET$PARTITION_NR to RAID array $MD_DEV:"
271
printf "\033[40m\033[1;31m"
272
mdadm --add "$MD_DEV" "$TARGET""$PARTITION_NR"
274
if [ "$retval" != "0" ]; then
275
printf "\033[40m\033[1;31mERROR: mdadm returned an error($retval) while adding device!\n\033[0m"
284
# Create swapspace on partitions with ID=82
285
echo "--> Creating swapspace on target device (if any swap partitions exist):"
287
for SWAP_DEVICE in `sfdisk -d "$TARGET" |grep -i 'Id=82' |awk '{ print $1 }'`; do
288
mkswap "$SWAP_DEVICE"
289
swapon "$SWAP_DEVICE"
291
if ! grep -E -q "^$SWAP_DEVICE[[:blank:]]*none[[:blank:]]*swap[[:blank:]]" /etc/fstab; then
292
printf "\033[40m\033[1;31mWARNING: /etc/fstab does NOT contain a (valid) swap entry for $SWAP_DEVICE\n\033[0m"
296
#echo "--> Showing current mdadm detail-scan (you may need to update your mdadm.conf (manually):"
297
#mdadm --detail --scan
299
echo "--> Showing current /proc/mdstat (you may need to update your mdadm.conf (manually):"
303
if [ "$NO_ADD" = "1" ]; then
304
printf "\033[40m\033[1;31mWARNING: No mdadm --add actions were performed, please investigate!\n\033[0m"
307
if [ "$BOOT" = "1" ]; then
308
printf "\033[40m\033[1;31mNOTE: Boot and/or root partition detected.\n You *MAY* need to reinstall your boot loader (ie. GRUB) on this device!\n\033[0m"
312
# sanity check nopt (check if target device has a partition table)?
313
# detect if device has superblock (mdadm --examine /dev/sda1; echo $?)?
314
# continue ask (show what will be done):?