http://blog.csdn.net/whuqin/article/details/6732898
1.创建域(虚拟机):包括Volume和描述硬件配置的xml
//前端与节点建立连接
Connect conn=new Connect("qemu+ssh://root@192.168.69/system",false);
//根据xml描述创建新虚拟机并启动
SAXReader reader = new SAXReader();
Document docu =reader.read(new File("/domain.xml"));
String xmlDesc=docu.asXML();
Domain domain=conn.domainCreateXML(xmlDesc, 0);
domain.resume();
补:新建域可以直接加载镜像(.img),但这样镜像只能被一个用户使用。为可以让镜像能被多个用户使用,可使用StorageVol来关联镜像,虚拟机加载该备份即可。
Connect con=new Connect("");
//根据存储池的名字获得StoragePool
StoragePool pool=con.storagePoolLookupByName("default");
SAXReader reader=new SAXReader();
Document docu=reader.read(new File("/volume.xml"));
StorageVol vol=pool.storageVolCreateXML(docu.asXML(), 0);
记录下vol的存储地址,赋值到域的配置文件中即可。
删除vol
StorageVol vol=pool.storageVolLookupByName(volName);
vol.delete(0);
补:StorageVol可以作为镜像的快照,大小很小,只记录了对镜像的修改。
2.迁移/销毁域
//前端与节点建立连接
Connect conn1=new Connect("qemu+ssh://root@192.168.82.70/system",false);
//根据域的uuid-unique id(在生成域时,libvirt自动分配唯一的uuid),返回域。
String uuid="";
Domain domain=conn1.domainLookupByUUIDString(uuid);
Connect conn2=new Connect("qemu+ssh://root@192.168.82.71/system",false);
/*
* 把域从当前主机迁移到目的主机
* Domain migrate(Connect dconn, long flags, String dname, String uri, long bandwidth)
* dconn:目的主机
* dname:可选,重命名域在目的机的名字
* uri:可选,目的机ip
* bandwidth:迁移带宽,Mbps
*/
domain.migrate(conn2, 1, null, null, 5);
//销毁域
domain.destroy();
补:域的名字、id等信息都在xml配置描述里定义。对于xml文件的操作使用dom4j。
标准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>
<emulator>/usr/bin/qemu-kvm</emulator>
<disk type='file' device='disk'>
<driver name='qemu' type='qcow2'/>
<source file='/var/lib/libvirt/images/test.img'/><!--运行的镜像-->
<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='0x0000' 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='5910' 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='9216' 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>
标准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> <!--新卷的地址-->
<format type='qcow2'/>
<permissions>
<mode>0600</mode>
<owner>0</owner>
<group>0</group>
</permissions>
</target>
<backingStore>
<path>/var/lib/libvirt/images/WinXP.img</path><!--关联的镜像-->
<format type='qcow2'/>
<permissions>
<mode>0600</mode>
<owner>107</owner>
<group>107</group>
</permissions>
</backingStore>
</volume>
补:dom4j使用示例
SAXReader reader = new SAXReader();
Document docu = reader.read(new File("/domain.xml"));
//更改元素的属性值
Element graphics=docu.getRootElement().element("devices").element("graphics");
Attribute attrPort=graphics.attribute("port");
attrPort.setText(getPort());
//更改元素值
Element nameEle=docu.getRootElement().element("name");
nameEle.setText("new name");
//以字符串形式返回xml
String docXmlText=docu.asXML();