From nobody Fri May 3 01:40:26 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 216.205.24.124 as permitted sender) client-ip=216.205.24.124; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-delivery-124.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 216.205.24.124 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=1603377750; cv=none; d=zohomail.com; s=zohoarc; b=EAWDJgf/cnCmH0Ripd+sjF35M/9BO8MUetI2rmuMYVyAACwXUbpqHvV53ZCtiQI5gp2P0fh9Pj8ScoApknFUmbXRgjdPGnd6U8AIwh/OfDQtTTBq0Q7ALl/yIK4VUjWB1QK5Hgua7IoJT94zJ7QG2jTvxsxuglJ4mVW/ISqngYk= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1603377750; h=Content-Type:Content-Transfer-Encoding:Date:From:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:To; bh=fQl8OFo8QPozQExbhVcaz3GUwFSTkoz5/+MfKwBmMb0=; b=PbARF4iweigJPSSmozJlOdAMsEsD8SM8jmiuKlLZIPXvNKa/FANdiwSXoQ0e1N/nfFiauV12VtulRrxEOavG8bdWK+pZH1bUq/lIqtMPojW2tuNP0/HqvTdnHErpDbMedbm1EksoP0EhyM3Q6vaszAOIbt4Az9NY0AsliPIJUGA= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 216.205.24.124 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-124.mimecast.com (us-smtp-delivery-124.mimecast.com [216.205.24.124]) by mx.zohomail.com with SMTPS id 1603377750919662.7995985436845; Thu, 22 Oct 2020 07:42:30 -0700 (PDT) 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-393-hX8UNq_VO4OUYApq93vWhg-1; Thu, 22 Oct 2020 10:42:27 -0400 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id DEFB0186DD24; Thu, 22 Oct 2020 14:42:19 +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 D955B5C1C7; Thu, 22 Oct 2020 14:42:17 +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 35CF7922F2; Thu, 22 Oct 2020 14:42:15 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 09MEflht003424 for ; Thu, 22 Oct 2020 10:41:47 -0400 Received: by smtp.corp.redhat.com (Postfix) id 4C4E75D9F1; Thu, 22 Oct 2020 14:41:47 +0000 (UTC) Received: from localhost.localdomain (unknown [10.40.195.27]) by smtp.corp.redhat.com (Postfix) with ESMTP id C231E5D9D5 for ; Thu, 22 Oct 2020 14:41:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1603377749; 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:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=fQl8OFo8QPozQExbhVcaz3GUwFSTkoz5/+MfKwBmMb0=; b=iRP3jlWkZ+4Va+U3NLjzfHNrgrAPKE573XA5uGXWVYhXHKUICbLepj2fPDLqvTJAua6qRG 7hVi/s28+9dnimhYKvQ7++P90bWIdUElWL3LD4kkqQYPHIFtdx/bKJPSsVDjxDzzf3g8/Q N0nrDRfIKP1vRRMWAKLwzqniAzvtXmQ= X-MC-Unique: hX8UNq_VO4OUYApq93vWhg-1 From: Michal Privoznik To: libvir-list@redhat.com Subject: [PATCH v2] virsh: Allow listing just domain IDs Date: Thu, 22 Oct 2020 16:41:18 +0200 Message-Id: <65fe3b4e654be4ad6f64be136c32fe3c530179e7.1603377586.git.mprivozn@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-loop: libvir-list@redhat.com 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.16 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=libvir-list-bounces@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) Content-Type: text/plain; charset="utf-8" Some completers for libvirt related tools might want to list domain IDs only. Just like the one I've implemented for virt-viewer [1]. I've worked around it using some awk magic, but if it was possible to just 'virsh list --id' then I could drop awk. 1: https://www.redhat.com/archives/virt-tools-list/2019-May/msg00014.html Signed-off-by: Michal Privoznik Reviewed-by: Martin Kletzander --- Diff to v1: - Documented the new switch in the manpage - Allowed --id to be used with --all docs/manpages/virsh.rst | 21 +++++++++--------- tools/virsh-domain-monitor.c | 42 +++++++++++++++++++++++++----------- 2 files changed, 39 insertions(+), 24 deletions(-) diff --git a/docs/manpages/virsh.rst b/docs/manpages/virsh.rst index d34a1c8684..848e1a6458 100644 --- a/docs/manpages/virsh.rst +++ b/docs/manpages/virsh.rst @@ -621,7 +621,7 @@ list =20 list [--inactive | --all] [--managed-save] [--title] - { [--table] | --name | --uuid } + { [--table] | --name | --uuid | --id } [--persistent] [--transient] [--with-managed-save] [--without-managed-save] [--autostart] [--no-autostart] @@ -758,16 +758,15 @@ If *--managed-save* is specified, then domains that h= ave managed save state in the listing. This flag is usable only with the default *--table* output. Note that this flag does not filter the list of domains. =20 -If *--name* is specified, domain names are printed instead of the table -formatted one per line. If *--uuid* is specified domain's UUID's are print= ed -instead of names. Flag *--table* specifies that the legacy table-formatted -output should be used. This is the default. - -If both *--name* and *--uuid* are specified, domain UUID's and names -are printed side by side without any header. Flag *--table* specifies -that the legacy table-formatted output should be used. This is the -default if neither *--name* nor *--uuid* are specified. Option -*--table* is mutually exclusive with options *--uuid* and *--name*. +If *--name* is specified, domain names are printed instead of the +table formatted one per line. If *--uuid* is specified domain's UUID's +are printed instead of names. If *--id* is specified then domain's ID's +are printed indead of names. However, it is possible to combine +*--name*, *--uuid* and *--id* to select only desired fields for +printing. Flag *--table* specifies that the legacy table-formatted +output should be used, but it is mutually exclusive with *--name*, +*--uuid* and *--id*. This is the default and will be used if neither of +*--name*, *--uuid* or *--id* is specified. =20 If *--title* is specified, then the short domain description (title) is printed in an extra column. This flag is usable only with the default diff --git a/tools/virsh-domain-monitor.c b/tools/virsh-domain-monitor.c index e0491d48ac..5d0a03afa9 100644 --- a/tools/virsh-domain-monitor.c +++ b/tools/virsh-domain-monitor.c @@ -1919,6 +1919,10 @@ static const vshCmdOptDef opts_list[] =3D { .type =3D VSH_OT_BOOL, .help =3D N_("list domain names only") }, + {.name =3D "id", + .type =3D VSH_OT_BOOL, + .help =3D N_("list domain IDs only") + }, {.name =3D "table", .type =3D VSH_OT_BOOL, .help =3D N_("list table (default)") @@ -1945,6 +1949,7 @@ cmdList(vshControl *ctl, const vshCmd *cmd) bool optTable =3D vshCommandOptBool(cmd, "table"); bool optUUID =3D vshCommandOptBool(cmd, "uuid"); bool optName =3D vshCommandOptBool(cmd, "name"); + bool optID =3D vshCommandOptBool(cmd, "id"); size_t i; char *title; char uuid[VIR_UUID_STRING_BUFLEN]; @@ -1988,8 +1993,9 @@ cmdList(vshControl *ctl, const vshCmd *cmd) =20 VSH_EXCLUSIVE_OPTIONS("table", "name"); VSH_EXCLUSIVE_OPTIONS("table", "uuid"); + VSH_EXCLUSIVE_OPTIONS("table", "id"); =20 - if (!optUUID && !optName) + if (!optUUID && !optName && !optID) optTable =3D true; =20 if (!(list =3D virshDomainListCollect(ctl, flags))) @@ -2007,6 +2013,8 @@ cmdList(vshControl *ctl, const vshCmd *cmd) } =20 for (i =3D 0; i < list->ndomains; i++) { + const char *sep =3D ""; + dom =3D list->domains[i]; id =3D virDomainGetID(dom); if (id !=3D (unsigned int) -1) @@ -2044,20 +2052,28 @@ cmdList(vshControl *ctl, const vshCmd *cmd) goto cleanup; } =20 - } else if (optUUID && optName) { - if (virDomainGetUUIDString(dom, uuid) < 0) { - vshError(ctl, "%s", _("Failed to get domain's UUID")); - goto cleanup; + } else { + if (optUUID) { + if (virDomainGetUUIDString(dom, uuid) < 0) { + vshError(ctl, "%s", _("Failed to get domain's UUID")); + goto cleanup; + } + vshPrint(ctl, "%s", uuid); + sep =3D " "; } - vshPrint(ctl, "%-36s %-30s\n", uuid, virDomainGetName(dom)); - } else if (optUUID) { - if (virDomainGetUUIDString(dom, uuid) < 0) { - vshError(ctl, "%s", _("Failed to get domain's UUID")); - goto cleanup; + if (optID) { + /* If we are asked to print IDs only then do that + * only for live domains. */ + if (id =3D=3D (unsigned int) -1 && !optUUID && !optName) + continue; + vshPrint(ctl, "%s%s", sep, id_buf); + sep =3D " "; } - vshPrint(ctl, "%s\n", uuid); - } else if (optName) { - vshPrint(ctl, "%s\n", virDomainGetName(dom)); + if (optName) { + vshPrint(ctl, "%s%s", sep, virDomainGetName(dom)); + sep =3D " "; + } + vshPrint(ctl, "\n"); } } =20 --=20 2.26.2