Tuesday, September 2, 2014

Virtual cdrom eject using libvirt

Today's post will be about ejecting virtual cdrom using libvirt.

When I wanted to install new virtual machine using libvirt in my computer I always had to do these steps:

  1. Define guest domain
  2. Define cdrom with iso image in the domain
  3. Set boot device to cdrom
  4. Install system
  5. Set boot device to hardrive
  6. Boot system from hardrive

It was really annoying to edit the domain by hand just to select boot device. Fortunatelly I've found change-media virsh command, which helped me to overcome this problem. Instead of setting boot device to either cdrom or hd I will leave there both:

[root@kra ~]$ virsh dumpxml puppet
<domain type='kvm'>
  <name>puppet
  <uuid>1a2afcba-37af-496e-beca-e4c59b913f31
  <memory unit='KiB'>1048576
  <currentMemory unit='KiB'>1048576
  <vcpu placement='static'>1
  <os>
    <type arch='x86_64' machine='pc-i440fx-rhel7.0.0'>hvm
    <boot dev='cdrom'/>
    <boot dev='hd'/>
  </os>
.
.
.

Now system will boot from cdrom if there is bootable iso in the virtual cdrom. Otherwise it will boot from disk. I can use domblklist command to see domain block devices:

virsh # domblklist puppet
Target     Source
------------------------------------------------
vda        /home/vajko/images/puppet.img
hdc        /home/vajko/images/CentOS-7.0-1406-x86_64-NetInstall.iso

Cdrom (hdc device) contains netinstall iso media. I can easily eject the medium:

virsh # change-media puppet hdc --eject
succeeded to complete action eject on media

virsh # domblklist puppet
Target     Source
------------------------------------------------
vda        /home/vajko/images/puppet.img
hdc        -

To insert new media is easy too:

virsh # change-media puppet hdc /home/hasul/images/Centos-5-boot.iso --insert
succeeded to complete action insert on media

virsh # domblklist puppet
Target     Source
------------------------------------------------
vda        /home/vajko/images/puppet.img
hdc        /home/vajko/images/Centos-5-boot.iso

This way I can easily control the device used for booting the system.

No comments:

Post a Comment