对于任何一个虚拟化平台,总有一个客户机软件和宿主机通信,例如vmware的vmtools。KVM下的就是qemu-guest-agent
因为PVE是基于KVM的,所以客户端依旧是 qemu-guest-agent 。
目录
客户端安装(不安装会提示Guest Agent未运行)
Linux
#for rehat yum install qemu-guest-agent -y
#for debian apt install qemu-guest-agent -y
Windows
直接使用virtio驱动光盘安装。如果是win7/xp需要手动进入光盘,找到agent目录,选择32位和64位就行
安装完成之后,在虚拟机选项里,勾选 QEMU Guest Agent
服务器使用agent
对于安装好agent的vm,在其概况界面,会显示VM的网卡信息。
更加全面的用法如,需要用到qm agent命令 语法如下
qm agent <vmid> <cmd>
这里vmid 就是vm的id啦。cmd就是命令。我这里列出来。
fsfreeze-freeze fsfreeze-status fsfreeze-thaw fstrim #查看ssd——trim get-fsinfo #查看磁盘信息 get-host-name #查看主机名 get-memory-block-info #查看内存块 信息 get-memory-blocks #查看您内存 get-osinfo #查看系统信息 get-time #查看时间 get-timezone #查看时区 get-users #用户 get-vcpus #查看CPU数量 info #查看支持的命令 network-get-interfaces #查看网络 ping #不明 shutdown #关机 suspend-disk #休眠,储存到硬盘 suspend-hybrid #休眠,混合 suspend-ram #挂起/休眠 内存
以上命令均支持TAB键 自动补全。
举个例子
#查看vm的网卡信息 qm agent 101 network-get-interfaces
#返回json [ { "hardware-address" : "00:00:00:00:00:00", "ip-addresses" : [ { "ip-address" : "127.0.0.1", "ip-address-type" : "ipv4", "prefix" : 8 }, { "ip-address" : "::1", "ip-address-type" : "ipv6", "prefix" : 128 } ], "name" : "lo", "statistics" : { "rx-bytes" : 6324, "rx-dropped" : 0, "rx-errs" : 0, "rx-packets" : 84, "tx-bytes" : 6324, "tx-dropped" : 0, "tx-errs" : 0, "tx-packets" : 84 } }, { "hardware-address" : "76:ee:94:38:3a:85", "ip-addresses" : [ { "ip-address" : "10.13.14.100", "ip-address-type" : "ipv4", "prefix" : 24 }, { "ip-address" : "fe80::74ee:94ff:fe38:3a85", "ip-address-type" : "ipv6", "prefix" : 64 } ], "name" : "eth0", "statistics" : { "rx-bytes" : 1324, "rx-dropped" : 0, "rx-errs" : 0, "rx-packets" : 9, "tx-bytes" : 1810, "tx-dropped" : 0, "tx-errs" : 0, "tx-packets" : 17 } } ]查看VM主机名#查看主机名 root@pve:~# qm agent 105 get-host-name { "host-name" : "nginx" }qm guest 命令集合
qm agent就是qm guest。qm guest主要有下面四个
qm guest cmd qm guest exec qm guest exec-status qm guest passwd
1、qm guset cmd
此项等同于qm agent
2、qm guest exec(创建进程)
此项是传递给VM的命令
qm guest exec <vmid> <vmcmd> --<option> <vmid>=vmid <vmcmd>=vm里面的命令。例如ip link /docker ps等等。命令不能有“-”,因为加了“-”,就会设别成后面的<option> <option>=这个是特别操作,有3个 pass-stdin=<0/1> 这个似乎不能用 synchronous=<0/1> 如果是0,则返回pid。如果是1,则返回json格式的输出结果 timeout=<整数数字> 超时时间
举几个例子
1、查看usr目录
qm guest exec 108 ls /usr
返回的是json,会多个N{ "exitcode" : 0, "exited" : 1, "out-data" : "bin\ngames\ninclude\nlib\nlocal\nsbin\nshare\nsrc\n" }如果加上一个 synchronous=0
直接返回了pidroot@pve:/# qm guest exec 108 ls /usr --synchronous=0 { "pid" : 2001 }下载一个文件
###下载百度首页 root@pve:/# qm guest exec 108 wget https://www.baidu.com { "err-data" : "--2020-05-18 19:52:09-- https://www.baidu.com/\nResolving www.baidu.com (www.baidu.com)... 61.135.169.121, 61.135.169.125\nConnecting to www.baidu.com (www.baidu.com)|61.135.169.121|:443... connected.\nHTTP request sent, awaiting response... 200 OK\nLength: 2443 (2.4K) [text/html]\nSaving to: ‘index.html’\n\n 0K .. 100% 258M=0s\n\n2020-05-18 19:52:09 (258 MB/s) - ‘index.html’ saved [2443/2443]\n\n", "exitcode" : 0, "exited" : 1 } ###查看根目录是否有index.html root@pve:/# qm guest exec 108 ls { "exitcode" : 0, "exited" : 1, "out-data" : "bin\nboot\ndev\netc\nhome\nindex.html\ninitrd.img\ninitrd.img.old\nlib\nlib64\nlost+found\nmedia\nmnt\nopt\nproc\nroot\nrun\nsbin\nsnap\nsrv\nsys\ntmp\nusr\nvar\nvmlinuz\nvmlinuz.old\n" }查看nginx是否在运行
qm guest exec 108 ps aux|grep nginx 返回一堆代表在运行,没运行是没有返回的。
3、qm guest exec-status
用法如下 qm guest exec-status <vmid> <pid> 此项可以查看VM的进程PID状态。 比如,我创建一个ping 的进程 qm guest exec 108 ping www.baidu.com --synchronous=0 返回如下。 { "pid" : 2722 } 那么我再通过这个命令,看看这个pid是什么状态 qm guest exec-status 108 2722 返回 { "exited" : 0 } 证明这个进程还在存活。 如果先杀掉这个进程,再执行查看pid,第一次回返回一长串数据 { "exited" : 1, "out-data" : "PING www.a.shifen.com (61.135.169.121) 56(84) bytes of data.\n64 bytes from 61.135.169.121 (61.135.169.121): icmp_seq=1 ttl=55 time=37.9 ms\n64 bytes from 61.135.169.121 (61.135.169.121): icmp_seq=2 ttl=55 time=38.4 ms\n64 bytes from 61.135.169.121 (61.135.169.121): icmp_seq=3 ttl=55 time=38.3 ms\n64 bytes from 61.135.169.121 (61.135.169.121): icmp_seq=4 ttl=55 time=38.4 ms\n64 bytes from 61.135.169.121 (61.135.169.121): icmp_seq=5 ttl=55 time=38.3 ms\n64 bytes from 61.135.169.121 (61.135.169.121): icmp_seq=6 ttl=55 time=38.4 ms\n64 bytes from 61.135.1 ****** } 再次执行qm guest exec-status 108 2722 Agent error: Invalid parameter 'pid' 这是就提示错误了。注意的是。qm guest exec-status这个命令是要和qm guest exec 配合使用。单独使用无效
4、qm guest passwd (重置密码)
此项非常有用
#语法如下 qm guest passwd <vmid> <username> [OPTIONS] #[OPTIONS]只有一个 --crypted=<0/1> 是否加密密码 默认0 实例 qm guest passwd 108 root #直接更改密码 qm guest passwd 108 administrator #适用于更改windows操作系统密码 #至于后面的--crypted= 一般不加,都是加密了的密码