一个用于管理 Libvirt KVM 虚拟机的 PHP 工具类,只需几行代码就能轻松创建、修改、控制虚拟机。
Go to file
Akkariin Meiko 1839128ad1 [+] 修复手滑bug
修复了手滑写错的bug
增加了 example 的注释
2018-10-10 02:12:21 +08:00
libvirt [+] 修复手滑bug 2018-10-10 02:12:21 +08:00
.gitattributes Initial commit 2018-09-28 01:33:48 +08:00
LICENSE Initial commit 2018-09-28 01:33:48 +08:00
README.md Update README.md 2018-09-28 15:10:49 +08:00
README_ZH.md Update README_ZH.md 2018-09-28 15:10:08 +08:00
example.php [+] 修复手滑bug 2018-10-10 02:12:21 +08:00

README.md

Libvirt-Manager

中文 ReadMe | ZeroDream

This is a simple php libvirt manager

It can start, shutdown, destory, get the virtual machines list or get the information for any virtual machine.

This project is to make it easier for developers to manage virtual machines.

You need install php_ssh2 module to use this manager.

Maybe this document has some grammatical mistakes, because the author is Chinese :P

Example code for connect a server

include("libvirt/libvirt.php");
$Libvirt = new Libvirt();
$Libvirt->setHost("192.168.3.181", 22, "/data/libvirt/");
$Libvirt->connect("root", "123456");

This code will connect to your server using SSH, make sure your server sshd service is running.

The 192.168.3.181 is your server hostname, 22 is your server port, and /data/libvirt/ is your libvirt data save path.

For authenticate, use username and password, the username and password in example code is root and 123456.

Example code for create a virtual machine

$Libvirt->createDisk("Test", "qcow2", "30G");
$Libvirt->createVMXML("Test", 2, 2048576, "/data/libvirt/images/Test/Test.qcow2", "/data/iso/CentOS-7-x86_64-Minimal-1804.iso", "cdrom", "network", "default", $Libvirt->randomMac(), "virbr0", 0, 0, 5902);
$Libvirt->define("/data/libvirt/Test.xml");
$Libvirt->setPermission("Test");
$Libvirt->start("Test");

Create a virtual disk

String createDisk ( Name, Format, Size )

create a Virtual Machine xml config file

The method of createVMXML have 13 params.

void createVMXL ( Name, vCPU, Ram, Disk, ISO, Boot Device, Network type, Network name, MAC Address, Network bridge, Bandwidth in, Bandwidth out, VNC Port )

Register the xml config file to system

String define ( XML File Path )

Set execute permission

void setPermission ( Name )

Start the virtual machine

String start ( Name )

Stop the virtual machine

String shutdown ( Name )

Force stop the virtual machine

if your virtual machine is in trouble and can't use shutdown to make if off, you can use this method to force stop it.

String destroy ( Name )

Get the virtual machine list

You can use this method to get the virtual machine list, It will return an array.

String getList ()

Get the virtual machine information

You can use this method to get any registed virtual machine information, It will return an array.

String getInfo ( Name )

Dump the virtual machine xml config file

This method can get any registered virtual machine xml config file and return.

String dumpxml ( Name )

Clone an exist virtual machine

You can use this method to clone a exist virtual machine.

This method may take a long time, you need add a line set_time_limit(120) in your code.

String cloneVM ( Name, New name, New disk path )

Set the virtual machine network

This method can control the virtual machine network.

The third param is Boolean type, if you give it a true value, the network will enable, else the network will disable.

String setNetwork ( Server, Network name, Status )

You can find out more info in libvirt/libvirt.php