Skip to content

Instantly share code, notes, and snippets.

@leapar
Last active June 6, 2017 05:08
Show Gist options
  • Save leapar/5fe36e6adbde5967e2b7d744e366a6df to your computer and use it in GitHub Desktop.
Save leapar/5fe36e6adbde5967e2b7d744e366a6df to your computer and use it in GitHub Desktop.

抓取代码

https://github.com/leapar/libvirt-metrics

连接

virsh -c qemu+ssh://[email protected]/system

vbox:///session                      (local access to per-user instance)
vbox+unix:///session                 (local access to per-user instance)
vbox+tcp://[email protected]/session  (remote access, SASl/Kerberos)
vbox+ssh://[email protected]/session  (remote access, SSH tunnelled)

virsh -c qemu+ssh://[email protected]/system?socket=/var/run/libvirt/libvirt-sock

获得域设备块状态

domblkstat 获得域设备块状态

 rd_req 498701
 rd_bytes 138012157092
 wr_req 1802345
 wr_bytes 56957160448
 flush_operations 191798
 rd_total_times 7565196324843
 wr_total_times 47947778870310
 flush_total_times 3151369647781

获得域网络接口状态

domifstat 获得域网络接口状态

 domifstat bg-119 vnet0
vnet0 rx_bytes 146557889315
vnet0 rx_packets 1623962729
vnet0 rx_errs 0
vnet0 rx_drop 0
vnet0 tx_bytes 4552180619130
vnet0 tx_packets 288549916
vnet0 tx_errs 0
vnet0 tx_drop 0

域信息

dominfo 域信息

dominfo bg-119
Id:             1
名称:       bg-119
UUID:           bb8e2dab-111f-4151-a664-924ec2f7376b
OS 类型:    hvm
状态:       running
CPU:          4
CPU 时间:   188749.6s
最大内存: 16777216 KiB
使用的内存: 16777216 KiB
持久:       是
自动启动: 禁用
管理的保存: 否
安全性模式: none
安全性 DOI: 0

分组的命令:

 Domain Management (help keyword 'domain'):
    attach-device                  从一个XML文件附加装置
    attach-disk                    附加磁盘设备
    attach-interface               获得网络界面
    autostart                      自动开始一个域
    blkdeviotune                   设定或者查询块设备 I/O 调节参数。
    blkiotune                      获取或者数值 blkio 参数
    blockcommit                    启动块提交操作。
    blockcopy                      启动块复制操作。
    blockjob                       管理活跃块操作
    blockpull                      使用其后端映像填充磁盘。
    blockresize                    创新定义域块设备大小
    change-media                   更改 CD 介质或者软盘驱动器
    console                        连接到客户会话
    cpu-baseline                   计算基线 CPU
    cpu-compare                    使用 XML 文件中描述的 CPU 与主机 CPU 进行对比
    cpu-stats                      显示域 cpu 统计数据
    create                         从一个 XML 文件创建一个域
    define                         从一个 XML 文件定义(但不开始)一个域
    desc                           显示或者设定域描述或者标题
    destroy                        销毁(停止)域
    detach-device                  从一个 XML 文件分离设备
    detach-disk                    分离磁盘设备
    detach-interface               分离网络界面
    domdisplay                     域显示连接 URI
    domfsfreeze                    Freeze domain's mounted filesystems.
    domfsthaw                      Thaw domain's mounted filesystems.
    domfsinfo                      Get information of domain's mounted filesystems.
    domfstrim                      在域挂载的文件系统中调用 fstrim。
    domhostname                    输出域主机名
    domid                          把一个域名或 UUID 转换为域 id
    domif-setlink                  设定虚拟接口的链接状态
    domiftune                      获取/设定虚拟接口参数
    domjobabort                    忽略活跃域任务
    domjobinfo                     域任务信息
    domname                        将域 id 或 UUID 转换为域名
    domrename                      rename a domain
    dompmsuspend                   使用电源管理功能挂起域
    dompmwakeup                    从 pmsuspended 状态唤醒域
    domuuid                        把一个域名或 id 转换为域 UUID
    domxml-from-native             将原始配置转换为域 XML
    domxml-to-native               将域 XML 转换为原始配置
    dump                           把一个域的内核 dump 到一个文件中以方便分析
    dumpxml                        XML 中的域信息
    edit                           编辑某个域的 XML 配置
    event                          Domain Events
    inject-nmi                     在虚拟机中输入 NMI
    iothreadinfo                   view domain IOThreads
    iothreadpin                    control domain IOThread affinity
    iothreadadd                    add an IOThread to the guest domain
    iothreaddel                    delete an IOThread from the guest domain
    send-key                       向虚拟机发送序列号
    send-process-signal            向进程发送信号
    lxc-enter-namespace            LXC 虚拟机进入名称空间
    managedsave                    管理域状态的保存
    managedsave-remove             删除域的管理保存
    memtune                        获取或者数值内存参数
    perf                           Get or set perf event
    metadata                       show or set domain's custom XML metadata
    migrate                        将域迁移到另一个主机中
    migrate-setmaxdowntime         设定最大可耐受故障时间
    migrate-compcache              获取/设定压缩缓存大小
    migrate-setspeed               设定迁移带宽的最大值
    migrate-getspeed               获取最长迁移带宽
    migrate-postcopy               Switch running migration from pre-copy to post-copy
    numatune                       获取或者数值 numa 参数
    qemu-attach                    QEMU 附加
    qemu-monitor-command           QEMU 监控程序命令
    qemu-monitor-event             QEMU Monitor Events
    qemu-agent-command             QEMU 虚拟机代理命令
    reboot                         重新启动一个域
    reset                          重新设定域
    restore                        从一个存在一个文件中的状态恢复一个域
    resume                         重新恢复一个域
    save                           把一个域的状态保存到一个文件
    save-image-define              为域的保存状态文件重新定义 XML
    save-image-dumpxml             在 XML 中保存状态域信息
    save-image-edit                为域保存状态文件编辑 XML
    schedinfo                      显示/设置日程安排变量
    screenshot                     提取当前域控制台快照并保存到文件中
    set-user-password              set the user password inside the domain
    setmaxmem                      改变最大内存限制值
    setmem                         改变内存的分配
    setvcpus                       改变虚拟 CPU 的号
    shutdown                       关闭一个域
    start                          开始一个(以前定义的)非活跃的域
    suspend                        挂起一个域
    ttyconsole                     tty 控制台
    undefine                       取消定义一个域
    update-device                  从 XML 文件中关系设备
    vcpucount                      域 vcpu 计数
    vcpuinfo                       详细的域 vcpu 信息
    vcpupin                        控制或者查询域 vcpu 亲和性
    emulatorpin                    控制火车查询域模拟器亲和性
    vncdisplay                     vnc 显示
    guestvcpus                     query or modify state of vcpu in the guest (via agent)

 Domain Monitoring (help keyword 'monitor'):
    domblkerror                    在块设备中显示错误
    domblkinfo                     域块设备大小信息
    domblklist                     列出所有域块
    domblkstat                     获得域设备块状态
    domcontrol                     域控制接口状态
    domif-getlink                  获取虚拟接口链接状态
    domifaddr                      Get network interfaces' addresses for a running domain
    domiflist                      列出所有域虚拟接口
    domifstat                      获得域网络接口状态
    dominfo                        域信息
    dommemstat                     获取域的内存统计
    domstate                       域状态
    domstats                       get statistics about one or multiple domains
    domtime                        domain time
    list                           列出域

 Host and Hypervisor (help keyword 'host'):
    allocpages                     Manipulate pages pool size
    capabilities                   性能
    cpu-models                     CPU models
    domcapabilities                domain capabilities
    freecell                       NUMA可用内存
    freepages                      NUMA free pages
    hostname                       打印管理程序主机名
    maxvcpus                       连接 vcpu 最大值
    node-memory-tune               获取或者设定节点内存参数
    nodecpumap                     节点 cpu 映射
    nodecpustats                   输出节点的 cpu 状统计数据。
    nodeinfo                       节点信息
    nodememstats                   输出节点的内存状统计数据。
    nodesuspend                    在给定时间段挂起主机节点
    sysinfo                        输出 hypervisor sysinfo
    uri                            打印管理程序典型的URI
    version                        显示版本

 Interface (help keyword 'interface'):
    iface-begin                    生成当前接口设置快照,可在今后用于提交 (iface-commit) 或者恢复 (iface-rollback)
    iface-bridge                   生成桥接设备并为其附加一个现有网络设备
    iface-commit                   提交 iface-begin 后的更改并释放恢复点
    iface-define                   define an inactive persistent physical host interface or modify an existing persistent one from an XML file
    iface-destroy                  删除物理主机接口(启用它请执行 "if-down")
    iface-dumpxml                  XML 中的接口信息
    iface-edit                     为物理主机界面编辑 XML 配置
    iface-list                     物理主机接口列表
    iface-mac                      将接口名称转换为接口 MAC 地址
    iface-name                     将接口 MAC 地址转换为接口名称
    iface-rollback                 恢复到之前保存的使用 iface-begin 生成的更改
    iface-start                    启动物理主机接口(启用它请执行 "if-up")
    iface-unbridge                 分离其辅助设备后取消定义桥接设备
    iface-undefine                 取消定义物理主机接口(从配置中删除)

 Network Filter (help keyword 'filter'):
    nwfilter-define                使用 XML 文件定义或者更新网络过滤器
    nwfilter-dumpxml               XML 中的网络过滤器信息
    nwfilter-edit                  为网络过滤器编辑 XML 配置
    nwfilter-list                  列出网络过滤器
    nwfilter-undefine              取消定义网络过滤器

 Networking (help keyword 'network'):
    net-autostart                  自动开始网络
    net-create                     从一个 XML 文件创建一个网络
    net-define                     define an inactive persistent virtual network or modify an existing persistent one from an XML file
    net-destroy                    销毁(停止)网络
    net-dhcp-leases                print lease info for a given network
    net-dumpxml                    XML 中的网络信息
    net-edit                       为网络编辑 XML 配置
    net-event                      Network Events
    net-info                       网络信息
    net-list                       列出网络
    net-name                       把一个网络UUID 转换为网络名
    net-start                      开始一个(以前定义的)不活跃的网络
    net-undefine                   undefine a persistent network
    net-update                     更新现有网络配置的部分
    net-uuid                       把一个网络名转换为网络UUID

 Node Device (help keyword 'nodedev'):
    nodedev-create                 根据节点中的 XML 文件定义生成设备
    nodedev-destroy                销毁(停止)节点中的设备
    nodedev-detach                 将节点设备与其设备驱动程序分离
    nodedev-dumpxml                XML 中的节点设备详情
    nodedev-list                   这台主机中中的枚举设备
    nodedev-reattach               重新将节点设备附加到他的设备驱动程序中
    nodedev-reset                  重置节点设备

 Secret (help keyword 'secret'):
    secret-define                  定义或者修改 XML 中的 secret
    secret-dumpxml                 XML 中的 secret 属性
    secret-get-value               secret 值输出
    secret-list                    列出 secret
    secret-set-value               设定 secret 值
    secret-undefine                取消定义 secret

 Snapshot (help keyword 'snapshot'):
    snapshot-create                使用 XML 生成快照
    snapshot-create-as             使用一组参数生成快照
    snapshot-current               获取或者设定当前快照
    snapshot-delete                删除域快照
    snapshot-dumpxml               为域快照转储 XML
    snapshot-edit                  编辑快照 XML
    snapshot-info                  快照信息
    snapshot-list                  为域列出快照
    snapshot-parent                获取快照的上级快照名称
    snapshot-revert                将域转换为快照

 Storage Pool (help keyword 'pool'):
    find-storage-pool-sources-as   找到潜在存储池源
    find-storage-pool-sources      发现潜在存储池源
    pool-autostart                 自动启动某个池
    pool-build                     建立池
    pool-create-as                 从一组变量中创建一个池
    pool-create                    从一个 XML 文件中创建一个池
    pool-define-as                 在一组变量中定义池
    pool-define                    define an inactive persistent storage pool or modify an existing persistent one from an XML file
    pool-delete                    删除池
    pool-destroy                   销毁(删除)池
    pool-dumpxml                   XML 中的池信息
    pool-edit                      为存储池编辑 XML 配置
    pool-info                      存储池信息
    pool-list                      列出池
    pool-name                      将池 UUID 转换为池名称
    pool-refresh                   刷新池
    pool-start                     启动一个(以前定义的)非活跃的池
    pool-undefine                  取消定义一个不活跃的池
    pool-uuid                      把一个池名称转换为池 UUID
    pool-event                     Storage Pool Events

 Storage Volume (help keyword 'volume'):
    vol-clone                      克隆卷。
    vol-create-as                  从一组变量中创建卷
    vol-create                     从一个 XML 文件创建一个卷
    vol-create-from                生成卷,使用另一个卷作为输入。
    vol-delete                     删除卷
    vol-download                   将卷内容下载到文件中
    vol-dumpxml                    XML 中的卷信息
    vol-info                       存储卷信息
    vol-key                        为给定密钥或者路径返回卷密钥
    vol-list                       列出卷
    vol-name                       为给定密钥或者路径返回卷名
    vol-path                       为给定密钥或者路径返回卷路径
    vol-pool                       为给定密钥或者路径返回存储池
    vol-resize                     创新定义卷大小
    vol-upload                     将文件内容上传到卷中
    vol-wipe                       擦除卷

 Virsh itself (help keyword 'virsh'):
    cd                             更改当前目录
    echo                           echo 参数
    exit                           退出这个非交互式终端
    help                           打印帮助
    pwd                            输出当前目录
    quit                           退出这个非交互式终端
    connect                        连接(重新连接)到 hypervisor
@leapar
Copy link
Author

leapar commented May 31, 2017

Since there are small inconsistencies in @mustafa answer, I am rewriting some of the 'steps'. The following was tested on Ubuntu 12.04

  1. Update the libvirt configurations. Modify /etc/libvirt/libvirtd.conf
before : #listen_tls = 0
after : listen_tls = 0

before : #listen_tcp = 1
after : listen_tcp = 1

add: auth_tcp = "none"
  1. Modify /etc/init/libvirt-bin.conf. (-l is short for –listen)
before : env libvirtd_opts="-d"
after : env libvirtd_opts="-d -l"
  1. Modify /etc/default/libvirt-bin.
before :libvirtd_opts=" -d"
after :libvirtd_opts=" -d -l"
  1. Restart libvirt. After executing the command, ensure that libvirt is successfully restarted.
# stop libvirt-bin && start libvirt-bin
$ ps -ef | grep libvirt
  1. The output of the last one should be something like:
root     XXXXX     1 22 07:55 ?        00:00:01 /usr/sbin/libvirtd -d -l

@leapar
Copy link
Author

leapar commented May 31, 2017

试用过程如下:http://blog.csdn.net/c80486/article/details/42836169

环境: Ubuntu 14.04 64bit

1, KVM需要CPU硬件支持虚拟化,所以首先要确认你的CPU支持。

 Intel CPU的虚拟化技术叫 vmx (Virtual Machine eXtension,就是一串虚拟机指令集) 
 AMD CPU的虚拟化技术叫 svm

开机,进BIOS, 设置开启虚拟化

 我的CPU是Intel的,BIOS是这样的: 选Security, 再进Virtualization项, 把各项设置成ENABLE,保存退出 。

有的BIOS是这样的:  选择ADVANCED,再进PROCESSOR CONFIGURATION找到 INTEL VIRTUALIZATION  TECHNOLOGY ,把各项设置成ENABLE,保存退出 。 

2, 启动 linux 系统 (强烈建议装64位的linux版本,否则虚拟机内存将被限制在2G之内)
(1)查看一下linux是32位还是64位

file /bin/ls
返回结果中将显示 64bit 或 32bit

 (2)确认一下 CPU支持硬件虚拟化     

egrep -o '(vmx|svm)' /proc/cpuinfo
如果有结果显示,就OK

3, 安装相关软件包

(为了下载快,安装前要把apt的源改为 163.com或sohu.com)

开始安装    

$ sudo apt-get install qemu-kvm qemu-system libvirt-bin bridge-utils
libvirt-bin provides libvirtd which you need to administer qemu and kvm instances using libvirt

qemu-kvm 是虚拟机 (qemu is the backend)

bridge-utils 用于网络桥接 (provides a bridge from your network to the virtual machines)

sudo apt-get install virt-manager python-spice-client-gtk
其中:virt-manager为虚拟机管理程序(在x window中运行,需要用到python-spice-client-gtk)

执行下面的命令看KVM是否安装成功:

kvm-ok
输出如下:
INFO: /dev/kvm exists
KVM acceleration can be used
如果输出是这样:
INFO: Your CPU does not support KVM extensions
KVM acceleration can NOT be used
这表明你的CPU不支持硬件虚拟化。 这时你还是可以跑虚拟机,不过会非常慢。
如果你的CPU支持硬件虚拟化, 但BIOS没有打开,也会提示给你的

查看kvm内核模块是否已加载    

sudo lsmod | grep kvm

若KVM内核没加载执行下面命令加载:

sudo modprobe kvm

4, 使用
(1) 初次使用,不太熟悉,还是装个 x window,用图形界面操作比较容易 (建议新手都这样做)
在Ubuntu server版下装 x window 的过程如下:
apt-get install xserver-xorg
apt-get install x-window-system-core
dpkg-reconfigure xserver-xorg
apt-get install gnome-core
apt-get install gdm xscreensaver
startx

(2)在 x win 中, 打开一个命令行窗口, 输入以下命令启动虚拟机管理程序
virt-manager

(3) 鼠标右键单击上图中的 localhost(QEMU) , 选菜单 New 创建一个虚拟机
按提示一步一步做即可

 在网上下载一个 windows xp 精简版的 iso 文件
  以这个iso文件为CD盘  (如果引导不起来,在虚拟机中设置中的Boot Options中设 CD盘优先启动)
 
  启动这个新的虚拟机, 开始装Windows xp 

(4) Windows xp 虚拟机装好了, 运行起来了

用这台虚拟机 ping www.baidu.com 没有问题,可上网

(3) 再创建一个新的虚拟机, 安装 Ubuntu Server.
在这台Ubuntu Server虚拟机上安装 LAMP
apt-get install apache2 mysql-server mysql-client php5 php5-gd php5-mysql phpmyadmin
chmod -R 777 /var/www

 用Windows XP虚拟机访问 Ubuntu Server虚拟机的网站,OK.  说明这两台虚拟机已经连在一个内网上了。

@leapar
Copy link
Author

leapar commented May 31, 2017

mac os 安装不来kvm,就用vitualbox跑ubuntu

vagrant init ubuntu/trusty64;
vagrant up --provider virtualbox

@leapar
Copy link
Author

leapar commented Jun 2, 2017

[
    {
        "metric": "domain.info.mem.max",
        "timestamp": 0,
        "value": "1048576",
        "tags": {
            "host": "ubuntu_2",
            "kvm-host": "172.29.231.80"
        }
    },
    {
        "metric": "domain.info.mem.usage",
        "timestamp": 0,
        "value": "1048576",
        "tags": {
            "host": "ubuntu_2",
            "kvm-host": "172.29.231.80"
        }
    },
    {
        "metric": "domain.info.cpu.num",
        "timestamp": 0,
        "value": "1",
        "tags": {
            "host": "ubuntu_2",
            "kvm-host": "172.29.231.80"
        }
    },
    {
        "metric": "domain.info.cpu.time",
        "timestamp": 0,
        "value": "69100000000",
        "tags": {
            "host": "ubuntu_2",
            "kvm-host": "172.29.231.80"
        }
    },
    {
        "metric": "domain.block.stats.rd.bytes",
        "timestamp": 0,
        "value": "512",
        "tags": {
            "Device": "vda",
            "File": "/var/lib/libvirt/images/ubuntu_2",
            "host": "ubuntu_2",
            "kvm-host": "172.29.231.80"
        }
    },
    {
        "metric": "domain.block.stats.rd.req",
        "timestamp": 0,
        "value": "1",
        "tags": {
            "Device": "vda",
            "File": "/var/lib/libvirt/images/ubuntu_2",
            "host": "ubuntu_2",
            "kvm-host": "172.29.231.80"
        }
    },
    {
        "metric": "domain.block.stats.wr.bytes",
        "timestamp": 0,
        "value": "0",
        "tags": {
            "Device": "vda",
            "File": "/var/lib/libvirt/images/ubuntu_2",
            "host": "ubuntu_2",
            "kvm-host": "172.29.231.80"
        }
    },
    {
        "metric": "domain.block.stats.wr.req",
        "timestamp": 0,
        "value": "0",
        "tags": {
            "Device": "vda",
            "File": "/var/lib/libvirt/images/ubuntu_2",
            "host": "ubuntu_2",
            "kvm-host": "172.29.231.80"
        }
    },
    {
        "metric": "domain.block.stats.rd.bytes",
        "timestamp": 0,
        "value": "0",
        "tags": {
            "Device": "hda",
            "File": "",
            "host": "ubuntu_2",
            "kvm-host": "172.29.231.80"
        }
    },
    {
        "metric": "domain.block.stats.rd.req",
        "timestamp": 0,
        "value": "0",
        "tags": {
            "Device": "hda",
            "File": "",
            "host": "ubuntu_2",
            "kvm-host": "172.29.231.80"
        }
    },
    {
        "metric": "domain.block.stats.wr.bytes",
        "timestamp": 0,
        "value": "0",
        "tags": {
            "Device": "hda",
            "File": "",
            "host": "ubuntu_2",
            "kvm-host": "172.29.231.80"
        }
    },
    {
        "metric": "domain.block.stats.wr.req",
        "timestamp": 0,
        "value": "0",
        "tags": {
            "Device": "hda",
            "File": "",
            "host": "ubuntu_2",
            "kvm-host": "172.29.231.80"
        }
    },
    {
        "metric": "domain.interface.stats.rx.bytes",
        "timestamp": 0,
        "value": "0",
        "tags": {
            "Bridge": "br0",
            "Device": "vnet1",
            "host": "ubuntu_2",
            "kvm-host": "172.29.231.80"
        }
    },
    {
        "metric": "domain.interface.stats.rx.packets",
        "timestamp": 0,
        "value": "0",
        "tags": {
            "Bridge": "br0",
            "Device": "vnet1",
            "host": "ubuntu_2",
            "kvm-host": "172.29.231.80"
        }
    },
    {
        "metric": "domain.interface.stats.rx.errs",
        "timestamp": 0,
        "value": "0",
        "tags": {
            "Bridge": "br0",
            "Device": "vnet1",
            "host": "ubuntu_2",
            "kvm-host": "172.29.231.80"
        }
    },
    {
        "metric": "domain.interface.stats.rx.drop",
        "timestamp": 0,
        "value": "18826",
        "tags": {
            "Bridge": "br0",
            "Device": "vnet1",
            "host": "ubuntu_2",
            "kvm-host": "172.29.231.80"
        }
    },
    {
        "metric": "domain.interface.stats.tx.bytes",
        "timestamp": 0,
        "value": "0",
        "tags": {
            "Bridge": "br0",
            "Device": "vnet1",
            "host": "ubuntu_2",
            "kvm-host": "172.29.231.80"
        }
    },
    {
        "metric": "domain.interface.stats.tx.packets",
        "timestamp": 0,
        "value": "0",
        "tags": {
            "Bridge": "br0",
            "Device": "vnet1",
            "host": "ubuntu_2",
            "kvm-host": "172.29.231.80"
        }
    },
    {
        "metric": "domain.interface.stats.tx.errs",
        "timestamp": 0,
        "value": "0",
        "tags": {
            "Bridge": "br0",
            "Device": "vnet1",
            "host": "ubuntu_2",
            "kvm-host": "172.29.231.80"
        }
    },
    {
        "metric": "domain.interface.stats.tx.drop",
        "timestamp": 0,
        "value": "0",
        "tags": {
            "Bridge": "br0",
            "Device": "vnet1",
            "host": "ubuntu_2",
            "kvm-host": "172.29.231.80"
        }
    }
]

@leapar
Copy link
Author

leapar commented Jun 2, 2017

host

[
    {
        "Id": 2,
        "Name": "ubuntu_2",
        "Info": {
            "State": 1,
            "MaxMem": 1048576,
            "Memory": 1048576,
            "NrVirtCpu": 1,
            "CpuTime": 68840000000
        }
    },
    {
        "Id": 1,
        "Name": "ubuntu1",
        "Info": {
            "State": 1,
            "MaxMem": 1048576,
            "Memory": 1048576,
            "NrVirtCpu": 1,
            "CpuTime": 69820000000
        }
    }
]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment