如何从 linux 内核中克隆模块?

How can I clone a module from linux kernel?

如果我只想专注于 linux 的一个模块,例如 perf,我怎样才能从 github 分叉或下载 perf 模块相关文件?我尝试了以下命令:

c:\work> git clone https://github.com/torvalds/linux/tree/master/tools/perf
Cloning into 'perf'...
fatal: repository 'https://github.com/torvalds/linux/tree/master/tools/perf/' not found

但是不行。

您需要结合使用 Git 的两个相对较新的功能。

第一个是 sparse-checkout(自 Git 1.7.0 起可用)。 Sparse-checkout 允许你通过明确指定你想要在你的 repo 中拥有哪些目录来保持你的工作空间干净。然而,它不会影响整个存储库的大小,下载所有 Linux 内核源代码的 1GB 是一件令人头疼的事情。这就是为什么您需要第二个功能:

第二个功能是浅层克隆(自Git 1.9.0 起可用)。它允许您使用 --depth 参数从历史记录中仅保留 n 变更集的回购中提取。

因此,如果您只想获得 tools/perf 模块,这是可行的方法:

git init
git remote add origin https://github.com/torvalds/linux.git
git config core.sparsecheckout true
echo "tools/perf" >> .git/info/sparse-checkout
git pull --depth=1 origin master

瞧!您的存储库中唯一的目录是 tools/perf,您只需下载 136MB。

添加到 Luboš 的回答中,因为我也只需要性能,但需要一个特定的标记版本。标签不会出现在浅克隆中。所以你需要在标记的版本中准确地检查回购协议。

替换

git pull --depth=1 origin master

git fetch --depth=1 origin v4.19
git checkout FETCH_HEAD

如果你想要的版本是4.19.

如果您想实际构建特定的 perf 版本,您将需要更多文件夹:

git init
git remote add origin https://github.com/torvalds/linux.git
git config core.sparsecheckout true
echo "tools/perf" >> .git/info/sparse-checkout
echo "tools/scripts" >> .git/info/sparse-checkout
echo "tools/build" >> .git/info/sparse-checkout
echo "tools/include" >> .git/info/sparse-checkout
echo "tools/include" >> .git/info/sparse-checkout
echo "tools/arch" >> .git/info/sparse-checkout
echo "tools/lib" >> .git/info/sparse-checkout
git fetch --depth=1 origin v4.19
git checkout FETCH_HEAD
make -C tools/perf

现在(如果您安装了所有依赖项):

$ ./tools/perf/perf --version
perf version 4.19.g84df