Is it possible to execute a command for eg:iostat inside a guest VM using libvirt or virsh .I want to find out IOPS for a guest vm but with libvirt java api I am not able to get the IOPS of a vm ,hence another method would be to execute a command like iostat inside a guest vm and take down the readings .

  1. If you have set up a virtual serial console for your VM, you can use the virsh console command to connect to it.

    You will have to use an expect script to login with username and password, run your command(s), extract the output, and then logout.

    Note, though, that expect is a single-purpose language and, IMO, there's no point in learning/using it if you don't already know it. You are much better off putting the time it would take to learn it into learning a general purpose language like perl or python.

    This has the added benefit that what you want to do will be a LOT easier with perl's Expect.pm or python's pexpect module than with expect itself. expect's ability to extract data from the session is quite limited and primitive compared to what either perl or python can do.

    BTW, you mentioned libvirt's Java API - a quick google search reveals that there are at least two different implementations of an expect-like library for Java. There may be more.



    I'm not a Java programmer, have never used either of these, have no idea if they're any good, and can not recommend one over the other.

  2. Alternatively, if the VM has a network interface with an IP address and has sshd installed (or you can install it), you can ssh into the VM as you would any other host. This is, by far, the easiest and best way to do what you want.

| improve this answer | |
  • :yes with Jsch indeed I can do ssh into the vm and execute commands .This was my option if we can't get IOPS with use of libvirt java api.As with libvirt java api seems doesnt have any thing to get iops for a guest vm . – arpit joshi Apr 26 '16 at 13:25
  • The hypervisor has full access to the VM. It's sad, that there is no easy password-less method to execute a command. SSH-Hostkey verification and pub-key exchange is doable, but irritating. Dear cas, don't get me wrong. That's not your fault. Thank your for your answer! – guettli Jul 29 '16 at 13:10
  • The hypervisor has full access to the VM, but not full knowledge of how the guest operating system works. For some types of virtualization, the access is too low-level to safely do things like execute system calls or pass commands to a running shell. – kbolino Jan 3 '19 at 3:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.