📜 ⬆️ ⬇️

VMware Server :: manage your car park

Hello ;)

Continuing ... (who missed a step back )
I wonder how you manage virtual machines ?!
Every time you take a snapshot, pause or turn off the machine (s), do you spend time on it ?!

Then we go to you! :)
')

I will try to tell you how I personally automated the work with virtual machines.
To many, this will seem utterly stupid, but this solution and IT were found by the hands of the user, not the developer.

Let's start with the most painful, with the shutdown. Why from him, but because usually there is never enough time for him, always somewhere late. Well, or, for example, you leave it to a colleague, but he, by forgetfulness, does not turn it off.
I do not know about the others, but it has been proved in my practice: if you put the machine into self-shutdown mode ( On host shutdown mode - Shut down guest operating system ), then the probability of incorrect shutdown is very high. Vmware just does not have time to stop everything correctly.
If someone has a desire, you can experience: we take a server, run several machines with the FreeBSD OS, run load tests on them, turn off the OS under which the VM is spinning. We get fsck running on FreeBSD on new startup.
Total: in such situations, I recommend VMware to pause. When removing from a pause, you just have to synchronize time.

Do you know?!: With a dense load of physical servers by virtual machines, time is slowing down on each of them.

We turn to the most interesting.
The implementation of the script to automatically switch to pause mode, all running machines.

Do you know?!: Under VMware, you can develop machine control scripts.
More details can be found at http://www.vmware.com/support/pubs/server_pubs.html

Before you is a modified example script from the distribution of the VMware distribution (on which it is written it says it all):

Primitive algorithm of work:
1. Check all registered cars (defined in invetory).
1.1. The machine is working.
1.1.1 pause
1.1.2 Check whether it is up or not.

check_vm.vbs - main script
'
' VmCOM VBScript Sample Script (sample2)
' Copyright 1998 VMware, Inc. All rights reserved. -- VMware Confidential
'
' Permission is hereby granted, free of charge, to any person obtaining a
' copy of the software in this file (the "Software"), to deal in the
' Software without restriction, including without limitation the rights to
' use, copy, modify, merge, publish, distribute, sublicense, and/or sell
' copies of the Software, and to permit persons to whom the Software is
' furnished to do so, subject to the following conditions:
'
' The above copyright notice and this permission notice shall be included in
' all copies or substantial portions of the Software.
'
' The names "VMware" and "VMware, Inc." must not be used to endorse or
' promote products derived from the Software without the prior written
' permission of VMware, Inc.
'
' Products derived from the Software may not be called "VMware", nor may
' "VMware" appear in their name, without the prior written permission of
' VMware, Inc.
'
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
' IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
' FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
' VMWARE,INC. BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
' IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
' CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
'
' ------
'
' This program is for educational purposes only.
' It is not to be used in production environments.
'
' Description:
'
' This script displays the virtual machines on the local server.
' It prints the configuration file path and current execution
' state of each VM. If a VM is in the stuck state, the current
' question and its choices are also printed.
' Additionally, if a VM is stuck on an undoable disk related
' question, the script automatically answers 'Keep' on a power-off
' and 'Append' on a power-on.
'
' NOTE: the question-answering logic used is language and product
' dependent, and is only provided for illustration purposes only!
'
' Instructions for Windows 2000 and later operating systems:
'
' - save the contents of this file to a file named 'sample2.vbs'
' unless it's already named that way
'
' - there should be an accompanying file named 'sample2.wsf'
' It is placed in the same directory as this file during
' product installation. This file is responsible for setting
' up the Windows Script Host environment and loading the
' VmCOM type library, thereby enabling this script to
' reference symbolic constants such as vmExecutionState_On
'
' - in a command line window, type:
' cscript //nologo sample2.wsf
'

Set cp = CreateObject( "VmCOM.VmConnectParams" )
Set server = CreateObject( "VmCOM.VmServerCtl" )

server.Connect cp
Set vmCollection = server.RegisteredVmNames

for each vmName in vmCollection
Set vm = CreateObject( "VmCOM.VmCtl" )
s = "path=" & vmName
On error resume next ' Clear error object
vm.Connect cp,vmName
if err.Number = vmErr_VMBUSY then
s = s & " UNAVAILABLE (controlled by local console)"
elseif err.Number <> 0 then
s = s & " ERROR CONNECTING desc='" & err.Description & "'"
else
On error goto 0 'Make errors fatal past this point
s = s & " state=" & State2Str(vm) & " os=" & vm.Config( "guestos" )
if vm.ExecutionState = vmExecutionState_Stuck then
Set q = vm.PendingQuestion
Set choices = q.choices
s = s & " question= '" & q.text & "' choices="
for each choice in choices
s = s & "[" & choice & "] "
next

' If this looks like an undoable disk save question,
' automatically answer 'Append' or 'Keep'
'
' NOTE: this code makes alot of assumptions about the product
' and the language used, and may break under some environments.
' It is shown for illustration purposes only!

Set r = new RegExp
r.pattern = "undoable disk"
r.ignorecase = True
Set matches = r.Execute(q.text)

if matches.count > 0 then
for i = 1 to choices.count
if choices(i) = "Append" or choices(i) = "Keep" then
WScript.Echo(s)
s = " --> Automatically selecting '" & q.choices(i) & "' as answer"
vm.AnswerQuestion q,i
exit for
end if
next
end if
end if
end if
WScript.Echo(s)
Recheck(vm) '
next

function State2Str(vm)
select case vm.ExecutionState
case vmExecutionState_On
State2Str = "ON"
case vmExecutionState_Off
State2Str = "OFF"
case vmExecutionState_Suspended
State2Str = "SUSPENDED"
case vmExecutionState_Stuck
State2Str = "STUCK"
case else
State2Str = "UNKNOWN"
end select
end function
' ,
function Recheck(vm)
if vm.ExecutionState = vmExecutionState_On then '
vm.Suspend (vmPowerOpMode_Hard) ' , ( )
WScript.Echo "Recheck this VM_machine state=" & State2Str(vm) '
WScript.Sleep(10000) ' ,
end if
end function
* This source code was highlighted with Source Code Highlighter .

'
' VmCOM VBScript Sample Script (sample2)
' Copyright 1998 VMware, Inc. All rights reserved. -- VMware Confidential
'
' Permission is hereby granted, free of charge, to any person obtaining a
' copy of the software in this file (the "Software"), to deal in the
' Software without restriction, including without limitation the rights to
' use, copy, modify, merge, publish, distribute, sublicense, and/or sell
' copies of the Software, and to permit persons to whom the Software is
' furnished to do so, subject to the following conditions:
'
' The above copyright notice and this permission notice shall be included in
' all copies or substantial portions of the Software.
'
' The names "VMware" and "VMware, Inc." must not be used to endorse or
' promote products derived from the Software without the prior written
' permission of VMware, Inc.
'
' Products derived from the Software may not be called "VMware", nor may
' "VMware" appear in their name, without the prior written permission of
' VMware, Inc.
'
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
' IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
' FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
' VMWARE,INC. BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
' IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
' CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
'
' ------
'
' This program is for educational purposes only.
' It is not to be used in production environments.
'
' Description:
'
' This script displays the virtual machines on the local server.
' It prints the configuration file path and current execution
' state of each VM. If a VM is in the stuck state, the current
' question and its choices are also printed.
' Additionally, if a VM is stuck on an undoable disk related
' question, the script automatically answers 'Keep' on a power-off
' and 'Append' on a power-on.
'
' NOTE: the question-answering logic used is language and product
' dependent, and is only provided for illustration purposes only!
'
' Instructions for Windows 2000 and later operating systems:
'
' - save the contents of this file to a file named 'sample2.vbs'
' unless it's already named that way
'
' - there should be an accompanying file named 'sample2.wsf'
' It is placed in the same directory as this file during
' product installation. This file is responsible for setting
' up the Windows Script Host environment and loading the
' VmCOM type library, thereby enabling this script to
' reference symbolic constants such as vmExecutionState_On
'
' - in a command line window, type:
' cscript //nologo sample2.wsf
'

Set cp = CreateObject( "VmCOM.VmConnectParams" )
Set server = CreateObject( "VmCOM.VmServerCtl" )

server.Connect cp
Set vmCollection = server.RegisteredVmNames

for each vmName in vmCollection
Set vm = CreateObject( "VmCOM.VmCtl" )
s = "path=" & vmName
On error resume next ' Clear error object
vm.Connect cp,vmName
if err.Number = vmErr_VMBUSY then
s = s & " UNAVAILABLE (controlled by local console)"
elseif err.Number <> 0 then
s = s & " ERROR CONNECTING desc='" & err.Description & "'"
else
On error goto 0 'Make errors fatal past this point
s = s & " state=" & State2Str(vm) & " os=" & vm.Config( "guestos" )
if vm.ExecutionState = vmExecutionState_Stuck then
Set q = vm.PendingQuestion
Set choices = q.choices
s = s & " question= '" & q.text & "' choices="
for each choice in choices
s = s & "[" & choice & "] "
next

' If this looks like an undoable disk save question,
' automatically answer 'Append' or 'Keep'
'
' NOTE: this code makes alot of assumptions about the product
' and the language used, and may break under some environments.
' It is shown for illustration purposes only!

Set r = new RegExp
r.pattern = "undoable disk"
r.ignorecase = True
Set matches = r.Execute(q.text)

if matches.count > 0 then
for i = 1 to choices.count
if choices(i) = "Append" or choices(i) = "Keep" then
WScript.Echo(s)
s = " --> Automatically selecting '" & q.choices(i) & "' as answer"
vm.AnswerQuestion q,i
exit for
end if
next
end if
end if
end if
WScript.Echo(s)
Recheck(vm) '
next

function State2Str(vm)
select case vm.ExecutionState
case vmExecutionState_On
State2Str = "ON"
case vmExecutionState_Off
State2Str = "OFF"
case vmExecutionState_Suspended
State2Str = "SUSPENDED"
case vmExecutionState_Stuck
State2Str = "STUCK"
case else
State2Str = "UNKNOWN"
end select
end function
' ,
function Recheck(vm)
if vm.ExecutionState = vmExecutionState_On then '
vm.Suspend (vmPowerOpMode_Hard) ' , ( )
WScript.Echo "Recheck this VM_machine state=" & State2Str(vm) '
WScript.Sleep(10000) ' ,
end if
end function
* This source code was highlighted with Source Code Highlighter .

'
' VmCOM VBScript Sample Script (sample2)
' Copyright 1998 VMware, Inc. All rights reserved. -- VMware Confidential
'
' Permission is hereby granted, free of charge, to any person obtaining a
' copy of the software in this file (the "Software"), to deal in the
' Software without restriction, including without limitation the rights to
' use, copy, modify, merge, publish, distribute, sublicense, and/or sell
' copies of the Software, and to permit persons to whom the Software is
' furnished to do so, subject to the following conditions:
'
' The above copyright notice and this permission notice shall be included in
' all copies or substantial portions of the Software.
'
' The names "VMware" and "VMware, Inc." must not be used to endorse or
' promote products derived from the Software without the prior written
' permission of VMware, Inc.
'
' Products derived from the Software may not be called "VMware", nor may
' "VMware" appear in their name, without the prior written permission of
' VMware, Inc.
'
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
' IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
' FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
' VMWARE,INC. BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
' IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
' CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
'
' ------
'
' This program is for educational purposes only.
' It is not to be used in production environments.
'
' Description:
'
' This script displays the virtual machines on the local server.
' It prints the configuration file path and current execution
' state of each VM. If a VM is in the stuck state, the current
' question and its choices are also printed.
' Additionally, if a VM is stuck on an undoable disk related
' question, the script automatically answers 'Keep' on a power-off
' and 'Append' on a power-on.
'
' NOTE: the question-answering logic used is language and product
' dependent, and is only provided for illustration purposes only!
'
' Instructions for Windows 2000 and later operating systems:
'
' - save the contents of this file to a file named 'sample2.vbs'
' unless it's already named that way
'
' - there should be an accompanying file named 'sample2.wsf'
' It is placed in the same directory as this file during
' product installation. This file is responsible for setting
' up the Windows Script Host environment and loading the
' VmCOM type library, thereby enabling this script to
' reference symbolic constants such as vmExecutionState_On
'
' - in a command line window, type:
' cscript //nologo sample2.wsf
'

Set cp = CreateObject( "VmCOM.VmConnectParams" )
Set server = CreateObject( "VmCOM.VmServerCtl" )

server.Connect cp
Set vmCollection = server.RegisteredVmNames

for each vmName in vmCollection
Set vm = CreateObject( "VmCOM.VmCtl" )
s = "path=" & vmName
On error resume next ' Clear error object
vm.Connect cp,vmName
if err.Number = vmErr_VMBUSY then
s = s & " UNAVAILABLE (controlled by local console)"
elseif err.Number <> 0 then
s = s & " ERROR CONNECTING desc='" & err.Description & "'"
else
On error goto 0 'Make errors fatal past this point
s = s & " state=" & State2Str(vm) & " os=" & vm.Config( "guestos" )
if vm.ExecutionState = vmExecutionState_Stuck then
Set q = vm.PendingQuestion
Set choices = q.choices
s = s & " question= '" & q.text & "' choices="
for each choice in choices
s = s & "[" & choice & "] "
next

' If this looks like an undoable disk save question,
' automatically answer 'Append' or 'Keep'
'
' NOTE: this code makes alot of assumptions about the product
' and the language used, and may break under some environments.
' It is shown for illustration purposes only!

Set r = new RegExp
r.pattern = "undoable disk"
r.ignorecase = True
Set matches = r.Execute(q.text)

if matches.count > 0 then
for i = 1 to choices.count
if choices(i) = "Append" or choices(i) = "Keep" then
WScript.Echo(s)
s = " --> Automatically selecting '" & q.choices(i) & "' as answer"
vm.AnswerQuestion q,i
exit for
end if
next
end if
end if
end if
WScript.Echo(s)
Recheck(vm) '
next

function State2Str(vm)
select case vm.ExecutionState
case vmExecutionState_On
State2Str = "ON"
case vmExecutionState_Off
State2Str = "OFF"
case vmExecutionState_Suspended
State2Str = "SUSPENDED"
case vmExecutionState_Stuck
State2Str = "STUCK"
case else
State2Str = "UNKNOWN"
end select
end function
' ,
function Recheck(vm)
if vm.ExecutionState = vmExecutionState_On then '
vm.Suspend (vmPowerOpMode_Hard) ' , ( )
WScript.Echo "Recheck this VM_machine state=" & State2Str(vm) '
WScript.Sleep(10000) ' ,
end if
end function
* This source code was highlighted with Source Code Highlighter .



Next, run the check_vm.wsf script from which control is transferred to the main check_vm.vbs :

<job id= "Check_vm" >
<reference object = "VmCOM.VmCtl" />
<script language= "VBScript" src= "check_vm.vbs" />
</job>

* This source code was highlighted with Source Code Highlighter .

<job id= "Check_vm" >
<reference object = "VmCOM.VmCtl" />
<script language= "VBScript" src= "check_vm.vbs" />
</job>

* This source code was highlighted with Source Code Highlighter .

<job id= "Check_vm" >
<reference object = "VmCOM.VmCtl" />
<script language= "VBScript" src= "check_vm.vbs" />
</job>

* This source code was highlighted with Source Code Highlighter .



Finally, a bat file that you can deliver, for example, 22 hours of work time. With this use, no one will forget to properly turn off your favorite and beloved park.

cscript check_vm.wsf >> log.txt
shutdown -s -f -t 100


In bat, the script is called, with redirection to the log.
Well shutdown, it's like tuning to shutdown. You can try to play with the parameters.

Example log:

path = E: \ vmware \ Ubuntu \ Ubuntu.vmx state = ON os = ubuntu
Recheck this VM_machine state = SUSPENDED
path = E: \ vmware \ Ubuntu_Kde_4 \ Ubuntu.vmx state = OFF os = ubuntu
path = E: \ development \ FreeBSD \ FreeBSD.vmx state = ON os = freebsd
Recheck this VM_machine state = SUSPENDED

That's all. :)
Uffff ... managed.
Sleep well, and most importantly take care of your nerves.
Until next article ...

Source: https://habr.com/ru/post/39130/


All Articles