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