From 271bb47d8bb4d545f85543baf26a088a9201d130 Mon Sep 17 00:00:00 2001 From: KasuganoSoras Date: Fri, 28 Sep 2018 03:45:10 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20libvirt.php?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 支持创建虚拟机了 --- libvirt/libvirt.php | 192 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 192 insertions(+) diff --git a/libvirt/libvirt.php b/libvirt/libvirt.php index e3459bb..b837bd8 100644 --- a/libvirt/libvirt.php +++ b/libvirt/libvirt.php @@ -200,4 +200,196 @@ class Libvirt { } return $data; } + + /** + * + * createVMXML 创建新的虚拟机配置并上传至服务器 + * + * @param $server 名称 + * @param $vcpu CPU 数量 + * @param $memory 运行内存 + * @param $disk 磁盘镜像路径 + * @param $iso ISO 镜像路径 + * @param $boot 首选启动设备 + * @param $network_type 网卡类型 + * @param $network_name 网卡名称 + * @param $network_mac 网卡 MAC 地址 + * @param $network_bridge 网卡桥接名称 + * @param $bandwidth_in 限制下行最大速率(0为不限制) + * @param $bandwidth_out 限制上行最大速率(0为不限制) + * @param $vnc_port VNC 远程连接端口 + * + */ + public function createVMXML($server, $vcpu, $memory, $disk = "", $iso = "", $boot = "hd", $network_type = "network", $network_name = "default", $network_mac = "", $network_bridge = "", $bandwidth_in = 0, $bandwidth_out = 0, $vnc_port = 5900) { + // 这一段写的非常骚气请不要在意 + $template = " + {$server} + {$uuid} + {$memory} + {$memory} + {$vcpu} + + /machine + + + hvm + + + + + + + + + + destroy + restart + destroy + + /usr/libexec/qemu-kvm +"; + // 如果设置了磁盘文件 + if($disk !== "") { + $template .= +" + + + + + +
+ +"; + } + + // 如果设置了 ISO 镜像 + if($iso !== "") { + $template .= +" + + + + + + +
+ +"; + } + + // 判断网络类型,选择不同的标签 + $tag_name = "network"; + if($network_type !== "network") { + $tag_name = "name"; + } + + $template .= +" + +
+ + + + + + +
+ + + + +"; + // 如果设置了最大宽带速率 + if($bandwidth_in !== 0 && $bandwidth_out !== 0) { + $template .= +" + + + +"; + } + $template .= +" + + +
+ + + + + + + + + + +