Skip to content

Instantly share code, notes, and snippets.

@jinnko
Created August 12, 2013 10:58
Show Gist options
  • Save jinnko/6209899 to your computer and use it in GitHub Desktop.
Save jinnko/6209899 to your computer and use it in GitHub Desktop.
Set disk values to maximize a resync, or restore the default settings. Dynamically detects the underlying volumes that make up the MD device. Based on the fine advice from Vivek Gite posted at http://www.cyberciti.biz/tips/linux-raid-increase-resync-rebuild-speed.html.
#!/bin/zsh
#
# @author Jinn Ko <https://gist.github.com/jinnko>
# @since 2013-08-11
MD_DEV=$1
ACTION=$2
MIN_SPEED=50000
MAX_SPEED=200000
READAHEAD=$(( 512 * 512 ))
if [[ -z "$MD_DEV" || -z "$ACTION" ]]; then
echo "Set disk values to maximize a resync or restore the default settings."
echo "Dynamically detects the underlying volumes that make up the MD device."
echo
echo "Usage: $(basename $0) MD_DEV (fast|normal)"
echo "Both arguments are required, where:"
echo " MD_DEV the base name of the MD device. For example: md0"
echo " [fast|normal] 'fast' sets:"
echo " sync_speed_min = $MIN_SPEED"
echo " read ahead = $READAHEAD"
echo " ncq = 1"
echo " 'normal' attempts to return values to those saved"
echo " prior to using 'fast'."
echo
echo " e.g. $(basename $0) md0 fast"
exit 1
fi
if [ "$ACTION" = "fast" ]; then
echo "Setting fast config for $MD_DEV."
# Set min speed
echo $MIN_SPEED >! /sys/block/${MD_DEV}/md/sync_speed_min
echo $MAX_SPEED >! /sys/block/${MD_DEV}/md/sync_speed_max
# Set read ahead
[ ! -r /tmp/md-raid-${MD_DEV}-ra ] && blockdev --getra /dev/${MD_DEV} > /tmp/md-raid-${MD_DEV}-ra
blockdev --setra $READAHEAD /dev/${MD_DEV}
# Set strip-cache_size - increases us by approx 25% at this point
[ ! -r /tmp/md-raid-${MD_DEV}-stripe_cache_size ] && cat /sys/block/${MD_DEV}/md/stripe_cache_size > /tmp/md-raid-${MD_DEV}-stripe_cache_size
echo 32768 >! /sys/block/${MD_DEV}/md/stripe_cache_size
# Disable NCQ - increases performance by 200% !!!
cd /sys/block/${MD_DEV}/md
for x in {0..$(( $(cat /sys/block/${MD_DEV}/md/raid_disks) - 1))};do
RAID_DEV=${$(basename $(readlink -f rd$x))/dev\-/}
RAID_DEV=${RAID_DEV/[0-9]/}
[ ! -r /tmp/md-raid-${RAID_DEV}-ncq ] && cat /sys/block/${RAID_DEV}/device/queue_depth > /tmp/md-raid-${RAID_DEV}-ncq
echo 1 >! /sys/block/${RAID_DEV}/device/queue_depth
done
elif [ "$ACTION" = "normal" ]; then
echo "Setting fast config for $MD_DEV."
# Set min speed
echo 1000 >! /sys/block/${MD_DEV}/md/sync_speed_min
echo 200000 >! /sys/block/${MD_DEV}/md/sync_speed_max
# Set read ahead
if [ -r /tmp/md-raid-${MD_DEV}-ra ]; then
blockdev --setra $(cat /tmp/md-raid-${MD_DEV}-ra) /dev/${MD_DEV}
else
echo "Unable to reset readahead - no saved setting file"
fi
# Set strip-cache_size - increases us by approx 25% at this point
if [ -f /tmp/md-raid-${MD_DEV}-stripe_cache_size ]; then
cat /tmp/md-raid-${MD_DEV}-stripe_cache_size >! /sys/block/${MD_DEV}/md/stripe_cache_size
else
echo "Unable to reset strip_cache_size - no saved setting file"
fi
# Disable NCQ - increases performance by 200% !!!
cd /sys/block/${MD_DEV}/md
for x in {0..$(( $(cat /sys/block/${MD_DEV}/md/raid_disks) - 1))};do
RAID_DEV=${$(basename $(readlink -f rd$x))/dev\-/}
RAID_DEV=${RAID_DEV/[0-9]/}
if [ -r /tmp/md-raid-${RAID_DEV}-ncq ]; then
cat /tmp/md-raid-${RAID_DEV}-ncq >! /sys/block/${RAID_DEV}/device/queue_depth
else
echo "Unable to reset NCQ - no saved setting file"
fi
done
else
echo "Uknown action; $ACTION."
echo "Aborting"
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment