1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
|
#!/bin/sh
. /usr/share/debconf/confmodule
# Mostly based on mdcfg
create_raid() {
RAID_TYPE="$1"
DEV_COUNT="$2"
if ([ "$DEV_COUNT" -lt 3 ] && [ $RAID_TYPE = "5" ]) ||
([ "$DEV_COUNT" -lt 4 ] && [ $RAID_TYPE = "6" ]) ||
([ "$DEV_COUNT" -lt 2 ] && [ $RAID_TYPE = "10" ]); then
db_input critical partman-auto-raid/notenoughparts
db_go partman-auto-raid/notenoughparts
exit 9
fi
SPARE_COUNT="$3"
REQUIRED=$(($DEV_COUNT + $SPARE_COUNT))
FS_TYPE="$4"
MOUNTPOINT="$5"
DEVICES="$6"
SPARE_DEVICES="$7"
RAID_DEVICES=$(echo $DEVICES | sed -e "s/#/ /g")
SPARE_DEVICES=$(echo $SPARE_DEVICES | sed -e "s/#/ /g")
NAMED_SPARES=$(echo $SPARE_DEVICES | wc -w)
if [ "$RAID_TYPE" != "0" ]; then
# Count them
SELECTED=$(echo $RAID_DEVICES | wc -w)
MISSING_DEVICES=""
# Add "missing" for as many devices as weren't selected
while [ "$SELECTED" -lt "$DEV_COUNT" ]; do
MISSING_DEVICES="$MISSING_DEVICES missing"
let SELECTED++
done
COUNT=$NAMED_SPARES
MISSING_SPARES=""
while [ "$COUNT" -lt "$SPARE_COUNT" ]; do
MISSING_SPARES="$MISSING_SPARES missing"
let COUNT++
done
fi
# Find the next available md-number
MD_NUM=$(grep ^md /proc/mdstat | sed -e 's/^md\(.*\) : active .*/\1/' | \
sort -n | tail -n 1)
if [ -z "$MD_NUM" ]; then
MD_NUM=0
else
let MD_NUM++
fi
# If we haven't already stashed the number of the first RAID device
# we used, do so now.
db_get partman-auto-raid/raidnum
if [ -z "$RET" ]; then
db_set partman-auto-raid/raidnum $MD_NUM
fi
echo "Raid devices count: $DEV_COUNT"
if [ "$RAID_TYPE" != "0" ]; then
logger -t partman-auto-raid "Selected spare count: $NAMED_SPARES"
logger -t partman-auto-raid "Spare devices count: $SPARE_COUNT"
MDADM_PARAMS="-x $SPARE_COUNT $RAID_DEVICES $MISSING_DEVICES $SPARE_DEVICES $MISSING_SPARES"
else
MDADM_PARAMS="$RAID_DEVICES"
fi
if ! log-output -t partman-auto-raid \
mdadm --create /dev/md$MD_NUM --auto=yes --force -R -l raid$RAID_TYPE \
-n $DEV_COUNT $MDADM_PARAMS
then
logger -t partman-auto-raid "Error creating array /dev/md$MD_NUM"
exit 1
fi
}
# Try to load the necessary modules.
depmod -a 1>/dev/null 2>&1
modprobe md-mod 1>/dev/null 2>&1
mkdir -p /dev/md
# Make sure that we have md-support
if [ ! -e /proc/mdstat ]; then
db_set mdcfg/nomd false
db_input critical mdcfg/nomd
db_go
exit 9
fi
# Force mdadm to be installed on the target system
apt-install mdadm || true
# Check we have recipe(s)
db_get partman-auto-raid/recipe
if [ -z "$RET" ]; then
logger -t partman-auto-raid "Error: No recipe specified in partman-auto-raid/recipe"
exit 1
fi
# Try to act on each recipe we were given
recipes=$RET
while [ -n "$recipes" ]; do
tmp=$recipes
recipes=$(echo $tmp | sed -e 's/^[^.]*\.\(.*\)$/\1/');
recipe=$(echo $tmp | sed -e 's/^\([^.]*\)\..*$/\1/');
if [ "$recipe" = "$recipes" ]; then
recipes=''
fi
# Do the recipe!
echo $recipe >/tmp/partman-auto-raid-recipe
read raidtype devcount sparecount fstype mountpoint devs sparedevs \
</tmp/partman-auto-raid-recipe
create_raid $raidtype $devcount $sparecount $fstype $mountpoint \
$devs $sparedevs
done
exit 0
|