Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
Howto setup a tiny root filesystem
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Documentation, Tips & Tricks
View previous topic :: View next topic  
Author Message
thebigslide
l33t
l33t


Joined: 23 Dec 2004
Posts: 790
Location: under a car or on top of a keyboard

PostPosted: Wed Feb 23, 2005 10:00 am    Post subject: Howto setup a tiny root filesystem Reply with quote

I've compiled some scripts that make it real easy to setup a miniscule root filesystem. You'll use busybox for this ideally.

libsof is useful for a few things. This gives you the full paths of all need dynamically linked libraries separated by CRLF for use in bash scripts or whatever.
Usage: libsof [-l] binary [binary binary ...]
-l = (L) nicely formatted output.

Code:
cat /bin/libsof
#!/bin/sh

rm ~/.libsof
touch ~/.libsof
start=1
if [ $1 == "-l" ]
then
        start=2
fi
for x in `/bin/seq $start $#`
do
        which ${!x} >> ~/.libsof
        ldd `which ${!x}`| grep lib | cut -d ">" -f2 | cut -d "(" -f1 | grep -v "ld-linux.so.2" >> ~/.libsof
        echo "" >> ~/.libsof
done

if [ $1 == "-l" ]
then
        cat ~/.libsof
else
        sort -u ~/.libsof | grep "/lib/" | cut -c 2-
fi


length returns the integer length of argument 1
Code:
cat /bin/length
#!/bin/bash
character=`echo $1 | cut -c 1`
length=0
while [ $character ]; do
        length=`echo "1+$length" | bc`
        character=`echo $1 | cut -c $length`
done
length=`echo "$length-1" | bc`
echo $length


count returns the integer number of times $1 occurs in $2
Code:
cat /bin/count
#!/bin/bash
count=0
length=`/bin/length $2`
for I in `seq $length`; do
        #echo -n `echo $2 | cut -c $I`
        #echo $1
        if [ `echo $2 | cut -c $I` == $1 ]; then
                count=`echo "$count + 1" | bc`
        fi
done
echo $count


pathof returns just the path of the file given as argument 1. Or returns the complete string before the last '/' in argument 1.
Code:
cat /bin/pathof
#!/bin/bash
A=`count / $1`
unset mypath
for I in `seq $A`; do
        mypath="$mypath/`echo -n $1 | cut -d '/' -f$I`"
#       echo -n $1 | cut -d '/' -f$I
done
echo $mypath | cut -c 2-


wordof returns the $1th word in the rest of the arguments given
Code:
cat /bin/wordof
#!/bin/sh
MATCH=`echo "$1+1" | /usr/bin/bc`
echo "${!MATCH}"


copylibs automates the job of copying all the dynamic libraries needed to run a binary.
Code:
cat /bin/copylibs
#!/bin/sh
CURRDIR=`pwd`
for I in `libsof $*`; do
       cp -pf $I ${CURRDIR}${I}
#        echo $I
        reallib="`pathof $I`/`ls -la $I | xargs wordof 11`"
        reallength=`length $reallib`
        while [ `echo $reallib | cut -c $reallength` != "/" ]
        do
               cp -pf ${reallib} ${CURRDIR}${reallib}
#                echo $reallib
                reallib="`pathof $reallib`/`ls -la $reallib | xargs wordof 11`"
                reallength=`length $reallib`
        done
done


copylibs can be used to copy all the dynamic libraries and links that an executable is dependent on.
Very handy for setting up chroots, whatever. Currently, libGL and libsamba screw it up because they're linked a little differently. Just copy those 2 files if you're involving an executabe involving those (you'll see the complaint on the command line).

There is a helper script 'copybins' that copies the binaries AND the libraries.
Code:
#!/bin/bash
CURRDIR=`pwd`
for I in `seq $#`; do
        BINARY=`which ${!I}`
        cp -pfH ${BINARY} ${CURRDIR}${BINARY}
done
copylibs $*


To setup a tiny root filesystem, try
Code:
emerge busybox;mkdir newroot;cd newroot;mkdir dev bin lib sbin usr usr/lib usr/bin usr/sbin proc sys root oldroot var tmp;copylibs sh bash busybox all the programs you desire
then you mknod (see /usr/src/linux/documentation/devices.txt) and copy the bits and pieces of /etc that your shizzle needs. The /oldroot directory isn't needed, but it's where I put my initrd when it chroots when booting this type of filesystem. You also need to copy over grub and a kernel if necessary before booting it. You might want to chroot into it first and make sure all the apps run as expected.

The applications of this are extensive. Movie (divx or mpeg) DVDs that are bootable and start playing with mplayer, autoinstallers, automatically extracting backup dvds, restore dvds, etc. The root filesystem with just bash and busybox is under 3MB. K drive and fluxbox don't add much.


Last edited by thebigslide on Fri Feb 25, 2005 6:25 am; edited 2 times in total
Back to top
View user's profile Send private message
Dhaki
Guru
Guru


Joined: 16 Jun 2004
Posts: 325
Location: Ticino - CH

PostPosted: Thu Feb 24, 2005 7:14 am    Post subject: Re: quickie - find the libs needed by any dynamic executable Reply with quote

thebigslide wrote:
Useful for a few things. This gives you the full paths separated by CRLF for use if bash scripts or whatever.
Usage: libsof [-l] binary [binary binary ...]
-l = (L) nicely formatted output.

Code:
#!/bin/sh

rm ~/.libsof
touch ~/.libsof
start=1
if [ $1 = "-l" ]
then
        start=2
fi
for x in `/bin/seq $start $#`
do
        which ${!x} >> ~/.libsof
        ldd `which ${!x}`| grep lib | cut -d ">" -f2 | cut -d "(" -f1 | grep -v "ld-linux.so.2" >> ~/.libsof
        echo "" >> ~/.libsof
done

if [ $1 = "-l" ]
then
        cat ~/.libsof
else
        sort -u ~/.libsof | grep "/lib/" | cut -c 2-
fi

Nice :wink:, but it shouldn't be like this?
Code:
[...]if [ $1 == "-l" ]
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Documentation, Tips & Tricks All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum