kvm tools: Fix home dir resolution
PATCH 1/5 kvm tools: Fix home dir resolution
Previous code didn't properly resolve home directory. Resolve user's home dir using the $HOME enviroment variable.
--- tools/kvm/kvm.c | 12 +++++++----- 1 files changed, 7 insertions(+), 5 deletions(-)
tools/kvm/kvm.c +7 -5
@@ -31,7 +31,8 @@#include <asm/unistd.h> #define DEFINE_KVM_EXIT_REASON (reason) [reason] = #reason -#define KVM_PID_FILE_PATH "~/.kvm-tools/" +#define KVM_PID_FILE_PATH "/.kvm-tools/" +#define HOME_DIR getenv("HOME")const char *kvm_exit_reasons[] = {DEFINE_KVM_EXIT_REASON (KVM_EXIT_UNKNOWN ),
@@ -122,8 +123,9 @@ static void kvm__create_pidfile(struct kvm *kvm)if (!kvm->name)return ; -mkdir (KVM_PID_FILE_PATH ,0777 ); -sprintf (full_name,"%s/%s.pid" ,KVM_PID_FILE_PATH , kvm->name); +sprintf (full_name,"%s/%s" ,HOME_DIR ,KVM_PID_FILE_PATH ); +mkdir (full_name,0777 ); +sprintf (full_name,"%s/%s/%s.pid" ,HOME_DIR ,KVM_PID_FILE_PATH , kvm->name); fd =open (full_name,O_CREAT |O_WRONLY ,0666 );sprintf (pid,"%u\n" ,getpid ());if (write (fd, pid,strlen (pid)) <=0 )
@@ -138,7 +140,7 @@ static void kvm__remove_pidfile(struct kvm *kvm)if (!kvm->name)return ; -sprintf (full_name,"%s/%s.pid" ,KVM_PID_FILE_PATH , kvm->name); +sprintf (full_name,"%s/%s/%s.pid" ,HOME_DIR ,KVM_PID_FILE_PATH , kvm->name);unlink (full_name); }
@@ -147,7 +149,7 @@ int kvm__get_pid_by_instance(const char *name)int fd, pid;char pid_str[10 ], pid_file[PATH_MAX ]; -sprintf (pid_file,"%s/%s.pid" ,KVM_PID_FILE_PATH , name); +sprintf (pid_file,"%s/%s/%s.pid" ,HOME_DIR ,KVM_PID_FILE_PATH , name); fd =open (pid_file,O_RDONLY );if (fd <0 )return -1 ;
Signature · 1 line
1.7.6
PATCH 2/5 kvm tools: Give guest instances default name
Name instances which aren't specifically named by the user (using the --name flag) with the following pattern: 'guest-<pid>'
--- tools/kvm/kvm-run.c | 9 +++++++-- 1 files changed, 7 insertions(+), 2 deletions(-)
tools/kvm/kvm-run.c +7 -2
@@ -466,7 +466,7 @@ void kvm_run_help(void)int kvm_cmd_run (int argc,const char **argv,const char *prefix) {struct virtio_net_parameters net_params; -static char real_cmdline[2048 ]; +static char real_cmdline[2048 ], default_name[20 ];struct framebuffer *fb =NULL ;unsigned int nr_online_cpus;int exit_code =0 ;
@@ -552,6 +552,11 @@ int kvm_cmd_run(int argc, const char **argv, const char *prefix)term_init (); +if (!guest_name) { +sprintf (default_name,"guest-%u" ,getpid ()); + guest_name = default_name; + } + kvm =kvm__init (kvm_dev, ram_size, guest_name);ioeventfd__init ();
@@ -609,7 +614,7 @@ int kvm_cmd_run(int argc, const char **argv, const char *prefix)free (hi); -printf (" # kvm run -k %s -m %Lu -c %d\n" , kernel_filename, ram_size /1024 /1024 , nrcpus); +printf (" # kvm run -k %s -m %Lu -c %d --name %s\n" , kernel_filename, ram_size /1024 /1024 , nrcpus, guest_name);if (!kvm__load_kernel (kvm, kernel_filename, initrd_filename, real_cmdline, vidmode))
Signature · 1 line
1.7.6
PATCH 3/5 kvm tools: Add instance enumerator
In reply to Sasha LevinAdd API kvm__enumerate_instances() to enumerate running guest instances.
--- tools/kvm/kvm.c | 25 +++++++++++++++++++++++++ 1 files changed, 25 insertions(+), 0 deletions(-)
tools/kvm/kvm.c +25 -0
@@ -29,6 +29,7 @@#include <time.h> #include <sys/eventfd.h> #include <asm/unistd.h> +#include <dirent.h> #define DEFINE_KVM_EXIT_REASON (reason) [reason] = #reason#define KVM_PID_FILE_PATH "/.kvm-tools/"
@@ -164,6 +165,30 @@ int kvm__get_pid_by_instance(const char *name)return pid; } +int kvm__enumerate_instances (void (*callback)(const char *name,int pid)) +{ +char full_name[PATH_MAX ]; +int pid; +DIR *dir; +struct dirent entry, *result; + +sprintf (full_name,"%s/%s" ,HOME_DIR ,KVM_PID_FILE_PATH ); + dir =opendir (full_name); + +for (;;) { +readdir_r (dir, &entry, &result); +if (result ==NULL ) +break ; +if (entry.d_type ==DT_REG ) { + entry.d_name[strlen (entry.d_name)-4 ] =0 ; + pid =kvm__get_pid_by_instance (entry.d_name); +callback (entry.d_name, pid); + } + } + +return 0 ; +} +void kvm__delete (struct kvm *kvm) {kvm__stop_timer (kvm);
Signature · 1 line
1.7.6
PATCH 4/5 kvm tools: Add 'kvm list' command
In reply to Sasha LevinUse 'kvm list' to list running virtual guest.
--- tools/kvm/Makefile | 5 +++-- tools/kvm/include/kvm/kvm-list.h | 6 ++++++ tools/kvm/include/kvm/kvm.h | 1 + tools/kvm/kvm-cmd.c | 2 ++ tools/kvm/kvm-list.c | 20 ++++++++++++++++++++ 5 files changed, 32 insertions(+), 2 deletions(-) create mode 100644 tools/kvm/include/kvm/kvm-list.h create mode 100644 tools/kvm/kvm-list.c
tools/kvm/Makefile +3 -2
@@ -57,8 +57,9 @@ OBJS += uip/csum.o OBJS += kvm-cmd.o OBJS += kvm-debug.o OBJS += kvm-help.o -OBJS += kvm-pause.o -OBJS += kvm-balloon.o +OBJS += kvm-pause.o +OBJS += kvm-balloon.o +OBJS += kvm-list.o OBJS += kvm-run.o OBJS += mptable.o OBJS += rbtree.o
tools/kvm/include/kvm/kvm-list.h new file +6 -0
@@ -0,0 +1,6 @@ +#ifndef KVM__LIST_H +#define KVM__LIST_H + +int kvm_cmd_list (int argc,const char **argv,const char *prefix); + +#endif
tools/kvm/include/kvm/kvm.h +1 -0
@@ -67,6 +67,7 @@ void kvm__pause(void);void kvm__continue (void );void kvm__notify_paused (void );int kvm__get_pid_by_instance (const char *name); +int kvm__enumerate_instances (void (*callback)(const char *name,int pid));/* * Debugging
tools/kvm/kvm-cmd.c +2 -0
@@ -8,6 +8,7 @@#include "kvm/kvm-debug.h" #include "kvm/kvm-pause.h" #include "kvm/kvm-balloon.h" +#include "kvm/kvm-list.h" #include "kvm/kvm-help.h" #include "kvm/kvm-cmd.h" #include "kvm/kvm-run.h"
@@ -16,6 +17,7 @@ struct cmd_struct kvm_commands[] = { { "pause", kvm_cmd_pause, NULL, 0 }, { "debug", kvm_cmd_debug, NULL, 0 }, { "balloon", kvm_cmd_balloon, NULL, 0 }, + { "list", kvm_cmd_list, NULL, 0 }, { "help", kvm_cmd_help, NULL, 0 }, { "run", kvm_cmd_run, kvm_run_help, 0 }, { NULL, NULL, NULL, 0 },
tools/kvm/kvm-list.c new file +20 -0
@@ -0,0 +1,20 @@ +#include <kvm/util.h> +#include <kvm/kvm-cmd.h> +#include <kvm/kvm-list.h> +#include <kvm/kvm.h> + +#include <stdio.h> +#include <string.h> +#include <signal.h> + +static void print_guest (const char *name,int pid) +{ +printf ("%s (PID: %d)\n" , name, pid); +} + +int kvm_cmd_list (int argc,const char **argv,const char *prefix) +{ +kvm__enumerate_instances (print_guest); + +return 0 ; +}
Signature · 1 line
1.7.6
PATCH 5/5 kvm tools: Allow pausing and debugging all running instances
In reply to Sasha LevinAdd a 'kvm [debug,pause] all' command to allow pausing or debugging all running guests on a host.
--- tools/kvm/kvm-debug.c | 10 ++++++++++ tools/kvm/kvm-pause.c | 10 ++++++++++ 2 files changed, 20 insertions(+), 0 deletions(-)
tools/kvm/kvm-debug.c +10 -0
@@ -7,6 +7,11 @@#include <string.h> #include <signal.h> +static void do_debug (const char *name,int pid) +{ +kill (pid,SIGQUIT ); +} +int kvm_cmd_debug (int argc,const char **argv,const char *prefix) {int pid;
@@ -14,6 +19,11 @@ int kvm_cmd_debug(int argc, const char **argv, const char *prefix)if (argc !=1 )die ("Usage: kvm debug [instance name]\n" ); +if (strcmp (argv[0 ],"all" ) ==0 ) { +kvm__enumerate_instances (do_debug); +return 0 ; + } + pid =kvm__get_pid_by_instance (argv[0 ]);if (pid <0 )die ("Failed locating instance name" );
tools/kvm/kvm-pause.c +10 -0
@@ -7,6 +7,11 @@#include <kvm/kvm-pause.h> #include <kvm/kvm.h> +static void do_pause (const char *name,int pid) +{ +kill (pid,SIGUSR2 ); +} +int kvm_cmd_pause (int argc,const char **argv,const char *prefix) {int pid;
@@ -14,6 +19,11 @@ int kvm_cmd_pause(int argc, const char **argv, const char *prefix)if (argc !=1 )die ("Usage: kvm debug [instance name]\n" ); +if (strcmp (argv[0 ],"all" ) ==0 ) { +kvm__enumerate_instances (do_pause); +return 0 ; + } + pid =kvm__get_pid_by_instance (argv[0 ]);if (pid <0 )die ("Failed locating instance name" );
Signature · 1 line
1.7.6
Re: [PATCH 4/5] kvm tools: Add 'kvm list' command
In reply to Sasha LevinQuoted 3 lines · Sasha Levin
Use 'kvm list' to list running virtual guest.
Signed-off-by Sasha Levin <[email protected]>
The pidfiles don't seem to get cleaned after guests exit. I ran 'make check' a few times and I now see this:
penberg@tiger:~/linux/tools/kvm$ ./kvm list guest-6121 (PID: 6121) guest-6184 (PID: 6184) guest-6035 (PID: 6035) penberg@tiger:~/linux/tools/kvm$ ps -eaf|grep kvm root 27 2 0 21:06 ? 00:00:00 [kvm-irqfd-clean] penberg 6204 5502 0 21:08 pts/1 00:00:00 grep --color=auto kvm
Re: [PATCH 4/5] kvm tools: Add 'kvm list' command
Quoted 8 lines · Pekka Enberg
Quoted 3 lines · Sasha Levin
Use 'kvm list' to list running virtual guest.
Signed-off-by Sasha Levin <[email protected]>The pidfiles don't seem to get cleaned after guests exit. I ran 'make check' a few times and I now see this:
Quoted 8 lines
penberg@tiger:~/linux/tools/kvm$ ./kvm list guest-6121 (PID: 6121) guest-6184 (PID: 6184) guest-6035 (PID: 6035) penberg@tiger:~/linux/tools/kvm$ ps -eaf|grep kvm root 27 2 0 21:06 ? 00:00:00 [kvm-irqfd-clean] penberg 6204 5502 0 21:08 pts/1 00:00:00 grep --color=auto kvm
Looks like it's the result of exiting the kernel test without properly freeing everything.
Is there a nice way to handle the pids when exiting with exit() or die()?
Signature · 1 line
Sasha.
Re: [PATCH 4/5] kvm tools: Add 'kvm list' command
Quoted 5 lines · Sasha Levin
Looks like it's the result of exiting the kernel test without properly freeing everything.
Is there a nice way to handle the pids when exiting with
exit()ordie()?
atexit()? OTOH, kvm__enumerate_instances() should probably check if there's a KVM process with such pid before telling the caller about it.