Creating a chroot environment – the script.

Table of contents for chroot

  1. Creating a chroot environment in Fedora with bash and other utils.
  2. Creating a chroot environment – the script.

Here is the script, very simple and effective 🙂

#!/bin/bash -
#===============================================================================
#
#          FILE:  mkchroot.sh
#
#         USAGE:  ./mkchroot.sh
#
#   DESCRIPTION:  Make a  chroot environ and cd to it
#
#       OPTIONS:  ---
#  REQUIREMENTS:  ---
#          BUGS:  ---
#         NOTES:  ---
#        AUTHOR: Amit Agarwal (aka), 
#       CREATED: 09/03/2011 02:53:37 PM IST
# Last modified: Sat Sep 03, 2011  03:11PM
#      REVISION:  ---
#===============================================================================

 binaries=( bash2 ls cat vi vim sudo)
#===  FUNCTION  ================================================================
#          NAME:  copy_binary
#   DESCRIPTION:  copy binary to chroot
#    PARAMETERS:
#       RETURNS:
#===============================================================================

copy_binary ()
{
   cmd=`which $1`
   echo $cmd
   cp $cmd bin/
   ldd $cmd
   while read line
   do
      while read ld
      do
         if [[ -f $ld ]]
         then
            echo copy $ld
            cp $ld lib/
            if [[ -L $ld ]]
            then
               ld1=$( ls -l $ld |sed 's/.*> //')
               echo "  copy $ld1"
               cp  /lib/$ld1 lib/
            fi
         fi
      done < <(echo $line|sed 's/.*> //'|sed 's/ .*//')
   done < <(ldd $cmd)
}	# ----------  end of function copy_binary  ----------

#===  FUNCTION  ================================================================
#          NAME:  init
#   DESCRIPTION:  Do the required initialization
#    PARAMETERS:
#       RETURNS:
#===============================================================================
init()
{
   mkdir -p {root,home,dev,etc,lib,usr,bin}
   mkdir -p usr/bin
   mkdir -p libexec/openssh



   mknod -m 666 dev/null c 1 3

   cd etc
   cp /etc/ld.so.cache .
   cp -avr /etc/ld.so.cache.d/ .
   cp -avr /etc/ld.so.conf.d/ .
   cp /etc/ld.so.conf .
   cp /etc/nsswitch.conf .
   cp /etc/passwd .
   cp /etc/group .
   cp /etc/hosts .
   cp /etc/resolv.conf .
   cd -
}
cd $1
if [[ -f .status ]]
then
   cat .status
   echo "Not running now"
else
   init
   for i in ${binaries[*]}
   do
       copy_binary $i
       cp -avr /etc/${i}* etc/
   done
   ln bin/bash2 bin/bash
   echo "complete" > .status
fi
cp -avr ~/bin/automation root/automation
sudo chroot .

.

Enhanced by Zemanta

Search & replace with find & ed

\"Computer
Image via Wikipedia

function sr() {

declare pattern replacement name usage
declare -i pvar=0 rvar=0 tvar=0

usage=\'usage: sr [-t ] [-n name] [-p pattern] [-r replacement] [-- ] [dir1 dir2 ...]\'

# cf. http://bsdpants.blogspot.com/2007/02/option-ize-your-shell-scripts.html

while [[ \"${1:0:1}\" == \'-\' ]] ; do

[[ \"${1}\" == \'--\' ]] && { shift; break; } # -- marks end of options

[[ ${#1} -ne 2 ]] && { echo \"${usage}\"; return 1; }

case \"${1:1:1}\" in
\'n\') name=\"${2}\"
shift 2 ;;
\'p\') pattern=\"${2}\"
pvar=1
shift 2 ;;
\'r\') replacement=\"${2}\"
rvar=1
shift 2 ;;
\'t\') tvar=1
shift ;;
*) echo \"${usage}\"
return 1 ;;
esac

done

if [[ $pvar -eq 0 ]] || [[ $rvar -eq 0 ]]; then
echo \'Both options -p and -r must be specified!\'
echo \"${usage}\"
return 1
fi

while read -d $\'\' fpath; do

if [[ -z \"$(/usr/bin/egrep -I -m1 \'^.\' \"${fpath}\")\" ]]; then # skip binary files
echo \"Binary file: ${fpath}\"
continue
fi

if [[ $tvar -eq 0 ]]; then # perform an actual search & replace

cat <<-EOF | /bin/ed -s \"${fpath}\"
H
g/${pattern}/s/${pattern}/${replacement}/g
wq
EOF

else # only simulate a search & replace and print the result to stdout

printf \"nn33[1m%s33[mnnn\" \"${fpath}\" # print the current file path

# delete all lines that do not contain a pattern match: v/...
# add a final trailing new line in case all lines got deleted: $a...
# do the matching: g/${pattern}/...
# insert \'#: \' at the beginning of each line: g/./s/(...

cat <<-EOF | /bin/ed -s \"${fpath}\"
H
v/${pattern}/d
$a

.
g/${pattern}/s/${pattern}/$(printf \"33[32m${replacement}33[m\")/g
g/./s/(.*)/#: 1/g
,p
EOF

fi

done < <(/usr/bin/find -x \"${@}\" -type f -name \"*${name}\" -not -empty -print0)

}

man ed | less -p \'REGULAR EXPRESSIONS\'

# test mode
sr -t -p hello -r world -n \".txt\" /path/to/dir | less -r

# actual search & replace without previous backup
sr -p hello -r world -n \".txt\" /path/to/dir

# backslash-escape the / character
sr -t -p \'/usr/local\' -r \'/opt/local\' /path/to/file

URL: http://snippets.dzone.com/posts/show/10695

\"Reblog