Device-assignment: free device if hotplug fails
[qemu-kvm/fedora.git] / kvm / user / balloon_ctl.c
blobe65b08d592baf222b40544be7509561b72688ad4
1 /*
2 * This binary provides access to the guest's balloon driver
3 * module.
5 * Copyright (C) 2007 Qumranet
7 * Author:
9 * Dor Laor <dor.laor@qumranet.com>
11 * This work is licensed under the GNU LGPL license, version 2.
14 #include <unistd.h>
15 #include <fcntl.h>
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <sys/mman.h>
19 #include <string.h>
20 #include <errno.h>
21 #include <sys/ioctl.h>
23 #define __user
24 #include <linux/kvm.h>
26 #define PAGE_SIZE 4096ul
29 static int balloon_op(int *fd, int bytes)
31 struct kvm_balloon_op bop;
32 int r;
34 bop.npages = bytes/PAGE_SIZE;
35 r = ioctl(*fd, KVM_BALLOON_OP, &bop);
36 if (r == -1)
37 return -errno;
38 printf("Ballon handled %d pages successfully\n", bop.npages);
40 return 0;
43 static int balloon_init(int *fd)
45 *fd = open("/dev/kvm_balloon", O_RDWR);
46 if (*fd == -1) {
47 perror("open /dev/kvm_balloon");
48 return -1;
51 return 0;
54 int main(int argc, char *argv[])
56 int fd;
57 int r;
58 int bytes;
60 if (argc != 3) {
61 perror("Please provide op=[i|d], bytes\n");
62 return 1;
64 bytes = atoi(argv[2]);
66 switch (*argv[1]) {
67 case 'i':
68 break;
69 case 'd':
70 bytes = -bytes;
71 break;
72 default:
73 perror("Wrong op param\n");
74 return 1;
77 if (balloon_init(&fd)) {
78 perror("balloon_init failed\n");
79 return 1;
82 if ((r = balloon_op(&fd, bytes))) {
83 perror("balloon_op failed\n");
84 goto out;
87 out:
88 close(fd);
90 return r;