网站首页 - 关于我们 - 团队成员 - 联系我们

 rss
♀ 您现在的位置在:首页 -> CNFUG期刊 -> 正文(阅读
制作RAMDISK in KERNEL的OpenBSD微系统

Matthew(黑夜编码人) <matthew@cnfug.org>

前言

在以前我们已经讲过如何制作软盘上运行的FreeBSD系统,现在我们来看看如何制作一个运行在软盘上的OpenBSD系统。与软盘上的FreeBSD不同的是,这次我们将系统中的所有配制文件及程序全部存放到OpenBSD的内核中,这样整个系统看起来就是一个文件。下面让我们具体来看看如何完成这样一个微系统的制作。

0、OpenBSD的启动过程

当硬盘MBR中的引导程序接过启动之后,MBR中的程序将读入硬盘OpenBSD分区中的引导程序,引导程序默认情况下会加载/boot,然后由boot载入内核,此时内核开始检测一些硬件和做一些初始化。初始化完成后kernel将mount root device,然后启动系统初始化进程/sbin/init,init将根据/etc/rc中的设置来进行初始化等。

1、定制RAMDISK内核

要使用RAMDISK in KERNEL就必须在内核配制文件中加入以下选项:

option RAMDISK_HOOKS
option MINIROOTSIZE=3560
pseudo-device rd 1

以上的内核参数意义如下:
option RAMDISK_HOOKS:开启ramdisk in kernel的功能。
option MINIROOTSIZE=xxx:设置内核中ramdisk的大小,以512b为单位(据说最大只能为23000 )
pseudo-device rd 1:在内核中产生1个ramdisk设备。

2、制作ramdisk镜像

# dd if=/dev/zero of=/ramdisk bs=512 count=3560
# vnconfig -c svnd0 /ramdisk
# disklabel -r -w svnd0 rdroot
# newfs -m 0 -S 512 -i 4096 /dev/svnd0a
# mount /dev/svnd0a /mnt
# mkdir /mnt/{dev,bin,sbin,etc}
# cp /dev/MAKEDEV /mnt/dev
# cp /bin/{sh,ls} /mnt/bin
# cp /sbin/init /mnt/sbin
# cd /mnt/dev/ && ./MAKEDEV ramdisk
# echo 'echo "This is my minibsd all in kernel" && /bin/sh' > /mnt/etc/rc
# sync && umount /mnt
# sync && vnconfig -u svnd0 && sync

3、将ramdisk写入kernel
# cc -o rdsetroot /usr/src/distrib/common/elfrdsetroot.c
# ./rdsetroot /minibsd < ramdisk

4、压缩kernel大小
# gzip -9 -c /minibsd > /minibsd.gz

5、制作目标软盘

因为系统需要使用/boot(/usr/mdec/boot)来加载kernel,所以我们需要在软盘上放置boot文件。

# disklabel -B -w -r /dev/fd0a fd1440
# newfs -m 0 -S 512 /dev/fd0a
# mount /dev/fd0a /mnt
# cp /usr/mdec/boot /mnt
# cp /minibsd.gz /mnt/bsd

现在你的ramdisk in kernel的miniopenbsd系统就已经做好了。

网友评论  以下本论与CNFUG(China FreeBSD User Group)立场无关
网友:Ansin 发表时间:2004年06月26日

方法很简单,也很实用,不过在编写过程中的语句时,把其相对应的内容写出来就好了,这样便于学习。你认为呢?

网友:rianren 发表时间:2004年11月27日

A Readonly filesystem on Bootable CD
这篇文章说的很仔细, 可算一个补充说明吧!

http://www.daemonnews.org/200106/bootable_CD.html

网友:rianren 发表时间:2004年12月03日

option RAMDISK_HOOKS # enable mini root hooks
option MINIROOTSIZE=23000 # mini root size of 11.5mb
...
# root lives on rd0a (ramdisk) with swap maybe on rd0b or wd0b disks
config bsd root on rd0a swap on rd0b and wd0b
...
pseudo-device rd 1 # ramdisk device driver

So initially I thought that I could build everything that I needed into the kernel ramdisk but unfortunately, there are kernel linking and runtime limitations on the size of this filesystem. After experimentation, I determined that a miniroot size of 23000 blocks (11.5mb) was about the maximum size which works. Sizes of 100000 (50mb) or above fail at the kernel link time, probably due to some sort of address violation (bleah). 30000 (15mb) sizes cause the kernel to immediately reboot, probably exceeding some boot or AT architecture limitation (grumble). 25000 (12.5mb) seems to hang immediately after kernel load (sigh). So 23000 (11.5mb) is the magic working size. No, I didn't try 24000. I figured that I can get just about all of the files from / that I want into this size. The other partitions would reside on the compact flash and would be copied into memory filesystems created at boot time by the /etc/rc boot script.

option MINIROOTSIZE=xxx:设置内核中ramdisk的大小,以512b为单位(据说最大只能为23000 )

minirootsize应该没有这个限制吧! 但不知最大是多少!

网友:rianren 发表时间:2004年12月03日

CRUNCH 0.3 README 7/23/94

Crunch is available via anonymous ftp to ftp.cs.umd.edu in
pub/bsd/crunch-0.3.tar.gz

WHAT'S NEW IN 0.3

* The prototype awk script has been replaced by a more capable and
hopefully more robust C program.
* No fragile template makefiles or dependencies on the details of the
bsd build environment.
* You can build crunched binaries even with no sources on-line, you
just need the .o files. Crunchgen still will try to figure out as
much as possible on its own, but you can override its guessing by
specifying the list of .o files explicitly.
* Crunch itself has been bmake'd and some man pages written, so it
should be ready to install.
* Added patch for FreeBSD from Jordan Hubbard, plus the .conf files used
for the FreeBSD install floppies as examples.


INTRODUCTION

Crunch is a little package that helps create "crunched" binaries for use
on boot, install, and fixit floppies. A crunched binary in this case is
one where many programs have been linked together into one a.out file.
The different programs are run depending on the value of argv[0], so
hard links to the crunched binary suffice to simulate a perfectly normal
system.

As an example, I have created an 980K crunched "fixit" binary containing
the following programs in their entirety:

cat chmod cp date dd df echo ed expr hostname kill ln ls mkdir
mt mv pwd rcp rm rmdir sh sleep stty sync test [ badsect
clri disklabel dump rdump dmesg fdisk fsck halt ifconfig init
mknod mount newfs ping reboot restore rrestore swapon umount
ftp rsh sed telnet rlogin vi cpio gzip gunzip gzcat

Note carefully: vi, cpio, gzip, ed, sed, dump/restore, some networking
utilities, and the disk management utilities, all in a binary small
enough to fit on a 1.2 MB root filesystem floppy (albeit with the kernel
on its own boot floppy). A more reasonable subset can be made to fit
easily with a kernel for a decent one-disk fixit filesystem.

The linking together of different programs by hand is an old
space-saving technique. Crunch automates the process by building the
necessary stub files and makefile for you (via the crunchgen program),
and by doctoring the symbol tables of the component .o files to allow
them to link without "symbol multiply defined" conflicts (via the
crunchide program).


BUILDING CRUNCH

Just type make, then make install.

Crunch was written and tested under NetBSD/i386, but should work under
other PC BSD systems that use GNU ld.

The crunchgen(1) and crunchide(1) man pages have more details on using
crunch, and the examples subdirectory contains some working .conf files
and a sample Makefile.

CREDITS

Thanks to the NetBSD team for a consistently high quality effort in
bringing together a solid, state of the art development environment.

Thanks to the FreeBSD guys; Rod Grimes, Nate Williams and Jordan
Hubbard; and to Bruce Evans, for immediate and detailed feedback on
crunch 0.1, and for pressing me to make the prototype more useable.

Crunch was written for the Maruti Hard Real-Time Operating System
project at the University of Maryland, to help make for better install
and recovery procedures for our NetBSD-based development environment. It
is copyright (c) 1994 by the University of Maryland under a UCB-style
freely- redistributable notice. See the file COPYRIGHT for details.

Please let me know of any problems or of enhancements you make to this
package. I'm particularly interested in the details of what you found
was good to put on your fixit or install disks. Thanks!

Share and Enjoy,
Jaime
............................................................................
: Stand on my shoulders, : jds@cs.umd.edu : James da Silva
: not on my toes. : uunet!mimsy!jds : http://www.cs.umd.edu/users/jds

我也说几句




推荐给好友

  


文章下载

本功能正在开发中,目前不能使用,敬请原谅。

√ 期刊在线投稿: /journal/contribute.html

√ 本文打印于《CNFUG期刊》,欢迎访问 http://www.cnfug.net 获取更多技术文章。