在上一篇 Vagrant 安裝Guest Ubuntu 中介紹了利用vagrant 快速部署Guest Ubuntu京办,下一步我們需要安裝一個(gè)自己編譯的內(nèi)核,作為后續(xù)的學(xué)習(xí)環(huán)境帆焕。
我們可以在Host Ubuntu中編譯內(nèi)核惭婿,也可以在Guest Ubuntu中編譯,推薦在Host Ubuntu中編譯叶雹,這樣編譯速度更快财饥,但是即便在Host中編譯,將來(lái)我們還是需要在Guest Ubuntu中編譯Vbox Guest Addition driver. 所以在Host和Guest Ubuntu中安裝如下軟件:
# 在Host Ubuntu中安裝
root@ubuntu:~/vagrant# sudo apt install bison flex libelf-dev libncurses5-dev \
openssl libssl-dev gcc bc make dpkg-dev git socat gdb libbabeltrace-dev
# 到Guest Ubuntu中安裝
root@ubuntu:~/vagrant# vagrant up
root@ubuntu:~/vagrant# vagrant ssh
vagrant@ubuntu-xenial:~$ sudo apt install bison flex libelf-dev libncurses5-dev \
openssl libssl-dev gcc bc make dpkg-dev git socat gdb libbabeltrace-dev
vagrant@ubuntu-xenial:~$ exit
現(xiàn)在建議創(chuàng)建另外一個(gè)snapshot.
root@ubuntu:~/vagrant# vagrant halt
root@ubuntu:~/vagrant# vagrant snapshot save installed-toolchains
root@ubuntu:~/vagrant# vagrant snapshot list
fresh-installed-ubuntu
installed-toolchains
前面說(shuō)過(guò)浑娜,Guest OS中 /vagrant目錄和Host OS的$HOME/vagrant目錄是共享的佑力,所以只要把內(nèi)核源代碼下載到Host OS就可以了。將來(lái)的試驗(yàn)過(guò)程中我們會(huì)對(duì)內(nèi)核進(jìn)行修改筋遭,所以用git建立一個(gè)新的branch打颤,方便記錄我們的修改。
root@ubuntu:~/vagrant# git clone https://github.com/torvalds/linux.git
root@ubuntu:~/vagrant# cd linux
# create a testing branch and check out this branch
root@ubuntu:~/vagrant/linux # git branch testing
root@ubuntu:~/vagrant/linux # git checkout testing
然后就可以編譯內(nèi)核了漓滔,通常編譯的步驟是make menuconfig编饺,但是自己難免會(huì)漏選,所以我們 拷貝Guest Ubuntu內(nèi)核的config 文件到共享目錄:
# This command in Guest Ubuntu
vagrant@ubuntu-xenial:~$ cp /boot/config-`uname -r` /vagrant/config
# following command in Host Ubuntu
root@ubuntu:~# cd vagrant/linux
root@ubuntu:~/vagrant/linux# mkdir -pv ../obj/x86_64
root@ubuntu:~/vagrant/linux# cp ../config ../obj/x86_64
root@ubuntu:~/vagrant/linux# yes '' | make O=../obj/x86_64/ oldconfig
現(xiàn)在內(nèi)核已經(jīng)配置好了响驴,但是我們還要enable 一些debug的配置透且,為后續(xù)學(xué)習(xí)做準(zhǔn)備。同樣,如果通過(guò)make menuconfig手動(dòng)配置秽誊,通常找到某一個(gè)選項(xiàng)鲸沮,結(jié)果它還依賴其它的,我們可以讓kernel的make file 自動(dòng)處理锅论,只需要做如下改動(dòng):
diff --git a/scripts/kconfig/Makefile b/scripts/kconfig/Makefile
index f9bdd02..e12c6ac 100644
--- a/scripts/kconfig/Makefile
+++ b/scripts/kconfig/Makefile
@@ -132,6 +132,11 @@ PHONY += kvmconfig
kvmconfig: kvm_guest.config
@:
+PHONY += testingconfig
+testingconfig: testing.config
+ @:
+
+
PHONY += xenconfig
xenconfig: xen.config
@:
diff --git a/kernel/configs/testing.config b/kernel/configs/testing.config
new file mode 100644
index 0000000..e81c872
--- /dev/null
+++ b/kernel/configs/testing.config
@@ -0,0 +1,8 @@
+CONFIG_FRAME_POINTER=y
+CONFIG_KGDB=y
+CONFIG_DEBUG_INFO=y
+CONFIG_KGDB_SERIAL_CONSOLE=y
+CONFIG_DEBUG_INFO_DWARF4=y
+CONFIG_FUNCTION_TRACER=y
+CONFIG_DYNAMIC_DEBUG=y
+CONFIG_BLK_DEV_RAM=y
+CONFIG_CC_OPTIMIZE_FOR_SIZE=y
然后使用make testingconfig merge這些選項(xiàng)到.config文件讼溺。
root@ubuntu:~/vagrant/linux# make O=../obj/x86_64 testingconfig
現(xiàn)在就可以編譯了,-j6是用6個(gè)線程編譯最易,可以適當(dāng)調(diào)整:
root@ubuntu:~/vagrant/linux# make O=../obj/x86_64 -j6 && \
make O=../obj/x86_64 bindeb-pkg
現(xiàn)在就是漫長(zhǎng)的等待了怒坯,記得把自己的改動(dòng)提交到自己的git branch
root@ubuntu:~/vagrant/linux# git add kernel/configs/testing.config
root@ubuntu:~/vagrant/linux# git commit -a -m "add testing.config"
推薦使用Host Ubuntu編譯,如果想嘗試在Guest Ubuntu編譯藻懒,需要注意的是:Guest OS中對(duì)共享目錄的文件調(diào)用mmap時(shí)會(huì)失敗剔猿,錯(cuò)誤為:
Could not mmap file: vmlinux
make: *** [vmlinux] Error 1
因此需要把編譯內(nèi)核的腳本修改一下,此方法出自這里嬉荆。原理就是把編譯時(shí)的vmlinux文件拷貝到/tmp目錄下归敬,然后執(zhí)行scripts/sortextable /tmp/vmlinux,最后再考回來(lái)鄙早。patch如下:
diff --git a/scripts/link-vmlinux.sh b/scripts/link-vmlinux.sh
index 9045823..dd5c6f7 100755
--- a/scripts/link-vmlinux.sh
+++ b/scripts/link-vmlinux.sh
@@ -155,7 +155,10 @@ mksysmap()
sortextable()
{
- ${objtree}/scripts/sortextable ${1}
+ cp ${1} /tmp/.${1}
+ scripts/sortextable /tmp/.${1}
+ cp /tmp/.${1} ${1}
+# ${objtree}/scripts/sortextable ${1}
}
# Delete output files in case of error
下一篇介紹在Vagrant中啟動(dòng)新編譯的內(nèi)核弄慰。