Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- declare -f -F trap_push > /dev/null || \
- source trap_stack.sh
- #The trap_stack lets us nest multiple trap statments. See:
- # https://stackoverflow.com/questions/16115144/save-and-restore-trap-state-easy-way-to-manage-multiple-handlers-for-traps
- # https://stackoverflow.com/questions/3338030/multiple-bash-traps-for-the-same-signal
- unmount_vfs(){
- if [ -z "$MMT_MOUNT_POINTS" ]; then #Wild guess in case MMT_MOUNT_POINTS is not defined
- for a_rootfs in "$rootfs" "root_fs" #The loop here is probably redundant
- if [ ! ${a_rootfs:0:1} = "/" ]; then
- if [ "${a_rootfs:0:1}" = "." ]; then
- a_rootfs=`pwd`/"${a_rootfs:1:}"
- else
- a_rootfs="$curdir/$a_rootfs"
- fi
- if [ ! -z "$a_rootfs" ]; then
- unmount_if_mounted "$a_rootfs/dev" 2>/dev/null
- unmount_if_mounted "$a_rootfs/sys" 2>/dev/null
- unmount_if_mounted "$a_rootfs/proc" 2>/dev/null
- fi
- if [ ! "$a_rootfs" = "/" ]; then
- umount --recursive "$a_rootfs"
- fi
- done
- #umount /mnt/wktaz 2>/dev/null
- #umount /mnt/wksfs 2>/dev/null
- elif [ -z "MNT_PT_ARRAYS" ] #This is the prefered branch
- for a_mp in "$MMT_MOUNT_POINTS"; then
- umount -l $a_mp 2>/dev/null
- fi
- else
- for arry_name in ${MNT_PT_ARRAYS[@]}; then
- arry_names_name=${1:-ALL_ArrNames}
- for a_mp in $(eval 'echo "${'$arry_names_name'[@]}"'); then
- umount -l $a_mp 2>/dev/null
- fi
- fi
- fi
- #if we source trap_stack.sh then
- declare -f -F trap_push > /dev/null || \
- close_file_descriptors
- }
- #safe_add_mount_points(){
- # mountpoint -q /home || \
- # add_mount_point
- #}
- unmount_vfs_and_maybe_close_fd(){
- unmount_vfs
- #if we source trap_stack.sh then we cose the file descriptor in a second
- #trap statment. Otherwise one trap statment has to clean up everything.
- #The declare statment checks whether or not trap_stack.sh is sourced.
- declare -f -F trap_push > /dev/null || \
- close_file_descriptors
- }
- close_file_descriptors(){
- exec 10>&-
- }
- unmount_if_mounted(){
- if [ "$(mount | grep ""$1"")" != "" ]; then
- if [ LAZY_UMOUNT -eq 1 ]; then
- umount -l "$1" 2>/dev/null
- else
- umount -f "$1" 2>/dev/null
- fi
- fi
- fi
- set_proc_mp_options(){
- PROC_MP_ACTION=${PROC_MP_ACTION:-"-t proc none"}
- case $PROC_MP_ACTION in
- "-t proc none")
- mp_options=( "-t" "proc" )
- if [ -z "$mp_source" ]; then
- mp_source="none"
- fi
- ;;
- "-t proc /proc")
- mp_options=( "-t" "proc" )
- if [ -z "$mp_source" ]; then
- mp_source="/proc"
- fi
- ;;
- "--rbind"|"-o rbind")
- mp_options=( "-o" "rbind" )
- if [ -z "$mp_source" ]; then
- mp_source="/proc"
- fi
- ;;
- "--bind"|"-o bind")
- mp_options=( "-o" "bind" )
- if [ -z "$mp_source" ]; then
- mp_source="/proc"
- fi
- ;;
- *)
- echo "Option PROC_MP_ACTION=\"$PROC_MP_ACTION\" not yet supported"
- read -p "Press enter to continue"
- ;;
- esac
- }
- set_sys_mp_options(){
- SYS_MP_ACTION=${SYS_MP_ACTION:-"-t sysfs none"}
- case $SYS_MP_ACTION in
- "-t sys none")
- mp_options=( "-t" "sysfs" )
- mp_source="none"
- ;;
- "-t sys /sys")
- mp_options=( "-t" "sysfs" )
- if [ -z "$mp_source" ]; then
- mp_source="/sys"
- fi
- ;;
- "--rbind"|"-o rbind")
- mp_options=( "-o" "rbind" )
- if [ -z "$mp_source" ]; then
- mp_source="/sys"
- fi
- ;;
- "--bind"|"-o bind")
- mp_options=( "-o" "bind" )
- if [ -z "$mp_source" ]; then
- mp_source="/sys"
- fi
- ;;
- *)
- echo "Option SYS_MP_ACTION=\"$SYS_MP_ACTION\" not yet supported"
- read -p "Press enter to continue"
- ;;
- esac
- }
- set_dev_mp_options(){
- DEV_MP_ACTION=${DEV_MP_ACTION:-"-o rbind"}
- case $DEV_MP_ACTION in
- "--rbind"|"-o rbind")
- mp_options=( "-o" "rbind" )
- if [ -z "mp_source" ]; then
- mp_source="/dev"
- fi
- ;;
- "--bind"|"-o bind") #Maybe also bind /dev/pts and /dev/shm if you do this
- mp_options=( "-o" "bind" )
- if [ -z "mp_source" ]; then
- mp_source="/dev"
- fi
- ;;
- *)
- echo "Option DEV_MP_ACTION=\"$DEV_MP_ACTION\" not yet supported"
- read -p "Press enter to continue"
- ;;
- esac
- }
- set_dev_pts_mp_options(){
- DEV_PTS_MP_ACTION=${DEV_PTS_MP_ACTION:-"-o bind"}
- case $DEV_PTS_MP_ACTION in
- "--bind"|"-o bind") #Maybe also bind /dev/pts and /dev/shm if you do this
- mp_options=( "-o" "bind" )
- if [ -z "mp_source" ]; then
- mp_source="/dev/pts"
- fi
- ;;
- *)
- echo "Option DEV_PTS_MP_ACTION=\"$DEV_PTS_MP_ACTION\" not yet supported"
- read -p "Press enter to continue"
- ;;
- esac
- }
- set_dev_pts_shm_options(){
- DEV_SHM_MP_ACTION=${DEV_SHM_MP_ACTION:-"-o bind"}
- case $DEV_PTS_MP_ACTION in
- "--bind"|"-o bind") #Maybe also bind /dev/pts and /dev/shm if you do this
- mp_options=( "-o" "bind" )
- if [ -z "mp_source" ]; then
- mp_source="/dev/shm"
- fi
- ;;
- *)
- echo "Option not yet supported"
- read -p "Press enter to continue"
- ;;
- esac
- }
- add_mp_and_check_path(){
- arg_count=$#
- mp_path=${@[-1]}
- #https://stackoverflow.com/questions/16860877/remove-an-element-from-a-bash-array
- unset '@[-1]' #Delete the last element of the array
- if [ $arg_count >1 ]; then
- mp_source=${@[-1]}
- unset '@[-1]'
- fi
- mp_options=( "$@" )
- if [ -z "$option" ]; then
- case "basename $mp_path" in
- "proc")
- set_proc_mp_options #The default is: option=( "-t" "proc" ); mp_source="none"
- ;;
- "sys")
- set_sys_mp_options #The default is: option=( "-t" "sysfs" ); mp_source="none"
- ;;
- "dev")
- #example said: https://unix.stackexchange.com/questions/24140/return-only-the-portion-of-a-line-after-a-matching-pattern
- #sed -n -e 's/^\(.*\)\(stalled: \)\(.*\)$/\3\2\1/p'
- #How I might use sed but I have a better idea
- #sed -n -e 's/^\(.*\)\/dev\/\(.*\)$/\3\2\1/p'
- case mp_path in
- */dev/shm)
- set_dev_pts_shm_options #The default is: option=( "-o" "bind" ); mp_source="/dev/shm"
- ;;
- */dev/pts)
- set_dev_pts_mp_options #The default is: option=( "-o" "bind" ); mp_source="/dev/pts"
- ;;
- */dev)
- #TODO add check to see if we have either mounted or plan to moun
- #/dev/shm /dev/pts and if so change option from rbind to bind.
- set_dev_mp_options
- ;;
- esac
- fi
- already_mounted=0
- mountpoint -q "$mp_path" && already_mounted=1
- if [ $already_mounted -eq 1 ]; then
- MMT_MOUNT_POINTS+=( "$mp_path" )
- mount "${mp_options[@]}" "$mp_source" "$mp_path"
- fi
- }
- bind_dirs(){
- local a_curd="`pwd`"
- for a_dir in $@; do
- #This step isn't necessary be is a percauation in case a "cd" accidently
- #gets inserted on a function called by this function.
- if [ ! "${a_dir:0:1" = "/" ]; then
- "$a_curd/$a_dir"
- fi
- add_mp_and_check_path "$a_dir"
- done
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement