Chinaunix首页 | 论坛 | 博客
  • 博客访问: 509384
  • 博文数量: 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.

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

上一篇:debian升级到5.0

下一篇:编译内核

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

chinaunix网友2011-07-18 17:52:14

深圳天日超声波机电设备有限公司 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=htt

chinaunix网友2011-07-18 17:49:19

深圳天日超声波机电设备有限公司 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.sz

chinaunix网友2011-07-18 17:48:08

深圳天日超声波机电设备有限公司 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-07-18 17:45:56

深圳天日超声波机电设备有限公司 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://

chinaunix网友2011-07-18 17:42:32

深圳天日超声波机电设备有限公司 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.szcsbw.com]高周波[/url] [url=http://www.szcsbw.com]电压机[/url] [url=http://www.szcsbw.com]高频机[/url] [url=http://www.sztr.cn]超声波焊接机[/url] [url=http://www.sztr.cn]金属熔体系统[/url] [url=http: