Here is an example how you can test if a machine is online using ping
:
#!/bin/bash
# test-online.sh
ret=1
while [ $ret != 0 ]
do
ping -c 1 $1 2>/dev/null
ret=$?
done
exit 0
You can adapt this to use ssh
by replacing the ping
line with something like ssh $1 "echo"
, assuming you can connect to the machine without a password.
Using the above script (let's call it test-online.sh
), you can start the machine and then measure the time using GNU time
(the first argument of this script is the machine name):
#!/bin/bash
# start-and-time.sh
start-vm $1
/usr/bin/time -f "%E" ./test-online.sh $1
The output will be something like 1:23.52
which means your machine took 1 minute 23 seconds to boot.
If you want to measure the boot time of many machines, you can just call start-and-time.sh
for every machine:
#!/bin/bash
mymachines=(machine1 machine2 machine3)
for machine in "${machines[@]}"
do
echo -n "$machine "
./start-and-time.sh $machine &
done
which will give the following exemplary output:
machine1 1:53.23
machine2 2:42.42
machine3 0:42.42
So with your question, "What is the right way to save and restore Running VMs?" you pretty much nailed it on the head. Saving with virsh save VM
and restoring with virsh restore VM
will do what you wanted it to do--that is save memory to disk and then stop the VM (see the first section of this link).
When your VM froze, I believe you would have been able to restore if you had not deleted the state file (depending on the root cause of the VM failure in the first place).
I generally do not save memory to state files because my use case runs persistent, stateless services that do not depend on memory. If I need to shut down I usually just run virsh destroy VM
and then start it as necessary. But, this all depends on your own use case.
You may want to also look into backing up your VM with snapshots, if you are leery of losing your machine again. Something like:
virsh snapshot-create-as --domain {VM-NAME} --name "{SNAPSHOT-NAME}"
See NixCraft for more details.
In any case, for getting your VM running after it froze you could have simply undefined the broken VM and re-attached the originally existing image from it to a new VM by updating the newly created VM's XML configuration document. First, tear down the original VM:
:~$ virsh destroy OriginalVM
:~$ virsh undefine OriginalVM
After that build the new VM, stop it and access the config:
:~$ virt-install --virt-type kvm --name NewVM --memory 4096 --disk size=1 --vcpus 4 --location /yadda/yadda.iso --network bridge=br0 --os-type=linux --so-on-and-so-forth
:~$ virsh destroy NewVM
:~$ virsh edit NewVM
Then, when editing the XML file search for "disk" by typing /disk
and update the path for the source file:
<disk type='file' device='disk'>
<driver name='qemu' type='raw'/>
<source file='/update_to/path_of/OriginalVM.img'/>
After saving and exiting (:w
, :q
or simply :x
) you should be able to execute a virsh start NewVM
and then access the new VM running your image via VNC, console, or ssh.
You could also try to clone your VM, though I have not tried this myself:
virt-clone --connect=qemu:///system -o srchost -n newhost -f /path/to/newhost.qcow2
Best Answer
The most probably as UEFI is installed (grub-efi-*) the bios boot loader is not installed. You should install boot loader for bios boot (grub-installer, grub-pc-bin, may be some other) and set up the boot using grub installer and customize the parameters if needed.
Once everything is working (the easiest way is to try to boot locally with bios boot) you can easily remove the line from fstab and remove the partition. Optionally you can also remove grub-efi-* package(s). In case you would remove it directly you will not be able to boot it normal way.
I am not sure if it is still the case but it used to be that the boot loader has been installed based on how did you boot / start the installation process so once you boot into installation using UEFI the UEFI boot is set up and once you boot using bios boot the bios boot is set up on the system. So in the worst case try to install VM again but start installation process using bios boot instead of UEFI ;-).