CentOS7中,KVM虚拟机,用C的libvirt库函数创建并启动虚拟机

本文介绍了如何在CentOS7系统中利用C语言的libvirt库函数创建并启动KVM虚拟机。首先通过`qemu-img create`命令创建一个3GB的raw格式磁盘镜像,然后使用C代码调用libvirt库进行虚拟机的创建。同时,文中提到的`stand.xml`是虚拟机的配置文件,可以依据个人需求进行调整。作者提到代码和配置文件来源于网络并已进行修改。
摘要由CSDN通过智能技术生成

//创建一个虚拟机,并且运行它

//命令的效果是创建一个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) {
   
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值