Bug 713932

Summary: RFE: implement insert-media and eject-media virsh commands
Product: Red Hat Enterprise Linux 6 Reporter: Dave Allan <dallan>
Component: libvirtAssignee: Osier Yang <jyang>
Status: CLOSED ERRATA QA Contact: Virtualization Bugs <virt-bugs>
Severity: high Docs Contact:
Priority: high    
Version: 6.2CC: dallan, dyuan, esammons, gsun, lagarcia, mzhan, rwu, whuang, yupzhang
Target Milestone: rcKeywords: FutureFeature
Target Release: ---   
Hardware: Unspecified   
OS: Unspecified   
Whiteboard:
Fixed In Version: libvirt-0.9.10-4.el6 Doc Type: Enhancement
Doc Text:
Cause: There are virsh commands which could be used to insert and eject media of CDROM or Floppy devices, but the user experience is not good. Consequense: Many users (both outside and internal) ask how to insert and eject media of CDROM and Floppy devices frequently. Fix: Introduce new virsh command 'change-media' to allow the user insert and eject media of CDROM and Floppy devices easily. Result: Users will be happy.
Story Points: ---
Clone Of: Environment:
Last Closed: 2012-06-20 06:28:46 UTC Type: ---
Regression: --- Mount Type: ---
Documentation: --- CRM:
Verified Versions: Category: ---
oVirt Team: --- RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: --- Target Upstream Version:
Embargoed:
Bug Depends On:    
Bug Blocks: 747667, 756082    

Description Dave Allan 2011-06-16 19:50:24 UTC
virsh should have commands to simplify inserting and ejecting media from block devices.  The typical case is inserting and ejecting CDROMs, but if possible the command shouldn't rule out the use of other media.  

Note that it is currently possible to insert and eject CDROMs with commands similar to the following, so this RFE simply requests a better user experience.

To insert a CDROM, create an XML file, in this case disk.xml, containing the following:

<disk type='file' device='cdrom'>
      <source file='/path/to/file.iso'/>
      <target dev='hda' bus='ide'/>
</disk>

and execute:

virsh update-device VMname disk.xml

To eject a CDROM, create an XML file, in this case empty.xml, containing the following:

<disk type='file' device='cdrom'>
  <target dev='hda' bus='ide'/>
  <readonly/>
</disk>

and execute:

virsh update-device VMname empty.xml

Comment 7 Osier Yang 2012-01-11 02:48:36 UTC
v4 posted to upstream:

https://www.redhat.com/archives/libvir-list/2012-January/msg00334.html

Comment 8 Osier Yang 2012-02-29 04:01:09 UTC
commits are in upstream.

commit 33855f5d0464d112af539cbd0996e448e9f81c34
Author: Osier Yang <jyang>
Date:   Tue Feb 28 15:36:38 2012 +0800

    virsh: New command cmdChangeMedia
    
    One could use it to eject, insert, or update media of the CDROM
    or floppy drive. See the documentation for more details.

commit 42accf1b671f24b2dcc78089a6e65a6f610f0f57
Author: Osier Yang <jyang>
Date:   Tue Feb 14 17:16:53 2012 +0800

    virsh: Use vshFindDisk and vshPrepareDiskXML in cmdDetachDisk
    
    The first use of the two new helper functions.

commit 025998eb79872c4c565d3b5499127863b2753998
Author: Osier Yang <jyang>
Date:   Tue Feb 28 14:38:03 2012 +0800

    virsh: Two new helper functions for disk device changes
    
    vshFindDisk is to find the disk node in xml doc with given source
    path or target of disk device, and type (indicates disk type,
    normal disk or changeable disk).
    
    vshPrepareDiskXML is to make changes on the disk node (e.g. create
    and insert the new <source> node for inserting media of CDROM drive).
    
    They are marked as unused temporarily.

Comment 9 Osier Yang 2012-03-01 13:51:36 UTC
http://post-office.corp.redhat.com/archives/rhvirt-patches/2012-March/msg00059.html

Patches posted internally.

Comment 12 Wayne Sun 2012-03-08 06:42:37 UTC
# rpm -q libvirt qemu-kvm kernel
libvirt-0.9.10-4.el6.x86_64
qemu-kvm-0.12.1.2-2.225.el6.x86_64
kernel-2.6.32-220.el6.x86_64

steps:
1. prepare a running domain with cdrom 
# virsh list
 Id    Name                           State
----------------------------------------------------
 3     rhel6u2                        running

# virsh dumpxml rhel6u2
...
    <disk type='file' device='cdrom'>
      <driver name='qemu' type='raw'/>
      <source file='/tmp/temp2.iso'/>
      <target dev='hdc' bus='ide'/>
      <readonly/>
      <address type='drive' controller='0' bus='1' target='0' unit='0'/>
    </disk>
...

2. prepare two iso file:
# mkisofs -o /var/lib/libvirt/images/aaa.iso /tmp
# mkdir /tmp/changetest
# vim /tmp/changetest/dsa
# mkisofs -o /var/lib/libvirt/images/bbb.iso /tmp

3. eject media
# virsh change-media rhel6u2 hdc --eject 
succeeded to complete action eject on media

4. insert media
# virsh change-media rhel6u2 hdc /var/lib/libvirt/images/aaa.iso --insert
succeeded to complete action insert on media

log in guest with x windows started, cdrom mounted and opened with nautilus
5. change media
# virsh change-media rhel6u2 hdc /var/lib/libvirt/images/bbb.iso --update
succeeded to complete action update on media

open cdrom in guest, content changed

6. eject media
# virsh change-media rhel6u2 hdc --eject
error: Failed to complete action eject on media
error: internal error unable to execute QEMU command 'eject': Device 'drive-ide0-1-0' is locked

in guest, umount cdrom

# virsh change-media rhel6u2 hdc --eject
succeeded to complete action eject on media

7. insert media with update
# virsh change-media rhel6u2 hdc /var/lib/libvirt/images/aaa.iso --update
succeeded to complete action update on media

in guest cdrom opened

8. force eject
# virsh change-media rhel6u2 hdc --eject --force
succeeded to complete action eject on media

in guest cdrom ejected

8. update media with --config
# virsh change-media rhel6u2 hdc /var/lib/libvirt/images/aaa.iso --update --config
succeeded to complete action update on media

destroy domain and check xml
# virsh destroy rhel6u2
Domain rhel6u2 destroyed
# virsh dumpxml rhel6u2
...
    <disk type='file' device='cdrom'>
      <driver name='qemu' type='raw'/>
      <source file='/var/lib/libvirt/images/aaa.iso'/>
      <target dev='hdc' bus='ide'/>
      <readonly/>
      <address type='drive' controller='0' bus='1' target='0' unit='0'/>
    </disk>
...

9 update media with --current
# virsh change-media rhel6u2 hdc /var/lib/libvirt/images/bbb.iso --update --current
succeeded to complete action update on media

# virsh dumpxml rhel6u2
...
    <disk type='file' device='cdrom'>
      <driver name='qemu' type='raw'/>
      <source file='/var/lib/libvirt/images/bbb.iso'/>
      <target dev='hdc' bus='ide'/>
      <readonly/>
      <address type='drive' controller='0' bus='1' target='0' unit='0'/>
    </disk>
...

So, this is working.

Comment 13 Osier Yang 2012-05-04 09:58:06 UTC
    Technical note added. If any revisions are required, please edit the "Technical Notes" field
    accordingly. All revisions will be proofread by the Engineering Content Services team.
    
    New Contents:
Cause: There are virsh commands which could be used to insert and eject media of CDROM or Floppy devices, but the user experience is not good.
Consequense: Many users (both outside and internal) ask how to insert and eject media of CDROM and Floppy devices frequently.
Fix: Introduce new virsh command 'change-media' to allow the user insert and eject media of CDROM and Floppy devices easily.
Result: Users will be happy.

Comment 15 errata-xmlrpc 2012-06-20 06:28:46 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.

http://rhn.redhat.com/errata/RHSA-2012-0748.html