简体   繁体   中英

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. 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.

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'. It was created following the process described here: Python libvirt API - create a Virtual Machine

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.

My question then is: Is there a portable way to do any of that using 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.

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. 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? 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.

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:

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. Debian complains that it failed to kill init.

The default unit for memory is KibiBytes, which is 1024 bytes. This means my configuration gave the VM a little over 214 Mebibytes (2^20 bytes) to work with. Turns out you can't do much with that except complain.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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