Correctly automatically start (autostart) the VM with network access after host reboot

Hello
How to properly autostart the VM after restarting the host so that the VM is in the ‘RUNNING’ state, the network in the VM is working, it is possible to connect via VNC, SSH?

My version of OpenNebula: OpenNebula 6.6.0
VM version: KVM, Ubuntu 22.04

I tried the method from the site - Using Hooks — OpenNebula 6.6.3 documentation - “A Complete Example: Autostart Hooks for KVM”

I created 2 templates:

$ onehook create autostart-host.tmpl
$ onehook create autostart-vm.tmpl

But it didn’t work. When I restart the host and the VM is in RUNNING state. After host reboot, VM status is POWEROFF and sometimes RUNNING.

Where should I put AUTOSTART=yes ? In http://127.0.0.1:9869/#hosts-tab/1 Infrastructure->Hosts->Info and here add the ‘AUTOSTART’ attribute with the value ‘yes’?
Or maybe as described at AUTOSTART = YES to restart all the VMs automaticallly · Issue #2787 · OpenNebula/one · GitHub ?
“AUTOSTART = YES” add in /var/lib/one/remotes/vmm/kvm/deploy ?

I also have a problem with the network connection (VNC, ssh) on the VM after restarting the host.

  1. If after host reboot VM is in POWEROFF state then if I click play button, network is working I can connect to VM via VNC (“CLIENT_ERROR”) and via ssh.
  2. If the VM is in the RUNNING state after the host is restarted, the network is down, I cannot connect to the VM (VNC, SSH).
    Then to make a network connection to the VM I have to do:
  • VM in “Power off” state
  • terminal commands:
    sudo systemctl restart libvirtd
    virsh net-list --all // (check, default must be inactive)
    virsh net-start default
  • click “Play”(Run?) VM in Sunstone
    The connection to the VM works.

Hi @oifpi,

The AUTOSTART attribute has to be added to the Host properties.

  • If AUTOSTART=yes the VM will be automatically restarted if it was running before the host was rebooted.
  • If AUTOSTART=always the VM will be automatically restarted always regardless its previous state.

Concerning the VNC issue, perhaps you can debug the problem in the way indicated in the documentation:

Note that any command can be specified in COMMAND, for debugging (COMMAND="/usr/bin/echo") can be very helpful.

Thank you for your answer.

I have a mistake with my first post (can’t edit post):

If after host reboot VM is in POWEROFF state then if I click play button, network is working I can connect to VM via VNC (“CLIENT_ERROR”) and via ssh.

here VNC works. Should not be: VNC (“CLIENT_ERROR”) . VNC works in point 1.
VNC (“CLIENT_ERROR”) is in point 2 when after host restart the VM is in RUNNING state.

Ok, I have this configuration:
In host settings:
AUTOSTART=yes

2 hooks:

  • 1 autostart-vm
  • 0 autostart-host

I have such a problem - after starting the host (Ubuntu) the state of the VM is - RUNNING, after some time (maybe over a minute) the state of the VM changes to POWEROFF.

I tested many times - host reboots (Ubuntu 22.04) and checking VM state.

// Example:

The VM is in the RUNNING state.

In the terminal:
sudo reboot

Immediately after restarting Ubuntu: the time is 19:03.
VM status is RUNNING. Connection to the VM (VNC, SSH) does not work.

After some time I check the log.
VM log:

Mon Jul 31 18:55:23 2023 [Z0][VM][I]: New LCM state is RUNNING
Mon Jul 31 19:05:08 2023 [Z0][LCM][I]: VM running but monitor state is POWEROFF
Mon Jul 31 19:05:08 2023 [Z0][VM][I]: New LCM state is SHUTDOWN_POWEROFF
Mon Jul 31 19:05:08 2023 [Z0][VM][I]: New state is POWEROFF
Mon Jul 31 19:05:08 2023 [Z0][VM][I]: New LCM state is LCM_INIT

VM is in POWEROFF state.
I’m doing the VM in Resume state. The VM is in the RUNNING state. Connection to VM (VNC, SSH) now works.
// End example

What to do so that the VM does not go into POWEROFF state and the network in the VM works (VNC, SSH connection) ?

Thanks for your detailed example. At first I don’t see any error on the OpenNebula side, as the hooks work correctly and in fact, the virtual machine restarts correctly as well.

This looks more like an error on the libvirt side. What version of libvirt are you using, can you see any error in the libvirt logs?

Thank you for your answer.

The version of libvirt I’m using:

$ virsh --version
8.0.0
$ libvirtd --version
libvirtd (libvirt) 8.0.0
$ virsh --version
8.0.0

Example of a test with VM and libvirt logs:

//Begin - example

18:40 - Pressing the Resume button in the VM

18:41 - checking the VM log:

Mon Sep 4 16:50:21 2023 [Z0][VM][I]: New LCM state is LCM_INIT
Mon Sep 4 18:41:04 2023 [Z0][VM][I]: New state is ACTIVE
Mon Sep 4 18:41:04 2023 [Z0][VM][I]: New LCM state is BOOT_POWEROFF
Mon Sep 4 18:41:04 2023 [Z0][VMM][I]: Generating deployment file: /var/lib/one/vms/3/deployment.83
Mon Sep 4 18:41:05 2023 [Z0][VMM][I]: Successfully execute transfer manager driver operation: tm_context.
Mon Sep 4 18:41:05 2023 [Z0][VMM][I]: ExitCode: 0
Mon Sep 4 18:41:05 2023 [Z0][VMM][I]: Successfully execute network driver operation: pre.
Mon Sep 4 18:41:05 2023 [Z0][VMM][I]: ExitCode: 0
Mon Sep 4 18:41:05 2023 [Z0][VMM][I]: Successfully execute virtualization driver operation: /bin/mkdir -p.
Mon Sep 4 18:41:05 2023 [Z0][VMM][I]: ExitCode: 0
Mon Sep 4 18:41:05 2023 [Z0][VMM][I]: Successfully execute virtualization driver operation: /bin/cat - >/var/lib/one//datastores/0/3/vm.xml.
Mon Sep 4 18:41:05 2023 [Z0][VMM][I]: ExitCode: 0
Mon Sep 4 18:41:06 2023 [Z0][VMM][I]: Successfully execute virtualization driver operation: /bin/cat - >/var/lib/one//datastores/0/3/ds.xml.
Mon Sep 4 18:41:07 2023 [Z0][LCM][I]: VM reported RUNNING by the drivers
Mon Sep 4 18:41:07 2023 [Z0][VM][I]: New LCM state is RUNNING
Mon Sep 4 18:41:08 2023 [Z0][VMM][I]: ExitCode: 0
Mon Sep 4 18:41:08 2023 [Z0][VMM][I]: Successfully execute virtualization driver operation: deploy.
Mon Sep 4 18:41:08 2023 [Z0][VMM][I]: ExitCode: 0
Mon Sep 4 18:41:08 2023 [Z0][VMM][I]: Successfully execute network driver operation: post.

18:43 - check log /var/log/libvirt/qemu/ :

last line:
2023-09-04T16:41:07.366117Z qemu-kvm-one: warning: host doesn't support requested feature: CPUID.80000001H:ECX.svm [bit 2]

18:47 - VM status - “Running”. Network in the VM is working, i can ping and log in to the VM.

18:47 - PC reboot:
sudo reboot

18:52 - login to the system
VM check :

  • VM status - “Running”. Network in VM does not work. Can’t login to the VM.

20:01 - checking the VM log:

Mon Sep 4 18:41:08 2023 [Z0][VMM][I]: Successfully execute network driver operation: post.
Mon Sep 4 18:53:29 2023 [Z0][LCM][I]: VM running but monitor state is POWEROFF
Mon Sep 4 18:53:29 2023 [Z0][VM][I]: New LCM state is SHUTDOWN_POWEROFF
Mon Sep 4 18:53:29 2023 [Z0][VM][I]: New state is POWEROFF
Mon Sep 4 18:53:29 2023 [Z0][VM][I]: New LCM state is LCM_INIT

20:02 - check log /var/log/libvirt/qemu/ :

2023-09-04T16:41:07.366117Z qemu-kvm-one: warning: host doesn't support requested feature: CPUID.80000001H:ECX.svm [bit 2]
2023-09-04T16:48:01.817773Z qemu-kvm-one: terminating on signal 15 from pid 798 (/usr/sbin/libvirtd)
2023-09-04 16:48:02.134+0000: shutting down, reason=shutdown

20:07 - check the libvirt log:

$ grep libvirt /var/log/syslog

Sep  4 16:46:43 fil-Lenovo dnsmasq-dhcp[989]: read /var/lib/libvirt/dnsmasq/default.hostsfile
Sep  4 18:41:06 fil-Lenovo kernel: [ 6874.419215] audit: type=1400 audit(1693845666.308:73): apparmor="STATUS" operation="profile_load" profile="unconfined" name="libvirt-3dae2b1d-3095-4323-b396-32205b54614f" pid=41958 comm="apparmor_parser"
Sep  4 18:41:06 fil-Lenovo kernel: [ 6874.604447] audit: type=1400 audit(1693845666.492:74): apparmor="STATUS" operation="profile_replace" profile="unconfined" name="libvirt-3dae2b1d-3095-4323-b396-32205b54614f" pid=41961 comm="apparmor_parser"
Sep  4 18:41:06 fil-Lenovo kernel: [ 6874.785249] audit: type=1400 audit(1693845666.672:75): apparmor="STATUS" operation="profile_replace" profile="unconfined" name="libvirt-3dae2b1d-3095-4323-b396-32205b54614f" pid=41965 comm="apparmor_parser"
Sep  4 18:41:06 fil-Lenovo kernel: [ 6874.969536] audit: type=1400 audit(1693845666.856:76): apparmor="STATUS" operation="profile_replace" info="same as current profile, skipping" profile="unconfined" name="libvirt-3dae2b1d-3095-4323-b396-32205b54614f" pid=41970 comm="apparmor_parser"
Sep  4 18:41:07 fil-Lenovo kernel: [ 6875.204521] audit: type=1400 audit(1693845667.092:77): apparmor="STATUS" operation="profile_replace" profile="unconfined" name="libvirt-3dae2b1d-3095-4323-b396-32205b54614f" pid=41983 comm="apparmor_parser"
Sep  4 18:49:51 fil-Lenovo systemd[1]: Starting Suspend/Resume Running libvirt Guests...
Sep  4 18:49:51 fil-Lenovo libvirt-guests.sh[875]: libvirt-guests is configured not to start any guests on boot
Sep  4 18:49:51 fil-Lenovo systemd[1]: Finished Suspend/Resume Running libvirt Guests.
Sep  4 18:49:51 fil-Lenovo dnsmasq[986]: read /var/lib/libvirt/dnsmasq/default.addnhosts - 0 addresses
Sep  4 18:49:51 fil-Lenovo dnsmasq-dhcp[986]: read /var/lib/libvirt/dnsmasq/default.hostsfile

VM status is “POWEROFF”

//End - example