Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- #IFS=$'\0' #https://stackoverflow.com/questions/8677546/reading-null-delimited-strings-through-a-bash-loop
- #IFS='' #https://stackoverflow.com/questions/6563979/how-to-use-null-0-as-the-delimiter-in-gnu-sort
- #IFS=' '
- #IFS=''
- OUT="/tmp/map_save/"
- mkdir -p "$OUT"
- decode_id() {
- if [ -f "$OUT"'BLKIDOUT' ]; then
- DECODE_ID_RTN=`cat "$OUT"'BLKIDOUT'`
- else
- BLKIDOUT="$(blkid)"
- DECODE_ID_RTN="$(echo "$BLKIDOUT" | grep -m1 -E " LABEL=.${1}| UUID=.${1}" | cut -f1 -d: | cut -f3 -d/)" #is LABEL or UUID
- echo "$DECODE_ID_RTN" > "$OUT"'BLKIDOUT'
- fi
- echo "$DECODE_ID_RTN"
- }
- ###################### Initial Parmaters ############
- #ROOT_UUI="fab3b011-746f-448b-85a3-5f7fb7f8e7f6" #sdd4 (Toshiba)
- ROOT_UUI="ebeda76b-c7e8-d201-a020-a46bc7e8d201" #sdd6
- #ROOT_REL_PATH #Variable not yet defined
- decode_id "$ROOT_UUI"
- SAVE_ROOT_DRV="$DECODE_ID_RTN"
- ROOT_REL_PATH="/tahrsave"
- SAVE_ROOT="/mnt/$SAVE_ROOT_DRV$ROOT_REL_PATH"
- #taken from line 222 if initrd/init tahrpup and trimmed extensively
- ###################### Functions ############
- bind_and_link_array(){
- local -n one=${1} # https://stackoverflow.com/questions/16461656/how-to-pass-array-as-an-argument-to-a-function-in-bash
- if [ $# -lt 2 ]; then
- SR="$SAVE_ROOT"
- else
- SR=$2
- fi
- echo "Ln 45 one=${one[@]}"
- #example from: https://stackoverflow.com/questions/34555278/bash-read-a-looping-on-null-delimited-string-variable
- #while IFS= read -r -d '' myvar; do echo $myvar; done < <(find . -type f -print0)
- #while IFS= read -r -d '' a; do
- # echo "LN#43 bind_and_link $a"
- # bind_and_link "$a" "$SR"
- #done < <( "${one[@]}" )
- for a in $one; do
- echo "LN#43 bind_and_link $a"
- bind_and_link "$a" "$SR"
- done
- }
- bind_and_link_dir(){
- #local -n one=$1
- if [ $# -lt 2 ]; then
- SR="$SAVE_ROOT"
- else
- SR=$2
- fi
- one="$1"
- echo "LN#65 one=$one"
- #echo "LN#63 one=${one[@]}"
- #declair -A DIRS #https://stackoverflow.com/questions/1063347/passing-arrays-as-parameters-in-bash
- [ ! `mountpoint -q "$one"` ] || return _
- #example from: https://stackoverflow.com/questions/34555278/bash-read-a-looping-on-null-delimited-string-variable
- #while IFS= read -r -d '' myvar; do echo $myvar; done < <(find . -type f -print0)
- #DIRS=`find "$SR$one" -mindepth 1 -maxdepth 1 -print0 -name '*' `
- #decliar -A DIRS2
- #DIRS2=()
- #DIRS=`while IFS= read -r -d '' myvar; do echo "${myvar#$SR}"; done < <( "${DIRS[@]}" )`
- #find $SR$one -mindepth 1 -maxdepth 1 -print0 -name '*' |
- DIRS2=( $( find $SR$one -mindepth 1 -maxdepth 1 -print0 -name '*' | removePrefix "$SR" ) )#
- #IFS="$'\0'"
- #for a in $DIRS; do
- # echo "a=$a"
- # DIRS2+=( $a )
- #
- echo "LN#82 DIRS2=${DIRS2[@]}"
- bind_and_link_array DIRS2 $2
- }
- bind_and_link(){
- f="$1"
- if [ $# -gt 1 ]; then
- SR="$2"
- else
- SR="$SAVE_ROOT"
- fi
- #https://stackoverflow.com/questions/4561153/check-for-symbolic-link
- #https://unix.stackexchange.com/questions/167610/determining-if-a-file-is-a-hard-link-or-symbolic-link
- #http://pubs.opengroup.org/onlinepubs/9699919799/utilities/test.html
- IsBind="True"
- mountpoint -q "$f" || IsBind="False"
- echo "f=$f IsBind=$IsBind"
- if [ "$IsBind" = "False" ]; then
- echo "Ln 74 Not a mount point"
- if [ -L "$SR$f" ]; then #$f should start w/ slash TODO add slash if missing
- echo "LN#76 Copying $f"
- cp -a -u "$SR$f" "$f"
- echo "$f" >> /tmp/coppied_links
- elif [ -d "$SR$f" ]; then
- if [ -d "$f" ]; then
- echo "LN#85 bind_and_link_dir $f"
- mountpoint -q "$f" || bind_and_link_dir $f
- else
- echo "LN# 84 binding $f"
- mkdir -p "$f"
- mount --bind "$SR$f" $f #Was bind_dir "$SR/$a" $f
- echo "$f" >> /tmp/bound_dirs
- fi
- elif [ -f "$SR$f" ]; then
- echo "LN#90 linking $f"
- echo "LN#91 links is `readlink $f`"
- ln -s "$SR$a" $a #Was link_file "$SR/$a" $a
- echo "$f" >> /tmp/linked_dirs
- fi
- fi
- }
- #echo $(dirname $(readlink -f "$fname")) # get directory name https://stackoverflow.com/questions/6121091/get-file-directory-path-from-file-path
- ######################### MAIN ###########################
- declare -a dirs_to_link_and_bind=\
- ( "/etc" "/DEBIAN" "/bin" "/etc" "/lib" "/lib64" \
- "/opt" "/root" "/sbin" "/var" "/usr" "/var" \
- )
- for dir in ${dirs_to_link_and_bind[@]}; do
- mkdir -p dir
- bind_and_link_dir $dir
- done
- ########################## TRASH #########################
- #makeDirs(){
- # declare -a markDirs_rtn=()
- # for d in $1; do
- # markDirs_rtn+=( ${d[@]} )
- # done
- #}
- ##Todo add binded directory to a list of binded directories.
- #bind_dir(){
- # OPT_PATH=optimize_path $1
- # mount --bind $OPT_PATH $2 # $1=source or $2=link-name
- #}
- # ToDo adjust path based on binded directories also use a single absolute link to the particular drive
- #link_file(){
- # OPT_PATH=optimize_path $1
- # ln -s $1 $2 # $1=source or $2=link-name
- #}
- # a in SAVE_DIRS_OPT; do
- # mount --bind a # https://unix.stackexchange.com/questions/198590/what-is-a-bind-mount
- # #note that --rbind is requited for /sys unmounting issues: https://unix.stackexchange.com/questions/120827/recursive-umount-after-rbind-mount
- #done
- #SAVE_DIRS_OPT=(`find /opt -maxdepth 1 -mindepth 1 -name '*'`)
- #for i in $(seq 1 ${DIRS_OPT[#]); do #http://murga-linux.com/puppy/viewtopic.php?p=980163#980163
- # #Could loop through keys instead. See: https://unix.stackexchange.com/questions/74197/how-to-unset-range-of-array-in-bash
- # a=${DIRS_OPT[i]}
- # for j in $(seq 1 ${SAVE_DIRS_OPT[#]); do
- # b=${SAVE_DIRS_OPT[j]}
- # if [ a -eq b] ; then
- # uset "SAVE_DIRS_OPT[$j]"
- # fi
- # done
- #done
- #for d in "${dirs[@]}"; do
- # #delta=$(s243a_size "$d")
- # delta=$(du --block-size=$BLKSIZE --total $d | grep total | cut -f1 )
- # echo "delta=$delta"
- # (( RDSIZE = RDSIZE + delta ))
- # echo "RDSIZE=$RDSIZE"
- #done
- #declare -a bind_dirs=\
- #('opt/*'
- #)
- #declare -a bind_dirs=\
- #('opt/*
- #)
- #
- #' 'dev' 'etc' 'lib' 'locale' 'mnt' 'proc' 'sbin' 'sys' 'tmp' 'usr' 'var' 'pup_a' 'pup_f' 'pup_ro3' 'pup_ro4'
- # 'pup_ro5' 'pup_ro6' 'pup_ro7' 'pup_ro8' 'pup_ro9' 'pup_y' 'pup_z'
- #
- # 'pup_new' # The Layer being added in the /initrd/init script
- # #See Lines #14 to #15 for def of puppy layers
- # #pmedia: ram atahd usbhd atahd usbflash Multisession
- # #savetype pfx=ram Full Full Frugal Frugal
- # #PUPMODE:5 6 7 12 13 77
- # 'pup_rw' # tmpfs PDEV1 tmpfs pup_save.3fs tmpfs tmpfs
- # 'pup_ro1' # ---- ----- PDEV1 ------------ pup_save.3fs folders
- # 'pup_ro2') # pup_xxx.sfs pup_xxx.sfs pup_xxx.sfs pup_xxx.sfs pup_xxx.sfs pup_xxx.sfs
- #proc dev sys etc bin sbin var usr lib lib64 tmp
- #SAVE_opt="SAVE_ROOT/opt"
- #DIRS_opt=(`find "$SAVE_ROOT/opt" -maxdepth 1 -mindepth 1 -name '*'`)
- #
- #SAVE_etc="SAVE_ROOT/etc"
- #DIRS_etc=(`find "$SAVE_ROOT/etc" -maxdepth 1 -mindepth 1 -name '*'`)
- #
- #SAVE_bin="SAVE_ROOT/bin"
- #DIRS_bin=(`find "$SAVE_ROOT/bin" -maxdepth 1 -mindepth 1 -name '*'`)
- #
- #SAVE_sbin="SAVE_ROOT/sbin"
- #DIRS_sbin=(`find "$SAVE_ROOT/sbin" -maxdepth 1 -mindepth 1 -name '*'`)
- #
- #SAVE_var="SAVE_ROOT/var"
- #DIRS_var=(`find "$SAVE_ROOT/var" -maxdepth 1 -mindepth 1 -name '*'`)
- #
- #SAVE_usr="SAVE_ROOT/usr"
- #DIRS_usr=(`find "$SAVE_ROOT/usr" -maxdepth 1 -mindepth 1 -name '*'`)
- #
- #SAVE_lib="SAVE_ROOT/lib"
- #DIRS_lib=(`find "$SAVE_ROOT/lib" -maxdepth 1 -mindepth 1 -name '*'`)
- #ETC_root="SAVE_ROOT/root"
- #DIRS_root=(`find "$SAVE_ROOT/lib" -maxdepth 1 -mindepth 1 -name '*'`)
- #DIRS+=( "${DIRS_opt[@]}" "${DIRS_etc[@]}" "${DIRS_etc[@]}"1 i
Add Comment
Please, Sign In to add comment