Execute command inside guest vm Libvirt
-
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 likeperl
orpython
.This has the added benefit that what you want to do will be a LOT easier with
perl
'sExpect.pm
orpython
'spexpect
module than withexpect
itself.expect
's ability to extract data from the session is quite limited and primitive compared to what eitherperl
orpython
can do.BTW, you mentioned
libvirt
's Java API - a quick google search reveals that there are at least two different implementations of anexpect
-like library for Java. There may be more.https://github.com/ronniedong/Expect-for-Java
https://github.com/Alexey1Gavrilov/ExpectIt
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.
Alternatively, if the VM has a network interface with an IP address and has
sshd
installed (or you can install it), you canssh
into the VM as you would any other host. This is, by far, the easiest and best way to do what you want.
Related videos on Youtube
arpit joshi
Updated on September 18, 2022Comments
-
arpit joshi over 1 year
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 .
-
arpit joshi about 8 years: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 .
-
guettli almost 8 yearsThe 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!
-
kbolino over 5 yearsThe 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.