How to Attach/Detach CDROM Devices to KVM/QEMU Virtual Machines Using virsh Command Line


1 views

When working with KVM/QEMU virtual machines, there are several ways to manage CDROM devices. While virt-manager provides a graphical interface, command-line operations offer more flexibility for automation and scripting purposes.

First, let's examine the current configuration of our VM named "testbed":

virsh dumpxml testbed | grep -A 5 -i cdrom

This command will show any existing CDROM devices configured for the domain.

To attach a physical CDROM from your host to the VM, use the following commands:

virsh attach-disk testbed /dev/cdrom hdc --type cdrom --mode readonly

Alternatively, to attach an ISO image:

virsh attach-disk testbed /path/to/image.iso hdc --type cdrom --mode readonly

To safely detach the CDROM:

virsh detach-disk testbed hdc --type cdrom

By default, these changes are transient (only apply to the running VM). For persistent changes that survive VM reboots:

virsh attach-disk testbed /dev/cdrom hdc --type cdrom --mode readonly --config
virsh detach-disk testbed hdc --type cdrom --config

For more complex scenarios, create an XML file (cdrom.xml) with device details:

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

Then attach it with:

virsh attach-device testbed cdrom.xml
virsh detach-device testbed cdrom.xml

For scripting purposes, here's a bash function to toggle CDROM attachment:

function toggle_cdrom() {
  local vm=$1
  local state=$(virsh domstate $vm)
  
  if [[ $state == "running" ]]; then
    if virsh domblklist $vm | grep -q hdc; then
      virsh detach-disk $vm hdc --type cdrom
    else
      virsh attach-disk $vm /dev/cdrom hdc --type cdrom --mode readonly
    fi
  else
    echo "VM $vm is not running"
  fi
}

If you encounter "Device already exists" errors, try specifying a different target device (like hdd instead of hdc). For permission issues, ensure your user has proper access to /dev/cdrom.


When working with KVM/QEMU virtual machines, the virsh command-line tool provides powerful control over device management. For CD-ROM operations, we primarily use the attach-device and detach-device subcommands.

Before making changes, inspect your domain's current configuration:

virsh dumpxml testbed | grep -A10 "CD-ROM"
# or for full XML output:
virsh dumpxml testbed > vm_config.xml

First, identify your host's CD-ROM device path:

ls /dev/cdrom*
# Common paths: /dev/cdrom, /dev/sr0

Create an XML file (e.g., cdrom.xml) with device configuration:

<disk type='block' device='cdrom'>
  <driver name='qemu' type='raw'/>
  <source dev='/dev/sr0'/>
  <target dev='hdc' bus='ide'/>
  <readonly/>
  <address type='drive' controller='0' bus='1' target='0' unit='0'/>
</disk>

Attach the device:

virsh attach-device testbed cdrom.xml --persistent

The --persistent flag makes the change survive VM reboots.

To use an ISO file instead of physical media:

<disk type='file' device='cdrom'>
  <driver name='qemu' type='raw'/>
  <source file='/path/to/your.iso'/>
  <target dev='hdc' bus='ide'/>
  <readonly/>
</disk>

To remove the device, use:

virsh detach-device testbed cdrom.xml --persistent

For ISO files, you can change media without full detach/attach:

virsh change-media testbed hdc /path/to/new.iso

To eject current media:

virsh change-media testbed hdc --eject

Remember the distinction:

  • Omit --persistent for temporary changes (lost after VM restart)
  • Include --persistent to modify the VM's permanent configuration

If the CD-ROM isn't recognized in Windows XP:

  • Ensure the IDE controller is enabled in the VM
  • Check Device Manager for yellow exclamation marks
  • Try different target devices (hdc, hdd)
  • Verify Windows has appropriate IDE drivers