参考链接:https://blog.nelhage.com/2013/12/lightweight-linux-kernel-development-with-kvm
1、内核编译
kernel: kernel-3.12
config: 从上述链接下载,去掉CONFIG_64BIT
make bzImage
2.创建磁盘disk
sudo debootstrap wheezy ./wheezyhttp://http.debian.net/debian/
//让root用户自动登录
sudosed -i '/^root/ { s/:x:/::/ }' wheezy/etc/passwd
mkfs.ext4 -F wheezy.img
sudo mkdir -p /mnt/wheezy
sudo mount -o loop wheezy.img /mnt/wheezy
sudo cp -a wheezy/. /mnt/wheezy/.
sudoumount /mnt/wheezy
3. 启动:
kvm-kernel arch/x86/boot/bzImage \
-drive file=$HOME/vms/wheezy.img,if=virtio \
-append root=/dev/vda
4. 配置网络
kvm[...]
-net nic,model=virtio,macaddr=52:54:00:12:34:56 \
-net user,hostfwd=tcp:127.0.0.1:4444-:22
//能ssh VM通过 localhost:4444
.
5. 去掉kvm的图形界面
kvm[...]
-append 'root=/dev/vda console=hvc0' \
-chardev stdio,id=stdio,mux=on,signal=off \
-device virtio-serial-pci \
-device virtconsole,chardev=stdio \
-mon chardev=stdio \
-display non
6. 内核调试
$ gdbvmlinux
(gdb)set architecture i386:x86-64
(gdb)target remote :1234
能实现源码级内核调试。分析了下,主要在编译用的config中增加了调试信息。
· CONFIG_DEBUG_KERNEL
· CONFIG_DEBUG_INFO
· CONFIG_DEBUG_INFO_REDUCED
7. Config文件的制作过程:
make allnoconfig
生成一个最小的config文件,之后添加了一些必要的配置项。
· CONFIG_64BIT
· CONFIG_BLK_DEV_INITRD Forbooting test programs in an initrd.
· CONFIG_BLK_DEV
· CONGIG_BLK_DEV_RAM Requiredfor initrd
· CONFIG_SMP Nearlyevery computer is SMP these days, so let's build for testing on SMP.
· CONFIG_PCI Virtio allruns over fake PCI devices
· CONFIG_ELF
· CONFIG_BINFMT_SCRIPT We reallyneed ELF and shebang support.
· CONFIG_IA32_EMULATION Optional,but being able to run 32-bit binaries can be useful for testing.
· CONFIG_FILE_LOCKING dpkg and andmany other essential userspace tools need file locking.
· CONFIG_NET
· CONFIG_UNIX
· CONFIG_PACKET dhclientneeds CONFIG_PACKET
· CONFIG_INET TCP/IP
· CONFIG_NETDEVICES
· CONFIG_VIRTIO_NET
· CONFIG_NET_9P Virtual 9Pnetworking device
· CONFIG_VIRTIO_PCI Supportvirtio PCI devices
· CONFIG_NET_9P_VIRTIO
· CONFIG_9P_FS Support9P-based filesystem exports.
· CONFIG_EXT4_FS
· CONFIG_EXT4_USE_FOR_EXT23 ext2/3/4support for the root
· CONFIG_TMPFS Needed forudev
· CONFIG_INOTIFY_USER Needed forudev
· CONFIG_VIRTIO_BLK Needed forthe virtio HDD
· CONFIG_VIRTIO_CONSOLE Needed forthe virtio console
· CONFIG_DEBUG_KERNEL
· CONFIG_DEBUG_INFO
· CONFIG_DEBUG_INFO_REDUCED Build withdebug symbols; _REDUCED gives significantly better buildtimes, but still keeps key symbols around. Drop it if you're going to be doingserious kernel debugging.