Qemu: User mode emulation and Full system emulation
转载: https://wiki.edubuntu.org/UbuntuDevelopment/Ports
QEMU
QEMU is a processor emulator and supports emulation of ARM, PowerPC, SPARC, x86, x86-64 and more.
QEMU has two operating modes:
- User mode emulation: QEMU can launch Linux processes compiled for one CPU on another CPU, translating syscalls on the fly.
- Full system emulation: QEMU emulates a full system (virtual machine), including a processor and various peripherals such as disk, ethernet controller etc.
User mode emulation and binfmt_misc
This QEMU mode is faster than full system emulation, but is not a perfect abstraction. For instance, if a program reads /proc/cpuinfo, the contents will be returned by the host kernel and so will describe the host CPU instead of the emulated CPU. Also, QEMU's emulation does not cover all syscalls so it might result in debug output like:
qemu: Unsupported syscall:
Which means that QEMU does not know how to emulate the guest syscall 335 (sys_pselect6). Worse, QEMU might emulate syscalls which are actually unimplemented in the target architecture, causing the emulated program to believe the target architecture is more capable than it really is.
To use QEMU syscall emulation, you can invoke qemu-''cpu'' binaries followed by the command you'd like to run, e.g. qemu-arm; unfortunately, this is quite limited because you may only run static binaries like this, as the shared binaries/shared libraries would be under a different path than the ones these were compiled with. For instance, in this interactive session we are at the top of an armel rootfs and we try running bin/ls with qemu-arm on an amd64 host:
% file bin/ls
bin/ls: ELF -bit LSB executable, ARM, version (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6., stripped % qemu-arm bin/ls
/lib/ld-linux.so.: No such file or directory % qemu-arm lib/ld-linux.so. bin/ls
bin/ls: error while loading shared libraries: librt.so.: wrong ELF class: ELFCLASS64 % qemu-arm lib/ld-linux.so. --library-path lib bin/ls
bin dev home lost+found mnt proc sbin srv tmp var
boot etc lib media opt root selinux sys usr
Worse, this doesn't propage to subprocesses, so if you try to run a shell:
% qemu-arm lib/ld-linux.so. --library-path lib bin/bash
$ qemu-arm bin/ls
/lib/ld-linux.so.: No such file or directory
This makes it impractical to call qemu-arm by hand. However, thanks to a Linux module called binfmt_misc, it's possible to run any executable file with a specific filename or specific contents with a configurable interpreter. The qemu-kvm-extras-static package in Ubuntu 10.04 and later registers QEMU interpreter for the binary patterns of binaries it can emulate with binfmt_misc; this means it's not needed to prefix commands with qemu-arm anymore:
% lib/ld-linux.so. --library-path lib bin/ls
bin dev home lost+found mnt proc sbin srv tmp var
boot etc lib media opt root selinux sys usr
This is still impractical with subcommands and even more so in chroots since qemu-arm is linked to amd64 shared libraries and would need /lib/ld-linux.so.2 for amd64 in the chroot:
% sudo cp /usr/bin/qemu-arm usr/bin
% sudo chroot . /bin/bash
chroot: cannot run command `/bin/bash': No such file or directory
% lib/ld-linux.so. --library-path lib bin/bash
$ bin/ls
/lib/ld-linux.so.: No such file or directory
But the qemu-kvm-extras-static package, as it name implies, provides static versions of qemu-''cpu'' interpreters, for instance qemu-arm-static. These work exactly like their shared equivalents, but as soon as they are copied in a rootfs tree, it becomes possible to chroot into it (without the need for a host ld-linux dynamic loader, or the host shared libraries):
% sudo cp /usr/bin/qemu-arm-static usr/bin/qemu-arm-static
% sudo chroot . /bin/bash
# ls
bin dev home lost+found mnt proc sbin srv tmp var
boot etc lib media opt root selinux sys usr
Such a chroot can be created with the qemu-debootstrap command (from the qemu-kvm-extras-static package) which behaves like debootstrap, but copies a static qemu interpreter in the chroot as well.
This chroot should behave mostly like a regular chroot, with the associated drawbacks (no isolation as in virtual machines) and the limitations of qemu syscall emulation.
One may combine syscall emulation with some tools like pbuilder or sbuild; read on for specific instructions for each tool.
In summary, user mode emulation is a nice mode when it works and should be preferred when speed matters, but full system emulation mode should be used for a more complete emulation.
Full system emulation
This QEMU mode emulates a virtual machine with a configurable CPU, video card, memory size and mode. It is much slower than user mode emulation since the target kernel is emulated, as well as device input/output, interrupts etc. However, it provides a much better emulation for guest programs and isolates from the host. It should not be considered a secure sandbox though.
Full system emulation should be preferred to run programs like gdb, or to test a real installed system perhaps with graphical apps, or running an OpenSSH server.
There are various ways to create a QEMU virtual machine.
For ARM, the currently supported methods are:
- using the "versatile" netboot images of the alternate installer; see below
using "rootstock"; see ARM/RootfsFromScratch
by hand, using debootstrap; see ARM/RootfsFromScratch/QemuDebootstrap
To install Ubuntu on ARM using the alternate installer, create a qemu harddisk with:
qemu-img create -f qcow2 sda.qcow2 16G
Next, download the "versatile" netboot images at http://ports.ubuntu.com/ubuntu-ports/dists/lucid/main/installer-armel/current/images/versatile/netboot/ and start the installer with for instance:
qemu-system-arm -M versatilepb -m -cpu cortex-a8 -kernel vmlinuz -initrd initrd.gz -hda sda.qcow2 -append "mem=256M"
pbuilder and QEMU syscall emulation
To create a pbuilder environment using QEMU in syscall emulation mode to build packages is relatively straightforward:
% sudo pbuilder --create --basetgz /var/cache/pbuilder/base-armel.tgz --debootstrap qemu-debootstrap --mirror http://ports.ubuntu.com/ubuntu-ports/ --distribution lucid --architecture armel
The pbuilder-dist script (in the ubuntu-dev-tools package) is also aware of qemu-debootstrap and will just do the right thing if you select an architecture which requires qemu emulation.
schroot/sbuild and QEMU syscall emulation
To create schroots using QEMU in syscall emulation mode is simiarly straightforward, using the mk-sbuild script (in the ubuntu-dev-tools package):
$ mk-sbuild --arch=powerpc lucid
One can use this environment as a chroot environment, including X forwarding with:
$ schroot -p -c lucid-powerpc
Running command-line programs will work normally, and launching X clients will transpaently forward to the host X server.
One can also use this environment to build packages with:
$ sbuild -d lucid-powerpc foo.dsc
By default, schroot environments are snapshots, with all changes destroyed on exit. To modify the base source, use the following:
$ sudo schroot -c lucid-powerpc-source -u root
(lucid-powerpc-source) % apt-get update
(lucid-powerpc-source) % apt-get dist-upgrade
(lucid-powerpc-source) % exit
qemubuilder
qemubuilder is a pbuilder mode using QEMU as its backend; it launches QEMU in machine emulation mode and builds the package in the virtual machine. The Debian wiki provides instructions for various architectures at http://wiki.debian.org/qemubuilder and Nikita V. Youshchenko provides some ARM-specific instructions at http://yoush.homelinux.org:8079/tech/setting-up-armel-qemubuilder with custom kernels. The Ubuntu 10.04 versatile kernels should work fine for this mode and are available at http://ports.ubuntu.com/ubuntu-ports/dists/lucid/main/installer-armel/current/images/versatile/netboot/ but you don't need the initrd part of them.
Cross-compilation
Specific software such as the kernel or bootloaders are easily cross-compiled; this works as expected under Ubuntu, it's a matter of making sure the relevant cross-compiler is in the $PATH, either by installing it from packages which ship it in /usr/bin, or by installing it to /usr/local/bin, or by installing it in one's $HOME/bin directory and appending ~/bin to the $PATH.
Some build systems will autodetect cross-compilation when passed host and target architectures, but others might expect the cross-compiler to be set in the CC, LD etc. environment variables.
Kernel cross-compilation
The Linux kernel is of course cross-compilation friendly; you can cross-compile the Linux kernel by setting the architecture and cross-tools prefix when invoking make, for instance if your cross-tools are named arm-linux-gnueabi-gcc, arm-linux-gnueabi-ld etc. use:
make ARCH=arm CROSS_COMPILE=arm-linux-gnueabi- menuconfig
make ARCH=arm CROSS_COMPILE=arm-linux-gnueabi- zImage
Cross-toolchains are not currently available from official Ubuntu repositories (but are in the works); in the mean time, you might find some of the toolchains below useful:
this cross-toolchain was created by Matthias Klose and used for the initial chromeos-build cross-builds in i386 chroots: http://people.canonical.com/~cjwatson/armel-cross/
CodeSourcery also provides cross-toolchains, either with or without a target libc, see their products pages: http://www.codesourcery.com/sgpp/editions.html
LoïcMinier is working on cross-toolchain packages in his PPA: https://launchpad.net/~lool/+archive/ppa
Qemu: User mode emulation and Full system emulation的更多相关文章
- Qemu 有用的链接
Qemu下载和编译 Download https://en.wikibooks.org/wiki/QEMU/Linux https://en.wikibooks.org/wiki/QEMU/Insta ...
- 【转】What's the difference between simulation and emulation
摘要:这2个单词 还是用英文解释,比较准确.按我的理解:simulation就是模拟,可以做些改变. emulation是仿真,是按照原来的样子进行部署,不可以改变. Yes, the concept ...
- 使用QEMU创建虚拟机
下载安装: wget http://wiki.qemu-project.org/download/qemu-2.0.0.tar.bz2 tar xjvf qemu- ./configure --ena ...
- QEMU, a Fast and Portable Dynamic Translator-Fabrice Bellard-翻译
Abstract We present the internals of QEMU, a fast machine emulator using an original portable dynami ...
- [qemu] qemu旧的net参数已经不再可用了,新的这样用。
老的命令: /root/BUILD_qemu/bin/qemu-system-x86_64 -enable-kvm \ -m 2G \ -cpu Nehalem -smp cores=,threads ...
- QEMU, a Fast and Portable Dynamic Translator
AbstractWe present the internals of QEMU, a fast machine emulator using an original portable dynamic ...
- Flexible implementation of a system management mode (SMM) in a processor
A system management mode (SMM) of operating a processor includes only a basic set of hardwired hooks ...
- [虚拟化/云][全栈demo] 为qemu增加一个PCI的watchdog外设(四)
通过前面的操作,我们已经可以创建一个带有我们自己的PCI的watchdog外设qemu 虚拟机了. 目的: 1. 了解我们的外设情况. 2. 为在guest中开发我们自己的linux PCI驱动程序做 ...
- 虚拟机迁移(QEMU动态迁移,Libvirt动(静)态迁移)
动静态迁移的原理 静态迁移是指在虚拟机关闭或暂停的情况下,将源宿主机上虚拟机的磁盘文件和配置文件拷贝到目标宿主机上.这种方式需要显式的停止虚拟机运行,对服务可用性要求高的需求不合适. *** 动态迁移 ...
随机推荐
- Linux下批量解压.Z格式文件
下面的代码演示如何将当前目录下sj目录下的所有.Z格式文件解压到sj_result目录下. 代码示例: for file in `ls ./sj` do prefix=${file%.*} echo ...
- ES SQL使用说明文档
ES SQL使用说明文档 一.Elasticsearch术语介绍 l 接近实时(NRT): Elasticsearch 是一个接近实时的搜索平台.这意味着,从索引一个文档直到这个文档能够被搜索到有一 ...
- 《CNCF × Alibaba云原生技术公开课》知识点自测(一):第一堂“云原生”课
(单选)1.容器启动后,我会时常 SSH 进入到容器里然后写很多文件.请问这破坏了云原生理念了吗? A. 否 B. 是 (单选)2.云原生架构必须选型 Kubernetes 方案. A. 否 B ...
- 【miscellaneous】gstreamer构建的简单方法
在博文"Gstreamer在Ubuntu上的安装和MP3的播放"中,写了在ubuntu上从头到尾构建gstreamer的详细过程,那是我在一次小项目培训中和队友一起努力了将近一周的 ...
- Android_7.1.1_r6源码编译
上篇文章讲述了如何下载Android源码,在篇文章就来说一说Android源码编译.其实一般来说如果修改的软件和底层没什么关系,直接提取相应的源代码到Android Studio编译就可以了,如果是与 ...
- dubbo40道面试题
1.Dubbo是什么? Dubbo是阿里巴巴开源的基于 Java 的高性能 RPC 分布式服务框架,现已成为 Apache 基金会孵化项目. 面试官问你如果这个都不清楚,那下面的就没必要问了.官网:h ...
- Python豆瓣源镜像
pip install pymysql -i http://pypi.douban.com/simple --trusted-host pypi.douban.com
- python中浅拷贝和深拷贝分析
首先,我们知道Python3中,有6个标准的数据类型,他们又分为可以变和不可变.不可变:Number(数字).String(字符串).Tuple(元组).可以变:List(列表).Dictionary ...
- js — 字符串
目录 1. 拼接字符串 2. 获取字符的方法 3. 字符串操作方法(切片) 4. 字符串位置方法 - 索引 5. trim()方法 6. 字符串大小写转换方法 字符串 typeof 用于校验当前变量的 ...
- spring cloud微服务实践七
在spring cloud 2.x以后,由于zuul一直停滞在1.x版本,所以spring官方就自己开发了一个项目 Spring Cloud Gateway.作为spring cloud微服务的网关组 ...