KVM Guest auto start after install

kvm-virtualizationrhel6

I'm trying to install a rhel guest inside a rhel host using kvm and have the rhel guest auto start after the installation completes.

The kickstart file has the "reboot" command which should tell RHEL to restart after the install.

the script is at /root/install_machine, and the variables are defined at the top of the script

virt-install \
--name=$name-$ip_short \
--arch=x86_64 \
--ram=$memory \
--os-type=linux \
--os-variant=virtio26 \
--hvm \
--connect=qemu:///system \
--network bridge:br0 \
--vcpus=$cpus \
--accelerate \
--autostart \
--disk path=/kvm/disks/$name-$ip_short.img,size=$disk_size \
--location $location \
--vnc \
-x "ks=$ks_file ksdevice=eth0 ip=$ip_long netmask=255.255.255.0 gateway=$gateway dns=8.8.8.8" 

I log into the machine, and run the script

ssh -X root@virtual_server
/root/install_machine

the virt-viewer window pops open, I watch the intall, I watch it reboot, and it runs great.

But if I login without graphics, I get a "cannot open display" error (which is expected), then the system installs, and then it shuts down and I have to manually start it

ssh root@virtual_server
/root/install_machine

Starting install...
Retrieving file .treeinfo...
Retrieving file vmlinuz...
Retrieving file initrd.img...
Creating storage file test2-178.img
Creating domain...
Cannot open display:
Run 'virt-viewer --help' to see a full list of available command line options
Domain installation still in progress. You can reconnect to 
the console to complete the installation process.

I also tried running the script from a cron, the machine is installed but is left in the turned off state and I have to manually turn it on.

Any suggestions for what I might try to get this to install and start without my input? I suppose I could monitor the process and run "virsh start $name-$ip_short", but that seems hacky. It seems like it should restart on its own. Adding –noautoconsole to virt-install doesn't seem to help either…

Best Answer

probably not the cleanest solution, but this works (with the definitions defined at the top of the script)

virsh destroy $name-$ip_short
virsh undefine $name-$ip_short
rm -fr /kvm/disks/$name-$ip_short.img

virt-install \
--name=$name-$ip_short \
--arch=x86_64 \
--ram=$memory \
--os-type=linux \
--os-variant=virtio26 \
--hvm \
--connect=qemu:///system \
--network bridge:br0 \
--vcpus=$cpus \
--accelerate \
--autostart \
--disk path=$disk_directory/$name-$ip_short.img,size=$disk_size \
--location http://$domain/$location_path \
--vnc \
--noautoconsole \
-x "ks=http://$domain/$ks_path ksdevice=eth0 ip=$ip_long netmask=255.255.255.0 gateway=$gateway dns=$dns"


finished="0";

while [ "$finished" = "0" ]; do
        sleep 5;
        if [ `virsh list --all | grep "running" | grep "$name-$ip_short" | wc -c` -eq 0 ];
        then
                #echo "setup finished, start vm $name-$ip_short"
                finished=1;
                virsh start $name-$ip_short
        fi
done