Before I reinvent the wheel, has anyone got a neat way to do an orderly shutdown of a windows (server, in this case 2008 R2 and SBS 2011) guest from the host- so if and when the host has to be rebooted/shutdown the guests can be neatly closed down? My host is CentOS 6.2 (UNIX)
I'm thinking some sort of expect script or similar, or is there a neater, more integrated way to do it?
Thanks!
clean shutdown of windows guests?
-
Ken Hagan
- Posts: 43
- Joined: 1. Oct 2009, 17:42
- Primary OS: Debian other
- VBox Version: VirtualBox+Oracle ExtPack
- Guest OSses: Windows (various)
- Location: UK
Re: clean shutdown of windows guests?
I use something like:
I've just cut and pasted this from a command file I use. %1 is the VM name. Your password will vary. SHUTDOWN.EXE is present in all recent versions of Windows. The second VBoxManage command waits for the VM's network connection to disappear, which is as close as I could easily get to the moment of shutdown. It's not perfect, so I sleep a little afterwards.
You'll also note that I allow five minutes for the shutdown. Depending on how your guest handles Windows Updates, shutdowns can occasionally be quite time-consuming and even five minutes probably isn't enough. In fact, if there is anyone out there who is reading this and thinking "Arghh, no! What you want to do is..." then feel free to enlighten me.
Code: Select all
VBoxManage -q guestcontrol "%1" execute --username Administrator --password whatever --image SHUTDOWN.EXE -- -s -t 5
VBoxManage -q guestproperty wait "%1" /VirtualBox/GuestInfo/Net/0/Status --timeout 300000
Sleep 60You'll also note that I allow five minutes for the shutdown. Depending on how your guest handles Windows Updates, shutdowns can occasionally be quite time-consuming and even five minutes probably isn't enough. In fact, if there is anyone out there who is reading this and thinking "Arghh, no! What you want to do is..." then feel free to enlighten me.
-
bleve97
- Posts: 17
- Joined: 16. Jun 2011, 14:10
- Primary OS: OpenSolaris other
- VBox Version: OSE other
- Guest OSses: XP, windows SBS
Re: clean shutdown of windows guests?
This is the perl script I knocked up to do this, it's not perfect but it works, it does assume that the call to shutdown is successful and that all clients are windows and all have the same username and password with permission to shut down, but it doesn't make any assumptions about how long it takes to shut down, it just polls every 30s using VBoxManage list runningvms, my apologies for this forum s/w quashing the indents.
#!/usr/bin/perl
#
#
use Data::Dumper;
use strict;
my $live = 0;
#my $live = 1;
my $VBM = q(/usr/bin/VBoxManage);
my $winShutdown = q(C:\WINDOWS\system32\shutdown.exe);
#my $winShutdown = q(C:\WINDOWS\system32\ipconfig.exe);
my $running = 1;
my $username = q(someusername);
my $password = q(somepassword);
my %told;
#print Dumper %VMs;
print "$0: shutting down VM's : \n";
while ($running) {
my @runningVMs = running();
if (@runningVMs) {
foreach my $machine (@runningVMs) {
unless(exists($told{$machine})) {
VMshutdown($machine);
$told{$machine} = 1;
} else {
print "waiting ... \n";
}
}
sleep(30);
}
$running = @runningVMs;
}
sub running { # returns array of running VMs'
open (VBM, "$VBM list runningvms |");
my @runningVMs;
while (<VBM>) {
my $line = $_;
my @bits = split(/\"/, $line);
my $serverName = $bits[1];
print $serverName."\n";
push @runningVMs, $serverName;
}
return @runningVMs;
}
sub VMshutdown {
my $target = @_[0];
#
#
print " VMshutdown(@_)\n";
print " Shutting down $target\n";
my $CMD = $VBM." guestcontrol ".$target." exec --image \"".$winShutdown."\" --username ".$username." --password \"".$password."\" --wait-stdout --verbose -- /s";
runit($CMD);
}
sub runit {
my $cmd = @_[0];
if ($live) {
open (CMD, "$cmd |") or warn "Can't exec $cmd : $1";
while (<CMD>) {
print;
}
} else {
print "not live, not running :\n $cmd\n\n";
}
}
#!/usr/bin/perl
#
#
use Data::Dumper;
use strict;
my $live = 0;
#my $live = 1;
my $VBM = q(/usr/bin/VBoxManage);
my $winShutdown = q(C:\WINDOWS\system32\shutdown.exe);
#my $winShutdown = q(C:\WINDOWS\system32\ipconfig.exe);
my $running = 1;
my $username = q(someusername);
my $password = q(somepassword);
my %told;
#print Dumper %VMs;
print "$0: shutting down VM's : \n";
while ($running) {
my @runningVMs = running();
if (@runningVMs) {
foreach my $machine (@runningVMs) {
unless(exists($told{$machine})) {
VMshutdown($machine);
$told{$machine} = 1;
} else {
print "waiting ... \n";
}
}
sleep(30);
}
$running = @runningVMs;
}
sub running { # returns array of running VMs'
open (VBM, "$VBM list runningvms |");
my @runningVMs;
while (<VBM>) {
my $line = $_;
my @bits = split(/\"/, $line);
my $serverName = $bits[1];
print $serverName."\n";
push @runningVMs, $serverName;
}
return @runningVMs;
}
sub VMshutdown {
my $target = @_[0];
#
#
print " VMshutdown(@_)\n";
print " Shutting down $target\n";
my $CMD = $VBM." guestcontrol ".$target." exec --image \"".$winShutdown."\" --username ".$username." --password \"".$password."\" --wait-stdout --verbose -- /s";
runit($CMD);
}
sub runit {
my $cmd = @_[0];
if ($live) {
open (CMD, "$cmd |") or warn "Can't exec $cmd : $1";
while (<CMD>) {
print;
}
} else {
print "not live, not running :\n $cmd\n\n";
}
}