Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- ######################### Intializations ###########################
- curdir=`pwd` #Not sure if we need this
- OUT="/mnt/home/symlinks"
- TMP='/mnt/home/symlinks' #probably not necessary
- #We will sync /initrd/pup_rw so we don't need to merge it manually in ROOTS array.
- declare -a ROOTS=\
- ( "/initrd/pup_ro2" \
- "/" \
- )
- declare -a main_dirs_to_link_and_bind=\
- ( "/bin" "/etc" "/lib" \
- "/opt" "/sbin" "/usr" "/var" \
- )
- declare -a other_dirs_to_link_and_bind=\
- ( "/boot" "/home" \
- "/root" "/run" \
- )
- declare -a dirs_to_link_and_bind=\
- ( ${main_dirs_to_link_and_bind[@]} \
- ${other_dirs_to_link_and_bind[@]} \
- )
- TARGET_ROOT=$OUT
- #------------------------------------------
- #Modify and uncomment if you want the new root in ram.
- #TARGET_ROOT="/chroot"
- ##if [ ! -d $TARGET_ROOT ]; then
- ## #https://www.hecticgeek.com/2015/12/create-ram-disk-ubuntu-linux/
- ## mount -t tmpfs -o size=250M,nr_inodes=5k,mode=1777 tmpfs2 $TARGET_ROOT
- ##fi
- #------------------------------------------
- #ROOT_REL_PATH #Variable not yet defined
- decode_id "$ROOT_UUI" #Not sure if we use this
- ######################### MAKE DIRECTORIES ###########################
- #------------------------------------------
- declare -a HOST_RBIND_POINTS=\
- ( "/sys" \
- "/dev" \
- )
- for aDIR in ${HOST_RBIND_POINTS[@]}; do
- mkdir -p "$TARGET_ROOT$aDIR"
- mount --rbind "$aDIR" "$TARGET_ROOT$aDIR"
- done
- #------------------------------------------
- declare -a HOST_MOUNT_T_POINTS=\
- ( "/proc" \
- )
- for aDIR in ${HOST_MOUNT_T_POINTS[@]}; do
- mkdir -p "$TARGET_ROOT$aDIR"
- mount -t proc "$aDIR" "$TARGET_ROOT$aDIR"
- done
- #------------------------------------------
- declare -a HOST_COPY=\
- ( "/etc/resolv.conf" "/etc/hosts" \
- )
- for aFile in ${HOST_COPY[@]}; do
- mkdir -p "$TARGET_ROOT$aFile"
- cp -a -u "$aFile" "$TARGET_ROOT$aFile"
- done
- #------------------------------------------
- declare -a HOST_RBIND_MAP=\
- ( "/" "/mnt/host" \
- )
- while read SOURCE; read TARGET; do
- mkdir -p "$TARGET_ROOT$TARGET"
- cd "$TARGET_ROOT"; cd "$SOURCE"
- mount --rbind . "$TARGET_ROOT$TARGET"
- done <<EOF
- `for a in ${HOST_RBIND_MAP[@]}; do echo "$a"; done`
- EOF
- #------------------------------------------
- #mkdir $OUT
- #------------------------------------------
- declare -a new_dirs=\
- ( "/tmp" \
- )
- mkdir -p "$OUT"
- for aDir in ${new_dirs[@]}; do
- mkdir -p "$TARGET_ROOT$aDir"
- done
- ######################### FUNCTIONS ###########################
- ###################### 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
- 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
- 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
- one=${one%')#'}
- 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
- export SR="$SAVE_ROOT"
- else
- export 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
- [ ! `/bin/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"; f=/${f#/}
- if [ $# -gt 1 ]; then
- SR="$2"
- else
- SR="$SAVE_ROOT"
- fi
- if [ $# -lt 3 ]; then
- export TR="$TARGET_ROOT"
- else
- export TR=$3
- 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" "$TR$f"
- echo "$f" >> $OUT/coppied_links
- elif [ -d "$SR$f" ]; then
- if [ -d "$TR$f" ]; then
- echo "LN#85 bind_and_link_dir $f"
- #Could user findmnt instead of cat /proc/self/mountinfo
- if [[ "`mountpoint $TR$f`" == *" is a "* ]]; then
- PATH2=`cat /proc/self/mountinfo | grep "$TR$f" | cut -d" " -f4`
- PATH_DrvMnt=`cat /proc/self/mountinfo | grep /chroot/var | cut -d" " -f9`
- PATH_DrvMnt=/mnt${PATH_DrvMnt#/dev}
- PATH3=$PATH_DrvMnt$PATH2
- SAVE_ROOT_NEW=${PATH3%$f}
- umount "$TR$f"
- mkdir "$TR$f"
- SAVE_ROOT_OLD=$SAVE_ROOT
- SAVE_ROOT=$SAVE_ROOT_NEW
- bind_and_link_dir $f
- SAVE_ROOT=$SAVE_ROOT_OLD
- bind_and_link_dir $f
- else
- bind_and_link_dir $f
- fi
- else
- echo "LN# 84 binding $f"
- mkdir -p "$TR$f"
- mount --bind "$SR$f" "$TR$f" #Was bind_dir "$SR/$a" $f
- echo "$f" >> $OUT/bound_dirs
- fi
- elif [ -f "$SR$f" ]; then
- echo "LN#236 linking $f"
- echo "LN#127 links is `readlink $SR$f`"
- RP=`realpath $SR$f`
- ln -s "/mnt/host$RP" "$TR$f" #Was link_file "$SR/$a" $a
- echo "$f" >> $OUT/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 ###########################
- if [ -e `which removePrefix` ]; then
- cat <<-'EOF' > /usr/sbin/removePrefix
- #!/bin/bash
- #bla=$IFS
- #FS=$'\0'
- while read -r -d '' a; do
- echo "a=$a">>/mnt/sdc6/removePrefixLog
- echo "${a#$1}"
- echo "${a#$1}">>/mnt/sdc6/removePrefixLog
- done
- EOF
- chmod go+x /usr/sbin/removePrefix #http://fideloper.com/user-group-permissions-chmod-apache
- fi
- for aRoot in ${ROOTS[@]}; do
- ROOT_REL_PATH=$aRoot
- SAVE_ROOT="$aRoot"
- for dir in ${dirs_to_link_and_bind[@]}; do
- #mkdir -p dir
- bind_and_link $dir
- done
- done
- #indexgen.sh #http://murga-linux.com/puppy/viewtopic.php?p=990215#990215
- #fixmenus
- #jwm -reload
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement