Ubuntuカーネルソースのインストール

ドライバをソースコードからコンパイルする場合などに現在稼働中のカーネルバージョンのソースが必要になる。

/usr/src配下にカーネルソースをダウンロードする。

# cd /usr/src
# apt-get source linux-image-$(uname -r)

ソースコードリポジトリを有効にしていない場合、aptがエラー終了する。

# apt-get source linux-image-$(uname -r)
Reading package lists... Done
E: You must put some 'source' URIs in your sources.list

エラーメッセージが指定しているsources.listは/etc/apt配下にあり、下記2行を追加する。

deb-src http://archive.ubuntu.com/ubuntu disco main
deb-src http://archive.ubuntu.com/ubuntu disco-updates main

今回の環境はVERSION="19.04 (Disco Dingo)"なのでdiscoとなっているが対象環境によってここは変わる。

sources.listを編集した後、apt updateを実行して認識させる。

# apt update
Hit:1 http://us.archive.ubuntu.com/ubuntu disco InRelease
Hit:2 http://packages.microsoft.com/repos/vscode stable InRelease   
Get:3 http://archive.ubuntu.com/ubuntu disco InRelease [257 kB]                
Get:4 http://security.ubuntu.com/ubuntu disco-security InRelease [97.5 kB]     
Hit:5 http://us.archive.ubuntu.com/ubuntu disco-updates InRelease              
Hit:6 http://us.archive.ubuntu.com/ubuntu disco-backports InRelease            
Get:7 http://archive.ubuntu.com/ubuntu disco-updates InRelease [97.5 kB]       
Get:8 http://archive.ubuntu.com/ubuntu disco/main Sources [838 kB]
Get:9 http://archive.ubuntu.com/ubuntu disco-updates/main Sources [98.0 kB] 
Fetched 1,388 kB in 4s (385 kB/s)                     
Reading package lists... Done
Building dependency tree       
Reading state information... Done
5 packages can be upgraded. Run 'apt list --upgradable' to see them.

これで再度ソースのダウンロードを実行すると無事追加された。

# uname -r
5.0.0-31-generic
#
# apt-get source linux-image-$(uname -r)
Reading package lists... Done
Picking 'linux-signed' as source package instead of 'linux-image-5.0.0-31-generic'
Need to get 19.2 kB of source archives.
Get:1 http://archive.ubuntu.com/ubuntu disco-updates/main linux-signed 5.0.0-31.33 (dsc) [2,098 B]
Get:2 http://archive.ubuntu.com/ubuntu disco-updates/main linux-signed 5.0.0-31.33 (tar) [17.1 kB]
Fetched 19.2 kB in 1s (23.9 kB/s)       
dpkg-source: info: extracting linux-signed in linux-signed-5.0.0
dpkg-source: info: unpacking linux-signed_5.0.0-31.33.tar.xz
W: Download is performed unsandboxed as root as file 'linux-signed_5.0.0-31.33.dsc' couldn't be accessed by user '_apt'. - pkgAcquire::Run (13: Permission denied)
#
# ls
linux-headers-5.0.0-27          linux-signed-5.0.0
linux-headers-5.0.0-27-generic  linux-signed_5.0.0-31.33.dsc
linux-headers-5.0.0-31          linux-signed_5.0.0-31.33.tar.xz
linux-headers-5.0.0-31-generic 

ウォーニングメッセージが出ているが、これはバグのようだ。Googleで検索したらDebianのバグレポートがでてきた。 bugs.debian.org

きちんとソースがダウンロードされているし、ドライバのコンパイルなど目的には支障がないので特に気にしない。

インストールしたカーネルソースをコンパイルできるようにするには下記を実行する。

# apt install build-essential libncurses-dev flex bison openssl libssl-dev dkms libelf-dev libudev-dev libpci-dev libiberty-dev autoconf
# apt build-dep linux linux-image-$(uname -r)

インストールする個々のパッケージの内容はapt showコマンドで確認できる。