Bug 1449510

Summary: libvirt will remove the host file after detach some device or attach failed when enable namespace
Product: Red Hat Enterprise Linux 7 Reporter: Luyao Huang <lhuang>
Component: libvirtAssignee: Michal Privoznik <mprivozn>
Status: CLOSED ERRATA QA Contact: yafu <yafu>
Severity: medium Docs Contact:
Priority: medium    
Version: 7.4CC: dyuan, rbalakri, xuzhang, yafu
Target Milestone: rc   
Target Release: ---   
Hardware: All   
OS: Linux   
Whiteboard:
Fixed In Version: libvirt-3.2.0-5.el7 Doc Type: If docs needed, set a value
Doc Text:
Story Points: ---
Clone Of: Environment:
Last Closed: 2017-08-02 00:08:25 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 Luyao Huang 2017-05-10 07:59:25 UTC
Description of problem:
libvirt will remove the host resource file after detach some device or attach failed when enable namespace

Version-Release number of selected component (if applicable):
libvirt-3.2.0-4.el7.x86_64

How reproducible:
100%

Steps to Reproduce:
1. prepare a running guest

# virsh list --all
 Id    Name                           State
----------------------------------------------------
 1     r7                             running

# lsns -t mnt
        NS TYPE NPROCS   PID USER   COMMAND
...
4026532357 mnt       1  4100 qemu   /usr/libexec/qemu-kvm -name guest=r7,debug-threads=on -S -object secret,id=masterKey0,format=raw,file=/var/lib/libvirt/qemu/domain-1-r7/master-key.aes -machine pc-i440fx-rhel7
...

2. prepare a resource not in dev dir:

# touch /tmp/fakerandom

# cat rng.xml 
  <rng model='virtio'>
    <backend model='random'>/tmp/fakerandom</backend>
 </rng>

3. attach a rng device which use this file as an backend:

# virsh attach-device r7 rng.xml 
error: Failed to attach device from rng.xml
error: internal error: unable to execute QEMU command 'object-add': Could not open '/tmp/fakerandom': Permission denied

4. attach failed since label is not correct, but this file also been removed:
# ls /tmp/fakerandom
ls: cannot access /tmp/fakerandom: No such file or directory

Actual results:

libvirt will remove the host resource file after detach some device or attach failed when enable namespace, and i have test this won't happen when disable namespace

Expected results:

file still exist

Additional info:

I think more device will have this problem, since i guess the source file should be deleted by qemuDomainDetachDeviceUnlink, and detach rng, Chardev, nvdimm device will call this function. This file path still in the same namespace of host

Comment 5 Michal Privoznik 2017-06-15 09:06:03 UTC
(In reply to yafu from comment #4)
> Reproduced with libvirt-3.2.0-4.el7.x86_64, test steps are as comment 0.
> 
> Verified pass with libvirt-3.2.0-10.virtcov.el7.x86_64.

You should verify with regular builds and not virtcov ;-) But there's no difference in the code between the two, so you'll find the same result when trying the official build.

Comment 6 yafu 2017-06-16 03:01:29 UTC
(In reply to Michal Privoznik from comment #5)
> (In reply to yafu from comment #4)
> > Reproduced with libvirt-3.2.0-4.el7.x86_64, test steps are as comment 0.
> > 
> > Verified pass with libvirt-3.2.0-10.virtcov.el7.x86_64.
> 
> You should verify with regular builds and not virtcov ;-) But there's no
> difference in the code between the two, so you'll find the same result when
> trying the official build.

You are right. I will update the comments. Thanks for your reminding.:)

Comment 7 yafu 2017-06-16 04:39:57 UTC
Reproduced with libvirt-3.2.0-4.el7.x86_64, test steps are as comment 0.

Verified pass with libvirt-3.2.0-10.el7.x86_64.
Scenario1:Test the host file will not remove by libvirt after attached failed
1.Start a guest:
#virsh start full-73

2.Prepare a resource not in dev dir:

# touch /tmp/testfile

# cat rng.xml 
  <rng model='virtio'>
    <backend model='random'>/tmp/testfile</backend>
 </rng>

4.Attach the rng device to the guest:
# virsh attach-device full-73 rng.xml 
error: Failed to attach device from rng.xml
error: internal error: unable to execute QEMU command 'object-add': Could not open '/tmp/testfile': Permission denied

5.The file still exists after attach failed:
# ll /tmp/testfile 
-rw-r--r--. 1 root root 0 Jun 15 12:50 /tmp/testfile

6.Also test attach chardev,nvdimm device failed, the source file will not delete  
 by libvirt;

Scenario 2:Test the file will not delete by libvirt after detach:
1.Start a guest with 2 disks:
#virsh domblklist full-73
Target     Source
------------------------------------------------
vda        /nfs-images/yafu/rhel7.2.qcow2
vdd        /var/lib/libvirt/images/generic.qcow2


2.Detach vdd from guest:
#cat disk.xml
 <disk type='file' device='disk'>
      <driver name='qemu' type='qcow2'/>
      <source file='/var/lib/libvirt/images/generic.qcow2'/>
      <target dev='vdd' bus='virtio'/>
    </disk>

#virsh detach-device full-73 disk.xml
Device detached successfully

3.Check the source file of vdd:
# ll /var/lib/libvirt/images/generic.qcow2 
-rw-------. 1 root root 21478375424 Jun  9 11:31 /var/lib/libvirt/images/generic.qcow2

Scenario 3:Attach/detach device with source file created in the preserved mount dir
1.Install vhostmd package and start the daemon:
#yum -y install vhostmd
#systemctl start vhostmd

2.Attach a vhostmd disk(Source file is created by vhostmd daemon in the /dev/shm/vhostmd0)
#cat vhostmd.disk
<disk type='file' device='disk'>
      <driver name='qemu' type='raw'/>
      <source file='/dev/shm/vhostmd0'/>
      <target dev='vdb' bus='virtio'/>
    </disk>
#virsh attach-device full-73 vhostmd.disk
Device attached successfully

3.Check the vhostmd is attached successfully:
#virsh domblklist full-73
Target     Source
------------------------------------------------
vda        /nfs-images/yafu/rhel7.2.qcow2
vdb        /dev/shm/vhostmd0

4.Detach the vhostmd disk:
# virsh detach-device full-73 disk-vhostmd.xml 
Device detached successfully

5.Check the vhostmd disk is detached successfully:
[root@localhost xml]# virsh domblklist full-73
Target     Source
------------------------------------------------
vda        /nfs-images/yafu/rhel7.2.qcow2

Comment 8 yafu 2017-06-22 11:35:03 UTC
According to comment 7, move this bug to verified.

Comment 9 errata-xmlrpc 2017-08-02 00:08:25 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 10 errata-xmlrpc 2017-08-02 01:32:35 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