I am currently writing a small program in go, which I would like to use later to start virtual machines. The VM's are all based on centos7.0. A kickstartfile is used to install the VM's according to the same scheme.
To use the function with the kickstartfile I have to activate the direct kernel boot. I pass the path to the kernel (vmlinuz
), initrd (initrd.img
) and the argements (ks=http://172.25.35.165/kvm-centos.ks
).
After the installation the VM starts again via the kickstartfile, which leads to a permanent installation routine.
I'm now looking for an EventHandler that notifies my go program that the VM is being restarted. Then I could disable the kernel boot settings with the kickstartfile argument.
I have already tried with DomainEventRegister, but the event is not triggered. The libvirt-go package is also very poorly documented when it comes to event handling.
Does anyone know how I can set a CallbackFunction for reboot actions to disable the kernel boot settings after successfully installing the VM?
Here my go function:
// CreateDomain
func CreateDomain(domainCfg *libvirtxml.Domain, conn *libvirt.Connect) error {
domainXML, err := domainCfg.Marshal()
if err != nil {
return err
}
domain, err := conn.DomainDefineXML(domainXML)
if err != nil {
return err
}
conn.DomainEventRegister(nil, func(c *libvirt.Connect, d *libvirt.Domain, event *libvirt.Event) {
log.Println("Reboot")
})
err = domain.CreateWithFlags(libvirt.DOMAIN_NONE)
if err != nil {
return err
}
domainState, _, err := domain.GetState()
for domainState == libvirt.DOMAIN_RUNNING {
state, _, err := domain.GetState()
if err != nil {
return err
}
time.Sleep(time.Second * 10)
log.Println(state)
if state != libvirt.DOMAIN_RUNNING {
break
}
}
return nil
}