交叉编译调试qemu_guest_agent
Winodws版本
编译环境Fedora23
下载VSS SDK的setup.exe
提取VSS SDK头文件
将下面的代码保存成extract-vsssdk-headers.sh脚本,然后放入setup.exe所在的/home/fedora/Downloads
目录,然后执行./extract-vsssdk-headers.sh setup.exe
#! /bin/bash
# extract-vsssdk-headers
# Author: Paolo Bonzini <pbonzini@redhat.com>
set -e
if test $# != 1 || ! test -f "$1"; then
echo 'Usage: extract-vsssdk-headers /path/to/setup.exe' >&2
exit 1
fi
if ! command -v msiextract > /dev/null; then
echo 'msiextract not found. Please install msitools.' >&2
exit 1
fi
if test -e inc; then
echo '"inc" already exists.' >&2
exit 1
fi
# Extract .MSI file in the .exe, looking for the OLE compound
# document signature. Extra data at the end does not matter.
export LC_ALL=C
MAGIC=$'\xd0\xcf\x11\xe0\xa1\xb1\x1a\xe1'
offset=$(grep -abom1 "$MAGIC" "$1" | sed -n 's/:/\n/; P')
tmpdir=$(mktemp -d)
trap 'rm -fr -- "$tmpdir" vsssdk.msi' EXIT HUP INT QUIT ALRM TERM
tail -c +$(($offset+1)) -- "$1" > vsssdk.msi
# Now extract the files.
msiextract -C $tmpdir vsssdk.msi
mv "$tmpdir/Program Files/Microsoft/VSSSDK72/inc" inc
echo 'Extracted SDK headers into "inc" directory.'
exit 0
安装交叉编译依赖
下载qemu源文件
安装依赖
- 32位环境
[fedora@bogon ~]$ rpm -qa|grep mingw32
mingw32-gcc-c++-4.9.2-1.fc21.x86_64
mingw32-SDL-1.2.15-5.fc21.noarch
mingw32-termcap-1.3.1-17.fc21.noarch
mingw32-filesystem-100-1.fc21.noarch
mingw32-glib2-static-2.42.2-1.fc21.noarch
mingw32-winpthreads-4.0.1-1.fc21.noarch
mingw32-binutils-2.25-1.fc21.x86_64
mingw32-win-iconv-0.0.6-2.fc21.noarch
mingw32-gettext-0.18.3.2-2.fc21.noarch
mingw32-gmp-6.0.0-2.fc21.noarch
mingw32-zlib-static-1.2.8-3.fc21.noarch
mingw32-headers-4.0.1-1.fc21.noarch
mingw32-nsis-2.46-13.fc21.x86_64
mingw32-gcc-4.9.2-1.fc21.x86_64
mingw32-zlib-1.2.8-3.fc21.noarch
mingw32-winpthreads-static-4.0.1-1.fc21.noarch
mingw32-gettext-static-0.18.3.2-2.fc21.noarch
mingw32-cpp-4.9.2-1.fc21.x86_64
mingw32-libffi-3.0.13-5.fc21.noarch
mingw32-glib2-2.42.2-1.fc21.noarch
mingw32-crt-4.0.1-1.fc21.noarch
mingw32-pkg-config-0.28-4.fc21.x86_64
mingw32-pixman-0.32.6-1.fc21.noarch
- 64位环境
[fedora@bogon ~]$ rpm -qa|grep mingw64
mingw64-binutils-2.25-1.fc21.x86_64
mingw64-glib2-2.42.2-1.fc21.noarch
mingw64-crt-4.0.1-1.fc21.noarch
mingw64-win-iconv-0.0.6-2.fc21.noarch
mingw64-gettext-static-0.18.3.2-2.fc21.noarch
mingw64-winpthreads-4.0.1-1.fc21.noarch
mingw64-gcc-4.9.2-1.fc21.x86_64
mingw64-termcap-1.3.1-17.fc21.noarch
mingw64-glib2-static-2.42.2-1.fc21.noarch
mingw64-headers-4.0.1-1.fc21.noarch
mingw64-gcc-c++-4.9.2-1.fc21.x86_64
mingw64-zlib-1.2.8-3.fc21.noarch
mingw64-gettext-0.18.3.2-2.fc21.noarch
mingw64-filesystem-100-1.fc21.noarch
mingw64-pkg-config-0.28-4.fc21.x86_64
mingw64-libffi-3.0.13-5.fc21.noarch
mingw64-zlib-static-1.2.8-3.fc21.noarch
mingw64-cpp-4.9.2-1.fc21.x86_64
mingw64-winpthreads-static-4.0.1-1.fc21.noarch
编译qemu-ga.exe
- 32位编译指令
cd /home/fedora/Downloads/qemu-2.6.0 && ./configure --enable-guest-agent --cross-prefix=i686-w64-mingw32- --with-vss-sdk=/home/fedora/Downloads && make msi
- 64位编译指令
cd /home/fedora/Downloads/qemu-2.6.0 && ./configure --enable-guest-agent --cross-prefix=x86_64-w64-mingw32- --with-vss-sdk=/home/fedora/Downloads && make msi
修改虚拟机配置
使用virsh edit 域名
编辑配置文件,添加如下内容
<channel type='unix'>
<source mode='bind' path='/var/lib/libvirt/qemu/f16x86_64.agent'/>
<target type='virtio' name='org.qemu.guest_agent.0'/>
</channel>
其中path
对应的是主机上的文件,虚拟机启动时候自动创建,用于和虚拟机进行通信,文件名不能和其他虚拟机重复,但name为固定值不能修改
安装qemu-ga.exe
qemu-ga.exe只能安装到win7以上版本
标准方法
直接双击msi完成安装
手动手法
- 安装virtio
- 在虚拟机中创建C:\Program Files\QEMU\run目录
- 拷贝qemu-ga.exe、qga-vss.dll、qga-vss.tlb文件到run目录
- 使用管理员身份在run启动一个控制台,执行
qemu-ga.exe –l "C:\Program Files\QEMU\run\log.txt" –s install
命令,出现如下字样表示成功
Registering QEMU Guest Agent VSS Provider:
C:\Program Files\QEMU\run\qga-vss.dll
C:\Program Files\QEMU\run\qga-vss.tlb
Service was installed successfully
- 执行
net start qemu-ga
启动服务,出现如下字样表示成功
QEMU Guest Agent 服务正在启动.
QEMU Guest Agent 服务已经启动成功.
Linux版本
编译环境Fedora23
下载qemu源文件
编译qemu-ga
cd /home/fedora/Downloads/qemu-2.6.0 && ./configure --enable-guest-agent && make qemu-ga
修改虚拟机配置
使用virsh edit 域名
编辑配置文件,添加如下内容
<channel type='unix'>
<source mode='bind' path='/var/lib/libvirt/qemu/f16x86_64.agent'/>
<target type='virtio' name='org.qemu.guest_agent.0'/>
</channel>
其中path
对应的是主机上的文件,虚拟机启动时候自动创建,用于和虚拟机进行通信,文件名不能和其他虚拟机重复,但name为固定值不能修改
安装qemu-ga
以下操作在虚拟机中执行
- 安装virtio
- 拷贝qemu-ga到/usr/bin目录
- 拷贝/home/fedora/Downloads/qemu-2.6.0/scripts/qemu-guest-agent/目录下的fsfreeze-hook文件和fsfreeze-hook.d目录到/usr/libexec/qemu-ga/目录
- 将下面内容保存为qemu-ga文件,并放入/etc/sysconfig/目录
# Transport method may be one of following:
# * unix-listen
# * virtio-serial
# * isa-serial
# Default: virtio-serial
TRANSPORT_METHOD="virtio-serial"
# You also can override the device/socket path
# Default: /dev/virtio-ports/org.qemu.guest_agent.0
DEVPATH="/dev/virtio-ports/org.qemu.guest_agent.0"
# If logfile is unset it defaults to stderr but the daemon
# function of init script redirects stderr to /dev/null
LOGFILE="/var/log/qemu-ga/qemu-ga.log"
# Override pidfile name
# Default: /var/run/qemu-ga.pid
PIDFILE="/var/run/qemu-ga.pid"
# SELinux note:
# About guest arbitrary file read/write
#
# A new selinux policy is introduced on RHEL-6.4 to deny qemu-ga to
# read/write arbitrary guest files except the device file used to talk
# with host processes, LOGFILE and PIDFILE.
#
# You can disable this policy by "restorecon -R -v /usr/bin/qemu-ga"
# Comma-separated blacklist of RPCs to disable or empty list to enable all
# Tip: You can get the list of RPC commands using `qemu-ga --blacklist ?`
# Default: blank list to enable all RPCs
# Note: There should be no spaces between commas and commands in the blacklist
BLACKLIST_RPC="guest-file-open,guest-file-close,guest-file-read,guest-file-write,guest-file-seek,guest-file-flush"
# Enable fsfreeze hook. See the --fsfreeze-hook option in "qemu-ga --help".
FSFREEZE_HOOK_ENABLE=0
# Set the option argument for --fsfreeze-hook, which is the full pathname of
# the hook script. An empty or unset value designates the default script
# (invoke "qemu-ga --help" to interrogate).
#
# This setting (independently of value) takes effect only when
# FSFREEZE_HOOK_ENABLE is nonzero.
FSFREEZE_HOOK_PATHNAME=/usr/libexec/qemu-ga/fsfreeze-hook
- 将下面内容保存为qemu-ga文件,并放入/etc/rc.d/init.d/目录
#! /bin/sh
#
# qemu-ga Start/Stop qemu-ga daemon
#
# chkconfig: 345 30 30
# description: qemu guest agent daemon
# processname: qemu-ga
# Source function library.
. /etc/init.d/functions
# Check that we're a privileged user
[ `id -u` = 0 ] || exit 4
[ -f /etc/sysconfig/qemu-ga ] && . /etc/sysconfig/qemu-ga
prog="qemu-ga"
RETVAL=0
lockfile=/var/lock/subsys/$prog
start() {
# Check if binary is installed & executable
[ -f /usr/bin/$prog ] && [ -x /usr/bin/$prog ] || exit 5
# Check the device/path exists
[ -n "$DEVPATH" ] && [ -e "$DEVPATH" ] || exit 6
echo -n $"Starting $prog: "
ARGS="--daemonize"
[ ! -z "$TRANSPORT_METHOD" ] && ARGS="$ARGS --method $TRANSPORT_METHOD"
[ ! -z "$DEVPATH" ] && ARGS="$ARGS --path $DEVPATH"
[ ! -z "$LOGFILE" ] && ARGS="$ARGS --logfile $LOGFILE"
[ ! -z "$PIDFILE" ] && ARGS="$ARGS --pidfile $PIDFILE"
[ ! -z "$BLACKLIST_RPC" ] && ARGS="$ARGS --blacklist $BLACKLIST_RPC"
if [ "$FSFREEZE_HOOK_ENABLE" -ne 0 ] 2>/dev/null; then
ARGS="$ARGS --fsfreeze-hook"
if [ -n "$FSFREEZE_HOOK_PATHNAME" ]; then
ARGS="$ARGS=$FSFREEZE_HOOK_PATHNAME"
fi
fi
daemon /usr/bin/$prog $ARGS
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && touch $lockfile
return $RETVAL
}
stop() {
echo -n $"Stopping $prog: "
killproc $prog
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && rm -f $lockfile
return $RETVAL
}
restart() {
stop
start
}
# See how we were called.
case "$1" in
start)
start
;;
stop)
stop
;;
status)
status $prog
;;
restart|reload|force-reload)
restart
;;
condrestart|try-restart)
[ -f $lockfile ] && restart || :
;;
*)
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload}"
exit 2
esac
exit $?
- 执行
service qemu-ga start
命令启动服务 - 执行
chkconfig qemu-ga on
命令设置开机启动
Solaris版本
编译环境Solaris 10
下载qemu源文件
需要在solaris系统中进行解压缩,拷贝进去会出现文件名全部变成小写的情况
设置编译环境
步骤 | 说明 | 命令 |
---|---|---|
安装pkgutil | 一种自动化安装工具,类似CentOS的yum | pkgadd -d http://get.opencsw.org/now |
设置bash环境 | 自动tab补齐 | 编辑/etc/passwd的root用户的/sbin/sh为/bin/bash |
设置编译bash环境 | configure使用的bash | 将系统自带的/usr/bin/sh保存为/usr/bin/sh.bak,然后建立/usr/bin/sh软链接到/usr/bin/bash |
扩展PATH路径 | 设置pkgutil的可执行路径 | 拷贝/etc/skel/local.profile为/.profile,然后修改PATH=/usr/bin:/usr/local/bin:/usr/ucb:/etc:/usr/sbin:/opt/csw/bin:/usr/ccs/bin:/usr/sfw/bin:.,最后添加export PS1='\u:\w#' |
安装gcc | 安装编译器 | pkgutil -i gcc5core |
安装glib | 安装编译器 | pkgutil -i glib2_devel_stub |
编译qemu-ga
./configure --cc=gcc --install=/usr/ucb/install --target-list= --cpu= --enable-guest-agent --without-pixman --disable-tools --disable-system --disable-fdt && gmake qemu-ga
测试
在主机上执行virsh qemu-agent-command domain_name --cmd '{"execute":"guest-fsfreeze-status"}'
命令测试虚拟机中的qemu-ga
运行是否正常
调试
Winodws版本
-
安装mingw32文件系统
[fedora@fedora ~]$ sudo yum -y install mingw32-filesystem.noarch
-
安装gdb
[fedora@fedora ~]$ sudo yum -y install mingw32-gdb.noarch
-
到
/usr/i686-w64-mingw32/sys-root/mingw/bin/
目录下提取gdb相关文件gdb.exe gdbserver.exe libexpat-1.dll zlib1.dll
-
将gdb相关文件放入qemu-ga.exe所在目录
-
到服务列表关闭已经启动的qemu相关服务,否则调试的时候会提示
failed to initialize guest agent channel
-
用管理员身份启动cmd,然后切换到qemu-ga.exe所在目录,使用
gdb.exe qemu-ga.exe
进行调试 -
先执行
run
待程序能正常运行后使用ctrl+c
暂停程序设置断点,由于没法加载源文件,只能去编译端查看要设置的断点位置,然后使用break filename:line
的方式设置断点 -
或者使用
gdbserver.exe 0.0.0.0:2345 qemu-ga.exe
开启远程调试,然后在编译端使用gdb qemu-ga.exe
开启调试,然后使用target remote guest_ip:2345
命令连接远程调试,这个的好处在于能加载编译端本地的源文件