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 #include <kvm/kvm-ipc.h>
11
12 static const char *instance_name;
13 static u64 inflate;
14 static u64 deflate;
15
16 static const char * const balloon_usage[] = {
17 "lkvm balloon [-n name] [-p pid] [-i amount] [-d amount]",
18 NULL
19 };
20
21 static const struct option balloon_options[] = {
22 OPT_GROUP("Instance options:"),
23 OPT_STRING('n', "name", &instance_name, "name", "Instance name"),
24 OPT_GROUP("Balloon options:"),
25 OPT_U64('i', "inflate", &inflate, "Amount to inflate (in MB)"),
26 OPT_U64('d', "deflate", &deflate, "Amount to deflate (in MB)"),
27 OPT_END(),
28 };
29
kvm_balloon_help(void)30 void kvm_balloon_help(void)
31 {
32 usage_with_options(balloon_usage, balloon_options);
33 }
34
parse_balloon_options(int argc,const char ** argv)35 static void parse_balloon_options(int argc, const char **argv)
36 {
37 while (argc != 0) {
38 argc = parse_options(argc, argv, balloon_options, balloon_usage,
39 PARSE_OPT_STOP_AT_NON_OPTION);
40 if (argc != 0)
41 kvm_balloon_help();
42 }
43 }
44
kvm_cmd_balloon(int argc,const char ** argv,const char * prefix)45 int kvm_cmd_balloon(int argc, const char **argv, const char *prefix)
46 {
47 int instance;
48 int r;
49 int amount;
50
51 parse_balloon_options(argc, argv);
52
53 if (inflate == 0 && deflate == 0)
54 kvm_balloon_help();
55
56 if (instance_name == NULL)
57 kvm_balloon_help();
58
59 instance = kvm__get_sock_by_instance(instance_name);
60
61 if (instance <= 0)
62 die("Failed locating instance");
63
64 if (inflate)
65 amount = inflate;
66 else if (deflate)
67 amount = -deflate;
68 else
69 kvm_balloon_help();
70
71 r = kvm_ipc__send_msg(instance, KVM_IPC_BALLOON,
72 sizeof(amount), (u8 *)&amount);
73
74 close(instance);
75
76 if (r < 0)
77 return -1;
78
79 return 0;
80 }
81