☞{ 编译内核;自制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.178linux.com/47625

(1)
上一篇 2016-09-15 18:10
下一篇 2016-09-15 22:20

相关推荐

  • 查找 -数据结构

    几种查找算法:顺序查找,折半查找,分块查找,散列表 一、顺序查找的基本思想:  从表的一端开始,向另一端逐个按给定值kx 与关键码进行比较,若找到,查找成功,并给出数据元素在表中的位置;若整个表检测完,仍未找到与kx 相同的关键码,则查找失败,给出失败信息。 说白了就是,从头到尾,一个一个地比,找着相同的就成功,找不到就失败。很明显的缺点就是查找效…

    Linux干货 2015-07-28
  • 马哥教育网络班22期+第三周课程练习

    1、列出当前系统上所有已经登录的用户的用户名,注意:同一个用户登录多次,则只显示一次即可。  who | tr -s [[:space:]] | cut -d" " -f1 | sort -u [root@localhost cdrom]# who root     pts/1     &…

    Linux干货 2016-09-07
  • 网络管理、进程管理、计划任务及启动流程

          网络管理 一.网卡模块操作步骤: 查询网卡模块类型:ethtool  -i  eth1 查询网卡模块lsmod |grep e1000 删除网卡模块rmmod e1000  或者modprobe  -r  e1000 添加网卡模块 modprobe e1000 删除模…

    Linux干货 2016-09-08
  • Linux cluster集群全讲解

                     Linux cluster集群 Linux cluster(集群): cluster:计算机组合,为解决某个特定问题组合起来形成的单个系统;   Linux Cluster类型:    LB:Load Balancing,负载均衡;    HA:High Availiablity,高可用;    A=MTBF(平均无故障时长…

    2016-11-18
  • 搭建SSH服务器

    Linux 远程登录服务:sshSSH是标准的网络协议,可用于大多数UNIX操作系统,能够实现字符界面的远程登录管理,它默认使用22号端口,采用密文的形式在网络中传输数据,相对于通过明文传输的Telnet,具有更高的安全性。SSH提供了口令和密钥两种用户验证方式,这两者都是通过密文传输数据的。不同的是,口令用户验证方式传输的是用户的账户名和密码,这要求输入的…

    Linux干货 2017-07-24
  • 第三周作业

    1、列出当前系统上所有已经登录的用户的用户名,注意:同一个用户登录多次,则只显示一次即可。         使用到的命令:w或who、cut、sort、uniq          &nbsp…

    Linux干货 2017-02-02