# Attach/Detach CDROM not supported

**URL:** <https://forum.opennebula.io/t/attach-detach-cdrom-not-supported/3723>\
**Category:** Integration Support\
**Created:** [February 6, 2017, 2:09pm UTC](https://forum.opennebula.io/t/attach-detach-cdrom-not-supported/3723 "2017-02-06T14:09:05Z")\
**Posts on this page:** 1\
**Showing post:** 7

<div class="post-metadata">

**Author:** ![kvaps](https://yyz1.discourse-cdn.com/flex031/user_avatar/forum.opennebula.io/kvaps/32/4496_2.png) [@kvaps](https://forum.opennebula.io/u/kvaps)\
**Post date:** [November 22, 2017, 4:33pm UTC](https://forum.opennebula.io/t/attach-detach-cdrom-not-supported/3723/7 "2017-11-22T16:33:45Z")

</div>

Hi @atodorov_storpool, this is my dirty solution for that:

```diff
--- a/detach_disk
+++ b/detach_disk
@@ -26,14 +26,18 @@ TARGET_INDEX="$4"
 
 DETACH_PARAMS="--domain $DOMAIN --target $TARGET"
 
-exec_and_log "virsh --connect $LIBVIRT_URI detach-disk $DETACH_PARAMS" \
- "Could not detach $TARGET from $DOMAIN"
-
-virsh --connect $LIBVIRT_URI dumpxml $DOMAIN | grep $TARGET > /dev/null 2>&1
-
-if [$? -eq 0] ; then
- error_message "Could not detach $TARGET from $DOMAIN"
- exit -1
-fi
-
-exit 0
+DETACH_CMD=$(cat <<EOF
+
+CDROM_TARGET="\$(virsh --connect $LIBVIRT_URI dumpxml --domain $DOMAIN | \
+ xmllint --xpath "string(/domain/devices/disk[target/@dev='${TARGET}'][@device='cdrom']/source/@file)" -)"
+
+if [-z "\$CDROM_TARGET"]
+then virsh --connect $LIBVIRT_URI detach-disk $DETACH_PARAMS
+else virsh --connect $LIBVIRT_URI change-media --domain $DOMAIN --eject "\$CDROM_TARGET" --force
+fi
+
+EOF
+)
+
+multiline_exec_and_log "$DETACH_CMD" \
+ "Could not detach $TARGET from $DOMAIN"

```

---

_[View the full topic](https://forum.opennebula.io/t/attach-detach-cdrom-not-supported/3723)._
