Home > Uncategorized > Guest Automation with QEMU/KVM

Guest Automation with QEMU/KVM

Recently, working on some project, I had to do some benchmarking on qemu. This is nothing but a two step process:

1. run a guest VM with qemu,

2. run some benchmarks inside the guest and collect the results.

Naturally, I looked for options to automate this process. I tried out a bunch of things and it took me couple of days to get to the right tools.

Step 1:

By default, qemu uses SDL to display the VGA output. So, the first step is make this interaction with qemu through stdio. Qemu provides an option for this.

From qemu docs:

-nographic   Normally, QEMU uses SDL to display the VGA output. With this option, you can totally disable graphical output so that QEMU is a simple command line application. The emulated serial port is redirected on the console. Therefore, you can still use QEMU to debug a Linux kernel with a serial console.

So, all you have to do is invoke qemu with -nographic.

qemu -nographic -hda guest.disk

Step 2:

Now that you can interact with your guest (or qemu process) through command line, you have to automate this interaction. The obvious way to do this in python is start the qemu process (with -nographic) with subprocess module and then communicate with that process. But to my surprise, this just didn’t work out for me. So, I looked for some other way.

Later, I found out that the most awesome tool for this kind of jobs is Expect. It is an automation tool for interactive applications written in tcl.

This guide should help you in getting started with Expect. Here is the script to run a guest with qemu using Expect.

#!/usr/bin/expect -f

#starts guest vm, run benchmarks, poweroff

set timeout -1

#Assign a variable to the log file

set log     [lindex $argv 0]

#Start the guest VM

spawn qemu -nographic -hda guest.disk

#Login process

expect “login: “

#Enter username

send “user\r”

#Enter Password

expect “Password: “

send “user\r”

#Do whatever you want to do with in the guest VM. ( Run a process and write result to log )

#poweroff the Guest VM

expect “# “

send “shutdown -h now\r”

Save this as automate_qemu.exp, mark this file as executable and then “./automate_qemu.exp qemu.log”

About these ads
Tags: , ,
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: