1 安裝相關(guān)依賴和軟件
sudo apt-get install vim
sudo apt-get install vim-scripts
sudo apt-get install vim-gtk
sudo apt-get install fcitx (中文輸入法需要)
sudo apt-get install fcitx-libs (中文輸入法需要)
sudo apt-get install kolourpaint4(圖片編輯軟件)
sudo apt-get install rar (壓縮伐坏、解壓文件)
sudo apt-get install unrar
sudo apt-get install wine (模擬win系統(tǒng)環(huán)境)
sudo apt-get install hexedit (16進(jìn)制編輯器)
sudo apt-get install net-tools(網(wǎng)絡(luò)相關(guān)工具)
sudo apt-get install wireshark (可選,抓包軟件)
sudo apt-get install libelf-dev
sudo apt-get install libssl-dev
sudo apt-get install bison
sudo apt-get install git
sudo apt-get install gcc-arm-linux-gnueabi
sudo apt-get install build-essential
sudo apt-get install ninja-build
sudo apt-get install libpixman-1-dev
sudo apt-get install flex(編譯用到的詞法分析器)
sudo apt update
$ sudo apt install build-essential
$ sudo apt install gcc-multilib
$ sudo apt install git
2 下載linux源碼和buildroot
git clone https://mirrors.tuna.tsinghua.edu.cn/git/linux.git
root@linux:~#wget https://buildroot.org/downloads/buildroot-2020.02.8.tar.gz
root@linux:~#tar zxvf buildroot-2020.02.8.tar.gz && cd buildroot-2020.02.8
root@linux:~#export ARCH=x86_64
root@linux:~#export FORCE_UNSAFE_CONFIGURE=1
root@linux:~#make menuconfig
這里下載buildroot壓縮包解壓,配置buildroot
它的作用就是生成制作rootfs,制作 rootfs 可以理解為創(chuàng)建 Linux 系統(tǒng)的根文件系統(tǒng)(Root File System)奠伪。在 Linux 中,根文件系統(tǒng)是整個(gè)文件系統(tǒng)的起點(diǎn)首懈,包含了操作系統(tǒng)啟動(dòng)所需的所有文件和目錄結(jié)構(gòu)绊率。它包括了操作系統(tǒng)內(nèi)核、設(shè)備文件究履、配置文件滤否、庫(kù)文件、可執(zhí)行文件等
除了上述方式buildroot還有busybox最仑,這里主要講buildroot
先看下在buildtools根目錄下執(zhí)行make menuconfig
你可以在這里進(jìn)行可視化配置
Toolchain --->
# 編譯一些package會(huì)用到藐俺,比如f2fstools
[*] Enable WCHAR support
System configuration --->
# 系統(tǒng)啟動(dòng)后,密碼為空泥彤,回車登錄shell
[*] Enable root login with password
( ) Root password
# 設(shè)置eth0接口為DHCP欲芹,如果不設(shè)置,qemu啟動(dòng)kernel鏡像后吟吝,需要手動(dòng)配置網(wǎng)絡(luò)
(eth0) Network interface to configure through DHCP
Target packages --->
Filesystem and flash utilities --->
# 格式化f2fs用到
[*] f2fs-tools
Filesystem images --->
[ ] ext2/3/4 root filesystem
# 設(shè)置rootfs的文件系統(tǒng)類型
[*] f2fs root filesystem
配置完成后菱父,buildtools 根目錄執(zhí)行
make
這個(gè)耗時(shí)長(zhǎng)一些,結(jié)束后你可以在output/images下看到roottfs.f2fs文件
注意點(diǎn)
修改buildroot-2020.02.8/package目錄下的package源碼后,重新編譯時(shí)浙宜,需要?jiǎng)h除output/build/目錄中對(duì)應(yīng)的package官辽,否則編譯出來(lái)的rootfs.f2fs鏡像不會(huì)包含改動(dòng)。
當(dāng)你生成了roottfs.f2fs后就完成了第一步
接下來(lái)將你的roottfs.f2fs文件拷貝到你的linux內(nèi)核源碼根目錄
在linux內(nèi)核源碼根目錄打開控制臺(tái)
root@linux:/home/gsf/linux-5.10.3# export ARCH=“x86_64”
root@linux:/home/gsf/linux-5.10.3# make x86_64_defconfig
root@linux:/home/gsf/linux-5.10.3# make menuconfig
繼續(xù)開啟內(nèi)核的基礎(chǔ)配置
Processor type and features --->
[ ] Randomize the address of the kernel image (KASLR) 不要選
Drevice Drivers --->
NVME Support
<*> NVM Express block device
[*] NVMe multipath support
[*] NVMe hardware monitoring
<*> NVM Express over Fabrics FC host driver
<*> NVM Express over Fabrics TCP host driver
File systems --->
<*> F2FS filesystem support
[*] F2FS Status Information (NEW)
[*] F2FS extended attributes (NEW)
[*] F2FS Access Control Lists (NEW)
Network File Systems--->
<*> NFS client support
<*> NFS client support for NFS version 2
<*> NFS client support for NFS version 3
[*] NFS client support for the NFSv3 ACL protocol extension
<*> NFS client support for NFS version 4
<*> SMB3 and CIFS support (advanced network filesystem)
Kernel hacking --->
Compile-time checks and compiler options --->
[*] Compile the kernel with debug info
[*] Provide GDB scripts for kernel debugging
注:高版本改成了
Kernel hacking --->
Compile-time checks and compiler options --->
Debug information (Disable debug information) --->
(X)Generate DWARF Version 5 debuginfo
[*] Provide GDB scripts for kernel debugging
按上述要求配置好linux內(nèi)核
root@linux:/home/gsf/linux-5.10.3#make -j4 bzImag
編譯引導(dǎo)鏡像 bzImag用于拉起linux內(nèi)核
當(dāng)你有了bzimg和roottfs.f2fs后粟瞬,就可以啟動(dòng)linux內(nèi)核了
最簡(jiǎn)單的啟動(dòng)野崇,資源很少,無(wú)disk亩钟,無(wú)網(wǎng)絡(luò)
qemu-system-x86_64 -kernel arch/x86_64/boot/bzImage -drive file=rootfs.f2fs,if=ide,format=raw -nographic -append "root=/dev/sda console=ttyS0"
啟動(dòng)完成后,用戶名輸入root鳖轰,登錄系統(tǒng)查看信息清酥,f2fs已經(jīng)掛載:
# cat /proc/mounts
/dev/root / f2fs rw,lazytime,relatime,background_gc=on,discard,no_heap,user_xattr,inline_xattr,acl,inline_data,inline_dentry,flush_merge,exte0
devtmpfs /dev devtmpfs rw,relatime,size=48612k,nr_inodes=12153,mode=755
接下來(lái)掛載 scsi disk(格式化成ext4)啟動(dòng)
- 1 制作ext4鏡像
root@linux:/home/gsf/linux-5.10.3#dd if=/dev/zero of=ext4.img bs=1M count=256
root@linux:/home/gsf/linux-5.10.3# mkfs.ext4 ext4.img
- 2 執(zhí)行qemu命令
qemu-system-x86_64 -kernel arch/x86_64/boot/bzImage -drive file=rootfs.f2fs,if=ide,format=raw,id=myid0 --nographic -append "root=/dev/sda console=ttyS0" -hdb ext4.img
這里要注意你生成bzImage路徑,機(jī)器不同可能是x86/x86_64
這里進(jìn)入虛擬linux系統(tǒng)中
- 3 添加掛載配置
創(chuàng)建目錄
# mkdir /mnt/scsimp
并在 vi /etc/fstab文件中增加一行:
/dev/sdb /mnt/scsimp ext4 defaults 0 0
- 4 reboot查看虛擬linux系統(tǒng)蕴侣,是否掛載ext4鏡像
# cat /proc/mounts
……
sysfs /sys sysfs rw,relatime 0 0
/dev/sdb /mnt/scsimp ext4 rw,relatime 0 0
完成ext4鏡像掛載 接下來(lái)就是nvme disk
這里給出nvme disk的配置步驟焰轻,但是我配置失敗了,如果你們配置成功可以給我個(gè)文檔鏈接昆雀,這里nvme的配置不會(huì)影響你使用虛擬linux系統(tǒng)
3 配置NVME disk
- 1 制作虛擬磁盤
root@linux:/home/gsf/linux-5.10.3#qemu-img create -f raw disk.qcow 1G
- 2 執(zhí)行qemu命令啟動(dòng)虛擬linux系統(tǒng)
qemu-system-x86_64 -kernel arch/x86_64/boot/bzImage -drive file=rootfs.f2fs,if=ide,format=raw,id=myid0 --nographic -append “root=/dev/sda console=ttyS0” -device nvme,drive=nvme1,serial=deadbeaf,num_queues=8 -drive file=disk.qcow,if=none,id=nvme1 -smp 4
這個(gè)命令是用 QEMU 模擬器來(lái)啟動(dòng)一個(gè) x86_64 架構(gòu)的虛擬機(jī)辱志,并加載 Linux 內(nèi)核和根文件系統(tǒng)進(jìn)行運(yùn)行。讓我解釋一下各個(gè)參數(shù)的含義:
qemu-system-x86_64:?jiǎn)?dòng) QEMU 模擬器狞膘,用于模擬 x86_64 架構(gòu)的虛擬機(jī)揩懒。
-kernel arch/x86_64/boot/bzImage:指定 Linux 內(nèi)核的鏡像文件路徑。
-drive file=rootfs.f2fs,if=ide,format=raw,id=myid0:掛載根文件系統(tǒng)挽封,使用 F2FS 文件系統(tǒng)格式已球,通過(guò) IDE 接口模擬硬盤,指定格式為 raw辅愿,設(shè)備 ID 為 myid0智亮。
--nographic:以非圖形化(無(wú)圖形界面)模式運(yùn)行 QEMU。
-append “root=/dev/sda console=ttyS0”:向內(nèi)核傳遞的啟動(dòng)參數(shù)点待,指定根文件系統(tǒng)為 /dev/sda阔蛉,并設(shè)置控制臺(tái)為串口終端 ttyS0。
-device nvme,drive=nvme1,serial=deadbeaf,num_queues=8:添加一個(gè) NVMe 設(shè)備癞埠,指定驅(qū)動(dòng)名稱為 nvme1状原,序列號(hào)為 deadbeaf,隊(duì)列數(shù)為 8燕差。
-drive file=disk.qcow,if=none,id=nvme1:將一個(gè) QCOW 格式的磁盤映像文件作為 NVMe 設(shè)備的存儲(chǔ)介質(zhì)遭笋,設(shè)備 ID 為 nvme1。
-smp 4:指定虛擬機(jī)的 CPU 個(gè)數(shù)為 4徒探。
這個(gè)命令的作用是啟動(dòng)一個(gè)使用指定 Linux 內(nèi)核和 F2FS 格式根文件系統(tǒng)的 x86_64 虛擬機(jī)瓦呼,并添加了一個(gè) NVMe 設(shè)備作為存儲(chǔ)介質(zhì)。
- 3 創(chuàng)建目錄 設(shè)置掛載配置
創(chuàng)建目錄
# mkdir /mnt/nvmemp
并在/etc/fstab文件中增加:
/dev/nvme0n1p1 /mnt/nvmemp f2fs defaults 0 0 -------后面會(huì)將disk.qcow格式化成f2fs,所以這里按f2fs掛載
格式化nvme disk
#fdisk /dev/nvme0n1
Device contains neither a valid DOS partition table, nor Sun, SGI, OSF
or GPT disklabel
Building a new DOS disklabel. Changes will remain in memory only,
until you decide to write them. After that the previous content
won’t be recoverable.
Command (m for help): n
Partition type
p primary partition (1-4)
e extended
p
Partition number (1-4): 1
First sector (32-2097151, default 32): ------------回車央串,默認(rèn)即可
Using default value 32
Last sector or +size{,K,M,G,T} (32-2097151, desfault 2097151):
------------回車磨澡,默認(rèn)即可
Using default value 2097151
Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table
[ 131.575409] nvme0n1: p1
格式化成功后,出現(xiàn)/dev/nvme0n1p1設(shè)備质和,這就是剛fdisk新建的分區(qū)
格式化新建的nvme分區(qū)
mkfs.f2fs -L nvmedisk /dev/nvme0n1p1
- 4 reboot重啟虛擬linux系統(tǒng)稳摄,查看nvme是否掛載
# cat /proc/mounts
……
sysfs /sys sysfs rw,relatime 0 0
/dev/sdb /mnt/scsimp ext4 rw,relatime 0 0
/dev/nvme0n1p1 /mnt/nvmemp f2fs rw,relatime 0 0
另外,還可以看到系統(tǒng)中有5個(gè)nvme隊(duì)列饲宿。nvme0q0是管理隊(duì)列厦酬。nvme0q1~4是IO隊(duì)列每個(gè)核一個(gè)(我們的qemu命令指定了num_queues=8、smp 4參數(shù))瘫想。
# cat /proc/interrupts
# cat /proc/interrupts
CPU0 CPU1 CPU2 CPU3
0: 183 0 0 0 IO-APIC 2-edge timer
1: 0 0 9 0 IO-APIC 1-edge i8042
……
24: 6 0 0 0 PCI-MSI 65536-edge nvme0q0
25: 0 0 0 11 PCI-MSI 65537-edge nvme0q1
26: 0 0 0 0 PCI-MSI 65538-edge nvme0q2
27: 0 0 0 0 PCI-MSI 65539-edge nvme0q3
28: 0 0 0 0 PCI-MSI 65540-edge nvme0q4
到這里基本就結(jié)束了
接下來(lái)就是配置vscode仗阅,可以debug linux源碼執(zhí)行流程,有助于你理解linux源碼
4 vscode配置
首先下載vscode gdb擴(kuò)展包
接下來(lái)配置(.vscode/launch.json
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "kernel debug",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/vmlinux",
"cwd": "${workspaceFolder}",
"MIMode": "gdb",
"miDebuggerPath":"/usr/bin/gdb-multiarch",
"miDebuggerServerAddress": "localhost:1234"
}
]
}
參數(shù)介紹
program: 調(diào)試的符號(hào)文件
miDebuggerPath:gdb的路徑国夜, 這里需要注意的是减噪,由于我們是arm64內(nèi)核,因此需要用gdb-multiarch來(lái)進(jìn)行調(diào)試
miDebuggerServerAddress:對(duì)端地址车吹,qemu會(huì)默認(rèn)使用1234這個(gè)端口
如果沒下載gdb-multiarch
sudo apt-get install gdb-multiarch
完成后,打開一個(gè)控制臺(tái)執(zhí)行
qemu-system-x86_64 -kernel arch/x86/boot/bzImage -drive file=rootfs.f2fs,if=ide,format=raw,id=myid0 --nographic -append "root=/dev/sda console=ttyS0" -nographic -s -S
這個(gè)命令可以等待vscode終端通過(guò)gdb 連接
vscode 執(zhí)行start debuging
然后就可以通過(guò)vscode調(diào)試linux源碼了
5 優(yōu)質(zhì)博主推薦
ubuntu20.04上linux內(nèi)核開發(fā)環(huán)境搭建(qemu+gdb+vscode)_ubuntu 內(nèi)核開發(fā)-CSDN博客