发布于 2017-10-17 23:05:30 | 130 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的Linux常用命令,程序狗速度看过来!

Linux

Linux是一套免费使用和自由传播的类Unix操作系统,是一个基于POSIX和UNIX的多用户、多任务、支持多线程和多CPU的操作系统。它能运行主要的UNIX工具软件、应用程序和网络协议。


这篇文章主要介绍了linux如何编译安装新内核支持NTFS文件系统(以redhat7.2x64为例),具有一定的参考价值,感兴趣的小伙伴们可以参考一下。

内核,是一个操作系统的核心。它负责管理系统的进程、内存、设备驱动程序、文件和网络系统,决定着系统的性能和稳定性。Linux作为一个自由软件,在广大爱好者的支持下,内核版本不断更新。新的内核修订了旧内核的bug,并增加了许多新的特性。如果用户想要使用这些新特性,或想根据自己的系统度身定制一个更高效,更稳定的内核,就需要重新编译内核。

本文将以kernel 4.7.2版本为实验,操作平台为RedHat 7.2,将通过以下三个方面来说明内核及模块的编译。

源码编译Linux内核

使用Linux内核模块

实战:编译一个NTFS内核模块,实现Linux挂载NTFS文件系统并实现读写功能

一、 源码编译linux内核准备工作:

1. redhat7或者以上版本,本文以vm12+redhat7.2为例。

2. 内核版本下载地址:到官网:https://cdn.kernel.org

查看最新稳定版内核:https://cdn.kernel.org/pub/linux/kernel/v4.x/linux-4.7.2.tar.xz

虚拟机硬件的要求:

硬盘可用空间大于8G.否则编译时,会因为空间不够,提示你安装不成功。

虚拟机内存要调到2.5G以上.最好是4G以上,这里是8G。

第一步: 对硬件进行设置,使其满足要求并下载内核:

1. 新添加一块20G的硬盘及修改内存:

2.检查当前的内核版本: uname -r

3.到官网:https://cdn.kernel.org 查看最新稳定版内核并下载

如果虚拟机不能上网(如何让虚拟机上网,参考本人相关博文),那也没有关系,直接从外网下载好后,用xshell工具上传至虚拟机。如图:

在xshell的终端输入rz,打开下面的上传界面:

上传即可。

或者点击下面按钮也可以:

如果虚拟机可以联网:不妨从虚拟机直接下载。

[root@xiaolyu ~]# wget https://cdn.kernel.org/pub/linux/kernel/v4.x/linux-4.7.2.tar.xz

第二步:使用硬盘:分区、格式化、挂载:

[root@xiaolyu ~]# fdisk /dev/sdb //对磁盘/dev/sdb进行格式化。
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0x63b985bb.

Command (m for help): m //查看帮助信息。
Command action
a toggle a bootable flag
b edit bsd disklabel
c toggle the dos compatibility flag
d delete a partition
g create a new empty GPT partition table
G create an IRIX (SGI) partition table
l list known partition types
m print this menu
n add a new partition
o create a new empty DOS partition table
p print the partition table
q quit without saving changes
s create a new empty Sun disklabel
t change a partition's system id
u change display/entry units
v verify the partition table
w write table to disk and exit
x extra functionality (experts only)

Command (m for help): n
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p): p
Partition number (1-4, default 1):
First sector (2048-41943039, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-41943039, default 41943039):
Using default value 41943039
Partition 1 of type Linux and of size 20 GiB is set

Command (m for help): p

Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x63b985bb

Device Boot Start End Blocks Id System
/dev/sdb1 2048 41943039 20970496 83 Linux

Command (m for help):
Command (m for help): w

对磁盘进行格式化: mkfs -t xfs /dev/sdb1

[root@xiaolyu ~]# ls /dev/sdb1
/dev/sdb1
[root@xiaolyu ~]# mkfs -t xfs /dev/sdb1
meta-data=/dev/sdb1 isize=256 agcount=4, agsize=1310656 blks
= sectsz=512 attr=2, projid32bit=1
= crc=0 finobt=0
data = bsize=4096 blocks=5242624, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=0
log =internal log bsize=4096 blocks=2560, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
[root@xiaolyu ~]#

创建挂载点并进行挂载:

[root@xiaolyu ~]# mkdir /sdb1 //创建挂载点。
[root@xiaolyu ~]# mount /dev/sdb1 /sdb1 //挂载硬盘。
[root@xiaolyu ~]# df -h | tail -1 //验证是否挂载成功。
/dev/sdb1 20G 33M 20G 1% /sdb1
[root@xiaolyu ~]#

第三步、编译、安装linux新内核及模块。

1.将源码包移动到/sdb1中。

2.检查系统是否安装make、gcc、gcc-c++ 、ncurses-devel和库工具等等

使用rpm -qa 检测上述工具及库是否存在。

[root@xiaolyu ~]# rpm -qa | grep make
automake-1.13.4-3.el7.noarch
make-3.82-21.el7.x86_64
[root@xiaolyu ~]# rpm -qa | grep gcc
gcc-4.8.5-4.el7.x86_64
gcc-gfortran-4.8.5-4.el7.x86_64
libgcc-4.8.5-4.el7.x86_64
gcc-c++-4.8.5-4.el7.x86_64
[root@xiaolyu ~]# rpm -qa |grep gcc-c++
gcc-c++-4.8.5-4.el7.x86_64

[root@xiaolyu ~]# rpm -qa | grep ncurses-devel
[root@xiaolyu ~]#yum -y install ncurses-devel #yum 安装 ncurses-devel动态库。

Loaded plugins: langpacks, product-id, search-disabled-repos, subscription-manager
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
rhel7-yum | 4.1 kB 00:00:00
Resolving Dependencies
--> Running transaction check
---> Package ncurses-devel.x86_64 0:5.9-13.20130511.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

=================================================

Package Arch Version Repository Size
=================================================

Installing:
ncurses-devel x86_64 5.9-13.20130511.el7 rhel7-yum 713 k

Transaction Summary
Total download size: 713 k
Installed size: 2.1 M
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : ncurses-devel-5.9-13.20130511.el7.x86_64 1/1
Verifying : ncurses-devel-5.9-13.20130511.el7.x86_64 1/1

Installed:
ncurses-devel.x86_64 0:5.9-13.20130511.el7

Complete!
[root@xiaolyu ~]#

3.解压内核源码包

xz -d 解压 .xz的压缩包

tar xf 解压.tar的压缩包

[root@xiaolyu sdb1]# ls
linux-4.7.2.tar.xz
[root@xiaolyu sdb1]# xz -d linux-4.7.2.tar.xz
[root@xiaolyu sdb1]# ls
linux-4.7.2.tar
[root@xiaolyu sdb1]# tar xf linux-4.7.2.tar
[root@xiaolyu sdb1]# ls
linux-4.7.2 linux-4.7.2.tar
[root@xiaolyu sdb1]#

[root@xiaolyu sdb1]# ls

linux-4.7.2 linux-4.7.2.tar
[root@xiaolyu sdb1]# cd linux-4.7.2
[root@xiaolyu linux-4.7.2]# ls
arch CREDITS firmware ipc lib net scripts usr
block crypto fs Kbuild MAINTAINERS README security virt
certs Documentation include Kconfig Makefile REPORTING-BUGS sound
COPYING drivers init kernel mm samples tools
[root@xiaolyu linux-4.7.2]# more README

#说明:这个地方可以查看README文件,每个源码包都有,里面给出了详细的安装编译配置信息。

4. 清理系统缓存。

尽可能给内核编译留出最大的内存空间。

查看系统缓存 free -m :

[root@xiaolyu linux-4.7.2]# free -m
total used free shared buff/cache available
Mem: 7969 611 5341 10 2015 7040
Swap: 2047 0 2047
[root@xiaolyu linux-4.7.2]#

查看默认缓存设置:cat /proc/sys/vm/drop_caches

[root@xiaolyu linux-4.7.2]# free -m
total used free shared buff/cache available
Mem: 7969 611 5341 10 2015 7040
Swap: 2047 0 2047
[root@xiaolyu linux-4.7.2]# cat /proc/sys/vm/drop_caches
0
[root@xiaolyu linux-4.7.2]# sync
[root@xiaolyu linux-4.7.2]# echo 3 > /proc/sys/vm/drop_caches #buff和cache都清空
[root@xiaolyu linux-4.7.2]# free -m
total used free shared buff/cache available
Mem: 7969 571 7214 10 183 7194
Swap: 2047 0 2047
[root@xiaolyu linux-4.7.2]#

#说明:/proc/sys/vm/drop_cashes的值有三个:

  0:buff/cache都不要清理。

  1:只清理buff。

  2:只清理cache。

  3:buff和cache都清理。

说明:重启(reboot和init 6)一样能清空缓存.

5. 通过图形界面配置内核编译参数,生成内核参数配置文件。

make menuconfig 生成内核参数配置文件。

[root@xiaolyu linux-4.7.2]# make menuconfig
HOSTCC scripts/basic/fixdep
HOSTCC scripts/kconfig/mconf.o
SHIPPED scripts/kconfig/zconf.tab.c
SHIPPED scripts/kconfig/zconf.lex.c
SHIPPED scripts/kconfig/zconf.hash.c
HOSTCC scripts/kconfig/zconf.tab.o
HOSTCC scripts/kconfig/lxdialog/checklist.o
HOSTCC scripts/kconfig/lxdialog/util.o
HOSTCC scripts/kconfig/lxdialog/inputbox.o
HOSTCC scripts/kconfig/lxdialog/textbox.o
HOSTCC scripts/kconfig/lxdialog/yesno.o
HOSTCC scripts/kconfig/lxdialog/menubox.o
HOSTLD scripts/kconfig/mconf
scripts/kconfig/mconf Kconfig
#
# using defaults found in /boot/config-3.10.0-327.el7.x86_64
#
Your display is too small to run Menuconfig!
It must be at least 19 lines by 80 columns.
make[1]: *** [menuconfig] Error 1
make: *** [menuconfig] Error 2

说明:直接在虚拟机的终端执行 make menuconfig出现上述错误,屏幕太小了,没法运行Menuconfig ,于是果断在xshell下执行上述命令:

经过反复研究,我将字体缩小的时候,当字体为13的时候,在终端执行上述命令,是不会出现因为显示不下而报错的。下面是截图。

生成.config配置文件,查看此配置文件:

[root@xiaolyu linux-4.7.2]# vim .config


选择“File system” 然后按回车

由上图可以看出,新内核支持多种文件系统。

按【空格键】,进入下图:

用原内核的配置文件,覆盖新内核的配置文件。这里说明一下:为什么要用原内核覆盖新内核,因为内核的配置,比较复杂,可以参考:http://blog.csdn.net/star_xiong/article/details/17357821

http://blog.csdn.net/xuyuefei1988/article/details/8635539

            http://www.linuxidc.com/Linux/2012-06/63092.htm

新旧内核的差别在于ntfs文件系统的支持,所以用老的来覆盖一下。

如果出现是否覆盖 n不覆盖 y 覆盖,这里选y覆盖。

[root@xiaolyu linux-4.7.2]# cp /boot/config-3.10.0-327.el7.x86_64 /sdb1/linux-4.7.2/.config
cp: overwrite ‘/sdb1/linux-4.7.2/.config'? y
[root@xiaolyu linux-4.7.2]#

比较原内核的配置文件和备份的新生成的配置文件的差异:

[root@xiaolyu linux-4.7.2]# diff .config .config_bak
3c3
< # Linux/x86_64 3.10.0-327.el7.x86_64 Kernel Configuration
---
> # Linux/x86 4.7.2 Kernel Configuration
13d12
< CONFIG_HAVE_LATENCYTOP_SUPPORT=y
14a14,17
> CONFIG_ARCH_MMAP_RND_BITS_MIN=28

因为差异实在太大了,想了解具体的差异的朋友,可以看我另一篇博文:

<linux内核更新前后配置文件的比较>http://www.cnblogs.com/jasmine-Jobs/p/5808949.html

差异还是蛮大的。因为太长了,这里仅仅给出一个局部的截图:

这个地方为了快速完成新内核的安装,采用了修改原配置文件的方法。

修改配置文件,使其支持ntfs读写。

[root@xiaolyu linux-4.7.2]# vim .config

5、编译内核

先检查openssl-devel 这个包安装没有 ,如果没有,提前安装这个包openssl-devel

rpm -qa | grep openssl-devel

yum -y install openssl-devel

[root@xiaolyu linux-4.7.2]# make bzImage //生成内核。这个过程非常非常的慢。

中间多次问你y/n,全部选y,就可以了。这个过程比较慢。

说明,上述的问题,我已经完全解决了,问题出在,我是先执行make menuconfig ,然后cp /boot/config-3.10.0-327.el7.x86_64 /sdb1/linux-4.7.2/.config

这样的结果使得,新生成的内核被完全覆盖掉,毫无用处,4.7.2的内核比3.1的内核多的东西都没有做任何配置。

正确的做法是:先执行cp /boot/config-3.10.0-327.el7.x86_64 /sdb1/linux-4.4/.config 然后再make menuconfig 。

如下图:

说明在编译内核: make bzImage 之前,要先安装一下这个包:openssl-devel,即:

yum -y install openssl-devel

否则会报如下错误:

即:

重新: make bzImage:

这里也是需要一段时间

出现此界面OK!

6、下面生成新内核的驱动模块:

[root@xiaolyu linux-4.7.2]# make modules -j 4
CHK include/config/kernel.release
CHK include/generated/uapi/linux/version.h
CHK include/generated/utsrelease.h
CHK include/generated/timeconst.h
CHK include/generated/bounds.h
CHK include/generated/asm-offsets.h
CALL scripts/checksyscalls.sh
CC [M] arch/x86/crypto/glue_helper.o

因为这个模块编译的过程非常漫长,所以当编译完成的时候,要echo $? 判断一下是否成功:

安装模块:make modules install

[root@xiaolyu linux-4.7.2]# make modules_install

出现下面的界面说明模块安装成功:

2)安装新编译的系统内核 :make install

[root@xiaolyu linux-4.7.2]# make install

重新启动系统,测试新内核的工作情况

注意,在启动的时候,需要自己进来一下选择,否则默认还是以前的内核启动哦。除非你在上一步把默认启动项给改了。

如果你将默认启动项给修改为4.7.2,那么会变成如下界面:

使用新内核启动系统后,查看内核版本:

[root@xiaolyuDesktop ~]# unmae -r

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持PHPERZ。



最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务