From patchwork Sat Jul 2 23:52:05 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sasha Levin X-Patchwork-Id: 939642 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by demeter2.kernel.org (8.14.4/8.14.4) with ESMTP id p61NrRbk021037 for ; Fri, 1 Jul 2011 23:53:27 GMT Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753561Ab1GAXxR (ORCPT ); Fri, 1 Jul 2011 19:53:17 -0400 Received: from mail-ww0-f44.google.com ([74.125.82.44]:37668 "EHLO mail-ww0-f44.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751876Ab1GAXxQ (ORCPT ); Fri, 1 Jul 2011 19:53:16 -0400 Received: by wwe5 with SMTP id 5so3645497wwe.1 for ; Fri, 01 Jul 2011 16:53:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=from:to:cc:subject:date:message-id:x-mailer:in-reply-to:references; bh=5TKAvFZBh+sNoGaSdi0s6eDn5BIZ7soSoKU4XTo8TYg=; b=kKoroyK2ThR+Vv2MdZ29gL9Bh10k/EIY2KnXffqSiOfdcQI2GVcF+WIPRx+9wBeeM4 BnVdYsVzjvCa14eu7/u7xVfEUV6kwqcsDTxYmkLkMzw3dRMpbl1cdnTF56mM0kWbK/D3 A9lG/NA5IEiKBMWkikm0yLL8DgSJ7cumyncK0= Received: by 10.216.63.131 with SMTP id a3mr3197175wed.64.1309564395175; Fri, 01 Jul 2011 16:53:15 -0700 (PDT) Received: from localhost.localdomain ([31.210.184.221]) by mx.google.com with ESMTPS id l53sm1858198weq.23.2011.07.01.16.53.11 (version=TLSv1/SSLv3 cipher=OTHER); Fri, 01 Jul 2011 16:53:14 -0700 (PDT) From: Sasha Levin To: penberg@kernel.org Cc: kvm@vger.kernel.org, mingo@elte.hu, asias.hejun@gmail.com, gorcunov@gmail.com, prasadjoshi124@gmail.com, Sasha Levin Subject: [PATCH v2 3/8] kvm tools: Allow giving instance names Date: Sat, 2 Jul 2011 19:52:05 -0400 Message-Id: <1309650731-5796-3-git-send-email-levinsasha928@gmail.com> X-Mailer: git-send-email 1.7.6 In-Reply-To: <1309650731-5796-1-git-send-email-levinsasha928@gmail.com> References: <1309650731-5796-1-git-send-email-levinsasha928@gmail.com> Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Greylist: IP, sender and recipient auto-whitelisted, not delayed by milter-greylist-4.2.6 (demeter2.kernel.org [140.211.167.43]); Fri, 01 Jul 2011 23:53:27 +0000 (UTC) This will allow tracking instance names and sending commands to specific instances if multiple instances are running. Signed-off-by: Sasha Levin --- tools/kvm/include/kvm/kvm.h | 5 +++- tools/kvm/kvm-run.c | 5 +++- tools/kvm/kvm.c | 56 ++++++++++++++++++++++++++++++++++++++++++- tools/kvm/term.c | 3 ++ 4 files changed, 66 insertions(+), 3 deletions(-) diff --git a/tools/kvm/include/kvm/kvm.h b/tools/kvm/include/kvm/kvm.h index 7d90d35..5ad3236 100644 --- a/tools/kvm/include/kvm/kvm.h +++ b/tools/kvm/include/kvm/kvm.h @@ -41,9 +41,11 @@ struct kvm { const char *vmlinux; struct disk_image **disks; int nr_disks; + + const char *name; }; -struct kvm *kvm__init(const char *kvm_dev, u64 ram_size); +struct kvm *kvm__init(const char *kvm_dev, u64 ram_size, const char *name); int kvm__max_cpus(struct kvm *kvm); void kvm__init_ram(struct kvm *kvm); void kvm__delete(struct kvm *kvm); @@ -61,6 +63,7 @@ bool kvm__deregister_mmio(struct kvm *kvm, u64 phys_addr); void kvm__pause(void); void kvm__continue(void); void kvm__notify_paused(void); +int kvm__get_pid_by_instance(const char *name); /* * Debugging diff --git a/tools/kvm/kvm-run.c b/tools/kvm/kvm-run.c index efae3c0..56c39ab 100644 --- a/tools/kvm/kvm-run.c +++ b/tools/kvm/kvm-run.c @@ -69,6 +69,7 @@ static const char *network; static const char *host_ip_addr; static const char *guest_mac; static const char *script; +static const char *guest_name; static bool single_step; static bool readonly_image[MAX_DISK_IMAGES]; static bool vnc; @@ -132,6 +133,8 @@ static int virtio_9p_rootdir_parser(const struct option *opt, const char *arg, i static const struct option options[] = { OPT_GROUP("Basic options:"), + OPT_STRING('\0', "name", &guest_name, "guest name", + "A name for the guest"), OPT_INTEGER('c', "cpus", &nrcpus, "Number of CPUs"), OPT_U64('m', "mem", &ram_size, "Virtual machine memory size in MiB."), OPT_CALLBACK('d', "disk", NULL, "image", "Disk image", img_name_parser), @@ -546,7 +549,7 @@ int kvm_cmd_run(int argc, const char **argv, const char *prefix) term_init(); - kvm = kvm__init(kvm_dev, ram_size); + kvm = kvm__init(kvm_dev, ram_size, guest_name); ioeventfd__init(); diff --git a/tools/kvm/kvm.c b/tools/kvm/kvm.c index c400c70..23d31a3 100644 --- a/tools/kvm/kvm.c +++ b/tools/kvm/kvm.c @@ -31,6 +31,7 @@ #include #define DEFINE_KVM_EXIT_REASON(reason) [reason] = #reason +#define KVM_PID_FILE_PATH "~/.kvm-tools/" const char *kvm_exit_reasons[] = { DEFINE_KVM_EXIT_REASON(KVM_EXIT_UNKNOWN), @@ -113,11 +114,60 @@ static struct kvm *kvm__new(void) return kvm; } +static void kvm__create_pidfile(struct kvm *kvm) +{ + int fd; + char full_name[PATH_MAX], pid[10]; + + if (!kvm->name) + return; + + mkdir(KVM_PID_FILE_PATH, 0777); + sprintf(full_name, "%s/%s.pid", 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) + die("Failed creating PID file"); + close(fd); +} + +static void kvm__remove_pidfile(struct kvm *kvm) +{ + char full_name[PATH_MAX]; + + if (!kvm->name) + return; + + sprintf(full_name, "%s/%s.pid", KVM_PID_FILE_PATH, kvm->name); + unlink(full_name); +} + +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); + fd = open(pid_file, O_RDONLY); + if (fd < 0) + return -1; + + if (read(fd, pid_str, 10) == 0) + return -1; + + pid = atoi(pid_str); + if (pid < 0) + return -1; + + return pid; +} + void kvm__delete(struct kvm *kvm) { kvm__stop_timer(kvm); munmap(kvm->ram_start, kvm->ram_size); + kvm__remove_pidfile(kvm); free(kvm); } @@ -237,7 +287,7 @@ int kvm__max_cpus(struct kvm *kvm) return ret; } -struct kvm *kvm__init(const char *kvm_dev, u64 ram_size) +struct kvm *kvm__init(const char *kvm_dev, u64 ram_size, const char *name) { struct kvm_pit_config pit_config = { .flags = 0, }; struct kvm *kvm; @@ -300,6 +350,10 @@ struct kvm *kvm__init(const char *kvm_dev, u64 ram_size) if (ret < 0) die_perror("KVM_CREATE_IRQCHIP ioctl"); + kvm->name = name; + + kvm__create_pidfile(kvm); + return kvm; } diff --git a/tools/kvm/term.c b/tools/kvm/term.c index 9947223..a0cb03f 100644 --- a/tools/kvm/term.c +++ b/tools/kvm/term.c @@ -9,7 +9,9 @@ #include "kvm/read-write.h" #include "kvm/term.h" #include "kvm/util.h" +#include "kvm/kvm.h" +extern struct kvm *kvm; static struct termios orig_term; int term_escape_char = 0x01; /* ctrl-a is used for escape */ @@ -32,6 +34,7 @@ int term_getc(int who) if (term_got_escape) { term_got_escape = false; if (c == 'x') { + kvm__delete(kvm); printf("\n # KVM session terminated.\n"); exit(1); }