分类: LINUX
2009-06-28 23:40:35
This document is written in the hope that it will be useful to those who want to embed Linux. Specially the one that are new to Linux (like me, I was a DOS/Win user). It will spare you the wrestling true all the documentation, HOWTO's and MAN pages I have gone true.
You can download Linux from the internet or you can take a full Linux distribution. The last one contains everything you need (utilities, sources, compiler, debugger, documentation ...) and is easy to install. (next to my other OS)
We should know some basics before we go any further
After the PC starts, the boot manager (found at the MBR of the boot device) launches the linux kernel. With a boot manager like lilo, syslinux, loadlin... you can pass parameters to the kernel. These parameters are neccessary to boot the kernel from another root device like a ramdisk. (or you can patch these settings into the kernel using rdev)
The kernel then checks your hardware and mounts the root device which contains the root file system. If /linuxrc is present at the root file system it is executed. Next init is started. Init is the parent of all the other processes that will run in your Linux OS, and as a good parent, it will watch these processes and start/stop/re-launch them if things changes. (This process creation is done by the fork=clone_me and execve=replace_me system calls.)
Init takes all information from /etc/inittab which in turn refers to scripts named /etc/rc... to do the system setup. Inittab also has an entry to start getty for every virtual console.
Getty will launch /bin/login and after a user has logged in, login starts the users default shell (found in /etc/passwd) in the users home directory. Then the shell will execute the users profile. If the user exits the shell init will re-run (respawn) getty.
If /etc/init is not present the shell /bin/sh is started (in the hope that some intelligent operator will be present to tell it what to do next).
More info can be found at Linux Documentation Project and the man pages of init and inittab.
Compiling the kernel is easy and is done with the following commands:
cd /usr/src/linux
make xconfig (kernel configuration)
make clean (cleanup old objects)
make dep (make all dependencies)
make zImage (compile the kernel, this can take a while)
With the kernel configuration utility 'make xconfig' you can select each feature
to compile into the kernel statically, modular or not at all.
You can remove everything you'll never need.
For the file systems I choose minix and dos. Because minix will be my root file system on
ramdisk, minix and ramdisk must be compiled in statically.
For some special devices like the DiskOnChip flash driver you first need to patch the kernel sources,
and then recompile the kernel.
After a successful compilation the new kernel image is found at arch/i386/boot/zImage.
The minimum root file system should contain the following:
/lib/libc.so5 /lib/ld-linux.so.1We could keep it simple and just run one application and named it /bin/sh, but mostly we need more so add the following:
/etc/ld.so.cach /bin/sh
/dev/console /dev/null
/dev/ram /dev/systty
/dev/tty1 /dev/tty2
/sbin/init /etc/rcYou can strip down commands by executing
/bin/mount /bin/cat
/bin/cp /bin/echo
/dev/fd0 /proc
/mnt /linuxrc
/bin/star /bin/zcat (or tar / gzip to install tgz packages)
objcopy --strip-all srccmd dstcmd
(for libraries use objcopy --strip-debug srclib dstlib
.)ldd
command
gives you a list of all the libraries used by a command.To build a ramdisk image we will use the loop device. (if necessary enable this by insmod loop.o
)
Copy the files above in a sub dir e.g. rootfs/
then execute the following
commands:
dd if=/dev/zero of=rootfs.img bs=1k count=400 (make an empty file of 400k)This will make a compressed ramdisk image
mkfs.minix -c rootfs.img (make a file system on it)
mount -o loop -t minix rootfs.img /mnt (mount this as loopback device)
cp -av rootfs/* /mnt (copy my rootfs to the loopback device)
umount /mnt (un mount the loopback device)
gzip -v9 -c rootfs.img >rootfs.gz (compress it into rootfs.gz)
rootfs.gz
.To add more utilities to your system, just copy everything you need ( a root tree)
into a sub dir e.g. pack_01
.
Then compress everything using cd pack_01; tar -zcpf ../pack_01.tgz *
Be sure to include all dependencies (libraries, configurations /etc/...) in the packages.
To install this at boot time copy all the packages to /pack, and inittab and rc to /etc of our boot device. If our boot device is a dos formatted floppy at /dev/fd0 then linuxrc (which is in rootfs.gz) can look like:
#--- /linuxrc ---Linuxrc copies everything from /etc on our boot device to /etc on the root device.
#!/bin/sh
mount -t dos /dev/fd0 /mnt
cp /mnt/etc/* /etc
#--- /etc/inittab ---If you run out of ramdisk you can always mount some more:
#syntax id : runlevel : action : path
id:2:initdefault:
si::sysinit:/etc/rc
01:12345:respawn:/sbin/getty 38400 tty1
02:2345:respawn:/sbin/getty 38400 tty2
#--- /etc/rc ---
#!/bin/sh
echo -n > /etc/mtab (write new mtab)
mount -o remount -t minix /dev/ram /
mount -o remount -t proc proc /proc
mount -o remount -t dos /dev/fd0 /mnt
PACKAGE=`ls /mnt/pack` (get a list of all packages)
for f in $PACKAGE; do
zcat < $f | star (decompress and install every package found)
done
...
mkdir /usrYou can add your own kernel boot parameters (e.g.
mkfs.minix -i 400 /dev/ram1
mount -t minix /dev/ram1 /usr
loadlin.exe zimage initrd=rootfs.gz root=/dev/ram mypar1=test1 mypar2=test2
) and get them into your script.
An easy way to do this:
cat /proc/cmdline >/etc/cmdline
echo -n "f=fix" >>/etc/cmdline
. /etc/cmdline
echo $mypar1
echo $mypar2
We are almost there now. Just copy the linux kernel, ramdisk image, etc and packages to your boot device and install a boot manager. For syslinux this boot dir contains:
/ldlinux.sysWhere syslinux.cfg should contain:
/syslinux.cfg
/zimage
/rootfs.gz
/etc/inittab
/etc/rc
/pack/pack_01.tgz
/pack/pack_02.tgz
timeout 0You can also put all this stuff in a sub dir of your hard drive and launch linux from the harddisk with:
default zimage
append=load_ramdisk=1 initrd=rootfs.gz root=/dev/ram
loadlin.exe zImage initrd=rootfs.gz root=/dev/ram
Thats all. Good Luck!
dd if=emblin.img of=/dev/fd0
to make a bootable
floppy.config.cmd
to edit the one and only configuration script rc.cmd
.
Set your network_interface eth0, ip_addr, network_mask, default gateway, DNS ...
Save your settings with Esc, Datei, Beenden, Y and restart Emblin.
http:ip_addr
) and you will get the home page.
Try out the , ftp, telnet, tftp.tar -xzvpf source.tgz /EmbLin
and do the reverse of the
mypack/build.pac
script.