18 | | To test out process manually: |
| 28 | == When things go wrong == |
| 29 | |
| 30 | === How it works === |
| 31 | |
| 32 | Before diving into details of what steps to take, here's a description of what happens when a guest is started. |
| 33 | |
| 34 | From the service file: |
| 35 | |
| 36 | {{{ |
| 37 | PermissionsStartOnly=true |
| 38 | User=%i |
| 39 | ExecStartPre=/usr/local/sbin/kvm-setup %i |
| 40 | ExecStart=/usr/local/sbin/kvm-start %i |
| 41 | ExecStop=/usr/local/sbin/kvm-stop %i |
| 42 | ExecStopPost=/usr/local/sbin/kvm-teardown %i |
| 43 | }}} |
| 44 | |
| 45 | The PermissionsStartOnly part means that only the start and stop scripts are executed by the user specified (which will always be the name of the guest). |
| 46 | |
| 47 | The ExecStartPre and ExecStopPost are executed as root. |
| 48 | |
| 49 | So this means, every time you start the guest: |
| 50 | |
| 51 | 1. `/usr/local/sbin/kvm-setup $guest` is run as root |
| 52 | 1. `/usr/local/sbin/kvm-start $guest` is run as the guest user |
| 53 | |
| 54 | Everytime you stop it: |
| 55 | |
| 56 | 1. `/usr/local/sbin/kvm-stop $guest` is run as $guest |
| 57 | 1. `/usr/local/sbin/kvm-teardown` is run as root. |
| 58 | |
| 59 | The kvm-setup and kvm-teardown scripts setup the networking for the guest. kvm-start launches the kvm process. And kvm-stop sends it the qemu signal to shut down. |
| 60 | |
| 61 | Additionally, we have: |
| 62 | |
| 63 | {{{ |
| 64 | Wants=kvm-screen@%i.service |
| 65 | Before=kvm-screen@%i.service |
| 66 | }}} |
| 67 | |
| 68 | This part indicates that everytime kvm@$guest.service starts, we should start kvm-screen@$guest.service - which launches the screen session that goes with it. |
| 69 | |
| 70 | The kvm-screen service simply runs: `/usr/local/sbin/kvm-screen $guest` as the $guest user. |
| 71 | |
| 72 | === Trouble Shooting === |
| 73 | |
| 74 | There are a number of steps you can take to debug what is going wrong. |
| 75 | |
| 76 | The first step is: `journalctl -u kvm@$guest.service` and `systemctl status kvm@$guest.service` |
| 77 | |
| 78 | In addition, you can try running each step manually. This is perfectly safe and acceptable to do - but you must ensure you run the right commands as the right user. |
| 95 | |
| 96 | Here's a real world example: |
| 97 | |
| 98 | {{{ |
| 99 | 0 medgar:~# systemctl status kvm@stokely.service |
| 100 | ● kvm@stokely.service - KVM Manager virtual guest management script for stokely |
| 101 | Loaded: loaded (/usr/local/share/kvm-manager/kvm@.service; disabled; vendor preset: enabled) |
| 102 | Active: failed (Result: exit-code) since Tue 2018-06-26 04:57:42 UTC; 9h ago |
| 103 | Process: 4408 ExecStartPre=/usr/local/sbin/kvm-setup stokely (code=exited, status=1/FAILURE) |
| 104 | |
| 105 | Jun 26 04:57:42 medgar systemd[1]: Failed to start KVM Manager virtual guest management script for stokely. |
| 106 | Jun 26 04:57:42 medgar systemd[1]: kvm@stokely.service: Unit entered failed state. |
| 107 | Jun 26 04:57:42 medgar systemd[1]: kvm@stokely.service: Failed with result 'exit-code'. |
| 108 | Jun 26 04:57:42 medgar systemd[1]: kvm@stokely.service: Service hold-off time over, scheduling restart. |
| 109 | Jun 26 04:57:42 medgar systemd[1]: Stopped KVM Manager virtual guest management script for stokely. |
| 110 | Jun 26 04:57:42 medgar systemd[1]: kvm@stokely.service: Start request repeated too quickly. |
| 111 | Jun 26 04:57:42 medgar systemd[1]: Failed to start KVM Manager virtual guest management script for stokely. |
| 112 | Jun 26 04:57:42 medgar systemd[1]: kvm@stokely.service: Unit entered failed state. |
| 113 | Jun 26 04:57:42 medgar systemd[1]: kvm@stokely.service: Failed with result 'exit-code'. |
| 114 | 3 medgar:~# |
| 115 | }}} |
| 116 | |
| 117 | This output indicates that the failure happened with the ExecStartPre command existing with a non-zero exit code. |
| 118 | |
| 119 | Now, running it manually: |
| 120 | |
| 121 | {{{ |
| 122 | 3 medgar:~# kvm-setup stokely |
| 123 | Running kvm-prepare |
| 124 | Configuring tap (stokely0) on bridge (br0). |
| 125 | ioctl(TUNSETIFF): Device or resource busy |
| 126 | 1 medgar:~# |
| 127 | }}} |
| 128 | |
| 129 | This error suggests that the networking may not have been torn down properly on a previous run. So, let's tear it down: |
| 130 | |
| 131 | {{{ |
| 132 | 1 medgar:~# kvm-teardown stokely |
| 133 | De-configuring the network. |
| 134 | 0 medgar:~# kvm-setup stokely |
| 135 | Running kvm-prepare |
| 136 | Configuring tap (stokely0) on bridge (br0). |
| 137 | /dev/mapper/vg_medgar0-stokely |
| 138 | kvm-prepare completed successfully |
| 139 | 0 medgar:~# |
| 140 | }}} |
| 141 | |
| 142 | Now it works! But, before trying to restart stokely, let's tear it down again so we are back in the right state for it to start on it's own: |
| 143 | |
| 144 | {{{ |
| 145 | 0 medgar:~# kvm-teardown stokely |
| 146 | De-configuring the network. |
| 147 | 0 medgar:~# |
| 148 | }}} |