Bug 1379895

Summary: Virsh/virt-admin client doesn't write the history commands into file
Product: Red Hat Enterprise Linux 7 Reporter: Fangge Jin <fjin>
Component: libvirtAssignee: John Ferlan <jferlan>
Status: CLOSED ERRATA QA Contact: Lili Zhu <lizhu>
Severity: medium Docs Contact:
Priority: medium    
Version: 7.3CC: dyuan, eskultet, jdenemar, jferlan, mzhan, rbalakri, tlavigne, xuzhang, zpeng
Target Milestone: rcKeywords: Regression
Target Release: ---   
Hardware: x86_64   
OS: Linux   
Whiteboard:
Fixed In Version: libvirt-2.5.0-1.el7 Doc Type: Bug Fix
Doc Text:
Cause: Erroneous skip of setting if interactive mode was not set; however, the field to check is cleared for 'quit' and 'exit' commands (NB: using <ctrl>D would save the history). Consequence: When exiting the "virsh" using the 'quit' or 'exit' command, the shell history will be lost. Fix: Removed the unnecessary check for flag. Result: When exiting the "virsh" shell using the 'quit' or 'exit' command, the shell history will be updated.
Story Points: ---
Clone Of: Environment:
Last Closed: 2017-08-01 17:16:43 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:
Attachments:
Description Flags
strace output of virsh client none

Description Fangge Jin 2016-09-28 02:37:40 UTC
Created attachment 1205380 [details]
strace output of virsh client

Description of problem:
There are two problems that exist for both virsh client and virt-admin client, take virsh client as example:

1)Virsh client doesn't create history file automatically
# ll /root/.cache/libvirt/
total 0

# virsh
Welcome to virsh, the virtualization interactive terminal.

Type:  'help' for help with commands
       'quit' to quit

virsh # echo 1
1
virsh # echo 2
2
virsh # exit

After execute some commands in interactive mode and exit, no history file is created.
# ll /root/.cache/libvirt/
total 0


2)After I create history file manually, virsh client still doesn't flush the history commands into the file.
# mkdir /root/.cache/libvirt/virsh

# touch /root/.cache/libvirt/virsh/history

# strace -o /tmp/strace  virsh
Welcome to virsh, the virtualization interactive terminal.

Type:  'help' for help with commands
       'quit' to quit

virsh # echo 1
1
virsh # echo 2
2
virsh # exit

# cat /root/.cache/libvirt/virsh/history
(no output)


Version-Release number of selected component:
libvirt-2.0.0-10.el7.x86_64

How reproducible:
100%

Steps to Reproduce:
As description

Actual results:
As description

Expected results:
1) Virsh/virt-admin client can create history file automatically if no such file exists
2) Virsh/virt-admin client can flush the history commands into file when exits the interactive mode


Additional info:
RHEl7.2 build libvirt-1.2.17-13.el7_2.5.x86_64 works well

Comment 3 John Ferlan 2016-09-28 19:28:33 UTC
Instead of typing "exit" or "quit", press the key combo <ctrl>D and the history file is automagically created.  IOW: 'write_history' is called. So something in the exit/quit handling is amiss...

This is because during main() loop processing/parsing interactive commands runs while "ctl->imode" is true.  When exit or quit is typed, cmdQuit() is called clearing the 'imode' setting.  When "<ctrl>D" is typed, the main() loop processing will "break" from the loop because EOF is found.

When the loop is exited, main() will call virshDeinit() which calls vshDeinit() which will only call vshReadlineDeinit() [e.g. the function that writes out the history file] if ctl->imode is set. Since exit/quit clear it, the history file isn't written. 

The "workaround" is to use <ctrl>D (which fortunately I've trained my fingers to do long ago).

In 7.2, the vshReadlineDeinit() was called unconditionally from vshDeinit() [this prior to a rather massive change from libvirt commit id '834c5720'.]

I've posted a patch upstream :

http://www.redhat.com/archives/libvir-list/2016-September/msg01312.html

Comment 5 John Ferlan 2016-09-29 11:38:19 UTC
The patch has been pushed upstream:

commit bcfa2f427d53264830ff5fa5d029b6897773702d
Author: John Ferlan <jferlan>
Date:   Wed Sep 28 15:01:55 2016 -0400

    vsh: Write out history on "quit" or "exit" in interactive mode
    
...

$ git describe bcfa2f427d53264830ff5fa5d029b6897773702d
v2.3.0-rc1-9-gbcfa2f4
$

Comment 6 Fangge Jin 2016-10-08 08:38:28 UTC
Test with upstream version v2.3.0-40-g0f3f8ac, it works well.

Steps:

1.
# ll /root/.cache/libvirt/
total 0

2. Enter interactive mode of virsh client and do some operations:
1) # virsh
Welcome to virsh, the virtualization interactive terminal.

Type:  'help' for help with commands
       'quit' to quit

virsh # echo 1
1
virsh # echo 2
2
virsh # quit


2) # virsh
Welcome to virsh, the virtualization interactive terminal.

Type:  'help' for help with commands
       'quit' to quit

virsh # echo 3
3
virsh # echo 4
4
virsh # exit


3) # virsh
Welcome to virsh, the virtualization interactive terminal.

Type:  'help' for help with commands
       'quit' to quit

virsh # echo 5
5
virsh # echo 6
6
virsh #
(Press Ctrl+D)

3. Check the history file, the history commands are all recorded correctly:
# cat /root/.cache/libvirt/virsh/history
echo 1
echo 2
quit
echo 3
echo 4
exit
echo 5
echo 6

4. Re-do step 2 and step3 with virt-admin client, it also works well.

Comment 9 Lili Zhu 2017-06-15 03:09:29 UTC
Reproduce this bug with build libvirt-2.0.0-10.el7.x86_64
steps to reproduce:
Scenario 1: virsh
1.
# ll /root/.cache/libvirt/
total 0

2.
# virsh 
Welcome to virsh, the virtualization interactive terminal.

Type:  'help' for help with commands
       'quit' to quit

virsh # list --all
 Id    Name                           State
----------------------------------------------------
 -     rhel6.9-mt6.6                  shut off
 -     rhel6.9-q35                    shut off
 -     rhel7.4-mt7.3                  shut off
 -     rhel7.4-q35                    shut off

virsh # echo 1
1
virsh # echo 2
2
virsh # exit

3. 
# ll /root/.cache/libvirt/
total 0


Scenario 2: virt-admin
1. # virt-admin 
Welcome to virt-admin, the administrating virtualization interactive terminal.

Type:  'help' for help with commands
       'quit' to quit

virt-admin # srv-list 
 Id    Name           
---------------
 0     admin          
 1     libvirtd       

virt-admin # srv-clients-list admin 
 Id    Transport       Connected since
--------------------------------------------------
 1     unix            2017-06-15 10:33:24+0800

virt-admin # srv-clients-list libvirtd 
 Id    Transport       Connected since
--------------------------------------------------

virt-admin # exit

# ll /root/.cache/libvirt/

Verify this bug with build libvirt-3.2.0-10.el7.x86_64
Steps to verify:
Scenario 1: virsh
1. 
# ll /root/.cache/libvirt/
total 0

2.
# virsh 
Welcome to virsh, the virtualization interactive terminal.

Type:  'help' for help with commands
       'quit' to quit

virsh # echo 1
1
virsh # echo 2
2
virsh # list --all
 Id    Name                           State
----------------------------------------------------
 -     rhel6.9-mt6.6                  shut off

virsh # 
(Press Ctrl+D)

3.# ll /root/.cache/libvirt/virsh/
total 4
-rw-------. 1 root root 45 Jun 15 11:01 history

4. 
# virsh 
Welcome to virsh, the virtualization interactive terminal.

Type:  'help' for help with commands
       'quit' to quit

virsh # echo 5 
5
virsh # echo 6
6
virsh # exit

5. 
# cat /root/.cache/libvirt/virsh/history 
echo 1
echo 2
list --all
echo 5 
echo 6
exit

Scenario 2: virt-admin 
1.
# virt-admin 
Welcome to virt-admin, the administrating virtualization interactive terminal.

Type:  'help' for help with commands
       'quit' to quit

virt-admin # echo 7 
7
virt-admin # echo 8
8
virt-admin # exit

2.
# cat /root/.cache/libvirt/virt-admin/history 
echo 7 
echo 8
exit


As the result match the expectations, so mark the bug as verified.

Comment 10 errata-xmlrpc 2017-08-01 17:16:43 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://access.redhat.com/errata/RHEA-2017:1846

Comment 11 errata-xmlrpc 2017-08-01 23:57:38 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://access.redhat.com/errata/RHEA-2017:1846