From nobody Thu Apr 25 23:05:20 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 207.211.31.81 as permitted sender) client-ip=207.211.31.81; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-delivery-1.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 207.211.31.81 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1575299027; cv=none; d=zohomail.com; s=zohoarc; b=Ylw+ljJiKNZwMvHXb3S3gxj6CB13/brFiTkpQmMX+QYBQ88WCCCULv6XG3gTO8y28SccVgPmyamxVFVuYRM+Wdf8B2xzul7b5tfEfBg3rBYajmKQ5ESvqwmQJGu552a26PXeoSXEKKj7LtFOPKpr8063j8tqySkwK0Wv1aceNtA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1575299027; h=Content-Type:Content-Transfer-Encoding:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=GkVRljihpMomkqhhMQcfKWCLp6370NRIZVyVzVnFapo=; b=NPnGyUFtxV+mNpiSS7FHJgIluNe9KsjBVXEsHmYUd8aM2BU1cKwgJPX+6wEUaYOycmUUbZ/QyXmGmVz7F0zEXTXmtKXfMhS5Emz1h2RqTD0FEiKcfGwDFFj1Au0XW1L3/P4mABDn5kZrjbMEynpls6X6Mnjwt3W5x4u1gmw/Ucw= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 207.211.31.81 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) header.from= Return-Path: Received: from us-smtp-delivery-1.mimecast.com (us-smtp-2.mimecast.com [207.211.31.81]) by mx.zohomail.com with SMTPS id 1575299027951342.5026568200294; Mon, 2 Dec 2019 07:03:47 -0800 (PST) Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-229-vzbd3qI4No-jXHYmF1rTMg-1; Mon, 02 Dec 2019 10:03:45 -0500 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 7D61E107ACC7; Mon, 2 Dec 2019 15:03:39 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 340045DA75; Mon, 2 Dec 2019 15:03:39 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id B69021803C37; Mon, 2 Dec 2019 15:03:37 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id xB2F3aNf030564 for ; Mon, 2 Dec 2019 10:03:36 -0500 Received: by smtp.corp.redhat.com (Postfix) id 56C19600CA; Mon, 2 Dec 2019 15:03:36 +0000 (UTC) Received: from dhcp-16-105.lcy.redhat.com (unknown [10.42.16.105]) by smtp.corp.redhat.com (Postfix) with ESMTP id C97CF600C9; Mon, 2 Dec 2019 15:03:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1575299026; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=GkVRljihpMomkqhhMQcfKWCLp6370NRIZVyVzVnFapo=; b=Tva4BnAqaxRqiPDdeHkmHDSucE2H2qO1nlsOPHhit5a40RkIFRHUPH3FFhpH282j7G0/WX H8AEQQFnrnP0ZTo1DxXbTfI4bR3AJxTK6KhDirKUMkwwwXy93+jNNT1oylM+oIg5dUpxqO UrHeqJVJDmhb1uj8XmE9+can39Aarb8= From: =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= To: libvir-list@redhat.com Date: Mon, 2 Dec 2019 15:03:25 +0000 Message-Id: <20191202150331.173475-2-berrange@redhat.com> In-Reply-To: <20191202150331.173475-1-berrange@redhat.com> References: <20191202150331.173475-1-berrange@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH 1/7] access: report an error if no access manager is present X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-MC-Unique: vzbd3qI4No-jXHYmF1rTMg-1 X-Mimecast-Spam-Score: 0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) The code calling this method expects it to have reported an error on failure. Signed-off-by: Daniel P. Berrang=C3=A9 Reviewed-by: Cole Robinson --- src/access/viraccessmanager.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/access/viraccessmanager.c b/src/access/viraccessmanager.c index 31e1787919..f4120c6139 100644 --- a/src/access/viraccessmanager.c +++ b/src/access/viraccessmanager.c @@ -65,6 +65,11 @@ VIR_ONCE_GLOBAL_INIT(virAccessManager); =20 virAccessManagerPtr virAccessManagerGetDefault(void) { + if (virAccessManagerDefault =3D=3D NULL) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("No access manager registered")); + return NULL; + } return virObjectRef(virAccessManagerDefault); } =20 --=20 2.23.0 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list From nobody Thu Apr 25 23:05:20 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 205.139.110.120 as permitted sender) client-ip=205.139.110.120; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-1.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 205.139.110.120 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1575299029; cv=none; d=zohomail.com; s=zohoarc; b=V/OdqrGrr2n7TViv6CIBWf6ahVkKGFsiQ4LOvnAVtltidTLE3N//07COsipKZFTgsMHpOghr1S8kCepvGXokq7ZDL1GEXAF15+jQx2Xiy7LZ8QqzC4ZCf8FPOsfGooLWhTxiS6Xne7bTgVMZkueorqX1XsbyzibdCdfycopm/zE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1575299029; h=Content-Type:Content-Transfer-Encoding:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=l9VMDHqIRrF1HDjofST6NogPN4cG0AbZeFOq9U8P44U=; b=kogNdPktr/ehTBb9RCGPknoaSx+RD2xYts1i3p79yoV2cBmDqMAqLzPrKO8iJjwCZb4tF4zS6J8cGktimuZ9l04zv7cb6sfq18oxBVwxTk5hy1frsjMHiEd15Ql8FxIDb+i+XNbDyQJxqzYDSaRH40ToopW7AeqqOseYYUR5mDs= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 205.139.110.120 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) header.from= Return-Path: Received: from us-smtp-1.mimecast.com (us-smtp-delivery-1.mimecast.com [205.139.110.120]) by mx.zohomail.com with SMTPS id 1575299029466748.4702535454478; Mon, 2 Dec 2019 07:03:49 -0800 (PST) Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-78-giEzksPuNwOvAOdR8Kg4Bg-1; Mon, 02 Dec 2019 10:03:45 -0500 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id CA193477; Mon, 2 Dec 2019 15:03:39 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 9609D5DA70; Mon, 2 Dec 2019 15:03:39 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id CEB711803C38; Mon, 2 Dec 2019 15:03:38 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id xB2F3bSk030576 for ; Mon, 2 Dec 2019 10:03:37 -0500 Received: by smtp.corp.redhat.com (Postfix) id 4404E600CA; Mon, 2 Dec 2019 15:03:37 +0000 (UTC) Received: from dhcp-16-105.lcy.redhat.com (unknown [10.42.16.105]) by smtp.corp.redhat.com (Postfix) with ESMTP id 96E35600C9; Mon, 2 Dec 2019 15:03:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1575299028; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=l9VMDHqIRrF1HDjofST6NogPN4cG0AbZeFOq9U8P44U=; b=J/buVKX6XOaDeWxGB4cRJrS0nH9aTYoTwNaVLK8faUPxwi3wqq++TY/Mi2/c3ByLjF5xsZ zBLtICjZd/EgdARB1bwedi8RXf2ZzkUFWI8O/lNcGO8FfserO8ndYAqBr0ZN2oqSCFI89p l/tfxJzcHk9tp4A5rqrwvPDRmVAJrg8= From: =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= To: libvir-list@redhat.com Date: Mon, 2 Dec 2019 15:03:26 +0000 Message-Id: <20191202150331.173475-3-berrange@redhat.com> In-Reply-To: <20191202150331.173475-1-berrange@redhat.com> References: <20191202150331.173475-1-berrange@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH 2/7] libvirt: pass a directory path into drivers for embedded usage X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-MC-Unique: giEzksPuNwOvAOdR8Kg4Bg-1 X-Mimecast-Spam-Score: 0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) The intent here is to allow the virt drivers to be run directly embedded in an arbitrary process without interfering with libvirtd. To achieve this they need to store all their configuration & state in a separate directory tree from the main system or session libvirtd instances. This can be useful for doing testing of the virt drivers in "make check" without interfering with the user's own libvirtd instances. It can also be used for applications using KVM/QEMU as a piece of infrastructure to build an service, rather than for general purpose OS hosting. A long standing example is libguestfs, which would prefer if its temporary VMs did show up in the main libvirtd VM list, because this confuses apps such as OpenStack Nova. A more recent example would be Kata which is using KVM as a technology to build containers. Signed-off-by: Daniel P. Berrang=C3=A9 Reviewed-by: Cole Robinson --- src/driver-state.h | 1 + src/interface/interface_backend_netcf.c | 7 +++++++ src/interface/interface_backend_udev.c | 7 +++++++ src/libvirt.c | 21 +++++++++++++++++++++ src/libvirt_internal.h | 4 +++- src/libxl/libxl_driver.c | 7 +++++++ src/lxc/lxc_driver.c | 8 ++++++++ src/network/bridge_driver.c | 7 +++++++ src/node_device/node_device_hal.c | 7 +++++++ src/node_device/node_device_udev.c | 7 +++++++ src/nwfilter/nwfilter_driver.c | 7 +++++++ src/qemu/qemu_driver.c | 7 +++++++ src/remote/remote_daemon.c | 1 + src/remote/remote_driver.c | 1 + src/secret/secret_driver.c | 7 +++++++ src/storage/storage_driver.c | 7 +++++++ src/vz/vz_driver.c | 7 +++++++ 17 files changed, 112 insertions(+), 1 deletion(-) diff --git a/src/driver-state.h b/src/driver-state.h index 69e2678dfc..1e2f6ed247 100644 --- a/src/driver-state.h +++ b/src/driver-state.h @@ -32,6 +32,7 @@ typedef enum { =20 typedef virDrvStateInitResult (*virDrvStateInitialize)(bool privileged, + const char *root, virStateInhibitCallback callback, void *opaque); =20 diff --git a/src/interface/interface_backend_netcf.c b/src/interface/interf= ace_backend_netcf.c index 4f46717cf3..951a7e6c23 100644 --- a/src/interface/interface_backend_netcf.c +++ b/src/interface/interface_backend_netcf.c @@ -89,9 +89,16 @@ virNetcfDriverStateDispose(void *obj) =20 static int netcfStateInitialize(bool privileged, + const char *root, virStateInhibitCallback callback G_GNUC_UNUSED, void *opaque G_GNUC_UNUSED) { + if (root !=3D NULL) { + virReportError(VIR_ERR_INVALID_ARG, "%s", + _("Driver does not support embedded mode")); + return -1; + } + if (virNetcfDriverStateInitialize() < 0) return VIR_DRV_STATE_INIT_ERROR; =20 diff --git a/src/interface/interface_backend_udev.c b/src/interface/interfa= ce_backend_udev.c index b7b06ed67a..a4f5deb26d 100644 --- a/src/interface/interface_backend_udev.c +++ b/src/interface/interface_backend_udev.c @@ -1150,11 +1150,18 @@ udevStateCleanup(void); =20 static int udevStateInitialize(bool privileged, + const char *root, virStateInhibitCallback callback G_GNUC_UNUSED, void *opaque G_GNUC_UNUSED) { int ret =3D VIR_DRV_STATE_INIT_ERROR; =20 + if (root !=3D NULL) { + virReportError(VIR_ERR_INVALID_ARG, "%s", + _("Driver does not support embedded mode")); + return -1; + } + if (VIR_ALLOC(driver) < 0) goto cleanup; =20 diff --git a/src/libvirt.c b/src/libvirt.c index 9d783761e6..bd2952d036 100644 --- a/src/libvirt.c +++ b/src/libvirt.c @@ -637,16 +637,36 @@ virRegisterStateDriver(virStateDriverPtr driver) * virStateInitialize: * @privileged: set to true if running with root privilege, false otherwise * @mandatory: set to true if all drivers must report success, not skipped + * @root: directory to use for embedded mode * @callback: callback to invoke to inhibit shutdown of the daemon * @opaque: data to pass to @callback * * Initialize all virtualization drivers. * + * Passing a non-NULL @root instructs the driver to run in embedded mode. + * Instead of using the compile time $prefix as the basis for directory + * paths, @root should be used instead. In addition any '/libvirt' + * component of the paths should be stripped. + * + * eg consider a build with prefix=3D/usr/local. A driver might use the + * locations + * + * /usr/local/etc/libvirt/$DRIVER/ + * /usr/local/var/lib/libvirt/$DRIVER/ + * /usr/local/run/libvirt/$DRIVER/ + * + * When run with @root, the locations should instead be + * + * @root/etc/$DRIVER/ + * @root/var/lib/$DRIVER/ + * @root/run/$DRIVER/ + * * Returns 0 if all succeed, -1 upon any failure. */ int virStateInitialize(bool privileged, bool mandatory, + const char *root, virStateInhibitCallback callback, void *opaque) { @@ -661,6 +681,7 @@ virStateInitialize(bool privileged, VIR_DEBUG("Running global init for %s state driver", virStateDriverTab[i]->name); ret =3D virStateDriverTab[i]->stateInitialize(privileged, + root, callback, opaque); VIR_DEBUG("State init result %d (mandatory=3D%d)", ret, mandat= ory); diff --git a/src/libvirt_internal.h b/src/libvirt_internal.h index 4a74dbc2af..00ef7aaf25 100644 --- a/src/libvirt_internal.h +++ b/src/libvirt_internal.h @@ -31,8 +31,10 @@ typedef void (*virStateInhibitCallback)(bool inhibit, =20 int virStateInitialize(bool privileged, bool mandatory, + const char *root, virStateInhibitCallback inhibit, - void *opaque); + void *opaque) + ATTRIBUTE_NONNULL(2); int virStateCleanup(void); int virStateReload(void); int virStateStop(void); diff --git a/src/libxl/libxl_driver.c b/src/libxl/libxl_driver.c index 44a74e8779..23ab29c4e8 100644 --- a/src/libxl/libxl_driver.c +++ b/src/libxl/libxl_driver.c @@ -647,6 +647,7 @@ libxlAddDom0(libxlDriverPrivatePtr driver) =20 static int libxlStateInitialize(bool privileged, + const char *root, virStateInhibitCallback callback G_GNUC_UNUSED, void *opaque G_GNUC_UNUSED) { @@ -655,6 +656,12 @@ libxlStateInitialize(bool privileged, char ebuf[1024]; bool autostart =3D true; =20 + if (root !=3D NULL) { + virReportError(VIR_ERR_INVALID_ARG, "%s", + _("Driver does not support embedded mode")); + return -1; + } + if (!libxlDriverShouldLoad(privileged)) return VIR_DRV_STATE_INIT_SKIPPED; =20 diff --git a/src/lxc/lxc_driver.c b/src/lxc/lxc_driver.c index 826bf074e3..7c2c4798e5 100644 --- a/src/lxc/lxc_driver.c +++ b/src/lxc/lxc_driver.c @@ -85,6 +85,7 @@ VIR_LOG_INIT("lxc.lxc_driver"); =20 =20 static int lxcStateInitialize(bool privileged, + const char *root, virStateInhibitCallback callback, void *opaque); static int lxcStateCleanup(void); @@ -1526,6 +1527,7 @@ lxcSecurityInit(virLXCDriverConfigPtr cfg) =20 =20 static int lxcStateInitialize(bool privileged, + const char *root, virStateInhibitCallback callback G_GNUC_UNUS= ED, void *opaque G_GNUC_UNUSED) { @@ -1533,6 +1535,12 @@ static int lxcStateInitialize(bool privileged, virLXCDriverConfigPtr cfg =3D NULL; bool autostart =3D true; =20 + if (root !=3D NULL) { + virReportError(VIR_ERR_INVALID_ARG, "%s", + _("Driver does not support embedded mode")); + return -1; + } + /* Check that the user is root, silently disable if not */ if (!privileged) { VIR_INFO("Not running privileged, disabling driver"); diff --git a/src/network/bridge_driver.c b/src/network/bridge_driver.c index e360645969..90f894bd78 100644 --- a/src/network/bridge_driver.c +++ b/src/network/bridge_driver.c @@ -702,6 +702,7 @@ firewalld_dbus_filter_bridge(DBusConnection *connection= G_GNUC_UNUSED, */ static int networkStateInitialize(bool privileged, + const char *root, virStateInhibitCallback callback G_GNUC_UNUSED, void *opaque G_GNUC_UNUSED) { @@ -713,6 +714,12 @@ networkStateInitialize(bool privileged, DBusConnection *sysbus =3D NULL; #endif =20 + if (root !=3D NULL) { + virReportError(VIR_ERR_INVALID_ARG, "%s", + _("Driver does not support embedded mode")); + return -1; + } + if (VIR_ALLOC(network_driver) < 0) goto error; =20 diff --git a/src/node_device/node_device_hal.c b/src/node_device/node_devic= e_hal.c index b40f93df46..1cc484a4ce 100644 --- a/src/node_device/node_device_hal.c +++ b/src/node_device/node_device_hal.c @@ -588,6 +588,7 @@ device_prop_modified(LibHalContext *ctx G_GNUC_UNUSED, =20 static int nodeStateInitialize(bool privileged G_GNUC_UNUSED, + const char *root, virStateInhibitCallback callback G_GNUC_UNUSED, void *opaque G_GNUC_UNUSED) { @@ -599,6 +600,12 @@ nodeStateInitialize(bool privileged G_GNUC_UNUSED, DBusConnection *sysbus; DBusError err; =20 + if (root !=3D NULL) { + virReportError(VIR_ERR_INVALID_ARG, "%s", + _("Driver does not support embedded mode")); + return -1; + } + /* Ensure caps_tbl is sorted by capability name */ qsort(caps_tbl, G_N_ELEMENTS(caps_tbl), sizeof(caps_tbl[0]), cmpstringp); diff --git a/src/node_device/node_device_udev.c b/src/node_device/node_devi= ce_udev.c index fabd2ec454..97d9982ac5 100644 --- a/src/node_device/node_device_udev.c +++ b/src/node_device/node_device_udev.c @@ -1782,6 +1782,7 @@ udevPCITranslateInit(bool privileged G_GNUC_UNUSED) =20 static int nodeStateInitialize(bool privileged, + const char *root, virStateInhibitCallback callback G_GNUC_UNUSED, void *opaque G_GNUC_UNUSED) { @@ -1789,6 +1790,12 @@ nodeStateInitialize(bool privileged, struct udev *udev =3D NULL; virThread enumThread; =20 + if (root !=3D NULL) { + virReportError(VIR_ERR_INVALID_ARG, "%s", + _("Driver does not support embedded mode")); + return -1; + } + if (VIR_ALLOC(driver) < 0) return VIR_DRV_STATE_INIT_ERROR; =20 diff --git a/src/nwfilter/nwfilter_driver.c b/src/nwfilter/nwfilter_driver.c index cc3ce98cc5..1c407727db 100644 --- a/src/nwfilter/nwfilter_driver.c +++ b/src/nwfilter/nwfilter_driver.c @@ -177,11 +177,18 @@ virNWFilterTriggerRebuildImpl(void *opaque) */ static int nwfilterStateInitialize(bool privileged, + const char *root, virStateInhibitCallback callback G_GNUC_UNUSED, void *opaque G_GNUC_UNUSED) { DBusConnection *sysbus =3D NULL; =20 + if (root !=3D NULL) { + virReportError(VIR_ERR_INVALID_ARG, "%s", + _("Driver does not support embedded mode")); + return -1; + } + if (virDBusHasSystemBus() && !(sysbus =3D virDBusGetSystemBus())) return VIR_DRV_STATE_INIT_ERROR; diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c index b5300241a8..31fe921ee3 100644 --- a/src/qemu/qemu_driver.c +++ b/src/qemu/qemu_driver.c @@ -646,6 +646,7 @@ qemuDomainFindMaxID(virDomainObjPtr vm, */ static int qemuStateInitialize(bool privileged, + const char *root, virStateInhibitCallback callback, void *opaque) { @@ -657,6 +658,12 @@ qemuStateInitialize(bool privileged, bool autostart =3D true; size_t i; =20 + if (root !=3D NULL) { + virReportError(VIR_ERR_INVALID_ARG, "%s", + _("Driver does not support embedded mode")); + return -1; + } + if (VIR_ALLOC(qemu_driver) < 0) return VIR_DRV_STATE_INIT_ERROR; =20 diff --git a/src/remote/remote_daemon.c b/src/remote/remote_daemon.c index b400b1dd10..f0a15279e0 100644 --- a/src/remote/remote_daemon.c +++ b/src/remote/remote_daemon.c @@ -834,6 +834,7 @@ static void daemonRunStateInit(void *opaque) * seriously delay OS bootup process */ if (virStateInitialize(virNetDaemonIsPrivileged(dmn), mandatory, + NULL, daemonInhibitCallback, dmn) < 0) { VIR_ERROR(_("Driver state initialization failed")); diff --git a/src/remote/remote_driver.c b/src/remote/remote_driver.c index a1384fc655..d8eb6e7833 100644 --- a/src/remote/remote_driver.c +++ b/src/remote/remote_driver.c @@ -236,6 +236,7 @@ static int remoteSplitURIScheme(virURIPtr uri, =20 static int remoteStateInitialize(bool privileged G_GNUC_UNUSED, + const char *root G_GNUC_UNUSED, virStateInhibitCallback callback G_GNUC_UNUSED, void *opaque G_GNUC_UNUSED) { diff --git a/src/secret/secret_driver.c b/src/secret/secret_driver.c index 93b4256450..d248121327 100644 --- a/src/secret/secret_driver.c +++ b/src/secret/secret_driver.c @@ -452,9 +452,16 @@ secretStateCleanup(void) =20 static int secretStateInitialize(bool privileged, + const char *root, virStateInhibitCallback callback G_GNUC_UNUSED, void *opaque G_GNUC_UNUSED) { + if (root !=3D NULL) { + virReportError(VIR_ERR_INVALID_ARG, "%s", + _("Driver does not support embedded mode")); + return -1; + } + if (VIR_ALLOC(driver) < 0) return VIR_DRV_STATE_INIT_ERROR; =20 diff --git a/src/storage/storage_driver.c b/src/storage/storage_driver.c index 580a5e6f15..6b3a36821b 100644 --- a/src/storage/storage_driver.c +++ b/src/storage/storage_driver.c @@ -251,6 +251,7 @@ storageDriverAutostart(void) */ static int storageStateInitialize(bool privileged, + const char *root, virStateInhibitCallback callback G_GNUC_UNUSED, void *opaque G_GNUC_UNUSED) { @@ -258,6 +259,12 @@ storageStateInitialize(bool privileged, g_autofree char *rundir =3D NULL; bool autostart =3D true; =20 + if (root !=3D NULL) { + virReportError(VIR_ERR_INVALID_ARG, "%s", + _("Driver does not support embedded mode")); + return -1; + } + if (VIR_ALLOC(driver) < 0) return VIR_DRV_STATE_INIT_ERROR; =20 diff --git a/src/vz/vz_driver.c b/src/vz/vz_driver.c index 6b925653d5..67ea4f8786 100644 --- a/src/vz/vz_driver.c +++ b/src/vz/vz_driver.c @@ -4089,12 +4089,19 @@ vzStateCleanup(void) =20 static int vzStateInitialize(bool privileged, + const char *root, virStateInhibitCallback callback G_GNUC_UNUSED, void *opaque G_GNUC_UNUSED) { if (!privileged) return VIR_DRV_STATE_INIT_SKIPPED; =20 + if (root !=3D NULL) { + virReportError(VIR_ERR_INVALID_ARG, "%s", + _("Driver does not support embedded mode")); + return -1; + } + vz_driver_privileged =3D privileged; =20 if (virFileMakePathWithMode(VZ_STATEDIR, S_IRWXU) < 0) { --=20 2.23.0 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list From nobody Thu Apr 25 23:05:20 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 205.139.110.61 as permitted sender) client-ip=205.139.110.61; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-delivery-1.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 205.139.110.61 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1575299046; cv=none; d=zohomail.com; s=zohoarc; b=BygVyoxlSSO+Zfxc43iWuQ/A6mnReC9D9ZTCdDz52MCbEH4JLO4FacElthe/CPILQRjl0pxKuuOBxvmnfU7UjSWblrr0Z1aoyudArmn29zGGsokJhT31exjguWMZcPlPyakIwlV1uhyWcFklpURQL8aRHtf8gOh35TwyXfafBl4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1575299046; h=Content-Type:Content-Transfer-Encoding:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=vYHTojIsTs98c5X2PmGuHvHXac9Cj87mG/g3fuNoH0U=; b=fbQ/JE7haL/zz1/Nt7ShR8vAxBL5UKZOHnkFxmCYflX2ThVAxtc1JS2pqWE8Ci4KeBL6OMKj5/1cyE1h3di7xQq9Fiy9yZqU4gr/bYUnCC7oB08nbe/+k73NVlDkBYwHhSQcJXbPSjO+pegM8eTyFn9Q7YzKpyNsTVKuWuZRAaM= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 205.139.110.61 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) header.from= Return-Path: Received: from us-smtp-delivery-1.mimecast.com (us-smtp-2.mimecast.com [205.139.110.61]) by mx.zohomail.com with SMTPS id 1575299046260900.2028680309921; Mon, 2 Dec 2019 07:04:06 -0800 (PST) Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-82-t8ZQ7WjJNbmydoMjpMu5pg-1; Mon, 02 Dec 2019 10:03:57 -0500 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 82593107ACCD; Mon, 2 Dec 2019 15:03:50 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 52BCD19C6A; Mon, 2 Dec 2019 15:03:50 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id E43995BBFD; Mon, 2 Dec 2019 15:03:49 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id xB2F3cZX030581 for ; Mon, 2 Dec 2019 10:03:38 -0500 Received: by smtp.corp.redhat.com (Postfix) id 1276B600CA; Mon, 2 Dec 2019 15:03:38 +0000 (UTC) Received: from dhcp-16-105.lcy.redhat.com (unknown [10.42.16.105]) by smtp.corp.redhat.com (Postfix) with ESMTP id 82497600C9; Mon, 2 Dec 2019 15:03:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1575299044; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=vYHTojIsTs98c5X2PmGuHvHXac9Cj87mG/g3fuNoH0U=; b=T+XzK5fJerzjbQZMA3FDAAJFBVuUM/T7gQtcIBtSN4YV338abwnDELux2hzlJgMYEAlTPe GD8JWS6BeCVZ1NCp8BVGNc38LGSOb/5z6j3xyyBB/IGas31rO87XxfiTp9cEr+AYH8sol2 D6o3Rx5QyXE7bExhQBzU9x7myJU/poU= From: =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= To: libvir-list@redhat.com Date: Mon, 2 Dec 2019 15:03:27 +0000 Message-Id: <20191202150331.173475-4-berrange@redhat.com> In-Reply-To: <20191202150331.173475-1-berrange@redhat.com> References: <20191202150331.173475-1-berrange@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH 3/7] event: add API for requiring an event loop impl to be registered X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 X-MC-Unique: t8ZQ7WjJNbmydoMjpMu5pg-1 X-Mimecast-Spam-Score: 0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) Signed-off-by: Daniel P. Berrang=C3=A9 Reviewed-by: Cole Robinson --- po/POTFILES.in | 1 + src/util/virevent.c | 25 +++++++++++++++++++++++++ src/util/virevent.h | 2 ++ 3 files changed, 28 insertions(+) diff --git a/po/POTFILES.in b/po/POTFILES.in index debb51cd70..b396797ff2 100644 --- a/po/POTFILES.in +++ b/po/POTFILES.in @@ -238,6 +238,7 @@ @SRCDIR@/src/util/virdnsmasq.c @SRCDIR@/src/util/virerror.c @SRCDIR@/src/util/virerror.h +@SRCDIR@/src/util/virevent.c @SRCDIR@/src/util/vireventpoll.c @SRCDIR@/src/util/virfcp.c @SRCDIR@/src/util/virfdstream.c diff --git a/src/util/virevent.c b/src/util/virevent.c index 3cac9f9472..a86acf64c0 100644 --- a/src/util/virevent.c +++ b/src/util/virevent.c @@ -29,6 +29,9 @@ =20 VIR_LOG_INIT("util.event"); =20 + +#define VIR_FROM_THIS VIR_FROM_EVENT + static virEventAddHandleFunc addHandleImpl; static virEventUpdateHandleFunc updateHandleImpl; static virEventRemoveHandleFunc removeHandleImpl; @@ -251,6 +254,26 @@ void virEventRegisterImpl(virEventAddHandleFunc addHan= dle, removeTimeoutImpl =3D removeTimeout; } =20 + +/** + * virEventRequireImpl: + * + * Require that there is an event loop implementation + * registered. + * + * Returns: -1 if no event loop is registered, 0 otherwise + */ +int virEventRequireImpl(void) +{ + if (!addHandleImpl || !addTimeoutImpl) { + virReportError(VIR_ERR_NO_SUPPORT, "%s", + _("An event loop implementation must be registered"= )); + return -1; + } + + return 0; +} + /** * virEventRegisterDefaultImpl: * @@ -276,6 +299,8 @@ int virEventRegisterDefaultImpl(void) { VIR_DEBUG("registering default event implementation"); =20 + virInitialize(); + virResetLastError(); =20 if (virEventPollInit() < 0) { diff --git a/src/util/virevent.h b/src/util/virevent.h index 0b4f7d2b1b..6c8fdcbac4 100644 --- a/src/util/virevent.h +++ b/src/util/virevent.h @@ -21,3 +21,5 @@ =20 #pragma once #include "internal.h" + +int virEventRequireImpl(void); --=20 2.23.0 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list From nobody Thu Apr 25 23:05:20 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 205.139.110.61 as permitted sender) client-ip=205.139.110.61; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-delivery-1.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 205.139.110.61 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1575299045; cv=none; d=zohomail.com; s=zohoarc; b=n4IzeD07duhYaLkA1HDUuv5LJZGUmTJZRRZWleyEzDNWhBpHdHac7fTeVWduY1cDq3nX6cuLAysm5W26+blPd+o4upDsTtFyzSCnbJJEyy5qfCSZHL9n7uWylgsd9dDVpqK9T9AwPMn1ZnF7VTz6kxOZ1vo7GZIVu0zoRyEpPbY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1575299045; h=Content-Type:Content-Transfer-Encoding:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=Lgd5xPv8qgejJsAw/QsN3/lJz5wtmsf20vB93KK+d+I=; b=hnBnexm+4v5zvN/mktAZcFqnvJZ25Vwy9rgYuPUxgOypRoW5auwWPtRnAYxFi6IWTIHufr4sS7dzYSXShM7V7Fe+BmUdoC1YrrppbhhNH9yLKjid7GfhvqvOljthVQqCZEpTUBbDpV8ikY7Bewm3m/MOYE8BFejA2RSCdxhd11E= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 205.139.110.61 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) header.from= Return-Path: Received: from us-smtp-delivery-1.mimecast.com (us-smtp-2.mimecast.com [205.139.110.61]) by mx.zohomail.com with SMTPS id 1575299045532358.5839586241442; Mon, 2 Dec 2019 07:04:05 -0800 (PST) Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-374-ElTfu9OhOLaBy4PHvBie8Q-1; Mon, 02 Dec 2019 10:04:01 -0500 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 9A743A1943; Mon, 2 Dec 2019 15:03:53 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 6D68F1001938; Mon, 2 Dec 2019 15:03:53 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id 0EAA618034EC; Mon, 2 Dec 2019 15:03:53 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id xB2F3c42030592 for ; Mon, 2 Dec 2019 10:03:38 -0500 Received: by smtp.corp.redhat.com (Postfix) id D6DDD600CD; Mon, 2 Dec 2019 15:03:38 +0000 (UTC) Received: from dhcp-16-105.lcy.redhat.com (unknown [10.42.16.105]) by smtp.corp.redhat.com (Postfix) with ESMTP id 51522600C9; Mon, 2 Dec 2019 15:03:38 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1575299043; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=Lgd5xPv8qgejJsAw/QsN3/lJz5wtmsf20vB93KK+d+I=; b=EbcjEbFFtiVTDk7NU35+138OMCR/m1BoPiLTeKVm9808hYifIEAlVuZxoKFVEHaVAa+QEG p2VzVtoOlV5pJIsxzO6/W/xwKNu3Mam4HRaBDHYFSe9fdlDRxQZP5eGnINDc+42bGVB33R nJjcWRF3qOxI4enakRe7E9m00bmaimo= From: =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= To: libvir-list@redhat.com Date: Mon, 2 Dec 2019 15:03:28 +0000 Message-Id: <20191202150331.173475-5-berrange@redhat.com> In-Reply-To: <20191202150331.173475-1-berrange@redhat.com> References: <20191202150331.173475-1-berrange@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH 4/7] libvirt: support an "embed" URI path selector for opening drivers X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 X-MC-Unique: ElTfu9OhOLaBy4PHvBie8Q-1 X-Mimecast-Spam-Score: 0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) The driver URI scheme: "$drivername:///embed?root=3D/some/path" enables a new way to use the drivers by embedding them directly in the calling process. To use this the process must have a thread running the libvirt event loop. This URI will then cause libvirt to dynamically load the driver module and call its global initialization function. This syntax is applicable to any driver, but only those will have been modified to support a custom root directory and embed URI path will successfully open. The application can now make normal libvirt API calls which are all serviced in-process with no RPC layer involved. It is required to specify an explicit root directory, and locks will be acquired on this directory to avoid conflicting with another app that might accidentally pick the same directory. Use of '/' is not explicitly forbidden, but note that the file layout used underneath the embedded driver root does not match the file layout used by system/session mode drivers. So this cannot be used as a backdoor to interact with, or fake, the system/session mode drivers. Libvirt will create arbitrary files underneath this root directory. The root directory can be kept untouched across connection open attempts if the application needs persistence. The application is responsible for purging everything underneath this root directory when finally no longer required. Even when a virt driver is used in embedded mode, it is still possible for it to in turn use functionality that calls out to other secondary drivers in libvirtd. For example an embedded instance of QEMU can open the network, secret or storage drivers in the system libvirtd. That said, the application would typically want to at least open an embedded secret driver ("secret:///embed?root=3D/some/path"). Note that multiple different embedded drivers can use the same root prefix and co-operate just as they would inside a normal libvirtd daemon. A key thing to note is that for this to work, the application that links to libvirt *MUST* be built with -Wl,--export-dynamic to ensure that symbols from libvirt.so are exported & thus available to the dynamically loaded driver module. If libvirt.so itself was dynamically loaded then RTLD_GLOBAL must be passed to dlopen(). Signed-off-by: Daniel P. Berrang=C3=A9 --- src/driver-state.h | 1 + src/driver.h | 2 ++ src/libvirt.c | 72 ++++++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 73 insertions(+), 2 deletions(-) diff --git a/src/driver-state.h b/src/driver-state.h index 1e2f6ed247..6b3f501e05 100644 --- a/src/driver-state.h +++ b/src/driver-state.h @@ -50,6 +50,7 @@ typedef virStateDriver *virStateDriverPtr; =20 struct _virStateDriver { const char *name; + bool initialized; virDrvStateInitialize stateInitialize; virDrvStateCleanup stateCleanup; virDrvStateReload stateReload; diff --git a/src/driver.h b/src/driver.h index ca82ac974b..6278aa05b3 100644 --- a/src/driver.h +++ b/src/driver.h @@ -82,6 +82,8 @@ struct _virConnectDriver { bool localOnly; /* Whether driver needs a server in the URI */ bool remoteOnly; + /* Whether driver can be used in embedded mode */ + bool embeddable; /* * NULL terminated list of supported URI schemes. * - Single element { NULL } list indicates no supported schemes diff --git a/src/libvirt.c b/src/libvirt.c index bd2952d036..17b6506faa 100644 --- a/src/libvirt.c +++ b/src/libvirt.c @@ -52,6 +52,7 @@ # include "rpc/virnettlscontext.h" #endif #include "vircommand.h" +#include "virevent.h" #include "virfile.h" #include "virrandom.h" #include "viruri.h" @@ -84,6 +85,7 @@ #ifdef WITH_BHYVE # include "bhyve/bhyve_driver.h" #endif +#include "access/viraccessmanager.h" =20 #define VIR_FROM_THIS VIR_FROM_NONE =20 @@ -676,10 +678,12 @@ virStateInitialize(bool privileged, return -1; =20 for (i =3D 0; i < virStateDriverTabCount; i++) { - if (virStateDriverTab[i]->stateInitialize) { + if (virStateDriverTab[i]->stateInitialize && + !virStateDriverTab[i]->initialized) { virDrvStateInitResult ret; VIR_DEBUG("Running global init for %s state driver", virStateDriverTab[i]->name); + virStateDriverTab[i]->initialized =3D true; ret =3D virStateDriverTab[i]->stateInitialize(privileged, root, callback, @@ -872,6 +876,7 @@ virConnectOpenInternal(const char *name, virConnectPtr ret; g_autoptr(virConf) conf =3D NULL; char *uristr =3D NULL; + bool embed =3D false; =20 ret =3D virGetConnect(); if (ret =3D=3D NULL) @@ -962,6 +967,52 @@ virConnectOpenInternal(const char *name, ret->uri) < 0) { goto failed; } + + if (STREQ(ret->uri->path, "/embed")) { + const char *root =3D NULL; + g_autofree char *regMethod =3D NULL; + VIR_DEBUG("URI path requests %s driver embedded mode", + ret->uri->scheme); + if (strspn(ret->uri->scheme, "abcdefghijklmnopqrstuvwxyz") != =3D + strlen(ret->uri->scheme)) { + virReportError(VIR_ERR_NO_CONNECT, + _("URI scheme '%s' for embedded driver is n= ot valid"), + ret->uri->scheme); + goto failed; + } + + for (i =3D 0; i < ret->uri->paramsCount; i++) { + virURIParamPtr var =3D &ret->uri->params[i]; + if (STREQ(var->name, "root")) + root =3D var->value; + } + + if (!root) { + virReportError(VIR_ERR_INVALID_ARG, "%s", + _("root parameter required for embedded dri= ver")); + goto failed; + } + + if (virEventRequireImpl() < 0) + goto failed; + + regMethod =3D g_strdup_printf("%sRegister", ret->uri->scheme); + + if (virDriverLoadModule(ret->uri->scheme, regMethod, false) < = 0) + goto failed; + + if (virAccessManagerGetDefault() =3D=3D NULL) { + virAccessManagerPtr acl =3D virAccessManagerNew("none"); + if (!acl) + goto failed; + virAccessManagerSetDefault(acl); + } + + if (virStateInitialize(geteuid() =3D=3D 0, true, root, NULL, N= ULL) < 0) + goto failed; + + embed =3D true; + } } else { VIR_DEBUG("no name, allowing driver auto-select"); } @@ -1018,6 +1069,12 @@ virConnectOpenInternal(const char *name, VIR_DEBUG("No URI, skipping driver with URI whitelist"); continue; } + if (embed && !virConnectDriverTab[i]->embeddable) { + VIR_DEBUG("Ignoring non-embeddable driver %s", + virConnectDriverTab[i]->hypervisorDriver->name); + continue; + } + VIR_DEBUG("Checking for supported URI schemes"); for (s =3D 0; virConnectDriverTab[i]->uriSchemes[s] !=3D NULL;= s++) { if (STREQ(ret->uri->scheme, virConnectDriverTab[i]->uriSch= emes[s])) { @@ -1031,9 +1088,20 @@ virConnectOpenInternal(const char *name, continue; } } else { - VIR_DEBUG("Matching any URI scheme for '%s'", ret->uri ? ret->= uri->scheme : ""); + if (embed) { + VIR_DEBUG("Skipping wildcard for embedded URI"); + continue; + } else { + VIR_DEBUG("Matching any URI scheme for '%s'", ret->uri ? r= et->uri->scheme : ""); + } } =20 + if (embed && !virConnectDriverTab[i]->embeddable) { + virReportError(VIR_ERR_CONFIG_UNSUPPORTED, + _("Driver %s cannot be used in embedded mode"), + virConnectDriverTab[i]->hypervisorDriver->name); + goto failed; + } /* before starting the new connection, check if the driver only wo= rks * with a server, and so return an error if the server is missing = */ if (virConnectDriverTab[i]->remoteOnly && ret->uri && !ret->uri->s= erver) { --=20 2.23.0 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list From nobody Thu Apr 25 23:05:20 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 205.139.110.61 as permitted sender) client-ip=205.139.110.61; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-delivery-1.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 205.139.110.61 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1575299039; cv=none; d=zohomail.com; s=zohoarc; b=PlaSS5BcHrhRlJRszKo912mYv06riCJtnTNBauln5w5OD4ZstESt/oYPVS6ln/Uya6n4u4MaAfkG01KdrTS6f6cNnt7WkPLD/3c8SzUi8OR/rEBa6TKN+wcgPvu9di/UMdj4Ph/w7W5Qeymo3nFdfj6Yb5FM0QuNYxshlYBvuf0= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1575299039; h=Content-Type:Content-Transfer-Encoding:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=hudg7Ab7UwzE/wp6k1ET8A1X1RbXQLnKSshKg8pxl2Y=; b=GT35K00yk/2+utQlUwVAB1NMdm/sgrxVxQj5VEmkEXEqjMyWc4M2MAza2fIyRjriCtyw06KZYCBV9XOUd741I1ljxUyd1p1K/prFY1ZRqsS/AC/wxrj8CVeH0mV+3oWNtO87AGOHgDeNPeyHGX58injVhj+mR1eFTDkpJHu5ElI= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 205.139.110.61 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) header.from= Return-Path: Received: from us-smtp-delivery-1.mimecast.com (us-smtp-1.mimecast.com [205.139.110.61]) by mx.zohomail.com with SMTPS id 1575299039512564.2539487955133; Mon, 2 Dec 2019 07:03:59 -0800 (PST) Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-128-blgZT5yKP8elQ5nPMWeP5w-1; Mon, 02 Dec 2019 10:03:56 -0500 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 8ABD018C35C4; Mon, 2 Dec 2019 15:03:50 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 5CA36600C9; Mon, 2 Dec 2019 15:03:50 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id ECB555BBFE; Mon, 2 Dec 2019 15:03:49 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id xB2F3dGJ030600 for ; Mon, 2 Dec 2019 10:03:39 -0500 Received: by smtp.corp.redhat.com (Postfix) id A3AFC600CC; Mon, 2 Dec 2019 15:03:39 +0000 (UTC) Received: from dhcp-16-105.lcy.redhat.com (unknown [10.42.16.105]) by smtp.corp.redhat.com (Postfix) with ESMTP id 216E1600C9; Mon, 2 Dec 2019 15:03:38 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1575299038; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=hudg7Ab7UwzE/wp6k1ET8A1X1RbXQLnKSshKg8pxl2Y=; b=hsTrlWNaFJumntsidJM+cOfSx9fx7UIPZsXsIeDExJsGqOUBEhNyWx2ySKtpk3uY06lBeU Y04EajSmqpVLsFUvyTvkvzw91xGWrG+0pZnzE5POkJKZJY2hKSFHdKKD7hCUaKbEhTlwc+ CD93nf+trlBMWhwYBab9HF/QpBIvb3s= From: =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= To: libvir-list@redhat.com Date: Mon, 2 Dec 2019 15:03:29 +0000 Message-Id: <20191202150331.173475-6-berrange@redhat.com> In-Reply-To: <20191202150331.173475-1-berrange@redhat.com> References: <20191202150331.173475-1-berrange@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH 5/7] qemu: add support for running QEMU driver in embedded mode X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-MC-Unique: blgZT5yKP8elQ5nPMWeP5w-1 X-Mimecast-Spam-Score: 0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) This enables support for running QEMU embedded to the calling application process using a URI: qemu:///embed?root=3D/some/path Note that it is important to keep the path reasonably short to avoid risk of hitting the limit on UNIX socket path names which is 108 characters. When using the embedded mode with a root=3D/var/tmp/embed, the driver will use the following paths: logDir: /var/tmp/embed/log/qemu swtpmLogDir: /var/tmp/embed/log/swtpm configBaseDir: /var/tmp/embed/etc/qemu stateDir: /var/tmp/embed/run/qemu swtpmStateDir: /var/tmp/embed/run/swtpm cacheDir: /var/tmp/embed/cache/qemu libDir: /var/tmp/embed/lib/qemu swtpmStorageDir: /var/tmp/embed/lib/swtpm defaultTLSx509certdir: /var/tmp/embed/etc/pki/qemu These are identical whether the embedded driver is privileged or unprivileged. This compares with the system instance which uses logDir: /var/log/libvirt/qemu swtpmLogDir: /var/log/swtpm/libvirt/qemu configBaseDir: /etc/libvirt/qemu stateDir: /run/libvirt/qemu swtpmStateDir: /run/libvirt/qemu/swtpm cacheDir: /var/cache/libvirt/qemu libDir: /var/lib/libvirt/qemu swtpmStorageDir: /var/lib/libvirt/swtpm defaultTLSx509certdir: /etc/pki/qemu At this time all features present in the QEMU driver are available when running in embedded mode, availability matching whether the embedded driver is privileged or unprivileged. Signed-off-by: Daniel P. Berrang=C3=A9 --- docs/drvqemu.html.in | 84 +++++++++++++++++++++++++++++++++++++++++ src/qemu/qemu_conf.c | 38 +++++++++++++++++-- src/qemu/qemu_conf.h | 6 ++- src/qemu/qemu_driver.c | 26 +++++++------ src/qemu/qemu_process.c | 15 ++++++-- tests/domaincapstest.c | 2 +- tests/testutilsqemu.c | 2 +- 7 files changed, 151 insertions(+), 22 deletions(-) diff --git a/docs/drvqemu.html.in b/docs/drvqemu.html.in index 294117ee1f..4bcf5ac002 100644 --- a/docs/drvqemu.html.in +++ b/docs/drvqemu.html.in @@ -63,6 +63,90 @@ qemu+tcp://example.com/system (remote access, SAS= l/Kerberos) qemu+ssh://root@example.com/system (remote access, SSH tunnelled) =20 +

Embedded driver

+ +

+ Since 6.0.0 the QEMU driver has experimental support for operating + in an embedded mode. In this scenario, rather than connecting to + the libvirtd daemon, the QEMU driver runs in the client application + process directly. To use this the client application must have + registered & be running an instance of the event loop. To open + the driver in embedded mode the app use the new URI path and specify + a virtual root directory under which the driver will create content. +

+ +
+      qemu:///embed?root=3D/some/dir
+    
+ +

+ Under the specified root directory the following locations will + be used +

+ +
+/some/dir
+  |
+  +- log
+  |   |
+  |   +- qemu
+  |   +- swtpm
+  |
+  +- etc
+  |   |
+  |   +- qemu
+  |   +- pki
+  |       |
+  |       +- qemu
+  |
+  +- run
+  |   |
+  |   +- qemu
+  |   +- swtpm
+  |
+  +- cache
+  |   |
+  |   +- qemu
+  |
+  +- lib
+      |
+      +- qemu
+      +- swtpm
+    
+ +

+ Note that UNIX domain sockets used for QEMU virtual machines had + a maximum filename length of 108 characters. Bear this in mind + when picking a root directory to avoid risk of exhausting the + filename space. The application is responsible for recursively + purging the contents of this directory tree once they no longer + require a connection, though it can also be left intact for reuse + when opening a future connection. +

+ +

+ Broadly speaking the range of functionality is intended to be + on a par with that seen when using the traditional system or + session libvirt connections to QEMU. The features will of course + differ depending on whether the application using the embedded + driver is running privileged or unprivileged. For example PCI + device assignment or TAP based networking are only available + when running privileged. While the embedded mode is still classed + as experimental some features may change their default settings + between releases. +

+ +

+ By default if the application uses any APIs associated with + secondary drivers, these will result in a connection being + opened to the corresponding driver in libvirtd. For example, + this allows a virtual machine from the embedded QEMU to connect + its NIC to a virtual network or connect its disk to a storage + volume. Some of the secondary drivers will also be able to support + running in embedded mode. Currently this is supported by the + secrets driver, to allow for use of VMs with encrypted disks +

+

Driver security architecture

=20

diff --git a/src/qemu/qemu_conf.c b/src/qemu/qemu_conf.c index 30637b21ac..e2b4c26b9b 100644 --- a/src/qemu/qemu_conf.c +++ b/src/qemu/qemu_conf.c @@ -104,7 +104,8 @@ qemuDriverUnlock(virQEMUDriverPtr driver) #endif =20 =20 -virQEMUDriverConfigPtr virQEMUDriverConfigNew(bool privileged) +virQEMUDriverConfigPtr virQEMUDriverConfigNew(bool privileged, + const char *root) { g_autoptr(virQEMUDriverConfig) cfg =3D NULL; =20 @@ -130,7 +131,24 @@ virQEMUDriverConfigPtr virQEMUDriverConfigNew(bool pri= vileged) =20 cfg->cgroupControllers =3D -1; /* -1 =3D=3D auto-detect */ =20 - if (privileged) { + if (root !=3D NULL) { + cfg->logDir =3D g_strdup_printf("%s/log/qemu", root); + cfg->swtpmLogDir =3D g_strdup_printf("%s/log/swtpm", root); + cfg->configBaseDir =3D g_strdup_printf("%s/etc", root); + cfg->stateDir =3D g_strdup_printf("%s/run/qemu", root); + cfg->swtpmStateDir =3D g_strdup_printf("%s/run/swtpm", root); + cfg->cacheDir =3D g_strdup_printf("%s/cache/qemu", root); + cfg->libDir =3D g_strdup_printf("%s/lib/qemu", root); + cfg->swtpmStorageDir =3D g_strdup_printf("%s/lib/swtpm", root); + + cfg->saveDir =3D g_strdup_printf("%s/save", cfg->libDir); + cfg->snapshotDir =3D g_strdup_printf("%s/snapshot", cfg->libDir); + cfg->checkpointDir =3D g_strdup_printf("%s/checkpoint", cfg->libDi= r); + cfg->autoDumpPath =3D g_strdup_printf("%s/dump", cfg->libDir); + cfg->channelTargetDir =3D g_strdup_printf("%s/channel/target", cfg= ->libDir); + cfg->nvramDir =3D g_strdup_printf("%s/nvram", cfg->libDir); + cfg->memoryBackingDir =3D g_strdup_printf("%s/ram", cfg->libDir); + } else if (privileged) { cfg->logDir =3D g_strdup_printf("%s/log/libvirt/qemu", LOCALSTATED= IR); =20 cfg->swtpmLogDir =3D g_strdup_printf("%s/log/swtpm/libvirt/qemu", @@ -194,6 +212,16 @@ virQEMUDriverConfigPtr virQEMUDriverConfigNew(bool pri= vileged) cfg->memoryBackingDir =3D g_strdup_printf("%s/qemu/ram", cfg->conf= igBaseDir); cfg->swtpmStorageDir =3D g_strdup_printf("%s/qemu/swtpm", cfg->configBaseDir); + } + + if (privileged) { + if (!virDoesUserExist("tss") || + virGetUserID("tss", &cfg->swtpm_user) < 0) + cfg->swtpm_user =3D 0; /* fall back to root */ + if (!virDoesGroupExist("tss") || + virGetGroupID("tss", &cfg->swtpm_group) < 0) + cfg->swtpm_group =3D 0; /* fall back to root */ + } else { cfg->swtpm_user =3D (uid_t)-1; cfg->swtpm_group =3D (gid_t)-1; } @@ -206,7 +234,11 @@ virQEMUDriverConfigPtr virQEMUDriverConfigNew(bool pri= vileged) * This will then be used as a fallback if the service specific * directory doesn't exist (although we don't check if this exists). */ - cfg->defaultTLSx509certdir =3D g_strdup(SYSCONFDIR "/pki/qemu"); + if (root =3D=3D NULL) { + cfg->defaultTLSx509certdir =3D g_strdup(SYSCONFDIR "pki/qemu"); + } else { + cfg->defaultTLSx509certdir =3D g_strdup_printf("%s/etc/pki/qemu", = root); + } =20 cfg->vncListen =3D g_strdup(VIR_LOOPBACK_IPV4_ADDR); cfg->spiceListen =3D g_strdup(VIR_LOOPBACK_IPV4_ADDR); diff --git a/src/qemu/qemu_conf.h b/src/qemu/qemu_conf.h index 95b33a1093..5de9d2f913 100644 --- a/src/qemu/qemu_conf.h +++ b/src/qemu/qemu_conf.h @@ -241,8 +241,9 @@ struct _virQEMUDriver { /* Atomic inc/dec only */ unsigned int nactive; =20 - /* Immutable value */ + /* Immutable values */ bool privileged; + bool embedded; =20 /* Immutable pointers. Caller must provide locking */ virStateInhibitCallback inhibitCallback; @@ -301,7 +302,8 @@ struct _virQEMUDriver { virHashAtomicPtr migrationErrors; }; =20 -virQEMUDriverConfigPtr virQEMUDriverConfigNew(bool privileged); +virQEMUDriverConfigPtr virQEMUDriverConfigNew(bool privileged, + const char *root); =20 int virQEMUDriverConfigLoadFile(virQEMUDriverConfigPtr cfg, const char *filename, diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c index 31fe921ee3..e51086ea5e 100644 --- a/src/qemu/qemu_driver.c +++ b/src/qemu/qemu_driver.c @@ -658,12 +658,6 @@ qemuStateInitialize(bool privileged, bool autostart =3D true; size_t i; =20 - if (root !=3D NULL) { - virReportError(VIR_ERR_INVALID_ARG, "%s", - _("Driver does not support embedded mode")); - return -1; - } - if (VIR_ALLOC(qemu_driver) < 0) return VIR_DRV_STATE_INIT_ERROR; =20 @@ -680,6 +674,7 @@ qemuStateInitialize(bool privileged, qemu_driver->inhibitOpaque =3D opaque; =20 qemu_driver->privileged =3D privileged; + qemu_driver->embedded =3D root !=3D NULL; =20 if (!(qemu_driver->domains =3D virDomainObjListNew())) goto error; @@ -693,7 +688,7 @@ qemuStateInitialize(bool privileged, if (privileged) qemu_driver->hostsysinfo =3D virSysinfoRead(); =20 - if (!(qemu_driver->config =3D cfg =3D virQEMUDriverConfigNew(privilege= d))) + if (!(qemu_driver->config =3D cfg =3D virQEMUDriverConfigNew(privilege= d, root))) goto error; =20 if (!(driverConf =3D g_strdup_printf("%s/qemu.conf", cfg->configBaseDi= r))) @@ -1208,10 +1203,18 @@ static virDrvOpenStatus qemuConnectOpen(virConnectP= tr conn, return VIR_DRV_OPEN_ERROR; } =20 - if (!virConnectValidateURIPath(conn->uri->path, - "qemu", - virQEMUDriverIsPrivileged(qemu_driver))) - return VIR_DRV_OPEN_ERROR; + if (qemu_driver->embedded) { + if (STRNEQ(conn->uri->path, "/embed")) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("URI must be qemu:///embed")); + return VIR_DRV_OPEN_ERROR; + } + } else { + if (!virConnectValidateURIPath(conn->uri->path, + "qemu", + virQEMUDriverIsPrivileged(qemu_driv= er))) + return VIR_DRV_OPEN_ERROR; + } =20 if (virConnectOpenEnsureACL(conn) < 0) return VIR_DRV_OPEN_ERROR; @@ -22950,6 +22953,7 @@ static virHypervisorDriver qemuHypervisorDriver =3D= { static virConnectDriver qemuConnectDriver =3D { .localOnly =3D true, .uriSchemes =3D (const char *[]){ "qemu", NULL }, + .embeddable =3D true, .hypervisorDriver =3D &qemuHypervisorDriver, }; =20 diff --git a/src/qemu/qemu_process.c b/src/qemu/qemu_process.c index a588ee25f8..c812fa680c 100644 --- a/src/qemu/qemu_process.c +++ b/src/qemu/qemu_process.c @@ -6717,10 +6717,17 @@ qemuProcessLaunch(virConnectPtr conn, =20 cfg =3D virQEMUDriverGetConfig(driver); =20 - if ((flags & VIR_QEMU_PROCESS_START_AUTODESTROY) && !conn) { - virReportError(VIR_ERR_INTERNAL_ERROR, "%s", - _("Domain autodestroy requires a connection handle"= )); - return -1; + if (flags & VIR_QEMU_PROCESS_START_AUTODESTROY) { + if (!conn) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("Domain autodestroy requires a connection han= dle")); + return -1; + } + if (driver->embedded) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("Domain autodestroy not supported for embedde= d drivers yet")); + return -1; + } } =20 hookData.vm =3D vm; diff --git a/tests/domaincapstest.c b/tests/domaincapstest.c index c6700f7e78..16a37b37fe 100644 --- a/tests/domaincapstest.c +++ b/tests/domaincapstest.c @@ -373,7 +373,7 @@ mymain(void) #endif =20 #if WITH_QEMU - virQEMUDriverConfigPtr cfg =3D virQEMUDriverConfigNew(false); + virQEMUDriverConfigPtr cfg =3D virQEMUDriverConfigNew(false, ""); =20 if (!cfg) return EXIT_FAILURE; diff --git a/tests/testutilsqemu.c b/tests/testutilsqemu.c index 7b9e427463..3a21dfac71 100644 --- a/tests/testutilsqemu.c +++ b/tests/testutilsqemu.c @@ -694,7 +694,7 @@ int qemuTestDriverInit(virQEMUDriver *driver) if (virMutexInit(&driver->lock) < 0) return -1; =20 - driver->config =3D virQEMUDriverConfigNew(false); + driver->config =3D virQEMUDriverConfigNew(false, ""); if (!driver->config) goto error; =20 --=20 2.23.0 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list From nobody Thu Apr 25 23:05:20 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 205.139.110.120 as permitted sender) client-ip=205.139.110.120; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-1.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 205.139.110.120 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1575299046; cv=none; d=zohomail.com; s=zohoarc; b=Nwmie5w0vtfUR6boeptwXiRdUvDDVO3jvOnO/xTJw8RFEkRWr+EdZjrMLbyJrkPlJGIEkonEOD451p9sv5pv52oPqlfQoWTWPQPPyulg6tdQCDudl01Zb6Yk3HaMRzzr3lrobz4L5OjH2HShJshvnNJS40Y/MyVcZACpHEla97s= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1575299046; h=Content-Type:Content-Transfer-Encoding:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=nQAMP0wzZB2mjCp6X9dCorX7o9KTfA6xN8aqLtt9LW4=; b=HljtcujaJP/8MGSZ/j1G+thkr3q/PcQtj/rPw70Hscyns0+2bVoFJVK9QmvThXCjvG+nXBjnElmcgvykGje+WhvRRAziWr+FLo1E1aPDwfS6gmgwmVAU6nvnc/ZtX/EKC3+9MENBlqTZy+8LRrVkAJIceSITmqm3qOVmzU5eBng= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 205.139.110.120 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) header.from= Return-Path: Received: from us-smtp-1.mimecast.com (us-smtp-delivery-1.mimecast.com [205.139.110.120]) by mx.zohomail.com with SMTPS id 1575299046127265.7699651038274; Mon, 2 Dec 2019 07:04:06 -0800 (PST) Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-326-yKGa-r1ZNvOEfYKX7mg_WQ-1; Mon, 02 Dec 2019 10:04:00 -0500 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 38F5118C35C0; Mon, 2 Dec 2019 15:03:55 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 0A32A600CC; Mon, 2 Dec 2019 15:03:55 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id ACD475BC02; Mon, 2 Dec 2019 15:03:54 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id xB2F3eY3030613 for ; Mon, 2 Dec 2019 10:03:40 -0500 Received: by smtp.corp.redhat.com (Postfix) id 7141F600CA; Mon, 2 Dec 2019 15:03:40 +0000 (UTC) Received: from dhcp-16-105.lcy.redhat.com (unknown [10.42.16.105]) by smtp.corp.redhat.com (Postfix) with ESMTP id E25E3600C9; Mon, 2 Dec 2019 15:03:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1575299044; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=nQAMP0wzZB2mjCp6X9dCorX7o9KTfA6xN8aqLtt9LW4=; b=XuapKEVx2zdhrDneiRvASAlDdKn7jKoXAAx2B3kQCzlIBvhcB2josi9VC67Szo7fnr50QV 5PcaRS99m7Uw2aaun6Uqt1XnkBfBKu1W5UJqbGkEyxeDyHTqWoiwHfZBX7Yud9Gbw4l1zy 3RVSTU8lnxcdu3mqcyh7TbZyj/S5ez4= From: =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= To: libvir-list@redhat.com Date: Mon, 2 Dec 2019 15:03:30 +0000 Message-Id: <20191202150331.173475-7-berrange@redhat.com> In-Reply-To: <20191202150331.173475-1-berrange@redhat.com> References: <20191202150331.173475-1-berrange@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH 6/7] secrets: add support for running secret driver in embedded mode X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-MC-Unique: yKGa-r1ZNvOEfYKX7mg_WQ-1 X-Mimecast-Spam-Score: 0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) This enables support for running the secret driver embedded to the calling application process using a URI: secret:///embed?root=3D/some/path When using the embedded mode with a root=3D/var/tmp/embed, the driver will use the following paths: configDir: /var/tmp/embed/etc/secrets stateDir: /var/tmp/embed/run/secrets These are identical whether the embedded driver is privileged or unprivileged. This compares with the system instance which uses configDir: /etc/libvirt/secrets stateDir: /var/lib/libvirt/secrets When an embedded instance of the secret driver is open, any other embedded drivers will automatically use the embedded secret driver. Signed-off-by: Daniel P. Berrang=C3=A9 --- docs/drivers.html.in | 1 + docs/drvsecret.html.in | 82 ++++++++++++++++++++++++++++++++++++++ src/secret/secret_driver.c | 46 ++++++++++++++++----- 3 files changed, 118 insertions(+), 11 deletions(-) create mode 100644 docs/drvsecret.html.in diff --git a/docs/drivers.html.in b/docs/drivers.html.in index 4539eedbcd..cb1ddc9212 100644 --- a/docs/drivers.html.in +++ b/docs/drivers.html.in @@ -8,6 +8,7 @@

  • Hypervisor drivers
  • Storage drivers
  • Node device driver
  • +
  • Secret driver
  • =20

    diff --git a/docs/drvsecret.html.in b/docs/drvsecret.html.in new file mode 100644 index 0000000000..9a05fe1f09 --- /dev/null +++ b/docs/drvsecret.html.in @@ -0,0 +1,82 @@ + + + + +

    Secret information management

    + +

    + The secrets driver in libvirt provides a simple interface for + storing and retrieving secret information. +

    + +

    Connections to SECRET driver

    + +

    + The libvirt SECRET driver is a multi-instance driver, providing a sing= le + system wide privileged driver (the "system" instance), and per-user + unprivileged drivers (the "session" instance). A connection to the sec= ret + driver is automatically available when opening a connection to one of = the + stateful primary hypervisor drivers. It is none the less also possible= to + explicitly open just the secret driver, using the URI protocol "secret" + Some example connection URIs for the driver are: +

    + +
    +secret:///session                      (local access to per-user instance)
    +secret+unix:///session                 (local access to per-user instance)
    +
    +secret:///system                       (local access to system instance)
    +secret+unix:///system                  (local access to system instance)
    +secret://example.com/system            (remote access, TLS/x509)
    +secret+tcp://example.com/system        (remote access, SASl/Kerberos)
    +secret+ssh://root@example.com/system   (remote access, SSH tunnelled)
    +
    + +

    Embedded driver

    + +

    + Since 6.0.0 the secret driver has experimental support for operating + in an embedded mode. In this scenario, rather than connecting to + the libvirtd daemon, the secret driver runs in the client application + process directly. To open the driver in embedded mode the app use the + new URI path and specify a virtual root directory under which the + driver will create content. +

    + +
    +      secret:///embed?root=3D/some/dir
    +    
    + +

    + Under the specified root directory the following locations will + be used +

    + +
    +/some/dir
    +  |
    +  +- etc
    +  |   |
    +  |   +- secrets
    +  |
    +  +- run
    +      |
    +      +- secrets
    +    
    + +

    + The application is responsible for recursively purging the contents + of this directory tree once they no longer require a connection, + though it can also be left intact for reuse when opening a future + connection. +

    + +

    + The range of functionality is intended to be on a par with that + seen when using the traditional system or session libvirt connections + to QEMU. Normal practice would be to open the secret driver in embed= ded + mode any time one of the other drivers is opened in embedded mode so + that the two drivers can interact in-process. +

    + + diff --git a/src/secret/secret_driver.c b/src/secret/secret_driver.c index d248121327..c791bde2f9 100644 --- a/src/secret/secret_driver.c +++ b/src/secret/secret_driver.c @@ -55,6 +55,8 @@ typedef virSecretDriverState *virSecretDriverStatePtr; struct _virSecretDriverState { virMutex lock; bool privileged; /* readonly */ + bool embedded; /* readonly */ + int embeddedRefs; virSecretObjListPtr secrets; char *stateDir; char *configDir; @@ -456,12 +458,6 @@ secretStateInitialize(bool privileged, virStateInhibitCallback callback G_GNUC_UNUSED, void *opaque G_GNUC_UNUSED) { - if (root !=3D NULL) { - virReportError(VIR_ERR_INVALID_ARG, "%s", - _("Driver does not support embedded mode")); - return -1; - } - if (VIR_ALLOC(driver) < 0) return VIR_DRV_STATE_INIT_ERROR; =20 @@ -474,8 +470,12 @@ secretStateInitialize(bool privileged, =20 driver->secretEventState =3D virObjectEventStateNew(); driver->privileged =3D privileged; + driver->embedded =3D root !=3D NULL; =20 - if (privileged) { + if (root) { + driver->configDir =3D g_strdup_printf("%s/etc/secrets", root); + driver->stateDir =3D g_strdup_printf("%s/run/secrets", root); + } else if (privileged) { driver->configDir =3D g_strdup_printf("%s/libvirt/secrets", SYSCON= FDIR); driver->stateDir =3D g_strdup_printf("%s/libvirt/secrets", RUNSTAT= EDIR); } else { @@ -552,19 +552,42 @@ secretConnectOpen(virConnectPtr conn, return VIR_DRV_OPEN_ERROR; } =20 - if (!virConnectValidateURIPath(conn->uri->path, - "secret", - driver->privileged)) - return VIR_DRV_OPEN_ERROR; + if (driver->embedded) { + if (STRNEQ(conn->uri->path, "/embed")) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("URI must be secret:///embed")); + return VIR_DRV_OPEN_ERROR; + } + } else { + if (!virConnectValidateURIPath(conn->uri->path, + "secret", + driver->privileged)) + return VIR_DRV_OPEN_ERROR; + } =20 if (virConnectOpenEnsureACL(conn) < 0) return VIR_DRV_OPEN_ERROR; =20 + if (driver->embedded) { + secretDriverLock(); + if (driver->embeddedRefs =3D=3D 0) + virSetConnectSecret(conn); + driver->embeddedRefs++; + secretDriverUnlock(); + } + return VIR_DRV_OPEN_SUCCESS; } =20 static int secretConnectClose(virConnectPtr conn G_GNUC_UNUSED) { + if (driver->embedded) { + secretDriverLock(); + driver->embeddedRefs--; + if (driver->embeddedRefs =3D=3D 0) + virSetConnectSecret(NULL); + secretDriverUnlock(); + } return 0; } =20 @@ -657,6 +680,7 @@ static virHypervisorDriver secretHypervisorDriver =3D { static virConnectDriver secretConnectDriver =3D { .localOnly =3D true, .uriSchemes =3D (const char *[]){ "secret", NULL }, + .embeddable =3D true, .hypervisorDriver =3D &secretHypervisorDriver, .secretDriver =3D &secretDriver, }; --=20 2.23.0 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list From nobody Thu Apr 25 23:05:20 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 205.139.110.120 as permitted sender) client-ip=205.139.110.120; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-1.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 205.139.110.120 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1575299119; cv=none; d=zohomail.com; s=zohoarc; b=XvM9RvL6E11a83tnSt8BoXojb46ni9P/ux4/vT4eSYkWKU57umSi9cbdq6aWV7DVWZ78Bp8gtoqYzZQeRiCFhaeS/ZNypFl2i4yWB45YtfMFcXkA53A+4xUYZYzeavOs+aP/N6cuWie2GSrQYNeZSobYCZNHHoOMAJLv5R0u+v0= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1575299119; h=Content-Type:Content-Transfer-Encoding:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=CRcZdmIASiHlt6GovbdcijhP7rgf6iW3ISdURnJXI+Y=; b=Qsnk83mxgu4yNDiYGARYHEHivo7icYEL5qtKfACRqQsrZshKgO4ltZBOEW5HZPXUMHsz+7GFd6kus1g0kDDGyGd2q1bDn1C1oKwLPudf3rx7ZcWCcQVQPngnAcLX9UuTzYd9i9AiPX+2Z+UjNInNC2I5914Atq07T/pmecTDsjI= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 205.139.110.120 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) header.from= Return-Path: Received: from us-smtp-1.mimecast.com (us-smtp-delivery-1.mimecast.com [205.139.110.120]) by mx.zohomail.com with SMTPS id 1575299119631157.57591317524918; Mon, 2 Dec 2019 07:05:19 -0800 (PST) Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-328-2QMdcpsgOXS-5N18Pzsh8g-1; Mon, 02 Dec 2019 10:04:04 -0500 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id CE9FCA1941; Mon, 2 Dec 2019 15:03:57 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id A4461600CD; Mon, 2 Dec 2019 15:03:57 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id 50ED018034F0; Mon, 2 Dec 2019 15:03:57 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id xB2F3fnF030626 for ; Mon, 2 Dec 2019 10:03:41 -0500 Received: by smtp.corp.redhat.com (Postfix) id 5E654600CC; Mon, 2 Dec 2019 15:03:41 +0000 (UTC) Received: from dhcp-16-105.lcy.redhat.com (unknown [10.42.16.105]) by smtp.corp.redhat.com (Postfix) with ESMTP id B15A3600C9; Mon, 2 Dec 2019 15:03:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1575299118; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=CRcZdmIASiHlt6GovbdcijhP7rgf6iW3ISdURnJXI+Y=; b=V186HkCr1e10ES26xkNNs3Re7bNAVuIJaIz5KpSTDwFe8K/wob5AAku9ZgyU1qlQiDje0y ZbwftqLxdVFJC/Ty3ow7T/qvZpJ34HeRVYfvf7OV75wZ3uY2dc5TN3qM+zSCF6TopkBRcc jo1fmxe/zw8NxXvCq+mZ0Nr10LrueXU= From: =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= To: libvir-list@redhat.com Date: Mon, 2 Dec 2019 15:03:31 +0000 Message-Id: <20191202150331.173475-8-berrange@redhat.com> In-Reply-To: <20191202150331.173475-1-berrange@redhat.com> References: <20191202150331.173475-1-berrange@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH 7/7] qemu: introduce a new "virt-qemu-run" program X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-MC-Unique: 2QMdcpsgOXS-5N18Pzsh8g-1 X-Mimecast-Spam-Score: 0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) The previous "QEMU shim" proof of concept was taking an approach of only caring about initial spawning of the QEMU process. It was then registered with the libvirtd daemon who took over management of it. The intent was that later libvirtd would be refactored so that the shim retained control over the QEMU monitor and libvirt just forwarded APIs to each shim as needed. This forwarding of APIs would require quite alot of significant refactoring of libvirtd to achieve. This impl thus takes a quite different approach, explicitly deciding to keep the VMs completely separate from those seen & managed by libvirtd. Instead it uses the new "qemu:///embed" URI scheme to embed the entire QEMU driver in the shim, running with a custom root directory. Once the driver is initialization, the shim starts a VM and then waits to shutdown automatically when QEMU shuts down, or should kill QEMU if it is terminated itself. This ought to use the AUTO_DESTROY feature but that is not yet available in embedded mode, so we rely on installing a few signal handlers to gracefully kill QEMU. This isn't reliable if we crash of course, but you can restart with the same root dir. Note this program does not expose any way to manage the QEMU process, since there's no RPC interface enabled. It merely starts the VM and cleans up when the guest shuts down at the end. This program is installed to /usr/bin/virt-qemu-run enabling direct use by end users. Most use cases will probably want to integrate the concept directly into their respective application codebases. This standalone binary serves as a nice demo though, and also provides a way to measure performance of the startup process quite simply. Signed-off-by: Daniel P. Berrang=C3=A9 --- build-aux/syntax-check.mk | 2 +- libvirt.spec.in | 2 + src/Makefile.am | 9 ++ src/qemu/Makefile.inc.am | 26 ++++ src/qemu/qemu_shim.c | 313 ++++++++++++++++++++++++++++++++++++++ src/qemu/qemu_shim.pod | 94 ++++++++++++ 6 files changed, 445 insertions(+), 1 deletion(-) create mode 100644 src/qemu/qemu_shim.c create mode 100644 src/qemu/qemu_shim.pod diff --git a/build-aux/syntax-check.mk b/build-aux/syntax-check.mk index 94e4945323..627734fc55 100644 --- a/build-aux/syntax-check.mk +++ b/build-aux/syntax-check.mk @@ -2340,7 +2340,7 @@ exclude_file_name_regexp--sc_prohibit_devname =3D \ ^(tools/virsh.pod|build-aux/syntax-check\.mk|docs/.*)$$ =20 exclude_file_name_regexp--sc_prohibit_virXXXFree =3D \ - ^(docs/|tests/|examples/|tools/|build-aux/syntax-check\.mk|src/test/test= _driver.c|src/libvirt_public.syms|include/libvirt/libvirt-(domain|network|n= odedev|storage|stream|secret|nwfilter|interface|domain-snapshot).h|src/libv= irt-(domain|qemu|network|nodedev|storage|stream|secret|nwfilter|interface|d= omain-snapshot).c$$) + ^(docs/|tests/|examples/|tools/|build-aux/syntax-check\.mk|src/test/test= _driver.c|src/libvirt_public.syms|include/libvirt/libvirt-(domain|network|n= odedev|storage|stream|secret|nwfilter|interface|domain-snapshot).h|src/libv= irt-(domain|qemu|network|nodedev|storage|stream|secret|nwfilter|interface|d= omain-snapshot).c|src/qemu/qemu_shim.c$$) =20 exclude_file_name_regexp--sc_prohibit_sysconf_pagesize =3D \ ^(build-aux/syntax-check\.mk|src/util/virutil\.c)$$ diff --git a/libvirt.spec.in b/libvirt.spec.in index c7fac104d9..886c7b73d5 100644 --- a/libvirt.spec.in +++ b/libvirt.spec.in @@ -1757,6 +1757,8 @@ exit 0 %{_libdir}/%{name}/connection-driver/libvirt_driver_qemu.so %dir %attr(0711, root, root) %{_localstatedir}/lib/libvirt/swtpm/ %dir %attr(0711, root, root) %{_localstatedir}/log/swtpm/libvirt/qemu/ +%{_bindir}/virt-qemu-run +%{_mandir}/man1/virt-qemu-run.1* %endif =20 %if %{with_lxc} diff --git a/src/Makefile.am b/src/Makefile.am index 696e64c52e..f86ba3fe3d 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -90,6 +90,8 @@ SYSTEMD_UNIT_FILES =3D SYSTEMD_UNIT_FILES_IN =3D SYSCONF_FILES =3D sbin_PROGRAMS =3D +bin_PROGRAMS =3D +man1_MANS =3D man8_MANS =3D DRIVER_SOURCES =3D man7_MANS =3D @@ -659,6 +661,13 @@ endif WITH_LIBVIRTD < $< > $@-t && \ mv $@-t $@ =20 +%.1: %.1.in $(top_srcdir)/configure.ac + $(AM_V_GEN)sed \ + -e 's|[@]sysconfdir[@]|$(sysconfdir)|g' \ + -e 's|[@]runstatedir[@]|$(runstatedir)|g' \ + < $< > $@-t && \ + mv $@-t $@ + CLEANFILES +=3D \ $(man8_MANS) \ $(MANINFILES) diff --git a/src/qemu/Makefile.inc.am b/src/qemu/Makefile.inc.am index bf30f8a3c5..d41853ac9e 100644 --- a/src/qemu/Makefile.inc.am +++ b/src/qemu/Makefile.inc.am @@ -231,3 +231,29 @@ EXTRA_DIST +=3D \ qemu/THREADS.txt \ libvirt_qemu_probes.d \ $(NULL) + +QEMU_SHIM_SOURCES =3D qemu/qemu_shim.c +QEMU_SHIM_POD =3D qemu/qemu_shim.pod + +EXTRA_DIST +=3D $(QEMU_SHIM_SOURCES) $(QEMU_SHIM_POD) + +if WITH_QEMU +bin_PROGRAMS +=3D virt-qemu-run + +man1_MANS +=3D virt-qemu-run.1 + +virt_qemu_run_SOURCES =3D $(QEMU_SHIM_SOURCES) + +virt_qemu_run_LDADD =3D libvirt.la +virt_qemu_run_LDFLAGS =3D -Wl,--export-dynamic +endif WITH_QEMU + +virt-qemu-run.1.in: qemu/qemu_shim.pod + $(AM_V_GEN)$(POD2MAN) $< $@-t1 && \ + if grep 'POD ERROR' $@-t1; then rm $@-t1; exit 1; fi && \ + sed \ + -e 's|SYSCONFDIR|\@sysconfdir\@|g' \ + -e 's|LOCALSTATEDIR|\@localstatedir\@|g' \ + < $@-t1 > $@-t2 && \ + rm -f $@-t1 && \ + mv $@-t2 $@ diff --git a/src/qemu/qemu_shim.c b/src/qemu/qemu_shim.c new file mode 100644 index 0000000000..10f9682143 --- /dev/null +++ b/src/qemu/qemu_shim.c @@ -0,0 +1,313 @@ +/* + * qemu_shim.c: standalone binary for running QEMU instances + * + * Copyright (C) 2019 Red Hat, Inc. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library. If not, see + * . + */ + +#include + +#include +#include + +#include "virfile.h" +#include "virstring.h" +#include "virgettext.h" + +#define VIR_FROM_THIS VIR_FROM_QEMU + +static bool eventQuitFlag; +static int eventQuitFD =3D -1; +static virDomainPtr dom; + +static void * +qemuShimEventLoop(void *opaque G_GNUC_UNUSED) +{ + while (!eventQuitFlag) + virEventRunDefaultImpl(); + + return NULL; +} + +/* Runs in event loop thread context */ +static void +qemuShimEventLoopStop(int watch G_GNUC_UNUSED, + int fd G_GNUC_UNUSED, + int event G_GNUC_UNUSED, + void *opaque G_GNUC_UNUSED) +{ + char c; + ignore_value(read(fd, &c, 1)); + eventQuitFlag =3D true; +} + +/* Runs in event loop thread context */ +static int +qemuShimDomShutdown(virConnectPtr econn G_GNUC_UNUSED, + virDomainPtr edom G_GNUC_UNUSED, + int event, + int detail G_GNUC_UNUSED, + void *opaque G_GNUC_UNUSED) +{ + if (event =3D=3D VIR_DOMAIN_EVENT_STOPPED) + eventQuitFlag =3D true; + + return 0; +} + +/* Runs in unknown thread context */ +static void +qemuShimSigShutdown(int sig G_GNUC_UNUSED) +{ + if (dom) + virDomainDestroy(dom); + ignore_value(safewrite(eventQuitFD, "c", 1)); +} + +static void +qemuShimQuench(void *userData G_GNUC_UNUSED, + virErrorPtr error G_GNUC_UNUSED) +{ +} + +int main(int argc, char **argv) +{ + GThread *eventLoopThread =3D NULL; + virConnectPtr conn =3D NULL; + virConnectPtr sconn =3D NULL; + g_autofree char *xml =3D NULL; + g_autofree char *uri =3D NULL; + g_autofree char *suri =3D NULL; + char *root =3D NULL; + bool tmproot =3D false; + int ret =3D 1; + g_autoptr(GError) error =3D NULL; + g_auto(GStrv) secrets =3D NULL; + bool verbose =3D false; + GStrv tmpsecrets; + GOptionContext *ctx; + GOptionEntry entries[] =3D { + { "secret", 's', 0, G_OPTION_ARG_STRING_ARRAY, &secrets, "Load sec= ret file", "SECRET-XML-FILE,SECRET-VALUE-FILE" }, + { "root", 'r', 0, G_OPTION_ARG_STRING, &root, "Root directory", "D= IR"}, + { "verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose, "Verbose output"= , NULL }, + { NULL } + }; + int quitfd[2] =3D {-1, -1}; + long long start =3D g_get_monotonic_time(); + +#define deltams() ((long long)g_get_monotonic_time() - start) + + ctx =3D g_option_context_new("- run a standalone QEMU process"); + g_option_context_add_main_entries(ctx, entries, PACKAGE); + if (!g_option_context_parse(ctx, &argc, &argv, &error)) { + g_printerr("%s: option parsing failed: %s\n", + argv[0], error->message); + return 1; + } + if (argc !=3D 2) { + g_autofree char *help =3D g_option_context_get_help(ctx, TRUE, NUL= L); + g_printerr("%s", help); + return 1; + } + + if (verbose) + g_printerr("%s: %lld: initializing libvirt\n", + argv[0], deltams()); + + if (virInitialize() < 0) { + g_printerr("%s: cannot initialize libvirt\n", argv[0]); + return 1; + } + if (virGettextInitialize() < 0) { + g_printerr("%s: cannot initialize libvirt translations\n", argv[0]= ); + return 1; + } + + virSetErrorFunc(NULL, qemuShimQuench); + + if (verbose) + g_printerr("%s: %lld: initializing signal handlers\n", + argv[0], deltams()); + + signal(SIGTERM, qemuShimSigShutdown); + signal(SIGINT, qemuShimSigShutdown); + signal(SIGQUIT, qemuShimSigShutdown); + signal(SIGHUP, qemuShimSigShutdown); + + if (root =3D=3D NULL) { + if (!(root =3D g_dir_make_tmp("libvirt-qemu-shim-XXXXXX", &error))= ) { + g_printerr("%s: cannot create temporary dir: %s\n", + argv[0], error->message); + return 1; + } + tmproot =3D true; + } + + virFileActivateDirOverrideForProg(argv[0]); + + if (verbose) + g_printerr("%s: %lld: preparing event loop thread\n", + argv[0], deltams()); + virEventRegisterDefaultImpl(); + + if (pipe(quitfd) < 0) { + g_printerr("%s: cannot create event loop pipe: %s", + argv[0], g_strerror(errno)); + goto cleanup; + } + + if (virEventAddHandle(quitfd[0], VIR_EVENT_HANDLE_READABLE, qemuShimEv= entLoopStop, NULL, NULL) < 0) { + VIR_FORCE_CLOSE(quitfd[0]); + VIR_FORCE_CLOSE(quitfd[1]); + quitfd[0] =3D quitfd[1] =3D -1; + g_printerr("%s: cannot register event loop handle: %s", + argv[0], virGetLastErrorMessage()); + goto cleanup; + } + eventQuitFD =3D quitfd[1]; + + eventLoopThread =3D g_thread_new("event-loop", qemuShimEventLoop, NULL= ); + + if (secrets && *secrets) { + suri =3D g_strdup_printf("secret:///embed?root=3D%s", root); + + if (verbose) + g_printerr("%s: %lld: opening %s\n", + argv[0], deltams(), suri); + + sconn =3D virConnectOpen(suri); + if (!sconn) { + g_printerr("%s: cannot open %s: %s\n", + argv[0], suri, virGetLastErrorMessage()); + goto cleanup; + } + + tmpsecrets =3D secrets; + while (tmpsecrets && *tmpsecrets) { + g_auto(GStrv) bits =3D g_strsplit(*tmpsecrets, ",", 2); + g_autofree char *sxml =3D NULL; + g_autofree char *value =3D NULL; + virSecretPtr sec; + size_t nvalue; + + if (!bits || bits[0] =3D=3D NULL || bits[1] =3D=3D NULL) { + g_printerr("%s: expected a pair of filenames for --secret = argument\n", + argv[0]); + goto cleanup; + } + + if (verbose) + g_printerr("%s: %lld: loading secret %s and %s\n", + argv[0], deltams(), bits[0], bits[1]); + + if (!g_file_get_contents(bits[0], &sxml, NULL, &error)) { + g_printerr("%s: cannot read secret XML %s: %s\n", + argv[0], bits[0], error->message); + goto cleanup; + } + + if (!g_file_get_contents(bits[1], &value, &nvalue, &error)) { + g_printerr("%s: cannot read secret value %s: %s\n", + argv[0], bits[1], error->message); + goto cleanup; + } + + if (!(sec =3D virSecretDefineXML(sconn, sxml, 0))) { + g_printerr("%s: cannot define secret %s: %s\n", + argv[0], bits[0], virGetLastErrorMessage()); + goto cleanup; + } + + if (virSecretSetValue(sec, (unsigned char *)value, nvalue, 0) = < 0) { + virSecretFree(sec); + g_printerr("%s: cannot set value for secret %s: %s\n", + argv[0], bits[0], virGetLastErrorMessage()); + goto cleanup; + } + virSecretFree(sec); + + tmpsecrets++; + } + } + + uri =3D g_strdup_printf("qemu:///embed?root=3D%s", root); + + if (verbose) + g_printerr("%s: %lld: opening %s\n", + argv[0], deltams(), uri); + + conn =3D virConnectOpen(uri); + if (!conn) { + g_printerr("%s: cannot open %s: %s\n", + argv[0], uri, virGetLastErrorMessage()); + goto cleanup; + } + + if (virConnectDomainEventRegisterAny( + conn, dom, VIR_DOMAIN_EVENT_ID_LIFECYCLE, + VIR_DOMAIN_EVENT_CALLBACK(qemuShimDomShutdown), + NULL, NULL) < 0) { + g_printerr("%s: cannot regiser for lifecycle events: %s\n", + argv[0], virGetLastErrorMessage()); + goto cleanup; + } + + if (verbose) + g_printerr("%s: %lld: starting guest %s\n", + argv[0], deltams(), argv[1]); + + if (!g_file_get_contents(argv[1], &xml, NULL, &error)) { + g_printerr("%s: cannot read %s: %s\n", + argv[0], argv[1], error->message); + goto cleanup; + } + + dom =3D virDomainCreateXML(conn, xml, 0); + if (!dom) { + g_printerr("%s: cannot start VM: %s\n", + argv[0], virGetLastErrorMessage()); + goto cleanup; + } + if (verbose) + g_printerr("%s: %lld: guest running, Ctrl-C to stop\n", + argv[0], deltams()); + + ret =3D 0; + + cleanup: + if (ret !=3D 0 && eventQuitFD !=3D -1) + ignore_value(safewrite(eventQuitFD, "c", 1)); + + if (eventLoopThread !=3D NULL && (ret =3D=3D 0 || eventQuitFD !=3D -1)) + g_thread_join(eventLoopThread); + + VIR_FORCE_CLOSE(quitfd[0]); + VIR_FORCE_CLOSE(quitfd[1]); + + if (dom !=3D NULL) + virDomainFree(dom); + if (sconn !=3D NULL) + virConnectClose(sconn); + if (conn !=3D NULL) + virConnectClose(conn); + if (tmproot) + virFileDeleteTree(root); + + if (verbose) + g_printerr("%s: %lld: cleaned up, exiting\n", + argv[0], deltams()); + return ret; +} diff --git a/src/qemu/qemu_shim.pod b/src/qemu/qemu_shim.pod new file mode 100644 index 0000000000..eb17fa0ac7 --- /dev/null +++ b/src/qemu/qemu_shim.pod @@ -0,0 +1,94 @@ +=3Dencoding utf8 + +=3Dhead1 NAME + +virt-qemu-run - run a standalone QEMU guest + +=3Dhead1 SYNOPSIS + +B [I...] [I] + +=3Dhead1 DESCRIPTION + +This tool provides a way to run a standalone QEMU guest such that it +is completely independant of libvirtd. It makes use of the embedded +QEMU driver support to run the VM placing files under an isolated +directory tree. When the guest is run with this tool it is invisible +to libvirtd and thus also invisible to other libvirt tools such as +virsh. + +The virt-qemu-run program will run the QEMU virtual machine, and then +block until the guest OS shuts down, at which point it will exit. + +If the virt-qemu-run program is interrupted (eg Ctrl-C) it will +immediately terminate the virtual machine without giving the guest +OS any opportunity to gracefully shutdown. + +=3Dhead1 OPTIONS + +=3Dover 4 + +=3Ditem C + +The full path to the XML file describing the guest virtual machine +to be booted. + +=3Ditem C<-h>, C<--help> + +Display the command line help + +=3Ditem C<-v>, C<--verbose> + +Display verbose information about startup + +=3Ditem C<-r DIR>, C<--root=3DDIR> + +Specify the root directory to use for storing state associated with +the virtual machine. The caller is responsible for deleting this +directory when it is no longer required. + +If this parameter is omitted, then a random temporary directory +will be created, and its contents be automaticlaly deleted at +VM shutdown. + +=3Ditem C<-s XML-FILE,VALUE-FILE> C<--secret=3DXML-FILE,VALUE-FILE> + +Specify a secret to be loaded into the secret driver. The C +is a path to the XML description of the secret, whose UUID should +match a secret referenced in the guest domain XML. The C +is a path containing the raw value of the secret. + +=3Dback + +=3Dhead1 EXIT STATUS + +Upon successful shutdown, an exit status of 0 will be set. Upon +failure a non-zero status will be set. + +=3Dhead1 AUTHOR + +Daniel P. Berrang=C3=A9 + +=3Dhead1 BUGS + +Report any bugs discovered to the libvirt community via the +mailing list L or bug tracker +L. +Alternatively report bugs to your software distributor / vendor. + +=3Dhead1 COPYRIGHT + +Copyright (C) 2019 by Red Hat, Inc. + +=3Dhead1 LICENSE + +virt-run-qemu is distributed under the terms of the GNU LGPL v2+. +This is free software; see the source for copying conditions. There +is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR +PURPOSE + +=3Dhead1 SEE ALSO + +L + +=3Dcut --=20 2.23.0 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list