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.

#!/bin/bash

declare -a DISKS=($(fdisk -l | grep 'dev' | awk '{if ($3 > 1000) print $2}' | sed s/.$//))
declare -a INUSE=($(mount | awk '{if ($1 ~ '/dev/') print $1}'))
declare -a UNUSED=(`comm -23 <(echo ${DISKS[@]} | sed 's/ /\n/g' | sort -u) <(echo ${INUSE[@]}  | sed 's/ /\n/g'| sort -u)`)

echo "All Disks larger than 1000 GB:"
for i in ${!DISKS[@]}
do
  echo  "$i=${DISKS[$i]}"
done

echo
echo "In use Disks:"
for i in ${!INUSE[@]}
do
  echo  "$i=${INUSE[$i]}"
done

echo
echo "Available Disks larger than 1000 GB:"
for i in ${!UNUSED[@]}
do
  echo  "$i=${UNUSED[$i]}"
done

# Create Physical Volumes
for i in ${!UNUSED[@]}
do
  pvcreate ${UNUSED[$i]}
done

# Create Volume Group from Physical Volumes
vgcreate datavg ${UNUSED[@]}

# Number of stripes to create in Logical Volume
#  -- set to number of disks in group
NUMSTRIPES=${#UNUSED[@]}

# Get Volume Group Size
SIZE=$(expr $(vgdisplay datavg | grep "Total PE" | awk '{print $3}') - 100)

echo "SIZE: $SIZE"
# Create Logical Volume
lvcreate -l $SIZE -i${NUMSTRIPES} -n DataVolume datavg

mkfs.ext4 /dev/mapper/datavg-DataVolume -L DataFS

echo "LABEL=DataFS    /scratch        ext4    defaults        0 0" >> /etc/fstab
mount -a

https://gist.github.com/nsabine/6196550


comments powered by Disqus