Use of KVM-libvirt: create virtual machines and snapshots

Source: Internet
Author: User
Tags dname

 

Http://blog.csdn.net/whuqin/article/details/6732898

1. Create a domain (Virtual Machine): including volume and XML describing hardware configuration

// Connect the front end to the node connect conn = new connect ("qemu + SSH: // root@192.168.69/system", false ); // create a new VM Based on the XML Description and start saxreader reader = new saxreader (); document docu = reader. read (new file ("/domain. XML "); string xmldesc = docu. asxml (); domain = Conn. domaincreatexml (xmldesc, 0); domain. resume ();

Supplement: You can directly attach an image (. IMG) to a new domain, but the image can only be used by one user. To enable the image to be used by multiple users, you can use storagevol to associate the image, and the virtual machine can load the backup.

Connect con = new connect (""); // obtain storagepoolstoragepool pool = con Based on the name of the storage pool. storagepoollookupbyname ("default"); saxreader reader = new saxreader (); document docu = reader. read (new file ("/volume. XML "); storagevol Vol = pool. storagevolcreatexml (Docu. asxml (), 0 );

Record the storage address of vol and assign it to the configuration file of the domain.

Delete vol

Storagevol Vol = pool. storagevollookupbyname (volname); vol. Delete (0 );Supplement:Storagevol can be used as an imageSnapshots, The size is small, only the changes to the image are recorded.

 

2. Migration/destruction of Domains

// Establish a connection between the front end and the node connect conn1 = new connect ("qemu + SSH: // root@192.168.82.70/system", false ); // return the Domain According to the uuid-Unique ID of the domain (when the domain is generated, libvirt automatically allocates a unique UUID. String UUID = ""; domain = conn1.domainlookupbyuuidstring (UUID); Connect conn2 = new connect ("qemu + SSH: // root@192.168.82.71/system", false ); /** migrate the domain from the current host to the destination host * domain migrate (connect dconn, long flags, string dname, string Uri, long bandwidth) * dconn: destination host * dname: (Optional) Name of the domain on the target * URI: (optional) IP Address * bandwidth: Migration bandwidth, Mbps */domain. migrate (conn2, 1, null, null, 5); // destroy domain. destroy ();Supplement:The domain name, ID, and other information are defined in the XML configuration description. Dom4j is used for XML file operations.

 

Standard domain. xml

 

<? XML version = "1.0" encoding = "UTF-8"?> <Domain type = 'kvm '> <Name> test </Name> <memory> 524288 </memory> <currentmemory> 524288 </currentmemory> <vcpu> 1 </vcpu> <OS> <type Arch = 'x86 _ 64' machine = 'pc-0.14 '> hvm </type> <boot Dev = 'hd'/> <bootmenu enable = 'no '/> </OS> <features> <ACPI/> <APIC/> <PAE/> </features> <clock offset = 'localtime'/> <on_poweroff> destroy </on_poweroff> <on_reboot> restart </on_reboot> <on_crash> restart </on_crash> <devices> <emula Tor>/usr/bin/qemu-KVM </emulator> <disk type = 'file' device = 'disk'> <driver name = 'qemu' type = 'qcow2 '/> <source file = '/var/lib/libvirt/images/test. IMG '/> <! -- Running image --> <target Dev = 'vda' bus = 'virtio '/> <address type = 'pci' domain = '0x0000 'bus = '0x00' slot =' 0x05 'function = '0x0'/> </disk> <interface type = 'network'> <MAC address = '52: 54: 00: 19: 25: 7b '/> <source network = 'default'/> <model type = 'virtio'/> <address type = 'pci 'domain = '0x000000' bus = '0x00' slot = '0x03' function = '0x0'/> </interface> <serial type = 'PTY'> <Target Port = '0'/> </serial> <console type = 'PTY'> <target type = 'serial' Port = '0'/> </Console> <input type = 'tablet' bus = 'usb '/> <input type = 'mouse 'bus = 'ps2 '/> <graphics type = 'vnc 'port = '000000' autoport = 'no' Listen = '0. 0.0.0 '/> <sound model = 'ac97'> <address type = 'pci 'domain = '0x0000' bus = '0x00' slot = '0x04 'function = '0x0 '/> </sound> <video> <model type = 'cirrus 'VRAM = '000000' heads = '1'/> <address type = 'pci' domain = '0x0000' Bus = '0x00' slot = '0x02' function = '0x0'/> </video> <memballoon model = 'virtio '> <address type = 'pci' domain = '0x0000' bus = '0x00' slot = '0x06' function = '0x0'/> </memballoon> </devices> </domain>

Standard Volume. xml

<? XML version = "1.0" encoding = "UTF-8"?> <Volume> <Name> </Name> <key>/var/lib/libvirt/images/temp. IMG </key> <source> </source> <capacity> 8589934592 </capacity> <allocation> 139264 </allocation> <target> <path>/var/lib/libvirt /images/temp. IMG </path> <! -- New volume address --> <format type = 'qcow2 '/> <permissions> <mode> 0600 </mode> <owner> 0 </owner> <group> 0 </group> </permissions> </Target> <backingstore> <path>/var/lib/libvirt/images/WINXP. IMG </path> <! -- Associated image --> <format type = 'qcow2 '/> <permissions> <mode> 0600 </mode> <owner> 107 </owner> <group> 107 </ group> </permissions> </backingstore> </volume>

Supplement: dom4j usage example

Saxreader reader = new saxreader (); document docu = reader. read (new file ("/domain. XML "); // modify the element attribute value element graphics = docu. getrootelement (). element ("devices "). element ("graphics"); Attribute attrport = graphics. attribute ("Port"); attrport. settext (getport (); // change the element value element nameele = docu. getrootelement (). element ("name"); nameele. settext ("new name"); // return xmlstring docxmltext = docu as a string. asxml ();

 

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.