Bug 1356461

Summary: Failed "virsh connect" return 0.
Product: Red Hat Enterprise Linux 7 Reporter: Yanqiu Zhang <yanqzhan>
Component: libvirtAssignee: Martin Kletzander <mkletzan>
Status: CLOSED ERRATA QA Contact: Virtualization Bugs <virt-bugs>
Severity: medium Docs Contact:
Priority: medium    
Version: 7.3CC: dyuan, fjin, mzhan, rbalakri, roliga.here, yafu, yanqzhan, zpeng
Target Milestone: rc   
Target Release: ---   
Hardware: x86_64   
OS: Linux   
Whiteboard:
Fixed In Version: libvirt-2.0.0-4.el7 Doc Type: If docs needed, set a value
Doc Text:
Story Points: ---
Clone Of: Environment:
Last Closed: 2016-11-03 18:48:48 UTC Type: Bug
Regression: --- Mount Type: ---
Documentation: --- CRM:
Verified Versions: Category: ---
oVirt Team: --- RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: --- Target Upstream Version:
Embargoed:

Description Yanqiu Zhang 2016-07-14 07:56:18 UTC
Description of problem:
Failed "virsh connect" return 0.

Version-Release number of selected component (if applicable):
libvirt-2.0.0-2.el7.x86_64
qemu-kvm-rhev-2.6.0-12.el7.x86_64
kernel-3.10.0-461.el7.x86_64

How reproducible:
100%

Steps to Reproduce:
1. virsh connect to an unexisting hypervisior
# virsh connect ddd
error: Failed to connect to the hypervisor
error: no connection driver available for ddd

2.Show return value:
# echo $?
0

Actual results:
return 0, as step 2 descripted.

Expected results:
 Should return 1 for failed connection.
# echo $?
1

Additional info:
1. It works well on rhel7.2, pkginfo:
kernel-3.10.0-327.el7.x86_64
libvirt-1.2.17-13.el7_2.5.x86_64
Qemu-kvm-rhev-2.3.0-31.el7_2.14.x86_64

2.It has influence on libvirt-guests service:
for 'usr/libexec/libvirt-guests.sh', there's a function "test_connect()" to test uri connection:
//----------------------
    while [ $i -gt 0 ]; do
        run_virsh "$uri" connect 2>/dev/null
        if [ $? -eq 0 ]; then
            return 0;
        fi
        sleep ${RETRIES_SLEEP}
        eval_gettext "Unable to connect to libvirt currently. Retrying .. \$i"
        i=$(($i-1))
    done
    eval_gettext "Can't connect to \$uri. Skipping."
    echo
    return 1
//----------------------------
So if "virsh connect" always return 0, the message "Can't connect to \$uri. Skipping"  can  never be displayed.  Steps to show influence:
#systemctl stop libvirtd
#systemctl stop libvirt-guests
#systemctl status libvirt-guests
ON RHEL7.2:
Unable to connect to libvirt currently. Retrying .. 9
Unable to connect to libvirt currently. Retrying .. 8
......
Unable to connect to libvirt currently. Retrying .. 1
Can't connect to default. Skipping.
ON RHEL7.3:
Running guests on default URI: error: failed to connect to the hypervisor
error: no valid connection
error: Failed to connect socket to '/var/run/libvirt/libvirt-sock': No such file or directory

Comment 2 roliga.here 2016-07-22 21:00:12 UTC
To narrow this down the change in return value seems to have appeared between libvirt version 1.3.4 and 1.3.5, and looking through the git logs commit d218344e6c1afce5788d7f72b78859219c50a3de could be a suspect:

commit d218344e6c1afce5788d7f72b78859219c50a3de
Author: Eric Blake <eblake>
Date:   Mon Mar 14 14:30:24 2011 -0600

    virsh: optimize creation of default connection
    
    Ramon de Carvalho Valle reported a problem with:
    virsh connect qemu:///system
    as a non-root user.  The real root problem appears to be a regression
    in libvirtd being auto-started on the default qemu:///session URI;
    however, the symptom points to an independent flaw in virsh - we
    shouldn't be wasting efforts on making a connection if we aren't going
    to be using that connection.  Fixing virsh avoids Ramon's issue, while
    I work in the meantime to fix the real libvirtd regression.
    
    This patch looks big, but that's because 'gcc -Wmissing-field-initializers'
    gets triggered by './autobuild.sh --enable-compile-warnings=error', so I
    had to add 0 initialization to everyone (rather than my preference of
    just adding the non-zero flags to virshCmds and to cmdConnect).
    
    Meanwhile, if you use 'virsh -c URI', the connection must succeed; this
    patch _only_ optimizes the default connection to be deferred to a later
    point where we know if a particular command to be run needs a connection.
    
    * tools/virsh.c (VSH_CMD_FLAG_NOCONNECT): New flag.
    (vshCmdDef): Add new flags field.
    (vshCommandRun): Honor new flag.
    (domManagementCmds, domMonitoringCmds, storagePoolCmds)
    (storageVolCmds, networkCmds, nodedevCmds, ifaceCmds)
    (nwfilterCmds, secretCmds, virshCmds, snapshotCmds)
    (hostAndHypervisorCmds): Populate new field.
    (vshReconnect): Don't warn on initial connection.


As this suggests, running virsh with a non-default connection using the -c argument does indeed yield the expected results:

# virsh -c qemu:///system; echo $?
error: failed to connect to the hypervisor
error: Failed to connect socket to '/var/run/libvirt/libvirt-sock': No such file or directory
1
# virsh connect; echo $?
error: failed to connect to the hypervisor

0
# virsh -v
1.3.5

Comment 3 Ján Tomko 2016-07-26 08:35:48 UTC
'git bisect' points to:
commit 0c56d94318393a8295cc0613abb6097a6fd60002

    tools: Fix connect command

git describe: v1.3.4-2-g0c56d94 contains: v1.3.5-rc1~505

Comment 4 Martin Kletzander 2016-08-02 11:37:21 UTC
Fixed upstream with v2.1.0-rc1-33-gff498a9ac70a:

commit ff498a9ac70a550bed08233a86e00a4faf12aecf
Author: Martin Kletzander <mkletzan>
Date:   Fri Jul 29 08:09:22 2016 +0200

    virsh: Report error when explicit connection fails

Comment 7 yafu 2016-08-08 09:03:45 UTC
Reproduced with libvirt-2.0.0-2.el7.x86_64.
Steps are as comment 0.

Verified pass with libvirt-2.0.0-4.el7.x86_64.
Test steps:
1.$virsh connect abcd
 error: failed to connect to the hypervisor
 error: no connection driver available for abcd

2.Check the return value:
  $echo $?
   1

3.$virsh connect abcd --readonly
 error: failed to connect to the hypervisor
 error: no connection driver available for abcd

4.Check the return value:
  $echo $?
   1

5.$virsh connect qemu:///session

6.Check the return value:
 $echo $?
   0

7.$virsh connnect


8.Check the return value:
 $echo $?
 0

9.$virsh "connect abcd; connect; uri"
error: failed to connect to the hypervisor
error: no connection driver available for abcd


qemu:///session

10.Check the return value:
$echo $?
0

Comment 9 errata-xmlrpc 2016-11-03 18:48:48 UTC
Since the problem described in this bug report should be
resolved in a recent advisory, it has been closed with a
resolution of ERRATA.

For information on the advisory, and where to find the updated
files, follow the link below.

If the solution does not work for you, open a new bug report.

https://rhn.redhat.com/errata/RHSA-2016-2577.html