Advertisement
s243a

bind_functions.sh

Apr 5th, 2019
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 7.06 KB | None | 0 0
  1. declare -f -F trap_push > /dev/null || \
  2.   source trap_stack.sh
  3. #The trap_stack lets us nest multiple trap statments. See:
  4. # https://stackoverflow.com/questions/16115144/save-and-restore-trap-state-easy-way-to-manage-multiple-handlers-for-traps
  5. # https://stackoverflow.com/questions/3338030/multiple-bash-traps-for-the-same-signal
  6.  
  7.  
  8. unmount_vfs(){
  9.   if [ -z "$MMT_MOUNT_POINTS" ]; then #Wild guess in case MMT_MOUNT_POINTS is not defined
  10.    for a_rootfs in "$rootfs" "root_fs" #The loop here is probably redundant
  11.      if [ ! ${a_rootfs:0:1} = "/" ]; then
  12.        if [ "${a_rootfs:0:1}" = "." ]; then
  13.           a_rootfs=`pwd`/"${a_rootfs:1:}"
  14.        else
  15.           a_rootfs="$curdir/$a_rootfs"
  16.        fi
  17.      if [ ! -z "$a_rootfs" ]; then
  18.        unmount_if_mounted "$a_rootfs/dev" 2>/dev/null
  19.        unmount_if_mounted "$a_rootfs/sys" 2>/dev/null
  20.        unmount_if_mounted "$a_rootfs/proc" 2>/dev/null
  21.      fi
  22.      if [ ! "$a_rootfs" = "/" ]; then
  23.        umount --recursive "$a_rootfs"
  24.      fi
  25.    done
  26.  #umount /mnt/wktaz 2>/dev/null
  27.  #umount /mnt/wksfs 2>/dev/null
  28.   elif [ -z "MNT_PT_ARRAYS" ] #This is the prefered branch
  29.     for a_mp in "$MMT_MOUNT_POINTS"; then
  30.       umount -l $a_mp 2>/dev/null
  31.     fi
  32.   else
  33.     for arry_name in ${MNT_PT_ARRAYS[@]}; then
  34.       arry_names_name=${1:-ALL_ArrNames}  
  35.       for a_mp in $(eval 'echo "${'$arry_names_name'[@]}"'); then
  36.         umount -l $a_mp 2>/dev/null
  37.       fi  
  38.     fi
  39.   fi
  40.  
  41. #if we source trap_stack.sh then
  42.  declare -f -F trap_push > /dev/null || \
  43.    close_file_descriptors
  44. }  
  45.  
  46.  
  47. #safe_add_mount_points(){
  48. #  mountpoint -q /home || \
  49. #    add_mount_point
  50. #}
  51. unmount_vfs_and_maybe_close_fd(){
  52.  unmount_vfs
  53.  
  54. #if we source trap_stack.sh then we cose the file descriptor in a second
  55. #trap statment. Otherwise one trap statment has to clean up everything.
  56. #The declare statment checks whether or not trap_stack.sh is sourced.
  57.  declare -f -F trap_push > /dev/null || \
  58.    close_file_descriptors
  59.  
  60. }  
  61. close_file_descriptors(){
  62.   exec 10>&-
  63. }
  64. unmount_if_mounted(){
  65.   if [ "$(mount | grep ""$1"")" != "" ]; then
  66.     if [ LAZY_UMOUNT -eq 1 ]; then
  67.       umount -l "$1" 2>/dev/null
  68.     else
  69.      umount -f "$1" 2>/dev/null
  70.     fi
  71.   fi  
  72. fi
  73. set_proc_mp_options(){
  74.    PROC_MP_ACTION=${PROC_MP_ACTION:-"-t proc none"}
  75.    case $PROC_MP_ACTION in
  76.    "-t proc none")
  77.      mp_options=( "-t" "proc" )
  78.      if [ -z "$mp_source" ]; then
  79.        mp_source="none"
  80.      fi      
  81.      ;;    
  82.    "-t proc /proc")
  83.      mp_options=( "-t" "proc" )
  84.      if [ -z "$mp_source" ]; then
  85.        mp_source="/proc"
  86.      fi
  87.      ;;  
  88.    "--rbind"|"-o rbind")
  89.      mp_options=( "-o" "rbind" )
  90.      if [ -z "$mp_source" ]; then
  91.          mp_source="/proc"
  92.      fi
  93.      ;;
  94.    "--bind"|"-o bind")
  95.      mp_options=( "-o" "bind" )
  96.      if [ -z "$mp_source" ]; then
  97.          mp_source="/proc"
  98.      fi
  99.      ;;    
  100.    *)
  101.      echo "Option PROC_MP_ACTION=\"$PROC_MP_ACTION\" not yet supported"
  102.      read -p "Press enter to continue"    
  103.      ;;
  104.    esac
  105. }
  106. set_sys_mp_options(){
  107.    SYS_MP_ACTION=${SYS_MP_ACTION:-"-t sysfs none"}
  108.    case $SYS_MP_ACTION in
  109.    "-t sys none")
  110.      mp_options=( "-t" "sysfs" )
  111.      mp_source="none"
  112.      ;;    
  113.    "-t sys /sys")
  114.      mp_options=( "-t" "sysfs" )
  115.      if [ -z "$mp_source" ]; then
  116.        mp_source="/sys"
  117.      fi
  118.      ;;  
  119.    "--rbind"|"-o rbind")
  120.      mp_options=( "-o" "rbind" )
  121.      if [ -z "$mp_source" ]; then
  122.          mp_source="/sys"
  123.      fi
  124.      ;;
  125.    "--bind"|"-o bind")
  126.      mp_options=( "-o" "bind" )
  127.      if [ -z "$mp_source" ]; then
  128.          mp_source="/sys"
  129.      fi
  130.      ;;  
  131.    *)
  132.      echo "Option SYS_MP_ACTION=\"$SYS_MP_ACTION\" not yet supported"
  133.      read -p "Press enter to continue"    
  134.      ;;    
  135.    esac
  136. }
  137. set_dev_mp_options(){
  138.    DEV_MP_ACTION=${DEV_MP_ACTION:-"-o rbind"}
  139.    case $DEV_MP_ACTION in
  140.    "--rbind"|"-o rbind")
  141.      mp_options=( "-o" "rbind" )
  142.      if [ -z "mp_source" ]; then
  143.          mp_source="/dev"
  144.      fi
  145.      ;;
  146.    "--bind"|"-o bind") #Maybe also bind /dev/pts and /dev/shm if you do this
  147.      mp_options=( "-o" "bind" )
  148.      if [ -z "mp_source" ]; then
  149.          mp_source="/dev"
  150.      fi
  151.      ;;  
  152.    *)
  153.      echo "Option DEV_MP_ACTION=\"$DEV_MP_ACTION\" not yet supported"
  154.      read -p "Press enter to continue"    
  155.      ;;  
  156.    esac
  157. }
  158. set_dev_pts_mp_options(){
  159.    DEV_PTS_MP_ACTION=${DEV_PTS_MP_ACTION:-"-o bind"}
  160.    case $DEV_PTS_MP_ACTION in
  161.    "--bind"|"-o bind") #Maybe also bind /dev/pts and /dev/shm if you do this
  162.      mp_options=( "-o" "bind" )
  163.      if [ -z "mp_source" ]; then
  164.          mp_source="/dev/pts"
  165.      fi
  166.      ;;
  167.    *)
  168.      echo "Option DEV_PTS_MP_ACTION=\"$DEV_PTS_MP_ACTION\" not yet supported"
  169.      read -p "Press enter to continue"    
  170.      ;;
  171.    esac
  172. }
  173. set_dev_pts_shm_options(){
  174.    DEV_SHM_MP_ACTION=${DEV_SHM_MP_ACTION:-"-o bind"}
  175.    case $DEV_PTS_MP_ACTION in
  176.    "--bind"|"-o bind") #Maybe also bind /dev/pts and /dev/shm if you do this
  177.      mp_options=( "-o" "bind" )
  178.      if [ -z "mp_source" ]; then
  179.          mp_source="/dev/shm"
  180.      fi
  181.      ;;
  182.    *)
  183.      echo "Option not yet supported"
  184.      read -p "Press enter to continue"    
  185.      ;;
  186.    esac
  187. }
  188. add_mp_and_check_path(){
  189.   arg_count=$#
  190.   mp_path=${@[-1]}
  191.   #https://stackoverflow.com/questions/16860877/remove-an-element-from-a-bash-array
  192.   unset '@[-1]' #Delete the last element of the array
  193.   if [ $arg_count >1 ]; then
  194.     mp_source=${@[-1]}
  195.     unset '@[-1]'
  196.   fi
  197.   mp_options=( "$@" )
  198.   if [ -z "$option" ]; then
  199.     case "basename $mp_path" in
  200.     "proc")
  201.        set_proc_mp_options #The default is: option=( "-t" "proc" ); mp_source="none"
  202.        ;;
  203.     "sys")
  204.        set_sys_mp_options #The default is: option=( "-t" "sysfs" ); mp_source="none"
  205.        ;;
  206.     "dev")
  207.        #example said: https://unix.stackexchange.com/questions/24140/return-only-the-portion-of-a-line-after-a-matching-pattern
  208.        #sed -n -e 's/^\(.*\)\(stalled: \)\(.*\)$/\3\2\1/p'
  209.        #How I might use sed but I have a better idea
  210.        #sed -n -e 's/^\(.*\)\/dev\/\(.*\)$/\3\2\1/p'
  211.        case mp_path in
  212.        */dev/shm)
  213.          set_dev_pts_shm_options #The default is: option=( "-o" "bind" ); mp_source="/dev/shm"
  214.          ;;
  215.        */dev/pts)
  216.          set_dev_pts_mp_options #The default is: option=( "-o" "bind" ); mp_source="/dev/pts"
  217.          ;;
  218.        */dev)
  219.          #TODO add check to see if we have either mounted or plan to moun
  220.          #/dev/shm /dev/pts and if so change option from rbind to bind.  
  221.          set_dev_mp_options
  222.        ;;
  223.        esac  
  224.   fi
  225.   already_mounted=0
  226.   mountpoint -q "$mp_path" && already_mounted=1
  227.   if [ $already_mounted -eq 1 ]; then
  228.     MMT_MOUNT_POINTS+=( "$mp_path" )
  229.     mount "${mp_options[@]}" "$mp_source" "$mp_path"
  230.   fi
  231.        
  232.  
  233. }
  234. bind_dirs(){
  235.   local a_curd="`pwd`"
  236.   for a_dir in $@; do
  237.     #This step isn't necessary be is a percauation in case a "cd" accidently
  238.     #gets inserted on a function called by this function.
  239.     if [ ! "${a_dir:0:1" = "/" ]; then
  240.       "$a_curd/$a_dir"
  241.     fi
  242.     add_mp_and_check_path "$a_dir"
  243.   done
  244. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement