原创作品,允许转载,转载时请务必以超链接形式标明文章
原始出处 、作者信息和本声明。否则将追究法律责任。
http://speakingbaicai.blog.51cto.com/5667326/1162005
- <domain type = 'kvm'> //虚拟机类型,kvm
- <name>demo</name> //虚拟机名称
- <memory>1048576</memory> //分配内存,单位kb
- <vcpu>1</vcpu> //分配vcpu,单位个数
- <os>
- <type arch = 'x86_64' machine = 'pc'>hvm</type>
- <boot dev = 'cdrom'/> //cd 启动
- <boot dev = 'hd'/> //硬盘启动
- </os>
- <features>
- <acpi/>
- <apic/>
- <pae/>
- </features>
- <clock offset = 'localtime'/>
- <on_poweroff>destroy</on_poweroff>
- <on_reboot>restart</on_reboot>
- <on_crash>destroy</on_crash>
- <devices>
- <emulator>/usr/bin/kvm</emulator>
- <disk type = 'file' device = 'disk'> //对应的镜像,就是之前使用qemu-img命令新建的img文件,注意路径要正确
- <driver name = 'qemu' type = 'raw'/>
- <source file = '/var/lib/lynn/img/template.img'/>
- <target dev = 'hda' bus = 'ide'/>
- </disk>
- <disk type = 'file' device = 'cdrom'> //可选项,iso通常是操作系统的安装光盘
- <source file = '/var/lib/lynn/img/template.iso'/>
- <target dev = 'hdb' bus = 'ide'/>
- </disk>
- <interface type = 'bridge'> //libvirt默认虚拟机的网络配置是NAT模式,就是虚拟机与宿主机的网络拓扑是NAT形式。实际中,许多开发者更希望使用网桥模式。
- <source bridge = 'br0'/>
- </interface>
- <input type ='tablet' bus='usb'/>
- <input type = 'mouse' bus = 'ps2'/>
- <graphics type = 'vnc' port = '-1' listen = '0.0.0.0' autoport = 'yes' keymap = 'en-us'/> //vnc端口系统自动配置
- </devices>
- </domain>
virConnectPtr virConnectOpen(const char * name) | |
- /* createvm.cpp */
- /* compile with: g++ createvm.cpp -o createvm -lvirt */
- /* Wang Min @ iie */
- /* autumn_sky_is@163.com */
- /* date: 2013-02-27 */
- #include <iostream>
- #include <cstdio>
- #include <string>
- #include <fstream>
- #include <sstream> // for stringstream
- #include "libvirt/libvirt.h"
- #include <libvirt/virterror.h>
- using namespace std;
- string vm_xml_location = "../xml/template.xml";
- int main()
- {
- ifstream file(vm_xml_location.c_str());
- if(!file) {
- cout<<"Cannot open file "<<vm_xml_location<<endl;
- return -1;
- }
- // read xml file
- stringstream buffer;
- buffer << file.rdbuf();
- string vm_xml_template = buffer.str();
- file.close();
- virConnectPtr conn = virConnectOpen("qemu:///system");
- if(NULL==conn) {
- fprintf(stderr, "Failed to build connection to qemu:///system.\n");
- return -1;
- }
- virDomainPtr vm_ptr = virDomainCreateXML(conn, vm_xml_template.c_str(), 0);
- if(!vm_ptr) {
- virErrorPtr error = virGetLastError();
- cout<<error->message<<endl;
- return -1;
- }
- return 0;
- }
- /* print.cpp */
- /* compile with: g++ print.cpp -o print -lvirt */
- /* Wang Min @ iie */
- /* autumn_sky_is@163.com */
- /* date: 2013-02-27 */
- #include <iostream>
- #include <cstdio>
- #include <string>
- #include <fstream>
- #include <sstream> // for stringstream
- #include "libvirt/libvirt.h"
- #include <libvirt/virterror.h>
- using namespace std;
- //using namespace rapidxml;
- int main(int argc, char * argv[])
- {
- if(2!=argc){
- cout<<"Error: print need 1 parametre"<<endl;
- cout<<"usage: ./print vm_name"<<endl;
- return -1;
- }
- virConnectPtr conn = virConnectOpen("lxc:///");
- if(NULL==conn) {
- fprintf(stderr, "Failed to build connection to lxc:///.\n");
- return -1;
- }
- virDomainPtr domain = virDomainLookupByName(conn, argv[1]);
- if(domain == NULL){
- cout<<"can not find "<<argv[1]<<", regard it as success."<<endl;
- return -1;
- }
- // get lxc Info
- virDomainInfo info;
- if(-1==virDomainGetInfo(domain, &info)){
- cout<<"can not get domain info"<<endl;
- return -1;
- }
- printf("state:%d|maxmem:%d|memused:%d|cpunum:%d|cputime:%ld\n",info.state,info.maxMem,info.memory,info.nrVirtCpu,info.cpuTime);
- cout<<"Helloworld"<<endl;
- return 0;
- }
- /* shutdownvm.cpp */
- /* compile with: g++ shutdownvm.cpp -o shutdownvm -lvirt */
- /* Wang Min @ iie */
- /* autumn_sky_is@163.com */
- /* date: 2013-02-27 */
- #include <iostream>
- #include <cstdio>
- #include <string>
- #include <fstream>
- #include <sstream> // for stringstream
- #include "libvirt/libvirt.h"
- #include <libvirt/virterror.h>
- using namespace std;
- string domname = "demo";
- int main()
- {
- virConnectPtr conn = virConnectOpen("qemu:///system");
- if(NULL==conn) {
- fprintf(stderr, "Failed to build connection to qemu:///system.\n");
- return -1;
- }
- virDomainPtr domain = virDomainLookupByName(conn, domname.c_str());
- if(domain == NULL){
- cout<<"can not find "<<domname <<", regard it as success."<<endl;
- return 0;
- }
- if(virDomainShutdown(domain)!=0){
- virErrorPtr error = virGetLastError();
- cout<<error->message<<endl;
- return -1;
- }
- return 0;
- }
- /* destroyvm.cpp */
- /* compile with: g++ destroyvm.cpp -o destroyvm -lvirt */
- /* Wang Min @ iie */
- /* autumn_sky_is@163.com */
- /* date: 2013-02-27 */
- #include <iostream>
- #include <cstdio>
- #include <string>
- #include <fstream>
- #include <sstream> // for stringstream
- #include "libvirt/libvirt.h"
- #include <libvirt/virterror.h>
- using namespace std;
- string domname = "demo";
- int main()
- {
- virConnectPtr conn = virConnectOpen("qemu:///system");
- if(NULL==conn) {
- fprintf(stderr, "Failed to build connection to qemu:///system.\n");
- return -1;
- }
- virDomainPtr domain = virDomainLookupByName(conn, domname.c_str());
- if(domain == NULL){
- cout<<"can not find "<<domname <<", regard it as success."<<endl;
- return 0;
- }
- if(virDomainDestroy(domain)!=0){
- virErrorPtr error = virGetLastError();
- cout<<error->message<<endl;
- return -1;
- }
- return 0;
- }