Scripting qemu / kvm monitor

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

Here is how.

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:

#!/bin/bash
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/change-vnc-pass.sh

Leave a Reply