s243a

map_save_back

Apr 29th, 2018
384
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/bash
  2. #IFS=$'\0' #https://stackoverflow.com/questions/8677546/reading-null-delimited-strings-through-a-bash-loop
  3. #IFS='' #https://stackoverflow.com/questions/6563979/how-to-use-null-0-as-the-delimiter-in-gnu-sort
  4. #IFS='  '
  5. #IFS=''
  6. OUT="/tmp/map_save/"
  7.  
  8. mkdir -p "$OUT"
  9. decode_id() {
  10.   if [ -f "$OUT"'BLKIDOUT' ]; then
  11.      DECODE_ID_RTN=`cat "$OUT"'BLKIDOUT'`
  12.   else
  13.     BLKIDOUT="$(blkid)"
  14.     DECODE_ID_RTN="$(echo "$BLKIDOUT" | grep -m1 -E " LABEL=.${1}| UUID=.${1}" | cut -f1 -d: | cut -f3 -d/)" #is LABEL or UUID
  15.     echo "$DECODE_ID_RTN" > "$OUT"'BLKIDOUT'
  16.   fi
  17.   echo "$DECODE_ID_RTN"
  18. }
  19.  
  20. ###################### Initial Parmaters ############
  21.  
  22. #ROOT_UUI="fab3b011-746f-448b-85a3-5f7fb7f8e7f6" #sdd4 (Toshiba)
  23. ROOT_UUI="ebeda76b-c7e8-d201-a020-a46bc7e8d201" #sdd6
  24.  
  25.  
  26. #ROOT_REL_PATH  #Variable not yet defined
  27.  
  28. decode_id "$ROOT_UUI"
  29. SAVE_ROOT_DRV="$DECODE_ID_RTN"
  30. ROOT_REL_PATH="/tahrsave"
  31. SAVE_ROOT="/mnt/$SAVE_ROOT_DRV$ROOT_REL_PATH"
  32.  
  33. #taken from line 222 if initrd/init tahrpup and trimmed extensively
  34.  
  35. ###################### Functions ############
  36.  
  37. bind_and_link_array(){
  38.   local -n one=${1} # https://stackoverflow.com/questions/16461656/how-to-pass-array-as-an-argument-to-a-function-in-bash
  39.   if [ $# -lt 2 ]; then
  40.     SR="$SAVE_ROOT"
  41.   else
  42.     SR=$2
  43.   fi
  44.  
  45.   echo "Ln 45 one=${one[@]}"   
  46.   #example from: https://stackoverflow.com/questions/34555278/bash-read-a-looping-on-null-delimited-string-variable
  47.   #while IFS= read -r -d '' myvar; do echo $myvar; done < <(find . -type f -print0)
  48.   #while IFS= read -r -d '' a; do
  49.   #    echo "LN#43 bind_and_link $a"
  50.   #    bind_and_link "$a" "$SR"
  51.   #done < <( "${one[@]}" )
  52.   for a in $one; do
  53.      echo "LN#43 bind_and_link $a"
  54.      bind_and_link "$a" "$SR"
  55.   done
  56. }
  57.  
  58.  
  59. bind_and_link_dir(){
  60.   #local -n one=$1
  61.   if [ $# -lt 2 ]; then
  62.     SR="$SAVE_ROOT"
  63.   else
  64.     SR=$2
  65.   fi   
  66.   one="$1"
  67.   echo "LN#65 one=$one"  
  68.   #echo "LN#63 one=${one[@]}"
  69.   #declair -A DIRS #https://stackoverflow.com/questions/1063347/passing-arrays-as-parameters-in-bash
  70.   [ ! `mountpoint -q "$one"` ] || return _
  71.   #example from: https://stackoverflow.com/questions/34555278/bash-read-a-looping-on-null-delimited-string-variable
  72.   #while IFS= read -r -d '' myvar; do echo $myvar; done < <(find . -type f -print0)
  73.   #DIRS=`find "$SR$one"  -mindepth 1 -maxdepth 1 -print0 -name '*' `
  74.   #decliar -A DIRS2
  75.   #DIRS2=()
  76.   #DIRS=`while IFS= read -r -d '' myvar; do echo "${myvar#$SR}"; done < <( "${DIRS[@]}" )`
  77.   #find $SR$one -mindepth 1 -maxdepth 1 -print0 -name '*' |
  78.   DIRS2=( $( find $SR$one -mindepth 1 -maxdepth 1 -print0 -name '*'  | removePrefix "$SR" ) )#
  79.   #IFS="$'\0'"
  80.   #for a in $DIRS; do
  81.   #  echo "a=$a"
  82.   #  DIRS2+=( $a )
  83.   #
  84.   echo "LN#82 DIRS2=${DIRS2[@]}"
  85.   bind_and_link_array DIRS2 $2
  86. }
  87. bind_and_link(){
  88.   f="$1"
  89.   if [ $# -gt 1 ]; then
  90.     SR="$2"
  91.   else
  92.     SR="$SAVE_ROOT"
  93.   fi
  94.   #https://stackoverflow.com/questions/4561153/check-for-symbolic-link
  95.   #https://unix.stackexchange.com/questions/167610/determining-if-a-file-is-a-hard-link-or-symbolic-link
  96.   #http://pubs.opengroup.org/onlinepubs/9699919799/utilities/test.html
  97.   IsBind="True"
  98.   mountpoint -q "$f" || IsBind="False"
  99.   echo "f=$f IsBind=$IsBind"
  100.   if [ "$IsBind" = "False" ]; then
  101.      echo "Ln 74 Not a mount point"
  102.     if [ -L "$SR$f" ]; then #$f should start w/ slash TODO add slash if missing
  103.       echo "LN#76 Copying $f"
  104.       cp -a -u "$SR$f" "$f"
  105.       echo "$f" >> /tmp/coppied_links
  106.     elif [ -d "$SR$f" ]; then    
  107.       if [ -d "$f" ]; then
  108.         echo "LN#85 bind_and_link_dir $f"
  109.         mountpoint -q "$f" || bind_and_link_dir $f
  110.       else
  111.         echo "LN# 84 binding $f"
  112.         mkdir -p "$f"      
  113.         mount --bind "$SR$f" $f #Was bind_dir "$SR/$a" $f
  114.         echo "$f" >> /tmp/bound_dirs
  115.       fi
  116.     elif [ -f "$SR$f" ]; then
  117.       echo "LN#90 linking $f"
  118.       echo "LN#91 links is `readlink $f`"
  119.       ln -s "$SR$a" $a #Was link_file "$SR/$a" $a
  120.         echo "$f" >> /tmp/linked_dirs    
  121.     fi
  122.   fi
  123. }
  124. #echo $(dirname $(readlink -f "$fname")) # get directory name https://stackoverflow.com/questions/6121091/get-file-directory-path-from-file-path
  125. ######################### MAIN ###########################
  126. declare -a  dirs_to_link_and_bind=\
  127. ( "/etc" "/DEBIAN" "/bin" "/etc" "/lib" "/lib64" \
  128.    "/opt" "/root" "/sbin" "/var" "/usr" "/var" \
  129. )
  130.  
  131. for dir in ${dirs_to_link_and_bind[@]}; do
  132.   mkdir -p dir
  133.   bind_and_link_dir $dir
  134. done
  135.  
  136. ########################## TRASH #########################
  137. #makeDirs(){
  138. #  declare -a markDirs_rtn=()
  139. #  for d in $1; do
  140. #     markDirs_rtn+=( ${d[@]} )
  141. #  done
  142. #}
  143. ##Todo add binded directory to a list of binded directories.
  144. #bind_dir(){
  145. #   OPT_PATH=optimize_path $1
  146. #   mount --bind $OPT_PATH $2 # $1=source or $2=link-name
  147. #}
  148. # ToDo adjust path based on binded directories also use a single absolute link to the particular drive
  149. #link_file(){
  150. #   OPT_PATH=optimize_path $1  
  151. #   ln -s $1 $2 # $1=source or $2=link-name
  152. #}
  153. # a in SAVE_DIRS_OPT; do
  154. #   mount --bind  a  # https://unix.stackexchange.com/questions/198590/what-is-a-bind-mount
  155. #                   #note that --rbind is requited for /sys unmounting issues: https://unix.stackexchange.com/questions/120827/recursive-umount-after-rbind-mount
  156. #done
  157. #SAVE_DIRS_OPT=(`find /opt -maxdepth 1 -mindepth 1 -name '*'`)
  158. #for i in $(seq 1 ${DIRS_OPT[#]); do #http://murga-linux.com/puppy/viewtopic.php?p=980163#980163
  159. #   #Could loop through keys instead. See: https://unix.stackexchange.com/questions/74197/how-to-unset-range-of-array-in-bash
  160. #   a=${DIRS_OPT[i]}
  161. #   for j in $(seq 1 ${SAVE_DIRS_OPT[#]); do
  162. #       b=${SAVE_DIRS_OPT[j]}
  163. #        if [ a -eq b] ; then
  164. #            uset "SAVE_DIRS_OPT[$j]"
  165. #        fi
  166. #    done
  167. #done
  168. #for d in "${dirs[@]}"; do
  169. #    #delta=$(s243a_size "$d")
  170. #    delta=$(du --block-size=$BLKSIZE --total $d | grep total | cut -f1 )
  171. #    echo "delta=$delta"
  172. #    (( RDSIZE = RDSIZE + delta ))
  173. #    echo "RDSIZE=$RDSIZE"
  174. #done
  175. #declare -a bind_dirs=\
  176. #('opt/*'
  177. #)
  178. #declare -a bind_dirs=\
  179. #('opt/*
  180. #)
  181. #
  182. #' 'dev' 'etc' 'lib' 'locale' 'mnt' 'proc' 'sbin' 'sys' 'tmp' 'usr' 'var' 'pup_a' 'pup_f' 'pup_ro3' 'pup_ro4'
  183. #  'pup_ro5' 'pup_ro6' 'pup_ro7' 'pup_ro8' 'pup_ro9' 'pup_y' 'pup_z'
  184. #
  185. # 'pup_new' # The Layer being added in the /initrd/init script
  186. #           #See Lines #14 to #15 for def of puppy layers
  187. #  #pmedia:         ram      atahd          usbhd         atahd       usbflash   Multisession
  188. #  #savetype      pfx=ram   Full            Full         Frugal       Frugal    
  189. #           #PUPMODE:5        6              7            12           13          77
  190. #  'pup_rw'    #   tmpfs     PDEV1          tmpfs    pup_save.3fs    tmpfs        tmpfs
  191. #  'pup_ro1'   #   ----      -----         PDEV1     ------------  pup_save.3fs  folders  
  192. #  'pup_ro2')  # pup_xxx.sfs pup_xxx.sfs pup_xxx.sfs pup_xxx.sfs   pup_xxx.sfs  pup_xxx.sfs
  193. #proc dev sys etc bin sbin var usr lib lib64 tmp
  194. #SAVE_opt="SAVE_ROOT/opt"
  195. #DIRS_opt=(`find "$SAVE_ROOT/opt" -maxdepth 1 -mindepth 1 -name '*'`)
  196. #
  197. #SAVE_etc="SAVE_ROOT/etc"
  198. #DIRS_etc=(`find "$SAVE_ROOT/etc" -maxdepth 1 -mindepth 1 -name '*'`)
  199. #
  200. #SAVE_bin="SAVE_ROOT/bin"
  201. #DIRS_bin=(`find "$SAVE_ROOT/bin" -maxdepth 1 -mindepth 1 -name '*'`)
  202. #
  203. #SAVE_sbin="SAVE_ROOT/sbin"
  204. #DIRS_sbin=(`find "$SAVE_ROOT/sbin" -maxdepth 1 -mindepth 1 -name '*'`)
  205. #
  206. #SAVE_var="SAVE_ROOT/var"
  207. #DIRS_var=(`find "$SAVE_ROOT/var" -maxdepth 1 -mindepth 1 -name '*'`)
  208. #
  209. #SAVE_usr="SAVE_ROOT/usr"
  210. #DIRS_usr=(`find "$SAVE_ROOT/usr" -maxdepth 1 -mindepth 1 -name '*'`)
  211. #
  212. #SAVE_lib="SAVE_ROOT/lib"
  213. #DIRS_lib=(`find "$SAVE_ROOT/lib" -maxdepth 1 -mindepth 1 -name '*'`)
  214.  
  215. #ETC_root="SAVE_ROOT/root"
  216. #DIRS_root=(`find "$SAVE_ROOT/lib" -maxdepth 1 -mindepth 1 -name '*'`)
  217. #DIRS+=( "${DIRS_opt[@]}" "${DIRS_etc[@]}" "${DIRS_etc[@]}"1 i
Add Comment
Please, Sign In to add comment