Created
November 19, 2013 17:37
-
-
Save ilovezfs/7549222 to your computer and use it in GitHub Desktop.
zfsadm
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/bash | |
set -e | |
#defaults: | |
export DEV_DIR="/Users/joe/Developer" | |
export INSTALL_DIR="/osxzfs" | |
export OWNER="joe" | |
export BRANCH="master" | |
export ZFS_BRANCH="default" | |
export SPL_BRANCH="default" | |
if [[ $(/usr/bin/id -u) -ne 0 ]]; then | |
set -e | |
sudo "$0" "$@" | |
exit | |
fi | |
set -e | |
OPTS=$(/usr/bin/ruby - "$@" <<'EndOfRuby' | |
require 'getoptlong' | |
devdir=ENV['DEV_DIR'] | |
installdir=ENV['INSTALL_DIR'] | |
owner=ENV['OWNER'] | |
branch=ENV['BRANCH'] | |
zfsbranch=ENV['ZFS_BRANCH'] | |
splbranch=ENV['SPL_BRANCH'] | |
longopts = GetoptLong.new( | |
[ '--dev-dir', '-d' , GetoptLong::REQUIRED_ARGUMENT ], | |
[ '--install-dir', '-i', GetoptLong::REQUIRED_ARGUMENT ], | |
[ '--owner', '-o', GetoptLong::REQUIRED_ARGUMENT ], | |
[ '--branch', '-b', GetoptLong::OPTIONAL_ARGUMENT], | |
[ '--zfs-branch', '-z', GetoptLong::OPTIONAL_ARGUMENT ], | |
[ '--spl-branch', '-s', GetoptLong::OPTIONAL_ARGUMENT ], | |
[ '--load', '-l', GetoptLong::NO_ARGUMENT ], | |
[ '--unload', '-u', GetoptLong::NO_ARGUMENT ], | |
[ '--kexts-only', '-k', GetoptLong::NO_ARGUMENT ], | |
[ '--configure', '-c', GetoptLong::NO_ARGUMENT ], | |
[ '--make', '-m', GetoptLong::NO_ARGUMENT ], | |
[ '--dry-run', '-n', GetoptLong::NO_ARGUMENT ], | |
[ '--help', '-h' , GetoptLong::NO_ARGUMENT ] | |
) | |
simpleopts={} | |
begin | |
longopts.each do |opt, arg| | |
arg = arg.to_s.strip | |
arg = (arg.length == 0) ? nil : arg | |
case opt | |
when '--dev-dir' | |
devdir = arg ? arg : devdir | |
when '--install-dir' | |
installdir = arg ? arg : installdir | |
when '--owner' | |
owner = arg ? arg : owner | |
when '--branch' | |
simpleopts[:b]=1 | |
branch = arg ? arg : branch | |
when '--zfs-branch' | |
simpleopts[:z]=1 | |
zfsbranch = arg ? arg : zfsbranch | |
when '--spl-branch' | |
simpleopts[:s]=1 | |
splbranch = arg ? arg : splbranch | |
when '--load' | |
simpleopts[:l]=1 | |
when '--unload' | |
simpleopts[:u]=1 | |
when '--kexts-only' | |
simpleopts[:k]=1 | |
when '--configure' | |
simpleopts[:c]=1 | |
when '--make' | |
simpleopts[:m]=1 | |
when '--dry-run' | |
simpleopts[:n]=1 | |
when '--help' | |
#RDoc::usage | |
simpleopts[:h]=1 | |
end | |
end | |
rescue => err | |
simpleopts[:e]=1 | |
end | |
simpleoptsstr = simpleopts.keys.map{ |i| i.to_s }.join | |
puts "-" + simpleoptsstr + \ | |
" " + devdir + \ | |
" " + installdir + \ | |
" " + owner + \ | |
" " + branch + \ | |
" " + zfsbranch + \ | |
" " + splbranch | |
EndOfRuby | |
) | |
(echo set -- "$OPTS" ; cat <<'EndOfBash' | |
set -e | |
# DEBUG_ | |
# echo "arg0 $0" | |
# echo "arg1 $1" | |
# echo "arg2 $2" | |
# echo "arg3 $3" | |
# echo "arg4 $4" | |
# echo "arg5 $5" | |
# echo "arg6 $6" | |
# echo "arg7 $7" | |
# echo "arg8 $8" | |
# _DEBUG | |
DEV_DIR="$2" | |
INSTALL_DIR="$3" | |
OWNER="$4" | |
BRANCH="$5" | |
ZFS_BRANCH="$6" | |
SPL_BRANCH="$7" | |
if [ x"${INSTALL_DIR:0:1}" = x"-" ] ; then | |
printf "Install directory path cannot start with \"-\"\n" | |
exit | |
elif [ x"$INSTALL_DIR" = x"/" ] ; then | |
printf "Install directory path cannot be \"/\"\n" | |
exit | |
fi | |
if [ x"$SPL_BRANCH" = x"default" ] ; then | |
SPL_BRANCH="$BRANCH" | |
fi | |
if [ x"$ZFS_BRANCH" = x"default" ] ; then | |
ZFS_BRANCH="$BRANCH" | |
fi | |
ZFS_DIR="$DEV_DIR"/zfs | |
SPL_DIR="$DEV_DIR"/spl | |
if [ -d "$ZFS_DIR" ] ; then | |
set +e | |
CURRENT_ZFS_BRANCH="$(git --git-dir="$ZFS_DIR"/.git rev-parse --abbrev-ref HEAD)" | |
set -e | |
fi | |
if [ -d "$SPL_DIR" ] ; then | |
set +e | |
CURRENT_SPL_BRANCH="$(git --git-dir="$SPL_DIR"/.git rev-parse --abbrev-ref HEAD)" | |
set -e | |
fi | |
if [ x"$CURRENT_ZFS_BRANCH" != x"$ZFS_BRANCH" ] ; then | |
ZFS_BRANCH_MATCHES_CURRENT_ZFS_BRANCH="no" | |
else | |
ZFS_BRANCH_MATCHES_CURRENT_ZFS_BRANCH="yes" | |
fi | |
if [ x"$CURRENT_SPL_BRANCH" != x"$SPL_BRANCH" ] ; then | |
SPL_BRANCH_MATCHES_CURRENT_SPL_BRANCH="no" | |
else | |
SPL_BRANCH_MATCHES_CURRENT_SPL_BRANCH="yes" | |
fi | |
SHOULD_SWITCH_BRANCH="no" | |
SHOULD_SWITCH_ZFS_BRANCH="no" | |
SHOULD_SWITCH_SPL_BRANCH="no" | |
SHOULD_LOAD="no" | |
SHOULD_UNLOAD="no" | |
SHOULD_CONFIGURE="yes" | |
SHOULD_MAKE="yes" | |
MUST_SKIP_CONFIGURE_AND_MAKE="no" | |
DRY_RUN="no" | |
ONE_OF_CONFIGURE_AND_MAKE_SPECIFIED="no" | |
BOTH_CONFIGURE_AND_MAKE_SPECIFIED="no" | |
DISPLAY_HELP="no" | |
RUBY_OPTION_PARSING_ERROR="no" | |
SHOULD_INSTALL="no" | |
while getopts "bzslukcmnhe" opt; do | |
case $opt in | |
b) | |
#echo "-b was triggered!" >&2 | |
if [ "$ZFS_BRANCH_MATCHES_CURRENT_ZFS_BRANCH" = "no" ] ; then | |
SHOULD_SWITCH_ZFS_BRANCH="yes" | |
fi | |
if [ "$SPL_BRANCH_MATCHES_CURRENT_SPL_BRANCH" = "no" ] ; then | |
SHOULD_SWITCH_SPL_BRANCH="yes" | |
fi | |
;; | |
z) | |
#echo "-z was triggered!" >&2 | |
if [ "$ZFS_BRANCH_MATCHES_CURRENT_ZFS_BRANCH" = "no" ] ; then | |
SHOULD_SWITCH_ZFS_BRANCH="yes" | |
fi | |
;; | |
s) | |
#echo "-s was triggered!" >&2 | |
if [ "$SPL_BRANCH_MATCHES_CURRENT_SPL_BRANCH" = "no" ] ; then | |
SHOULD_SWITCH_SPL_BRANCH="yes" | |
fi | |
;; | |
l) | |
#echo "-l was triggered!" >&2 | |
SHOULD_INSTALL="yes" | |
SHOULD_LOAD="yes" | |
;; | |
u) | |
#echo "-u was triggered!" >&2 | |
SHOULD_UNLOAD="yes" | |
SHOULD_LOAD="no" | |
SHOULD_CONFIGURE="no" | |
SHOULD_MAKE="no" | |
;; | |
k) | |
#echo "-k was triggered!" >&2 | |
MUST_SKIP_CONFIGURE_AND_MAKE="yes" | |
SHOULD_INSTALL="yes" | |
SHOULD_LOAD="yes" | |
;; | |
c) | |
#echo "-c was triggered!" >&2 | |
SHOULD_CONFIGURE="yes" | |
SHOULD_MAKE="no" | |
if [ "$ONE_OF_CONFIGURE_AND_MAKE_SPECIFIED" = "yes" ] ; then | |
BOTH_CONFIGURE_AND_MAKE_SPECIFIED="yes" | |
fi | |
ONE_OF_CONFIGURE_AND_MAKE_SPECIFIED="yes" | |
;; | |
m) | |
#echo "-m was triggered!" >&2 | |
SHOULD_MAKE="yes" | |
SHOULD_CONFIGURE="no" | |
if [ "$ONE_OF_CONFIGURE_AND_MAKE_SPECIFIED" = "yes" ] ; then | |
BOTH_CONFIGURE_AND_MAKE_SPECIFIED="yes" | |
fi | |
ONE_OF_CONFIGURE_AND_MAKE_SPECIFIED="yes" | |
;; | |
n) | |
#echo "-n was triggered!" >&2 | |
DRY_RUN="yes" | |
;; | |
h) | |
#echo "-h was triggered!" >&2 | |
DISPLAY_HELP="yes" | |
;; | |
e) | |
#echo "-e was triggered!" >&2 | |
RUBY_OPTION_PARSING_ERROR="yes" | |
;; | |
\?) | |
echo "Invalid option: -$OPTARG" >&2 | |
;; | |
esac | |
done | |
if [ "$BOTH_CONFIGURE_AND_MAKE_SPECIFIED" = "yes" ] ; then | |
SHOULD_MAKE="yes" | |
SHOULD_CONFIGURE="yes" | |
fi | |
if [ "$MUST_SKIP_CONFIGURE_AND_MAKE" = "yes" ] ; then | |
SHOULD_MAKE="no" | |
SHOULD_CONFIGURE="no" | |
fi | |
if [ "$SHOULD_LOAD" = "yes" ] ; then | |
SHOULD_UNLOAD="yes" | |
SHOULD_INSTALL="yes" | |
fi | |
SK="$INSTALL_DIR"/spl.kext | |
ZK="$INSTALL_DIR"/zfs.kext | |
if [ "$SHOULD_SWITCH_ZFS_BRANCH" = "no" ] ; then | |
ZFS_BRANCH_CHANGE_STATUS="no change" | |
else | |
ZFS_BRANCH_CHANGE_STATUS="changing zfs branch to $ZFS_BRANCH" | |
fi | |
if [ "$SHOULD_SWITCH_SPL_BRANCH" = "no" ] ; then | |
SPL_BRANCH_CHANGE_STATUS="no change" | |
else | |
SPL_BRANCH_CHANGE_STATUS="changing spl branch to $SPL_BRANCH" | |
fi | |
if [ "$SHOULD_SWITCH_ZFS_BRANCH" = "yes" -a "$SHOULD_SWITCH_SPL_BRANCH" = "yes" ] ; then | |
BRANCH_CHANGE_STATUS="changing zfs and spl branches" | |
elif [ "$SHOULD_SWITCH_ZFS_BRANCH" = "yes" ] ; then | |
BRANCH_CHANGE_STATUS="changing zfs branch" | |
elif [ "$SHOULD_SWITCH_SPL_BRANCH" = "yes" ] ; then | |
BRANCH_CHANGE_STATUS="changing spl branch" | |
else | |
BRANCH_CHANGE_STATUS="no change" | |
fi | |
printf "Configuration:\n" | |
printf " dev dir = %s\n" "$DEV_DIR" | |
printf " install dir = %s\n" "$INSTALL_DIR" | |
printf " source owner = %s\n" "$OWNER" | |
printf " branch status = %s\n" "$BRANCH_CHANGE_STATUS" | |
printf " zfs branch = %s\n" "$ZFS_BRANCH_CHANGE_STATUS" | |
printf " spl branch = %s\n" "$SPL_BRANCH_CHANGE_STATUS" | |
printf " load kexts = %s\n" "$SHOULD_LOAD" | |
printf " unload kexts = %s\n" "$SHOULD_UNLOAD" | |
printf " kexts only = %s\n" "$MUST_SKIP_CONFIGURE_AND_MAKE" | |
printf " run configure = %s\n" "$SHOULD_CONFIGURE" | |
printf " run make = %s\n" "$SHOULD_MAKE" | |
printf " install kexts = %s\n" "$SHOULD_INSTALL" | |
printf " spl dir = %s\n" "$SPL_DIR" | |
printf " zfs dir = %s\n" "$ZFS_DIR" | |
printf " spl kext dir = %s\n" "$SK" | |
printf " zfs kext dir = %s\n" "$ZK" | |
printf " install dir = %s\n" "$INSTALL_DIR" | |
if [ "$RUBY_OPTION_PARSING_ERROR" = "yes" ] ; then | |
printf "Error parsing your options. Perhaps try --help.\n" | |
exit 1 | |
fi | |
if [ "$DISPLAY_HELP" = "yes" ] ; then | |
printf " %s\t\t%s\n" "Short options:" "Long options:" | |
printf "\t%s\t\t\t%s\n" '-d' '--dev-dir' | |
printf "\t%s\t\t\t%s\n" '-i' '--install-dir' | |
printf "\t%s\t\t\t%s\n" '-o' '--owner' | |
printf "\t%s\t\t\t%s\n" '-b' '--branch' | |
printf "\t%s\t\t\t%s\n" '-z' '--zfs-branch' | |
printf "\t%s\t\t\t%s\n" '-s' '--spl-branch' | |
printf "\t%s\t\t\t%s\n" '-l' '--load' | |
printf "\t%s\t\t\t%s\n" '-u' '--unload' | |
printf "\t%s\t\t\t%s\n" '-k' '--kexts-only' | |
printf "\t%s\t\t\t%s\n" '-c' '--configure' | |
printf "\t%s\t\t\t%s\n" '-m' '--make' | |
printf "\t%s\t\t\t%s\n" '-n' '--dry-run' | |
printf "\t%s\t\t\t%s\n" '-h' '--help' | |
exit 2 | |
fi | |
if [ "$DRY_RUN" = "yes" ] ; then | |
printf "Dry run. Exiting.\n" | |
exit 3 | |
fi | |
if [ "$SHOULD_UNLOAD" = "yes" ] ; then | |
if [ $(kextstat -b net.lundman.zfs | wc -l) -gt 1 ] ; then | |
printf "\nUnloading zfs.kext...\n" | |
sudo kextunload -b net.lundman.zfs | |
fi | |
if [ $(kextstat -b net.lundman.spl | wc -l) -gt 1 ] ; then | |
printf "\nUnloading spl.kext...\n" | |
sudo kextunload -b net.lundman.spl | |
fi | |
set +e | |
sudo kextstat | grep lundman | |
set -e | |
fi | |
if [ "$SHOULD_CONFIGURE" = "no" -a "$SHOULD_MAKE" = "no" -a "$SHOULD_LOAD" = "no" ] ; then | |
exit 0 | |
fi | |
if [ "$SHOULD_CONFIGURE" = "yes" -o "$SHOULD_MAKE" = "yes" ] ; then | |
if [ ! -d "$DEV_DIR" ] ; then | |
sudo mkdir "$DEV_DIR" | |
sudo chown "$OWNER":staff "$DEV_DIR" | |
fi | |
if [ ! -d "$SPL_DIR" ] ; then | |
sudo mkdir -p "$SPL_DIR" | |
cd "$SPL_DIR"/.. | |
sudo rmdir "$SPL_DIR" | |
sudo git clone git://github.com/zfs-osx/spl.git | |
cd spl | |
cd .. | |
sudo chown -R "$OWNER":staff spl | |
else | |
sudo chown -R "$OWNER":staff "$SPL_DIR" | |
cd "$SPL_DIR" | |
sudo -u "$OWNER" git pull | |
fi | |
if [ ! -d "$ZFS_DIR" ] ; then | |
sudo mkdir -p "$ZFS_DIR" | |
cd "$ZFS_DIR"/.. | |
sudo rmdir "$ZFS_DIR" | |
sudo git clone git://github.com/zfs-osx/zfs.git | |
cd zfs | |
cd .. | |
sudo chown -R "$OWNER":staff zfs | |
else | |
sudo chown -R "$OWNER":staff "$ZFS_DIR" | |
cd "$ZFS_DIR" | |
sudo -u "$OWNER" git pull | |
fi | |
fi | |
SWITCHED_SPL_BRANCH="no" | |
if [ "$SHOULD_SWITCH_SPL_BRANCH" = "yes" ] ; then | |
CURRENT_SPL_BRANCH="$(git --git-dir="$DEV_DIR"/spl/.git rev-parse --abbrev-ref HEAD)" | |
SPL_BRANCH_MATCHES="no" | |
if [ x"$CURRENT_SPL_BRANCH" = x"$SPL_BRANCH" ] ; then | |
SPL_BRANCH_MATCHES="yes" | |
printf "spl branch already matches.\n" | |
else | |
printf "spl branch does not match.\n" | |
fi | |
if [ "$SPL_BRANCH_MATCHES" = "no" ] ; then | |
cd "$SPL_DIR" | |
printf "Trying to switch spl branch ...\n" | |
sudo -u "$OWNER" git checkout "$SPL_BRANCH" | |
if [ $? -eq 0 ] ; then | |
SWITCHED_SPL_BRANCH="yes" | |
printf "Switched spl branch.\n" | |
else | |
printf "Did not switch spl branch.\n" | |
fi | |
fi | |
fi | |
SWITCHED_ZFS_BRANCH="no" | |
if [ "$SHOULD_SWITCH_ZFS_BRANCH" = "yes" ] ; then | |
CURRENT_ZFS_BRANCH="$(git --git-dir="$DEV_DIR"/zfs/.git rev-parse --abbrev-ref HEAD)" | |
ZFS_BRANCH_MATCHES="no" | |
if [ x"$CURRENT_ZFS_BRANCH" = x"$ZFS_BRANCH" ] ; then | |
ZFS_BRANCH_MATCHES="yes" | |
printf "zfs branch already matches.\n" | |
else | |
printf "zfs branch does not match.\n" | |
fi | |
if [ "$ZFS_BRANCH_MATCHES" = "no" ] ; then | |
cd "$ZFS_DIR" | |
printf "Trying to switch zfs branch ...\n" | |
sudo -u "$OWNER" git checkout "$ZFS_BRANCH" | |
if [ $? -eq 0 ] ; then | |
SWITCHED_ZFS_BRANCH="yes" | |
printf "Switched zfs branch.\n" | |
else | |
printf "Did not switch zfs branch.\n" | |
fi | |
fi | |
fi | |
if [ "$SHOULD_CONFIGURE" = "yes" ] ; then | |
cd "$SPL_DIR" | |
sudo -u "$OWNER" /bin/bash "$SPL_DIR"/autogen.sh | |
sudo -u "$OWNER" /bin/bash "$SPL_DIR"/configure CC=clang CXX=clang++ | |
cd "$ZFS_DIR" | |
sudo -u "$OWNER" /bin/bash "$ZFS_DIR"/autogen.sh | |
sudo -u "$OWNER" /bin/bash "$ZFS_DIR"/configure CC=clang CXX=clang++ --with-spl="$SPL_DIR" | |
fi | |
if [ "$SHOULD_MAKE" = "yes" ] ; then | |
cd "$SPL_DIR" | |
sudo -u "$OWNER" /usr/bin/make | |
cd "$ZFS_DIR" | |
sudo -u "$OWNER" /usr/bin/make | |
fi | |
if [ "$SHOULD_CONFIGURE" = "no" ] ; then | |
printf "\nDid not run configure.\n" | |
fi | |
if [ "$SHOULD_MAKE" = "no" ] ; then | |
printf "\nDid not run make.\n" | |
fi | |
if [ "$SHOULD_INSTALL" = "yes" ] ; then | |
sudo rm -rf "$SK" | |
sudo rm -rf "$ZK" | |
sudo mkdir -p "$INSTALL_DIR" | |
sudo cp -r "$SPL_DIR"/module/spl/spl.kext "$INSTALL_DIR" | |
sudo cp -r "$ZFS_DIR"/module/zfs/zfs.kext "$INSTALL_DIR" | |
sudo chown -R root:wheel "$SK" | |
sudo chown -R root:wheel "$ZK" | |
fi | |
if [ "$SHOULD_LOAD" = "yes" ] ; then | |
printf "\nLoading spl.kext...\n" | |
sudo kextload "$SK" | |
printf "\nLoading zfs.kext...\n\n" | |
sudo kextload -d "$SK" "$ZK" | |
set +e | |
sudo kextstat | grep lundman | |
set -e | |
else | |
printf "\nYou need to load the kexts manually or specify -l or --load\n\n" | |
fi | |
EndOfBash | |
) | /bin/bash | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment