简体   繁体   English

如何使用python-libvirt启动具有空磁盘的虚拟机?

[英]How to start a virtual machine with an empty disk using python-libvirt?

I am trying to write a script that creates a virtual machine from scratch. 我正在尝试编写一个从头开始创建虚拟机的脚本。 I am using python 3 and libvirt to achieve this. 我正在使用python 3和libvirt实现此目的。 I know there's easy ways using commandline tools, but for my specific application, I can't use those. 我知道使用命令行工具很简单,但是对于我的特定应用程序,我不能使用它们。

My virtual machine is defined as such: 我的虚拟机定义如下:

<domain type='qemu'>
  <name>QEMU_test</name>
  <memory>219200</memory>
  <currentMemory>219200</currentMemory>
  <vcpu>2</vcpu>
  <os>
    <type arch='x86_64' machine='pc'>hvm</type>
    <boot dev='cdrom'/>
    <boot dev='hd'/>
  </os>
  <devices>
    <emulator>/usr/bin/qemu-system-x86_64</emulator>
    <disk type='file' device='cdrom'>
      <driver name='qemu' type='raw' />
      <source file='/vm/ubuntu-18.04.2-desktop-amd64.iso'/>
      <target dev='hdc'/>
      <readonly/>
    </disk>
    <disk type='file' device='disk'>
      <driver name='qemu' type='raw' />
      <source file='/vm/test/test2.img'/>
      <target dev='hda'/>
    </disk>
    <graphics type='vnc' port='-1'/>
    <interface type='network'>
      <source network='default'/>
      <model type='virtio' />
      <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
      <mac address="00:11:22:33:44:55"/>

    </interface>
  </devices>
</domain>

All I am doing is creating a connection to 'qemu:///system', reading the XML configuration file into a variable and executing 'createXML' to create a transient guest domain. 我要做的就是创建与“ qemu:/// system”的连接,将XML配置文件读取到变量中,并执行“ createXML”以创建临时来宾域。

The issue I have is probably partly due to my lack of understanding of how Operating Systems start up. 我遇到的问题可能部分是由于我对操作系统的启动方式缺乏了解。 The file 'test2.img' is a file generated using '/dev/zero'. 文件“ test2.img”是使用“ / dev / zero”生成的文件。 It was created following the process described here: Python libvirt API - create a Virtual Machine 它是按照此处描述的过程创建的: Python libvirt API-创建虚拟机

Trying to start this machine results in kernel panic: 尝试启动这台机器会导致内核崩溃:

Kernel Panic - not syncing: VFS: Unable to mount root fs on unknown-block(0,0)

I am guessing this is because the 'initramfs' parameter in the XML needs to be specified for empty disks, or otherwise there needs to be a way to partition the disk so that the expected root partition exists. 我猜这是因为需要为空磁盘指定XML中的'initramfs'参数,否则需要有一种分区磁盘的方法,以便存在预期的根分区。

My question then is: Is there a portable way to do any of that using Python? 那么我的问题是:是否有一种可移植的方式使用Python来完成上述任务?

I don't want to explicitly specify any paths as I want this script to be as portable as possible (unless there's a standard I can use for all Operating Systems that I'm missing). 我不想显式地指定任何路径,因为我希望此脚本尽可能地可移植(除非存在我可以用于所有缺少的操作系统的标准)。 I'd also prefer if I didn't have to distribute anything except the script to have it work, if possible. 如果可能的话,我也希望不需要分发脚本以外的任何东西来使其工作。

Edit to clarify behaviour: If I use a Debian CD, I can see the initial menu pop up with the installation options, however once I select one, a kernel panic is caused. 编辑以阐明行为:如果我使用Debian CD,可以看到带有安装选项的弹出初始菜单,但是一旦选择一个,就会引起内核崩溃。

Using /dev/zero to create a 'raw' disk image should work, but generally it's better to use 'qemu-img' for these types of things. 使用/ dev / zero创建“原始”磁盘映像应该可以工作,但是通常最好将“ qemu-img”用于这些类型的事情。 Either way, just creating a disk image like that and then trying to boot off of it won't work as there's no operating system inside it to boot from. 无论哪种方式,仅创建类似的磁盘映像然后尝试从中引导都将不起作用,因为其中没有可从中引导的操作系统。

Is the VM actually booting off of the ubuntu CDROM media? VM实际上是从ubuntu CDROM介质启动的吗? I would expect that to work even if test2.img is totally empty, and then you can use the ubuntu installer to populate test2.img. 我希望即使test2.img完全为空也能正常工作,然后您可以使用ubuntu安装程序填充test2.img。

Well shit. 妈的 After messing around with the --debug switch and minimizing the configuration that virt-* tools create as a default, I finally found out why it wasn't working: 在弄乱了--debug开关并最小化了virt- *工具默认创建的配置之后,我终于找到了为什么它不起作用:

I didn't give the VM enough memory. 我没有给虚拟机足够的内存。

This results in different kernel panics for different Operating Systems. 对于不同的操作系统,这会导致不同的内核崩溃。 For example, Ubuntu complains that rootfs cannot be found. 例如,Ubuntu抱怨找不到rootfs。 Debian complains that it failed to kill init. Debian抱怨说它没有杀死init。

The default unit for memory is KibiBytes, which is 1024 bytes. 内存的默认单位是KibiBytes,即1024字节。 This means my configuration gave the VM a little over 214 Mebibytes (2^20 bytes) to work with. 这意味着我的配置使VM可以使用214 MB以上(2 ^ 20字节)。 Turns out you can't do much with that except complain. 事实证明,除了抱怨之外,您对此无能为力。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM