VirTips wind between the clouds

Editing files inside vm image

There are some situations when you have to edit file from vm image and you can’t or don’t want to start virtual machine to do it.

Old way

If you have raw image without partitons you can just mount it via loop device:

If you have paritions it’s getting more complicated. You can play with offset option for mount command or for example you can use program called kpartx.

Then you can mount partitions from /mnt/mappert/loop0p1 and so on. It’s getting even more complicated when you are using no raw image formats, for example qcow2. Then you can use program called qemu-nbd.

virt-edit for rescue

All this are old days. Currently you can just use already mentioned on this blog project libguestfs. There is a program virt-edit. Just run:

Change file, save it and quit program. That’s all what you need.

You can also try guestfish and virt-rescue programs.

Reseting, restarting or rebooting virtual machine

Libvirt gives you a several ways to reboot virtual machine:

  • reboot – by default it sends ACPI request to VM. It can be ignored(acpid daemon required in vm)
  • reset - emulates the power reset button on a machine
  • send ctrl-alt-del keys

Some examples in python:
Reboot

Reset:

Send ctrl-alt-del:

 

Get network stats for virtual machine

It retunrs values in bytes.

Read more about interfaceStats in libvirt C API docs.

Getting disk space usage from running VM.

There is amazing tool which may help in many administrative tasks. It is called libguestfs. To get information about disk space usage you can use program virt-df.
To get disks stats from all running vms just run(libvirt required):

To get disks stats from specific vm:

If you do not have libvirt, you can manually specify disk

Libguestfs is mainly written in C, but it provides bindings for other languages : C++, Erlang, Java, OCaml, Perl, Python, Ruby.
For example to get  disk stats in python you can use this code:

 

Attaching disk via libvirt using python

In VM load modules acpiphp, pci_hotplug. Read more on KVM site

Changing qemu VNC password via libvirt using python

 

Follow

Get every new post delivered to your Inbox

Join other followers