Last active
December 16, 2015 07:29
-
-
Save bithive/5399548 to your computer and use it in GitHub Desktop.
Bash script for creating ZFS volumes on SmartOS and sharing them over iSCSI
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
bold=$(tput bold) | |
norm=$(tput sgr0) | |
ENV="" | |
GUID="" | |
NAME="" | |
NEWVOL="" | |
OLDVOL="" | |
VIEW="" | |
volExists() { | |
$(zfs list $1 > /dev/null 2>&1) | |
if [ $? -eq 0 ]; then | |
return 0 | |
else | |
return 1 | |
fi | |
} | |
createVolume() { | |
# prompt for base volume | |
echo "Select the base volume to clone:" | |
select OLDVOL in "squeeze-amd64" "squeeze-i686" "wheezy-amd64" "wheezy-i686" "None"; do | |
if [[ $OLDVOL != "None" ]]; then | |
OLDVOL="zones/$OLDVOL" | |
# make sure the zvol exists | |
if ! volExists $OLDVOL; then | |
echo "Fatal: Base volume $OLDVOL does not exist!" | |
exit 1 | |
fi | |
# prompt for snapshot | |
SNAPSHOTS=$(zfs list -r -t snapshot -o name,creation $OLDVOL | sed 1d | cut -d' ' -f1 | cut -d'@' -f2) | |
echo "Select a snapshot of ${bold}$OLDVOL${norm} to use as the starting point:" | |
select SNAPSHOT in $SNAPSHOTS; do | |
$(zfs clone $OLDVOL@$SNAPSHOT $NEWVOL) | |
break | |
done | |
else | |
echo "Enter the size of the volume with unit suffix (e.g. 1024M or 1T)" | |
read SIZE | |
$(zfs create -V $SIZE -s $NEWVOL) | |
fi | |
if [ $? -ne 0 ]; then | |
echo "Fatal: Error while creating volume, aborting." | |
exit 1 | |
else | |
echo "Volume ${bold}$NEWVOL${norm} created:" | |
echo "$(zfs list $NEWVOL)" | |
fi | |
break | |
done | |
} | |
luExists() { | |
if [[ $(sbdadm list-lu | sed 1,5d | grep -i $1$ | wc -l) != '0' ]]; then | |
return 0 | |
else | |
return 1 | |
fi | |
} | |
viewExists() { | |
VIEW=$(stmfadm list-view -l $1 2>&1) | |
return $? | |
} | |
# prompt for name | |
echo "Enter the base name for this VM (e.g. solr, postgres, myapp, etc)." | |
echo "You will specify the environment later:" | |
read NAME | |
# prompt for environment | |
echo "Select the environment:" | |
select ENV in "production" "staging" "development" "test"; do | |
ENV=$(echo $ENV | cut -c 1-3) | |
NEWVOL="zones/$NAME-$ENV" | |
if [[ ! -z $ENV ]]; then | |
break | |
else | |
echo "Invalid selection." | |
fi | |
done | |
# check if the volume exists already | |
if volExists $NEWVOL; then | |
echo "Volume ${bold}$NEWVOL${norm} exists:" | |
echo "$(zfs list $NEWVOL)" | |
else | |
createVolume | |
fi | |
# create logical unit, get GUID | |
if ! luExists $NEWVOL; then | |
GUID=$(sbdadm create-lu /dev/zvol/rdsk/$NEWVOL | sed 1,4d | cut -d' ' -f1) | |
if [ $? -ne 0 ]; then | |
echo "Fatal: Error while creating logical unit, aborting." | |
exit 1 | |
fi | |
else | |
GUID=$(sbdadm list-lu | sed 1,5d | grep -i $1$ | cut -d' ' -f1) | |
fi | |
# create view | |
if ! viewExists $GUID; then | |
$(stmfadm add-view $GUID) | |
if [ $? -ne 0 ]; then | |
echo "Fatal: Error while adding view, aborting." | |
exit 1 | |
fi | |
fi | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment