LoginSignup
17

More than 5 years have passed since last update.

カーネル(Linux-4.8.6)をコンパイルしてインストールしてみた

Posted at

0.構築環境

カーネルを再構築した環境は以下になります。

  • ディストリビューション:CentOS 7.2
  • CPU:Intel(R) Core(TM) i5-6400 4Core
  • 既存のカーネルのバージョン:3.10.0-327.36.1.el7.x86_64

1-1.開発ツールのインストール

カーネル再構築に必要なツールをインストールします。

[root@localhost ~]# 
yum groupinstall "Development Tools"
yum install kernel-devel
yum install rpm-build redhat-rpm-config unifdef
yum install ncurses ncurses-devel

1-2.カーネルソースのダウンロード

カーネルソースをダウンロードします。
https://www.kernel.org/
git経由でもHTTP経由でもダウンロードできます。
今回はHTTP経由でダウンロードしました。2016/11/04現在、最新の安定版は4.8.6となっております。
一般的にカーネルは「/usr/src」ディレクトリに格納するため、今回も「/usr/src」にソースを格納します。

[root@localhost ~]# 
cd /usr/src/
wget https://cdn.kernel.org/pub/linux/kernel/v4.x/linux-4.8.6.tar.xz
xz -dc linux-4.8.6.tar.xz | tar xvf -
mv linux-4.8.6.tar.xz /tmp/linux-4.8.6.tar.xz

2-1.コンフィグレーション

カーネルモジュールの個別の設定に関しては今回は割愛します。
既存の設定ファイルをコピーし、最新のカーネルで設定が必要な「NEW」となっている項目はデフォルトの設定のまま進めます。
選択する項目の際は、何も入力せずに改行します。
なお、設定が必要な「Kernel compression mode」については、今回は「1. Gzip (KERNEL_GZIP)」とします。

[root@localhost linux-4.8.6]#
cat /boot/config-3.10.0-327.36.1.el7.x86_64 > ./.config
[root@localhost linux-4.8.6]#
make oldconfig
scripts/kconfig/conf  --oldconfig Kconfig
.config:600:warning: symbol value 'm' invalid for CPU_FREQ_STAT
*
* Restart config...
*
*
* General setup
*
Cross-compiler tool prefix (CROSS_COMPILE) [] 
Compile also drivers which will not load (COMPILE_TEST) [N/y/?] (NEW) 
Local version - append to kernel release (LOCALVERSION) [] 
Automatically append version information to the version string (LOCALVERSION_AUTO) [N/y/?] n
Kernel compression mode
> 1. Gzip (KERNEL_GZIP)
  2. Bzip2 (KERNEL_BZIP2)
  3. LZMA (KERNEL_LZMA)
  4. XZ (KERNEL_XZ)
  5. LZO (KERNEL_LZO)
  6. LZ4 (KERNEL_LZ4) (NEW)
choice[1-6?]: 1
Default hostname (DEFAULT_HOSTNAME) [(none)] (none)
Support for paging of anonymous memory (swap) (SWAP) [Y/n/?] y
System V IPC (SYSVIPC) [Y/n/?] y
POSIX Message Queues (POSIX_MQUEUE) [Y/n/?] y
Enable process_vm_readv/writev syscalls (CROSS_MEMORY_ATTACH) [Y/n/?] y
uselib syscall (USELIB) [Y/n/?] (NEW) 
Auditing support (AUDIT) [Y/n/?] y
*
* IRQ subsystem
*
Expose hardware/virtual IRQ mapping via debugfs (IRQ_DOMAIN_DEBUG) [N/y/?] n
*
* Timers subsystem

・・・(以下省略)・・・

個別にconfigの設定をしたい場合は、menuconfigで設定するのがよいと思われます。
今回はデフォルト設定のままとしました。

[root@localhost linux-4.8.6]#
make menuconfig
 .config - Linux/x86 4.8.6 Kernel Configuration
 ────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
  ┌──────────────────────────────────────────────────────────────────────── Linux/x86 4.8.6 Kernel Configuration ─────────────────────────────────────────────────────────────────────────┐
  │  Arrow keys navigate the menu.  <Enter> selects submenus ---> (or empty submenus ----).  Highlighted letters are hotkeys.  Pressing <Y> includes, <N> excludes, <M> modularizes       │  
  │  features.  Press <Esc><Esc> to exit, <?> for Help, </> for Search.  Legend: [*] built-in  [ ] excluded  <M> module  < > module capable                                               │  
  │                                                                                                                                                                                       │  
  │                                                                                                                                                                                       │  
  │ ┌───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐ │  
  │ │                                                      [*] 64-bit kernel                                                                                                            │ │  
  │ │                                                          General setup  --->                                                                                                      │ │  
  │ │                                                      [*] Enable loadable module support  --->                                                                                     │ │  
  │ │                                                      -*- Enable the block layer  --->                                                                                             │ │  
  │ │                                                          Processor type and features  --->                                                                                        │ │  
  │ │                                                          Power management and ACPI options  --->                                                                                  │ │  
  │ │                                                          Bus options (PCI etc.)  --->                                                                                             │ │  
  │ │                                                          Executable file formats / Emulations  --->                                                                               │ │  
  │ │                                                      < > Volume Management Device Driver                                                                                          │ │  
  │ │                                                      [*] Networking support  --->                                                                                                 │ │  
  │ │                                                          Device Drivers  --->                                                                                                     │ │  
  │ │                                                          Firmware Drivers  --->                                                                                                   │ │  
  │ │                                                          File systems  --->                                                                                                       │ │  
  │ │                                                          Kernel hacking  --->                                                                                                     │ │  
  │ │                                                          Security options  --->                                                                                                   │ │  
  │ │                                                      -*- Cryptographic API  --->                                                                                                  │ │  
  │ │                                                      [*] Virtualization  --->                                                                                                     │ │  
  │ │                                                          Library routines  --->                                                                                                   │ │  
  │ │                                                                                                                                                                                   │ │  
  │ │                                                                                                                                                                                   │ │  
  │ │                                                                                                                                                                                   │ │  
  │ │                                                                                                                                                                                   │ │  
  │ │                                                                                                                                                                                   │ │  
  │ │                                                                                                                                                                                   │ │  
  │ │                                                                                                           

2-2.コンパイル

configの設定が完了したので、ダウンロードしたカーネルソースをコンパイルします。
「make」コマンドは -j オプションで並列実行してくれます。
CPUのCore数+1と設定します。(Core数に+1をする理由は不明)
私の場合、4Coreでしたので、並列実行数は5とします。

[root@localhost linux-4.8.6]#
make -j 5

ビルドの完了までに30分~2時間程度かかるようです。

2-3.インストール

コンパイルしたモジュールをインストールします。

[root@localhost linux-4.8.6]#
make modules_install
[root@localhost linux-4.8.6]#
make install
sh ./arch/x86/boot/install.sh 4.8.6 arch/x86/boot/bzImage \
    System.map "/boot"

以下のようにディレクトリが存在すれば、モジュールのインストールは完了です。

[root@localhost ~]#
ll /lib/modules 
drwxr-xr-x  3 root root 4096 11月  4 00:27 4.8.6
[root@localhost linux-4.8.6]#
ll /boot/
lrwxrwxrwx  1 root root        22 11月  4 00:27 System.map -> /boot/System.map-4.8.6
-rw-r--r--  1 root root   3423327 11月  4 00:27 System.map-4.8.6
-rw-r--r--  1 root root   3423327 11月  4 00:24 System.map-4.8.6.old
drwxr-xr-x. 2 root root        26  9月  3 18:06 grub
drwx------. 6 root root      4096 11月  4 00:29 grub2
lrwxrwxrwx  1 root root        19 11月  4 00:27 vmlinuz -> /boot/vmlinuz-4.8.6
-rw-------  1 root root  62997794 11月  4 00:29 initramfs-4.8.6.img
-rw-r--r--  1 root root   5772128 11月  4 00:27 vmlinuz-4.8.6
-rw-r--r--  1 root root   5772128 11月  4 00:24 vmlinuz-4.8.6.old

・・・(その他省略)・・・

※補足:今回ビルドを実施した際に、「opensslv.h」が存在しません。
といったエラーが生じたため以下のパッケージもダウンロードしました。

[root@localhost ~]# 
yum install openssl-devel

3-1.完了確認

インストールまで完了しましたら、「reboot」します。
起動画面で以下のように表示されている

CentOS Linux (4.8.6) 7 (Core)

またターミナル上でも

[root@localhost ~]# 
uname -r
4.8.6

となっていればカーネルのインストールは完了です。

やってみるまでは、カーネルの再構築って難しそうな印象を持っていましたが、
実際やってみると、案外スムーズにできました!
ご参考になれば幸いです。

4.参考書籍

  • 『これ一冊で完全理解 Linuxカーネル超入門』(日経Linux 著、日経BP社)

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
17