daily pastebin goal
36%
SHARE
TWEET

tuneraid.sh

a guest Oct 8th, 2010 453 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/bash
  2. ###############################################################################
  3. #  simple script to set some parameters to increase performance on a mdadm
  4. # raid5 or raid6. Ajust the ## parameters ##-section to your system!
  5. #
  6. #  WARNING: depending on stripesize and the number of devices the array might
  7. # use QUITE a lot of memory after optimization!
  8. #
  9. #  27may2010 by Alexander Peganz
  10. #  09oct2010 by Rafael Fonseca -- added option to tune2fs different drive
  11. ###############################################################################
  12.  
  13.  
  14. ## parameters ##
  15. MDDEV=md0               # e.g. md51 for /dev/md51
  16. #FSDEV=$MDDEV               # same as above for filesystem on device
  17. FSDEV=lvm-raid/lvm0     # used for LVM on RAID setups - e.g. /dev/lvm-raid/lvm
  18. CHUNKSIZE=1024          # in kb
  19. BLOCKSIZE=4             # of file system in kb
  20. NCQ=enable              # disable, enable. ath. else keeps current setting
  21. NCQDEPTH=31             # 31 should work for almost anyone
  22. FORCECHUNKSIZE=true     # force max sectors kb to chunk size > 512
  23. DOTUNEFS=true           # run tune2fs, ONLY SET TO true IF YOU USE EXT[34]
  24. RAIDLEVEL=raid5         # raid5, raid6
  25.  
  26.  
  27. ## code ##
  28. # test for priviledges
  29. if [ "$(whoami)" != 'root' ]
  30. then
  31.   echo $(date): Need to be root
  32.   exit 1
  33. fi
  34.  
  35. # set number of parity devices
  36. NUMPARITY=1
  37. if [[ $RAIDLEVEL == "raid6" ]]
  38. then
  39.   NUMPARITY=2
  40. fi
  41.  
  42. # get all devices
  43. DEVSTR="`grep \"^$MDDEV : \" /proc/mdstat` eol"
  44. while [ -z "`expr match \"$DEVSTR\" '\(\<sd[a-z]\[[12]\?[0-9]\]\((S)\)\? \)'`" ]
  45. do
  46.   DEVSTR="`echo $DEVSTR|cut -f 2- -d \ `"
  47. done
  48.  
  49. # get active devices list and spares list
  50. DEVS=""
  51. SPAREDEVS=""
  52. while [ "$DEVSTR" != "eol" ]; do
  53.   CURDEV="`echo $DEVSTR|cut -f -1 -d \ `"
  54.   if [ -n "`expr match \"$CURDEV\" '\(\<sd[a-z]\[[12]\?[0-9]\]\((S)\)\)'`" ]
  55.   then
  56.     SPAREDEVS="$SPAREDEVS${CURDEV:2:1}"
  57.   elif [ -n "`expr match \"$CURDEV\" '\(\<sd[a-z]\[[12]\?[0-9]\]\)'`" ]
  58.   then
  59.     DEVS="$DEVS${CURDEV:2:1}"
  60.   fi
  61.   DEVSTR="`echo $DEVSTR|cut -f 2- -d \ `"
  62. done
  63. NUMDEVS=${#DEVS}
  64. NUMSPAREDEVS=${#SPAREDEVS}
  65.  
  66. # test if number of devices makes sense
  67. if [ ${#DEVS} -lt $[1+$NUMPARITY] ]
  68. then
  69.   echo $(date): Need more devices
  70.   exit 1
  71. fi
  72.  
  73. # set read ahead
  74. RASIZE=$[$NUMDEVS*($NUMDEVS-$NUMPARITY)*2*$CHUNKSIZE]   # in 512b blocks
  75. echo read ahead size per device: $RASIZE blocks \($[$RASIZE/2]kb\)
  76. MDRASIZE=$[$RASIZE*$NUMDEVS]
  77. echo read ahead size of array: $MDRASIZE blocks \($[$MDRASIZE/2]kb\)
  78. blockdev --setra $RASIZE /dev/sd[$DEVS]
  79. if [ $NUMSPAREDEVS -gt 0 ]
  80. then
  81.   blockdev --setra $RASIZE /dev/sd[$SPAREDEVS]
  82. fi
  83. blockdev --setra $MDRASIZE /dev/$MDDEV
  84.  
  85. # set stripe cache size
  86. STRCACHESIZE=$[$RASIZE/8]                               # in pages per device
  87. echo stripe cache size of devices: $STRCACHESIZE pages \($[$STRCACHESIZE*4]kb\)
  88. echo $STRCACHESIZE > /sys/block/$MDDEV/md/stripe_cache_size
  89.  
  90. # set max sectors kb
  91. DEVINDEX=0
  92. MINMAXHWSECKB=$(cat /sys/block/sd${DEVS:0:1}/queue/max_hw_sectors_kb)
  93. until [ $DEVINDEX -ge $NUMDEVS ]
  94. do
  95.   DEVLETTER=${DEVS:$DEVINDEX:1}
  96.   MAXHWSECKB=$(cat /sys/block/sd$DEVLETTER/queue/max_hw_sectors_kb)
  97.   if [ $MAXHWSECKB -lt $MINMAXHWSECKB ]
  98.   then
  99.     MINMAXHWSECKB=$MAXHWSECKB
  100.   fi
  101.   DEVINDEX=$[$DEVINDEX+1]
  102. done
  103. if [ $CHUNKSIZE -le $MINMAXHWSECKB ] &&
  104.   ( [ $CHUNKSIZE -le 512 ] || [[ $FORCECHUNKSIZE == "true" ]] )
  105. then
  106.   echo setting max sectors kb to match chunk size
  107.   DEVINDEX=0
  108.   until [ $DEVINDEX -ge $NUMDEVS ]
  109.   do
  110.     DEVLETTER=${DEVS:$DEVINDEX:1}
  111.     echo $CHUNKSIZE > /sys/block/sd$DEVLETTER/queue/max_sectors_kb
  112.     DEVINDEX=$[$DEVINDEX+1]
  113.   done
  114.   DEVINDEX=0
  115.   until [ $DEVINDEX -ge $NUMSPAREDEVS ]
  116.   do
  117.     DEVLETTER=${SPAREDEVS:$DEVINDEX:1}
  118.     echo $CHUNKSIZE > /sys/block/sd$DEVLETTER/queue/max_sectors_kb
  119.     DEVINDEX=$[$DEVINDEX+1]
  120.   done
  121. fi
  122.  
  123. # enable/disable NCQ
  124. DEVINDEX=0
  125. if [[ $NCQ == "enable" ]] || [[ $NCQ == "disable" ]]
  126. then
  127.   if [[ $NCQ == "disable" ]]
  128.   then
  129.     NCQDEPTH=1
  130.   fi
  131.   echo setting NCQ queue depth to $NCQDEPTH
  132.   until [ $DEVINDEX -ge $NUMDEVS ]
  133.   do
  134.     DEVLETTER=${DEVS:$DEVINDEX:1}
  135.     echo $NCQDEPTH > /sys/block/sd$DEVLETTER/device/queue_depth
  136.     DEVINDEX=$[$DEVINDEX+1]
  137.   done
  138.   DEVINDEX=0
  139.   until [ $DEVINDEX -ge $NUMSPAREDEVS ]
  140.   do
  141.     DEVLETTER=${SPAREDEVS:$DEVINDEX:1}
  142.     echo $NCQDEPTH > /sys/block/sd$DEVLETTER/device/queue_depth
  143.     DEVINDEX=$[$DEVINDEX+1]
  144.   done
  145. fi
  146.  
  147. # tune2fs
  148. if [[ $DOTUNEFS == "true" ]]
  149. then
  150.   STRIDE=$[$CHUNKSIZE/$BLOCKSIZE]
  151.   STRWIDTH=$[$CHUNKSIZE/$BLOCKSIZE*($NUMDEVS-$NUMPARITY)]
  152.   echo setting stride to $STRIDE blocks \($CHUNKSIZEkb\)
  153.   echo setting stripe-width to $STRWIDTH blocks \($[$STRWIDTH*$BLOCKSIZE]kb\)
  154.   tune2fs -E stride=$STRIDE,stripe-width=$STRWIDTH /dev/$FSDEV
  155. fi
  156.  
  157. # exit
  158. echo $(date): Success
  159. exit 0
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top