Linux從用戶層到內核層系列 - TCP/IP協議棧部分系列7: 基礎知識之 - Linux內核源碼目錄與內核編譯選項

題記:本系列文章的目的是拋開書本從源代碼和使用的角度分析Linux內核和相關源代碼,byhankswang和你一起玩轉linux開發


輕鬆搞定TCP/IP協議棧,原創文章歡迎交流, [email protected]微笑

歡迎加入到CHLK - Linux開發交流羣 QQ:327084515 討論Linux開發相關問題

 

Linux內核源碼目錄與內核編譯選項

1.Linux內核源碼目錄說明

從kernel.org上下載一份內核代碼,ls一下可以看到有很多文件和目錄:COPYING 、Documentation 、Kconfig 、 Makefile、REPORTING-BUGS、block 、drivers、 fs 、 init 、 kernel、mm  、 samples、security  、tools 、virt、CREDITS、Kbuild、MAINTAINERS、README、arch、crypto、firmware、include、ipc 、lib、net 、scripts、sound和usr。下面我們逐個分析各個目錄和文件的用處。

Arch

目錄包括了所有和體系結構相關的核心代碼。它下面的每一個子目錄都代表一種Linux支持的體系結構,例如X86就是Intel CPU及與之相兼容體系結構的子目錄。Arch目錄下的子目錄包括:/arch/kernel、/arch/mm、/arch/include、/arch/boot、/arch/configs。其中/arch/kernel目錄包含了與CPU相關的中斷和SMP等信息,/arch/boot包含了系統啓動的相關信息和make之後生成的啓動鏡像文件bzImage。

Copying

目錄下是GPL版權申明。對具有GPL版權的源代碼改動而形成的程序,或使用GPL工具產生的程序,具有使用GPL發表的義務,如公開源代碼。

Credits

目錄下是光榮榜。對Linux做出過很大貢獻的一些人的信息。

Documentation

目錄下是一些文檔,linux-3.0.4版本該目錄下有217個文件,文件00-INDEX對該目錄下的所有文件進行了簡要說明,其他文件分別對linux操作系統的各個部分進行了說明。IRQ.txt描述了什麼是中斷及中斷在系統中的枚舉定義和頭文件的位置;Changes描述了當前版本的系統對各個軟件版本的需求,linux 3.0.4要求make的版本爲3.8.0,grub的版本爲0.93。

Drivers

目錄中是系統中所有的設備驅動程序。它又進一步劃分成幾類設備驅動,每一種有對應的子目錄,如聲卡的驅動對應於drivers/sound; block 下爲塊設備驅動程序,比如ide(ide.c)。如果你希望查看所有可能包含文件系統的設備是如何初始化的,你可以看drivers/block/genhd.c中的device_setup()。它不僅初始化硬盤,也初始化,因爲安裝nfs文件系統的時候需要網絡其他: 如, Lib放置核心的庫代碼; Net,核心與網絡相關的代碼; Ipc,這個目錄包含核心的進程間通訊的代碼; Fs,所有的文件系統代碼和各種類型的文件操作代碼,它的每一個子目錄支持一個文件系統,例如fat和ext2。

Fs

目錄存放Linux支持的文件系統代碼和各種類型的文件操作代碼。每一個子目錄支持一個文件系統,包括我們常用的文件系統類型:Ext2、Ext3、Ext4、 Proc、Fat和Nfs。其中Ext3文件系統對應的就是/fs/ext3目錄。

Include

目錄包括編譯核心所需要的大部分頭文件,例如與平臺無關的頭文件在include/linux子目錄下,與 intel cpu相關的頭文件在include/asm-i386子目錄下,而include/scsi目錄則是有關scsi設備的頭文件目錄。

Init

目錄包含核心的初始化代碼(不是系統的引導代碼),有main.c和Version.c兩個文件。這是研究核心如何工作的好起點。

Ipc

目錄包含了核心進程間的通信代碼。

Kernel

內核管理的核心代碼,此目錄下的文件實現了大多數linux系統的內核函數,其中最重要的文件當屬sched.c;同時與處理器結構相關代碼都放在arch/*/kernel目錄下。

Lib

目錄包含了核心的庫代碼,不過與處理器結構相關的庫代碼被放在arch/*/lib/目錄下。

Maintainers

目錄存放了維護人員列表,對當前版本的內核各部分都有誰負責。

Makefile

目錄第一個Makefile文件。用來組織內核的各模塊,記錄了個模塊間的相互這間的聯繫和依託關係,編譯時使用;仔細閱讀各子目錄下的Makefile文件對弄清各個文件這間的聯繫和依託關係很有幫助。

Mm

目錄包含了所有獨立於 cpu 體系結構的內存管理代碼,如頁式存儲管理內存的分配和釋放等。與具體硬件體系結構相關的內存管理代碼位於arch/*/mm目錄下,例如arch/i386/mm/Fault.c 。

Net

目錄裏是核心的網絡部分代碼,其每個子目錄對應於網絡的一個方面。

ReadMe

文件提供內核的各種編譯方法;生成文件的查看方法,如 nm vmlinux | sort | less

Reporting-bugs

目錄裏是有關報告Bug 的一些內容

Rules.make

目錄裏是各種Makefilemake所使用的一些共同規則

Scripts

目錄包含用於配置核心的腳本文件等。

一般在每個目錄下都有一個.depend文件和一個Makefile文件。這兩個文件都是編譯時使用的輔助文件。仔細閱讀這兩個文件對弄清各個文件之間的聯繫和依託關係很有幫助。另外有的目錄下還有Readme文件,它是對該目錄下文件的一些說明,同樣有利於對內核源碼的理解。

 

隱藏文件:

.Config

Make oldconfig 和make defconfig會把生成的默認的configuration放到文件.config中。執行Make之後會根據Makefile上下層級依賴關係編譯整個系統,.config作爲編譯各個模塊的依據。

 

2.內核編譯的Make選項

Make提供的僞目標

Cleaning targets:

  clean                 - Remove most generated files but keep theconfig and enough build support to build external modules

  mrproper          - Remove all generated files + config +various backup files

  distclean           - mrproper + remove editor backup and patchfiles

 

Configuration targets:

  config                 -Update current config utilising a line-oriented program

  nconfig          - Update current config utilising a ncursesmenu based program

  menuconfig      -Update current config utilising a menu based program

  xconfig               -Update current config utilising a QT based front-end

  gconfig              -Update current config utilising a GTK based front-end

  oldconfig             -Update current config utilising a provided .config as base

  localmodconfig  - Update current config disabling modules notloaded

  localyesconfig  - Update current config converting local modsto core

  silentoldconfig - Same as oldconfig, butquietly, additionally update deps

  defconfig           - New config with default from ARCH supplieddefconfig

  savedefconfig   - Save current config as ./defconfig(minimal config)

  allnoconfig        - New config where all options are answeredwith no

  allyesconfig      - New config where all options are acceptedwith yes

  allmodconfig    - New config selecting modules when possible

  alldefconfig    - New config with all symbols set todefault

  randconfig        - New config with random answer to alloptions

  listnewconfig   - List new options

  oldnoconfig     - Same as silentoldconfig but set newsymbols to n (unset)

 

Other generic targets:

  all                      - Build all targets marked with [*]

*vmlinux              - Build the bare kernel

*modules             - Build all modules

  modules_install - Install all modules toINSTALL_MOD_PATH (default: /)

  firmware_install- Install all firmware toINSTALL_FW_PATH

                    (default:$(INSTALL_MOD_PATH)/lib/firmware)

  dir/            - Build all files in dir and below

  dir/file.[oisS] - Build specified target only

  dir/file.lst    - Build specified mixed source/assemblytarget only

                    (requires a recent binutilsand recent build (System.map))

  dir/file.ko     - Build module including final link

  modules_prepare - Set up for buildingexternal modules

  tags/TAGS        - Generate tags file for editors

  cscope              - Generate cscope index

  gtags           - Generate GNU GLOBAL index

  kernelrelease   - Output the release version string

  kernelversion   - Output the version stored in Makefile

  headers_install - Install sanitised kernelheaders to INSTALL_HDR_PATH

                    (default:/home/hanks/linux-3.0.4/usr)

 

Static analysers

  checkstack      - Generate a list of stack hogs

  namespacecheck  - Name space analysis on compiled kernel

  versioncheck    - Sanity check on version.h usage

  includecheck    - Check for duplicate included header files

  export_report   - List the usages of all exported symbols

  headers_check   - Sanity check on exported headers

  headerdep       - Detect inclusion cycles in headers

  coccicheck      - Check with Coccinelle.

 

Kernel packaging:

  rpm-pkg             - Build both source and binary RPMkernel packages

  binrpm-pkg          - Build only the binary kernelpackage

  deb-pkg             - Build the kernel as an debpackage

  tar-pkg            - Build the kernel as anuncompressed tarball

  targz-pkg           - Build the kernel as a gzipcompressed tarball

  tarbz2-pkg          - Build the kernel as a bzip2compressed tarball

  tarxz-pkg           - Build the kernel as a xzcompressed tarball

  perf-tar-src-pkg    - Build perf-3.0.4.tar source tarball

  perf-targz-src-pkg  - Build perf-3.0.4.tar.gz source tarball

  perf-tarbz2-src-pkg - Buildperf-3.0.4.tar.bz2 source tarball

  perf-tarxz-src-pkg  - Build perf-3.0.4.tar.xz source tarball

 

Documentation targets:

 Linux kernel internal documentation indifferent formats:

  htmldocs        - HTML

  pdfdocs         - PDF

  psdocs          - Postscript

  xmldocs         - XML DocBook

  mandocs         - man pages

  installmandocs  - install man pages generated by mandocs

  cleandocs       - clean all generated DocBook files

 

Architecture specifictargets (x86):

*bzImage      - Compressed kernel image(arch/x86/boot/bzImage)

  install     - Install kernel using

                  (your) ~/bin/installkernel or

                  (distribution)/sbin/installkernel or

                  install to $(INSTALL_PATH)and run lilo

  fdimage     - Create 1.4MB boot floppy image (arch/x86/boot/fdimage)

  fdimage144  - Create 1.4MB boot floppy image (arch/x86/boot/fdimage)

  fdimage288  - Create 2.8MB boot floppy image (arch/x86/boot/fdimage)

  isoimage    - Create a boot CD-ROM image (arch/x86/boot/image.iso)

                  bzdisk/fdimage*/isoimage alsoaccept:

                  FDARGS="..."  arguments for the booted kernel

                  FDINITRD=file initrd for thebooted kernel

 

  i386_defconfig           - Build for i386

  x86_64_defconfig         - Build for x86_64

 

  make V=0|1  [targets] 0 => quiet build (default), 1=> verbose build

  make V=2   [targets] 2 => give reason forrebuild of target

  make O=dir  [targets] Locate all output files in"dir", including .config

  make C=1   [targets] Check all c source with$CHECK (sparse by default)

  make C=2  [targets] Force check of all c source with $CHECK

  make W=n  [targets] Enable extra gcc checks, n=1,2,3 where

                            1: warnings whichmay be relevant and do not occur too often

                            2: warnings whichoccur quite often but may still be relevant

                            3: more obscurewarnings, can most likely be ignored

                            Multiple levels canbe combined with W=12 or W=123

  make RECORDMCOUNT_WARN=1 [targets] Warn aboutignored mcount sections

 

Execute"make" or "make all" to build all targets marked with [*]

Forfurther info see the ./README file

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章