xref: /kvmtool/builtin-balloon.c (revision 0725673ad39930be912cfcb0600eb4d5f9188b14)
1 #include <stdio.h>
2 #include <string.h>
3 #include <signal.h>
4 
5 #include <kvm/util.h>
6 #include <kvm/kvm-cmd.h>
7 #include <kvm/builtin-balloon.h>
8 #include <kvm/parse-options.h>
9 #include <kvm/kvm.h>
10 
11 static const char * const balloon_usage[] = {
12 	"kvm balloon {inflate|deflate} <size in MiB> <instance name>",
13 	NULL
14 };
15 
16 static const struct option balloon_options[] = {
17 	OPT_END()
18 };
19 
20 void kvm_balloon_help(void)
21 {
22 	usage_with_options(balloon_usage, balloon_options);
23 }
24 
25 int kvm_cmd_balloon(int argc, const char **argv, const char *prefix)
26 {
27 	int pid;
28 	int amount, i;
29 	int inflate = 0;
30 
31 	if (argc != 3)
32 		kvm_balloon_help();
33 
34 	pid = kvm__get_pid_by_instance(argv[2]);
35 	if (pid < 0)
36 		die("Failed locating instance name");
37 
38 	if (strcmp(argv[0], "inflate") == 0)
39 		inflate = 1;
40 	else if (strcmp(argv[0], "deflate"))
41 		die("command can be either 'inflate' or 'deflate'");
42 
43 	amount = atoi(argv[1]);
44 
45 	for (i = 0; i < amount; i++)
46 		kill(pid, inflate ? SIGKVMADDMEM : SIGKVMDELMEM);
47 
48 	return 0;
49 }
50