[libvirt-users] How does libvirt interaction with KVM to create a VM?

Alex Jia ajia at redhat.com
Thu Jun 28 11:03:36 UTC 2012


On 06/28/2012 05:21 PM, Dennis Chen wrote:
> All,
>
> These days I am trying to understand the interaction relationship 
> between the libvirt and KVM kernel module, eg. kvm_intel.ko.
>
> We know that KVM kernel module expose an entry in form of device file 
> "/dev/kvm" which can be accessed by user space application to control, 
> for example, create a VM using KVM_CREATE_VM with help of ioctl.
>
> Now let's say the tool virsh based upon libvirt, we can create a guest 
> domain with the command looks like:
> #virsh create guest.xml
> Obviously, the above command will create a VM. But when I try to 
> investigate the libvirt code, I can't find any code play with the 
> "/dev/kvm" to send KVM_CREATE_VM ioctl code to KVM kernel module.  
> But  I do found that the reference count of the kvm_intel.ko changed 
> before the virsh create command launched and after.
>
> So my question is: how does the libvirt interaction with KVM to create 
> a VM? Anybody can give me some tips about that, eg, the corresponding 
> codes in libvirt?

http://libvirt.org/git/?p=libvirt.git;a=blob;f=src/qemu/qemu_driver.c;h=2f934040ea7d996574ac447498a195d440ec0783;hb=HEAD

114 /* device for kvm ioctls */
115 #define KVM_DEVICE "/dev/kvm"

1022 static int kvmGetMaxVCPUs(void) {
1023     int maxvcpus = 1;
1024
1025     int r, fd;
1026
1027     fd = open(KVM_DEVICE, O_RDONLY);
1028     if (fd < 0) {
1029         virReportSystemError(errno, _("Unable to open %s"), 
KVM_DEVICE);
1030         return -1;
1031     }
1032
1033     r = ioctl(fd, KVM_CHECK_EXTENSION, KVM_CAP_NR_VCPUS);
1034     if (r > 0)
1035         maxvcpus = r;
1036
1037     VIR_FORCE_CLOSE(fd);
1038     return maxvcpus;
1039 }


http://libvirt.org/git/?p=libvirt.git;a=blob;f=src/qemu/qemu_process.c;h=c5140c3c92a9ceec8cb7778c79c8542f60eca486;hb=HEAD

Please see qemuProcessStart() implemetation:

3288 int qemuProcessStart(virConnectPtr conn,
3289                      struct qemud_driver *driver,
3290                      virDomainObjPtr vm,
3291                      const char *migrateFrom,
3292                      int stdin_fd,
3293                      const char *stdin_path,
3294                      virDomainSnapshotObjPtr snapshot,
3295                      enum virNetDevVPortProfileOp vmop,
3296                      unsigned int flags)
......
>
> BRs.
> Dennis
>
> _______________________________________________
> libvirt-users mailing list
> libvirt-users at redhat.com
> https://www.redhat.com/mailman/listinfo/libvirt-users




More information about the libvirt-users mailing list