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);
void kvm__pause(void);
void kvm__continue(void);
void kvm__notify_paused(void);
+int kvm__get_pid_by_instance(const char *name);
/*
* Debugging
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;
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),
term_init();
- kvm = kvm__init(kvm_dev, ram_size);
+ kvm = kvm__init(kvm_dev, ram_size, guest_name);
ioeventfd__init();
#include <asm/unistd.h>
#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),
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);
}
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;
if (ret < 0)
die_perror("KVM_CREATE_IRQCHIP ioctl");
+ kvm->name = name;
+
+ kvm__create_pidfile(kvm);
+
return kvm;
}