Hyper-V Server and a UPS

February 17th, 2010 by Charles Gardner

Microsoft’s Hyper-V Server 2008 R2 can be a great hypervisor choice for a small business with just one or two servers.  In this environment though the common power protection scheme is going to be a single, direct-connected UPS with a USB signaling cable.  In this scenario we need to be able to safely shutdown the hypervisor and guests before power gives out.

I claim no original thoughts here, but I did want to preserve a link to a good answer I found and have implemented.  The original thread is here on the Technet Forums.

First create ups-shutdown.vbs and load it with:

set wmi = GetObject(“winmgmts:{impersonationLevel=impersonate,(Shutdown)}!\\.\root\cimv2″)
set batteryColl = wmi.ExecQuery(“select * from Win32_Battery”)
set osColl = wmi.ExecQuery(“select * from Win32_OperatingSystem”)

while true
for each battery in batteryColl
battery.Refresh_
if battery.batteryStatus = 1 and battery.EstimatedChargeRemaining <= 40 then
for each os in osColl
os.Win32Shutdown 1
next
end if
next
wscript.Sleep 15000
wend

Schedule this to run at startup using the Task Scheduler.  (Connect from another machine and set this up.)

Next create ups-monitor.ps1 and insert:

# Initialize Variables
# Shutdown threshold at 50% of remaining UPS capacity
$threshhold = 40
$interval = 60
$OnBattery = 0
$Event = 0

$hostname = hostname

# Create SMTP client
$Server = “mail.xxxxxxxxxxxx.com”
$Port = 25
$Client = New-Object System.Net.Mail.SmtpClient $Server, $Port

$Client.Credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials

$To      = “admin@xxxxxxxxxxxx.com”
$From    = “hyperv@yyyyyyyyyyyy.com”

# Loop on Battery Query
while (1)
{
$bat = get-wmiobject -class CIM_Battery -namespace “root\CIMV2″
$batstatus = $bat.batterystatus
$batcapacity = $bat.estimatedchargeremaining
$timetoshutdown = $bat.estimatedruntime/2

if ($batstatus -eq 1)
{
$Event = 1
$OnBattery = 1
# “On Battery”

$Subject = “Utility Power Failure: {0} is running On UPS Battery” -f $hostname
$Body   = “UPS at {0} % remaining capacity, approximately {1} minutes before {2} shutdown.” -f $batcapacity, $timetoshutdown, $hostname

if ($batcapacity -lt ($threshhold +5) )
{
$Body = “Shutdown imminent at {0} %, with ” -f $threshhold + $Body
}

}

elseif (($batstatus -eq 2) -and ($OnBattery -eq 1))
{
$Event = 1
$OnBattery = 0
# “Power Restored”

$Subject = “Utility Power Restored to {0}.” -f $hostname
$Body   = “Battery at {0} % capacity. UPS charging… ” -f $batcapacity
}

if ($Event -eq 1) # Create mail message
{
$Event = 0
$Message = New-Object System.Net.Mail.MailMessage $From, $To, $Subject, $Body
$Message.Priority = [System.Net.Mail.MailPriority]::High
try {
$Client.Send($Message)
# “Message sent successfully”
}
catch {
“Exception caught in UPS_Monitor.ps1″
}
}

sleep $interval
}

Change the mail server, to and from address, and you’re in business.

Create ups-monitor.cmd with the following:

powershell -command c:\path\to\your\script\ups-monitor.ps1

Again using Task Scheduler, schedule ups-monitor.cmd to run at startup, and you’re set.

Make sure you have your VMs set to save at shutdown and autostart, and then go pull the plug on that UPS just to make sure things work to your liking.

Also from the above referenced thread, you can check the battery condition using powershell with this:

PS$ get-wmiobject -class CIM_Battery -namespace “root\CIMV2″

Have fun.

Hyper-V, Virtualization, Windows Server 2008

Windows Server Backup WBAdmin

February 14th, 2010 by Charles Gardner

I was just setting up a Hyper-V Server 2008 R2 box and wanted to get backup running to an external drive.  I installed the Windows Server Backup role via the Core Configurator tool, but then the backups need to be configured and run via the wbadmin command line tool.

This command line reference for wbadmin was helpful so I wanted to mark it in case I need it again in the near future.

Hyper-V, Virtualization, Windows

Links of Interest

February 3rd, 2010 by Charles Gardner

Virtualization Benefits for Small Business

Coalescing a few links I’ve been keeping up with for further reading:

A Virtual Door Opens for SMBsA Virtual Door Opens for SMBs

Other, Virtualization

More Interesting Videos – Defcon Peek and TEDx MidAtlantic

November 11th, 2009 by Charles Gardner
Projector

Photo credit pedrosimoes7

I’m bookmarking a couple more videos to watch.

First, Defcon released their teaser set of videos from this year’s con, including Adam Savage’s “Failure”.

https://www.defcon.org/

Second, I saw a link to Marcus Ranum talking at TEDx MidAtlantic.

http://tedxmidatlantic.com/live/#MarcusRanum


Education, Events, Security

DojoCon 2009 Videos Posted

November 10th, 2009 by Charles Gardner

I had the opportunity to watch the first few minutes of the stream of Marcus Ranum’s talk at DojoCon 2009 but then had to go to a client site.  I was happy to see they posted the videos to UStream so I can go back and watch the rest.

Among those who spoke:

  • Richard Bejtlich
  • Marcus Ranum
  • Chris Hoff
  • …and a whole lot more

In case you want to catch up too:

http://www.ustream.tv/channel/dojocon-2009

Education, Events, Security

Disk2vhd P2V Tool for Microsoft Virtualization

November 10th, 2009 by Charles Gardner

This post is mostly a note to myself.  I haven’t tried this yet, but the Disk2vhd tool from Sysinternals is for P2V for Microsoft virtualization such as Hyper-V.

When I get to try this out, I’ll post some notes about using it and how it stacks up against something like VMWare Converter.

Hyper-V, Virtualization

Moving!

June 12th, 2009 by Charles Gardner

Just a quick note.  I will be moving my focus to putting posts up on my company web site at www.sterlingideas.com.  Any really technical items will still end up here, but I am going to make a new effort to post regular content over on the company site, particularly items of interest to my clients.

Be sure to add http://www.sterlingideas.com/feed/rss/ to your feed reader.

Other

How Not To Expose Root in Sendmail

May 23rd, 2009 by Charles Gardner

Quick note:

When you do domain masquerading with sendmail, root is exempted from that by default.  No big deal unless the host name of your system is not actually registered in DNS.  I have a couple VMs that don’t need outside access or DNS registrations, but I’d like to receive their cron output cleanly.

This is a rather easy fix.  In most sendmail .mc files you will find the DOMAIN(generic) statement.  This refers to loading the generic.m4 file which includes a default statement to expose root without masquerading — EXPOSED_USER(`root’).  Copy the generic.m4 to mycustom.m4 and remove the EXPOSED_USER line.  Go to your .mc file and change the DOMAIN(generic) to DOMAIN(mycustom) and rebuild your sendmail.cf file.

Applications

Copy Virtual Machines on VMWare Server 2

May 12th, 2009 by Charles Gardner

This is a simple note to capture process.

  • On the VMWare server, copy the guest’s directory to a new name.
    • cp -ax /vm/srv1 /vm/srv2
  • Rename the vmdk disk image
    • cd /vm/srv2
    • vmware-vdiskmanager -n srv1.vmdk srv2.vmdk
  • Rename the other files
    • mv srv1.* srv2.*
  • Open the VM configuration and change the names there
    • vi srv2.vmx
    • :%s/srv1/srv2/
  • In the VMWare host’s web console, use the Add Virtual Machine to Inventory to add the new VM.

VMWare

perl-after-upgrade Is Your Friend

January 24th, 2009 by Charles Gardner

I just upgraded the main perl port on a FreeBSD box from 5.8.8 to 5.8.9 and a perl based service promptly died, complaining of problems locating dependencies.  D’Oh!!  That’s not good.

After a bit of crunching away I found that each perl module port (each p5-* port) needed a ‘make deinstall && make reinstall’ to align with the new perl version.  The only bugger is that this machine has 54 p5-* ports installed.  Now I’m basically lazy so I wanted a better way than manually reinstalling each port or even writiing a script to handle these specific ports.

Thankfully a little deeper google exercise turned up pearl-after-upgrade.  From the man page:

The standard procedure after a perl port (either lang/perl5 or lang/perl5.8) upgrade is to basically reinstall all other packages that depend on perl. This is always a painful exercise. The perl-after-upgrade utility makes this process mostly unnecessary.

The tool goes through the list of installed packages, looks for those that depend on perl, moves files around, modifies shebang lines in those scripts in which it is necessary to do so, tries its best to adjust dynamically linked binaries that link with libperl.so in the old path, and updates the package database.


Brilliant!! Just what I was looking for.

I ran perl-after-upgrade followed by perl-after-upgrade -f, and it did all the heavy lifting of getting things straight.  Just for good measure I ran a rebuild on mimedefang (portmaster mimedefang), and it was back off to the races for that system.

So I must say….  perl-after-upgrade is your friend!

FreeBSD, Is Your Friend