Auto-Start and Close Options for Windows Virtual PC

In Windows® Virtual PC (WVPC), Virtual Machine (VM) start-up and close behavior can be configured to have a customized user experience. Few start-up options can actually lead to perceivably faster launching of Virtual Applications. In this blog, we will discuss these options and settings along with their pros and cons. This should be helpful for you to understand the best performance tweaks that can be done with WVPC VM and the kind of impact it can have on your machines.

Once a VM is created, it can be started in Desktop mode from the Windows® Virtual PC Center. However Windows® XP Mode VM can be directly started from the Start Menu.

Fig 1

Figure 1: Windows® Virtual PC Center to create or launch a VM

Start-up time of a VM may be reduced if this setting is disabled but Integration Features like Clipboard (copy/paste), Drive, Printer and Smart Card redirection will not be available. Also the POST screen of the VM booting would be visible.

This may be the ideal usage setting where VM needs to be used in isolation from the host or is only remotely accessed through Remote Desktop Connection etc.

Fig 2 Fig 3

Figure 2(a): WVPC Settings to change Auto-Enable Integration Features Figure 2(b): Manually disabling Integration Features

Script #1: Disable Integration Features at Start-up

    1: Option Explicit   ' Force explicit variable declaration.
    2:  
    3: Dim defaultVMTag, defaultVM, objArgs, objVPC, vmName  
    4: Set objArgs = WScript.Arguments
    5: Set objVPC = CreateObject("VirtualPC.Application")
    6:  
    7: defaultVMTag = "default_vm"
    8: defaultVM = objVPC.GetConfigurationValue(defaultVMTag)
    9:      
   10: if objArgs.Count < 1 Then
   11: WScript.Echo "Syntax: cscript <scriptname>.vbs <”VMName”>"
   12:      WScript.Echo "Considering default VM."     
   13:      vmName  = defaultVM
   14: Else
   15:      vmName  = objArgs(0)        
   16: End if
   17:  
   18: DisableIF()
   19:  
   20: Sub DisableIF()
   21:     Dim vmcTag, objVM
   22:     vmcTag = "ui_options/use_enhancedmode"
   23:     Set objVM = objVPC.FindVirtualMachine(vmName)
   24:     
   25: if objVM is Nothing Then
   26: WScript.Echo "VM not found: """ + vmName + """" 
   27: Else
   28: WScript.Echo "Disabling Integration Features at Startup for the VM: """ + vmName + """"
   29: objVM.SetConfigurationValue vmcTag, False
   30:     WScript.Echo "Integration Features at start-up disabled successfully"        
   31: End if
   32: End Sub

This script will disable Integration Features for the default VM (or the specified VM, if <VMName> is passed as an argument to the script).

Similarly, a Virtual application can also be launched from Windows® 7 Host start menu once the application has been published from the VM. All published applications of a VM are listed under, Programs -> Windows Virtual PC -> <VMName> Application* in Windows® 7 Start Menu.

PS: Scripting provides a useful means to perform a number of operations on VMs such as Creating, deleting, changing VM configuration, states and tracking VM state, health and other information about it. PowerShell is another powerful medium to interact with the VM. The blog uses VBScript for code snippets. These code snippets can be saved as <ScriptName>.vbs file and executed in Windows® 7 command prompt with the following syntax: cscript <Scriptname>.vbs arg1

Starting a headless VM

Sometimes, you might want to start a VM in the back ground without any User Interface. This can be of great use for IT Administrators/Professionals, Developers & testers. For example if you want to reduce the latency of Virtual Applications start-up time, remotely use the VM through Remote Desktop Connections or execute some tests in isolation without any user interference.

This is referred as a Headless VM configuration.

Here is a sample VB script to start the VM in Headless mode.

Script #2: Starting a VM in Headless Mode

    1: Option Explicit   ' Force explicit variable declaration.
    2:  
    3: Dim defaultVMTag, defaultVM, objArgs, objVPC, vmName  
    4: Set objArgs = WScript.Arguments
    5: Set objVPC = CreateObject("VirtualPC.Application")
    6:  
    7: defaultVMTag = "default_vm"
    8: defaultVM = objVPC.GetConfigurationValue(defaultVMTag)
    9:      
   10: if objArgs.Count < 1 Then
   11: WScript.Echo "Syntax: cscript <scriptname>.vbs <”VMName”>"
   12:      WScript.Echo "Considering default VM."     
   13:      vmName  = defaultVM
   14: Else
   15:      vmName  = objArgs(0)        
   16: End if
   17:  
   18: WScript.Echo "Starting VM: """ + vmName + """"
   19: StartHeadlessVM()
   20:  
   21:    Sub StartHeadlessVM()
   22:     Dim objVM
   23:     Set objVM = objVPC.FindVirtualMachine(vmName)
   24:     if objVM is Nothing Then
   25:         WScript.Echo "VM not found: """ + vmName + """"          
   26:     Else
   27: Dim objVMTask
   28:         Set objVMTask = objVM.Startup()
   29:         'Set objVMTask = objVM.TurnOff() 'Immediately Turns Off/Powers Down the VM
   30:         'Set objVMTask = objVM.Save()    'Hibernate the VM
   31:         'Set objVMTask = objVM.GuestOS.Shutdown(0) 'Controlled Shutdown
   32:         'Set objVMTask = objVM.GuestOS.Shutdown(1) 'Forced Shutdown
   33:         objVMTask.WaitForCompletion -1
   34:         WScript.Echo "VM Started : """ + vmName + ""        
   35:     End if
   36:    End Sub

When host logoff, shutdown or restart is initiated, each running headless VM is first hibernated. This may cause some delay in host logoff, shutdown or restart. Also Integration Features cannot be enabled for headless VM. Additionally, user also has to keep a track of headless VMs as they run behind the scene i.e. without any User Interface.

Auto Start a VM

A VM can be configured to start at Windows® 7 start-up. When a VM is setup to start on Windows 7 Start up, it drastically reduces the time required for first time launch of Virtual Application. However, with this configuration, the VPC.exe will keep running, holding the resources like RAM allocated to the VM and speed up battery drainage on laptops. However in case of desktops the following configuration can help is faster launching of Virtual Application. Since the VM would be started immediately after login into Windows 7, there might be some impact on the responsiveness of the host for couple of seconds.

Auto Start a Virtual Application:

Copy the shortcut of the Virtual Application to start-up folder of Windows 7 that needs to be started on Windows 7 start-up. In this example, Virtual Application-Internet Explorer 6 has been set to auto start with Windows 7 start-up.

Location:

Virtual Application: %appdata%\Microsoft\Windows\Start Menu\Programs\Windows Virtual PC\<VMName> Applications*

Windows 7 Start-up Folder: %appdata%\Microsoft\Windows\Start Menu\Programs\Startup

Auto Start VM in Headless mode

Create a shortcut of script #2 under Windows® 7 start-up folder with the following target: cscript <pathoftheScript>\<scriptname>.vbs <”VMName”>

Fig_3

Figure 3: Set a Virtual Application to Auto-Start

Auto Start VM in Desktop Mode

VM can also be set to auto start with Windows® 7 start-up in desktop mode. This can be easily accomplished by adding the shortcut of the <VMName> .vmcx file (vmcx file is located in Windows® Virtual PC Center) in start-up folder.

Fig 4

Figure 4: Set VM to Auto-Start in Desktop Mode

Close a VM

A VM in Desktop Mode can be manually closed by the user, either from the user Interface (Figure - 5) or through scripts (refer script #3 for details). Notice that there are 4 options for ‘Closing’ a VM. Each option has its own benefits and use case.

Fig 5

Figure 5: Close VM options.

Hibernate: This is very similar to hibernate of a physical machine. The VM is initially set to Pause/sleep state and then VM memory is purged to a save state file (<VMName>.vsv). The difference in VM hibernate as appose to a Physical machine is that VM is unaware about hibernation or resume thereafter. Unlike the physical machine where the machine itself initiates hibernation/resume, here the Host does it for the VM. Hence, this operation doesn’t require ICs to be installed. This mechanism offers benefits like quick shutdown and resume, no data loss. This is the default close setting for XP Mode VM. This setting can be modified either through VPCSettings -> Close.

It is not recommended to migrate a VM in hibernate state as difference in processor architecture of the two host machines can render the save state useless. In such case, deleting the save state file would allow the VM to boot up, but may lead to data loss.

Alternatively VM can be hibernated through scripts. Script #2 can be easily modified to accommodate the same.

Shutdown: This would initiate a graceful shutdown of the VM. Since this is done by Guest OS it requires ICs to be installed in the VM. Here the scripts provide an additional option to force shutdown the VM. Shutdown of a VM is absolutely identical to a physical machine.

Turn Off: This is very similar to Powering off the machine. It is not recommended as it may lead to data loss or corruption of Guest OS.

Turn Off and discard changes: This option appears only when ‘Undo Disks’ is enabled. It will delete all data in the undo disk (.vud file). This option should be used with caution as it may lead to data loss.

Fig 6

Figure 6: Default close VM setting for XP Mode

Script #3: Enable prompt while closing the VM

    1: Option Explicit   ' Force explicit variable declaration.
    2:  
    3: Dim defaultVMTag, defaultVM, objArgs, objVPC, vmName  
    4: Set objArgs = WScript.Arguments
    5: Set objVPC = CreateObject("VirtualPC.Application")
    6:  
    7: defaultVMTag = "default_vm"
    8: defaultVM = objVPC.GetConfigurationValue(defaultVMTag)
    9:      
   10: if objArgs.Count < 1 Then
   11: WScript.Echo "Syntax: cscript <scriptname>.vbs <”VMName”>"
   12:      WScript.Echo "Considering default VM."     
   13:      vmName  = defaultVM
   14: Else
   15:      vmName  = objArgs(0)        
   16: End if
   17:  
   18: PromptAtClose()
   19:  
   20:    Sub PromptAtClose()
   21: Dim vmcTag, objVM
   22:     vmcTag = "ui_options/onclose_showmessage"
   23:     Set objVM = objVPC.FindVirtualMachine(vmName)
   24:     if objVM is Nothing Then
   25:         WScript.Echo "VM not found: """ + vmName + """"          
   26:     Else
   27: WScript.Echo "Enable prompt on VM close: """ + vmName + """"
   28:         'This will enable prompt at close. User can then choose the close method
   29:         objVM.SetConfigurationValue vmcTag, True
   30:         'To set the default behaviour at VM close. No prompt will be thrown in this case
   31: 'and default action can also be set
   32:         'objVM.SetConfigurationValue vmcTag, False
   33: 'objVM.SetConfigurationValue "ui_options/onclose_defaultaction", "save" 'save/shutdown/turnoff
   34:         WScript.Echo "Close option set successfully"        
   35:     End if
   36:    End Sub

However, shutdown in case of a Virtual Application does not involve any explicit User Interface. Let’s look at the VM state transitions that take place when a user interacts with Virtual Applications:

  1. VM will be started if it is not already running when the first Virtual Application is launched.
  2. When the last Virtual Application (launched by the user) is closed and none of the other Virtual Application’s window is opened, the VM hibernates after 5 minutes. Script #4 can be modified to set the value of the VMC tag: ‘ui_options/seconds_to_save’ to the desired time duration).
  3. While Virtual Applications are running and Windows® 7 host is logged off or shutdown, graceful (controlled) shutdown of VM is initiated.
  4. Similarly, if Windows® 7 host goes to hibernate or sleep state, the VM is paused. On host resume, the VM auto wakes up. This may also cause the Virtual applications to be unresponsive during this short time interval.
  5. In case of forced Host shutdown, VM is powered off abruptly.

Note: In case of headless VM, Windows® 7 Host logoff/shutdown/restart will always initiate VM hibernate/save.

‘Never Hibernate’ VM Setting

When the last Virtual Application is closed (launched by the user), the VM will go into hibernate state after 5 minutes by default. This is done to ensure that host resources are freed and battery drain can be reduced when no virtual application is running. This setting can be changed so that the VM never hibernates and subsequent Virtual application can be launched with minimum latency.

Script #4: Set VM to Never Hibernate

    1: Option Explicit   ' Force explicit variable declaration.
    2:  
    3: Dim defaultVMTag, defaultVM, objArgs, objVPC, vmName  
    4: Set objArgs = WScript.Arguments
    5: Set objVPC = CreateObject("VirtualPC.Application")
    6:  
    7: defaultVMTag = "default_vm"
    8: defaultVM = objVPC.GetConfigurationValue(defaultVMTag)
    9:      
   10: if objArgs.Count < 1 Then
   11: WScript.Echo "Syntax: cscript <scriptname>.vbs <”VMName”>"
   12:      WScript.Echo "Considering default VM."     
   13:      vmName  = defaultVM
   14: Else
   15:      vmName  = objArgs(0)        
   16: End if
   17:  
   18:  
   19:  SetValue()
   20:  
   21:  Sub SetValue()
   22:  Dim vmcTag, objVM
   23:  vmcTag = "ui_options/seconds_to_save"
   24:  Set objVM = objVPC.FindVirtualMachine(vmName)
   25:  if objVM is Nothing Then
   26:         WScript.Echo "VM not found: """ + vmName + """"          
   27:  Else
   28:          objVM.SetConfigurationValue vmcTag, 0
   29: WScript.Echo "VM: """ + vmName + """ set to never hibernate"
   30:  End if
   31:  End Sub

This script sets the value of VMC tag "ui_options\seconds_to_save" to 0 and hence VM would never be hibernated even after closing the last Virtual Application. This tag can also be used to increase or decrease the time to hibernate the VM when last Virtual application is closed.

Note: This tag doesn’t impact VMs running in desktop or headless mode.

In most of the cases, this would ensure faster virtual application launch, as VM doesn’t hibernates after closing the last virtual application (launched by the user). But in case of host restart, first virtual application launch will still take time as VM needs to be started first. This can also be tweaked by adding an entry of starting a headless VM on Window 7 startup (refer Auto Start VM in Headless Mode)

Conclusion

In WVPC, you can configure the VM start-up and close behavior using WVPC settings and the sample scripts presented above, to optimize the user experience and performance of VMs and virtual applications.  Check out WVPC and Windows XP Mode today, and let us know what you think, either via the comments section here, or sharing your feedback on the WVPC and Windows XP Mode Forum on TechNet here.

*This may get localized based on Windows® 7 Host language.

Technorati Tags: Windows 7,Windows Virtual PC,Windows XP Mode,Application Compatibility,Windows Upgrade,VPC,VHD,VM,Virtual Machine,Virtualization,Close,Shutdown,Hibernate,VM script,Headless VM

Priyank Gaharwar

SDET

Microsoft Virtualization Team