28

I have set up a virtual machine running Windows XP on my Ubuntu laptop. Using the virt-manager GUI application, I can insert a CD in my drive and go to Details→IDE CDROM 1 and click on the Connect button. Then the CD becomes available in my virtual machine.

How can I do the same through the command line? Obviously, I'd like to be able to disconnect from the command line too.

Note: I can start the VM from the command line using virsh start testbed (testbed being the name of the domain/VM).

ΤΖΩΤΖΙΟΥ
  • 1,038
  • 1
  • 10
  • 18

5 Answers5

28

If you defined no CDROM when you created your virtual machine, you can attach the device even to a running domain (virtual machine) by running the following command:

virsh attach-disk testbed /dev/sr0 hdc --type cdrom

If you already defined a CDROM, but it pointed to an ISO image, in my experience, you can still run the same command. The hdcpart needs to match the block device you have in the testbed virtual machine.

When you want to point to an ISO image again, you replace /dev/sr0 to the filename on the host, something like

virsh attach-disk testbed ~/virtio-win-0.1-22.iso hdc --type cdrom

The documentation suggests using virsh update-device, but it is more labour to create an XML definition something like:

<disk type='block' device='cdrom'>
  <driver name='qemu' type='raw'/>
  <source dev='/dev/sr0'/>
  <target dev='hdc' bus='ide'/>
  <readonly/>
</disk>

If you are into this way, save something like that into a file (say ~/cdrom-real.xml) and then fire:

virsh update-device testbed ~/cdrom-real.xml
wigy
  • 396
  • 3
  • 6
  • This looks like it's what I want; I will verify as soon as possible (can't at the moment). Thank you in any case. – ΤΖΩΤΖΙΟΥ Mar 30 '12 at 16:54
  • Yes, this is it. Do you also know how to detach the device? I tried `virsh detach-disk testbed hdc`, but I got `error: unsupported configuration: This type of device cannot be hot unplugged`. – ΤΖΩΤΖΙΟΥ Mar 31 '12 at 23:59
  • IDE devices are not hotpluggable indeed. `detach-disk` does not work for a stopped virtual machine. To remove the CD altogether, you need to use `virsh edit testbed` while the machine is stopped and delete the `disk` element defining to the cdrom. If you are not a `vi` editor expert, execute `export EDITOR=nano` before executing the edit command. – wigy Apr 03 '12 at 08:16
  • By the way, if you feel that my answer helped you, please accept it as an answer. – wigy Apr 03 '12 at 08:17
  • I will accept your answer. The thing is, if the device is connected to the testbed VM and I start testbed without a disc inserted, testbed won't come up, and I need to open the virtual manager GUI and disconnect the device first. That is why in my question I stated I wanted a way to disconnect the device too. – ΤΖΩΤΖΙΟΥ Apr 04 '12 at 07:02
  • I got 「error: Requested operation is not valid: cannot do live update a device on inactive domain」 with your command, but if I “virsh edit” and insert your XML snippet, it works like a charm. Embarrassing that virt-manager can’t do this. Thanks! – mirabilos Nov 12 '14 at 14:19
  • Both suggestions not working on host debian 11 "bullseye": 1.) 'virsh attach-disk jibz_vm01 ../installimages/debian-live-11.1.0-amd64-standard.iso hdc --type cdrom' --- error: Operation not supported: cdrom/floppy device hotplug isn't supported 2.) 'virsh update-device jibz_vm01 add_cdrom.xml' --- error: internal error: disk 'hdc' not found – Jochen Gebsattel Oct 23 '21 at 14:57
6

Use virsh's qemu-monitor-command to pass the eject and change commands down to qemu.

First use "info block" to get qemu's device name for your cdrom.

virsh # qemu-monitor-command mirage --hmp --cmd "info block"
drive-virtio-disk0: type=hd removable=0 file=/home/daoist/mirage/mirage.qcow2 ro=0 drv=raw encrypted=0
drive-ide0-0-0: type=cdrom removable=1 locked=0 file=/home/daoist/iso/en_windows_7_ultimate_with_sp1_x64_dvd_u_677332.iso ro=1 drv=raw encrypted=0

So in my case I'm interested in drive-ide0-0-0. So to change the disk, do this:

virsh # qemu-monitor-command mirage --hmp --cmd "eject drive-ide0-0-0"
virsh # qemu-monitor-command mirage --hmp --cmd "change drive-ide0-0-0 /path/to/new.iso"
virsh #
Keith Twombley
  • 235
  • 2
  • 11
  • The "info block" command results in an error for me: "error: internal error cannot parse json info block: lexical error: invalid char in json text." – cg. Nov 18 '12 at 10:36
  • Seems the syntax must have changed out from under me. I swear it worked when I wrote it. I tried out the syntax suggested by Joe Huss's answer and it works now. I've updated my answer. – Keith Twombley Jul 01 '15 at 20:08
2

Regarding the "info block" command above, the correct way to call it is like this:

 virsh qemu-monitor-command <domain> --hmp --cmd "info block"

ie

 virsh qemu-monitor-command windows44407 --hmp --cmd "info block"
Joe Huss
  • 71
  • 3
0

I was passing through a BluRay player that didn't work correctly unless I passed it through another way. Here is how I did it based on this article.

https://lime-technology.com/forums/topic/33851-blu-ray-dvd-rom-passthrough/

Find SCSI #:

lsscsi

[root@kvmatrix ~]# lsscsi
[#:0:0:0]    cd/dvd  HL-DT-ST BD-RE  WH14NS40  1.03  /dev/sr0

Edit Domain XML and add this:

Note: Replace scsi_host# with the # found in lssci

<controller type='scsi' index='0' model='virtio-scsi'/>
    <hostdev mode='subsystem' type='scsi'>
      <source>
        <adapter name='scsi_host#'/>
        <address type='scsi' bus='0' target='0' unit='0'/>
      </source>
      <readonly/>
      <address type='drive' controller='0' bus='0' target='0' unit='0'/>
    </hostdev>

I had to force the XML to be accepted as virsh edit gave errors, just press i and then reboot vm to test.

FreeSoftwareServers
  • 571
  • 1
  • 6
  • 25
-6

mount -t iso9660 /dev/sr0 /media

You can use whatever device is set for your cdrom in /dev directory. Also /dev/cdrom is a link to the proper device on most Linux systems. The -t is for type of device you are mounting and iso9660 is the universal type for cdrom media.

The mount point can be whatever you want obviously, just make sure it exists.

Brian
  • 1
  • Um. That's how I can mount a CD in the host. Ok, my mistake. “How to connect a cdrom device to a kvm/qemu domain/VM?” Note that this answer is completely not helpful, so you should either edit it, or delete it. – ΤΖΩΤΖΙΟΥ Mar 28 '12 at 18:57