| 1 | #!/bin/shscip-shell filesystem . . `/bin/sh`. |
| 2 | # |
| 3 | # Support: [email protected] |
| 4 | # License: GNU General Public License (GPL) |
| 5 | # |
| 6 | # Resource Agent for domains managed by the libvirt API. |
| 7 | # Requires a running libvirt daemon (libvirtd). |
| 8 | # |
| 9 | # (c) 2008-2010 Florian Haas, Dejan Muhamedagic, |
| 10 | # and Linux-HA contributors |
| 11 | # |
| 12 | # usage: $0 {start|stop|status|monitor|migrate_to|migrate_from|meta-data|validate-all} |
| 13 | # |
| 14 | ####################################################################### |
| 15 | # Initialization: |
| 16 | : ${OCF_FUNCTIONS_DIR=${OCF_ROOT}/lib/heartbeat} |
| 17 | . ${OCF_FUNCTIONS_DIR}/ocf-shellfuncs |
| 18 | |
| 19 | # Defaults |
| 20 | OCF_RESKEY_config_default="" |
| 21 | OCF_RESKEY_migration_transport_default="" |
| 22 | OCF_RESKEY_migration_downtime_default=0 |
| 23 | OCF_RESKEY_migration_speed_default=0 |
| 24 | OCF_RESKEY_migration_network_suffix_default="" |
| 25 | OCF_RESKEY_force_stop_default=0 |
| 26 | OCF_RESKEY_monitor_scripts_default="" |
| 27 | OCF_RESKEY_autoset_utilization_cpu_default="true" |
| 28 | OCF_RESKEY_autoset_utilization_host_memory_default="true" |
| 29 | OCF_RESKEY_autoset_utilization_hv_memory_default="true" |
| 30 | OCF_RESKEY_unset_utilization_cpu_default="false" |
| 31 | OCF_RESKEY_unset_utilization_host_memory_default="false" |
| 32 | OCF_RESKEY_unset_utilization_hv_memory_default="false" |
| 33 | OCF_RESKEY_migrateport_default=$(( 49152 + $(ocf_maybe_random) % 64 )) |
| 34 | OCF_RESKEY_CRM_meta_timeout_default=90000 |
| 35 | OCF_RESKEY_save_config_on_stop_default=false |
| 36 | OCF_RESKEY_sync_config_on_stop_default=false |
| 37 | OCF_RESKEY_snapshot_default="" |
| 38 | OCF_RESKEY_backingfile_default="" |
| 39 | OCF_RESKEY_stateless_default="false" |
| 40 | OCF_RESKEY_copyindirs_default="" |
| 41 | OCF_RESKEY_shutdown_mode_default="" |
| 42 | OCF_RESKEY_start_resources_default="false" |
| 43 | |
| 44 | : ${OCF_RESKEY_config=${OCF_RESKEY_config_default}} |
| 45 | : ${OCF_RESKEY_migration_transport=${OCF_RESKEY_migration_transport_default}} |
| 46 | : ${OCF_RESKEY_migration_downtime=${OCF_RESKEY_migration_downtime_default}} |
| 47 | : ${OCF_RESKEY_migration_speed=${OCF_RESKEY_migration_speed_default}} |
| 48 | : ${OCF_RESKEY_migration_network_suffix=${OCF_RESKEY_migration_network_suffix_default}} |
| 49 | : ${OCF_RESKEY_force_stop=${OCF_RESKEY_force_stop_default}} |
| 50 | : ${OCF_RESKEY_monitor_scripts=${OCF_RESKEY_monitor_scripts_default}} |
| 51 | : ${OCF_RESKEY_autoset_utilization_cpu=${OCF_RESKEY_autoset_utilization_cpu_default}} |
| 52 | : ${OCF_RESKEY_autoset_utilization_host_memory=${OCF_RESKEY_autoset_utilization_host_memory_default}} |
| 53 | : ${OCF_RESKEY_autoset_utilization_hv_memory=${OCF_RESKEY_autoset_utilization_hv_memory_default}} |
| 54 | : ${OCF_RESKEY_unset_utilization_cpu=${OCF_RESKEY_unset_utilization_cpu_default}} |
| 55 | : ${OCF_RESKEY_unset_utilization_host_memory=${OCF_RESKEY_unset_utilization_host_memory_default}} |
| 56 | : ${OCF_RESKEY_unset_utilization_hv_memory=${OCF_RESKEY_unset_utilization_hv_memory_default}} |
| 57 | : ${OCF_RESKEY_migrateport=${OCF_RESKEY_migrateport_default}} |
| 58 | : ${OCF_RESKEY_CRM_meta_timeout=${OCF_RESKEY_CRM_meta_timeout_default}} |
| 59 | : ${OCF_RESKEY_save_config_on_stop=${OCF_RESKEY_save_config_on_stop_default}} |
| 60 | : ${OCF_RESKEY_sync_config_on_stop=${OCF_RESKEY_sync_config_on_stop_default}} |
| 61 | : ${OCF_RESKEY_snapshot=${OCF_RESKEY_snapshot_default}} |
| 62 | : ${OCF_RESKEY_backingfile=${OCF_RESKEY_backingfile_default}} |
| 63 | : ${OCF_RESKEY_stateless=${OCF_RESKEY_stateless_default}} |
| 64 | : ${OCF_RESKEY_copyindirs=${OCF_RESKEY_copyindirs_default}} |
| 65 | : ${OCF_RESKEY_shutdown_mode=${OCF_RESKEY_shutdown_mode_default}} |
| 66 | : ${OCF_RESKEY_start_resources=${OCF_RESKEY_start_resources_default}} |
| 67 | |
| 68 | if ocf_is_true ${OCF_RESKEY_sync_config_on_stop}; then |
| 69 | OCF_RESKEY_save_config_on_stop="true" |
| 70 | fi |
| 71 | ####################################################################### |
| 72 | |
| 73 | ## I'd very much suggest to make this RA use bash, |
| 74 | ## and then use magic $SECONDS. |
| 75 | ## But for now: |
| 76 | NOW=$(datescip-shell system . . date. +%s) |
| 77 | |
| 78 | usagescip-shell shell shell-project 0.0.0 usage().() { |
| 79 | echoscip-shell system . . echo. "usage: $0 {start|stop|status|monitor|migrate_to|migrate_from|meta-data|validate-all}" |
| 80 | } |
| 81 | |
| 82 | VirtualDomain_meta_datascip-shell shell shell-project 0.0.0 VirtualDomain_meta_data().() { |
| 83 | catscip-shell system . . cat. <<EOF |
| 84 | <?xml version="1.0"?> |
| 85 | <!DOCTYPE resource-agent SYSTEM "ra-api-1.dtd"> |
| 86 | <resource-agent name="VirtualDomain" version="1.1"> |
| 87 | <version>1.0</version> |
| 88 | |
| 89 | <longdesc lang="en"> |
| 90 | Resource agent for a virtual domain (a.k.a. domU, virtual machine, |
| 91 | virtual environment etc., depending on context) managed by libvirtd. |
| 92 | </longdesc> |
| 93 | <shortdesc lang="en">Manages virtual domains through the libvirt virtualization framework</shortdesc> |
| 94 | |
| 95 | <parameters> |
| 96 | |
| 97 | <parameter name="config" unique="1" required="1"> |
| 98 | <longdesc lang="en"> |
| 99 | Absolute path to the libvirt configuration file, |
| 100 | for this virtual domain. |
| 101 | </longdesc> |
| 102 | <shortdesc lang="en">Virtual domain configuration file</shortdesc> |
| 103 | <content type="string" default="${OCF_RESKEY_config_default}" /> |
| 104 | </parameter> |
| 105 | |
| 106 | <parameter name="hypervisor" unique="0" required="0"> |
| 107 | <longdesc lang="en"> |
| 108 | Hypervisor URI to connect to. See the libvirt documentation for |
| 109 | details on supported URI formats. The default is system dependent. |
| 110 | Determine the system's default uri by running 'virsh --quiet uri'. |
| 111 | </longdesc> |
| 112 | <shortdesc lang="en">Hypervisor URI</shortdesc> |
| 113 | <content type="string"/> |
| 114 | </parameter> |
| 115 | |
| 116 | <parameter name="force_stop" unique="0" required="0"> |
| 117 | <longdesc lang="en"> |
| 118 | Always forcefully shut down ("destroy") the domain on stop. The default |
| 119 | behavior is to resort to a forceful shutdown only after a graceful |
| 120 | shutdown attempt has failed. You should only set this to true if |
| 121 | your virtual domain (or your virtualization backend) does not support |
| 122 | graceful shutdown. |
| 123 | </longdesc> |
| 124 | <shortdesc lang="en">Always force shutdown on stop</shortdesc> |
| 125 | <content type="boolean" default="${OCF_RESKEY_force_stop_default}" /> |
| 126 | </parameter> |
| 127 | |
| 128 | <parameter name="migration_transport" unique="0" required="0"> |
| 129 | <longdesc lang="en"> |
| 130 | Transport used to connect to the remote hypervisor while |
| 131 | migrating. Please refer to the libvirt documentation for details on |
| 132 | transports available. If this parameter is omitted, the resource will |
| 133 | use libvirt's default transport to connect to the remote hypervisor. |
| 134 | </longdesc> |
| 135 | <shortdesc lang="en">Remote hypervisor transport</shortdesc> |
| 136 | <content type="string" default="${OCF_RESKEY_migration_transport_default}" /> |
| 137 | </parameter> |
| 138 | |
| 139 | <parameter name="migration_user" unique="0" required="0"> |
| 140 | <longdesc lang="en"> |
| 141 | The username will be used in the remote libvirt remoteuri/migrateuri. No user will be |
| 142 | given (which means root) in the username if omitted |
| 143 | |
| 144 | If remoteuri is set, migration_user will be ignored. |
| 145 | </longdesc> |
| 146 | <shortdesc lang="en">Remote username for the remoteuri</shortdesc> |
| 147 | <content type="string" /> |
| 148 | </parameter> |
| 149 | |
| 150 | <parameter name="migration_downtime" unique="0" required="0"> |
| 151 | <longdesc lang="en"> |
| 152 | Define max downtime during live migration in milliseconds |
| 153 | </longdesc> |
| 154 | <shortdesc lang="en">Live migration downtime</shortdesc> |
| 155 | <content type="integer" default="${OCF_RESKEY_migration_downtime_default}" /> |
| 156 | </parameter> |
| 157 | |
| 158 | <parameter name="migration_speed" unique="0" required="0"> |
| 159 | <longdesc lang="en"> |
| 160 | Define live migration speed per resource in MiB/s |
| 161 | </longdesc> |
| 162 | <shortdesc lang="en">Live migration speed</shortdesc> |
| 163 | <content type="integer" default="${OCF_RESKEY_migration_speed_default}" /> |
| 164 | </parameter> |
| 165 | |
| 166 | <parameter name="migration_network_suffix" unique="0" required="0"> |
| 167 | <longdesc lang="en"> |
| 168 | Use a dedicated migration network. The migration URI is composed by |
| 169 | adding this parameters value to the end of the node name. If the node |
| 170 | name happens to be an FQDN (as opposed to an unqualified host name), |
| 171 | insert the suffix immediately prior to the first period (.) in the FQDN. |
| 172 | At the moment Qemu/KVM and Xen migration via a dedicated network is supported. |
| 173 | |
| 174 | Note: Be sure this composed host name is locally resolvable and the |
| 175 | associated IP is reachable through the favored network. This suffix will |
| 176 | be added to the remoteuri and migrateuri parameters. |
| 177 | |
| 178 | See also the migrate_options parameter below. |
| 179 | </longdesc> |
| 180 | <shortdesc lang="en">Migration network host name suffix</shortdesc> |
| 181 | <content type="string" default="${OCF_RESKEY_migration_network_suffix_default}" /> |
| 182 | </parameter> |
| 183 | |
| 184 | <parameter name="migrateuri" unique="0" required="0"> |
| 185 | <longdesc lang="en"> |
| 186 | You can also specify here if the calculated migrate URI is unsuitable for your |
| 187 | environment. |
| 188 | |
| 189 | If migrateuri is set then migration_network_suffix, migrateport and |
| 190 | --migrateuri in migrate_options are effectively ignored. Use "%n" as the |
| 191 | placeholder for the target node name. |
| 192 | |
| 193 | Please refer to the libvirt documentation for details on guest |
| 194 | migration. |
| 195 | </longdesc> |
| 196 | <shortdesc lang="en">Custom migrateuri for migration state transfer</shortdesc> |
| 197 | <content type="string" /> |
| 198 | </parameter> |
| 199 | |
| 200 | <parameter name="migrate_options" unique="0" required="0"> |
| 201 | <longdesc lang="en"> |
| 202 | Extra virsh options for the guest live migration. You can also specify |
| 203 | here --migrateuri if the calculated migrate URI is unsuitable for your |
| 204 | environment. If --migrateuri is set then migration_network_suffix |
| 205 | and migrateport are effectively ignored. Use "%n" as the placeholder |
| 206 | for the target node name. |
| 207 | |
| 208 | Please refer to the libvirt documentation for details on guest |
| 209 | migration. |
| 210 | </longdesc> |
| 211 | <shortdesc lang="en">live migrate options</shortdesc> |
| 212 | <content type="string" /> |
| 213 | </parameter> |
| 214 | |
| 215 | <parameter name="monitor_scripts" unique="0" required="0"> |
| 216 | <longdesc lang="en"> |
| 217 | To additionally monitor services within the virtual domain, add this |
| 218 | parameter with a list of scripts to monitor. |
| 219 | |
| 220 | Note: when monitor scripts are used, the start and migrate_from operations |
| 221 | will complete only when all monitor scripts have completed successfully. |
| 222 | Be sure to set the timeout of these operations to accommodate this delay. |
| 223 | </longdesc> |
| 224 | <shortdesc lang="en">space-separated list of monitor scripts</shortdesc> |
| 225 | <content type="string" default="${OCF_RESKEY_monitor_scripts_default}" /> |
| 226 | </parameter> |
| 227 | |
| 228 | <parameter name="autoset_utilization_cpu" unique="0" required="0"> |
| 229 | <longdesc lang="en"> |
| 230 | If set true, the agent will detect the number of domainU's vCPUs from virsh, and put it |
| 231 | into the CPU utilization of the resource when the monitor is executed. |
| 232 | </longdesc> |
| 233 | <shortdesc lang="en">Enable auto-setting the CPU utilization of the resource</shortdesc> |
| 234 | <content type="boolean" default="${OCF_RESKEY_autoset_utilization_cpu_default}" /> |
| 235 | </parameter> |
| 236 | |
| 237 | <parameter name="autoset_utilization_host_memory" unique="0" required="0"> |
| 238 | <longdesc lang="en"> |
| 239 | If set true, the agent will detect the number of *Max memory* from virsh, and put it |
| 240 | into the host_memory utilization of the resource when the monitor is executed. |
| 241 | </longdesc> |
| 242 | <shortdesc lang="en">Enable auto-setting the host_memory utilization of the resource</shortdesc> |
| 243 | <content type="boolean" default="${OCF_RESKEY_autoset_utilization_host_memory_default}" /> |
| 244 | </parameter> |
| 245 | |
| 246 | <parameter name="autoset_utilization_hv_memory" unique="0" required="0"> |
| 247 | <longdesc lang="en"> |
| 248 | If set true, the agent will detect the number of *Max memory* from virsh, and put it |
| 249 | into the hv_memory utilization of the resource when the monitor is executed. |
| 250 | </longdesc> |
| 251 | <shortdesc lang="en">Enable auto-setting the hv_memory utilization of the resource</shortdesc> |
| 252 | <content type="boolean" default="${OCF_RESKEY_autoset_utilization_hv_memory_default}" /> |
| 253 | </parameter> |
| 254 | |
| 255 | <parameter name="unset_utilization_cpu" unique="0" required="0"> |
| 256 | <longdesc lang="en"> |
| 257 | If set true then the agent will remove the cpu utilization resource when the monitor |
| 258 | is executed. |
| 259 | </longdesc> |
| 260 | <shortdesc lang="en">Enable auto-removing the CPU utilization of the resource</shortdesc> |
| 261 | <content type="boolean" default="${OCF_RESKEY_unset_utilization_cpu_default}" /> |
| 262 | </parameter> |
| 263 | |
| 264 | <parameter name="unset_utilization_host_memory" unique="0" required="0"> |
| 265 | <longdesc lang="en"> |
| 266 | If set true then the agent will remove the host_memory utilization resource when the monitor |
| 267 | is executed. |
| 268 | </longdesc> |
| 269 | <shortdesc lang="en">Enable auto-removing the host_memory utilization of the resource</shortdesc> |
| 270 | <content type="boolean" default="${OCF_RESKEY_unset_utilization_host_memory_default}" /> |
| 271 | </parameter> |
| 272 | |
| 273 | <parameter name="unset_utilization_hv_memory" unique="0" required="0"> |
| 274 | <longdesc lang="en"> |
| 275 | If set true then the agent will remove the hv_memory utilization resource when the monitor |
| 276 | is executed. |
| 277 | </longdesc> |
| 278 | <shortdesc lang="en">Enable auto-removing the hv_memory utilization of the resource</shortdesc> |
| 279 | <content type="boolean" default="${OCF_RESKEY_unset_utilization_hv_memory_default}" /> |
| 280 | </parameter> |
| 281 | |
| 282 | <parameter name="migrateport" unique="0" required="0"> |
| 283 | <longdesc lang="en"> |
| 284 | This port will be used in the qemu migrateuri. If unset, the port will be a random highport. |
| 285 | </longdesc> |
| 286 | <shortdesc lang="en">Port for migrateuri</shortdesc> |
| 287 | <content type="integer" /> |
| 288 | </parameter> |
| 289 | |
| 290 | <parameter name="remoteuri" unique="0" required="0"> |
| 291 | <longdesc lang="en"> |
| 292 | Use this URI as virsh connection URI to commuicate with a remote hypervisor. |
| 293 | |
| 294 | If remoteuri is set then migration_user and migration_network_suffix are |
| 295 | effectively ignored. Use "%n" as the placeholder for the target node name. |
| 296 | |
| 297 | Please refer to the libvirt documentation for details on guest |
| 298 | migration. |
| 299 | </longdesc> |
| 300 | <shortdesc lang="en">Custom remoteuri to communicate with a remote hypervisor</shortdesc> |
| 301 | <content type="string" /> |
| 302 | </parameter> |
| 303 | |
| 304 | <parameter name="save_config_on_stop" unique="0" required="0"> |
| 305 | <longdesc lang="en"> |
| 306 | Changes to a running VM's config are normally lost on stop. |
| 307 | This parameter instructs the RA to save the configuration back to the xml file provided in the "config" parameter. |
| 308 | </longdesc> |
| 309 | <shortdesc lang="en">Save running VM's config back to its config file</shortdesc> |
| 310 | <content type="boolean" /> |
| 311 | </parameter> |
| 312 | |
| 313 | <parameter name="sync_config_on_stop" unique="0" required="0"> |
| 314 | <longdesc lang="en"> |
| 315 | Setting this automatically enables save_config_on_stop. |
| 316 | When enabled this parameter instructs the RA to |
| 317 | call csync2 -x to synchronize the file to all nodes. |
| 318 | csync2 must be properly set up for this to work. |
| 319 | </longdesc> |
| 320 | <shortdesc lang="en">Save running VM's config back to its config file</shortdesc> |
| 321 | <content type="boolean" /> |
| 322 | </parameter> |
| 323 | |
| 324 | <parameter name="snapshot"> |
| 325 | <longdesc lang="en"> |
| 326 | Path to the snapshot directory where the virtual machine image will be stored. When this |
| 327 | parameter is set, the virtual machine's RAM state will be saved to a file in the snapshot |
| 328 | directory when stopped. If on start a state file is present for the domain, the domain |
| 329 | will be restored to the same state it was in right before it stopped last. This option |
| 330 | is incompatible with the 'force_stop' option. |
| 331 | </longdesc> |
| 332 | <shortdesc lang="en"> |
| 333 | Restore state on start/stop |
| 334 | </shortdesc> |
| 335 | <content type="string" default="${OCF_RESKEY_snapshot_default}"/> |
| 336 | </parameter> |
| 337 | |
| 338 | <parameter name="backingfile" unique="0" required="0"> |
| 339 | <longdesc lang="en"> |
| 340 | When the VM is used in Copy-On-Write mode, this is the backing file to use (with its full path). |
| 341 | The VMs image will be created based on this backing file. |
| 342 | This backing file will never be changed during the life of the VM. |
| 343 | </longdesc> |
| 344 | <shortdesc lang="en">If the VM is wanted to work with Copy-On-Write mode, this is the backing file to use (with its full path)</shortdesc> |
| 345 | <content type="string" default="${OCF_RESKEY_backingfile_default}" /> |
| 346 | </parameter> |
| 347 | |
| 348 | <parameter name="stateless" unique="0" required="0"> |
| 349 | <longdesc lang="en"> |
| 350 | If set to true and backingfile is defined, the start of the VM will systematically create a new qcow2 based on |
| 351 | the backing file, therefore the VM will always be stateless. If set to false, the start of the VM will use the |
| 352 | COW (<vmname>.qcow2) file if it exists, otherwise the first start will create a new qcow2 based on the backing |
| 353 | file given as backingfile. |
| 354 | </longdesc> |
| 355 | <shortdesc lang="en">If set to true, the (<vmname>.qcow2) file will be re-created at each start, based on the backing file (if defined)</shortdesc> |
| 356 | <content type="boolean" default="${OCF_RESKEY_stateless_default}" /> |
| 357 | </parameter> |
| 358 | |
| 359 | <parameter name="copyindirs" unique="0" required="0"> |
| 360 | <longdesc lang="en"> |
| 361 | List of directories for the virt-copy-in before booting the VM. Used only in stateless mode. |
| 362 | </longdesc> |
| 363 | <shortdesc lang="en">List of directories for the virt-copy-in before booting the VM stateless mode.</shortdesc> |
| 364 | <content type="string" default="${OCF_RESKEY_copyindirs_default}" /> |
| 365 | </parameter> |
| 366 | |
| 367 | <parameter name="shutdown_mode"> |
| 368 | <longdesc lang="en"> |
| 369 | virsh shutdown method to use. Please verify that it is supported by your virsh toolsed with 'virsh help shutdown' |
| 370 | When this parameter is set --mode shutdown_mode is passed as an additional argument to the 'virsh shutdown' command. |
| 371 | One can use this option in case default acpi method does not work. Verify that this mode is supported |
| 372 | by your VM. By default --mode is not passed. |
| 373 | </longdesc> |
| 374 | <shortdesc lang="en"> |
| 375 | Instruct virsh to use specific shutdown mode |
| 376 | </shortdesc> |
| 377 | <content type="string" default="${OCF_RESKEY_shutdown_mode_default}"/> |
| 378 | </parameter> |
| 379 | |
| 380 | <parameter name="start_resources"> |
| 381 | <longdesc lang="en"> |
| 382 | Start the virtual storage pools and networks used by the virtual machine before starting it or before live migrating it. |
| 383 | </longdesc> |
| 384 | <shortdesc lang="en"> |
| 385 | Ensure the needed virtual storage pools and networks are started |
| 386 | </shortdesc> |
| 387 | <content type="boolean" default="${OCF_RESKEY_start_resources_default}"/> |
| 388 | </parameter> |
| 389 | |
| 390 | </parameters> |
| 391 | |
| 392 | <actions> |
| 393 | <action name="start" timeout="90s" /> |
| 394 | <action name="stop" timeout="90s" /> |
| 395 | <action name="status" depth="0" timeout="30s" interval="10s" /> |
| 396 | <action name="monitor" depth="0" timeout="30s" interval="10s" /> |
| 397 | <action name="migrate_from" timeout="60s" /> |
| 398 | <action name="migrate_to" timeout="120s" /> |
| 399 | <action name="meta-data" timeout="5s" /> |
| 400 | <action name="validate-all" timeout="5s" /> |
| 401 | </actions> |
| 402 | </resource-agent> |
| 403 | EOF |
| 404 | } |
| 405 | |
| 406 | set_util_attrscip-shell shell shell-project 0.0.0 set_util_attr().() { |
| 407 | local attr=$1 val=$2 |
| 408 | local cval outp |
| 409 | |
| 410 | cval=$(crm_resource -Q -r $OCF_RESOURCE_INSTANCE -z -g $attr 2>/dev/nullscip-shell filesystem . . `/dev/null`.) |
| 411 | if [scip-shell system . . `[`. $? -ne 0 ] && [scip-shell system . . `[`. -z "$cval" ]; then |
| 412 | crm_resource -Q -r $OCF_RESOURCE_INSTANCE -z -g $attr 2>&1 | grepscip-shell system . . grep. -e "not connected" > /dev/nullscip-shell filesystem . . `/dev/null`. 2>&1 |
| 413 | if [scip-shell system . . `[`. $? -eq 0 ]; then |
| 414 | ocf_log debug "Unable to set utilization attribute, cib is not available" |
| 415 | return |
| 416 | fi |
| 417 | fi |
| 418 | |
| 419 | if [scip-shell system . . `[`. "$cval" != "$val" ]; then |
| 420 | outp=$(crm_resource -r $OCF_RESOURCE_INSTANCE -z -p $attr -v $val 2>&1) || |
| 421 | ocf_log warn "crm_resource failed to set utilization attribute $attr: $outp" |
| 422 | fi |
| 423 | } |
| 424 | |
| 425 | unset_util_attrscip-shell shell shell-project 0.0.0 unset_util_attr().() { |
| 426 | local attr=$1 |
| 427 | local cval outp |
| 428 | |
| 429 | outp=$(crm_resource --resource=$OCF_RESOURCE_INSTANCE --utilization --delete-parameter=$attr 2>&1) || |
| 430 | ocf_log warn "crm_resource failed to unset utilization attribute $attr: $outp" |
| 431 | } |
| 432 | |
| 433 | update_utilizationscip-shell shell shell-project 0.0.0 update_utilization().() { |
| 434 | local dom_cpu dom_mem |
| 435 | |
| 436 | if ocf_is_true "$OCF_RESKEY_autoset_utilization_cpu"; then |
| 437 | dom_cpu=$(LANG=C virsh $VIRSH_OPTIONS dominfo ${DOMAIN_NAME} 2>/dev/nullscip-shell filesystem . . `/dev/null`. | awkscip-shell system . . awk. '/CPU\(s\)/{print $2}scip-shell filesystem . . `/CPU\(s\)/{print $2}`.') |
| 438 | testscip-shell system . . test. -n "$dom_cpu" && set_util_attrscip-shell shell shell-project 0.0.0 set_util_attr(). cpu $dom_cpu |
| 439 | elif ocf_is_true "$OCF_RESKEY_unset_utilization_cpu"; then |
| 440 | unset_util_attrscip-shell shell shell-project 0.0.0 unset_util_attr(). cpu |
| 441 | fi |
| 442 | |
| 443 | if ocf_is_true "$OCF_RESKEY_autoset_utilization_host_memory"; then |
| 444 | dom_mem=$(LANG=C virsh $VIRSH_OPTIONS dominfo ${DOMAIN_NAME} 2>/dev/nullscip-shell filesystem . . `/dev/null`. | awkscip-shell system . . awk. '/Max memory/{printf("%d", $3/1024)}scip-shell filesystem . . `/Max memory/{printf("%d", $3/1024)}`.') |
| 445 | testscip-shell system . . test. -n "$dom_mem" && set_util_attrscip-shell shell shell-project 0.0.0 set_util_attr(). host_memory "$dom_mem" |
| 446 | elif ocf_is_true "$OCF_RESKEY_unset_utilization_host_memory"; then |
| 447 | unset_util_attrscip-shell shell shell-project 0.0.0 unset_util_attr(). host_memory |
| 448 | fi |
| 449 | |
| 450 | if ocf_is_true "$OCF_RESKEY_autoset_utilization_hv_memory"; then |
| 451 | dom_mem=$(LANG=C virsh $VIRSH_OPTIONS dominfo ${DOMAIN_NAME} 2>/dev/nullscip-shell filesystem . . `/dev/null`. | awkscip-shell system . . awk. '/Max memory/{printf("%d", $3/1024)}scip-shell filesystem . . `/Max memory/{printf("%d", $3/1024)}`.') |
| 452 | testscip-shell system . . test. -n "$dom_mem" && set_util_attrscip-shell shell shell-project 0.0.0 set_util_attr(). hv_memory "$dom_mem" |
| 453 | elif ocf_is_true "$OCF_RESKEY_unset_utilization_hv_memory"; then |
| 454 | unset_util_attrscip-shell shell shell-project 0.0.0 unset_util_attr(). hv_memory |
| 455 | fi |
| 456 | } |
| 457 | |
| 458 | get_emulatorscip-shell shell shell-project 0.0.0 get_emulator().() |
| 459 | { |
| 460 | local emulator="" |
| 461 | |
| 462 | emulator=$(virsh $VIRSH_OPTIONS dumpxml $DOMAIN_NAME 2>/dev/nullscip-shell filesystem . . `/dev/null`. | sedscip-shell system . . sed. -n -e 's/^.*<emulator>\(.*\)<\/emulator>.*$/\1/p') |
| 463 | if [scip-shell system . . `[`. -z "$emulator" ] && [scip-shell system . . `[`. -e "$EMULATOR_STATE" ]; then |
| 464 | emulator=$(catscip-shell system . . cat. $EMULATOR_STATE) |
| 465 | fi |
| 466 | if [scip-shell system . . `[`. -z "$emulator" ]; then |
| 467 | emulator=$(catscip-shell system . . cat. ${OCF_RESKEY_config} | sedscip-shell system . . sed. -n -e 's/^.*<emulator>\(.*\)<\/emulator>.*$/\1/p') |
| 468 | fi |
| 469 | |
| 470 | if [scip-shell system . . `[`. -n "$emulator" ]; then |
| 471 | basenamescip-shell system . . basename. $emulator |
| 472 | fi |
| 473 | } |
| 474 | |
| 475 | update_emulator_cachescip-shell shell shell-project 0.0.0 update_emulator_cache().() |
| 476 | { |
| 477 | local emulator |
| 478 | |
| 479 | emulator=$(get_emulatorscip-shell shell shell-project 0.0.0 get_emulator().) |
| 480 | if [scip-shell system . . `[`. -n "$emulator" ]; then |
| 481 | echoscip-shell system . . echo. $emulator > $EMULATOR_STATE |
| 482 | fi |
| 483 | } |
| 484 | |
| 485 | # attempt to check domain status outside of libvirt using the emulator process |
| 486 | pid_statusscip-shell shell shell-project 0.0.0 pid_status().() |
| 487 | { |
| 488 | local rc=$OCF_ERR_GENERIC |
| 489 | local emulator=$(get_emulatorscip-shell shell shell-project 0.0.0 get_emulator().) |
| 490 | # An emulator is not required, so only report message in debug mode |
| 491 | local loglevel="debug" |
| 492 | |
| 493 | if ocf_is_probe; then |
| 494 | loglevel="notice" |
| 495 | fi |
| 496 | |
| 497 | case "$emulator" in |
| 498 | qemu-kvm|qemu-dm|qemu-system-*) |
| 499 | rc=$OCF_NOT_RUNNING |
| 500 | psscip-shell system . . ps. awx | grepscip-shell system . . grep. -E "[q]emu-(kvm|dm|system).*-name ($DOMAIN_NAME|[^ ]*guest=$DOMAIN_NAME(,[^ ]*)?) " > /dev/nullscip-shell filesystem . . `/dev/null`. 2>&1 |
| 501 | if [scip-shell system . . `[`. $? -eq 0 ]; then |
| 502 | rc=$OCF_SUCCESS |
| 503 | fi |
| 504 | ;; |
| 505 | libvirt_lxc) |
| 506 | rc=$OCF_NOT_RUNNING |
| 507 | psscip-shell system . . ps. awx | grepscip-shell system . . grep. -E "[l]ibvirt_lxc.*-name ($DOMAIN_NAME|[^ ]*guest=$DOMAIN_NAME(,[^ ]*)?) " > /dev/nullscip-shell filesystem . . `/dev/null`. 2>&1 |
| 508 | if [scip-shell system . . `[`. $? -eq 0 ]; then |
| 509 | rc=$OCF_SUCCESS |
| 510 | fi |
| 511 | ;; |
| 512 | # This can be expanded to check for additional emulators |
| 513 | *) |
| 514 | # We may be running xen with PV domains, they don't |
| 515 | # have an emulator set. try xl list or xen-lists |
| 516 | if have_binary xl; then |
| 517 | rc=$OCF_NOT_RUNNING |
| 518 | xl list $DOMAIN_NAME >/dev/nullscip-shell filesystem . . `/dev/null`. 2>&1 |
| 519 | if [scip-shell system . . `[`. $? -eq 0 ]; then |
| 520 | rc=$OCF_SUCCESS |
| 521 | fi |
| 522 | elif have_binary xen-list; then |
| 523 | rc=$OCF_NOT_RUNNING |
| 524 | xen-list $DOMAIN_NAME 2>/dev/nullscip-shell filesystem . . `/dev/null`. | grepscip-shell system . . grep. -qs "State.*[-r][-b][-p]--" 2>/dev/nullscip-shell filesystem . . `/dev/null`. |
| 525 | if [scip-shell system . . `[`. $? -eq 0 ]; then |
| 526 | rc=$OCF_SUCCESS |
| 527 | fi |
| 528 | else |
| 529 | ocf_log $loglevel "Unable to determine emulator for $DOMAIN_NAME" |
| 530 | fi |
| 531 | ;; |
| 532 | esac |
| 533 | |
| 534 | if [scip-shell system . . `[`. $rc -eq $OCF_SUCCESS ]; then |
| 535 | ocf_log debug "Virtual domain $DOMAIN_NAME is currently running." |
| 536 | elif [scip-shell system . . `[`. $rc -eq $OCF_NOT_RUNNING ]; then |
| 537 | ocf_log debug "Virtual domain $DOMAIN_NAME is currently not running." |
| 538 | fi |
| 539 | |
| 540 | return $rc |
| 541 | } |
| 542 | |
| 543 | VirtualDomain_statusscip-shell shell shell-project 0.0.0 VirtualDomain_status().() { |
| 544 | local try=0 |
| 545 | rc=$OCF_ERR_GENERIC |
| 546 | status="no state" |
| 547 | while [scip-shell system . . `[`. "$status" = "no state" ]; do |
| 548 | try=$(($try + 1 )) |
| 549 | status=$(LANG=C virsh $VIRSH_OPTIONS domstate $DOMAIN_NAME 2>&1 | trscip-shell system . . tr. 'A-Z' 'a-z') |
| 550 | case "$status" in |
| 551 | *"error:"*"domain not found"|*"error:"*"failed to get domain"*|"shut off") |
| 552 | # shut off: domain is defined, but not started, will not happen if |
| 553 | # domain is created but not defined |
| 554 | # "Domain not found" or "failed to get domain": domain is not defined |
| 555 | # and thus not started |
| 556 | ocf_log debug "Virtual domain $DOMAIN_NAME is not running: $(echoscip-shell system . . echo. $status | sedscip-shell system . . sed. s/error://g)" |
| 557 | rc=$OCF_NOT_RUNNING |
| 558 | ;; |
| 559 | running|paused|idle|blocked|"in shutdown") |
| 560 | # running: domain is currently actively consuming cycles |
| 561 | # paused: domain is paused (suspended) |
| 562 | # idle: domain is running but idle |
| 563 | # blocked: synonym for idle used by legacy Xen versions |
| 564 | # in shutdown: the domain is in process of shutting down, but has not completely shutdown or crashed. |
| 565 | ocf_log debug "Virtual domain $DOMAIN_NAME is currently $status." |
| 566 | rc=$OCF_SUCCESS |
| 567 | ;; |
| 568 | ""|*"failed to "*"connect to the hypervisor"*|"no state") |
| 569 | # Empty string may be returned when virsh does not |
| 570 | # receive a reply from libvirtd. |
| 571 | # "no state" may occur when the domain is currently |
| 572 | # being migrated (on the migration target only), or |
| 573 | # whenever virsh can't reliably obtain the domain |
| 574 | # state. |
| 575 | status="no state" |
| 576 | if [scip-shell system . . `[`. "$__OCF_ACTION" = "stop" ] && [scip-shell system . . `[`. $try -ge 3 ]; then |
| 577 | # During the stop operation, we want to bail out |
| 578 | # quickly, so as to be able to force-stop (destroy) |
| 579 | # the domain if necessary. |
| 580 | ocf_exit_reason "Virtual domain $DOMAIN_NAME has no state during stop operation, bailing out." |
| 581 | return $OCF_ERR_GENERIC; |
| 582 | elif [scip-shell system . . `[`. "$__OCF_ACTION" = "monitor" ]; then |
| 583 | pid_statusscip-shell shell shell-project 0.0.0 pid_status(). |
| 584 | rc=$? |
| 585 | if [scip-shell system . . `[`. $rc -ne $OCF_ERR_GENERIC ]; then |
| 586 | # we've successfully determined the domains status outside of libvirt |
| 587 | return $rc |
| 588 | fi |
| 589 | |
| 590 | else |
| 591 | # During all other actions, we just wait and try |
| 592 | # again, relying on the CRM/LRM to time us out if |
| 593 | # this takes too long. |
| 594 | ocf_log info "Virtual domain $DOMAIN_NAME currently has no state, retrying." |
| 595 | fi |
| 596 | sleepscip-shell system . . sleep. 1 |
| 597 | ;; |
| 598 | *) |
| 599 | # any other output is unexpected. |
| 600 | ocf_log error "Virtual domain $DOMAIN_NAME has unknown status \"$status\"!" |
| 601 | sleepscip-shell system . . sleep. 1 |
| 602 | ;; |
| 603 | esac |
| 604 | done |
| 605 | return $rc |
| 606 | } |
| 607 | |
| 608 | # virsh undefine removes configuration files if they are in |
| 609 | # directories which are managed by libvirt. such directories |
| 610 | # include also subdirectories of /etc (for instance |
| 611 | # /etc/libvirt/*) which may be surprising. VirtualDomain didn't |
| 612 | # include the undefine call before, hence this wasn't an issue |
| 613 | # before. |
| 614 | # |
| 615 | # There seems to be no way to find out which directories are |
| 616 | # managed by libvirt. |
| 617 | # |
| 618 | verify_undefinedscip-shell shell shell-project 0.0.0 verify_undefined().() { |
| 619 | local tmpf |
| 620 | if virsh --connect=${OCF_RESKEY_hypervisor} list --all --name 2>/dev/nullscip-shell filesystem . . `/dev/null`. | grepscip-shell system . . grep. -wqs "$DOMAIN_NAME" |
| 621 | then |
| 622 | tmpf=$(mktempscip-shell system . . mktemp. -t vmcfgsave.XXXXXX) |
| 623 | if [scip-shell system . . `[`. ! -r "$tmpf" ]; then |
| 624 | ocf_log warn "unable to create temp file, disk full?" |
| 625 | # we must undefine the domain |
| 626 | virsh $VIRSH_OPTIONS undefine $DOMAIN_NAME > /dev/nullscip-shell filesystem . . `/dev/null`. 2>&1 |
| 627 | else |
| 628 | cpscip-shell system . . cp. -p $OCF_RESKEY_config $tmpf |
| 629 | virsh $VIRSH_OPTIONS undefine $DOMAIN_NAME > /dev/nullscip-shell filesystem . . `/dev/null`. 2>&1 |
| 630 | [scip-shell system . . `[`. -f $OCF_RESKEY_config ] || cpscip-shell system . . cp. -f $tmpf $OCF_RESKEY_config |
| 631 | rmscip-shell system . . rm. -f $tmpf |
| 632 | fi |
| 633 | fi |
| 634 | } |
| 635 | |
| 636 | start_resourcesscip-shell shell shell-project 0.0.0 start_resources().() { |
| 637 | local virsh_opts="--connect=$1 --quiet" |
| 638 | local pool_state net_state |
| 639 | for pool in `sedscip-shell system . . sed. -n "s/^.*pool=['\"]\([^'\"]\+\)['\"].*\$/\1/gp" ${OCF_RESKEY_config} | sortscip-shell system . . sort. | uniqscip-shell system . . uniq.`; do |
| 640 | pool_state=`LANG=C virsh ${virsh_opts} pool-info ${pool} | sedscip-shell system . . sed. -n 's/^State: \+\(.*\)$/\1/gp'` |
| 641 | if [scip-shell system . . `[`. "$pool_state" != "running" ]; then |
| 642 | virsh ${virsh_opts} pool-start $pool |
| 643 | if [scip-shell system . . `[`. $? -ne 0 ]; then |
| 644 | ocf_exit_reason "Failed to start required virtual storage pool ${pool}." |
| 645 | return $OCF_ERR_GENERIC |
| 646 | fi |
| 647 | else |
| 648 | virsh ${virsh_opts} pool-refresh $pool |
| 649 | fi |
| 650 | done |
| 651 | |
| 652 | for net in `sedscip-shell system . . sed. -n "s/^.*network=['\"]\([^'\"]\+\)['\"].*\$/\1/gp" ${OCF_RESKEY_config} | sortscip-shell system . . sort. | uniqscip-shell system . . uniq.`; do |
| 653 | net_state=`LANG=C virsh ${virsh_opts} net-info ${net} | sedscip-shell system . . sed. -n 's/^Active: \+\(.*\)$/\1/gp'` |
| 654 | if [scip-shell system . . `[`. "$net_state" != "yes" ]; then |
| 655 | virsh ${virsh_opts} net-start $net |
| 656 | if [scip-shell system . . `[`. $? -ne 0 ]; then |
| 657 | ocf_exit_reason "Failed to start required virtual network ${net}." |
| 658 | return $OCF_ERR_GENERIC |
| 659 | fi |
| 660 | fi |
| 661 | done |
| 662 | |
| 663 | return $OCF_SUCCESS |
| 664 | } |
| 665 | |
| 666 | VirtualDomain_startscip-shell shell shell-project 0.0.0 VirtualDomain_start().() { |
| 667 | local snapshotimage |
| 668 | |
| 669 | if VirtualDomain_statusscip-shell shell shell-project 0.0.0 VirtualDomain_status().; then |
| 670 | ocf_log info "Virtual domain $DOMAIN_NAME already running." |
| 671 | return $OCF_SUCCESS |
| 672 | fi |
| 673 | |
| 674 | # systemd drop-in to stop domain before libvirtd terminates services |
| 675 | # during shutdown/reboot |
| 676 | if systemd_is_running ; then |
| 677 | systemd_drop_in "99-VirtualDomain-libvirt" "After" "libvirtd.service" |
| 678 | systemd_drop_in "99-VirtualDomain-machines" "Wants" "virt-guest-shutdown.target" |
| 679 | systemctl start virt-guest-shutdown.target |
| 680 | fi |
| 681 | |
| 682 | snapshotimage="$OCF_RESKEY_snapshot/${DOMAIN_NAME}.state" |
| 683 | if [scip-shell system . . `[`. -n "$OCF_RESKEY_snapshot" -a -f "$snapshotimage" ]; then |
| 684 | virsh restore $snapshotimage |
| 685 | if [scip-shell system . . `[`. $? -eq 0 ]; then |
| 686 | rmscip-shell system . . rm. -f $snapshotimage |
| 687 | return $OCF_SUCCESS |
| 688 | fi |
| 689 | ocf_exit_reason "Failed to restore ${DOMAIN_NAME} from state file in ${OCF_RESKEY_snapshot} directory." |
| 690 | return $OCF_ERR_GENERIC |
| 691 | fi |
| 692 | |
| 693 | # Make sure domain is undefined before creating. |
| 694 | # The 'create' command guarantees that the domain will be |
| 695 | # undefined on shutdown, but requires the domain to be undefined. |
| 696 | # if a user defines the domain |
| 697 | # outside of this agent, we have to ensure that the domain |
| 698 | # is restored to an 'undefined' state before creating. |
| 699 | verify_undefinedscip-shell shell shell-project 0.0.0 verify_undefined(). |
| 700 | |
| 701 | if ocf_is_true "${OCF_RESKEY_start_resources}"; then |
| 702 | start_resourcesscip-shell shell shell-project 0.0.0 start_resources(). ${OCF_RESKEY_hypervisor} |
| 703 | rc=$? |
| 704 | if [scip-shell system . . `[`. $rc -eq $OCF_ERR_GENERIC ]; then |
| 705 | return $rc |
| 706 | fi |
| 707 | fi |
| 708 | |
| 709 | if [scip-shell system . . `[`. -z "${OCF_RESKEY_backingfile}" ]; then |
| 710 | virsh $VIRSH_OPTIONS create ${OCF_RESKEY_config} |
| 711 | if [scip-shell system . . `[`. $? -ne 0 ]; then |
| 712 | ocf_exit_reason "Failed to start virtual domain ${DOMAIN_NAME}." |
| 713 | return $OCF_ERR_GENERIC |
| 714 | fi |
| 715 | else |
| 716 | if ocf_is_true "${OCF_RESKEY_stateless}" || [scip-shell system . . `[`. ! -s "${OCF_RESKEY_config%%.*}.qcow2" ]; then |
| 717 | # Create the Stateless image |
| 718 | dirconfig=`dirnamescip-shell system . . dirname. ${OCF_RESKEY_config}` |
| 719 | qemu-img create -f qcow2 -b ${OCF_RESKEY_backingfile} ${OCF_RESKEY_config%%.*}.qcow2 |
| 720 | if [scip-shell system . . `[`. $? -ne 0 ]; then |
| 721 | ocf_exit_reason "Failed qemu-img create ${DOMAIN_NAME} with backing file ${OCF_RESKEY_backingfile}." |
| 722 | return $OCF_ERR_GENERIC |
| 723 | fi |
| 724 | |
| 725 | virsh define ${OCF_RESKEY_config} |
| 726 | if [scip-shell system . . `[`. $? -ne 0 ]; then |
| 727 | ocf_exit_reason "Failed to define virtual domain ${DOMAIN_NAME}." |
| 728 | return $OCF_ERR_GENERIC |
| 729 | fi |
| 730 | |
| 731 | if [scip-shell system . . `[`. -n "${OCF_RESKEY_copyindirs}" ]; then |
| 732 | # Inject copyindirs directories and files |
| 733 | virt-copy-in -d ${DOMAIN_NAME} ${OCF_RESKEY_copyindirs} /scip-shell filesystem . . `/`. |
| 734 | if [scip-shell system . . `[`. $? -ne 0 ]; then |
| 735 | ocf_exit_reason "Failed on virt-copy-in command ${DOMAIN_NAME}." |
| 736 | return $OCF_ERR_GENERIC |
| 737 | fi |
| 738 | fi |
| 739 | else |
| 740 | virsh define ${OCF_RESKEY_config} |
| 741 | if [scip-shell system . . `[`. $? -ne 0 ]; then |
| 742 | ocf_exit_reason "Failed to define virtual domain ${DOMAIN_NAME}." |
| 743 | return $OCF_ERR_GENERIC |
| 744 | fi |
| 745 | fi |
| 746 | |
| 747 | virsh $VIRSH_OPTIONS start ${DOMAIN_NAME} |
| 748 | if [scip-shell system . . `[`. $? -ne 0 ]; then |
| 749 | ocf_exit_reason "Failed to start virtual domain ${DOMAIN_NAME}." |
| 750 | return $OCF_ERR_GENERIC |
| 751 | fi |
| 752 | fi |
| 753 | |
| 754 | while ! VirtualDomain_monitorscip-shell shell shell-project 0.0.0 VirtualDomain_monitor().; do |
| 755 | sleepscip-shell system . . sleep. 1 |
| 756 | done |
| 757 | |
| 758 | return $OCF_SUCCESS |
| 759 | } |
| 760 | |
| 761 | force_stopscip-shell shell shell-project 0.0.0 force_stop().() |
| 762 | { |
| 763 | local out ex translate |
| 764 | local status=0 |
| 765 | |
| 766 | ocf_log info "Issuing forced shutdown (destroy) request for domain ${DOMAIN_NAME}." |
| 767 | out=$(LANG=C virsh $VIRSH_OPTIONS destroy ${DOMAIN_NAME} 2>&1) |
| 768 | ex=$? |
| 769 | translate=$(echoscip-shell system . . echo. $out|trscip-shell system . . tr. 'A-Z' 'a-z') |
| 770 | echoscip-shell system . . echo. >&2 "$translate" |
| 771 | case $ex$translate in |
| 772 | *"error:"*"domain is not running"*|*"error:"*"domain not found"*|\ |
| 773 | *"error:"*"failed to get domain"*) |
| 774 | : ;; # unexpected path to the intended outcome, all is well |
| 775 | [!0]*) |
| 776 | ocf_exit_reason "forced stop failed" |
| 777 | return $OCF_ERR_GENERIC ;; |
| 778 | 0*) |
| 779 | while [scip-shell system . . `[`. $status != $OCF_NOT_RUNNING ]; do |
| 780 | VirtualDomain_statusscip-shell shell shell-project 0.0.0 VirtualDomain_status(). |
| 781 | status=$? |
| 782 | done ;; |
| 783 | esac |
| 784 | return $OCF_SUCCESS |
| 785 | } |
| 786 | |
| 787 | sync_configscip-shell shell shell-project 0.0.0 sync_config().(){ |
| 788 | ocf_log info "Syncing $DOMAIN_NAME config file with csync2 -x ${OCF_RESKEY_config}" |
| 789 | if ! csync2 -x ${OCF_RESKEY_config}; then |
| 790 | ocf_log warn "Syncing ${OCF_RESKEY_config} failed."; |
| 791 | fi |
| 792 | } |
| 793 | |
| 794 | save_configscip-shell shell shell-project 0.0.0 save_config().(){ |
| 795 | CFGTMP=$(mktempscip-shell system . . mktemp. -t vmcfgsave.XXX) |
| 796 | virsh $VIRSH_OPTIONS dumpxml --inactive --security-info ${DOMAIN_NAME} > ${CFGTMP} |
| 797 | if [scip-shell system . . `[`. -s ${CFGTMP} ]; then |
| 798 | if ! cmpscip-shell system . . cmp. -s ${CFGTMP} ${OCF_RESKEY_config}; then |
| 799 | if virt-xml-validate ${CFGTMP} domain 2>/dev/nullscip-shell filesystem . . `/dev/null`. ; then |
| 800 | ocf_log info "Saving domain $DOMAIN_NAME to ${OCF_RESKEY_config}. Please make sure it's present on all nodes or sync_config_on_stop is on." |
| 801 | if catscip-shell system . . cat. ${CFGTMP} > ${OCF_RESKEY_config} ; then |
| 802 | ocf_log info "Saved $DOMAIN_NAME domain's configuration to ${OCF_RESKEY_config}." |
| 803 | if ocf_is_true "$OCF_RESKEY_sync_config_on_stop"; then |
| 804 | sync_configscip-shell shell shell-project 0.0.0 sync_config(). |
| 805 | fi |
| 806 | else |
| 807 | ocf_log warn "Moving ${CFGTMP} to ${OCF_RESKEY_config} failed." |
| 808 | fi |
| 809 | else |
| 810 | ocf_log warn "Domain $DOMAIN_NAME config failed to validate after dump. Skipping config update." |
| 811 | fi |
| 812 | fi |
| 813 | else |
| 814 | ocf_log warn "Domain $DOMAIN_NAME config has 0 size. Skipping config update." |
| 815 | fi |
| 816 | rmscip-shell system . . rm. -f ${CFGTMP} |
| 817 | } |
| 818 | |
| 819 | VirtualDomain_stopscip-shell shell shell-project 0.0.0 VirtualDomain_stop().() { |
| 820 | local i |
| 821 | local status |
| 822 | local shutdown_timeout |
| 823 | local needshutdown=1 |
| 824 | |
| 825 | VirtualDomain_statusscip-shell shell shell-project 0.0.0 VirtualDomain_status(). |
| 826 | status=$? |
| 827 | |
| 828 | case $status in |
| 829 | $OCF_SUCCESS) |
| 830 | if ocf_is_true $OCF_RESKEY_force_stop; then |
| 831 | # if force stop, don't bother attempting graceful shutdown. |
| 832 | force_stopscip-shell shell shell-project 0.0.0 force_stop(). |
| 833 | return $? |
| 834 | fi |
| 835 | |
| 836 | ocf_log info "Issuing graceful shutdown request for domain ${DOMAIN_NAME}." |
| 837 | |
| 838 | if [scip-shell system . . `[`. -n "$OCF_RESKEY_snapshot" ]; then |
| 839 | virsh save $DOMAIN_NAME "$OCF_RESKEY_snapshot/${DOMAIN_NAME}.state" |
| 840 | if [scip-shell system . . `[`. $? -eq 0 ]; then |
| 841 | needshutdown=0 |
| 842 | else |
| 843 | ocf_log error "Failed to save snapshot state of ${DOMAIN_NAME} on stop" |
| 844 | fi |
| 845 | fi |
| 846 | |
| 847 | # save config if needed |
| 848 | if ocf_is_true "$OCF_RESKEY_save_config_on_stop"; then |
| 849 | save_configscip-shell shell shell-project 0.0.0 save_config(). |
| 850 | fi |
| 851 | |
| 852 | # issue the shutdown if save state didn't shutdown for us |
| 853 | if [scip-shell system . . `[`. $needshutdown -eq 1 ]; then |
| 854 | # Issue a graceful shutdown request |
| 855 | if [scip-shell system . . `[`. -n "${OCF_RESKEY_CRM_shutdown_mode}" ]; then |
| 856 | shutdown_opts="--mode ${OCF_RESKEY_CRM_shutdown_mode}" |
| 857 | fi |
| 858 | virsh $VIRSH_OPTIONS shutdown ${DOMAIN_NAME} $shutdown_opts |
| 859 | fi |
| 860 | |
| 861 | # The "shutdown_timeout" we use here is the operation |
| 862 | # timeout specified in the CIB, minus 5 seconds |
| 863 | shutdown_timeout=$(( $NOW + ($OCF_RESKEY_CRM_meta_timeout/1000) -5 )) |
| 864 | # Loop on status until we reach $shutdown_timeout |
| 865 | while [scip-shell system . . `[`. $NOW -lt $shutdown_timeout ]; do |
| 866 | VirtualDomain_statusscip-shell shell shell-project 0.0.0 VirtualDomain_status(). |
| 867 | status=$? |
| 868 | case $status in |
| 869 | $OCF_NOT_RUNNING) |
| 870 | # This was a graceful shutdown. |
| 871 | return $OCF_SUCCESS |
| 872 | ;; |
| 873 | $OCF_SUCCESS) |
| 874 | # Domain is still running, keep |
| 875 | # waiting (until shutdown_timeout |
| 876 | # expires) |
| 877 | sleepscip-shell system . . sleep. 1 |
| 878 | ;; |
| 879 | *) |
| 880 | # Something went wrong. Bail out and |
| 881 | # resort to forced stop (destroy). |
| 882 | break; |
| 883 | esac |
| 884 | NOW=$(datescip-shell system . . date. +%s) |
| 885 | done |
| 886 | ;; |
| 887 | $OCF_NOT_RUNNING) |
| 888 | ocf_log info "Domain $DOMAIN_NAME already stopped." |
| 889 | return $OCF_SUCCESS |
| 890 | esac |
| 891 | |
| 892 | # OK. Now if the above graceful shutdown hasn't worked, kill |
| 893 | # off the domain with destroy. If that too does not work, |
| 894 | # have the LRM time us out. |
| 895 | force_stopscip-shell shell shell-project 0.0.0 force_stop(). |
| 896 | } |
| 897 | |
| 898 | mk_migrateuriscip-shell shell shell-project 0.0.0 mk_migrateuri().() { |
| 899 | local target_node |
| 900 | local migrate_target |
| 901 | local hypervisor |
| 902 | |
| 903 | target_node="$OCF_RESKEY_CRM_meta_migrate_target" |
| 904 | |
| 905 | # A typical migration URI via a special migration network looks |
| 906 | # like "tcp://bar-mig:49152". The port would be randomly chosen |
| 907 | # by libvirt from the range 49152-49215 if omitted, at least since |
| 908 | # version 0.7.4 ... |
| 909 | if [scip-shell system . . `[`. -n "${OCF_RESKEY_migration_network_suffix}" ]; then |
| 910 | hypervisor="${OCF_RESKEY_hypervisor%%[+:]*}" |
| 911 | # Hostname might be a FQDN |
| 912 | migrate_target=$(echoscip-shell system . . echo. ${target_node} | sedscip-shell system . . sed. -e "s,^\([^.]\+\),\1${OCF_RESKEY_migration_network_suffix},") |
| 913 | case $hypervisor in |
| 914 | qemu) |
| 915 | # For quiet ancient libvirt versions a migration port is needed |
| 916 | # and the URI must not contain the "//". Newer versions can handle |
| 917 | # the "bad" URI. |
| 918 | echoscip-shell system . . echo. "tcp:${migrate_target}:${OCF_RESKEY_migrateport}" |
| 919 | ;; |
| 920 | xen) |
| 921 | echoscip-shell system . . echo. "${migrate_target}" |
| 922 | ;; |
| 923 | *) |
| 924 | ocf_log warn "$DOMAIN_NAME: Migration via dedicated network currently not supported for ${hypervisor}." |
| 925 | ;; |
| 926 | esac |
| 927 | fi |
| 928 | } |
| 929 | |
| 930 | VirtualDomain_migrate_toscip-shell shell shell-project 0.0.0 VirtualDomain_migrate_to().() { |
| 931 | local rc |
| 932 | local target_node |
| 933 | local remoteuri |
| 934 | local transport_suffix |
| 935 | local migrateuri |
| 936 | local migrate_opts |
| 937 | local migrate_pid |
| 938 | |
| 939 | target_node="$OCF_RESKEY_CRM_meta_migrate_target" |
| 940 | |
| 941 | if VirtualDomain_statusscip-shell shell shell-project 0.0.0 VirtualDomain_status().; then |
| 942 | # Find out the remote hypervisor to connect to. That is, turn |
| 943 | # something like "qemu://foo:9999/system" into |
| 944 | # "qemu+tcp://bar:9999/system" |
| 945 | |
| 946 | if [scip-shell system . . `[`. -n "${OCF_RESKEY_remoteuri}" ]; then |
| 947 | remoteuri=`echoscip-shell system . . echo. "${OCF_RESKEY_remoteuri}" | |
| 948 | sedscip-shell system . . sed. "s/%n/$target_node/g"` |
| 949 | else |
| 950 | if [scip-shell system . . `[`. -n "${OCF_RESKEY_migration_transport}" ]; then |
| 951 | transport_suffix="+${OCF_RESKEY_migration_transport}" |
| 952 | fi |
| 953 | |
| 954 | # append user defined suffix if virsh target should differ from cluster node name |
| 955 | if [scip-shell system . . `[`. -n "${OCF_RESKEY_migration_network_suffix}" ]; then |
| 956 | # Hostname might be a FQDN |
| 957 | target_node=$(echoscip-shell system . . echo. ${target_node} | sedscip-shell system . . sed. -e "s,^\([^.]\+\),\1${OCF_RESKEY_migration_network_suffix},") |
| 958 | fi |
| 959 | |
| 960 | # a remote user has been defined to connect to target_node |
| 961 | if echoscip-shell system . . echo. ${OCF_RESKEY_migration_user} | grepscip-shell system . . grep. -q "^[a-z][-a-z0-9]*$" ; then |
| 962 | target_node="${OCF_RESKEY_migration_user}@${target_node}" |
| 963 | fi |
| 964 | |
| 965 | # Scared of that sed expression? So am I. :-) |
| 966 | remoteuri=$(echoscip-shell system . . echo. ${OCF_RESKEY_hypervisor} | sedscip-shell system . . sed. -e "s,\(.*\)://[^/:]*\(:\?[0-9]*\)/\(.*\),\1${transport_suffix}://${target_node}\2/\3,") |
| 967 | fi |
| 968 | |
| 969 | # User defined migrateuri or do we make one? |
| 970 | migrate_opts="$OCF_RESKEY_migrate_options" |
| 971 | |
| 972 | # migration_uri is directly set |
| 973 | if [scip-shell system . . `[`. -n "${OCF_RESKEY_migrateuri}" ]; then |
| 974 | migrateuri=`echoscip-shell system . . echo. "${OCF_RESKEY_migrateuri}" | |
| 975 | sedscip-shell system . . sed. "s/%n/$target_node/g"` |
| 976 | |
| 977 | # extract migrationuri from options |
| 978 | elif echoscip-shell system . . echo. "$migrate_opts" | $FGREP -qs -- "--migrateuri="; then |
| 979 | migrateuri=`echoscip-shell system . . echo. "$migrate_opts" | |
| 980 | sedscip-shell system . . sed. "s/.*--migrateuri=\([^ ]*\).*/\1/;s/%n/$target_node/g"` |
| 981 | |
| 982 | # auto generate |
| 983 | else |
| 984 | migrateuri=`mk_migrateuriscip-shell shell shell-project 0.0.0 mk_migrateuri().` |
| 985 | fi |
| 986 | |
| 987 | # remove --migrateuri from migration_opts |
| 988 | migrate_opts=`echoscip-shell system . . echo. "$migrate_opts" | |
| 989 | sedscip-shell system . . sed. "s/\(.*\)--migrateuri=[^ ]*\(.*\)/\1\2/"` |
| 990 | |
| 991 | |
| 992 | # save config if needed |
| 993 | if ocf_is_true "$OCF_RESKEY_save_config_on_stop"; then |
| 994 | save_configscip-shell shell shell-project 0.0.0 save_config(). |
| 995 | fi |
| 996 | |
| 997 | if ocf_is_true "${OCF_RESKEY_start_resources}"; then |
| 998 | start_resourcesscip-shell shell shell-project 0.0.0 start_resources(). $remoteuri |
| 999 | rc=$? |
| 1000 | if [scip-shell system . . `[`. $rc -eq $OCF_ERR_GENERIC ]; then |
| 1001 | return $rc |
| 1002 | fi |
| 1003 | fi |
| 1004 | |
| 1005 | # Live migration speed limit |
| 1006 | if [scip-shell system . . `[`. ${OCF_RESKEY_migration_speed} -ne 0 ]; then |
| 1007 | ocf_log info "$DOMAIN_NAME: Setting live migration speed limit for $DOMAIN_NAME (using: virsh ${VIRSH_OPTIONS} migrate-setspeed $DOMAIN_NAME ${OCF_RESKEY_migration_speed})." |
| 1008 | virsh ${VIRSH_OPTIONS} migrate-setspeed $DOMAIN_NAME ${OCF_RESKEY_migration_speed} |
| 1009 | fi |
| 1010 | |
| 1011 | # OK, we know where to connect to. Now do the actual migration. |
| 1012 | ocf_log info "$DOMAIN_NAME: Starting live migration to ${target_node} (using: virsh ${VIRSH_OPTIONS} migrate --live $migrate_opts $DOMAIN_NAME $remoteuri $migrateuri)." |
| 1013 | virsh ${VIRSH_OPTIONS} migrate --live $migrate_opts $DOMAIN_NAME $remoteuri $migrateuri & |
| 1014 | |
| 1015 | migrate_pid=${!} |
| 1016 | |
| 1017 | # Live migration downtime interval |
| 1018 | # Note: You can set downtime only while live migration is in progress |
| 1019 | if [scip-shell system . . `[`. ${OCF_RESKEY_migration_downtime} -ne 0 ]; then |
| 1020 | sleepscip-shell system . . sleep. 2 |
| 1021 | ocf_log info "$DOMAIN_NAME: Setting live migration downtime for $DOMAIN_NAME (using: virsh ${VIRSH_OPTIONS} migrate-setmaxdowntime $DOMAIN_NAME ${OCF_RESKEY_migration_downtime})." |
| 1022 | virsh ${VIRSH_OPTIONS} migrate-setmaxdowntime $DOMAIN_NAME ${OCF_RESKEY_migration_downtime} |
| 1023 | fi |
| 1024 | |
| 1025 | wait ${migrate_pid} |
| 1026 | |
| 1027 | rc=$? |
| 1028 | if [scip-shell system . . `[`. $rc -ne 0 ]; then |
| 1029 | ocf_exit_reason "$DOMAIN_NAME: live migration to ${target_node} failed: $rc" |
| 1030 | return $OCF_ERR_GENERIC |
| 1031 | else |
| 1032 | ocf_log info "$DOMAIN_NAME: live migration to ${target_node} succeeded." |
| 1033 | return $OCF_SUCCESS |
| 1034 | fi |
| 1035 | else |
| 1036 | ocf_exit_reason "$DOMAIN_NAME: migrate_to: Not active locally!" |
| 1037 | return $OCF_ERR_GENERIC |
| 1038 | fi |
| 1039 | } |
| 1040 | |
| 1041 | VirtualDomain_migrate_fromscip-shell shell shell-project 0.0.0 VirtualDomain_migrate_from().() { |
| 1042 | # systemd drop-in to stop domain before libvirtd terminates services |
| 1043 | # during shutdown/reboot |
| 1044 | if systemd_is_running ; then |
| 1045 | systemd_drop_in "99-VirtualDomain-libvirt" "After" "libvirtd.service" |
| 1046 | systemd_drop_in "99-VirtualDomain-machines" "Wants" "virt-guest-shutdown.target" |
| 1047 | systemctl start virt-guest-shutdown.target |
| 1048 | fi |
| 1049 | |
| 1050 | while ! VirtualDomain_monitorscip-shell shell shell-project 0.0.0 VirtualDomain_monitor().; do |
| 1051 | sleepscip-shell system . . sleep. 1 |
| 1052 | done |
| 1053 | ocf_log info "$DOMAIN_NAME: live migration from ${OCF_RESKEY_CRM_meta_migrate_source} succeeded." |
| 1054 | # save config if needed |
| 1055 | if ocf_is_true "$OCF_RESKEY_save_config_on_stop"; then |
| 1056 | save_configscip-shell shell shell-project 0.0.0 save_config(). |
| 1057 | fi |
| 1058 | return $OCF_SUCCESS |
| 1059 | } |
| 1060 | |
| 1061 | VirtualDomain_monitorscip-shell shell shell-project 0.0.0 VirtualDomain_monitor().() { |
| 1062 | # First, check the domain status. If that returns anything other |
| 1063 | # than $OCF_SUCCESS, something is definitely wrong. |
| 1064 | VirtualDomain_statusscip-shell shell shell-project 0.0.0 VirtualDomain_status(). |
| 1065 | rc=$? |
| 1066 | if [scip-shell system . . `[`. ${rc} -eq ${OCF_SUCCESS} ]; then |
| 1067 | # OK, the generic status check turned out fine. Now, if we |
| 1068 | # have monitor scripts defined, run them one after another. |
| 1069 | for script in ${OCF_RESKEY_monitor_scripts}; do |
| 1070 | script_output="$($script 2>&1)" |
| 1071 | script_rc=$? |
| 1072 | if [scip-shell system . . `[`. ${script_rc} -ne ${OCF_SUCCESS} ]; then |
| 1073 | # A monitor script returned a non-success exit |
| 1074 | # code. Stop iterating over the list of scripts, log a |
| 1075 | # warning message, and propagate $OCF_ERR_GENERIC. |
| 1076 | ocf_exit_reason "Monitor command \"${script}\" for domain ${DOMAIN_NAME} returned ${script_rc} with output: ${script_output}" |
| 1077 | rc=$OCF_ERR_GENERIC |
| 1078 | break |
| 1079 | else |
| 1080 | ocf_log debug "Monitor command \"${script}\" for domain ${DOMAIN_NAME} completed successfully with output: ${script_output}" |
| 1081 | fi |
| 1082 | done |
| 1083 | fi |
| 1084 | |
| 1085 | update_emulator_cachescip-shell shell shell-project 0.0.0 update_emulator_cache(). |
| 1086 | update_utilizationscip-shell shell shell-project 0.0.0 update_utilization(). |
| 1087 | # Save configuration on monitor as well, so we will have a better chance of |
| 1088 | # having fresh and up to date config files on all nodes. |
| 1089 | if ocf_is_true "$OCF_RESKEY_save_config_on_stop"; then |
| 1090 | save_configscip-shell shell shell-project 0.0.0 save_config(). |
| 1091 | fi |
| 1092 | |
| 1093 | return ${rc} |
| 1094 | } |
| 1095 | |
| 1096 | VirtualDomain_validate_allscip-shell shell shell-project 0.0.0 VirtualDomain_validate_all().() { |
| 1097 | if ocf_is_true $OCF_RESKEY_force_stop && [scip-shell system . . `[`. -n "$OCF_RESKEY_snapshot" ]; then |
| 1098 | ocf_exit_reason "The 'force_stop' and 'snapshot' options can not be used together." |
| 1099 | return $OCF_ERR_CONFIGURED |
| 1100 | fi |
| 1101 | |
| 1102 | # check if we can read the config file (otherwise we're unable to |
| 1103 | # deduce $DOMAIN_NAME from it, see below) |
| 1104 | if [scip-shell system . . `[`. ! -r $OCF_RESKEY_config ]; then |
| 1105 | if ocf_is_probe; then |
| 1106 | ocf_log info "Configuration file $OCF_RESKEY_config not readable during probe." |
| 1107 | elif [scip-shell system . . `[`. "$__OCF_ACTION" = "stop" ]; then |
| 1108 | ocf_log info "Configuration file $OCF_RESKEY_config not readable, resource considered stopped." |
| 1109 | else |
| 1110 | ocf_exit_reason "Configuration file $OCF_RESKEY_config does not exist or not readable." |
| 1111 | fi |
| 1112 | return $OCF_ERR_INSTALLED |
| 1113 | fi |
| 1114 | |
| 1115 | if [scip-shell system . . `[`. -z $DOMAIN_NAME ]; then |
| 1116 | ocf_exit_reason "Unable to determine domain name." |
| 1117 | return $OCF_ERR_INSTALLED |
| 1118 | fi |
| 1119 | |
| 1120 | # Check if csync2 is available when config tells us we might need it. |
| 1121 | if ocf_is_true $OCF_RESKEY_sync_config_on_stop; then |
| 1122 | check_binary csync2 |
| 1123 | fi |
| 1124 | |
| 1125 | # Check if migration_speed is a decimal value |
| 1126 | if ! ocf_is_decimal ${OCF_RESKEY_migration_speed}; then |
| 1127 | ocf_exit_reason "migration_speed has to be a decimal value" |
| 1128 | return $OCF_ERR_CONFIGURED |
| 1129 | fi |
| 1130 | |
| 1131 | # Check if migration_downtime is a decimal value |
| 1132 | if ! ocf_is_decimal ${OCF_RESKEY_migration_downtime}; then |
| 1133 | ocf_exit_reason "migration_downtime has to be a decimal value" |
| 1134 | return $OCF_ERR_CONFIGURED |
| 1135 | fi |
| 1136 | |
| 1137 | if ocf_is_true "${OCF_RESKEY_stateless}" && [scip-shell system . . `[`. -z "${OCF_RESKEY_backingfile}" ]; then |
| 1138 | ocf_exit_reason "Stateless functionality can't be achieved without a backing file." |
| 1139 | return $OCF_ERR_CONFIGURED |
| 1140 | fi |
| 1141 | } |
| 1142 | |
| 1143 | VirtualDomain_getconfigscip-shell shell shell-project 0.0.0 VirtualDomain_getconfig().() { |
| 1144 | # Grab the virsh uri default, but only if hypervisor isn't set |
| 1145 | : ${OCF_RESKEY_hypervisor=$(virsh --quiet uri 2>/dev/nullscip-shell filesystem . . `/dev/null`.)} |
| 1146 | |
| 1147 | # Set options to be passed to virsh: |
| 1148 | VIRSH_OPTIONS="--connect=${OCF_RESKEY_hypervisor} --quiet" |
| 1149 | |
| 1150 | # Retrieve the domain name from the xml file. |
| 1151 | DOMAIN_NAME=`$EGREP '[[:space:]]*<name>.*</name>[[:space:]]*$' ${OCF_RESKEY_config} 2>/dev/nullscip-shell filesystem . . `/dev/null`. | sedscip-shell system . . sed. -e 's/[[:space:]]*<name>\(.*\)<\/name>[[:space:]]*$/\1/'` |
| 1152 | |
| 1153 | EMULATOR_STATE="${HA_RSCTMP}/VirtualDomain-${DOMAIN_NAME}-emu.state" |
| 1154 | } |
| 1155 | |
| 1156 | OCF_REQUIRED_PARAMS="config" |
| 1157 | OCF_REQUIRED_BINARIES="virsh sed" |
| 1158 | ocf_rarun $* |