Advertisement
s243a

twistedSave

Feb 22nd, 2019
447
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 7.44 KB | None | 0 0
  1. #!/bin/bash
  2. ######################### Intializations ###########################
  3. curdir=`pwd` #Not sure if we need this
  4. OUT="/mnt/home/symlinks"
  5. TMP='/mnt/home/symlinks' #probably not necessary
  6. #We will sync /initrd/pup_rw so we don't need to merge it manually in ROOTS array.
  7. declare -a  ROOTS=\
  8. ( "/initrd/pup_ro2" \
  9.   "/" \
  10. )
  11.  
  12.  
  13. declare -a  main_dirs_to_link_and_bind=\
  14. ( "/bin" "/etc" "/lib" \
  15.    "/opt" "/sbin" "/usr" "/var"  \
  16. )
  17. declare -a  other_dirs_to_link_and_bind=\
  18. ( "/boot" "/home"  \
  19.    "/root" "/run"  \
  20. )
  21. declare -a  dirs_to_link_and_bind=\
  22. ( ${main_dirs_to_link_and_bind[@]} \
  23.   ${other_dirs_to_link_and_bind[@]} \
  24. )
  25. TARGET_ROOT=$OUT
  26. #------------------------------------------
  27. #Modify and uncomment if you want the new root in ram.
  28. #TARGET_ROOT="/chroot"
  29. ##if [ ! -d $TARGET_ROOT ]; then
  30. ##  #https://www.hecticgeek.com/2015/12/create-ram-disk-ubuntu-linux/
  31. ##  mount -t tmpfs -o size=250M,nr_inodes=5k,mode=1777 tmpfs2 $TARGET_ROOT
  32. ##fi
  33. #------------------------------------------
  34. #ROOT_REL_PATH  #Variable not yet defined
  35.  
  36. decode_id "$ROOT_UUI" #Not sure if we use this
  37.  
  38. ######################### MAKE DIRECTORIES ###########################
  39. #------------------------------------------
  40. declare -a  HOST_RBIND_POINTS=\
  41. ( "/sys" \
  42.    "/dev" \
  43. )
  44. for aDIR in ${HOST_RBIND_POINTS[@]}; do
  45.   mkdir -p "$TARGET_ROOT$aDIR"
  46.   mount --rbind "$aDIR" "$TARGET_ROOT$aDIR"
  47. done
  48. #------------------------------------------
  49. declare -a  HOST_MOUNT_T_POINTS=\
  50. ( "/proc" \
  51. )
  52. for aDIR in ${HOST_MOUNT_T_POINTS[@]}; do
  53.   mkdir -p "$TARGET_ROOT$aDIR"
  54.   mount -t proc "$aDIR" "$TARGET_ROOT$aDIR"
  55. done
  56. #------------------------------------------
  57. declare -a  HOST_COPY=\
  58. ( "/etc/resolv.conf" "/etc/hosts" \
  59. )
  60. for aFile in ${HOST_COPY[@]}; do
  61.   mkdir -p "$TARGET_ROOT$aFile"
  62.   cp -a -u  "$aFile" "$TARGET_ROOT$aFile"
  63. done
  64. #------------------------------------------
  65. declare -a  HOST_RBIND_MAP=\
  66. ( "/" "/mnt/host" \
  67. )
  68. while read SOURCE; read TARGET; do
  69.   mkdir -p "$TARGET_ROOT$TARGET"
  70.   cd "$TARGET_ROOT"; cd "$SOURCE"
  71.   mount --rbind . "$TARGET_ROOT$TARGET"
  72. done <<EOF
  73. `for a in ${HOST_RBIND_MAP[@]}; do echo "$a"; done`
  74. EOF
  75. #------------------------------------------
  76. #mkdir $OUT
  77. #------------------------------------------
  78. declare -a  new_dirs=\
  79. ( "/tmp"  \
  80. )
  81. mkdir -p "$OUT"
  82. for aDir in ${new_dirs[@]}; do
  83.   mkdir -p "$TARGET_ROOT$aDir"
  84. done
  85. ######################### FUNCTIONS ###########################
  86.  
  87.  
  88. ###################### Functions ############
  89.  
  90. bind_and_link_array(){
  91.   local -n one=${1} # https://stackoverflow.com/questions/16461656/how-to-pass-array-as-an-argument-to-a-function-in-bash
  92.   if [ $# -lt 2 ]; then
  93.     SR="$SAVE_ROOT"
  94.   else
  95.     SR=$2
  96.   fi
  97.   one=${one[*]} #https://stackoverflow.com/questions/3348443/a-confusion-about-array-versus-array-in-the-context-of-a-bash-comple?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa
  98.   one=${one#'('} #https://stackoverflow.com/questions/16623835/remove-a-fixed-prefix-suffix-from-a-string-in-bash?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa
  99.   one=${one%')#'}
  100.   echo "Ln 45 one=${one[@]}"   
  101.   #example from: https://stackoverflow.com/questions/34555278/bash-read-a-looping-on-null-delimited-string-variable
  102.   #while IFS= read -r -d '' myvar; do echo $myvar; done < <(find . -type f -print0)
  103.   #while IFS= read -r -d '' a; do
  104.   #    echo "LN#43 bind_and_link $a"
  105.   #    bind_and_link "$a" "$SR"
  106.   #done < <( "${one[@]}" )
  107.   for a in $one; do
  108.      echo "LN#43 bind_and_link $a"
  109.      bind_and_link "$a" "$SR"
  110.   done
  111. }
  112.  
  113.  
  114. bind_and_link_dir(){
  115.   #local -n one=$1
  116.   if [ $# -lt 2 ]; then
  117.     export SR="$SAVE_ROOT"
  118.   else
  119.     export SR=$2
  120.   fi
  121.  
  122.    
  123.   one="$1"
  124.   echo "LN#65 one=$one"  
  125.   #echo "LN#63 one=${one[@]}"
  126.   #declair -A DIRS #https://stackoverflow.com/questions/1063347/passing-arrays-as-parameters-in-bash
  127.   [ ! `/bin/mountpoint -q "$one"` ] || return _
  128.   #example from: https://stackoverflow.com/questions/34555278/bash-read-a-looping-on-null-delimited-string-variable
  129.   #while IFS= read -r -d '' myvar; do echo $myvar; done < <(find . -type f -print0)
  130.   #DIRS=`find "$SR$one"  -mindepth 1 -maxdepth 1 -print0 -name '*' `
  131.   #decliar -A DIRS2
  132.   #DIRS2=()
  133.   #DIRS=`while IFS= read -r -d '' myvar; do echo "${myvar#$SR}"; done < <( "${DIRS[@]}" )`
  134.   #find $SR$one -mindepth 1 -maxdepth 1 -print0 -name '*' |
  135.   DIRS2=( $( find "$SR$one" -mindepth 1 -maxdepth 1 -print0 -name '*'  | removePrefix "$SR" ) )#
  136.   #IFS="$'\0'"
  137.   #for a in $DIRS; do
  138.   #  echo "a=$a"
  139.   #  DIRS2+=( $a )
  140.   #
  141.   echo "LN#82 DIRS2=${DIRS2[@]}"
  142.   bind_and_link_array DIRS2 $2
  143. }
  144. bind_and_link(){
  145.   f="$1"; f=/${f#/}
  146.   if [ $# -gt 1 ]; then
  147.     SR="$2"
  148.   else
  149.     SR="$SAVE_ROOT"
  150.   fi
  151.   if [ $# -lt 3 ]; then
  152.     export TR="$TARGET_ROOT"
  153.   else
  154.     export TR=$3
  155.   fi  
  156.   #https://stackoverflow.com/questions/4561153/check-for-symbolic-link
  157.   #https://unix.stackexchange.com/questions/167610/determining-if-a-file-is-a-hard-link-or-symbolic-link
  158.   #http://pubs.opengroup.org/onlinepubs/9699919799/utilities/test.html
  159.   IsBind="True"
  160.   mountpoint -q "$f" || IsBind="False"
  161.   echo "f=$f IsBind=$IsBind"
  162.   if [ "$IsBind" = "False" ]; then
  163.      echo "Ln 74 Not a mount point"
  164.     if [ -L "$SR$f" ]; then #$f should start w/ slash TODO add slash if missing
  165.       echo "LN#76 Copying $f"
  166.       cp -a -u "$SR$f" "$TR$f"
  167.       echo "$f" >> $OUT/coppied_links
  168.     elif [ -d "$SR$f" ]; then    
  169.       if [ -d "$TR$f" ]; then
  170.         echo "LN#85 bind_and_link_dir $f"
  171.         #Could user findmnt instead of cat /proc/self/mountinfo  
  172.         if [[ "`mountpoint $TR$f`" == *" is a "* ]]; then
  173.           PATH2=`cat /proc/self/mountinfo | grep "$TR$f" | cut -d" " -f4`
  174.           PATH_DrvMnt=`cat /proc/self/mountinfo | grep /chroot/var | cut -d" " -f9`
  175.           PATH_DrvMnt=/mnt${PATH_DrvMnt#/dev}
  176.           PATH3=$PATH_DrvMnt$PATH2
  177.           SAVE_ROOT_NEW=${PATH3%$f}
  178.  
  179.           umount "$TR$f"
  180.           mkdir "$TR$f"
  181.           SAVE_ROOT_OLD=$SAVE_ROOT
  182.  
  183.           SAVE_ROOT=$SAVE_ROOT_NEW
  184.           bind_and_link_dir $f
  185.           SAVE_ROOT=$SAVE_ROOT_OLD
  186.           bind_and_link_dir $f          
  187.         else  
  188.           bind_and_link_dir $f
  189.         fi
  190.       else
  191.         echo "LN# 84 binding $f"
  192.         mkdir -p "$TR$f"      
  193.         mount --bind "$SR$f" "$TR$f" #Was bind_dir "$SR/$a" $f
  194.         echo "$f" >> $OUT/bound_dirs
  195.       fi
  196.     elif [ -f "$SR$f" ]; then
  197.       echo "LN#236 linking $f"
  198.       echo "LN#127 links is `readlink $SR$f`"
  199.       RP=`realpath $SR$f`
  200.       ln -s "/mnt/host$RP" "$TR$f" #Was link_file "$SR/$a" $a
  201.         echo "$f" >> $OUT/linked_dirs    
  202.     fi
  203.   fi
  204. }
  205. #echo $(dirname $(readlink -f "$fname")) # get directory name https://stackoverflow.com/questions/6121091/get-file-directory-path-from-file-path
  206. ######################### MAIN ###########################
  207.  
  208. if [ -e `which removePrefix` ]; then
  209.     cat <<-'EOF' > /usr/sbin/removePrefix
  210.     #!/bin/bash
  211.     #bla=$IFS
  212.     #FS=$'\0'
  213.     while read -r -d '' a; do
  214.         echo "a=$a">>/mnt/sdc6/removePrefixLog
  215.         echo "${a#$1}"
  216.         echo "${a#$1}">>/mnt/sdc6/removePrefixLog  
  217.         done
  218.     EOF
  219.     chmod go+x /usr/sbin/removePrefix #http://fideloper.com/user-group-permissions-chmod-apache
  220. fi
  221. for aRoot in ${ROOTS[@]}; do
  222.   ROOT_REL_PATH=$aRoot
  223.   SAVE_ROOT="$aRoot"
  224.  
  225.   for dir in ${dirs_to_link_and_bind[@]}; do
  226.     #mkdir -p dir
  227.     bind_and_link $dir
  228.   done
  229. done
  230. #indexgen.sh #http://murga-linux.com/puppy/viewtopic.php?p=990215#990215
  231. #fixmenus
  232. #jwm -reload
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement