Advertisement
s243a

map_ddog

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