Welcome 微信登录

首页 / 操作系统 / Linux / 私人定制自己的Linux小系统

一、前言Linux操作系统至1991.10.5号诞生以来,就源其开源性和自由性得到了很多技术大牛的青睐,每个Linux爱好者都为其贡献了自己的一份力,不管是在Linux内核还是开源软件等方面,都为我们后来人提供了一个良好的学习和研究环境。做为一个Linuxer,感谢各位前辈们为我们提供一个自由的空间,让我们也能够在学习的同时去研究Linux。本文主要通过裁剪现有Linux系统,打造一个属于自己的Linux小系统,让其能够装载网卡驱动,并配置IP地址,实现网络功能。二、原理启动流程介绍制作linux小系统之前,我们有必要再了解一下linux的启动流程:1、首先linux要通过POST自检,检查硬件设备有没有故障2、如果有多块启动盘的话,需要在BIOS中选择启动磁盘3、启动MBR中的bootloader引导程序4、加载内核文件5、执行所有进程的父进程、老祖宗init6、打印欢迎界面在linux的启动流程中,加载内核文件时还需要借助别外两个文件:1)initrd,是CentOS5上用内存模拟的磁盘设备2)initramfs,是CentOS6上用内存模拟的文件系统在启程的流程中,init主要是用来做哪些操作的呢?init通过调用/etc/inittab这个配置文件,然后再去执行/etc/rc.d/rc.sysinit的系统初始化脚本启发到linux打印欢迎界面后,就说明系统已经启动成功,如果我们要制作一个linux小系统,我们只需要把它在开机流程中用到的各文件都装载到一起,就可以点亮属于我们自己的系统了,而linux是一个模块化的操作系统,好多功能组件都是通过模块化的工具来实现的,而且支持动态装载和卸载,我们要是想实现某种功能,只需加载相应的模块即可,就可以实现我们的linux操作系统大瘦身了。三、操作步骤1、目标磁盘分区在宿主机上挂一块新磁盘,命名为soft-linux,此块磁盘是宿主机上的第二块磁盘,所以这里是/dev/sdb,而到时候挂载到目标主机的时候,因为那里只有这一块磁盘,所以在目标主机上的名称应该是/dev/sda,这个不能搞混了。首先,我们要在目标磁盘上分两个区,并进行格式化。第一个分区500M,用来装引导程序;第二个分区10G,用来装根文件系统。然后再进行挂载操作,将/dev/sdb1挂载到/mnt/boot下,将/dev/sdb2挂载到/mnt/sysroot下。[root@changsheng ~]# mount /dev/sdb1 /mnt/bootmount: mount point /mnt/boot does not exist[root@changsheng ~]# mkdir -p /mnt/boot /mnt/sysroot[root@changsheng ~]# mount /dev/sdb1 /mnt/boot[root@changsheng ~]# mount /dev/sdb2 /mnt/sysroot/[root@changsheng ~]#2、安装grub至目标磁盘一个系统能启动,就需要引导,所以我们首先要安装一个grub引导程序到我们的新磁盘上,安装grub引导程序主要有两个命令,一个是grub-install,另一个是setup,这里最好使用grub-install来安装。因为:①grub-install会安装grub引导第二阶段的文件②setup不会安装第二阶段的引导程序,是安装引导信息到MBR第二个需要注意的地方就是--root-directory=后面接的路径应该是boot目录所在的地方,而不是/mnt/boot,因为boot目录在mnt下;目标磁盘是/dev/sdb[root@changsheng ~]# grub-install --root-directory=/mnt /dev/sdbProbing devices to guess BIOS drives. This may take a long time.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[root@changsheng ~]# cd /mnt/boot/[root@changsheng boot]# lsgrub  lost+found[root@changsheng boot]# cd grub/[root@changsheng grub]# lsdevice.map  e2fs_stage1_5  fat_stage1_5  ffs_stage1_5  iso9660_stage1_5  jfs_stage1_5  minix_stage1_5  reiserfs_stage1_5  stage1  stage2  ufs2_stage1_5  vstafs_stage1_5  xfs_stage1_5[root@changsheng grub]#安装完grub后,进入grub目录,会发现没有grub.conf配置文件,这样就导致我们的引导程序是不健全的,所以我们需要手动写一个配置文件在里边,不过这得需要知道内核的版本,等移植完内核版本,再回过头来补充此步。3、复制内核文件和initrd文件init是系统中用来产生其它所有进程的程序。它以守护进程的方式存在,其进程号为1,init是所有进程的父进程,老祖宗,所以不移植是不行的。它通过调用/etc/inittab这个配置文件,然后再去执行/etc/rc.d/rc.sysinit的系统初始化脚本。将内核文件和initrd文件复制到/dev/sdb下的boot目录中。[root@changsheng grub]# cp /boot/vmlinuz-2.6.32-358.el6.x86_64 /mnt/boot/vmlinuz-soft[root@changsheng grub]# cp /boot/initramfs-2.6.32-358.el6.x86_64.img /mnt/boot/initramfs-soft.img[root@changsheng grub]#4、创建目标主机根文件系统①使用命令行展开创建文件系统[root@changsheng sysroot]# mkdir -pv /mnt/sysroot/{etc/rc.d,usr,var,proc,sys,dev,lib,lib64,bin,sbin,boot,srv,mnt,media,home,root}mkdir: created directory `/mnt/sysroot/etc"mkdir: created directory `/mnt/sysroot/etc/rc.d"mkdir: created directory `/mnt/sysroot/usr"mkdir: created directory `/mnt/sysroot/var"mkdir: created directory `/mnt/sysroot/proc"mkdir: created directory `/mnt/sysroot/sys"mkdir: created directory `/mnt/sysroot/dev"mkdir: created directory `/mnt/sysroot/lib"mkdir: created directory `/mnt/sysroot/lib64"mkdir: created directory `/mnt/sysroot/bin"mkdir: created directory `/mnt/sysroot/sbin"mkdir: created directory `/mnt/sysroot/boot"mkdir: created directory `/mnt/sysroot/srv"mkdir: created directory `/mnt/sysroot/mnt"mkdir: created directory `/mnt/sysroot/media"mkdir: created directory `/mnt/sysroot/home"mkdir: created directory `/mnt/sysroot/root"[root@changsheng sysroot]# lsbin  boot  dev  etc  home  lib  lib64  lost+found  media  mnt  proc  root  sbin  srv  sys  usr  var[root@changsheng sysroot]#②移植bash命令和其库文件到根文件系统[root@changsheng mnt]# sh ~/scripts/cporder.shEnter a command: bashEnter a command: shutdownEnter a command: rebootEnter a command: vimEnter a command: touchEnter a command: mkdirEnter a command: rmEnter a command: lsEnter a command: catEnter a command: lessEnter a command: ifconfigEnter a command: ipEnter a command: routeEnter a command: quitquit[root@changsheng mnt]# sync[root@changsheng mnt]# sync[root@changsheng mnt]# lsboot  sysroot[root@changsheng mnt]# cd sysroot/[root@changsheng sysroot]# lsbin  lib64  sbin  usr[root@changsheng sysroot]# cd bin/[root@changsheng bin]# lsbash  cat  ls  mkdir  rm  touch[root@changsheng bin]# ln -sv bash sh`sh" -> `bash"[root@changsheng bin]# sync[root@changsheng bin]#附:命令移植脚本#!/bin/bash#target=/mnt/sysrootclearCmd() {if which $cmd &> /dev/null; thencmdPath=`which --skip-alias $cmd`elseecho "No such command"return 5fi}cmdCopy() {cmdDir=`dirname $1`[ -d ${target}${cmdDir} ] || mkdir -p ${target}${cmdDir}[ -f ${target}${1} ] || cp $1 ${target}${cmdDir}}libCopy() {for lib in `ldd $1 | grep -o "/[^[:space:]]{1,}"`; dolibDir=`dirname $lib`[ -d ${target}${libDir} ] || mkdir -p ${target}${libDir}[ -f ${target}${lib} ]|| cp $lib ${target}${libDir}done}while true; doread -p "Enter a command: " cmdif [ "$cmd" == "quit" ] ;thenecho "quit"exit 0ficlearCmd $cmd[ $? -eq 5 ] && continuecmdCopy $cmdPathlibCopy $cmdPathdone5、为grub提供配置文件上面移植了内核和initrd文件,我们就可以根据内核版本和initrd版本来编写grub.conf配置文件了[root@changsheng grub]# vim grub.confdefault=0timeout=5title  changsheng  soft-linuxroot (hd0,0)kernel /vmlinuz-soft ro root=/dev/sda2 quiet selinux=0 init=/bin/bashinitrd /initramfs-soft.img~quiet是静默安装,不再显示安装时的一大堆信息。后面要把selinux关掉,而且init要使用/bin/bash,告诉内核不要再去找init程序了。如果不指定这一步,在启动过程中就会报kernel panic(内核恐慌),以为系统就它一个了,没有init进程,恐慌的不行。6、启动测试7、特别提醒如果在vmvare上做此实验,在新建虚拟机创建新磁盘的时候,一定要选"Store virtual disk as a single file",否则,也会出现内核恐慌kennel panic。更多详情见请继续阅读下一页的精彩内容: http://www.linuxidc.com/Linux/2015-02/112825p2.htm
  • 1
  • 2
  • 下一页
Linux下解压 tar 文件到不同的目录中Linux强大命令 Awk 20分钟入门介绍相关资讯      定制Linux  定制Linux系统 
  • Linux入门之定制Linux系统---  (04/04/2014 13:23:19)
  • bash自动化 定制Linux属于自己的  (03/30/2014 08:31:38)
本文评论 查看全部评论 (0)
表情: 姓名: 字数