Linuxカーネル・ソースを読むための準備した
前回の続きです。
今回はカーネル・ソースを実際に読めるようなところまでの手順をご紹介したいと思います。
SRPMファイルのダウンロード
普通状態であれば、OSにはソースコードは付いてこないので、SRPMファイルというファイルをダウンロードして、インストールする必要があります。
まずは適切なバージョンのSRPMファイルをwget等でダウンロードします。
[root@localhost tmp]# wget http://archives.fedoraproject.org/pub/archive/fedora/linux/releases/20/Fedora/source/SRPMS/k/kernel-3.11.10-301.fc20.src.rpm --2018-05-16 12:55:14-- http://archives.fedoraproject.org/pub/archive/fedora/linux/releases/20/Fedora/source/SRPMS/k/kernel-3.11.10-301.fc20.src.rpm Resolving archives.fedoraproject.org (archives.fedoraproject.org)... 209.132.181.25, 209.132.181.23, 209.132.181.24 Connecting to archives.fedoraproject.org (archives.fedoraproject.org)|209.132.181.25|:80... connected. HTTP request sent, awaiting response... 200 OK Length: 75668415 (72M) [application/x-rpm] Saving to: ‘kernel-3.11.10-301.fc20.src.rpm’ 100%[======================================>] 75,668,415 1.39MB/s in 60s 2018-05-16 12:56:16 (1.19 MB/s) - ‘kernel-3.11.10-301.fc20.src.rpm’ saved [75668415/75668415]
SRPMインストール
続いてrpmコマンドを使用して、インストールします。
[root@localhost tmp]# rpm -ivh kernel-3.11.10-301.fc20.src.rpm warning: kernel-3.11.10-301.fc20.src.rpm: Header V3 RSA/SHA256 Signature, key ID 246110c1: NOKEY Updating / installing... 1:kernel-3.11.10-301.fc20 ################################# [100%]
カーネル・ソースの展開
インストールが終わったら、~/rpmbuild/SPECS/にkernel.specが作成されます。
そのままではカーネルのソースコードは、まだ展開されていない状態になります。
ので、rpmbuildコマンドを使用して展開する必要があります。
[root@localhost tmp]# cd ~/rpmbuild/SPECS/ [root@localhost SPECS]# ls kernel.spec [root@localhost SPECS]# rpmbuild -bp kernel.spec -bash: rpmbuild: command not found
んっ!?
コマンドが見つからない!?
どうやらrpmbuildコマンドが導入されていないようでしたので、インストールします。
[root@localhost SPECS]# [root@localhost SPECS]# yum install rpm-build Dependency Updated: elfutils-libelf.x86_64 0:0.161-6.fc20 rpm-build-libs.x86_64 0:4.11.3-3.fc20 rpm-libs.x86_64 0:4.11.3-3.fc20 rpm-python.x86_64 0:4.11.3-3.fc20 Complete!
これで実行できるので、コマンドを実行します。
[root@localhost SPECS]# rpmbuild -bp kernel.spec warning: line 517: Invalid version (double separator '-'): elfutils-0.153-1: BuildRequires: rpm-build >= 4.9.0-1, elfutils >= elfutils-0.153-1 error: Failed build dependencies: m4 is needed by kernel-3.11.10-301.fc20.x86_64 gcc >= 3.4.2 is needed by kernel-3.11.10-301.fc20.x86_64 hmaccalc is needed by kernel-3.11.10-301.fc20.x86_64 bc is needed by kernel-3.11.10-301.fc20.x86_64 xmlto is needed by kernel-3.11.10-301.fc20.x86_64 asciidoc is needed by kernel-3.11.10-301.fc20.x86_64 elfutils-devel is needed by kernel-3.11.10-301.fc20.x86_64 zlib-devel is needed by kernel-3.11.10-301.fc20.x86_64 binutils-devel is needed by kernel-3.11.10-301.fc20.x86_64 newt-devel is needed by kernel-3.11.10-301.fc20.x86_64 python-devel is needed by kernel-3.11.10-301.fc20.x86_64 perl(ExtUtils::Embed) is needed by kernel-3.11.10-301.fc20.x86_64 bison is needed by kernel-3.11.10-301.fc20.x86_64 audit-libs-devel is needed by kernel-3.11.10-301.fc20.x86_64 pciutils-devel is needed by kernel-3.11.10-301.fc20.x86_64 pesign >= 0.10-4 is needed by kernel-3.11.10-301.fc20.x86_64
んっ!?
どうやら必要なパッケージがあるようなので、それぞれ表示されているパッケージをyumコマンドでインストールしていきます。
インストール完了後の確認
rpmbuild コマンドでのインストールが完了すると、~/rpmbuild/BUILD/kernel-3.11.fc20/linux-3.11.10-301.fc20.x86_64/配下にソースコードが作成されていることがわかります。
[root@localhost SPECS]# cd ~/rpmbuild/BUILD/kernel-3.11.fc20/linux-3.11.10-301.fc20.x86_64/ [root@localhost linux-3.11.10-301.fc20.x86_64]# ls arch config-powerpc64 fs REPORTING-BUGS block config-powerpc64p7 include samples config-arm64 config-powerpc-generic init scripts config-arm-generic configs ipc security config-armv7 config-s390x Kbuild sound config-armv7-generic config-x86-32-generic Kconfig temp-arm64 config-armv7-lpae config-x86_64-generic kernel temp-armv7 config-debug config-x86-generic lib temp-armv7-lpae config-generic COPYING MAINTAINERS temp-x86-32 config-i686-PAE CREDITS Makefile temp-x86-64 config-local crypto merge.pl tools config-nodebug Documentation mm usr config-powerpc32-generic drivers net virt config-powerpc32-smp firmware README
Makefileの冒頭部分を確認するとちゃんと想定したバージョンのカーネルのソースコードであることがわかります。
[root@localhost linux-3.11.10-301.fc20.x86_64]# head Makefile VERSION = 3 PATCHLEVEL = 11 SUBLEVEL = 10 EXTRAVERSION = NAME = Linux for Workgroups # *DOCUMENTATION* # To see a list of typical targets execute "make help" # More info can be located in ./README # Comments in this file are targeted only to the developer, do not
このままでもソースコードの閲覧はできるのですが、このままだと閲覧しにくいので、タグ・ジャンプ機能を使用できるようにするため、下記コマンドを実行して、タグファイルを作成します。
find `pwd` -type f -name "*.[ch]" -o -name "*.[sS]" | sed -e 's/^/"/' -e 's/$/"/' | xargs /usr/bin/ctags -a
こうすることで、関数の呼び出し元を閲覧しやすくなります。
例えば、
vim -t printk
とすると、一気に/kernel/printk/printk.cまでジャンプすることができます。
これでカーネルのソースコードを読む準備ができたので、せっせと読んでいきたいのですが、量が多すぎて、どこから手をつけようかと迷っています。笑