当前位置:首页 > linux教程 > 列表

通过网络连接模拟处理器QEMU的monitor

发布:smiling 来源: PHP粉丝网  添加日期:2015-04-21 14:16:38 浏览: 评论:0 

QEMU是一套开源的模拟处理器,在GNU/Linux平台上使用广泛,本文我们将通过网络连接QEMU的监控器monitor.

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

[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的选项,做如下说明:

  1. tcp – raw tcp sockets  #下面第2点,我的举例是RAW TCP socket 
  2. 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 
  3. 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 “::” . 
  4. 4444 – port number to listen on. 
  5. server – listening in server mode 
  6. 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上,代码如下:

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

2.QEMU monitor支持RAW socket的远程访问,代码如下:

[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,代码如下:

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

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

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

上面那个连接monitor的socket程序示例为:

https://github.com/smilejay/c-cpp/blob/master/c2013/socket_qemu_monitor_client.c

代码如下:

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

Tags: QEMU monitor 模拟处理器

分享到: