29

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).

5 Answers 5

30

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
8
  • 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, 2012 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, 2012 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 3, 2012 at 8:16
  • By the way, if you feel that my answer helped you, please accept it as an answer.
    – wigy
    Apr 3, 2012 at 8: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 4, 2012 at 7:02
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 #
2
  • 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, 2012 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. Jul 1, 2015 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"
0
1

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.

-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.

1
  • 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, 2012 at 18:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .