{"componentChunkName":"component---src-templates-post-template-js","path":"/posts/linux-kernel-build-install","result":{"data":{"markdownRemark":{"id":"c5168cb9-126d-5aeb-97cc-fe4e6e4d2b97","html":"<p>Linux カーネルのソースコードを少しだけ書き換えてビルド・インストールしてみたので、手順などを残しておきます。</p>\n<h2 id=\"環境\" style=\"position:relative;\"><a href=\"#%E7%92%B0%E5%A2%83\" aria-label=\"環境 permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>環境</h2>\n<p>KVM 上の Ubuntu 20.04</p>\n<div class=\"gatsby-highlight\" data-language=\"console\"><pre class=\"language-console\"><code class=\"language-console\">$ uname -r\n5.8.0-63-generic</code></pre></div>\n<h2 id=\"linux-カーネルのダウンロード\" style=\"position:relative;\"><a href=\"#linux-%E3%82%AB%E3%83%BC%E3%83%8D%E3%83%AB%E3%81%AE%E3%83%80%E3%82%A6%E3%83%B3%E3%83%AD%E3%83%BC%E3%83%89\" aria-label=\"linux カーネルのダウンロード permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Linux カーネルのダウンロード</h2>\n<p>Linux カーネルのソースコードは /usr/src 以下に配置するので、/usr/src に移動してソースコードをダウンロード・展開します。</p>\n<div class=\"gatsby-highlight\" data-language=\"console\"><pre class=\"language-console\"><code class=\"language-console\">$ cd /usr/src\n$ sudo curl -LO https://cdn.kernel.org/pub/linux/kernel/v5.x/linux-5.13.7.tar.xz\n$ xz -dc linux-5.13.7.tar.xz | sudo tar xvf -\n$ cd linux-5.13.7</code></pre></div>\n<p>これで linux-5.13.7 というディレクトリにソースコードが展開されました。</p>\n<p>なお、カーネルのダウンロード URL は、以下のページで確認できます。</p>\n<p><a href=\"https://www.kernel.org/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">https://www.kernel.org/</a></p>\n<h2 id=\"設定\" style=\"position:relative;\"><a href=\"#%E8%A8%AD%E5%AE%9A\" aria-label=\"設定 permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>設定</h2>\n<p>カーネルをビルドする際の設定として、/usr/src/linux-5.13.7 以下に .config というファイルを作成する必要があります。</p>\n<p>このファイルは</p>\n<ul>\n<li><code class=\"language-text\">make oldconfig</code></li>\n<li><code class=\"language-text\">make localmodconfig</code></li>\n</ul>\n<p>などで作成できます。</p>\n<p>カーネルのビルド時間を短くしたり、サイズを小さくしたりするには、<code class=\"language-text\">make localmodconfig</code> を使って、必要なモジュールだけでビルドするのが簡単です。</p>\n<p>参考</p>\n<ul>\n<li><a href=\"https://mongonta.com/f277-howto-shorten-linux-kernel-build-time/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">カーネルのビルド時間を短縮する技 | ガジェット好きの日記</a></li>\n</ul>\n<h2 id=\"少し書き換えてみる\" style=\"position:relative;\"><a href=\"#%E5%B0%91%E3%81%97%E6%9B%B8%E3%81%8D%E6%8F%9B%E3%81%88%E3%81%A6%E3%81%BF%E3%82%8B\" aria-label=\"少し書き換えてみる permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>少し書き換えてみる</h2>\n<p>./init/main.c の kernel_init 関数を少しだけ書き換えてみます。</p>\n<p>GitHub 上のソースコードの該当箇所は <a href=\"https://github.com/torvalds/linux/blob/62fb9874f5da54fdb243003b386128037319b219/init/main.c#L1443\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">こちら</a> です。 </p>\n<p>ひとまずログ出力を追加するだけとします。</p>\n<div class=\"gatsby-highlight\" data-language=\"diff\"><pre class=\"language-diff\"><code class=\"language-diff\"><span class=\"token unchanged\"><span class=\"token prefix unchanged\"> </span>\tdo_sysctl_args();\n<span class=\"token prefix unchanged\"> </span>\n</span><span class=\"token inserted-sign inserted\"><span class=\"token prefix inserted\">+</span>\tpr_info(\"[MYLOG] log1 ramdisk_execute_command: %s\\n\", ramdisk_execute_command);\n</span><span class=\"token unchanged\"><span class=\"token prefix unchanged\"> </span>\tif (ramdisk_execute_command) {\n</span><span class=\"token inserted-sign inserted\"><span class=\"token prefix inserted\">+</span>\t\tpr_info(\"[MYLOG] log2 ramdisk_execute_command: %s\\n\", ramdisk_execute_command);\n</span><span class=\"token unchanged\"><span class=\"token prefix unchanged\"> </span>\t\tret = run_init_process(ramdisk_execute_command);\n<span class=\"token prefix unchanged\"> </span>\t\tif (!ret)\n<span class=\"token prefix unchanged\"> </span>\t\t\treturn 0;</span></code></pre></div>\n<h2 id=\"ビルド\" style=\"position:relative;\"><a href=\"#%E3%83%93%E3%83%AB%E3%83%89\" aria-label=\"ビルド permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>ビルド</h2>\n<p>ビルド自体は <code class=\"language-text\">sudo make</code> だけで実行可能ですが、高速化するために 2 つほど工夫をいれます。</p>\n<h3 id=\"ccache-のインストール\" style=\"position:relative;\"><a href=\"#ccache-%E3%81%AE%E3%82%A4%E3%83%B3%E3%82%B9%E3%83%88%E3%83%BC%E3%83%AB\" aria-label=\"ccache のインストール permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>ccache のインストール</h3>\n<p>「<a href=\"https://mongonta.com/f277-howto-shorten-linux-kernel-build-time/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">カーネルのビルド時間を短縮する技 | ガジェット好きの日記</a>」という記事を参考に、ccache を導入しました。</p>\n<p>一回目のビルドは高速化しませんが、二回目以降は高速になるはずです。</p>\n<h3 id=\"並列での実行\" style=\"position:relative;\"><a href=\"#%E4%B8%A6%E5%88%97%E3%81%A7%E3%81%AE%E5%AE%9F%E8%A1%8C\" aria-label=\"並列での実行 permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>並列での実行</h3>\n<p>CPU の複数コアを活かしてビルドするため、make コマンドにオプションをつけます。</p>\n<div class=\"gatsby-highlight\" data-language=\"console\"><pre class=\"language-console\"><code class=\"language-console\">sudo make -j8</code></pre></div>\n<p>しばらく待てばビルドが完了します。</p>\n<h3 id=\"configsystemtrusted_keys-に対応するファイルがない旨のエラー\" style=\"position:relative;\"><a href=\"#configsystemtrusted_keys-%E3%81%AB%E5%AF%BE%E5%BF%9C%E3%81%99%E3%82%8B%E3%83%95%E3%82%A1%E3%82%A4%E3%83%AB%E3%81%8C%E3%81%AA%E3%81%84%E6%97%A8%E3%81%AE%E3%82%A8%E3%83%A9%E3%83%BC\" aria-label=\"configsystemtrusted_keys に対応するファイルがない旨のエラー permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>CONFIG<em>SYSTEM</em>TRUSTED_KEYS に対応するファイルがない旨のエラー</h3>\n<p>.config 内の CONFIG<em>SYSTEM</em>TRUSTED_KEYS の設定に対応するファイルがない場合、エラーが発生します。</p>\n<p>とりあえずビルドを通すだけでよければ、.config を</p>\n<div class=\"gatsby-highlight\" data-language=\"txt\"><pre class=\"language-txt\"><code class=\"language-txt\">CONFIG_SYSTEM_TRUSTED_KEYS=&quot;&quot;</code></pre></div>\n<p>のように書き換えることで解消できます。</p>\n<h2 id=\"インストール\" style=\"position:relative;\"><a href=\"#%E3%82%A4%E3%83%B3%E3%82%B9%E3%83%88%E3%83%BC%E3%83%AB\" aria-label=\"インストール permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>インストール</h2>\n<p>カーネルモジュールのインストール</p>\n<div class=\"gatsby-highlight\" data-language=\"console\"><pre class=\"language-console\"><code class=\"language-console\">$ sudo make modules_install</code></pre></div>\n<p>カーネルのインストール</p>\n<div class=\"gatsby-highlight\" data-language=\"console\"><pre class=\"language-console\"><code class=\"language-console\">$ sudo make install</code></pre></div>\n<h2 id=\"再起動\" style=\"position:relative;\"><a href=\"#%E5%86%8D%E8%B5%B7%E5%8B%95\" aria-label=\"再起動 permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>再起動</h2>\n<p>新しくインストールしたカーネルで起動するか、再起動して確認します。</p>\n<div class=\"gatsby-highlight\" data-language=\"console\"><pre class=\"language-console\"><code class=\"language-console\">sudo reboot</code></pre></div>\n<p>uname で確認すると…</p>\n<div class=\"gatsby-highlight\" data-language=\"console\"><pre class=\"language-console\"><code class=\"language-console\">$ uname -r\n5.13.7</code></pre></div>\n<p>カーネルのバージョンが 5.13.7 になっています。</p>\n<h2 id=\"ログを確認\" style=\"position:relative;\"><a href=\"#%E3%83%AD%E3%82%B0%E3%82%92%E7%A2%BA%E8%AA%8D\" aria-label=\"ログを確認 permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>ログを確認</h2>\n<p>dmesg で、カーネルのログを確認すると…</p>\n<div class=\"gatsby-highlight\" data-language=\"console\"><pre class=\"language-console\"><code class=\"language-console\">$ dmesg | grep MYLOG\n[    0.633390] [MYLOG] log1 ramdisk_execute_command: /init\n[    0.633391] [MYLOG] log2 ramdisk_execute_command: /init</code></pre></div>\n<p>追加したログが反映されていました。</p>","fields":{"slug":"/posts/linux-kernel-build-install","tagSlugs":["/tag/linux/"],"autoRecommendPosts":["linux-from-scratch","udemy-linux-reintroduction","487311313X","what-is-installation"]},"frontmatter":{"date":"2021-08-01T05:23:36.826Z","description":"Linux カーネルのソースコードを少しだけ書き換えてビルド・インストールしてみたので、手順などを残しておきます。","tags":["linux"],"title":"Linux カーネルのコードを少しだけ書き換えてビルド・インストールする","socialImage":null,"recommendPosts":null}}},"pageContext":{"slug":"/posts/linux-kernel-build-install"}},"staticQueryHashes":["251939775","3942705351","401334301"]}