2024-02-12 12:53:25 -08:00
|
|
|
#!/bin/bash
|
|
|
|
|
|
|
|
|
|
|
|
if ! which fusermount &> /dev/null; then
|
|
|
|
echo unabled to load bindfs module because bindfs is not installed
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
# echo loading bindfs
|
|
|
|
|
|
|
|
export BFS_MOUNT_DIR=${BFS_MOUNT_DIR:-/mnt/bfs}
|
|
|
|
|
|
|
|
if [ -v PS1 ]; then
|
|
|
|
alias rbfsu="dir_rebind_user"
|
|
|
|
alias rbfs="dir_rebind"
|
|
|
|
alias bfsu="dir_bind_user"
|
|
|
|
alias bfs="dir_bind"
|
|
|
|
alias bfsum="dir_bind_unmount"
|
|
|
|
alias bfse="bfs_vscode"
|
2024-12-07 13:42:53 -08:00
|
|
|
|
|
|
|
if [[ ! -d $BFS_MOUNT_DIR ]]; then
|
|
|
|
sudo mkdir $BFS_MOUNT_DIR 2> /dev/null
|
|
|
|
sudo chown $USER:$USER $BFS_MOUNT_DIR
|
|
|
|
fi
|
2024-02-12 12:53:25 -08:00
|
|
|
fi
|
|
|
|
|
|
|
|
mounted () {
|
|
|
|
mountpoint "$1" &> /dev/null && echo yes || return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
dir_bind_unmount () {
|
|
|
|
local usesudo
|
2024-08-03 14:49:44 -07:00
|
|
|
local mp=${1:-$PWD}
|
2024-02-12 12:53:25 -08:00
|
|
|
[[ ! $(mounted $mp) ]] && echo no mountpoint at $mp && mp=$BFS_MOUNT_DIR/$(basename $mp)
|
|
|
|
[[ ! $(mounted $mp) ]] && echo no mountpoint at $mp either, aborting && return 1
|
|
|
|
[[ $EUID -ne 0 ]] && usesudo=sudo
|
|
|
|
if $usesudo fusermount -u $mp; then
|
|
|
|
echo unmounted $mp, removing empty mountpoint directory
|
|
|
|
rm -rf $mp
|
|
|
|
else
|
|
|
|
echo error, unable to unmount $mp
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
dir_bind_user () {
|
|
|
|
local usesudo; local dir;local user;local group;local mp
|
|
|
|
|
|
|
|
if [ $# -lt 3 ]; then
|
|
|
|
echo "minimum 3 args needed to rebind <user? <source dir> <mountpoint>"
|
|
|
|
echo passed were $@
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
[[ $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
|
|
|
|
[[ $(mounted $mp) ]] && echo something already mounted at $mp, aborting && return 1
|
|
|
|
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
|
|
|
|
[[ $? -gt 0 ]] && echo error in call to bindfs
|
|
|
|
|
|
|
|
if [[ $(mounted $mp) ]]; then
|
|
|
|
echo $dir has been mounted at $mp for user $1
|
|
|
|
echo "to unmount use: dir_bind_unmount $mp or bfsum $mp"
|
|
|
|
else
|
|
|
|
echo unable to mount $dir at $mp as user $user
|
2024-08-03 14:49:44 -07:00
|
|
|
fi
|
2024-02-12 12:53:25 -08:00
|
|
|
|
2024-08-03 14:49:44 -07:00
|
|
|
}
|
2024-02-12 12:53:25 -08:00
|
|
|
|
|
|
|
dir_bind () {
|
2024-08-03 14:49:44 -07:00
|
|
|
dir=${1:-$PWD}
|
|
|
|
mp=${2:-$BFS_MOUNT_DIR/$(basename "$dir")}
|
|
|
|
dir_bind_user $USER $dir $mp
|
2024-02-12 12:53:25 -08:00
|
|
|
if [ -v PS1 ]; then
|
2024-08-03 14:49:44 -07:00
|
|
|
if [[ $BFSEDIT ]]; then /opt/bin/vscode $mp 1>/dev/null ; else xdg-open $mp; fi
|
|
|
|
echo $BFSEDIT
|
|
|
|
echo enter \"u\" to unmount, otherwise any other key will leave mounted
|
|
|
|
unset BFSEDIT
|
|
|
|
read -n1 ans
|
|
|
|
echo -e "\n"
|
|
|
|
[[ $ans == "u" ]] && dir_bind_unmount $mp
|
2024-02-12 12:53:25 -08:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
dir_rebind () {
|
|
|
|
dir_bind_user $USER $1 $1
|
|
|
|
}
|
|
|
|
|
|
|
|
dir_rebind_user () {
|
|
|
|
dir_bind_user $1 $2 $2
|
|
|
|
}
|
|
|
|
|
|
|
|
bfs_vscode () {
|
2024-08-03 14:49:44 -07:00
|
|
|
BFSEDIT="When done editing FIRST close your vscode window then"
|
|
|
|
dir_bind "$@"
|
2024-02-12 12:53:25 -08:00
|
|
|
}
|
|
|
|
|
2024-08-03 14:49:44 -07:00
|
|
|
|
2024-02-12 12:53:25 -08:00
|
|
|
(return 0 2>/dev/null) || dir_bind_user $@
|
|
|
|
|