gpt4 book ai didi

java - 从 .xml 文件创建 .img 文件,以便使用 libvirt api 和 java 在 ubuntu 中创建 kvm

转载 作者:行者123 更新时间:2023-12-01 04:49:23 27 4
gpt4 key购买 nike

我正在尝试使用 libvirt api 使用 java 创建一个 kvm。我面临的问题是我需要一个设备(图像文件(例如:kvm.img))。我正在使用 .xml 文件使用 libvirt api 和 java 创建 kvm。现在我正在做的是首先使用从终端创建的 qemu-img 创建 .img 文件。 qemu-img create /var/lib/libvirt/images/kvm.img 10G是我正在使用的代码,在 xml 中我提到创建 kvm <source file='var/lib/libvirt/images/kvm.img'/>

现在我需要知道libvirt api中是否有任何方法可以通过传递xml文件来创建.img文件,并且我也需要了解xml文件(创建img文件)

我使用的是 libvirt 1.0

我用来创建 kvm 的 xml 文件是: <domain type='kvm'><name>ft</name><memory>131072</memory><currentMemory>131072</currentMemory><vcpu>1</vcpu><os><type arch='x86_64' machine='pc-0.12'>hvm</type><boot dev='hd'/></os><features><acpi/></features> <clock offset='utc'/> <on_poweroff>destroy</on_poweroff><on_reboot>restart</on_reboot><on_crash>destroy</on_crash> <devices><emulator>/usr/bin/kvm</emulator><disk type='file' device='disk'><source file='var/lib/libvirt/images/ft.img'/><target dev='hda' bus='ide'/></disk><interface type='network'><mac address='52:54:00:8b:08:dd'/><source network='default'/><model type='virtio'/></interface><input type='mouse' bus='ps2'/><graphics type='vnc' port='-1' autoport='yes' listen='127.0.0.1'/><video><model type='cirrus' vram='9216' heads='1'/> </video></devices></domain>

在java中使用libvirt方法作为Domain createVm = con.domainCreateXML(str, 0);str我正在传递 xml 字符串

最佳答案

镜像文件的创建不在 libvirt API 之外,因为是 qemu 关心实际的磁盘设备。从技术上讲,VM 磁盘设备可以是原始镜像文件(如 *.img),也可以是循环设备或存储设备(如映射的 iSCSI 目标),而 libvirt 不知道它来自哪里。

因此您需要自行处理虚拟机磁盘文件的创建。 qemu-imgdd 都可以完成这项工作。请参阅此问题的答案 question 。如果您只想为 KVM 虚拟机创建原始文件,实际上您只需要通过 Java File API 创建一个稀疏文件。查看问题Create file with given size in Java ,它的工作与 qemu-img create 完全相同。

关于java - 从 .xml 文件创建 .img 文件,以便使用 libvirt api 和 java 在 ubuntu 中创建 kvm,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15244096/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com