自制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.www58058.com/48369

(0)
zhengyibozhengyibo
上一篇 2016-09-21
下一篇 2016-09-21

相關推薦

  • btrfs文件系統介紹

    btrfs文件系統介紹 簡介        Btrfs(B-tree,ButterFS ,Better FS通常念成Butter FS),由Oracle于2007年宣布并進行中的COW(copy-on-write式)文件系統。繼ext3/4文件系統之后linux又有了比較強大的文件系統(btrfs文件系統),btrfs…

    系統運維 2016-04-27
  • 一次簡單的內核編譯(一)

    一、編譯環境   1、準備一臺測試機,放置兩塊硬盤   2、安裝"Development Tools"和"Server Platform Development"編譯環境   3、內核下載地址:https://www.kernel.org(本人使用的是3.10版本)   4、下載…

    Linux干貨 2015-06-01
  • 運維面試題和答案

    1、簡述TCP三次握手四次揮手過程及各過程中客戶端和服務器端的狀態。 #三次握手 客戶端向服務器端發送SYN包,客戶端進入SYN_SEND狀態 服務器端收到客戶端發送的包返回ACK+SYN包,服務器端進入SYN_RECV狀態 客戶端收到服務器端返回的包再發回ACK包,客戶端進入ESTABLISHED狀態,服務器端收到包也進入ESTABLISHED狀態 客戶端…

    Linux干貨 2016-06-10
  • 一起學DHCP系列(四)安裝、配置

    原創作品,允許轉載,轉載時請務必以超鏈接形式標明文章 原始出處 、作者信息和本聲明。否則將追究法律責任。http://jeffyyko.blog.51cto.com/28563/162580       本節將主要講述DHCP的安裝和配置。     …

    Linux干貨 2015-03-25
  • 再不自動化就晚啦!優云教你4步打造基于CentOS的產品鏡像

    隨著Linux程序的增多,軟件的安裝過程中經常出現如下問題: 1、硬件配置類似或者相同時,批量安裝系統和軟件,希望實現自動化安裝,減少安裝時間和人為出錯。 2、工程實施人員在不同客戶現場進行系統和軟件安裝(硬件配置不同),由于硬件有差別,不容易實現自動化。 筆者針對以上場景,從提高生產效率,減少誤操作的立場,提出如下解決方案,希望能達到拋磚引玉的目的,并能與…

    系統運維 2016-07-16
  • 常用磁盤陣列說明

    一.什么是磁盤陣列     磁盤陣列英文全名為RedundantArrays of Inexpensive Disks(RAID),即容錯廉價磁盤陣列。     RAID可以將一些容量較小的磁盤通技術手段組成一個容量較大的磁盤設備,而且不只是容量上的提升,RAID還可以提供數據?!?/p>

    Linux干貨 2015-04-02
欧美性久久久久