通过网络连接到QEMU monitor

本文介绍了两种远程访问QEMU监控器的方法:通过telnet协议和RAW sockets,并提供了使用示例及一个C语言客户端程序。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在我的KVM书中,关于QEMU监控器(monitor)的使用还是讲了不少东西的,不过,翻看以前收藏的一篇文章,发现其中提到的通过网络访问QEMU monitor的方法是没有在书中提及的。所以,在这里简单介绍一下吧,也是给自己留个记忆。(如果–仅仅是如果,有再版的话,可以考虑加上一两页来讲个东东。)

1. QEMU monitor支持远程telnet访问:

View Code BASH
1
[root@kvm-host ~]# qemu-system-x86_64 -enable-kvm -smp 2 -m 1024 vm2.img -monitor telnet:10.1.77.82:4444,server,nowait

关于这里-monitor的选项,做如下说明:

View Code BASH
1
2
3
4
5
6
    tcp – raw tcp sockets  #下面第2点,我的举例是RAW TCP socket
    telnet – the telnet protocol is used instead of raw tcp sockets. This is the preferred option over tcp as you can break out of the monitor using Ctrl-] then typing quit. You can’t break out of the monitor like this after connecting with the raw socket option
    10.1.77.82 – Listen on this host/IP only. You can use 127.0.0.1 if you want to only allow connections locally. If you want to listen on any ip address on the server, just leave this blank so you end up with two consecutive colons ie “::” .
    4444 – port number to listen on.
    server – listening in server mode
    nowait – qemu will wait for a client socket application to connect to the port before continuing unless this option is used. In most cases you’ll want to use the nowait option.


通过telnet连接到远程的QEMU monitor上:

View Code BASH
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
jay@jay-linux:~$ telnet 10.1.77.82 4444
Trying 10.1.77.82...
Connected to 10.1.77.82.
Escape character is '^]'.
QEMU 1.7.50 monitor - type 'help' for more information
(qemu) info kvm
kvm support: enabled
(qemu) info status
VM status: running
(qemu) help migrate
migrate [-d] [-b] [-i] uri -- migrate to URI (using -d to not wait for completion)
                         -b for migration without shared storage with full copy of disk
                         -i for migration without shared storage with incremental copy of disk (base image shared between src and destination)
(qemu) ctrl + ] (断掉telnet连接)
telnet> quit      (退出telnet)
Connection closed.

2. QEMU monitor支持RAW socket的远程访问:

View Code BASH
1
[root@kvm-host ~]# qemu-system-x86_64 -enable-kvm -smp 2 -m 1024 vm2.img -monitor tcp:10.1.77.82:4444,server,nowait

可以使用netcat去连接这个socket:

View Code BASH
1
2
3
4
5
6
jay@jay-linux:~$ nc 10.1.77.82 4444
QEMU 1.7.50 monitor - type 'help' for more information
(qemu) info kvm
info kvm
kvm support: enabled
(qemu) ^C

自己写了个socket程序示例如连接monitor,效果如下:

View Code BASH
1
2
3
4
5
6
7
8
jay@jay-linux:~/workspace/c-cpp.git/c2013$ ./monitor_client
sending command 'info kvm
' to remote qemu monitor
output from qemu monitor: QEMU 1.7.50 monitor - type 'help' for more information
 ��,�
output from qemu monitor: (qemu) info kvm
kvm support: enabled
(qemu)

上面那个连接monitor的socket程序示例为:
https://github.com/smilejay/c-cpp/blob/master/c2013/socket_qemu_monitor_client.c

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/* socket_qemu_monitor_clinet.c
* Connect to remote QEMU monitor socket and sending a command to the monitor.
* author: Jay <smile665@gmail.com>
*/
 
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
 
int main(int argc, char *argv[])
{
        int sockfd;
        int len;
        struct sockaddr_in address;
        int result;
        char *cmd = "info kvm\n";
        char output[200];
 
        /* Create a socket for the client. */
        sockfd = socket(AF_INET, SOCK_STREAM, 0);
 
        /* Name the socket, as agreed with the server. */
        address.sin_family = AF_INET;
        address.sin_addr.s_addr = inet_addr("10.1.77.82");
        address.sin_port = htons(4444);
        len = sizeof(address);
 
        /* Now connect our socket to the server's socket. */
        result = connect(sockfd, (struct sockaddr *)&address, len);
 
        if(result == -1) {
                perror("oops: socket connetion failed.");
                exit(1);
        }
 
        /* We can now read/write via sockfd. */
        printf("sending command '%s' to remote qemu monitor\n", cmd);
        write(sockfd, cmd, strlen(cmd));
        read(sockfd, output, sizeof(output));
        printf("output from qemu monitor: %s\n", output);
        read(sockfd, output, sizeof(output));
        printf("output from qemu monitor: %s\n", output);
        close(sockfd);
        exit(0);
}

参考资料:

http://www.linux-kvm.com/content/two-ways-access-your-virtual-machine-monitor-across-network

标签:  KVMLinuxQEMUVirtualization
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值