如何编辑Linux源代码?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1988248/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to edit Linux source code?
提问by user223541
I want to modify the source code for Linux. I don't know from where to start.
我想修改Linux的源代码。我不知道从哪里开始。
I want to study the code and then modify it and see the changes by runnng it. Where do I start? I am a college passout and I know C and C++ but never edited the source code of Linux. I want a small light-weight Linux that has small source code files so that I can study it and modiy. To create my own one. Which Linux will be best for me and how do I get started with editing the soucre code? Will just installing Linux also give me its source code?
我想研究代码然后修改它并通过运行它来查看更改。我从哪里开始?我是一名大学毕业生,我知道 C 和 C++,但从未编辑过 Linux 的源代码。我想要一个小的轻量级 Linux,它有小的源代码文件,以便我可以研究和修改。创建我自己的。哪种 Linux 最适合我,我如何开始编辑源代码?只是安装 Linux 也会给我它的源代码吗?
回答by DisplacedAussie
Learn how to download, compile and install your own kernel
Read and edit the code
阅读和编辑代码
Recompile and install your edited kernel
重新编译并安装您编辑过的内核
回答by Eli Bendersky
Here's a nice tutorialon compiling the Linux kernel.
A word of advice: use a virtual machine like VirtualBox for running the modified kernel - this way you can easily experiment without harming any real hardware.
忠告:使用像 VirtualBox 这样的虚拟机来运行修改后的内核——这样你就可以轻松地进行实验而不会损害任何真实的硬件。
回答by Quentin
The term "Linux" can mean a couple of different things.
术语“Linux”可能意味着几个不同的东西。
The Linux Kernel
Linux 内核
This is the real Linux, and is available from http://kernel.org/.
这是真正的 Linux,可以从http://kernel.org/ 获得。
You won't find a small or light weight version of this. The kernel is the kernel. (That said, you can turn features off during compile time, which is useful if you are targeting low powered hardware such as for an embedded device).
您不会找到它的小型或轻型版本。内核就是内核。(也就是说,您可以在编译期间关闭功能,如果您的目标是低功耗硬件,例如嵌入式设备,这将非常有用)。
Linux distributions
Linux 发行版
This is a Linux kernel, bundled with a big stack of other software needed to make it useful. The source code for the various pieces of software are available separately.
这是一个 Linux 内核,捆绑了一大堆其他软件,使其有用。各种软件的源代码可单独获得。
A netinstall of Debiangives you the core of the system in a small download. It has a package management system that makes it easy to get the source codeof the various programs available for it.
Debian的 netinstall只需一个小的下载即可为您提供系统的核心。它有一个包管理系统,可以很容易地获取各种可用程序的源代码。
回答by John T
What exactly do you want to edit from a Linux distribution?
你到底想从 Linux 发行版编辑什么?
Linux itself is just a kernel, but the term is also used to refer to operating systems using the kernel. If you want to tinker with GUI programs, you can install a Linux distribution then download the source of those programs separately. If you want to tinker with the kernel itself, the source can be obtained from kernel.org. Each Linux distribution has it's own set of programs and features you can change around to your liking, but I doubt you want to edit everythingthat comes in a standard distribution like Ubuntu, as recompiling everything each time to see changes will take a while.
Linux 本身只是一个内核,但该术语也用于指代使用内核的操作系统。如果您想修改 GUI 程序,您可以安装 Linux 发行版,然后单独下载这些程序的源代码。如果您想修改内核本身,可以从kernel.org获得源代码。每个 Linux 发行版都有自己的一套程序和功能,您可以根据自己的喜好进行更改,但我怀疑您是否想编辑标准发行版中的所有内容,例如Ubuntu,因为每次重新编译所有内容以查看更改都需要一段时间。
Linux is a lotto dive into for a first timer in the OS field. There is a great smaller Unix-like operating system for learning called MINIX. The source download is around 2.2M I believe and it is used as a teaching aid in many operating system development courses worldwide. Personally I'd sink my teeth into that a bit before taking on Linux, but the choice is yours, so have fun with it! Build it, break it, and try to rebuild it again! It's a great learning experience.
Linux对于操作系统领域的初学者来说有很多值得深入研究的地方。有一个非常小的类 Unix 操作系统供学习使用,称为MINIX。源下载大约为 2.2MI,它被用作全球许多操作系统开发课程的教学辅助工具。就我个人而言,在使用 Linux 之前,我会认真考虑一下,但选择权在你,所以尽情享受吧!建造它,打破它,并尝试再次重建它!这是一次很棒的学习经历。
回答by Vatine
The linux kernel is a monster of a source tree (at least in the sense of not being small). Old kernels are smaller, so it may make sense to look at those if you're after a small linux kernel to look at and play around with.
linux 内核是一个源代码树的怪物(至少在不小的意义上)。旧内核较小,因此如果您正在寻找一个小型 linux 内核来查看和使用它们,那么查看这些内核可能是有意义的。
However, if all you're looking for is some unix-like kernel to play around with, Minix 3may be another possible choice.
然而,如果您只是在寻找一些类似 Unix 的内核,那么Minix 3可能是另一种可能的选择。
For the specifics, install any linux distribution, then install the kernel source package (Gentoo will probably install this by default; the specifci name of the kernel source package depends on your distribution).
具体来说,安装任何 linux 发行版,然后安装内核源代码包(Gentoo 可能会默认安装它;内核源代码包的具体名称取决于您的发行版)。
回答by Richard Pennington
If you just want to learn about operating systems in general, Minix might be a good place to start: http://www.minix3.org. Minix uses a fairly small (<5000) line microkernel so it is fairly easy to get familiar with its basic operation. Running it virtually (e.g. under vmware) is the easiest way to get started.
如果您只是想了解一般的操作系统,Minix 可能是一个不错的起点:http: //www.minix3.org。Minix 使用相当小的(<5000)行微内核,因此很容易熟悉其基本操作。虚拟运行(例如在 vmware 下)是最简单的入门方法。
回答by Michael Stum
If you want to modify the Linux Kernel or one of the "core" applications that usually form a distribution, and if you don't have much experience with Linux or modifying it, I recommend looking at Linux From Scratch. It will guide you to the process of installing a Linux system purely from it's core components and allows you to really understand a) what is actually part of Linux and b) what every part does.
如果您想修改 Linux 内核或通常构成发行版的“核心”应用程序之一,并且您对 Linux 没有太多经验或对其进行修改,我建议您查看Linux From Scratch。它将引导您完全从其核心组件安装 Linux 系统的过程,并让您真正了解 a) Linux 的实际组成部分和 b) 每个部分的作用。
Once you've done that, you should know where the sources are, how to compile them and how to deploy the changes. The next step is then just firing up your favorite editor and modifying it.
完成此操作后,您应该知道源在哪里、如何编译它们以及如何部署更改。下一步就是启动你最喜欢的编辑器并修改它。
回答by MarkR
Installing a Linux distribution is of course a reasonable prerequisite - Linux is self-hosting, in that it can (probably) only be compiled under a GNU/Linux system (with gcc and the gnu C library - although the C library is only used in tools during compilation - it isn't linked into the kernel). Numerous other tools are also required to compile Linux, which are documented.
安装 Linux 发行版当然是一个合理的先决条件 - Linux 是自托管的,因为它(可能)只能在 GNU/Linux 系统下编译(使用 gcc 和 gnu C 库 - 尽管 C 库仅用于编译期间的工具 - 它没有链接到内核中)。编译 Linux 还需要许多其他工具,这些工具已记录在案。
Getting the source code is easy enough, but the difficult bit is configuring the kernel correctly for your machine - therefore you probably want to use your distribution's kernel source instead (which may have patches but they will be insignificant for your purposes).
获取源代码很容易,但困难的一点是为您的机器正确配置内核 - 因此您可能想改用发行版的内核源代码(可能有补丁,但它们对您的目的来说无关紧要)。
The easiest way to "try out" kernel programming is to write a kernel module - these can be dynamically loaded (which means you can test your code without a reboot - provided it doesn't crash the system)
“尝试”内核编程的最简单方法是编写一个内核模块——这些模块可以动态加载(这意味着你可以在不重启的情况下测试你的代码——前提是它不会使系统崩溃)
There are loads of examples of kernel modules around, however almost all of them are obsolete due to the fast-changing internal APIs in the kernel itself (even the build system changes relatively rapidly).
周围有大量内核模块的示例,但由于内核本身的内部 API 快速变化(即使构建系统变化相对较快),几乎所有示例都已过时。
Kernel modules can do almost anything that doesn't involve directly changing a core part of the kernel - there are a lot of "hook" functions (mostly register_something to register a device, hook, protocol, etc) which can be used to extend the kernel's userspace API or modify its behaviour in some way.
内核模块几乎可以做任何不涉及直接更改内核核心部分的事情——有很多“钩子”函数(主要是 register_something 来注册设备、钩子、协议等),它们可用于扩展内核的用户空间 API 或以某种方式修改其行为。
回答by DigitalRoss
You might consider the BSD distributions, they are a lot easier to compile from source
您可能会考虑 BSD 发行版,它们更容易从源代码编译
I know you said Linux but distributions like NetBSD are also a flavor of Unixand they are much more approachable from a compiler-everything-from-source perspective.
我知道你说的是 Linux,但像 NetBSD 这样的发行版也是 Unix 的一种风格,从编译器的一切从源代码的角度来看,它们更容易接近。
- Generally a much smaller default or minimal install
- The base system is built as a single system with make. Linux is a collection of utilities and packages from a lot of different places that are individually built, while the BSD distributions have a specific source tree that can be built from the top of /usr/src with a single command.
- A lot easier to cross-compile, should you need to do that.
- 通常较小的默认安装或最小安装
- 基本系统是作为一个带有 make 的系统构建的。Linux 是从许多不同地方单独构建的实用程序和包的集合,而 BSD 发行版有一个特定的源代码树,可以使用单个命令从 /usr/src 的顶部构建。
- 如果您需要这样做,交叉编译会容易得多。
With the BSD distributions a single source tree contains the base system, and a clear dividing line is drawn between internal and external elements. The projects distribute their entire systems in ready-to-compile form.
在 BSD 发行版中,一个单一的源代码树包含基本系统,并在内部和外部元素之间划出了一条清晰的分界线。这些项目以准备编译的形式分发其整个系统。
回答by achoora
Directly Trying to Edit the source code may be Tough a.It depends on your proficiency of C and Operating System knowledge.I would say that you should first look about how functionalities like cat ,grep ,etc are implemented in C.Then comes the modules concepts .Different types of kernel module and basic linux operations with the modules .Then choose a specific functionality like networking or some other.The go through the code for that section . Example : http://www.leidinger.net/FreeBSD/dox/netipsec/html/df/d62/ipsec_8h_source.html
直接尝试编辑源代码可能很难 a.这取决于您对 C 的熟练程度和操作系统知识。我会说您应该首先看看 cat ,grep 等功能是如何在 C 中实现的。然后是模块概念。不同类型的内核模块和使用这些模块的基本 linux 操作。然后选择特定的功能,如网络或其他一些。通过该部分的代码。示例:http: //www.leidinger.net/FreeBSD/dox/netipsec/html/df/d62/ipsec_8h_source.html
Try to do a kernel compilation on Ubuntu or in any other version .At this point delve in to Kernel modification.I know this will take time but the Learning curve will be steep,and you will have a comphrehensive view.
尝试在 Ubuntu 或任何其他版本上进行内核编译。此时深入研究内核修改。我知道这需要时间,但学习曲线会很陡峭,你会有一个全面的观点。