Chinaunix首页 | 论坛 | 博客
  • 博客访问: 509391
  • 博文数量: 110
  • 博客积分: 3971
  • 博客等级: 中校
  • 技术积分: 1175
  • 用 户 组: 普通用户
  • 注册时间: 2006-06-20 23:27
文章分类

全部博文(110)

文章存档

2015年(2)

2014年(1)

2013年(1)

2011年(9)

2010年(28)

2009年(12)

2008年(13)

2007年(23)

2006年(21)

我的朋友

分类: LINUX

2010-07-31 14:38:29

Howto create an initramfs image

Initramfs is the new way of doing stuff before the root partition is mounted. This is useful if you need to do something special to get your root partition visible to the kernel. For example, if it is an lvm partition, encrypted or on USB.
You can also stay in the initramfs. This can be very useful for debugging, or if you just want a very fast booting system.

Creating the folder structure

Before we get to the fun bit, you'll need to create a few folders.
Alternatively you can use tarball, which also contains the /bin/busybox binary and the /init script.
The empty mdev.conf is created so that newer versions of busybox would not complain.

$ mkdir -p work/initramfs/{bin,sbin,etc,proc,sys,newroot}
$ cd work
$ touch initramfs/etc/mdev.conf

Getting BusyBox

combines most common UNIX utilities into a single binary. If it's compiled statically, it is usually the only binary file you will need in the initramfs image. Instead you could ldd the dynamicly linked BusyBox binary, and copy all the necessary libraries to /lib in the initramfs, but it is a lot easier to go with a static binary.
BusyBox can be compiled using glibc or uClibc. Both work just the same, but uClibc produces quite a lot smaller binaries.

$ wget -O - | bunzip2 > initramfs/bin/busybox
$ chmod +x initramfs/bin/busybox
$ ln -s busybox initramfs/bin/sh

To use your own BusyBox, but make sure it's static. You can use file /bin/{bb,busybox} | grep static to check if your current system has a static version of busybox

/init

If an initramfs is built into the kernel or passed to it, the kernel will try to execute /init in the initramfs. Usually /init is a shell script that will find and mount the root partition, then switch_root onto the root partiton and execute /sbin/init.

$ touch initramfs/init
$ chmod +x initramfs/init

Then edit initramfs/init

#!/bin/sh

#Mount things needed by this script
mount -t proc proc /proc
mount -t sysfs sysfs /sys

#Disable kernel messages from popping onto the screen
echo 0 > /proc/sys/kernel/printk

#Clear the screen
clear

#Create all the symlinks to /bin/busybox
busybox --install -s

#Create device nodes
mknod /dev/null c 1 3
mknod /dev/tty c 5 0
mdev -s

#Function for parsing command line options with "=" in them
# get_opt("init=/sbin/init") will return "/sbin/init"
get_opt() {
echo "$@" | cut -d "=" -f 2
}

#Defaults
init="/sbin/init"
root="/dev/hda1"

#Process command line options
for i in $(cat /proc/cmdline); do
case $i in
root\=*)
root=$(get_opt $i)
;;
init\=*)
init=$(get_opt $i)
;;
esac
done

#Mount the root device
mount "${root}" /newroot

#Check if $init exists and is executable
if [[ -x "/newroot/${init}" ]] ; then
#Unmount all other mounts so that the ram used by
#the initramfs can be cleared after switch_root
umount /sys /proc

#Switch to the new root and execute init
exec switch_root /newroot "${init}"
fi

#This will only be run if the exec above failed
echo "Failed to switch_root, dropping to a shell"
exec sh

This /init script doesnt actually do anything that you couldn't do without an initramfs, but it can very easily be extended.

Creating the .cpio and .igz

When passing an initramfs to the kernel using your bootloader (the initrd option in Grub), the kernel is expecting a gzipped cpio archive (.igz). When building the initramfs image into the kernel, one of the easiest ways is to create a cpio archive (.cpio).
We will be creating both files.

$ cd initramfs
$ find . | cpio -H newc -o > ../initramfs.cpio
$ cd ..
$ cat initramfs.cpio | gzip > initramfs.igz

You might want to put these commands in a shell script file so you can easily re-create the initramfs images when you make changes to the content of work/initramfs/

Kernel configuration

The only thing you will need in the kernel is CONFIG_BLK_DEV_INITRD=y
If you want to build your initramfs image into the kernel. This way you won't need to pass an initrd option to your bootloader, but when you update the initramfs image, you will also need to rebuild the kernel. If you do want to do this, give the path to the initramfs.cpio file in Initramfs source file(s).

    General Setup --->
[*] Initial RAM filesystem and RAM disk (initramfs/initrd) support
() Initramfs source file(s)

Grub configuration

To pass the initramfs.igz to the kernel using Grub you will need the initrd option in menu.lst (grub.conf for Gentoo users).

title Gentoo
kernel /vmlinuz
initrd /initramfs.igz

As we have set default root partition and init in our work/initramfs/init, we do not need to specify them on the kernel line. You still can specify those options if you want to or need to by appending root=/dev/hda1 init=/sbin/init.

阅读(2987) | 评论(22) | 转发(0) |
0

上一篇:debian升级到5.0

下一篇:编译内核

给主人留下些什么吧!~~

chinaunix网友2011-07-18 17:41:37

深圳天日超声波机电设备有限公司 www.sztr.cn 高周波|超声波|超声波熔接|电压机|超声波清洗机|深圳超声波|超声波维修|清洗机维修|各款超声波维修 邓志彬 13510559166 电话:0755-28993845 28998949 传真:28993843 [url=http://www.sztr.cn]超声波[/url] [url=http://www.sztr.cn]超声波熔接[/url] [url=http://www.sztr.cn]超声波维修[/url] [url=http://www.sztr.cn]深圳超声波[/url] [url=http://www.sztr.cn]超声波清洗机[/url] [url=http://www.sztr.cn]超声波口罩机[/url] [url=http://www.sztr.cn]超声波焊接机[/url] [url=http://www.sztr.cn]金属熔体系统[/url] [url=http://www.sztr.cn]超声波金属熔体处理系统[/url] [url=http://www.

chinaunix网友2011-05-20 14:40:49

[url=http://www.szyigang.com]美孚润滑油[/url] [url=http://www.szyigang.com]壳牌润滑油[/url] [url=http://www.szyigang.com]长城润滑油[/url] [url=http://www.szyigang.com]加德士润滑油[/url] [url=http://www.szyigang.com]UP润滑油[/url] [url=http://www.szyigang.com]力士润滑油[/url] [url=http://www.szyigang.com]埃索润滑油[/url] [url=http://www.szyigang.com]嘉实多润滑油[/url] [url=http://www.szyigang.com]BP润滑油[/url] [url=http://www.szyigang.com]克鲁勃润滑油[/url] [url=http://www.szyigang.com]N-甲基吡咯烷酮nmp[/url]

chinaunix网友2011-01-25 15:02:30

深圳镁极照明技术有限公司 13265734515 [url= http://www.mgledcn.com/products.asp ] LED MR16[/url] [url= http://www.mgledcn.com/products.asp ] LED E27 [/url] [url= http://www.mgledcn.com/products.asp ] LED GU10 [/url] [url= http://www.mgledcn.com/products.asp ] LED MR16[/url] [url= http://www.mgledcn.com/products.asp ] LED Spotlight [/url] [url= http://www.mgledcn.com/products.asp ] LED Spot light [/url] [url= http://www.mgledcn.com/products.asp ] LED downlight [/url] [url= http://www

chinaunix网友2010-10-09 11:12:36

[url=http://www.hopebright-led.com]led auto bulb[/url] [url=http://www.hopebright-led.com]led auto light[/url] [url=http://www.hopebright-led.com]led auto lights[/url] [url=http://www.hopebright-led.com]led auto lighting[/url] [url=http://www.hopebright-led.com]led auto lamp[/url] [url=http://www.hopebright-led.com]led car bulb[/url] [url=http://www.hopebright-led.com]led car light[/url] [url=http://www.hopebright-led.com]led car lights[/url] [url=http://www.hopebright-led.com]led car li