How to set KVM Kernel boot "only once"?

Rayer asked:

I am trying using automation install via KVM. The step is :

  1. Load Kernel and initrd from disk
  2. It will install customized Linux to HDD, and automatically reboot
  3. Boot from HDD

So I use this command to install my customized linux :

virt-install --name Kernel_Test --ram 16384 --vcpus=8 --os-type=linux --os-variant=rhel6 --vnc --hvm --disk path=/kvm_images//Kernel_Test.qcow2,device=disk,format=qcow2,size=50,bus=sata --boot hd,kernel=/tmp/my_vmlinuz,initrd=/tmp/my_image,kernel_args="append stage2=initrd:" --network bridge=bridge1,model=virtio --network bridge=bridge1,model=virtio --network bridge=bridge0,model=virtio 

However, it will get stuck in a loop, as follows:

  1. Boot from kernel/initrd -> installation completed
  2. Restart
  3. Boot from kernel/initrd -> installation completed
  4. Restart
  5. (loop)

I have tried to use the following command to strip kernel related configuration, without success:

virsh dumpxml <VMNAME> >> vmname.xml
(do something to change vmname.xml, strip kernel/initrd boot part)
virsh define vmname.xml

It will still keep looping, and when I dumpxml again, it still holds kernel/initrd boot configuration.

Because I need to automatic installation, how can I let KVM only boot once from destinated kernel/initrd? I have set boot sequence --boot hd, in front of kernel= ..., but it just skips it and keesp booting from local kernel/initrd.

My answer:

If a direct kernel boot is present, it will always be used, and the regular boot order will be ignored.

The best way to handle this is to not use direct kernel boot for installing the system, but rather pass a --location where the Linux distribution you are installing lives (which can be on a local disk or a remote web server, FTP or NFS server). This also allows you to inject a local kickstart or preseed file for a completely automated installation. For example:

--boot hd --location --initrd-inject=my_vm.ks --extra-args="ks=file:/my_vm.ks"

If you can’t do that for some reason, then pass --noreboot and hack the XML after virt-install exits. You will have to restart the VM manually. Remember that virt-install can exit before the installation completes, so simply restarting the VM won’t work reliably and could interrupt an installation in progress.

View the full question and answer on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.