shell-host/modules/bindfs.lib

100 lines
2.9 KiB
Bash

#!/bin/bash
#bmount () {}
# sudo bindfs -o allow_other -o map=root/1001:@root/@1001 /var/lib/docker/volumes /data/dvols
if [ -v PS1 ]; then
alias drbu="dir_rebind_user"
alias dbu="dir_bind_user"
alias dbum="dir_bind_unmount"
fi
dir_rebind_user () {
local usesudo; local dir;local user;local group
# $1 new owner
# $2 dir
[ $# -lt 2 ] && echo minimum args needed to rebind are user and directory && return 1
[[ $EUID -ne 0 ]] && usesudo=sudo
[[ $(id -u $1 2> /dev/null) ]] || { echo user $1 does not exist can not continue; return 2; }
dir=$(realpath $2)
user=$(stat -c '%U' $dir)
group=$(stat -c '%G' $dir)
$usesudo bindfs --force-user=$1 --force-group=$1 --create-for-user=$user --create-for-group=$group --chown-ignore --chgrp-ignore $dir $dir
# echo use \'dir_rebind_user_remove $dir\' to remove
}
dir_bind_unmount () {
local usersudo
[[ $EUID -ne 0 ]] && usesudo=sudo
$usesudo fusermount -u $1
}
dir_bind_user () {
local usesudo; local dir;local user;local group;local mp
[ $# -lt 3 ] && echo minimum 3 args needed to rebind are user, source directory and mountpoint && return 1
[[ $EUID -ne 0 ]] && usesudo=sudo
[[ $(id -u $1 2> /dev/null) ]] || { echo user $1 does not exist can not continue; return 2; }
dir=$(realpath $2)
mp=$3
if ! $usesudo mkdir -p $mp; then echo unable to make mountpoint aborting; return 2; fi
$usesudo chown $1:$1 $mp
user=$(stat -c '%U' $dir)
group=$(stat -c '%G' $dir)
$usesudo bindfs --force-user=$1 --force-group=$1 --create-for-user=$user --create-for-group=$group --chown-ignore --chgrp-ignore $dir $mp
# echo use \'dir_rebind_user_remove $dir\' to remove
}
# module_load bindfs && dir_bind_user david /mnt/datadrive/@images /data/images
(return 0 2>/dev/null) || dir_bind_user $@
# function bumount {
# echo "removing bind mount at $1"
# notify-send "removing bind mount at ${1}" --icon=dialog-information -t 2000
# fusermount -u "$1"
# }
# function bmount {
# if [ "$1" == "-d" ]; then
# bumount $2
# else
# if [[ " ${array[@]} " =~ " ${value} " ]]; then
# # whatever you want to do when array contains value
# fi
# mp="${@: -2}"
# echo mounting $@
# bindfs $@
# fi
# }
# must have fuser and bindfs installed
# for use by sudoers
# ln -s $BASH_SHELL_HOST/all/modules/bind-mount/bind-mount.sh /opt/bin/bmount
# function xxbmount () {
# if [ "$1" == "-mp" ]; then
# MOUNTED=$(mountpoint "$2" | grep not)
# if [ -z "$MOUNTED" ]; then
# echo $2 is a mount point so bind mounting $2/$3 to $4
# notify-send "bind mounting ${2}/${3} to ${4}" --icon=dialog-information -t 2000
# bindfs "$2/$3" "$4"
# else
# notify-send "${2} not a mount point - Unable to bind mount ${2}/${3} to ${4}" --icon=dialog-error -t 2000
# fi
# else
# echo bind mounting $1 to $2
# notify-send "bind mounting ${1} to ${2}" --icon=dialog-information -t 2000
# bindfs "$1" "$2"
# fi
# }