实验环境:
1、一个作为宿主机的Linux;本文使用的是CentOS 6.5 x86_64;
2、在宿主机上提供一块额外的硬盘作为新系统的存储盘
3、Linux内核源码,busybox源码;本文使用的是目前最新的内核源码linux-3.13.6和busybox-1.22.1。
一、创建新系统盘
先建立一个大小为50M的主分区作为新建系统的boot分区和一个512M的分区作为目标系统的根分区;
添加第三个分区用于swap,大小为256MB,使用mkswap将其创建为swap分区
50M的分区格式化后将其挂载至/mnt/boot目录下;
512M的分区格式化后将挂载至/mnt/sysroot目录;
两个分区的文件系统均为ext4。# mke2fs -t ext4 /dev/sdb1
# mke2fs -t ext4 /dev/sdb2
# mkswap /dev/sdb3
编辑/etc/fstab,添加如下内容/dev/sdb1 /mnt/boot ext4 defaults 0 0
/dev/sdb2 /mnt/sysroot ext4 defaults 0 0
创建目录:# mkdir /mnt/boot
# mkdir /mnt/sysroot
# mount -a //自动挂载
二、编译内核源代码
[root@www ~]# tar xf linux-3.13.6.tar.xz -C /usr/src/
[root@www ~]# cd /usr/src
[root@www src]# ln -sv linux-3.13.6 linux
[root@www ~]# cd linux
这里我们采用make allnoconfig 编译[root@www linux]# make allnoconfig
[root@www linux]# make menuconfig
读者可以从这里下载我配置好的config文件
把下载的config文件改名为.config,复制到/usr/src/linux中,执行下面操作[root@www linux]# make bzImage
//编译完成后面复制文件
[root@www linux]# cp arch/x86/boot/bzImage /mnt/boot/
三、编译busybox
[root@www ~]# yum -y install libmcrypt-devel glibc-static //glibc-static在第2张光盘里
[root@www ~]# tar xf busybox-1.22.1.tar.bz2
[root@www ~]# cd busybox-1.22.1
[root@www ~]# make menuconfig
注:、此处需要选择 Busybox Settings --> Build Options --> Build BusyBox as a static binary (no shared libs),这样可以把Busybox编译成一个不使用共享库的静态二进制文件,从而避免了对宿主机的共享库产生依赖;[root@www busybox-1.22.1]# make
[root@www busybox-1.22.1]# make install
安装后的文件均位于_install目录中;把这些文件复制进/mnt/sysroot[root@www busybox-1.22.1]# cp -a _install/* /mnt/sysroot/
四、建立真正的根文件系统
1.创建必要的目录
[root@www sysroot]# mkdir -pv proc sys etc/rc.d/init.d tmp dev/pts boot var/log bin dev home lib64 media sbin srv usr
2.建立系统初始化脚本文件[root@www sysroot]# vim etc/rc.d/rc.sysinit
#!/bin/sh
echo -e "\tWelcome to \033[31mGulong\033[0m Little Linux"
echo -e "Remounting the root filesystem ..."
mount -t proc proc /proc
mount -t sysfs sysfs /sys
mount -o remount,rw /
echo -e "Creating the files of device ..."
mdev -s
echo -e "Mount filesystems in /etc/fstab"
ifconfig eth0 172.16.251.55
ifconfig lo 127.0.0.1
mount -a
3.而后让此脚本具有执行权限:[root@www sysroot]# chmod +x etc/rc.d/rc.sysinit
4.配置init及其所需要inittab文件
创建两个必要的设备文件:[root@www sysroot]# cd /mnt/sysroot/dev/
[root@www dev]# mknod console c 5 1
[root@www dev]# mknod null c 1 3
创建inittab文件[root@www sysroot]# rm linuxrc
[root@www sysroot]# vim etc/inittab
//添加如下内容:
::sysinit:/etc/rc.d/rc.sysinit
console::respawn:-/bin/sh
::ctrlaltdel:/bin/reboot
::shutdown:/bin/umount -a -r
5.为系统准备一个“文件系统表”配置文件/etc/fstab[root@www sysroot]# vim etc/fstab
//添加以下内容
sysfs /sys sysfs defaults 0 0
proc /proc proc defaults 0 0
/dev/sda1 /boot ext4 defaults 0 0
/dev/sda2 / ext4 defaults 0 0
/dev/sda3 swap swap defaults 0 0
五、为此系统创建所需的引导程序
安装grub
[root@www ~]# grub-install --root-directory=/mnt /dev/sdb
为grub建立配置文件[root@www ~]# vim /mnt/boot/grub/grub.conf
//添加如下内容
default=0
timeout=5
title Mini Linux (3.13.6-gulong)
root (hd0,0)
kernel /bzImage ro root=/dev/sda2 init=/sbin/init
接下来将此块硬盘接入一个新的主机(这里使用的是虚拟机),启动一下并测试使用。
至此基于busybox的linux的已经完成了,在后面的博客中我们会来完善下这个系统,比如远程登录,密码登录等!