Create Linux Spanned Volume

The following Bash script finds the AWS ephemeral drives and creates a spanned volume, mounted as /scratch. I used this in a bootstrap script to dynamically allocate processing space.

  1. #!/bin/bash
  2.  
  3. declare -a DISKS=($(fdisk -l | grep 'dev' | awk '{if ($3 > 1000) print $2}' | sed s/.$//))
  4. declare -a INUSE=($(mount | awk '{if ($1 ~ '/dev/') print $1}'))
  5. declare -a UNUSED=(`comm -23 <(echo ${DISKS[@]} | sed 's/ /\n/g' | sort -u) <(echo ${INUSE[@]} | sed 's/ /\n/g'| sort -u)`)
  6.  
  7. echo "All Disks larger than 1000 GB:"
  8. for i in ${!DISKS[@]}
  9. do
  10. echo "$i=${DISKS[$i]}"
  11. done
  12.  
  13. echo
  14. echo "In use Disks:"
  15. for i in ${!INUSE[@]}
  16. do
  17. echo "$i=${INUSE[$i]}"
  18. done
  19.  
  20. echo
  21. echo "Available Disks larger than 1000 GB:"
  22. for i in ${!UNUSED[@]}
  23. do
  24. echo "$i=${UNUSED[$i]}"
  25. done
  26.  
  27. # Create Physical Volumes
  28. for i in ${!UNUSED[@]}
  29. do
  30. pvcreate ${UNUSED[$i]}
  31. done
  32.  
  33. # Create Volume Group from Physical Volumes
  34. vgcreate datavg ${UNUSED[@]}
  35.  
  36. # Number of stripes to create in Logical Volume
  37. # -- set to number of disks in group
  38. NUMSTRIPES=${#UNUSED[@]}
  39.  
  40. # Get Volume Group Size
  41. SIZE=$(expr $(vgdisplay datavg | grep "Total PE" | awk '{print $3}') - 100)
  42.  
  43. echo "SIZE: $SIZE"
  44. # Create Logical Volume
  45. lvcreate -l $SIZE -i${NUMSTRIPES} -n DataVolume datavg
  46.  
  47. mkfs.ext4 /dev/mapper/datavg-DataVolume -L DataFS
  48.  
  49. echo "LABEL=DataFS /scratch ext4 defaults 0 0" >> /etc/fstab
  50. mount -a
https://gist.github.com/nsabine/6196550

comments powered by Disqus