Front | Info | Lists | Newsfeeds | Study Guide | What is BSD? RSS  

Building tiny systems with embedded NetBSD (page 2)

Back to page one

Creating the filesystem image

Now that our binaries are all wrapped up, the next thing to do is to populate the filesystem. This involves linking all the programs to the crunched binary and copying or creating the necessary data files. This is easily done with the following commands. Don't forget to set the permissions to root:wheel!
$ mkdir files/bin files/sbin files/dev

$ cp /dev/MAKEDEV files/dev
$ cd files/dev
$ ./MAKEDEV floppy ramdisk wscons
$ cd ../..

$ cp work/mytiny files/sbin/init
$ ln files/sbin/init files/bin/sh
$ ln files/sbin/init files/bin/ls
$ ln files/sbin/init files/sbin/reboot

$ su root
# chown -R root:wheel files
# exit
With the filesystem populated, we can wrap it up into an image file that can be embedded into a kernel. This is done with the makefs command. This tool lets you take a directory and bundle it up into a single file. Make sure that your size is not larger than the size you specified in your kernel configuration. Once the image is created, link it to the kernel with the mdsetimage command. Your kernel is now ready to go!

You can compress your kernel if you like. The standard NetBSD bootloader knows how to decompress gzipped kernels. Remember, your spartan OS includes 4 megabytes of mostly empty space in the filesystem. I compressed my kernel down to 825,544 bytes.

# makefs -s 4m -t ffs crunch.image files
# mdsetimage netbsd.ramdisk crunch.image
# gzip -c netbsd.ramdisk > netbsd
# ls -l netbsd
-rw-r--rw-  1 brose  users  825544 Aug 24 22:28 netbsd

When you boot this kernel, you will see the normal output and then it will present you with a shell prompt. At that time, you can do whatever it is you need to do. My example will let you move around the file structure, list the contents with ls, and reboot. Not very useful, but it is a good starting point.

The minimum multi user system

A multi user system is built in the same manner, but you need a few extra programs and data files. I also use the standard init program. Notice that I commented out the special init line, so that I use the stock init for the multiuser configuration. You may want to go back to your init source and make a fresh init, just in case the .o files are from your single user build.

$ cd /usr/src/sbin/init
$ make clean; make
Here's my crunchgen configuration.
srcdirs /usr/src/bin /usr/src/sbin /usr/src/usr.bin /usr/src/usr.sbin /usr/src/libexec

progs init mount newfs mount_ffs sh ttyflags getty pwd_mkdb passwd login reboot ls

ln sh -sh
ln newfs mount_mfs

# special init objpaths /usr/src/sbin/init/init.smallprog.o

# libraries used by the programs
# ---------------- Minimum single user files
# init : -lutil -lcrypt
# mount :
# newfs : -lutil
# mount_ffs : 
# sh : -ll -ledit -ltermcap
# ---------------- Minimum multiuser files
# ttyflags : 
# getty : -lutil -ltermcap
# pwd_mkdb : -lutil
# passwd : -lrpcsvc -lcrypt -lutil -lkrb5 -lcrypto -lasn1 -lcom_err -lroken
# login : -lutil -lcrypt  -lskey  -lkrb5 -lasn1  -lkrb -lcrypto -lroken -lcom_err
# ---------------- Useful utilities
# ls :
# reboot : -lutil
# umount :
libs -lutil -ll -ledit -ltermcap -lcrypt -lrpcsvc -lkrb5 -lkrb -lcrypto -lasn1 -lcom_err -lroken -lskey
And my shell commands for populating the filesystem.
mkdir files/bin files/sbin files/usr files/etc files/var files/dev files/tmp files/root files/home 
mkdir files/usr/bin files/usr/sbin files/usr/libexec
mkdir files/var/run files/var/db files/var/crash
mkdir files/usr/share
mkdir files/usr/share/misc

cp /dev/MAKEDEV files/dev
cd files/dev
./MAKEDEV floppy ramdisk wscons
cd ../..

echo "/dev/md0a / ffs rw 1 1" > files/etc/fstab

echo "echo Initializing system..." > files/etc/rc
echo "export PATH=/sbin:/bin:/usr/sbin:/usr/bin" >> files/etc/rc
echo "mount -ua" >> files/etc/rc
echo "ttyflags -a" >> files/etc/rc

cp work/mytiny files/sbin/init
ln files/sbin/init files/bin/ls
ln files/sbin/init files/sbin/mount
ln files/sbin/init files/sbin/mount_ffs
ln files/sbin/init files/sbin/mount_mfs
ln files/sbin/init files/sbin/umount
ln files/sbin/init files/bin/sh

ln files/sbin/init files/usr/libexec/getty
ln files/sbin/init files/sbin/ttyflags
ln files/sbin/init files/sbin/pwd_mkdb
ln files/sbin/init files/usr/bin/passwd
ln files/sbin/init files/usr/bin/login
ln files/sbin/init files/sbin/reboot
ln files/sbin/init files/sbin/newfs

cp /etc/ttys files/etc
cp /etc/master.passwd files/etc
cp /etc/pwd.db files/etc
cp /etc/spwd.db files/etc
cp /etc/passwd files/etc
cp files/usr/share/misc/termcap 
cp /etc/gettytab files/etc

The file is simply a hand trimmed version of the termcap file. This is used by the getty program when initializing the console. Since it is very large (over 500k) and most of it is useless, you can trim out the terminal types that you don't use. I trimmed mine down to about 8k.

Once you have done this, simply place this filesystem into a kernel and boot. As you can see, a multi user setup is a bit larger. But most of the extra space is from incorporating the extra libraries for the login and getty programs. As you add more code to the system, your code growth should not be as dramatic.

# makefs -s 4m -t ffs crunch.image files
# mdsetimage netbsd.ramdisk crunch.image
# gzip -c netbsd.ramdisk > netbsd
# ls -l netbsd
-rw-r--rw-  1 brose  users  1066057 Aug 24 22:28 netbsd

Brian Rose is an electrical engineer who has developed embedded software for the telecom and video distribution industries. He is currently on involuntary hiatus (layoff) and pondering the benefits of being a scuba instructor at Mexican resorts.

Copyright (C) 2003 by Brian Rose, all rights reserved.
Reproduced by BSD with permission of the author.

Newsfactor: Open Source Alternative: NetBSD


Discuss this article below.

Very Nice. - Ian Harding
Booting - Aniju Lukose
Cool - Geert Hendrickx
make -D SMALLPROG - Tony

Very Nice.
Ian Harding - September 20, 2003 23:20:17
This is a very good article. Thank you. I am going to try it out now!!

Aniju Lukose - May 12, 2004 13:52:11
Very good......

Thank you for ....

Geert Hendrickx - April 28, 2005 05:56:28
Thanks for this explanation. It helped me creating a custom macppc-installer fitting on 1 floppy.

Tony - December 18, 2007 00:36:04
Hello, Good work. When one make -D SMALLPROG You will receive an no such file error unless you have built the tooldir. Of course gcc -c init.c will build init.o (without SMALLPROG being set). How had you intended process to take place. Is it useful to build the tools? Thanks in advance





Stop Spam Abuse: What operating system's CVS history begins in March 1993?

BSD Links

· Advocacy
· Drivers
· Events
· Flavours
· FAQs
· Guides
· Programming
· Security
· Software
· User Groups

September 16, 2013 11:24:32

Front | Information | Lists | Newsfeeds | Study Guide