Virtio Balloon in action — with native linux kvm tool

Over the weekend, for fun, I decided to tinker with the virtio ballon feature. For simplicity’s sake, I tested it with native linux kvm tool. I previously wrote about it here

If you want to get a quick refresher about virtio balloon concept, a while ago, Richard W Jones made an excellent post w/ nice illustration using images over here

Before I went ahead, I pulled the latest git, and applied a couple of patches from Sasha Levin(nlkt dev), which are on kvm mailing list, which fix the behaviour of kvm tool commands ‘stat’, ‘debug’ and a couple other things. Then I compiled the kvm tool, built the kernel bzImage, on a Fedora 16 box. Here is how it went:

First, setup a new rootfs(which will be stored under ~/.kvm-tools/default), and will mount your host file system in a read-only mode in the guest:


$ ./kvm setup default
  Info: Your new rootfs named default has been created.
You can now start it by running 'kvm run -d default'

$

Then, boot the kernel bzImage with the virtio ballon option. Once, booted, guest shell access over a serial console will be presented.
(Also, the below command line will allocate the guest memory, guest vcpu depending on the host configuration)


$ ./kvm run --balloon -k ../../arch/x86/boot/bzImage                                                                                                                                            
  # kvm run -k ../../arch/x86/boot/bzImage -m 448 -c 4 --name guest-31362
.
.
.
[    1.505724] Bluetooth: BNEP filters: protocol multicast
[    1.508899] Bluetooth: HIDP (Human Interface Emulation) ver 1.2
[    1.511511] lib80211: common routines for IEEE802.11 drivers
[    1.513885] Installing 9P2000 support
[    1.517080] Registering the dns_resolver key type
[    1.518737]   Magic number: 15:85:793
[    1.520495] BIOS EDD facility v0.16 2004-Jun-25, 0 devices found
[    1.523497] EDD information not available.
[    1.537035] Sending DHCP requests ., OK
[    1.558038] IP-Config: Got DHCP answer from 192.168.33.1, my address is 192.168.33.15
[    1.560257] IP-Config: Complete:
[    1.561163]      device=eth0, addr=192.168.33.15, mask=255.255.255.0, gw=192.168.33.1,
[    1.563257]      host=192.168.33.15, domain=, nis-domain=(none),
[    1.564861]      bootserver=192.168.33.1, rootserver=0.0.0.0, rootpath=
[    1.567199] VFS: Unable to mount root fs via NFS, trying floppy.
[    1.574154] VFS: Mounted root (9p filesystem) on device 0:13.
[    1.576080] devtmpfs: mounted
[    1.578602] Freeing unused kernel memory: 512k freed
[    1.580356] Write protecting the kernel read-only data: 12288k
[    1.591640] Freeing unused kernel memory: 1916k freed
[    1.601083] Freeing unused kernel memory: 1436k freed
Mounting...
Starting '/bin/sh'...
sh-4.2# 

Get the guest current memory statistics:


$ ./kvm stat -m --all                                                                  


        *** Guest memory statistics ***

The amount of memory that has been swapped in (in bytes):0
The amount of memory that has been swapped out to disk (in bytes):0
The number of major page faults that have occurred:40
The number of minor page faults that have occurred:495
The amount of memory not being used for any purpose (in bytes):438673408
The total amount of memory available (in bytes):449032192

$ 

List the current running guest instances:


$ ./kvm list -i
  PID GUEST
31362 guest-31362
$ 

Now, on a different terminal, let’s try to inflate/deflate the balloon. The current available memory, is roughly 448MB (Refer above. In bytes — 449032192)

To inflate, which means, to return memory from the guest, run the below command, which returns 10MB of memory back to the guest.


$ ./kvm balloon -n guest-31362 --inflate 10
$ 

Run the memory status again. Now, 10485760 bytes(10MB) is reduced from the guest and returned to the host. (which means, the virtio balloon is inflated)


$ ./kvm stat -m --all


        *** Guest memory statistics ***

The amount of memory that has been swapped in (in bytes):0
The amount of memory that has been swapped out to disk (in bytes):0
The number of major page faults that have occurred:40
The number of minor page faults that have occurred:502
The amount of memory not being used for any purpose (in bytes):428339200
The total amount of memory available (in bytes):438546432

$ 

Now, let’s return back the memory to the guest from the host, by running the deflate command


$ ./kvm balloon -n guest-31362 --deflate 10
$ 

Again, re-check the memory status. 10485760 bytes(10MB) is now returned back to the guest from the host. (which means, the virtio balloon is inflated)


$ ./kvm stat -m --all                                                                  


        *** Guest memory statistics ***

The amount of memory that has been swapped in (in bytes):0
The amount of memory that has been swapped out to disk (in bytes):0
The number of major page faults that have occurred:40
The number of minor page faults that have occurred:502
The amount of memory not being used for any purpose (in bytes):436846592
The total amount of memory available (in bytes):449032192

$ 

Now, like me, if you’re wondering what happens if you reduce the memory( inflate) by 10MB, and try to return back(deflate) 100 MB to the guest, it won’t let you.

Thanks to Sasha for helping me to fix some of the compile errors w/ newest gcc.

Advertisements

Leave a comment

Filed under Uncategorized

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