UML进行Linux内核调试 --1 环境搭建

来源:互联网 发布:尼古丁口香糖 知乎 编辑:程序博客网 时间:2024/05/21 19:48


1、下载内核版本:

linux-3.4.9.tar.bz2


 

2、解压编译,注意在配置的时候menuconfig或者xconfig等的时候不要去修改UML的关于cpu设置的配置

linux #make mrproperlinux #make mrproper ARCH=um linux #make menuconfig ARCH=um linux #make linux ARCH=umlinux #make modules ARCH=um linux #make modules_install ARCH=um 


 

OK了,生成我们需要的内核了,而且支持调试了。

直接启动会有问题的,提示找不到root文件系统。这样我们用人家现成的。

去这里下载,一个文件系统,我的系统是RHEL 6.3,没有关系就下一个Centos的文件系统就可以了:

http://user-mode-linux.sourceforge.net/

这个网址里面有配置好的内核版本和编译好的文件系统。

可以去kernel.org下载源代码形式的内核版本呢,这里http://uml.devloop.org.uk/kernels.html他给出了一个config文件(记得选择一直的内核版本和config文件),复制过去,你直接到内核根目录下面去,执行这个命令:

make linux ARCH=um

图方便就直接下载二进制的内核版本就是了,但是我不使用人家的现成的,因为那样无法修改内核代码,只能trace流程:
http://uml.devloop.org.uk/kernels.html

文件系统呢,下载就是100m左右,解压后是1.6g左右。如果下载了文件系统,就不要像上面一样编译了,可以直接解压使用的。

http://fs.devloop.org.uk/

我下载的文件系统是CentOS6.x-x86-root_fs.bz2,解压,重命名为:root_fs即可

文件系统下载完了,就可以直接启动内核了:

[root@localhost linux-3.4.9]# ./vmlinux ubda=root_fs mem=256m或者:[root@localhost linux-3.4.9]# ./linux ubda=root_fs mem=256m

他会一直运行,知道提示输入用户名和密码,用户名初始为root,没有密码。

但是,这样也有问题,进入系统后,会一直提示:

modprobe: FATAL: Could not load /lib/modules/3.5.1/modules.dep: No such file or directorymodprobe: FATAL: Could not load /lib/modules/3.5.1/modules.dep: No such file or directorymodprobe: FATAL: Could not load /lib/modules/3.5.1/modules.dep: No such file or directory

这是因为我们从网上下载的现成的文件系统的lib的/lib/modules/路径下面没有我们刚才编译的内核的modules,所以需要把开始操作:linux #make modules_install ARCH=um 生成的文件夹拷贝到文件

系统映像里面去,路径就是host主机的/lib/modules/3.5.1。但是我不这么做,因为网上下载的文件太小,我觉得不够用,所以我自己做一个文件映像:

路径:/azuo操作1,创建文件系统:[root@localhost azuo]# dd if=/dev/zero of=disk.img bs=1000M count=10 操作2,格式化为ext3格式:mkfs.ext3  disk.img 操作3,重命名:mv disk.img root_fs操作4,创建挂载文件夹,/azuo/mnt_fsmkdir /azuo/mnt_fs操作5,挂载root_fs到/azuo/mnt_fs:cd /azuo;mount -o loop root_fs mnt_fs操作6: 下载网上的已经做好的映像文件到路径/um_linux/,重命名为root_fs;[root@localhost linux-3.5.1]# cd /um_linux/[root@localhost um_linux]# dirconfig  install.sh  kernel32-3.5.1  linux-3.5.1  linux-3.5.1.tar.bz2  root_fs操作7,把这个root_fs挂载到/mnt:mount -o loop root_fs /mnt操作8,把挂载后的/mnt下面的所有东西都拷贝到/azuo/mnt_fs下去[root@localhost um_linux]# cd /mnt[root@localhost mnt]# dirbin  boot  cgroup  dev  etc  home  lib  lost+found  media  mnt  opt proc  root  sbin  selinux  srv  sys  tmp  usr  var[root@localhost mnt]# cp -r * /azuo/mnt_fs/操作9,把宿主机host上面linux #make modules_install ARCH=um 生成的文件夹拷贝到/azuo/mnt_fs下去[root@localhost modules]# cp -r /lib/modules/3.5.1 /azuo/mnt_fs/lib/modules/操作10,卸载挂载的文件夹:[root@localhost azuo]# umount mnt_fs/[root@localhost azuo]# umount /mnt

然后我们试试看看这个文件系统好使不,到编译好的um格式的linux内核路径下去操作:

[root@localhost azuo]# cd /um_linux/linux-3.5.1[root@localhost linux-3.5.1]# [root@localhost linux-3.5.1]# dirarch     CREDITS        drivers   include  Kbuild   lib          Makefile         modules.order   README          samples   sound       usr      vmlinux.oblock    crypto         firmware  init     Kconfig  linux        mm               Module.symvers  REPORTING-BUGS  scripts   System.map  virtCOPYING  Documentation  fs        ipc      kernel   MAINTAINERS  modules.builtin  net root_fs         security  tools       vmlinux[root@localhost linux-3.5.1]# ./linux ubda=/azuo/root_fs mem=256m

实际效果如下,相当好使:

------------------------------[root@localhost linux-3.5.1]# ./linux ubda=/azuo/root_fs mem=256mLocating the bottom of the address space ... 0x0Locating the top of the address space ... 0xc0000000Core dump limits :        soft - 0        hard - NONEChecking that ptrace can change system call numbers...OKChecking syscall emulation patch for ptrace...OKChecking advanced syscall emulation patch for ptrace...OKChecking for tmpfs mount on /dev/shm...OKChecking PROT_EXEC mmap in /dev/shm/...OKChecking for the skas3 patch in the host:  - /proc/mm...not found: No such file or directory  - PTRACE_FAULTINFO...not found  - PTRACE_LDT...not foundUML running in SKAS0 modeAdding 12857344 bytes to physical memory to account for exec-shield gapInitializing cgroup subsys cpusetInitializing cgroup subsys cpuLinux version 3.5.1 (root@localhost.localdomain) (gcc version 4.4.6 20120305 (Red Hat 4.4.6-4) (GCC) ) #1 Fri Aug 17 08:52:02 CST 2012Built 1 zonelists in Zone order, mobility grouping on.  Total pages: 68138Kernel command line: ubda=/azuo/root_fs mem=256m root=98:0PID hash table entries: 2048 (order: 1, 8192 bytes)Dentry cache hash table entries: 65536 (order: 6, 262144 bytes)Inode-cache hash table entries: 32768 (order: 5, 131072 bytes)allocated 549400 bytes of page_cgroupplease try 'cgroup_disable=memory' option if you don't want memory cgroupsMemory: 254248k availableNR_IRQS:15Calibrating delay loop... 984.67 BogoMIPS (lpj=4923392)pid_max: default: 32768 minimum: 301Mount-cache hash table entries: 512Initializing cgroup subsys cpuacctInitializing cgroup subsys memoryInitializing cgroup subsys devicesInitializing cgroup subsys freezerChecking for host processor cmov support...YesChecking that host ptys support output SIGIO...YesChecking that host ptys support SIGIO on close...No, enabling workarounddevtmpfs: initializedUsing 2.6 host AIONET: Registered protocol family 16bio: create slab <bio-0> at 0Switching to clocksource itimerNET: Registered protocol family 2IP route cache hash table entries: 4096 (order: 2, 16384 bytes)TCP established hash table entries: 16384 (order: 5, 131072 bytes)TCP bind hash table entries: 16384 (order: 6, 327680 bytes)TCP: Hash tables configured (established 16384 bind 16384)TCP: reno registeredUDP hash table entries: 256 (order: 1, 12288 bytes)UDP-Lite hash table entries: 256 (order: 1, 12288 bytes)NET: Registered protocol family 1mconsole (version 2) initialized on /root/.uml/m30WOZ/mconsoleChecking host MADV_REMOVE support...OKHost TLS support detectedDetected host type: i386 (GDT indexes 6 to 9)Initializing RT-Tester: OKVFS: Disk quotas dquot_6.5.2Dquot-cache hash table entries: 1024 (order 0, 4096 bytes)fuse init (API version 7.19)msgmni has been set to 496io scheduler noop registeredio scheduler deadline registered (default)start plist testend plist testTCP: cubic registeredNET: Registered protocol family 17Initialized stdio console driverConsole initialized on /dev/tty0console [tty0] enabledInitializing software serial port version 1console [mc-1] enabled ubda: unknown partition tablekjournald starting.  Commit interval 5 secondsEXT3-fs (ubda): mounted filesystem with ordered data modeVFS: Mounted root (ext3 filesystem) readonly on device 98:0.devtmpfs: mountedcat: /proc/cmdline: No such file or directory                Welcome to CentOS Starting udev: udev: starting version 147udevd (306): /proc/306/oom_adj is deprecated, please use /proc/306/oom_score_adj instead.line_ioctl: tty0: unknown ioctl: 0x541e[  OK  ]Setting hostname localhost.localdomain:  [  OK  ]Setting up Logical Volume Management:   No volume groups found[  OK  ]Checking filesystemsChecking all file systems.[  OK  ]Remounting root filesystem in read-write mode:  EXT3-fs (ubda): using internal journal[  OK  ]Mounting local filesystems:  [  OK  ]/etc/rc.d/rc.sysinit: line 596: plymouth: command not foundEnabling /etc/fstab swaps:  [  OK  ]/etc/rc.d/rc.sysinit: line 661: plymouth: command not foundEntering non-interactive startupFATAL: Module ipv6 not found.Bringing up loopback interface:  [  OK  ]Bringing up interface eth0:  Device eth0 does not seem to be present, delaying initialization.[FAILED]FATAL: Module ipv6 not found.Mounting other filesystems:  [  OK  ]Retrigger failed udev events[  OK  ]cat: /proc/sys/crypto/fips_enabled: No such file or directory/etc/rc3.d/S55sshd: line 50: [: too many argumentsStarting sshd: [  OK  ]CentOS release 6.2 (Final)Kernel 3.5.1 on an i686localhost login: CentOS release 6.2 (Final)Kernel 3.5.1 on an i686localhost login: rootLast login: Thu Aug 16 20:44:48 on tty0[root@localhost ~]# ------------------------------

下面用gdb调试内核:

linux #gdb ./linux (gdb) handle SIGSEGV pass nostop noprint (gdb) handle SIGUSR1 pass nostop noprint (gdb) b main (gdb) run ubd0=root_fs mem=256m记住上面的两个handle一定要输入的,要不系统会一直卡在启动的地方提示segment fault。

上面启动的都是自己编译的生成文件。下载的直接启动./kernel32-3.5.1(前提是kernel32-3.5.1是下载的文件)

 注意:

补充1、我上面从官网下面下载的是centos 6.2的版本,但是2012 6 18日官网更新到了centos 6.3版本,这个版本根本起不来,只能配置官网编译好的内核使用,自己编译的是不行的。

只能自己拷贝自己主机host根目录下面的lib bin sbin过去到自己做的那个文件系统下面去就是了。

 

[root@zhoutianzuo mnt]# cp -r /lib /azuo/mnt[root@zhoutianzuo mnt]# cp -r /bin /azuo/mnt[root@zhoutianzuo mnt]# cp -r /sbin /azuo/mnt[root@zhoutianzuo mnt]# cp -r /boot /azuo/mnt[root@zhoutianzuo mnt]# cp -r /etc /azuo/mnt[root@zhoutianzuo mnt]# cp -r /sys /azuo/mnt[root@zhoutianzuo mnt]# cp -r /dev /azuo/mnt[root@zhoutianzuo mnt]# mkdir /azuo/mnt/usr[root@zhoutianzuo mnt]# cp -r /usr/bin /azuo/mnt/usr[root@zhoutianzuo mnt]# cp -r /usr/sbin /azuo/mnt/usr[root@zhoutianzuo mnt]# cp -r /usr/lib /azuo/mnt/usr[root@zhoutianzuo mnt]# cp -r /usr/etc /azuo/mnt/usr

 

如果可以还是请使用官网发行是文件系统,拷贝到自己的文件系统,然后,把modules部分拷贝过去。

补充2、今天2012-8-19尝试了使用Centos 5.8,这个上面编译不了内核3.5.2这个版本,所以我尝试了2.6.35.13这个版本,但是这个版本要注意,一定要在编译的内核里面开启EXT4功能的支持。然后使用官网下载的文件系统版本,因为官网的文件系统是ext4格式的。.

 

 

补充3 今天换了虚拟机,Virtualbox 4.2.6,安装系统是CentOS 6.3,下面可以显示系统默认内核版本,

[root@BTazuo linux-2.6.39]# [root@BTazuo linux-2.6.39]# uname -aLinux BTazuo 2.6.32-279.el6.i686 #1 SMP Fri Jun 22 10:59:55 UTC 2012 i686 i686 i386 GNU/Linux[root@BTazuo linux-2.6.39]# 

我这里尝试过3.5.1 3.4.9的内核版本都不行,可行的就是2.6.39.


 

原创粉丝点击