Scripting qemu / kvm monitor

From lxadm | Linux administration tips, tutorials, HOWTOs and articles
Jump to: navigation, search

There are some tasks in qemu / kvm monitor which could be scripted (i.e. like changing VNC password on demand).

Here is how to do it with netcat and minicom.

Using netcat (nc)[edit]

Use this option:

-monitor tcp:,server,nowait

Then you can do:

RESP=`echo “change vnc password” | nc 1234`
echo “$RESP” | grep -q “Password:”
if [ $RC -eq 0 ]; then
echo “Command OK”
echo “some-password” | nc 1234

You can also use nc -U to access the unix socket file.

Using minicom[edit]

First, you need to tell qemu / kvm process to have a monitor accessible via a UNIX socket – if you use Proxmox VE, it will be created automatically, if not, just use such a command line parameter:

-monitor unix:/var/run/qemu-server/105.mon,server,nowait

Where /var/run/qemu-server/105.mon is the path to the socket.

Next, install minicom and create the /etc/minicom/minirc.105-mon file with the contents like below – this will tell minicom to connect to the serial monitor console of this qemu / kvm quest:

pu port             unix#/var/run/qemu-server/105.mon
pu minit
pu mreset

Next, minicom needs to execute a script – an example for changing a VNC password below (save it as “change-vnc-pass”; see “man runscript” for more syntax help):

send \r\r
send "change vnc password"
expect "Password:"
send some-password

The last thing would be to start minicom and execute these commands:

minicom 105-mon -S /some/path/change-vnc-pass &
sleep 3s
kill $!

That’s it!

If you’d like to start it from crontab, you have to use screen – otherwise, minicom will complain about “No cursor motion capability (cm)” when started using cron:

50 * * * *      root    screen -d -m /some/path/