?{ 編譯內核;自制linux; }

編譯內核、自制linux

自制簡單的linux

前提約定 
CentOS 6.8 , Kernel-2.6.32-642.el6.x86_64 
基于GRUB – 0.97 
/ 分區與 boot 分區獨立, /boot 分區 100M+ ,/ 根分區看具體需求,此處為 1G 
Vmware 12.1,新建一個Linux虛擬機,硬盤選擇現有磁盤,即制作Linux的磁盤


VMware 添加一塊磁盤設備,并劃分一個多于100M的boot分區以及根分區,格式化為ext4分區

[root@cent6]~>fdisk /dev/sdc
Command (m for help): n
Partition number (1-4): 1
First cylinder (1-1697, default 1):
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-1697, default 1697): +100M

Command (m for help): n
Partition number (1-4): 2
First cylinder (15-1697, default 15):  
Using default value 15
Last cylinder, +cylinders or +size{K,M,G} (15-1697, default 1697): +1G
Command (m for help): w

[root@cent6]~>partx -a /dev/sdc

[root@cent6]~>lsblk
NAME   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sdc      8:32   0    13G  0 disk
├─sdc1   8:33   0 109.8M  0 part   #作為boot
└─sdc2   8:34   0     1G  0 part   #作為  /

[root@cent6]~>mkfs.ext4 /dev/sdc1
[root@cent6]~>mkfs.ext4 /dev/sdc2

掛載boot分區,生成grub引導,寫grub配置文件,拷貝vmlinuz與initramfs文件到boot分區下

[root@cent6]~>mkdir /mnt/{boot,root}
[root@cent6]~>mount /dev/sdc1 /mnt/boot/

[root@cent6]~>cp /boot/vmlinuz-2.6.32-642.el6.x86_64 /boot/initramfs-2.6.32-642.el6.x86_64.img /mnt/     #掛載點的上層目錄

[root@cent6]~>grub-install --root-directory=/mnt/boot/ /dev/sdc  #boot分區無boot目錄

[root@cent6]~>vim /mnt/boot/boot/grub/grub.conf
default=0
timeout=2
title diylinux
root (hd0,0)
kernel /vmlinuz-2.6.32-642.el6.x86_64 root=/dev/sda2 selinux=0 init=/bin/bash
initrd /initramfs-2.6.32-642.el6.x86_64.img    #設備名為sda,關閉selinux,init啟動bash作為第一個進程

默認情況下stage2(GRUB啟動菜單):由Grub通過讀取grub.conf,載入當前根(boot)下的內核鏡像和initrd鏡像到內存,之后 stage2 的引導加載程序釋放控制權,kernel階段就開始了

Kernenl是一個zImage kernel啟動后就可以訪問磁盤設備和 / 根文件系統,


掛載根分區,創建FHS所需的目錄結構,增加 fstab 文件

[root@cent6]~>mount /dev/sdc2 /mnt/root/

[root@cent6]~>mkdir -pv /mnt/root/{boot,bin,dev,etc,home,lib,lib64,mnt,opt,proc,root,sys,sbin,srv,tmp,usr,var}
#/proc是內核狀態和統計信息的輸出接口

[root@cent6]~>vim /mnt/root/etc/fstab
/dev/sda1       /boot   ext4    defaults        1       1
/dev/sda2       /       ext4    defaults        1       2

拷貝bash到等外部命令的二進制文件和庫文件到根分區,這里使用腳本完成

[root@cent6]~>bash copycmd.sh 

Please input a executable command:bash
Please input a executable command:ls
Please input a executable command:cat
Please input a executable command:vi
Please input a executable command:mount
Please input a executable command:halt
Please input a executable command:date
Please input a executable command:train

拷貝命令腳本copycmd.sh如下:

 1 #!/bin/bash
2 #
3 #Author:jasonmc
4 #Connect me:jasonmc@helloc.me
5 #Date:Nov 22 ETS 2016
6 #Description:
7 #
8 #
9 clear
10
11 dest_dir=/mnt/root
12
13 [ -d $dest_dir ]||mkdir $dest_dir
14
15 read -p "Please input a executable command:" cmd
16
17 until [[ "$cmd" = 'quit' || "$cmd" = 'q' ]]
18 do
19       cmd_path=`which $cmd 2> /dev/null`
20
21       if [ $? -eq 0 ]
22       then
23             if [ ! -e "$dest_dir$cmd_path" ]
24             then
25                     cp -p -u --parents $cmd_path  $dest_dir &&\
26                         ldd $cmd_path|grep -o '/.* '| \
27                         cp -p -u --parents `xargs` $dest_dir
28             else
29                     echo "the $dest_dir$cmd_path does exist."
30             fi
31       else
32             echo " Confirm the existence of the command you entered."
33       fi
34       read -p "Please input a executable command:" cmd
35 done

嘗試chroot到根分區

[root@cent6]~>chroot /mnt/root/
bash-4.1# ls
bin   dev  home  lib64       mnt  proc  sbin  sys  var
boot  etc  lib   lost+found  opt  root  srv   usr

新建VMware虛擬機,添加自制Linux所在的磁盤

自制linux 虛擬機配置.jpg

啟動虛擬機,進入GRUB菜單

grub.jpg

進入系統,看到 init 啟動的 bash

啟動.jpg

注意,如果 / 分區與 /boot分區不獨立時有兩個地方與上面不同 
1、 fstab 文件只有一條記錄 /dev/sda1 / ext4 default 1 2 
2、 grub.conf文件:(1)root (hd0,0) (2)kernel /boot/vmzlinux-xxxx (3)initrd /boot/initramfs-xxxx.img


編譯內核

前提條件: 
(1)準備好開發環境(開發工具,開發庫) 
(2)獲取目標主機的硬件設備信息 
(3)添加需求,獲取目標主機所需的功能,如啟用的文件系統 
(4)獲取合適版本的內核:www.kernel.org

當前內核版本和發行版信息

[root@cent7]~>uname -r
3.10.0-327.el7.x86_64

[root@cent7]~>cat /etc/centos-release
CentOS Linux release 7.2.1511 (Core)

安裝開發工具,下載內核源碼包

#安裝開發工具和服務器開發平臺
[root@centos7]~>yum groupinstall "Development tools" "Server Platform Development" -y
#確認安裝的包組中有 ncurses 軟件包
[root@centos7]~>yum list all *ncurses*

查看主機的硬件設備信息

#CPU信息
[root@centos7]~>cat /proc/cpuinfo

[root@centos7]~>lscpu
Architecture:
        x86_64
CPU op-mode(s):        32-bit, 64-bit
Byte
Order:            Little Endian
CPU(s):
               2
Model
name:            Intel(R) Xeon(R) CPU E5-2650L v3 @ 1.80GHz
Stepping:
             2
CPU
MHz:               1799.998
BogoMIPS:
             3599.99
Virtualization:
      VT-x
Hypervisor vendor:    KVM
Virtualization type:  full
L1d cache:             32K
L1i
cache:             32K
L2
cache:              256K
L3
cache:              30720K
NUMA
node0 CPU(s):     0,1


#PCI設備信息
[root@centos7]~>lspci [-v][-vv]

[root@centos7]~>lsusb

[root@centos7]~>hal-device

獲取內核源碼,解壓內核

[root@centos7]~>wget https://cdn.kernel.org/pub/linux/kernel/v4.x/linux-4.7.4.tar.xz   

#官方特別推薦放在 /usr/src 目錄下
[root@centos7]~>tar -xf linux-4.7.4.tar.xz -C /usr/src

#linux-xxx 目錄鏈接為 linux 目錄,方便將來需要編譯某個驅動時訪問 linux 目錄
[root@centos7]~>cd /usr/src/
[root@centos7]/usr/src/linux>ln -sv linux-4.7.4 linux
[root@centos7]/usr/src/linux>ls linux  #平臺、文檔、驅動等
arch     crypto         include  kernel       net          security
block    Documentation  init     lib          README          sound
certs    drivers        ipc      MAINTAINERS  REPORTING-BUGS  tools
COPYING  firmware       Kbuild   Makefile     samples         usr
CREDITS  fs             Kconfig  mm           scripts         virt

編譯內核并安裝

#獲取編譯幫助
[root@centos7]/usr/src/linux>make help   #Configuration targets幫助信息

#最易用的字符界面 Configure,需安裝 ncurses-devel,也可以使用圖形界面的工具
[root@centos7]~>yum -y install ncurses-devel

#拷貝配置文件的模板,在其他發行版中是 /proc/config.gz 文件
[root@centos7]~>cp /boot/config-3.10.0-327.22.2.el7.x86_64 /usr/src/linux/.config

#啟動字符界面的編譯配置
[root@centos7]/usr/src/linux>make menuconfig  選擇需要的模塊 * 打進內核、M編譯成模塊
#如果系統連續運行足夠長的時間,現在被加載的就是需要的,否則就不需要
#General setup -- Default hostname
#                 (1.el7)local version
#Processor type -- Processor family -- Xcon
#BUS options
#Networking -- ipv6、wireless #移除不要的模塊
#Device Drives
#File system -- ntfs
#Security #SELinux相關
#Cryptographic  #加密解密庫
#

make menu.jpg

[root@centos7]/usr/src/linux>screen       #遠程編譯時運行screen防止網絡中斷造成編譯未完成

[root@centos7]/usr/src/linux>make -j4   #開始編譯,使用 -j 指定線程數量
#在 arch/x86/boot/目錄下就是內核文件如bzImage

[root@centos7]/usr/src/linux>make modules_install   #安裝內核模塊
[root@centos7]~>ls /lib/modules/
3.10.0-327.28.3.el7.x86_64  4.7.4first.el7

[root@centos7]/usr/src/linux>make install   #安裝內核
#在/boot目錄下生成vmlinuz-xxx文件和initramfs-xxx.img文件等
#在grub2.cfg文件自動創建了一個內核啟動項,內核鏡像由gzip和cpio解壓
#編譯時間為25分鐘,時間取決于編譯模塊的數量

[root@centos7]/usr/src/linux>ll -h /boot/vmlinuz-4.7.4first.el7 /boot/initramfs-4.7.4first.el7.img
-rw------- 1 root root  30M Sep 18 13:15 /boot/initramfs-4.7.4first.el7.img
-rw-r--r-- 1 root root 5.4M Sep 18 13:14 /boot/vmlinuz-4.7.4first.el7

選擇新內核啟動

[root@centos7]~>grub2-reboot 1
[root@centos7]~>reboot

進入系統,查看ntfs模塊信息

[root@centos7]~>uname -a
Linux centos7 4.7.4first.el7 #1 SMP Sun Sep 12 13:06:46 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux

[root@centos7]~>modinfo ntfs
filename:       /lib/modules/4.7.4first.el7/kernel/fs/ntfs/ntfs.ko
license:        GPL
version:        2.1.32
description:    NTFS 1.2/3.x driver - Copyright (c) 2001-2014 Anton Altaparmakov and Tuxera Inc.
author:         Anton Altaparmakov <anton@tuxera.com>
alias:          fs-ntfs
srcversion:     895AC4F48E15A9818751453
depends:        
intree:         Y
vermagic:       4.7.4first.el7 SMP mod_unload modversions

將來需要編譯內核中的某一模塊 
(1)只編譯子目錄的相關代碼 
cd /usr/src/linux 
make path/dir/ 
(2)只編譯一特定的模塊 
cd /usr/src/linux 
make path/file.ko 
cp /lib/modules/3-xx/kernel/xxx/

清理內核編譯的文件 
make clean #清理編譯產生的文件,保留config文件和一些模塊文件 
make mrproper #清理編譯生成的所有文件 
make distclean #額外清理mrproper、patches以及編輯器備份文件

原創文章,作者:helloc,如若轉載,請注明出處:http://www.www58058.com/47625

(1)
hellochelloc
上一篇 2016-09-15
下一篇 2016-09-15

相關推薦

  • 文本處理工具三劍客之awk

    文本處理工具:grep,sed,awk awk:報告生成器,格式化文本輸出 AWK: Aho ,Weinberger,Kernighan gawk:GNU awk gawk – pattren  scanning  and  processing  language 基本語法:gawk  [opt…

    Linux干貨 2016-09-22
  • ?RAID使用手冊

    RAID使用手冊 RAID演示

    Linux干貨 2016-09-02
  • nginx配置文件中文文檔

    Nginx配置參數中文說明。 #定義Nginx運行的用戶和用戶組user www www; #nginx進程數,建議設置為等于CPU總核心數。worker_processes 8; #全局錯誤日志定義類型,[ debug | info | notice | warn | error | crit ]error_log /var/log/nginx/error…

    Linux干貨 2017-08-08
  • 海量數據處理算法—Bit-Map

    1. Bit Map算法簡介         來自于《編程珠璣》。所謂的Bit-map就是用一個bit位來標記某個元素對應的Value, 而Key即是該元素。由于采用了Bit為單位來存儲數據,因此在存儲空間方面,可以大大節省。 2、 Bit Map的基本思想       &nbs…

    Linux干貨 2015-11-10
  • 計算機和操作系統的一些概念

    一、計算機組成     (一) 硬件         CPU:運算器、控制器、寄存器、緩存器         存儲器:主內存,RAM(Random Access…

    Linux干貨 2016-08-15
  • 密碼保護:第二天

    無法提供摘要。這是一篇受保護的文章。

    Linux干貨 2017-07-15
欧美性久久久久