Start populating /sys/hypervisor with KVM entries when we're running on KVM. This is to replicate functionality that's available when we're running on Xen. Start with /sys/hypervisor/uuid, which users prefer over /sys/devices/virtual/dmi/id/product_uuid as a way to recognize a virtual machine, since it's also available when running on Xen HVM and on Xen PV and, on top of that doesn't require root privileges by default. Let's create arch-specific hooks so that different architectures can provide different implementations. Signed-off-by: Filippo Sironi <sironi@xxxxxxxxx> --- v2: * move the retrieval of the VM UUID out of uuid_show and into kvm_para_get_uuid, which is a weak function that can be overwritten drivers/Kconfig | 2 ++ drivers/Makefile | 2 ++ drivers/kvm/Kconfig | 14 ++++++++++++++ drivers/kvm/Makefile | 1 + drivers/kvm/sys-hypervisor.c | 30 ++++++++++++++++++++++++++++++ 5 files changed, 49 insertions(+) create mode 100644 drivers/kvm/Kconfig create mode 100644 drivers/kvm/Makefile create mode 100644 drivers/kvm/sys-hypervisor.c diff --git a/drivers/Kconfig b/drivers/Kconfig index 45f9decb9848..90eb835fe951 100644 --- a/drivers/Kconfig +++ b/drivers/Kconfig @@ -146,6 +146,8 @@ source "drivers/hv/Kconfig" source "drivers/xen/Kconfig" +source "drivers/kvm/Kconfig" + source "drivers/staging/Kconfig" source "drivers/platform/Kconfig" diff --git a/drivers/Makefile b/drivers/Makefile index c61cde554340..79cc92a3f6bf 100644 --- a/drivers/Makefile +++ b/drivers/Makefile @@ -44,6 +44,8 @@ obj-y += soc/ obj-$(CONFIG_VIRTIO) += virtio/ obj-$(CONFIG_XEN) += xen/ +obj-$(CONFIG_KVM_GUEST) += kvm/ + # regulators early, since some subsystems rely on them to initialize obj-$(CONFIG_REGULATOR) += regulator/ diff --git a/drivers/kvm/Kconfig b/drivers/kvm/Kconfig new file mode 100644 index 000000000000..3fc041df7c11 --- /dev/null +++ b/drivers/kvm/Kconfig @@ -0,0 +1,14 @@ +menu "KVM driver support" + depends on KVM_GUEST + +config KVM_SYS_HYPERVISOR + bool "Create KVM entries under /sys/hypervisor" + depends on SYSFS + select SYS_HYPERVISOR + default y + help + Create KVM entries under /sys/hypervisor (e.g., uuid). When running + native or on another hypervisor, /sys/hypervisor may still be + present, but it will have no KVM entries. + +endmenu diff --git a/drivers/kvm/Makefile b/drivers/kvm/Makefile new file mode 100644 index 000000000000..73a43fc994b9 --- /dev/null +++ b/drivers/kvm/Makefile @@ -0,0 +1 @@ +obj-$(CONFIG_KVM_SYS_HYPERVISOR) += sys-hypervisor.o diff --git a/drivers/kvm/sys-hypervisor.c b/drivers/kvm/sys-hypervisor.c new file mode 100644 index 000000000000..43b1d1a09807 --- /dev/null +++ b/drivers/kvm/sys-hypervisor.c @@ -0,0 +1,30 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +#include <asm/kvm_para.h> + +#include <linux/kobject.h> +#include <linux/sysfs.h> + +__weak const char *kvm_para_get_uuid(void) +{ + return NULL; +} + +static ssize_t uuid_show(struct kobject *obj, + struct kobj_attribute *attr, + char *buf) +{ + const char *uuid = kvm_para_get_uuid(); + return sprintf(buf, "%s\n", uuid); +} + +static struct kobj_attribute uuid = __ATTR_RO(uuid); + +static int __init uuid_init(void) +{ + if (!kvm_para_available()) + return 0; + return sysfs_create_file(hypervisor_kobj, &uuid.attr); +} + +device_initcall(uuid_init); -- 2.7.4