自制Linux系统

                      自制Linux系统

1、环境准备:

        centos6上添加一块新硬盘,并分区格式化。

        虚拟机不关机情况下添加新硬盘,需执行以下命令,让系统识别新硬盘。

        [root@centos6 Desktop]# echo "- – -" > /sys/class/scsi_host/host2/scan

        使用fdisk分区工具,创建两个必要的分区

        使用mkfs格式化工具,格式化成ext4格式的文件系统。

 

 2、开始对/dev/sdc硬盘分区并格式化:

      1)使用fdisk分区工具,创建两个必要的分区:

          /dev/sdc1 :对应/boot分区;

          /dev/sdc2 :对应根(rootfs)分区;

      2)使用mkfs格式化工具,格式化成ext4格式的文件系统。

          [root@centos6 Desktop]# fdisk /dev/sdc

             Device Boot      Start         End      Blocks   Id  System

          /dev/sdc1               1          65      522081   83  Linux

          /dev/sdc2              66        2610    20442712+  83  Linux

 

      3)使用lsbik命令查看/dev/sdc磁盘分区

          [root@centos6 Desktop]# lsblk  /dev/sdc

          NAME   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT

          sdc      8:32   0    20G  0 disk

         ├─sdc1   8:33   0 509.9M  0 part

         └─sdc2   8:34   0  19.5G  0 part

          [root@centos6 Desktop]#

 

      4)格式化/dev/sdc1分区和/dev/sdc2分区:

          [root@centos6 Desktop]# mkfs.ext4 /dev/sdc1

          [root@centos6 Desktop]# mkfs.ext4 /dev/sdc2

 

 3、创建/boot/sysroot系统根目录挂载点:

        [root@centos6 Desktop]# mkdir /mnt/boot

        [root@centos6 Desktop]# mkdir /mnt/sysroot

        [root@centos6 Desktop]#

 

 4、挂载/dev/sdc1/分区和/dev/sdc2分区到指定的目录上:

       [root@centos6 Desktop]# mount /dev/sdc1 /mnt/boot/

       [root@centos6 Desktop]# mount /dev/sdc2 /mnt/sysroot/

 

 5、复制内核(kernel)文件和initramfs文件到/mnt/boot目录(也可以从光盘拷贝):

       [root@centos6 Desktop]# cp -rf /boot/vmlinuz-2.6.32-642.el6.x86_64 /mnt/boot

       [root@centos6 Desktop]# cp -rf /boot/initramfs-2.6.32-642.el6.x86_64.img /mnt/boot

       [root@centos6 Desktop]#

 

 6、安装/mnt/boot目录下的grub目录及grub目录下的文件:

       注意:执行此操作时必须指定/boot目录的父目录作为根目录。

             因为此项操作实际上是写数据到/dev/sdc1分区中的,相当于就是/boot目录。

       [root@centos6 Desktop]# grub-install –root-directory=/mnt  /dev/sdc

       Probing 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

       (hd2) /dev/sdc

       [root@centos6 Desktop]#

 

 7、建立/mnt/boot/grub/grub.conf配置文件:

       1 default=0

       2 timeout=5

       3 title=centos6.8 small Linux

       4 kernel  /vmlinuz-2.6.32-642.el6.x86_64  ro root=/dev/sda2 selinux=0 init=/bin/bash

       5 initrd     /initramfs-2.6.32-642.el6.x86_64.img

 

 8、创建/mnt/sysroot目录下small Linux 根目录下的目录:

       [root@centos6 Desktop]# mkdir -pv /mnt/sysroot/

       {etc,lib,lib64,bin,sbin,tmp,var,usr,sys,proc,opt,home,root,boot,dev,mnt,media}

 

    查看/mnt/sysroot目录下创建的目录:

       [root@centos6 Desktop]# tree /mnt/sysroot/

       /mnt/sysroot/

      ├── bin

      ├── boot

      ├── dev

      ├── etc

      ├── home

      ├── lib

      ├── lib64

      ├── lost+found

      ├── media

      ├── mnt

      ├── opt

      ├── proc

      ├── root

      ├── sbin

      ├── sys

      ├── tmp

      ├── usr

      └── var

      18 directories, 0 files

      [root@centos6 Desktop]#

 

 9、创建/mnt/sysroot/etc/fstab文件:

      [root@centos6 Desktop]# vim /mnt/sysroot/etc/fstab

      1 /dev/sda1   /boot   ext4  defaults 1 1

      2 /dev/sda2   /       ext4  defaults 1 2

     x  (保存退出)

 

 10、复制bash和相关库文件;复制相关命令及相关库文件:

      由于复制相关命令和库文件比较繁琐,所以我们使用脚本进行复制;

      查看脚本文件:

       [root@centos6 Desktop]# vim /testdir/copycmd.sh

       1 #!/bin/bash

       2

       3 ch_root="/mnt/sysroot"

       4 [ ! -d $ch_root ] && mkdir $ch_root

       5

       6 bincopy() {

       7     if which $1 &>/dev/null; then

       8     

       9         local cmd_path=`which –skip-alias $1`

      10         local bin_dir=`dirname $cmd_path`

      11         [ -d ${ch_root}${bin_dir} ] || mkdir -p ${ch_root}${bin_dir}

      12         [ -f ${ch_root}${cmd_path} ] || cp $cmd_path ${ch_root}${bin_dir}

      13         return 0

      14     else

      15         echo "Command not found."

      16         return 1

      17     fi  

      18 }

      19  

      20 libcopy() {

      21     local lib_list=$(ldd `which –skip-alias $1` | grep -Eo '/[^[:space:]]+')

      22     for loop in $lib_list;do

      23         local lib_dir=`dirname $loop`

      24         [ -d ${ch_root}${lib_dir} ] || mkdir -p  ${ch_root}${lib_dir}

      25         [ -f ${ch_root}${loop} ] || cp $loop ${ch_root}${lib_dir}

      26     done

      27 }

      28  

      29 read -p "Please input a command: " command

      30

      31 while [ "$command" != "quit" ];do

      32     if bincopy $command ;then

      33         libcopy $command

      34     fi

      35     read -p "Please input a command or quit: " command

      36 done

 

  拷贝命令及相关库文件:

     [root@centos6 Desktop]# source /testdir/copycmd.sh

     Please input a command: bash

     Please input a command or quit: vim

     Please input a command or quit: reboot

     Please input a command or quit: poweroff

     Please input a command or quit: cd

     Please input a command or quit: ifconfig

     Please input a command or quit: cat

     Please input a command or quit: ls

     Please input a command or quit: hostname

     Please input a command or quit: init

     Please input a command or quit: passwd

     Please input a command or quit: tree

     Please input a command or quit: lsblk

     Please input a command or quit: blkid

     Please input a command or quit: shutdown

     Please input a command or quit: quit

     [root@centos6 Desktop]#

 

 至此精简版的smalllinux已经制作完成,可以将/dev/sdc磁盘移至其它虚拟机中进行测试。

 

 

 

 

 

 

        

 

   

原创文章,作者:zhengyibo,如若转载,请注明出处:http://www.178linux.com/48369

(0)
上一篇 2016-09-21 21:18
下一篇 2016-09-21 21:24

相关推荐

  • Linux系统启动流程与内管管理(上)

    在讲linux系统启动流程之前,来讲讲linux的组成,这样能能帮助我们深入了解系统的启动流程,废话不多说直接上系统启动流程图 linux组成 linux:kernel+rootfs kenrel的作用:进程管理、内存管理、网络管理、驱动程序、文件系统、安全管理等   rootfs:程序和glibc 库:函数结合,function,调用接口(头文件…

    系统运维 2016-09-14
  • 为你的网络传输加把锁(OpenSSL)

    互联网形成的初期,更多关注的是设备之间通过网络相互访问功能的实现,我们都知道,两个设备之间要想相互通信,就必需遵循某种协议,于是早期的互联网也出现来众多的协议,比如传输层最著名的协议就是tcp和udp,而应用层比较著名的协议有:http、ftp、pop、smtp、dns、telnet等等,而这些协议这开发初期,更多是关注功能的实现,数据这网络上传输是明文方式…

    Linux干货 2015-12-06
  • linux下逻辑卷管理LVM

    LVM:逻辑卷管理 LVM这个技术就是把底层的存储设备组成一个卷组,底层存储设备的存储空间会变成一个个PE(盘区,大小为2的n次方),这个卷组里就是一个个的PE,然后,这个卷组会重新分区,这些分区就为逻辑卷,这些逻辑卷都是由卷组里分配的PE组成。 关于LVM里的命令: pv的命令: pvcreate pvs pvdisplay pvscan pvremove…

    Linux干货 2015-08-04
  • 关于 进程和性能监控

            Linux系统状态的查看及管理工具:     pstree, ps, pidof, pgrep, top, htop, glance,pmap, vmstat, dstat, kill, pkill, job, bg, fg, nohup pstree命…

    系统运维 2016-09-11
  • corosync+pacemaker+drbd+mysql来实现mysql的高可用和数据的同步

    实现拓扑图: 实验流程: 先对两主机安装上corosync和pacemaker 两主机安装drbd服务,并且设置好drbd的组设备 选择drbd主节点上,进行数据库的初始化 进行资源的定义和配置 测试 实验前,我们还需要做一些准备工作: 1、时间必须保持同步   使用ntp服务器 2、节点必须名称互相通信    解析节点名称 &…

    Linux干货 2015-11-10
  • 一起学习吧:SDCC 2017即将在上海共话架构、数据和运维!

    2017活动家邀你一起学习吧!SDCC 2017在上海共话架构、数据和运维,还有两天!【召开时间为:3月17至3月19日】 在互联网大潮下,2017年的就业环境越发恶劣,技术人如何去适应技术变革和学习新技术,以及如何快速增强自身的技术实力成为亟需解决的问题。如果你在看完一本技术图书后还是疑惑不解,处于目前项目遇到困难时无人请教的尴尬境地,亦不清楚业界同行在使…

    2017-03-15