Linux下搭建UML环境
这里要开始搭建UML环境了,但请注意这里的UML并非“统一建模语言”,而是User-Mode-Linux,用户模式Linux,使用它有什么好处呢?如我们前面说到的《【Linux Kernel】添加系统调用》,使用UML后不需要重启机器就可以进行测试难,还有GDB调试等,即很多需要重启或真机调试下的问题都可以在UML下模拟学习。
方法/步骤
这里要开始搭建UML环境了,但请注意这里的UML并非“统一建模语言”,而是User-Mode-Linux,用户模式Linux,使用它有什么好处呢?如我们前面说到的《【Linux Kernel】添加系统调用》,使用UML后不需要重启机器就可以进行测试难,还有GDB调试等,即很多需要重启或真机调试下的问题都可以在UML下模拟学习。那么这一只能在Linux下使用的神奇环境要如何搭建它呢?首先得有一个安装了Linux发行版系统的机器,然后我们再在上面搭建UML环境。
我们使用的主机环境为安装了64位的Ubuntu13.10(3.11.0-12-generic)系统机器,下面是整个UML环境的搭建步骤:
1.下载Linux Kernel源码
访问https://www.kernel.org/网站,看到现在(2014-3-10)最新的版本为3.13.6,那么就下载它了:
cd ~/uml/
wget https://www.kernel.org/pub/linux/kernel/v3.x/linux-3.13.6.tar.xz
2.编译UM配置的Kernel
解压下载的内核源码:tar xvf linux-3.13.6.tar.xz
配置及编译内核源码:
cd linux-3.13.6
make ARCH=um defconfig
生成UM默认的配置文件,运行后有如下信息:
xinu@slam:~/uml/linux-3.13.6$ make ARCH=um defconfig
HOSTCC scripts/basic/fixdep
HOSTCC scripts/kconfig/conf.o
SHIPPED scripts/kconfig/zconf.tab.c
SHIPPED scripts/kconfig/zconf.lex.c
SHIPPED scripts/kconfig/zconf.hash.c
HOSTCC scripts/kconfig/zconf.tab.o
HOSTLD scripts/kconfig/conf
*** Default configuration is based on 'x86_64_defconfig'
#
# configuration written to .config
#
配置文件生成后,就是编译了,执行如下命令:
make ARCH=um
编译完成后会在当前目录下生成linux可执行文件。
3.准备根文件系统
编译完成后,需要一个根文件系统,此时可以从http://fs.devloop.org.uk/下载一个现成的资源,由于看到Ubuntu Saucy对UML的兼容不好(没有命令行界面,即运行后输入无响应),故而下载与Ubuntu同系的Debian Wheezy,下载及解压命令如下:
cd ..
wget http://fs.devloop.org.uk/filesystems/Debian-Wheezy/Debian-Wheezy-AMD64-root_fs.bz2
bunzip2 Debian-Wheezy-AMD64-root_fs.bz2
解压后是名为 Debian-Wheezy-AMD64-root_fs 文件。
4.运行UML
cd linux-3.13.6/
./linux ubda=../ Debian-Wheezy-AMD64-root_fs mem=256m
运行起来后,到最后有如下提示:
Serial line 0 assigned device '/dev/pts/4'
Debian GNU/Linux 7 changeme tty0
changeme login:
此时我们输入root,回车后有如下提示:
Last login: Mon Mar 10 09:14:54 UTC 2014 on tty0
Linux changeme 3.13.6 #3 Mon Mar 10 16:46:54 CST 2014 x86_64
The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.
Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
root@changeme:~#
这就表示UML启动成功了。
当然从上面提供的网站下载的文件系统有些未必匹配使用,我这边测试了还能使用的有busybox的,该包跟我们嵌入式平台平时移植的Busybox一致,不过版本会旧些,我们还是继续先使用Debian的。
先到这,我们把UML关机先,输入halt命令,提示如下:
root@changeme:~# halt
Broadcast message from root@changeme (tty0) (Mon Mar 10 09:23:30 2014):
The system is going down for system halt NOW!
INIT: Switching to runlevel: 0
INIT: Sending processes the TERM signal
[info] Using makefile-style concurrent boot in runlevel 0.
[ ok ] Asking all remaining processes to terminate...done.
[ ok ] All processes ended within 1 seconds...done.
[ ok ] Stopping enhanced syslogd: rsyslogd.
[info] Saving the system clock.
hwclock: Cannot access the Hardware Clock via any known method.
hwclock: Use the --debug option to see the details of our search for an access method.
[ ok ] Deconfiguring network interfaces...done.
[ ok ] Unmounting temporary filesystems...done.
[ ok ] Deactivating swap...done.
EXT4-fs (ubda): re-mounted. Opts: (null)
[info] Will now halt.
reboot: System halted
xinu@slam:~/uml/linux-3.13.6$
至此,UML环境搭建并测试成功了。