Skip to content

Instantly share code, notes, and snippets.

@vena vena/mount-greyhole
Created Jun 2, 2012

Embed
What would you like to do?
[Greyhole] Upstart tools for mounting shares locally at startup
#!/bin/sh
# Mounts available greyhole shares locally
# - Depends on "greyhole" in comment property for SMB share definition
# - Depends on .smb_credentials file in defined MGS_USER's home dir
# Default environment
[ -z "$MGS_USER" ] && MGS_USER=`id -u`
[ -z "$MGS_GROUP" ] && MGS_GROUP=`id -g`
[ -z "$MGS_ROOT" ] && MGS_ROOT=`pwd`
[ -z "$MGS_FILE_MODE" ] && MGS_FILE_MODE="0744"
[ -z "$MGS_DIR_MODE" ] && MGS_DIR_MODE="0744"
# Command line arguments
while [ "$#" -gt "0" ]
do
case "$1" in
-u|--user)
shift
MGS_USER="$1"
;;
-g|--group)
shift
MGS_GROUP="$1"
;;
-r|--root)
shift
MGS_ROOT="$1"
;;
-f|--fmode)
shift
MGS_FILE_MODE="$1"
;;
-d|--dmode)
shift
MGS_DIR_MODE="$1"
;;
*)
echo "Unknown option. Usage: $0 [-u|--user username] [-g|--group groupname] [-r|--root path] [-f|--fmode ####] [-d|--dmode ####]"
exit 1
;;
esac
shift
done
id ${MGS_USER} 1>/dev/null 2>&- || { logger -st mount-greyhole-shares "User ${MGS_USER} does not exist, aborting."; exit 1; }
grep "^${MGS_GROUP}:" /etc/group 1>/dev/null 2>&- || { logger -st mount-greyhole-shares "Group ${MGS_GROUP} does not exist, aborting."; exit 1; }
UID=`id -u $MGS_USER`
GID=`grep "^${MGS_GROUP}:" /etc/group | cut -d: -f3`
SMBOPTS="credentials=/home/${MGS_USER}/.smb_credentials,uid=${UID},gid=${GID},file_mode=${MGS_FILE_MODE},dir_mode=${MGS_DIR_MODE},nobrl,hard,_netdev,iocharset=utf8,noserverinfo,mfsymlinks"
SHARECHECK=''
CHECKCOUNT=0
do_mounts()
{
SAVEIFS=$IFS
IFS='|'
umount $MGS_ROOT/* 2>/dev/null
for SHARE in `/usr/bin/smbclient -gNL localhost 2>/dev/null | perl -n -e'/^Disk\|([^\|]*).*greyhole/ && print "$1|"'`; do
if [ "`mount | grep "//127.0.0.1/$SHARE" | wc -l`" != 0 ]; then
umount -f "$MGS_ROOT/$SHARE" 2>/dev/null
fi
if [ -d "$MGS_ROOT/$SHARE" ]; then
rm -rf "$MGS_ROOT/$SHARE" 2>/dev/null
fi
logger -t mount-greyhole-shares "Mounting ${SHARE}"
mkdir -p "$MGS_ROOT/$SHARE"
/sbin/mount.cifs //127.0.0.1/"${SHARE}" "$MGS_ROOT/$SHARE" -o $SMBOPTS | logger -t mount-greyhole-shares
SHARECHECK=$SHARE
done
IFS=$SAVEIFS
check_mounts
}
check_count()
{
if [ $CHECKCOUNT -ge 60 ]; then
logger -st mount-greyhole-shares 'Cycled 60 times, aborting.'
exit 2
fi
CHECKCOUNT=`expr $CHECKOUT + 1`
}
check_mounts()
{
logger -t mount-greyhole-shares "Checking for last attempted share mount: ${SHARECHECK}"
if [ "`mount | grep "//127.0.0.1/$SHARECHECK" | wc -l`" = 0 ]; then
check_count
/bin/sleep 2s
check_mounts
return 2
else
if [ `stat -c %u "$MGS_ROOT/$SHARECHECK"` -ne $UID ]; then
check_count
logger -t mount-greyhole-shares 'Share found, but owner does not match expected. Retrying...'
/bin/sleep 2s
check_mounts
return 2
fi
fi
logger -t mount-greyhole-shares 'Share found, mounting successful.'
}
do_mounts
description "Mount Greyhole shares"
author "daniel vena <deaconvena+mount-greyhole-locally@gmail.com>"
start on runlevel [2345]
stop on runlevel [016]
console log
emits mount-greyhole-shares
env MGS_USER="usertomountas"
env MGS_GROUP="grouptomountas"
env MGS_ROOT="/root-dir-to-mount-to"
env MGS_FILE_MODE="0665"
env MGS_DIR_MODE="0775"
pre-stop script
/usr/bin/umount-greyhole
end script
script
/usr/bin/mount-greyhole
/bin/sleep 5s
logger -t mount-greyhole-shares 'Greyhole mount process complete.'
end script
#!/bin/sh
# Unmounts greyhole shares
# - Depends on "greyhole" in comment property for SMB share definition
# Default environment
[ -z "$MGS_ROOT" ] && MGS_ROOT=`pwd`
# Command line arguments
while [ "$#" -gt "0" ]
do
case "$1" in
-r|--root)
shift
MGS_ROOT="$1"
;;
*)
echo "Unknown option. Usage: $0 [-r|--root path]"
exit 1
;;
esac
shift
done
SAVEIFS=$IFS
IFS='|'
for SHARE in `/usr/bin/smbclient -gNL localhost 2>/dev/null | perl -n -e'/^Disk\|([^\|]*).*greyhole/ && print "$1|"'`; do
if [ "`mount | grep "//127.0.0.1/$SHARE" | wc -l`" -ne 0 ]; then
umount -f "$MGS_ROOT/$SHARE" 2>/dev/null
fi
done
IFS=$SAVEIFS
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.