Welcome, Guest. Please login or register.
Did you miss your activation email?

Author Topic: [DE] mychroot Skript  (Read 5305 times)

Offline ralul

  • User
  • Posts: 1.814
[DE] mychroot Skript
« on: 2010/09/21, 00:18:58 »
Ich hab mir ein neues chroot Script gemacht, dass kann mit
Label-Namen, Device Name (/dev/sdX) aufgerufen werden oder in ein Verzeichnis gehen:
Code: [Select]
#!/bin/bash
# Ralph Ulrich - GPL2 - mychroot-v1.1
Label=$1
me=$(basename $0)
[[ 4 > ${BASH_VERSION%%.*} ]] && echo "$me : need bash-4.x Version" && exit

declare -a Q=( 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 )
declare -a R=( $1 /etc/resolv.conf /dev /dev/pts /dev/shm /proc /sys )

myEXIT() {
  echo "$me : $1"
  [[ --help = $1 ]] && echo "$me  /dev/sdX | Label | Dir"
  exit
  # here is called umounty() by a trap signal - see below
}

mounty() {
    for ((i=0 ; i < ${#R[@]} ; i++)) ; do
        t=${R[$i]}
        if [[ $i = 0 ]] ; then
           [[ ${Q[0]} = 0 ]] && continue
           [ ! -e $P ]       && mkdir -p $P
           mount $Target $P  || myEXIT "exit : could not mount : $Label"
        else
           [[ ! -e $t ]] && continue
           fgrep -q $P$t /etc/mtab && Q[$i]=0 && continue
           if [ -f $t ] ; then
              # -f special for /etc/resolv.conf
              [ ! -e $P$t ] && mkdir -p $(dirname $P$t) && touch $P$t
              mount -o bind $(readlink -f $t) $P$t || myEXIT "exit : could not bind : $P$t"
              # readlink -f : there is a Debian resolvconf package linking symlinks....
           else
              [ ! -e $P$t ] && mkdir -p $P$t
              mount -o bind $t $P$t || myEXIT "exit : could not bind : $P$t"
           fi
        fi
        Q[$i]=1
    done
    [[ -d $P/etc ]] && grep -e "$P" /etc/mtab |sed -e "s@$P@/@" -e "s@//@/@" >$P/etc/mtab
    # better df in chroot
}

umounty() {
    for ((i=${#R[@]}-1 ; i>=0 ; i--)) ; do
        [[ $i = 0 ]] && u=$P || u=$P${R[$i]}
        [[ ${Q[$i]} = 0 ]] && echo "$me : keep mount $u" && continue
        umount $u
    done
}

[[ -z $1 ]]              &&  myEXIT "--help"
[[ $1 = -h ]]            &&  myEXIT "--help"
[[ $1 = --help ]]        &&  myEXIT "--help"
[[ root != $USER ]]      &&  myEXIT "exit : You need to be root!"
[[ 1 = $MeChangedRoot ]] &&  myEXIT "exit : NOT chrooting when in a chroot"

if [[ -L /dev/disk/by-label/$Label ]] ; then
   Target=/dev/$(basename $(readlink /dev/disk/by-label/$Label))
else
   Target=$Label
   # Might be a directory or /dev/sdX
fi

if [[ -b $Target ]] ; then
   ici="/dev/$(basename $(readlink -f $(mount |grep '/dev/' |grep ' / ' |head -n 1 |sed -e's@ .*@@')))"
   [[ $ici = $Target ]] && myEXIT "exit : same filesystem!"
   if mount |grep -q $Target ; then
      P=$(mount |grep $Target |sed -e 's@ type .*@@' -e 's@.* @@')
      Q[0]=0
   else
      P=/media/$(basename $Label)
      Q[0]=1
   fi
elif [[ -d $Label ]] ; then
     P=$Label
     Q[0]=0
     echo "$me : chroot into directory $Label"
else
     echo   "$me : exit : cannot chroot to : $Label"
     myEXIT "--help"
fi

trap umounty SIGINT SIGKILL EXIT
mounty
echo    "chroot $P   --------------"
LC_ALL=C MeChangedRoot=1 chroot $P /bin/bash
myEXIT  "END  ---------------------"
experiencing siduction runs better than my gentoo makes me know I know nothing

Offline RoEn

  • User
  • Posts: 219
Config- und Log-Files von und für User
« Reply #1 on: 2011/05/01, 13:10:13 »
Config- und Log-Files von und für User - stell es doch unter diesem Motto in das Wiki =>  http://wiki.aptosid.de/index.php?title=Hauptseite
naja...

RoEn