From mboxrd@z Thu Jan 1 00:00:00 1970 From: Sasha Levin Subject: [PATCH 7/7] kvm tools: Add option to list rootfs in 'kvm list' Date: Thu, 13 Oct 2011 11:32:46 +0200 Message-ID: <1318498366-7824-7-git-send-email-levinsasha928@gmail.com> References: <1318498366-7824-1-git-send-email-levinsasha928@gmail.com> Cc: kvm@vger.kernel.org, mingo@elte.hu, asias.hejun@gmail.com, gorcunov@gmail.com, Sasha Levin To: penberg@cs.helsinki.fi Return-path: Received: from mail-ey0-f174.google.com ([209.85.215.174]:61705 "EHLO mail-ey0-f174.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754269Ab1JMJdX (ORCPT ); Thu, 13 Oct 2011 05:33:23 -0400 Received: by mail-ey0-f174.google.com with SMTP id 27so1629727eye.19 for ; Thu, 13 Oct 2011 02:33:22 -0700 (PDT) In-Reply-To: <1318498366-7824-1-git-send-email-levinsasha928@gmail.com> Sender: kvm-owner@vger.kernel.org List-ID: Previously we would only list running instances, now that we create rootfs instances it's appropriate to add a method to list them as well. Suggested-by: Ingo Molnar Signed-off-by: Sasha Levin --- tools/kvm/builtin-list.c | 68 ++++++++++++++++++++++++++++++++++++++++++++- 1 files changed, 66 insertions(+), 2 deletions(-) diff --git a/tools/kvm/builtin-list.c b/tools/kvm/builtin-list.c index fcf9bb0..43a0aa8 100644 --- a/tools/kvm/builtin-list.c +++ b/tools/kvm/builtin-list.c @@ -4,6 +4,7 @@ #include #include +#include #include #include #include @@ -11,12 +12,18 @@ #define PROCESS_NAME "kvm" +static bool run; +static bool rootfs; + static const char * const list_usage[] = { "kvm list", NULL }; static const struct option list_options[] = { + OPT_GROUP("General options:"), + OPT_BOOLEAN('i', "run", &run, "List running instances"), + OPT_BOOLEAN('r', "rootfs", &rootfs, "List rootfs instances"), OPT_END() }; @@ -58,9 +65,66 @@ cleanup: return 0; } -int kvm_cmd_list(int argc, const char **argv, const char *prefix) +static int kvm_list_running_instances(void) { printf(" PID GUEST\n"); - return kvm__enumerate_instances(print_guest); + return kvm__enumerate_instances(print_guest); +} + +static int kvm_list_rootfs(void) +{ + char name[PATH_MAX]; + DIR *dir; + struct dirent *dirent; + + snprintf(name, PATH_MAX, "%s%s", HOME_DIR, KVM_PID_FILE_PATH); + dir = opendir(name); + if (dir == NULL) + return -1; + + printf(" ROOTFS\n"); + + while ((dirent = readdir(dir))) { + if (dirent->d_type == DT_DIR && + strcmp(dirent->d_name, ".") && + strcmp(dirent->d_name, "..")) + printf("%s\n", dirent->d_name); + } + + return 0; +} + +static void parse_setup_options(int argc, const char **argv) +{ + while (argc != 0) { + argc = parse_options(argc, argv, list_options, list_usage, + PARSE_OPT_STOP_AT_NON_OPTION); + if (argc != 0) + kvm_list_help(); + } +} + +int kvm_cmd_list(int argc, const char **argv, const char *prefix) +{ + int r; + + parse_setup_options(argc, argv); + + if (!run && !rootfs) + kvm_list_help(); + + if (run) { + r = kvm_list_running_instances(); + if (r < 0) + perror("Error listing instances"); + } + + if (rootfs) { + r = kvm_list_rootfs(); + if (r < 0) + perror("Error listing rootfs"); + } + + return 0; } -- 1.7.7