xref: /kvmtool/builtin-balloon.c (revision c9cba7910f52a48ecc99c9976bab69883aa3ef58)
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 int kvm_cmd_balloon(int argc, const char **argv, const char *prefix)
21 {
22 	int pid;
23 	int amount, i;
24 	int inflate = 0;
25 
26 	if (argc != 3)
27 		usage_with_options(balloon_usage, balloon_options);
28 
29 	pid = kvm__get_pid_by_instance(argv[2]);
30 	if (pid < 0)
31 		die("Failed locating instance name");
32 
33 	if (strcmp(argv[0], "inflate") == 0)
34 		inflate = 1;
35 	else if (strcmp(argv[0], "deflate"))
36 		die("command can be either 'inflate' or 'deflate'");
37 
38 	amount = atoi(argv[1]);
39 
40 	for (i = 0; i < amount; i++)
41 		kill(pid, inflate ? SIGKVMADDMEM : SIGKVMDELMEM);
42 
43 	return 0;
44 }
45