//创建一个虚拟机,并且运行它
//命令的效果是创建一个img的磁盘镜像
qemu-img create -f raw template.img 3G//先运行命令,创建磁盘镜像文件(随便建一个文件是没用的)
main.c://注意,需要用g++编译
/***************************************************************************
* create_vm.cpp
* create kvm machine(domain) based on conf.xml
* the first parameter is the conf xml files' name
* Note: the .xml must has two boot types (cdrom/hd) by any order
* compile command: 'g++ create_vm.cpp -o createvm -lvirt'
* running command: './createvm /path/to/xml/example.xml'
* author : Aborn Jiang
* date : Aug.17, 2013
* version : v0.1
***************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <cstdio>
#include <string>
#include <fstream>
#include <sstream>
#include <libvirt/libvirt.h>
#include <libvirt/virterror.h>
#include <memory.h>
using namespace std;
int main(int argc, char* argv[])
{
if ( 1 == argc ) {
cout << "must and only need an argument, this is, configure .xml file name." << endl;
return -1;
}
if ( 3 <= argc ) {
cout << "too many arguments. must and only need one, that is, .xml file name." << endl;
return -1;
}
string xmlfile=argv[1];//配置文件的路径
cout << "*************************" << endl;
cout << "begin to build vm ..." << endl;
cout << "xmlfile path:" << xmlfile <<endl;
ifstream file(xmlfile.c_str());//打开该路径的文件
if (!file) {