#!/bin/bash #.Y / 70 83 / / / / 105 / / / / / / ;A4_QUER.Y #.YT 3 2 0 0 3 3 1 / / / #.H!a!0:linux//dos_/vm_/THIS!0.!c # # copyright : (C) 2009 by Thomas Bruecker =: tbrue # email : public0x05bf@bluewin.ch # # tbrue 31oct2007 # # dos_ vm_ THIS : virtual-machines functions # ++++++++++++++++++++++++++++++++++++++++++ # # MyHash("linux//dos_/vm_/THIS") = 0x0A8D # # * for every function: # * if returned result is not commented, the function returns 0 on no-error. SCRIPTS=root/bin . /$SCRIPTS/io/f_/THIS vm_ToAbsoluteVmxPath() # $1 : path to a folder containing one and only one "*.vmx"-file denoting the # virtual machine to stop. # * print absolute path to the "*.vmx"-file to stdout. { local AbsoluteFolderPath0x0A8D AbsoluteVmxPath0x0A8D Result0x0A8D AbsoluteFolderPath0x0A8D="$(f_ToAbsolutePath "$1")" Result0x0A8D=$? if [ ! "$Result0x0A8D" = "0" ]; then return $Result0x0A8D; fi # else AbsoluteVmxPath0x0A8D="$( find "$AbsoluteFolderPath0x0A8D" -maxdepth 1 \ -name "*.vmx" )" # * give "-maxdepth 1" to look just in . Result0x0A8D=$? if [ ! "$Result0x0A8D" = "0" ]; then return $Result0x0A8D; fi # else # * may be Result0x0A8D==0 but *.vmx has still not been found ==> # =="" . # * check for this: if [ -z "$AbsoluteVmxPath0x0A8D" ]; then Result0x0A8D="1" # corresponsds to "find"-not- else # found-error. Result0x0A8D="0" fi echo "$AbsoluteVmxPath0x0A8D" return $Result0x0A8D } vm_RunningQ() # Running? # $1 : path to a folder containing one and only one "*.vmx"-file denoting the # virtual machine check. # * print 0 to stdout if the virtual machine has state "on". # * print 1 to stdout if the virtual machine has state "off". { local AbsoluteVmxPath0x0A8D Result0x0A8D Running0x0A8D AbsoluteVmxPath0x0A8D=$(vm_ToAbsoluteVmxPath "$1") Result0x0A8D=$? if [ ! "$Result0x0A8D" = "0" ]; then return $Result0x0A8D; fi # else vm_runningQ "$AbsoluteVmxPath0x0A8D" # output done by this line. Result0x0A8D=$? if [ ! "$Result0x0A8D" = "0" ]; then return $Result0x0A8D; fi # else return 0 # no error. } vm_runningQ() # running? # $1 : absolute path to a "*.vmx"-file denoting the virtual machine to check. # * must be a path generated by "vm_ToAbsoluteVmxPath" . # * print 0 to stdout if the virtual machine has state "on". # * print 1 to stdout if the virtual machine has state "off". { local StateText0x0A8D Result0x0A8D StateText0x0A8D="$( /usr/bin/vmware-cmd "$1" getstate )" Result0x0A8D=$? if [ ! "$Result0x0A8D" = "0" ]; then return $Result0x0A8D; fi # else string_MatchQ "$StateText0x0A8D" "getstate() = on" if [ "$?" = "0" ]; then # "... on" found ==> "on" echo 0 else echo 1 fi return # result of "echo" = 0 . } vm_Start() # $1 : powerop_mode in { "hard", "soft", "trysoft" } . # * explanation of "hard" and "soft" according CS>: # * hard: Power On # * soft: Start Up Guest . # # $2 : path to a folder containing one and only one "*.vmx"-file denoting the # virtual machine to start. # # * starts the virtual machine denoted by $2 using powerop_mode. # * this function does not wait until the started virtual machine has com- # pletely booted. { local AbsoluteVmxPath0x0A8D Result0x0A8D AbsoluteVmxPath0x0A8D=$(vm_ToAbsoluteVmxPath "$2") Result0x0A8D=$? if [ ! "$Result0x0A8D" = "0" ]; then return $Result0x0A8D; fi # else /usr/bin/vmware-cmd "$AbsoluteVmxPath0x0A8D" start $1 return # result of vmware-cmd . } vm_Stop() # $1 : powerop_mode in { "hard", "soft", "trysoft" } . # * explanation of "hard" and "soft" according CS>: # * hard: Power Off # * soft: Shut Down Guest . # # $2 : path to a folder containing one and only one "*.vmx"-file denoting the # virtual machine to stop. # # * stops the virtual machine denoted by $2 using powerop_mode. # * this function is designed not to wait until the stopping virtual machine # is completely shut down. { local AbsoluteVmxPath0x0A8D Result0x0A8D AbsoluteVmxPath0x0A8D=$(vm_ToAbsoluteVmxPath "$2") Result0x0A8D=$? if [ ! "$Result0x0A8D" = "0" ]; then return $Result0x0A8D; fi # else /usr/bin/vmware-cmd "$AbsoluteVmxPath0x0A8D" stop $1 & \ >/dev/null 2>/dev/null return # result of vmware-cmd . } vm_WaitForRunningOK() # $1 : path to a folder containing one and only one "*.vmx"-file denoting the # virtual machine to wait for. # * the folder must contain a "RunningOK"-script too. { local AbsoluteFolderPath0x0A8D AbsoluteRunningOkPath0x0A8D Result0x0A8D local Running0x0A8D AbsoluteFolderPath0x0A8D="$(f_ToAbsolutePath "$1")" Result0x0A8D=$? if [ ! "$Result0x0A8D" = "0" ]; then return $Result0x0A8D; fi # else AbsoluteRunningOkPath0x0A8D="$AbsoluteFolderPath0x0A8D/RunningOK0x0A8D" if [ ! -x "$AbsoluteRunningOkPath0x0A8D" ] then echo -e "\"$AbsoluteRunningOkPath0x0A8D\" does not exist." >&2 return 1 fi # else # load RunningOK-script . "$AbsoluteRunningOkPath0x0A8D" # not running: ==> runningQ0x0A8D returns "1" . Running0x0A8D="1" # assume not running. while # while not running. [ "$Running0x0A8D" = "1" ] do sleep 1 # avoid extensive cpu-usage. # * do this before checking, # so you may use the result # immediately. runningQ0x0A8D # get result of local func- # to $? . Running0x0A8D=$? done return 0 } vm_WaitForStop() # $1 : path to a folder containing one and only one *.vmx denoting the vir- # tual machine to wait for. # # * waits until the virtual machine is completely shut down. { local AbsoluteVmxPath0x0A8D Result0x0A8D Running0x0A8D AbsoluteVmxPath0x0A8D=$(vm_ToAbsoluteVmxPath "$1") Result0x0A8D=$? if [ ! "$Result0x0A8D" = "0" ]; then return $Result0x0A8D; fi # else Running0x0A8D="0" # assume running. while # while running. [ "$Running0x0A8D" = "0" ] do sleep 2 # * avoid extensive cpu-usage. # * do this before checking, # so you may use the result # immediately. Running0x0A8D=$( vm_runningQ "$AbsoluteVmxPath0x0A8D" ) Result0x0A8D=$? if [ ! "$Result0x0A8D" = "0" ]; then return $Result0x0A8D; fi # else done return 0 # no error. }