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

打造自己的专属Linux(一):快速建立一个小型Linux

2013-09-09 17:58 489 查看
##请脚下留情。。。踩我的同学请告诉我原因,(二)刚发上去一上午被踩了10次。。。

##打造自己的专属Linux(二)的链接/article/2357180.html

跟着马哥学Linux已经快一个月了,在前几天创建一个小型Linux,大小不足10M,萌生了一个想法:DIY一个自己的Linux,所以准备写一个系列篇,通过宿主机的方式,一步一步创建一个小型Linux,并慢慢增加它的功能模块:网络,服务,ssh,图形界面等等~如果你有兴趣,那么我们一起来学习吧!
如果你对linux的启动流程不熟悉的话可以读我的文章:

深入浅出:linux启动流程刨析 http://blog.csdn.net/deansrk/article/details/6659421
我的linux版本:redhat 5.4

虚拟机客户端:Vmware Workstation 6.5

我们分步骤来实现这个小型linux

第一步:在虚拟机上添加一块新硬盘到linux,大小20G (可以自定)



第二步:1)打开终端,使用fdisk格式化分区刚才挂载的新硬盘/dev/sdb,划分出一个50M大小的主分区和一个512M大小的主分区。

[cpp] view
plaincopy

[root@server69 ~]# fdisk /dev/sdb

The number of cylinders for this disk is set to 2610.

There is nothing wrong with that, but this is larger than 1024,

and could in certain setups cause problems with:

1) software that runs at boot time (e.g., old versions of LILO)

2) booting and partitioning software from other OSs

(e.g., DOS FDISK, OS/2 FDISK)

Command (m for help): p

Disk /dev/sdb: 21.4 GB, 21474836480 bytes

255 heads, 63 sectors/track, 2610 cylinders

Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks Id System

/dev/sdb1 1 7 56196 83 Linux

/dev/sdb2 8 70 506047+ 83 Linux

/dev/sdb3 71 102 257040 82 Linux swap / Solaris

Command (m for help): n

Command action

e extended

p primary partition (1-4)

p

Selected partition 4

First cylinder (103-2610, default 103):

Using default value 103

Last cylinder or +size or +sizeM or +sizeK (103-2610, default 2610): +50M

Command (m for help): w

2)重读下分区表

[cpp] view
plaincopy

[root@server69 ~]# partprobe /dev/sdb

格式化刚建立的分区为ext3文件系统

[cpp] view
plaincopy

[root@server69 ~]# mke2fs -j /dev/sdb1

[root@server69 ~]# mke2fs -j /dev/sdb2

第三步:将刚格式化好的分区/dev/sdb1挂载到/mnt/boot /dev/sdb2挂载到/mnt/sysroot,如果你没有这两个文件夹,请手动建立,如果你挂载了其他设备,可以换其他路径。

[cpp] view
plaincopy

[root@server69 LX]# mount /dev/sdb1 /mnt/boot

[root@server69 LX]# mount /dev/sdb2 /mnt/sysroot

第四步:1)安装grub到/dev/sdb

[cpp] view
plaincopy

[root@server69 mnt]# grub-install --root-directory=/mnt/ /dev/sdb

Installation finished. No error reported.

This is the contents of the device map /mnt//boot/grub/device.map.

Check if this is correct or not. If any of the lines is incorrect,

fix it and re-run the script `grub-install'.

(fd0) /dev/fd0

(hd0) /dev/sda

(hd1) /dev/sdb

2)修改/mnt/boot/grub/device.map

[root@server69 mnt]# vim /mnt/boot/grub/device.map

(fd0) /dev/fd0 //去掉这一行 fd指的是软盘

(hd0) /dev/sda

(hd1) /dev/sdb //去掉这一行

第五步:拷贝kernel和initrd到小linux里,如果对kernel和initrd没有概念的话也请看我的文章,里面有详细的说明

深入浅出:linux启动流程刨析

1)拷贝kernel文件,并且重命名方便记忆。

[cpp] view
plaincopy

[root@server69 LX]# cp /boot/vmlinuz-2.6.18-164.el5 /mnt/boot/vmlinuz

2)拷贝initrd文件,并且重命名方便记忆。

[cpp] view
plaincopy

[root@server69 LX]# cp /boot/initrd-2.6.18-164.el5.img /mnt/boot/initrd

第六步:解压缩initrd, 编辑里面的init文件,然后重新压缩封装。

[cpp] view
plaincopy

[root@server69 mnt]# mv initrd initrd.gz //刚其改名,否则gzip不识别

[root@server69 mnt]# gzip -d initrd.gz //解压

[root@server69 mnt]# cpio -id < initrd.gz //释放归档

14731 blocks

[root@server69 mnt]# ls

bin boot dev etc init lib proc sbin sys sysroot

//看到了吗?initrd几乎具有所有的linux文件目录,简直就是一个小linux

[root@server69 mnt]# vim init

99 mkrootdev -t ext3 -o defaults,ro /dev/vol0/root // 修改为 mkrootdev -t ext3 -o defaults,ro /dev/sda2

[root@server69 mnt]#find. | cpio -H newc -o quiet | gzip -9 //重新归档并压缩

第七步:创建小型linux的文件目录结构

[cpp] view
plaincopy

[root@server69 mnt]# cd /mnt/sysroot/

[root@server69 sysroot]# mkdir -pv root bin sbin usr/{bin,sbin} lib dev tmp proc sys home mnt media boot

第八步:拷贝命令到小型linux里去

1)怎么样拷贝命令?

[cpp] view
plaincopy

[root@server69 sysroot]# which bash //使用which命令查看命令所在路径

/bin/bash

[root@server69 sysroot]# ldd /bin/bash //ldd 加命令路径可以显示出这个命令所依赖的库文件

linux-gate.so.1 => (0x008e6000) //这个文件不用拷贝

libtermcap.so.2 => /lib/libtermcap.so.2 (0x0041e000)

libdl.so.2 => /lib/libdl.so.2 (0x003ef000)

libc.so.6 => /lib/libc.so.6 (0x002a7000)

/lib/ld-linux.so.2 (0x00284000)

[root@server69 sysroot]# cp /lib/lidkl.so.2 /mnt/sysroot/lib/ //将上面的库文件都复制到小linux对应的目录

2)拷贝init 和bash ,mount , ls 这几个个命令去linux 注意,init和bash必须拷贝。

第九步:创建 /mnt/etc/fstab文件和/mnt/etc/inittab 文件,(这个文件的说明可以在我的启动流程文章里找到)

[cpp] view
plaincopy

[root@server69 ~]# vim /mnt/sysroot/etc/fstab

/dev/sda2 / ext3 defaults 0 0

/dev/sda1 /boot ext3 defaults 0 0

sysfs /sys sysfs defaults 0 0

proc /proc proc defaults 0 0

[root@server69 ~]# vim /mnt/sysroot/etc/inittab

id:3:initdefault:

si::sysinit:/etc/rc.d/rc.sysinit

第十步:创建/mnt/boot/grub/grub.conf 和 /etc/rc.d/rc.sysinit,并给rc,sysyinit 赋执行权限

[cpp] view
plaincopy

[root@server69 ~]# vim /mnt/boot/grub/grub.conf

default=0

timeout=10

title Little Linux //grub的标题,可以自己写

kernel /vmlinuz ro root=/dev/sda2 quiet //加载内核,只读方式挂载根文件系统

initrd /initrd.gz //加载内存文件系统

[root@server69 ~]# vim /mnt/sysroot/etc/rc.d/rc.sysinit

#!/bin/bash

echo -e "\t\tWelcome to \033[31mLittle\033[0m Linux" //这里可以写自己想写的欢迎界面 ^^

mount -n -o remount.rw / //以读写方式重启挂载根文件系统

mount -n -a //挂载所有设备,并不记录

/bin/bash //加载bash shell

[cpp] view
plaincopy

<pre name="code" class="cpp" style="margin-top: 4px; margin-right: 0px; margin-bottom: 4px; margin-left: 0px; background-color: rgb(240, 240, 240); ">[root@server69 ~]# chmod +x /etc/rc.d/rc.sysinit //给rc.sysinit赋执行权限 </pre>

<pre></pre>

<p></p>

<p>好了,大功告成,新建一个虚拟机,选择linux2.6xx ,挂载上这块硬盘看下效果吧~</p>

<p>这是我的grub界面<img alt="微笑" src="http://static.blog.csdn.net/xheditor/xheditor_emot/default/smile.gif">很朴素吧~你可以自己更改背景哈</p>

<img src="http://hi.csdn.net/attachment/201108/4/0_13124675894oO6.gif" alt="">启动完成的效果,很简陋吧,注意Little<img alt="偷笑" src="http://static.blog.csdn.net/xheditor/xheditor_emot/default/titter.gif"><img src="http://hi.csdn.net/attachment/201108/4/0_1312467597Bd65.gif" alt="">

<p><span style="font-size:18px; color:#CC0000">##更新:第十步最后要给rc.sysinit赋执行权限,否则可能系统加载时无法执行!</span></p>

<p></p>

<span style="font-size:18px">下篇预告:全自动脚本实现自动创建小linux和命令移植,让你的小linux健步如飞~</span>

<pre></pre>

<pre></pre>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: