您的位置:首页 > 运维架构 > Linux

在linux下如何制作img的映像文件

2012-10-10 12:12 597 查看
看你想将rh_linux.img做成什么样子。如果是文件系统的话,那么你需要这么作:
dd if=/dev/zero of=rh_linux.img count=CONTS
参数count表示需要创建的文件大小(以块为单位,每块大小为512字节,如果rh_linux.img大小为1M,则CONTS=2000).
创建完成rh_linux.img以后,需要使用文件系统工具(mkfs.ext2、mkfs.ext3、mkfs.jffs2等)格式化它(例如:
mkfs.ext3 rh_linux.img);接下来使用:mount -o loop MDIR
rh_linux.img将镜象文件挂载到目录MDIR下,将boot.bin拷贝进去就可以了。
一.为什么要做这样一个linux
我一直想做一个属于自己的小型的linux,但从网上搜索所得结果都是打造放在软盘中的linux,而且都是用lilo启动。很不幸,本人的笔记本没有软驱,所以这些也不适合自己。干脆自己做一个可从光盘启动并运行的linux。
二.Linux启动简介
系统加电后进入bios,随后读取硬盘的主引导记录(MBR),然后调用另一个引导程序(grub或lilo)来加载内核和镜像文件。加载内核后系统会把文件系统存放到ram中,然后系统运行。在这里我们使用grub来加载内核和镜像文件。
三.编译内核
我使用的linux 2.6 内核,详细编译内核方法可以从以下文章中得到,这里不再赘述。 http://hi.baidu.com/lianxi1ArrayArrayArray/blog/item/Array5c782111f75a212b8127b03.html 四.编译busybox
busybox是一个集成了一百多个最常用linux命令和工具的软件,我的理解就是用来生成linux下的常用命令的小程序。把编译后的busybox程序放到我们的要做的系统中后,我们就能用一些linux下的常用命令了。
编译busybox与编译其他linux下的程序一样,首先下载busybox,我用的是1.0版。
#tar xvfz busybox-1.00.tar.gz // 解开busybox
#cd busybox-1.00
#make menuconfig //配置busybox
下面是需要编译进busybox的功能选项,其他的可以根据需要自己
General Configuration应该选的选项
Show verbose applet usage messages
Runtime SUID/SGID configuration via /etc/busybox.conf
Build Options
Build BusyBox as a static binary (no shared libs)
这个选项是一定要选择的,这样才能把busybox编译成静态链接的可执行文件,运行时才独立于其他函数库.否则必需要其他库文件才能运行,在单一个linux内核不能使他正常工作.
Installation Options
Don’t use /usr
这个选项也一定要选,否则make install 后busybox将安装在原系统的/usr下,这将覆盖掉系统原有的命令.选择这个选项后,make install后会在busybox目录下生成一个叫_install的目录,里面有busybox和指向他的链接.
其他选项都是一些linux基本命令选项,自己需要哪些命令就编译进去,一般用默认的就可以了.
配置好后退出并保存.
#make //编译busybox
#make install //安装busybox
编译好后在busybox目录下生成子目录_install,里面的内容:
bin
linuxrc -> bin/busybox
sbin
五. 制作文件系统 (这一步主要参 http://blog.linuxmine.com/html/34/6534_itemid_861.html 谢谢GuCuiwen)
这是比较麻烦的一步,我弄了好久,看了挺多网上的文章,但总是不对。
建一个目录rootfs 用来装文件系统
#mkdir etc usr var tmp proc home root dev //建立文件目录
其中etc,proc和dev是一定要建的,bin和sbin可以拷贝busybox生成的,其他的可以象征性的建几个就可以了.
拷贝busybox下的_install文件夹到rootfs下
#cp -R busybox-1.00/_install/* rootfs/
在dev文件夹下建立设备文件名:
#cd rootfs/dev
为了方便我们直接从原系统的/dev目录下拷贝过来.一定要叫-r参数
#cp -R /dev/console ./
#cp -R /dev/null ./
#cp -R /dev/zero ./
...
其中,fd0,hda,ram,ram1,tty1,null,zero,loop1,fb0,fb,tty(这个GuCuiwen文章中没有,但我觉得要加)等是必备的.
其它的hda,hda1,hdb等可以根据实际需要决定.但是上表中的选择是比较合理的,即能满足大部分的需要,
建立etc目录下的配置文件
busybox.conf group inittab motd passwd resolv.conf shadow-
fstab init.d issue mtab profile shadow
其中init.d是一个目录,从busybox-1.00源代码目录下拷贝过来.
#cp -R busybox-1.00/examples/bootflopyp/etc/init.d rootfs/etc/
把init.d拷过来后要更改其中的文件rcS:
请确保这个文件是可执行的,否则请改成可执行的:
#chmod u+x rcS
用记事本打开rcS的内容:

#! /bin/sh
mount -o remount,rw /
/bin/mount -a
> /etc/mtab
echo
echo
echo
echo
echo -en "\t\tWelcom to lxh Linux\\033][0;3Arraym\n"
hostname LxhLinux


echo后面跟的都是系统可以自己作相应的修改.

busybox.conf是一个空文件.

其他文件的内容如下:

fstab
/dev/fd0 / ext2 defaults 0 0
none /proc proc defaults 0 0
/dev/cdrom /mnt/cdrom udf,isoArray660 noauto,owner,kudzu,ro 0 0
/dev/fd0 /mnt/floppy auto noauto,owner,kudzu 0 0
group
root:0:root
inittab
::sysinit:/etc/init.d/rcS
::askfirst:/bin/sh
tty2::respawn:/bin/getty 38400 tty2
tty3::respawn:/bin/getty 38400 tty3
tty4::respawn:/bin/getty 38400 tty4
# Stuff to do when restarting the init process
::restart:/bin/init
# Stuff to do before rebooting
::ctrlaltdel:/bin/reboot
::shutdown:/bin/umount -a -r
::shutdown:/bin/swapoff -a
issue
Baby Linux release 0.1
motd
mtab
passwd
root::0:0:root:/root:/bin/ash
profile
# /etc/profile: system-wide .profile file for the Bourne shells
echo
echo
export PS1="[\u@\h \w]\$"
echo "Done"
alias ll=’ls -l’
alias du=’du -h’
alias df=’df -h’
alias rm=’rm -i’
echo


resolv.conf
nameserver 1Array2.168.1.12
shadow
root1$$adltABArraySr/MSKqylIvSJT/:12705:0:ArrayArrayArrayArrayArray:7:::
shadow-
root1$DWU.tenP$B7ANiXoGoiZMwJR6Ih8810:12705:0:ArrayArrayArrayArrayArray:7:::


六.制作img映象文件

把上面的文件系统做成img映像文件,以便内核把它载入ram中

linux下有很多ram,我们用ram1,首先把ram1格式化成ext2文件系统

[root@gucuiwen babylinux]# mkfs.ext2 -m0 /dev/ram1

mke2fs 1.32 (0Array-Nov-2002)

Filesystem label=

OS type: Linux

Block size=1024 (log=0)

Fragment size=1024 (log=0)

1024 inodes, 40Array6 blocks

0 blocks (0.00%) reserved for the super user

First data block=1

1 block group

81Array2 blocks per group, 81Array2 fragments per group

1024 inodes per group

Writing inode tables: done

Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 37 mounts or

180 days, whichever comes first. Use tune2fs -c or -i to override.

将ram1挂装到文件系统中:

先建立一个挂装点:

#mkdir /mnt/ram

挂上ram1:

#mount /dev/ram1 /mnt/ram

将先前做好的rootfs根文件系统拷贝到ram1上.

#cp ?R rootfs/* /mnt/ram

拷贝好根文件系统后卸载ram1:

#umount /dev/ram1

再用dd把这个ram1以映象方式取出来:

[root@gucuiwen babylinux]# dd if=/dev/ram1 of=initrd.img

读入了 81Array2+0 个块输出了 81Array2+0 个块

把生成的initrd.img拷到一个地方备用

七. 制作grub启动光盘

首先下载grub安装文件,我下的是0。Array6版的grub。

tar zxvf grub-0.Array5.tar.gz //解开grub

cd grub-0.Array5

./configure

make

make install

好了,grub安装完成。安装完成后得到了最重要的stage2_eltorito 我们把它拷出来备用。

现在开始制作启动光盘:

mkdir iso ////建立iso文件夹,我们把所要的文件拷进这个文件夹

mkdir -p iso/boot/grub

cp /stage2_eltorito所在的路径/stage2_eltorito iso/boot/grub ////把stage2_eltorito文件拷入grub文件夹中

cp /boot/grub/menu.lst iso/boot/grub

cp /boot/grub/grub.conf iso/boot/grub

////把grub所用的启动文件grub.conf,menu.lst 拷入grub文件夹

下面我们更改menu.lst中的内容

# grub.conf generated by anaconda

#

# Note that you do not have to rerun grub after making changes to this file

# NOTICE: You do not have a /boot partition. This means that

# all kernel and initrd paths are relative to /, eg.

# root (hd0,0)

# kernel /boot/vmlinuz-version ro root=/dev/hda1

# initrd /boot/initrd-version.img

#boot=/dev/hda

default=1

timeout=10

title I love lxh ^_^ //显示在选项上的标题

root (cd)

kernel /boot/grub/vmlinuz-2.6.20 ro root=/dev/ram0

initrd /boot/grub/initrd.img

要更改的内容我用红色表示了,cd表示从光驱启动,/boot/grub/vmlinuz-2.6.20表示内核文件vmlinuz-2.6.20在光盘

上存放的路径,/dev/ram0表示根文件目录的位置,由于我们是从光盘启动,启动时所有文件系统都拷入了ram,所以这里我们从ram0启动。

/boot/grub/initrd.img是我们上一步我们做的镜像文件存放在光盘的位置

八,集成,刻录

下面我们把所制作的文件全部集成在一起

iso-》boot -》grub -》grub.conf

iso-》boot -》grub -》menu.lst

iso-》boot -》grub -》stage2_eltorito

iso-》boot -》grub -》vmlinuz-2.6.20

iso-》boot -》grub -》initrd.img

vmlinuz-2.6.20,和 initrd.img可以不放在这里,若更改了存放路径则grub.conf中也要更改。

mkisofs -R -b boot/grub/stage2_eltorito -no-emul-boot -boot-load-size 4 -boot-info-table -o grub.iso iso

////这步的意思是把iso文件夹中的内容做成一个镜像,-b参数是表示做一个启动盘 ,其他的东西我也不太明白,如果哪位高手知道-no-emul-boot -boot-load-size 4 -boot-info-table是什么意思希望mail我。

好了,现在grub.iso就是你所需要的镜像了,我们可以先放到虚拟机的光驱中看看它是不是能够引导系统。如果不能仔细检查没一步。注意

grub.iso不能用winiso制作,原因好像是winiso做的iso文件缺少了光盘信息。但我们可以用demotools载入grub.iso文

件,然后用全盘复制的方式刻录一张光盘linux。

九,这里我们用grub来引导,用lilo是一样的,只是配置文件不同而已,网上关于lilo的文章很多。

十:关于ram

制作过程中关于ram我很迷惑,现在我的理解是计算机把一部分内存模拟成磁盘,所以我们可以对这部分内存进行格式化。这个格式化一个真正的磁盘是一样的,但是由于这毕竟是虚拟的,所以其中的数据掉电后就会消失。这部分内存就叫ram

用grub打造属于自己的光盘linux.rar

转自:
http://hi.baidu.com/websfx/blog/item/6e16aa3e565277fb838b1353.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: