Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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

share|improve this question

3 Answers 3

up vote 13 down vote accepted

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
share|improve this answer
    
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 3 '12 at 8:16
    
By the way, if you feel that my answer helped you, please accept it as an answer. –  wigy Apr 3 '12 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 '12 at 7:02

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 "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 "eject drive-ide0-0-0"
virsh # qemu-monitor-command mirage "change drive-ide0-0-0 /path/to/new.iso"
virsh #

shamelessly cribbed from my blog.

share|improve this answer
    
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

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.

share|improve this answer
    
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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.