~ubuntu-branches/ubuntu/precise/mdadm/precise-updates

« back to all changes in this revision

Viewing changes to debian/patches/contrib/scripts/mdadd.diff

  • Committer: Package Import Robot
  • Author(s): Clint Byrum
  • Date: 2012-02-09 16:53:02 UTC
  • mfrom: (1.1.27 sid)
  • Revision ID: package-import@ubuntu.com-20120209165302-bs4cfosmhoga2rpt
Tags: 3.2.3-2ubuntu1
* Merge from Debian testing. (LP: #920324)  Remaining changes:
  - Call checks in local-premount to avoid race condition with udev
    and opening a degraded array.
  - d/initramfs/mdadm-functions: Record in /run when boot-degraded 
    question has been asked so that it is only asked once
  - pass --test to mdadm to enable result codes for degraded arrays. 
  - Build udeb with -O2 on ppc64, working around a link error.
  - debian/control: we need udev and util-linux in the right version. We
    also remove the build dependency from quilt and docbook-to-man as both
    are not used in Ubuntus mdadm.
  - debian/initramfs/hook: kept the Ubuntus version for handling the absence
    of active raid arrays in <initramfs>/etc/mdadm/mdadm.conf
  - debian/initramfs/script.local-top.DEBIAN, debian/mdadm-startall,
    debian/mdadm.raid.DEBIAN: removed. udev does its job now instead.
  - debian/mdadm-startall.sgml, debian/mdadm-startall.8: documentation of
    unused startall script
  - debian/mdadm.config, debian/mdadm.postinst - let udev do the handling
    instead. Resolved merge conflict by keeping Ubuntu's version.
  - debian/mdadm.postinst, debian/mdadm.config, initramfs/init-premount:
    boot-degraded enablement; maintain udev starting of RAID devices;
    init-premount hook script for the initramfs, to provide information at
    boot
  - debian/mkconf.in is the older mkconf. Kept the Ubuntu version.
  - debian/rules: Kept Ubuntus version for installing apport hooks, not
    installing un-used startall script and for adding a udev rule
    corresponding to mdadm.
  - debian/install-rc, check.d/_numbers, check.d/root_on_raid: Ubuntu partman
    installer changes
  - debian/presubj: Dropped this unused bug reporting file. Instead use
    source_mdadm.py act as an apport hook for bug handling.
  - rename debian/mdadm.vol_id.udev to debian/mdadm.mdadm-blkid.udev so that
    the rules file ends up with a more reasonable name
  - d/p/debian-changes-3.1.4-1+8efb9d1ubuntu4: mdadm udev rule
    incrementally adds mdadm member when detected. Starting such an
    array in degraded mode is possible by mdadm -IRs. Using mdadm
    -ARs without stopping the array first does nothing when no
    mdarray-unassociated device is available. Using mdadm -IRs to
    start a previously partially assembled array through incremental
    mode. Keeping the mdadm -ARs for assembling arrays which were for
    some reason not assembled through incremental mode (i.e through
    mdadm's udev rule).

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
From: Arno van Amersfoort <arnova@rocky.eld.leidenuniv.nl>
2
 
Subject: Add contributed mdadd script
3
 
 
4
 
The mdadd.sh script adds a harddisk to multiple MD-arrays, using an existing
5
 
harddisk as a template.
6
 
 
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>
10
 
 
11
 
---
12
 
 contrib/mdadd.sh |  318 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
13
 
 1 files changed, 318 insertions(+), 0 deletions(-)
14
 
 
15
 
diff --git a/contrib/mdadd.sh b/contrib/mdadd.sh
16
 
new file mode 100644
17
 
index 0000000..8613c11
18
 
--- /dev/null
19
 
+++ b/contrib/mdadd.sh
20
 
@@ -0,0 +1,318 @@
21
 
+#!/bin/sh
22
 
+
23
 
+MY_VERSION="1.47e"
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.
35
 
+#
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.
40
 
+#
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
 
+# ----------------------------------------------------------------------------------------------------------------------
45
 
+
46
 
+EOL='
47
 
+'
48
 
+
49
 
+show_help()
50
 
+{
51
 
+  echo "Bad or missing parameter(s)"
52
 
+  echo "Usage: $(basename $0) [ options ] [ source_disk ] [ target_disk ]"
53
 
+  echo "Options:"
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!)"
57
 
+}
58
 
+
59
 
+
60
 
+get_partitions()
61
 
+{
62
 
+  cat /proc/partitions |awk '{ print $NF }' |sed -e '1,2d' -e 's,^/dev/,,'
63
 
+}
64
 
+
65
 
+
66
 
+check_binary()
67
 
+{
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
71
 
+    exit 2
72
 
+  fi
73
 
+}
74
 
+
75
 
+
76
 
+sanity_check()
77
 
+{
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"
80
 
+    exit 1
81
 
+  fi
82
 
+
83
 
+  check_binary mdadm
84
 
+  check_binary sfdisk
85
 
+  check_binary fdisk
86
 
+  check_binary dd
87
 
+  check_binary awk
88
 
+  check_binary grep
89
 
+  check_binary sed
90
 
+  check_binary cat
91
 
+
92
 
+  if [ -z "$SOURCE" ] || [ -z "$TARGET" ]; then
93
 
+    echo "ERROR: Bad or missing argument(s)"
94
 
+    show_help;
95
 
+    exit 4
96
 
+  fi
97
 
+
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"
100
 
+    exit 5
101
 
+  fi
102
 
+
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"
105
 
+    exit 5
106
 
+  fi
107
 
+
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)!"
111
 
+    exit 5
112
 
+  fi
113
 
+
114
 
+  # We also want variables without /dev/ :
115
 
+  SOURCE_NODEV="$(echo "$SOURCE" |sed 's,^/dev/,,')"
116
 
+  TARGET_NODEV="$(echo "$TARGET" |sed 's,^/dev/,,')"
117
 
+
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"
120
 
+    exit 7
121
 
+  fi
122
 
+
123
 
+  if get_partitions |grep -E -q -x "${TARGET_NODEV}p?[0-9]+" && [ "$FORCE" != "1" ]; then
124
 
+    fdisk -l "$TARGET"
125
 
+    printf "\033[40m\033[1;31mERROR: Target device $TARGET already contains partitions! Use --force to override. Quitting...\n\033[0m"
126
 
+    exit 8
127
 
+  fi
128
 
+
129
 
+  echo "--> Saving mdadm detail scan to /tmp/mdadm-detail-scan..."
130
 
+  mdadm --detail --scan --verbose >/tmp/mdadm-detail-scan
131
 
+  retval=$?
132
 
+  if [ "$retval" != "0" ]; then
133
 
+    printf "\033[40m\033[1;31mERROR: mdadm returned an error($retval) while determining detail information!\n\033[0m"
134
 
+    exit 9
135
 
+  fi 
136
 
+
137
 
+  echo "--> Saving partition table of target device $TARGET to /tmp/partitions.target..."
138
 
+  sfdisk -q -d "$TARGET" >"/tmp/partitions.target"
139
 
+  retval=$?
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"
142
 
+    exit 10
143
 
+  fi
144
 
+
145
 
+  echo "--> Saving partition table of source device $SOURCE to /tmp/partitions.source..."
146
 
+  sfdisk -d "$SOURCE" >"/tmp/partitions.source"
147
 
+  retval=$?
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"
150
 
+    exit 11
151
 
+  fi
152
 
+
153
 
+  MD_DEV=""
154
 
+  IFS=$EOL
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 }')"
159
 
+
160
 
+      IFS=$EOL
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"
164
 
+          exit 12
165
 
+        fi
166
 
+      done
167
 
+    fi
168
 
+
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:
171
 
+
172
 
+      #FIXME!
173
 
+      IFS=$EOL
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"
177
 
+          read
178
 
+        fi
179
 
+      done
180
 
+    fi
181
 
+  done
182
 
+}
183
 
+
184
 
+
185
 
+# Program entry point
186
 
+echo "MDadd for SoftRAID-MDADM v$MY_VERSION"
187
 
+echo "Written by Arno van Amersfoort"
188
 
+echo "--------------------------------"
189
 
+
190
 
+# Set environment variables to default
191
 
+FORCE=0
192
 
+NOPTUPDATE=0
193
 
+NOMBRUPDATE=0
194
 
+SOURCE=""
195
 
+TARGET=""
196
 
+
197
 
+# Check arguments
198
 
+unset IFS
199
 
+for arg in $*; do
200
 
+  ARGNAME="$(echo "$arg" |cut -d= -f1)"
201
 
+  ARGVAL="$(echo "$arg" |cut -d= -f2)"
202
 
+
203
 
+  if ! echo "$ARGNAME" |grep -q '^-'; then
204
 
+    if [ -z "$SOURCE" ]; then
205
 
+      SOURCE="$ARGVAL"
206
 
+    else
207
 
+      if [ -z "$TARGET" ]; then
208
 
+        TARGET="$ARGVAL"
209
 
+      else
210
 
+        show_help;
211
 
+        exit 3
212
 
+      fi
213
 
+    fi
214
 
+  else
215
 
+    case "$ARGNAME" in
216
 
+      --force|-force|-f) FORCE=1;;
217
 
+      --noptupdate|-noptupdate|--noptu|-noptu) NOPTUPDATE=1;;
218
 
+      --nombrupdate|-nombrupdate|--nombru|nombru) NOMBRUPDATE=1;;
219
 
+      --help) show_help;
220
 
+              exit 0;;
221
 
+      *) echo "ERROR: Bad argument: $ARGNAME";
222
 
+         show_help;
223
 
+         exit 3;;
224
 
+    esac
225
 
+  fi
226
 
+done
227
 
+
228
 
+# Make sure everything is sane:
229
 
+sanity_check;
230
 
+
231
 
+# Disable all swaps on target disk
232
 
+echo "--> Disabling any swap partitions on target device $TARGET"
233
 
+IFS=$EOL
234
 
+for SWAP in `grep -E "^${TARGET}p?[0-9]+" /proc/swaps |awk '{ print $1 }'`; do
235
 
+  swapoff $SWAP >/dev/null 2>&1
236
 
+done
237
 
+
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
242
 
+  retval=$?
243
 
+  if [ "$retval" != "0" ]; then
244
 
+    printf "\033[40m\033[1;31mERROR: dd returned an error($retval) while copying track0!\n\033[0m"
245
 
+    exit 9
246
 
+  fi
247
 
+fi
248
 
+
249
 
+if [ "$NOPTUPDATE" != "1" ]; then
250
 
+  echo "--> Restoring partition table from /tmp/partitions.source to $TARGET..."
251
 
+  cat "/tmp/partitions.source" |sfdisk --force "$TARGET"
252
 
+  retval=$?
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"
255
 
+    exit 9
256
 
+  fi
257
 
+else
258
 
+  echo "--> Restoring partition table from /tmp/partitions.target to $TARGET..."
259
 
+  cat "/tmp/partitions.target" |sfdisk --force "$TARGET"
260
 
+  retval=$?
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"
263
 
+    exit 9
264
 
+  fi
265
 
+fi
266
 
+
267
 
+
268
 
+# Copy/build all md devices that exist on the source drive:
269
 
+BOOT=0
270
 
+NO_ADD=1
271
 
+IFS=$EOL
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 }')
275
 
+  fi
276
 
+
277
 
+  if echo "$LINE" |grep -E -q "devices=.*${SOURCE}p?[0-9]+"; then
278
 
+    PARTITION_NR=""
279
 
+    IFS=','
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"::`
283
 
+        break;
284
 
+      fi
285
 
+    done
286
 
+
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"
289
 
+      exit 11
290
 
+    fi
291
 
+
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
294
 
+      BOOT=1
295
 
+    fi
296
 
+
297
 
+    NO_ADD=0
298
 
+    echo ""
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}"
302
 
+    retval=$?
303
 
+    if [ "$retval" != "0" ]; then
304
 
+      printf "\033[40m\033[1;31mERROR: mdadm returned an error($retval) while adding device!\n\033[0m"
305
 
+      exit 12
306
 
+    fi
307
 
+    printf "\033[0m"
308
 
+  fi
309
 
+done
310
 
+
311
 
+echo ""
312
 
+
313
 
+# Create swapspace on partitions with ID=82
314
 
+echo "--> Creating swapspace on target device (if any swap partitions exist)..."
315
 
+IFS=$EOL
316
 
+for SWAP_DEVICE in `sfdisk -d "$TARGET" |grep -i 'Id=82' |awk '{ print $1 }'`; do
317
 
+  mkswap "$SWAP_DEVICE"
318
 
+  swapon "$SWAP_DEVICE"
319
 
+
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"
322
 
+  fi
323
 
+done
324
 
+
325
 
+# Wait a bit for mdstat to settle
326
 
+sleep 3
327
 
+
328
 
+echo "--> Showing current /proc/mdstat (you may need to update your mdadm.conf (manually)..."
329
 
+cat /proc/mdstat
330
 
+echo ""
331
 
+
332
 
+if [ "$NO_ADD" = "1" ]; then
333
 
+  printf "\033[40m\033[1;31mWARNING: No mdadm --add actions were performed, please investigate!\n\033[0m"
334
 
+fi
335
 
+
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"
338
 
+fi
339
 
tg: (d28c1a7..) contrib/scripts/mdadd (depends on: mdadm-3.0.3)