Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aalbertson/6877f208fc6b58507015 to your computer and use it in GitHub Desktop.
Save aalbertson/6877f208fc6b58507015 to your computer and use it in GitHub Desktop.
This code can slip right into user-data for an AWS ec2 instance for both win2k8 and win2k12r2 as is to stripe your ephemeral volumes. This works on more than 2 ephemerals as well (tested on a d2.xlarge with 3 1.8TB drives)
<powershell>
#################################################
# Detect the Ephemeral drives and stripe them
#################################################
$DriveLetterToAssign = "Z:" # Be sure to choose a drive letter that will not already be assigned
#################################################
# Given a device (e.g. xvda), strip off "xvd" and convert the remainder to the appripriate SCSI ID
#################################################
function GetSCSI {
Param([string]$device)
$deviceSuffix = $device.substring(3) # remove xvd prefix
if ($deviceSuffix.length -eq 1) {
$scsi = (([int][char] $deviceSuffix[0]) - 97)
}
else {
$scsi = (([int][char] $deviceSuffix[0]) - 96) * 26 + (([int][char] $deviceSuffix[1]) - 97)
}
return $scsi
}
#################################################
# Main
#################################################
# From metadata read the device list and grab only the ephermals
$alldrives = (Invoke-WebRequest -Uri http://169.254.169.254/latest/meta-data/block-device-mapping/).Content
$ephemerals = $alldrives.Split(10) | where-object {$_ -like 'ephemeral*'}
# Build a list of scsi ID's for the ephemerals
$scsiarray = @()
foreach ($ephemeral in $ephemerals) {
$device = (Invoke-WebRequest -Uri http://169.254.169.254/latest/meta-data/block-device-mapping/$ephemeral).Content
$scsi = GetSCSI $device
$scsiarray = $scsiarray + $scsi
}
# Convert the scsi ID's to OS drive numbers and set them up with diskpart
$diskarray = @()
foreach ($scsiid in $scsiarray) {
$disknumber = (Get-WmiObject -Class Win32_DiskDrive | where-object {$_.SCSITargetId -eq $scsiid}).Index
if ($disknumber -ne $null)
{
$diskarray += $disknumber
$dpcommand = "select disk $disknumber
select partition 1
delete partition
convert dynamic
exit"
$dpcommand | diskpart
}
}
# Build the stripe from the diskarray
$diskseries = $diskarray -join ','
if ($diskarray.count -gt 0)
{
if ($diskarray.count -eq 1) {
$type = "simple"
}
else {
$type = "stripe"
}
$dpcommand = "create volume $type disk=$diskseries
assign letter=$DriveLetterToAssign
format fs=ntfs quick
exit"
$dpcommand | diskpart
}
</powershell>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment