Advertise on warmetal.nl!
Click for more information
about advertising here.

Did you find this website useful? Did I save you a lot of time?
Please consider donating to support this site:

 

Lun Aligning

For information about why you should keep LUNs aligned with your vmdks I'd like to refer to this article from yellow-bricks.com. It explains why alignment is important, but I'll tell you it comes down to performance. When the LUNs, vmdk and Guest OS blocks are not aligned the array will request more blocks than the VMs are actually asking for.

Please note the following remarks:

  1. Windows Server 2008 and Windows Vista are aligned by default if the VM was created as Windows Server 2008/Windows Vista.
  2. Citrix Servers are not supported because they remap the C-drive.
  3. Windows Dynamic Disks are not supported and will be corrupted if an alignment is performed, you will be warned with something like this: “partition type: unknown - 0x42”. Do not align these partitions.
  4. Linux LVM volumes are not supported.
  5. If you align linux guests that use grub as their boot loader you'll have to reinstall grub
  6. Windows Server 2003 non-boot disks that have been added (d:, e:, etc) will need to be remapped in Computer Management. The drive letter will be lost on alignment.
  7. Because the virtual disks are being recreated you'll need enough free space on your volume/datastore.
  8. Aligning can only be done on VMs that are turned off (so downtime)
  9. Aligning is not supported on VMs that have snapshots or linked clones

Performing Alignment

Before you can actually start aligning you need to know which VMs should be aligned. In this article we use Windows native tools and NetApp tools. The NetApp tools can be obtained on several ways: First they are part of the host utilities but they're also part of the Virtual Storage Console, an extension you can install on your vCenter. I grabbed them from there and made them available on my ESX host:
[root@esx01 ~]# gunzip mbrtools.tar.gz
[root@esx01 ~]# tar -xf mbrtools.tar
[root@esx01 ~]# ll
total 3860
-rwxrwxrwx 1 root root   86949 Apr 28  2009 howto.pdf
-rw------- 1 root root    1629 Apr 13 21:53 ks.cfg
-rwxrwxrwx 1 root root  962284 Apr 16  2009 mbralign
-rwxrwxrwx 1 root root  134186 Apr 28  2009 mbralign1.2.pdf
-rwxrwxrwx 1 root root  771276 Oct  5  2008 mbrscan
-rw-r--r-- 1 root root 1960960 May 12 12:57 mbrtools.tar
-rwxrwxrwx 1 root root    1133 Apr 28  2009 readme.html

Determine Which VMs Should Be Aligned

I'll explain three ways to determine which VMs should be aligned. We'll use the NetApp tool mbrscan, we'll use the native Windows tools and we'll use a PowerCLI script.

MBRscan

After obtaining the NetApp tools you can use the mbrscan tool:

  1. Navigate to the directory of the VM you'd like to check for alignment.
  2. If the VM is running, create an ESX snapshot.
  3. In this directory run /root/mbrscan <filename>-flat.vmdk (NOTE: you are checking the –flat.vmdk file for proper alignment)
  4. Check if your virtual disk file is misaligned.
  5. Delete the snapshot.


This option can be used when scanning one VM or when scanning an entire host (while using the -all switch).

MBRScan Example

If VM is running:

[root@esx01 WINXP01]# /root/mbrscan WINXP01-flat.vmdk
--------------------
Failed to open WINXP01-flat.vmdk - [Device or resource busy]
--------------------

So create a snapshot and rerun:

[root@esx01 WINXP01]# /root/mbrscan WINXP01-flat.vmdk
--------------------
WINXP01-flat.vmdk p1 (NTFS)    lba:63  offset:32256    aligned:No
--------------------
Note: You can run the tool using the ”–all” switch which will scan all -flat.vmdk files that are on the host.

Native Windows Tool MSInfo32

You can check disks from within Windows:

  • Run Start → All Programs → Accessories → System Tools → System Information (or Start → Run → msinfo32 ).
  • Go to Components → Storage → Disks and check the value for Partition Starting Offset.
  • The value listed here should be completely divided by 4096. If so, the disk is aligned.
  • Check every disk in the list.


For misaligned VMs, you typically will find that the VM is running with a default starting offset value of 32,256, which is not completely divisible by 4,096; hence, the partition is not aligned.

This option can be used when you just want to know if one particular VM is aligned.

PowerCLI

You can also use this powercli script to determine per vCenter/Datacenter/Folder/Cluster/Host which VM Disks are misaligned:
# Original script: http://ict-freak.nl/2009/12/15/powercli-check-partition-alignment-windows-vms-only/
# Changed: Sjoerd Hooft - 10 Nov 2011
 
$myCol = @()
 
$timestamp = Get-Date -format "yyyyMMdd-HH.mm"
$cluster = "Acceptance"
$csvfile = "D:\sjoerd\$timestamp-alignmentinfo-$cluster.csv"
 
#$vms = Get-Datacenter "The Netherlands" | get-vm | where {$_.PowerState -eq "PoweredOn" -and $_.Guest.OSFullName -match "Microsoft Windows*" } | Sort Name
$vms = Get-Cluster $cluster | get-vm | where {$_.PowerState -eq "PoweredOn" -and $_.Guest.OSFullName -match "Microsoft Windows*" } | Sort Name
 
foreach($vm in $vms){
  $wmi = $null
  $wmi = get-wmiobject -class "Win32_DiskPartition" -namespace "root\CIMV2" -ComputerName $vm
  # Use default authentication, but if unsuccesfull try other credentials until succesfull
  $teller = 1
  while (($wmi -eq $null) -and ($teller -lt 4)){
     Write-Host "Enter Credentials for $vm. This is try $teller, with a maximum of 3 tries." -Foregroundcolor Red
     $wmi = get-wmiobject -class "Win32_DiskPartition" -namespace "root\CIMV2" -ComputerName $vm -Credential domain\sjoerd
     $teller++
  }
  foreach ($objItem in $wmi){
    $Details = "" | Select-Object VMName, Partition, Status, StartingOffSet,Size
    if ($objItem.StartingOffset -eq $null){
      Write-Host "Checking $vm"
      $vmview = Get-VM $vm | Get-View
      $Details.VMName = $vmview.Name
      $Details.Status = "VM could not be analyzed"
    }
    else{
      # Divide StartingOffset by 4096 and round the number to zero decimals
      # If both numbers are the same then the partition is aligned
      $divoffset = ($objItem.StartingOffset / 4096)
      if ([System.Math]::Round($divoffset,0) -eq $divoffset){
        $Details.VMName = $objItem.SystemName
        $Details.Partition = $objItem.Name
        $Details.Status = "Partition aligned"
        $Details.StartingOffSet = $objItem.StartingOffset
        $Details.Size = $objItem.Size
      }
      else{
        $Details.VMName = $objItem.SystemName
        $Details.Partition = $objItem.Name
        $Details.Status = "Partition NOT aligned"
        $Details.StartingOffSet = $objItem.StartingOffset
        $Details.Size = $objItem.Size
      }
    }
  $myCol += $Details
  }
}
$myCol | Export-Csv -NoTypeInformation $csvfile

Aligning VMDK Files

For alignment we'll use the mbralign tool. To do so, follow these steps:

  1. Remove any ESX snapshots from the VM that is to be realigned.
  2. Shut down the VM.
  3. Navigate to the directory of the VM you'd like to realign.
  4. Run the mbralign tool by using the command /root/mbralign <filename>.vmdk (NOTE: you are aligning the .vmdk file).
  5. To verify the virtual disk is now aligned run the mbrscan tool again.
  6. You should now be able to power the VM on.
  7. If the VM uses the grub boot loader (linux), please see appendix A.
  8. If the VM boots properly and you are satisfied with the result, the backup files should be removed from the virtual machine

directory in the datastore. These files end in “-mbralign-backup” and can be removed from the Virtual Center Client datastore
browser or from the ESX service console.

Aligning Example

Shutdown the VM and make sure no snapshots exist:
[root@esx01 WINXP01]# ll
total 6676992
-rw-r--r-- 1 root root      82109 Apr 12 16:20 vmware-45.log
-rw-r--r-- 1 root root      69145 Apr 12 16:20 vmware-46.log
-rw-r--r-- 1 root root      66815 Apr 12 16:20 vmware-47.log
-rw-r--r-- 1 root root      81427 Apr 12 16:20 vmware-48.log
-rw-r--r-- 1 root root      54797 Apr 12 16:20 vmware-49.log
-rw-r--r-- 1 root root      62878 Apr 12 16:20 vmware-50.log
-rw-r--r-- 1 root root      64727 Apr 12 20:33 vmware-51.log
-rw-r--r-- 1 root root      97232 May 12 14:29 vmware.log
-rw------- 1 root root 8589934592 May 12 14:29 WINXP01-flat.vmdk
-rw------- 1 root root       8684 Apr 13 22:33 WINXP01.nvram
-rw------- 1 root root        566 Apr 12 16:20 WINXP01.vmdk
-rw-r--r-- 1 root root         43 May 12 13:42 WINXP01.vmsd
-rwxr-xr-x 1 root root       2814 May 12 14:29 WINXP01.vmx
-rw-r--r-- 1 root root       1587 Apr 13 22:33 WINXP01.vmxf

Start the mbralign tool and confirm there are no snapshots:

[root@esx01 WINXP01]# /root/mbralign WINXP01.vmdk
 Part    Type          old LBA    New Start LBA      New End LBA     Length in KB
   P1      07               63               64         16755796          8377866

NOTICE:
This tool does not check for the existence of Virtual Machine snapshots or linked clones.
The use of this tool on a vmdk file that has a snapshot or linked clone associated with it
can result in unrecoverable data loss and/or data corruption.
Are you sure that no snapshots/linked clones exist for this vmdk? (y/n)y
Creating a backup of WINXP01.vmdk
Creating a backup of ./WINXP01-flat.vmdk
Creating a copy the Master Boot Record
Working on partition P1 (3): Starting to migrate blocks from 32256 to 32768.
12801 read ops in 6 sec.  59.82% read (15.74 mB/s).  59.82% written (15.74 mB/s)
---
12801 read ops in 2 sec.  98.88% read (46.99 mB/s).  98.88% written (46.99 mB/s)
Working on space not in any partition: Starting to migrate blocks.
100.00 percent complete.  100.00 percent written. .
Making adjustments to ./WINXP01-flat.vmdk.
Adjusting the descriptor file.

[root@esx01 WINXP01]# ll
total 15066688
-rw-r--r-- 1 root root      82109 Apr 12 16:20 vmware-45.log
-rw-r--r-- 1 root root      69145 Apr 12 16:20 vmware-46.log
-rw-r--r-- 1 root root      66815 Apr 12 16:20 vmware-47.log
-rw-r--r-- 1 root root      81427 Apr 12 16:20 vmware-48.log
-rw-r--r-- 1 root root      54797 Apr 12 16:20 vmware-49.log
-rw-r--r-- 1 root root      62878 Apr 12 16:20 vmware-50.log
-rw-r--r-- 1 root root      64727 Apr 12 20:33 vmware-51.log
-rw-r--r-- 1 root root      97232 May 12 14:29 vmware.log
-rw-r--r-- 1 root root 8589935104 May 12 14:38 WINXP01-flat.vmdk
-rw------- 1 root root 8589934592 May 12 14:29 WINXP01-flat.vmdk-mbralign-backup
-rw------- 1 root root       8684 Apr 13 22:33 WINXP01.nvram
-rw------- 1 root root        566 May 12 14:38 WINXP01.vmdk
-rw-r--r-- 1 root root        566 May 12 14:31 WINXP01.vmdk-mbralign-backup
-rw-r--r-- 1 root root         43 May 12 13:42 WINXP01.vmsd
-rwxr-xr-x 1 root root       2814 May 12 14:29 WINXP01.vmx
-rw-r--r-- 1 root root       1587 Apr 13 22:33 WINXP01.vmxf

[root@esx01 WINXP01]# /root/mbrscan WINXP01-flat.vmdk
--------------------
WINXP01-flat.vmdk p1 (NTFS)    lba:64  offset:32768    aligned:Yes
--------------------

As you can see, the vmdk file is aligned now and there are two backup files. The VM started successfully so the two backup files van be deleted.

Note: You can run the mbralign utility with several options:
Options:
   --sparse          The resulting file will be sparse.  This option is
                      designed for NetApp NFS based datastores.
   --force           The default behavior is to skip properly aligned disks.
                      The --force option can be used to
                      override this. (Useful for migrating and making sparse).
   --bs=n            Use n as the block size (specified in kB).  n must be one
                      of (8, 16, 32, 64, 128, 1024).  The default is 8.
   --codeview         Show what would be done, then exit.  No changes are made
                      to the file.
   --quiet           Only print warnings and errors (no other status isi
                      printed to screen).
   --debug           Print debug comments.
   --help            Print usage.
   --thereAreNoVmSnapshots  Supcodess the warning message about snapshots and
                             answer YES to the question of whether to continue.

Linux Systems Using GRUB

The following steps are required to make linux machines bootable again:
  1. After realignment, boot from old vmdk or CD (iso)
  2. Use grub to fix stage1.5 and stage2

Example Fixing MBR

grub>find /boot/grub/stage1
(hd0,0) [old, misaligned vmdk file]
(hd1,0) [new, aligned vmdk file]
grub>root (hd1,0)
grub>setup (hd1)
Checking if "/boot/grub/stage1" exists... yes
Checking if "/boot/grub/stage2" exists... yes
Checking if "/boot/grub/e2fs_stage1_5" exists... yes
Running "embed /boot/grub/e2fs_stage1_5 (hd1)"... 17 sectors are embedded.
succeeded
Running "install /boot/grub/stage1 (hd1) (hd1)1+17 p (hd1,0)/boot/grub/stage2 /boot/grub/menu.lst"...
succeeded
Done.

Creating Aligned Disks

To prevent the issue from happening you could also create new disks for new VMs with the partitions already aligned.

OS Disks

Virtual disks to be used as the boot disk can be formatted with the correct offset at the time of creation by connecting the new virtual disk to a running VM before installing an operating system and manually setting the partition offset. For Windows guest operating systems, you might consider using an existing Windows Preinstall Environment boot CD or alternative tools like Bart’s PE CD. To set up the starting offset, follow these steps.
  • Boot the VM with the WinPE CD.
  • Select Start → Run and enter DiskPart.
  • Enter Select Disk0.
  • Enter “create partition primary align=32”

Data Disks

To format virtual disks to be used as the data disk with the correct offset at the time of creation, use DiskPart in the VM. Attach the data disk to the VM. Check that there is no data on the disk and follow these steps:
  • Select Start → Run.
  • Enter diskpart.
  • Enter list disk to determine the disk # for the new data disk.
  • Enter select disk <disk_number> (for example, select disk 1).
  • Enter “create partition primary align=32”.
  • Enter exit to exit the DiskPart utility.
  • Format the data disk as you normally do

Lun Alignment Script

This script can be used to align VMDKs from the console on an ESX server:
Please note the following manual actions:
  • Check for enough diskspace since the VMDK files get backupped
  • VM should be running on the host where you run the script
  • After alignment disk mappings other than C: are lost and have to be mapped again manually
  • Remove backup files after manual check
    • For example: rm `ls *mbralign`
#!/bin/bash
########################################################################################################################
# Author : Sjoerd Hooft
# Date Initial Version: 27 December 2011
# Comments: sjoerd_warmetal_nl
#
# Description:
# Script to align the disks of a Virtual Machine
#
# Recommendations:
# The script is designed for a 120 column terminal.
# The running user must be root.
#
# Changes:
# Please comment on your changes to the script (your name and email address, line number, description):
# DATE - USERNAME - EMAILADDRESS - CHANGE DESCRIPTION
########################################################################################################################
 
# Possible improvements
# Ways out if the VM does not shut down or start up properly
# Email notification of the report/logfile
 
# Debug option; uncomment for debugging
# set -x
 
# Script Variables
HOSTNAME_SHORT=`hostname -s`
BASEDIR=`dirname $0`
WHATAMI=`basename $0`
LOGFILE="$BASEDIR/$WHATAMI.log"
DATE=`date +%Y%m%d`
# Highlight output on screen
BOLD=`tput bold`
BOLDOFF=`tput sgr0`
# Send all output to logfile; disable if screen output is needed
exec > $LOGFILE 2>&1
 
# Align Variables
align=/tmp/netapp/mbralign
 
# Write down the name of the Virtual Machine to align
inputvm=$1
 
if [ -z "$inputvm" ]; then
   echo
   echo "Usage $0 Script:"
   echo "--------------------------------------"
   echo "$BOLD $0 <NAME of VM> $BOLDOFF"
   echo "--------------------------------------"
   echo
   echo NAME of VM: Name of VM as found in vCenter, case sensitive
   echo
   exit
fi
 
functioncheckvm() {
vmxfile=`vmware-cmd -l | grep $inputvm`
 
checkvmexists=`vmware-cmd -l | grep $inputvm | wc -l`
 
if [ "$checkvmexists" != "1" ]; then
   echo VMexists is $checkvmexists
   echo VM $inputvm does not exist.
   echo Is $inputvm located on this host? If not please vMotion the VM to this host.
   echo Exiting...
   exit
fi
 
snapshotexist=`vmware-cmd $vmxfile hassnapshot | cut -f3 -d" "`
 
if [ "$snapshotexist" != "0" ]; then
   echo Snapshotexists is $snapshotexist
   echo VM $inputvm has snapshot, exiting...
   exit
fi
 
echo VM $inputvm does esixt and has no snapshots. Continuing...
 
}
 
functionstopvm() {
 
vmware-cmd $vmxfile stop trysoft
 
stopstate=on
while [ "$stopstate" != "off" ]
do
  stopstate=`vmware-cmd $vmxfile getstate | cut -f3 -d" "`
  echo Stopstate = $stopstate
  sleep 10
done
 
}
 
functionaligndisks() {
 
# Record time
echo start time is `date +%H%M`
 
for disks in $(cat $vmxfile | grep vmdk | grep -v vmfs | cut -f2 -d"\"" ); do
  dir=`dirname $vmxfile`
  echo $dir/$disks
  $align --thereAreNoVmSnapshots $dir/$disks
done
 
for disks in $(cat $vmxfile | grep vmdk | grep vmfs | cut -f2 -d"\"" ); do
  echo $disks
  $align --thereAreNoVmSnapshots $disks
done
 
# Record time
echo end time is `date +%H%M`
 
}
 
functionstartvm() {
 
vmware-cmd $vmxfile start trysoft
 
startstate=5
while [ "$startstate" != "1" ]
do
  startstate=`vmware-cmd $vmxfile gettoolslastactive | cut -f3 -d" "`
  echo Startstate = $startstate
  sleep 10
done
 
}
 
functioncheckvm
functionstopvm
functionaligndisks
functionstartvm
 
exit

LUN Alignment Tools

In the past I've evaluated a couple of alignment tools to see which option was better, using the script I wrote above or using paid 3rd party tools. These are my evaluation reports:

Paragon For ESX

I used this week to evaluate Paragon for ESX and am sorry that I have to inform you that the outcome is not positive:

  • Adding disks for alignment is slow and annoying. You can only add one disk per time, and only if the VM is turned off.
  • It is not possible to create projects/tasks so disks can be aligned after business hours
  • There is no user friendly report withe details on of the process, so required reports have to made manually
  • Speed with protection against reboots and file system errors is about 15 GB an hour. Speed without protection against reboots and file system errors is about 40 GB an hour which is still to slow.
  • I couldn't add vCenter as a connection
  • It wouldn't remember individual esx hosts
  • After the second try I was unable to add any connections at all, vcenter or esx servers. I got this error:
    • Can't connect to VMware Infrastructure
      • Exception: InvalidCastException; Name: Unable to cast object of type Vim25Api.VirtualDiskRawMappingVer1Backinginfo to type Vim25Api.VirtualDiskFlatVer2BackingInfo

There were also a few positive points:

  • Installation was very smooth
  • The tool requires just a little bit of free space on the datastore
  • Load on the SAN was quite acceptable during the alignment

Overall I would say that the technique used is very good, but would be better if it would do multiple disks at the same time so the overall speed would improve. I also really miss the option to schedule jobs while the VMs are still running. Using this tool would mean I'd have to get up at night, configure the tool and start the alignment. I would have to wait for it to finish so I can start the VMs before business hours. That makes it unacceptable.

Quest vOptimizer

  • Na installatie en scannen (duur: 1 werkdag) blijkt dat de tool niet kan werken vanaf een machine met meer dan 1 netwerkkaart. Dit stond niet in de documentatie en heeft een volledige dag werk gekost.
  • Indien een job in project mode draait lijkt er geen enkele manier te zijn om te kijken hoever de job is. Dit heeft als vervelend gevolg dat tijdens de voorbereiding en nazorg van een VM (die overigens ook heel lang duren) het niet duidelijk is of de job is vastgelopen of nog ergens actief mee bezig is.
  • In het begin mislukte het sturen van een email met het rapport. Het versturen per email is niet te testen, alleen de verbinding naar de SMTP server. Het is vervolgens dan ook onduidelijk waarom een email niet wordt verstuurd. Het niet verzonden rapport is vervolgens dan nergens in te zien.
  • Bij het gebruik van verschillende credentials moet dit per VM op een omslachtige manier ingesteld worden. Vervolgens kan je niet 1 VM scannen voor alignment, dan gaat de tool gelijk alle VMs scannen. Dit zou misschien nog acceptabel zijn indien je dan door kan werken, dit is niet het geval, de Alignment scan window heeft de focus en wil deze niet loslaten.
  • De tool aligned slechts 2 VMs maximaal tegelijk, en ook nog eens maar 1 disk per keer. Nog zonder backup optie deed de alignment van een VM met 96 GB er 3 uur en 6 minuten over. Een VM met 45 GB deed er 2 uur en 10 minuten over. Dat is tussen de 20 en 30 GB per uur.
  • Ik heb de volgende foutmeldingen gezien:
    • Stage: VmStartupBootImage, Description: Timeout expired when trying to establish connection with Boot image.
    • Errors Count: 2
    • Stage: DatastoreSpaceReclaim, Description: Insufficient free space on datastore to complete operation. VMDK: [04A129_DATA] amsactd02/amsactd02.vmdk, space used on datastore before operation: 53687091200, requested space: 107365235712.
    • Stage: DatastoreSpaceReclaim, Description: Insufficient free space on datastore to complete operation. VMDK: [04A129_DATA] amsactd02/amsactd02_1.vmdk, space used on datastore before operation: 26843545600, requested space: 53682601984.
      • De onderste is ook nog eens incorrect. De desbetreffende datastore heeft genoeg vrije ruimte beschikbaar. Het is niet mogelijk om een andere datastore aan te wijzen voor backup of tijdelijke bestanden.

Je begrijpt denk ik wel dat dit alles bij elkaar niet zo positief is. Vaak werkt de tool niet, als hij werkt is het niet echt gebruikersvriendelijk en hij is traag. Helaas kan ik dus geen positief advies geven. Ik krijg er geen warm gevoel van en heb eigenlijk het idee dat de alignment optie niet “af” is, dat het er meer bij is gezet voor de echte functionaliteit, namelijk storage space reclaiming.

Overigens ben ik wel te spreken over de support en de hulp, nogmaals mijn dank daarvoor.

Discussion

Enter your comment:
 
lunaligning.txt · Last modified: 2012/02/22 14:52 by sjoerd