10015.Linux EMBEDDING mini HOWTO
发表于2004/10/18 10:26:00 1076人阅读
Luc Hermans, 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.
- 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
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.1 /etc/ld.so.cach /bin/sh /dev/console /dev/null /dev/ram /dev/systty /dev/tty1 /dev/tty2We 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
lddcommand 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
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.
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/* /etcLinuxrc 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 /usrYou 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
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.tgzWhere syslinux.cfg should contain:
timeout 0 default zimage append=load_ramdisk=1 initrd=rootfs.gz root=/dev/ramYou 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!
- After downloading the floppy disk image emblin.img use the command
dd if=emblin.img of=/dev/fd0to 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.cmdto 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).
tar -xzvpf source.tgz /EmbLinand do the reverse of the
Linux Mini HOWTO(中文版)
leapple 2007-08-28 13:20 16
C++ dlopen mini HOWTO
adcxf 2011-04-01 19:48 458
Howto Compile and Install Linux Kernel 3.3rc / 3.2 / 3.1 / 3.0 in Ubuntu 11.10, 11.04, 10.10 and 10.
rookieding 2012-03-07 00:21 708
HOWTO USE linux disksim ssd model
nianglao 2012-08-27 20:11 2990
The Linux Bootdisk HOWTO 之 3. Bootdisks and the boot process 启动盘与启动过程
pro_or_gram 2012-01-18 20:05 740
Linux 串口编程 中英文简体对照 1.0 版 Serial Programming HOWTO
CyberLabs 2011-10-31 08:19 1959
HOWTO do Linux kernel development - take 3 (中文版)
sanlinux 2011-03-21 22:10 758
linux scsi generic howto学习笔记
kafeiflynn 2012-05-17 15:52 7826