LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 1205|回复: 0

ZZ -Linux EMBEDDING mini HOWTO

[复制链接]
发表于 2005-1-11 20:30:43 | 显示全部楼层 |阅读模式
http://www.dobit.com/emblin/embhowto.htm   
--------------------------------------------------------------------------------

Linux EMBEDDING mini HOWTO
Luc Hermans, lhermans@dobit.com
v0.2, 15 March 2000


--------------------------------------------------------------------------------
This document shows how to strip down your Linux OS, and provides one method of putting it in an embedded i386 PC and hopefully get it running.
--------------------------------------------------------------------------------



Content
1. Introduction
2. Getting Linux
3. Linux boot process
4. Compile the Linux kernel
5. Build an initial ramdisk image
6. Build and install additional packages
7. Putting everything together
8. Download
9. Useful links
1. Introduction
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.

2. Getting Linux
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)

3. Linux boot process
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.

4. Compile the Linux kernel
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.

5. Build an initial ramdisk image
The minimum root file system should contain the following:

    /lib/libc.so5      /lib/ld-linux.so.1
    /etc/ld.so.cach    /bin/sh
    /dev/console       /dev/null
    /dev/ram           /dev/systty
    /dev/tty1          /dev/tty2       

We could keep it simple and just run one application and named it /bin/sh, but mostly we need more so add the following:
    /sbin/init   /etc/rc
    /bin/mount   /bin/cat
    /bin/cp      /bin/echo
    /dev/fd0         /proc
    /mnt         /linuxrc                
    /bin/star    /bin/zcat     (or tar / gzip to install tgz packages)

You can strip down commands by executing objcopy --strip-all srccmd dstcmd (for libraries use objcopy --strip-debug srclib dstlib.)
When you add commands you must also include the libraries used. The ldd command gives you a list of all the libraries used by a command.
There are small versions of commands available, frequently used is ash, a tiny shell, and busybox. Busybox contains different commands in just one binary and spare you a lot of ram / disk space.

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)
    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)

This will make a compressed ramdisk image rootfs.gz.
Maybe its best to keep the rootfs as small as possible. You can always install more features as described below. This way its easier to upgrade, change or download new features.

6. Build and install additional packages
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 ---
    #!/bin/sh
    mount -t dos /dev/fd0 /mnt
    cp /mnt/etc/* /etc

Linuxrc copies everything from /etc on our boot device to /etc on the root device.
A simple /etc/inittab and /etc/rc:
    #--- /etc/inittab ---
    #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
    ...

If you run out of ramdisk you can always mount some more:
    mkdir /usr
    mkfs.minix -i 400 /dev/ram1
    mount -t minix /dev/ram1 /usr

You can add your own kernel boot parameters (e.g. 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


7. Putting everything together
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.sys
    /syslinux.cfg
    /zimage
    /rootfs.gz
    /etc/inittab
    /etc/rc
    /pack/pack_01.tgz
    /pack/pack_02.tgz

Where syslinux.cfg should contain:
    timeout 0
    default zimage
    append=load_ramdisk=1 initrd=rootfs.gz root=/dev/ram

You can also put all this stuff in a sub dir of your hard drive and launch linux from the harddisk with:
loadlin.exe zImage initrd=rootfs.gz root=/dev/ram
Thats all. Good Luck!

8. Download and Quick start
After downloading the floppy disk image emblin.img use the command
dd if=emblin.img of=/dev/fd0 to make a bootable floppy.
From DOS you can run the dos app rawrite3 or WinImage to build the floppy.
Reboot from this floppy and type 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.
From another network stations browse to EmbLin with your favorite navigator (enter http:ip_addr) and you will get the EmbLin home page. Try out the sysstat CGI-script, ftp, telnet, tftp.
PS: If you run the windows telnet use Ctrl-J (LF) instead of Enter (CR)
You can also use EmbLin as a client; lynx, ftp, telnet and tftp
For more help you can always try help.
This floppy also contains the sources of a tiny init, extracted from an old version of busybox I modified. If you build your own Linux system use the new busybox which has much more features now (but did not compile on my system).
Install with tar -xzvpf source.tgz /EmbLin and do the reverse of the mypack/build.pac script.
9. Useful links
EmbLin Home
Embedded Linux Howto
Linux Documentation Project
Linux Bootdisk HOWTO
Linux-Embedded
www.linux.org
www.linux.com

--------------------------------------------------------------------------------

      
  

--------------------------------------------------------------------------------
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表