* [PATCH 2/3] kvm tools: Introduce KVM_IPC_VMSTATE IPC cmd
2011-12-21 12:13 [PATCH 1/3] kvm tools: Handle multiple IPC cmd at a time Asias He
@ 2011-12-21 12:13 ` Asias He
2011-12-21 12:13 ` [PATCH 3/3] kvm tools: Respect paused status in ./lkvm list Asias He
2011-12-21 20:30 ` [PATCH 1/3] kvm tools: Handle multiple IPC cmd at a time Pekka Enberg
2 siblings, 0 replies; 5+ messages in thread
From: Asias He @ 2011-12-21 12:13 UTC (permalink / raw)
To: Pekka Enberg; +Cc: Sasha Levin, Cyrill Gorcunov, Ingo Molnar, kvm, Asias He
This can be used to get vm status information:
vm is running or pasued.
Signed-off-by: Asias He <asias.hejun@gmail.com>
---
tools/kvm/builtin-run.c | 20 +++++++++++++++++---
tools/kvm/include/kvm/kvm-ipc.h | 1 +
tools/kvm/include/kvm/kvm.h | 5 +++++
tools/kvm/powerpc/include/kvm/kvm-arch.h | 1 +
tools/kvm/x86/include/kvm/kvm-arch.h | 2 ++
5 files changed, 26 insertions(+), 3 deletions(-)
diff --git a/tools/kvm/builtin-run.c b/tools/kvm/builtin-run.c
index fac274c..2f48685 100644
--- a/tools/kvm/builtin-run.c
+++ b/tools/kvm/builtin-run.c
@@ -507,17 +507,30 @@ static int is_paused;
static void handle_pause(int fd, u32 type, u32 len, u8 *msg)
{
- if (type == KVM_IPC_RESUME && is_paused)
+ if (type == KVM_IPC_RESUME && is_paused) {
+ kvm->vm_state = KVM_VMSTATE_RUNNING;
kvm__continue();
- else if (type == KVM_IPC_PAUSE && !is_paused)
+ } else if (type == KVM_IPC_PAUSE && !is_paused) {
+ kvm->vm_state = KVM_VMSTATE_PAUSED;
kvm__pause();
- else
+ } else
return;
is_paused = !is_paused;
pr_info("Guest %s\n", is_paused ? "paused" : "resumed");
}
+static void handle_vmstate(int fd, u32 type, u32 len, u8 *msg)
+{
+ int r = 0;
+
+ if (type == KVM_IPC_VMSTATE)
+ r = write(fd, &kvm->vm_state, sizeof(kvm->vm_state));
+
+ if (r < 0)
+ pr_warning("Failed sending VMSTATE");
+}
+
static void handle_debug(int fd, u32 type, u32 len, u8 *msg)
{
int i;
@@ -862,6 +875,7 @@ int kvm_cmd_run(int argc, const char **argv, const char *prefix)
kvm_ipc__register_handler(KVM_IPC_PAUSE, handle_pause);
kvm_ipc__register_handler(KVM_IPC_RESUME, handle_pause);
kvm_ipc__register_handler(KVM_IPC_STOP, handle_stop);
+ kvm_ipc__register_handler(KVM_IPC_VMSTATE, handle_vmstate);
nr_online_cpus = sysconf(_SC_NPROCESSORS_ONLN);
diff --git a/tools/kvm/include/kvm/kvm-ipc.h b/tools/kvm/include/kvm/kvm-ipc.h
index 731767f..c68b89c 100644
--- a/tools/kvm/include/kvm/kvm-ipc.h
+++ b/tools/kvm/include/kvm/kvm-ipc.h
@@ -17,6 +17,7 @@ enum {
KVM_IPC_RESUME = 5,
KVM_IPC_STOP = 6,
KVM_IPC_PID = 7,
+ KVM_IPC_VMSTATE = 8,
};
int kvm_ipc__register_handler(u32 type, void (*cb)(int fd, u32 type, u32 len, u8 *msg));
diff --git a/tools/kvm/include/kvm/kvm.h b/tools/kvm/include/kvm/kvm.h
index 75adb25..d88ff74 100644
--- a/tools/kvm/include/kvm/kvm.h
+++ b/tools/kvm/include/kvm/kvm.h
@@ -21,6 +21,11 @@
.name = #ext, \
.code = ext
+enum {
+ KVM_VMSTATE_RUNNING,
+ KVM_VMSTATE_PAUSED,
+};
+
struct kvm_ext {
const char *name;
int code;
diff --git a/tools/kvm/powerpc/include/kvm/kvm-arch.h b/tools/kvm/powerpc/include/kvm/kvm-arch.h
index 10aa2d9..c4b493c 100644
--- a/tools/kvm/powerpc/include/kvm/kvm-arch.h
+++ b/tools/kvm/powerpc/include/kvm/kvm-arch.h
@@ -65,6 +65,7 @@ struct kvm {
unsigned long initrd_gra;
unsigned long initrd_size;
const char *name;
+ int vm_state;
};
#endif /* KVM__KVM_ARCH_H */
diff --git a/tools/kvm/x86/include/kvm/kvm-arch.h b/tools/kvm/x86/include/kvm/kvm-arch.h
index 1ce3d31..244d36c 100644
--- a/tools/kvm/x86/include/kvm/kvm-arch.h
+++ b/tools/kvm/x86/include/kvm/kvm-arch.h
@@ -48,6 +48,8 @@ struct kvm {
int nr_disks;
const char *name;
+
+ int vm_state;
};
static inline void *guest_flat_to_host(struct kvm *kvm, unsigned long offset); /* In kvm.h */
--
1.7.7.3
^ permalink raw reply related [flat|nested] 5+ messages in thread
* [PATCH 3/3] kvm tools: Respect paused status in ./lkvm list
2011-12-21 12:13 [PATCH 1/3] kvm tools: Handle multiple IPC cmd at a time Asias He
2011-12-21 12:13 ` [PATCH 2/3] kvm tools: Introduce KVM_IPC_VMSTATE IPC cmd Asias He
@ 2011-12-21 12:13 ` Asias He
2011-12-21 20:30 ` [PATCH 1/3] kvm tools: Handle multiple IPC cmd at a time Pekka Enberg
2 siblings, 0 replies; 5+ messages in thread
From: Asias He @ 2011-12-21 12:13 UTC (permalink / raw)
To: Pekka Enberg; +Cc: Sasha Levin, Cyrill Gorcunov, Ingo Molnar, kvm, Asias He
'./lkvm list' does not give the correct vm status if the vm is pasued
currently.
This patch fixes this by using KVM_IPC_VMSTATE IPC cmd to query the vm
status.
Suppose the guest is paused by ./lkvm pause:
Before:
asias@hj$ ./lkvm list
PID NAME STATE
------------------------------------
3036 guest-3036 running
default shut off
After:
asias@hj$ ./lkvm list
PID NAME STATE
------------------------------------
3036 guest-3036 paused
default shut off
Signed-off-by: Asias He <asias.hejun@gmail.com>
---
tools/kvm/builtin-list.c | 35 +++++++++++++++++++++++++++++++++--
1 files changed, 33 insertions(+), 2 deletions(-)
diff --git a/tools/kvm/builtin-list.c b/tools/kvm/builtin-list.c
index ec1c067..e74b636 100644
--- a/tools/kvm/builtin-list.c
+++ b/tools/kvm/builtin-list.c
@@ -16,6 +16,11 @@ struct pid_cmd {
u32 len;
};
+struct vmstate_cmd {
+ u32 type;
+ u32 len;
+};
+
static bool run;
static bool rootfs;
@@ -32,6 +37,7 @@ static const struct option list_options[] = {
};
#define KVM_INSTANCE_RUNNING "running"
+#define KVM_INSTANCE_PAUSED "paused"
#define KVM_INSTANCE_SHUTOFF "shut off"
void kvm_list_help(void)
@@ -56,12 +62,34 @@ static pid_t get_pid(int sock)
return pid;
}
+static int get_vmstate(int sock)
+{
+ struct vmstate_cmd cmd = {KVM_IPC_VMSTATE, 0};
+ int r;
+ int vmstate;
+
+ r = write(sock, &cmd, sizeof(cmd));
+ if (r < 0)
+ return r;
+
+ r = read(sock, &vmstate, sizeof(vmstate));
+ if (r < 0)
+ return r;
+
+ return vmstate;
+
+}
+
static int print_guest(const char *name, int sock)
{
char proc_name[PATH_MAX];
char *comm = NULL;
FILE *fd;
- pid_t pid = get_pid(sock);
+ pid_t pid;
+ int vmstate;
+
+ pid = get_pid(sock);
+ vmstate = get_vmstate(sock);
sprintf(proc_name, "/proc/%d/stat", pid);
fd = fopen(proc_name, "r");
@@ -70,7 +98,10 @@ static int print_guest(const char *name, int sock)
if (fscanf(fd, "%*u (%as)", &comm) == 0)
goto cleanup;
- printf("%5d %-20s %s\n", pid, name, KVM_INSTANCE_RUNNING);
+ if (vmstate == KVM_VMSTATE_PAUSED)
+ printf("%5d %-20s %s\n", pid, name, KVM_INSTANCE_PAUSED);
+ else
+ printf("%5d %-20s %s\n", pid, name, KVM_INSTANCE_RUNNING);
free(comm);
--
1.7.7.3
^ permalink raw reply related [flat|nested] 5+ messages in thread