Load_file (Vm_mem + 0x100000, av[2]); Virdomaincreatexm

Source: Internet
Author: User
Tags function prototype uuid
Listing 1. Application fragment to test KVM System Management Program
int main ()
{
	void *vm_mem;

	Kvm_init (&test_callbacks, 0);
	if (!KVM) {
	    fprintf (stderr, "Kvm_init failed\n");
	    return 1;
	}
	if (kvm_create(KVM, 128 * 1024 * 1024, &VM_MEM) < 0) {
	    kvm_finalize(KVM);
	    fprintf (stderr, "kvm_create failed\n");
	    return 1;
	}
	if (AC > 1)
	    if (strcmp (av[1], " -32")!= 0)
		load_file(Vm_mem + 0xf0000, av[1]);
	    else
		enter_32(KVM);
	if (AC > 2)
	    load_file(Vm_mem + 0x100000, av[2]);
	Kvm_show_regs (KVM, 0);

	Kvm_run (KVM, 0);

	return 0;
}

2.2 Key APIs for creating virtual machines via Libvirt

By analyzing the Virsh source code in 2.1, we can see that using Libvirt for virtual machine creation to invoke two key api--Virfilereadall and Virdomaincreatexml is described separately below.

2.2.1 Virfilereadall

The function prototype is intvirfilereadall (const char *path, int maxlen, char **buf), which reads the file contents of the path specified by parameter "path" into a buffer and records the buffer address in the parameter "*buf". The parameter "MaxLen" specifies the maximum length of the file. With this API, we can use the XML configuration file into a buffer to facilitate subsequent usage.

2.2.2virDomainCreateXML

The function is a prototype of Virdomainptr Virdomaincreatexml (virconnectptrconn, const char * xmldesc, unsigned int flags), and functions are based on the parameters "Xmld ESC to create a domain and return a pointer to that field. The parameter "conn" is a pointer to the virtual Machine Manager, and by setting a different flags flag, you can make the domain you create have different properties.

Three Use the Libvirt library to write your own virtual machine creation program

the command used by the Virsh command to create a virtual machine is: Virsh Create, which generates the client from the given XML file and starts the client.

Here is a test example to illustrate how to create a virtual machine by using the Virsh command.

The concrete operation Practice Step is: first need to create the virtual hard disk, in order to place the operating system, the command is: kvm-img create

701.img10g, which is to create a virtual hard disk size of 10G.

2. Write an XML file containing some features of the startup operating system, such as: memory capacity, operating system location, virtual hard disk location, etc., in fact there are many fields, you can abbreviate an XML file, if some fields are not defined, then the system will default, the following gives an XML file, Named 701.xml, the program is:

<domain type= ' Qemu ' >

<name>linux10.0421</name>

<uuid></uuid>

<memory>512000</memory>

<currentMemory>512000</currentMemory>

<vcpu>1</vcpu>

<os>

<type arch= ' i686 ' machine= ' pc ' >hvm</type>

<boot dev= ' cdrom '/>

<boot dev= ' HD '/>

</os>

<devices>

<emulator>/usr/bin/qemu-system-x86_64</emulator>

<disk type= ' file ' device= ' cdrom ' >

<source file= '/usr/src/ubuntu-10.04-desktop-i386.iso '/>

<target dev= ' hdc '/>

<readonly/>

</disk>

<disk type= ' file ' device= ' disk ' >

<sourcefile= '/var/lib/libvirt/images/701.img '/>

<target dev= ' Hda '/>

</disk>

<graphics type= ' vnc ' port= ' 5901 ' listen= ' 127.0.0.1 '

</devices>

</domain>

3. Then write a C file, the name 701.c the main implementation of this file is called the XML file to create and start the virtual machine. This c program code is:

#include <stdio.h>

#include <stdlib.h>

#include <memory.h>

#include <libvirt/libvirt.h>

const char *from=null;

Static Virconnectptr Conn=null;

#define Virsh_max_xml_file 10*1024*1024

void Closeconn ()

{

if (conn!=null)

Virconnectclose (conn);

}

int Cmdcreate ()

{

Virdomainptr Dom;

Char *buffer;

unsigned int flags=vir_domain_none;

Conn=virconnectopen ("Qemu:///system");

if (conn==null)

{

fprintf (stderr, "Failed to connect tohypervisor/n");

Closeconn ();

return 0;

}

if (Virfilereadall (from,virsh_max_xml_file,&buffer) <0)

return 0;

Dom=virdomaincreatexml (Conn,buffer,flags);

memset (buffer,0,sizeof (buffer));

if (dom!=null) {

fprintf (stdout, "Domain%screated from%s\n", Virdomaingetname (DOM), from);

Virdomainfree (DOM);

}

else{

fprintf (stdout, "Failed to CreateDomain from%s");

}

}

int main (int argc,char *argv[])

{

if (argc<2) {

fprintf (stdout, "There are too fewparameters,should has two more parameters!");

}

FROM=*++ARGV;

Cmdcreate ();

return 0;

}

4. Execute Gcc-lvirt-o 701 701.c in the Command window, and then execute./701 701.xml, you can see the virtual machine is created and started up.

2. Function Kvm_create (): This function is primarily used to create a virtual machine kernel environment. The function prototype is:

int kvm_create (kvm_context_t kvm,unsignedlong phys_mem_bytes, void **phys_mem);

Parameter: kvm_context_t represents the passed user-state virtual machine context, Phys_mem_bytes represents the size of the physical memory that needs to be created, and Phys_mem represents the first address to create the virtual machine. This function first invokes the KVM_CREATE_VM () assignment IRQ and initializes to 0, setting the value of vcpu[0] to 1, that is, scheduling virtual machine execution is not allowed. Then call the IOCTL system call IOCTL (fd,kvm_create_vm,0) to create the virtual machine kernel data structure struct KVM.

3. The system calls function ioctl (fd,kvm_create_vm,0), which is used to create the data structure associated with the virtual machine in the kernel. The function prototype is:

Static long kvm_dev_ioctl (struct file *filp,unsigned intioctl, Unsignedlong Arg), where IOCTL represents the command. This function calls KVM_DEV_IOCTL_CREATE_VM () to create the virtual machine instance kernel-related data structure. The function first creates the KVM context struct KVM in the kernel through the KVM_CREATE_VM () function in the kernel, and then passes the function

ANNO_INODE_GETFD ("KVM_VM", &kvm_vm_fops,kvm,0) returns the file descriptor of the virtual machine, returns to the user to invoke the function, and assigns the function described in 2 to the virtual machine descriptor in the User state virtual machine context variable Kvm_vm_ Fd.

4. After the kernel creates a virtual machine KVM object, it then calls the Kvm_arch_create function to create some architectural-related information, mainly including KVM_INIT_TSS, Kvm_create_pit, and Kvm_init_coalsced_ Mmio and other information. The Kvm_create_phys_mem is then called to create the physical memory, and the function kvm_create_irqchip is used to create the kernel IRQ information, which is called IOCTL (kvm->vm_fd,kvm_create_irqchip) through the system.

5, Function kvm_create_vcpu (): Used to create a virtual processor. The function prototype is:

int Kvm_create_vcpu (kvm_context_t KVM, Intslot);

Parameter: The KVM represents the corresponding user state virtual machine context, slot represents the number of virtual processors that need to be created.

This function creates a virtual processor belonging to the virtual machine through the IOCTL system call IOCTL (Kvm->vm_fd,kvm_create_vcpu,slot). The system calls the function:

The Static init kvm_vm_ioctl_create_vcpu (STRUCT*KVM, N) parameter KVM is the kernel virtual machine instance data structure, n is the number of virtual CPUs created.

6, the function Kvm_create_phys_mem () is used to create the virtual machine memory space, which is a prototype:

Void * KVM_CREATE_PHYS_MEM (kvm_context_tkvm,unsigned long phys_start,unsigned len,int log,int writable);

Parameter: The KVM represents the user state virtual machine context information, Phys_start is the physical starting address assigned to the virtual machine, Len represents the memory size, log indicates whether the dirty page is logged, and writable indicates whether the page table that corresponds to the memory is writable.

The function first requests a struct kvm_userspace_memory_region and then sets the properties of the corresponding memory in the kernel through system call kvm_set_user_memory_region. The system calls the function prototype:

Ioctl (int kvm->vm_fd,kvm_set_user_memory_region,&memory);

Parameters: The first parameter is VM_FD to a file descriptor pointing to the kernel virtual machine instance object, and the second parameter kvm_set_user_memory_region to the system call command parameter, indicating that the system call is to create a kernel client mapping, that is, a shadow page table. The third parameter, memory, represents the memory space address that points to the virtual machine. The system call first copies the struct_user_momory_region variable from the user space through the function Copy_from_user via the parameter memory, and then through the Kvm_vm_ioctl_set_memory_ The region function sets the corresponding memory domain in the kernel. The function prototype:

Int kvm_vm_ioctl_set_memory_region (struct*kvm,struct kvm_usersapce_memory_region *mem,int user_alloc); The function calls the function again Kvm_ Set_memory_resgion () Sets the shadow page table. When all this is ready, call the Kvm_run () function to schedule the execution of the virtual processor.

7, Function Kvm_run (): For scheduling running virtual processors. The function prototype is:

Int Kvm_run (kvm_context_t kvm,int vcpu,void *env) The function first obtains a descriptor for the VCPU, and then invokes the system call IOCTL (fd,kvm_run,0) to schedule the running of the virtual processor. The Kvm_run function does not return in normal operation unless one of the following events occurs: An I/O event occurs, an I/O event is handled by a user-state qemu, and an exception event that cannot be handled by both the client and the KVM. Kvm_run () to return the intercepted events, mainly I/O and downtime and other events.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.