VMware vSphere

 View Only
Expand all | Collapse all

Partition Alignment PowerCLI Script for LINUX VM

  • 1.  Partition Alignment PowerCLI Script for LINUX VM

    Posted Dec 22, 2014 05:08 PM

    Folks,

    I am looking for some examples (or even references) of powerCLI partition alignment script for LINUX VM (using FDISK.). I have already found all information for Windows VM using wmiobject -class "Win32_DiskPartition", but not for LINUX VMs.

    Any help is appreciated.

    Regards,

    Andy



  • 2.  RE: Partition Alignment PowerCLI Script for LINUX VM

    Posted Dec 23, 2014 09:40 AM

    For Windows like you said you can use powershell, for Linux i think you have to either script something using fdisk and let it run on a host to scan the vmdks or you are or know a netapp customer and use the handy mbrscan utility netapp customers get.

    For the doing you could use different ways. Again, a netapp tool called mbralign which is really nice because it is a.) a command line tool plus scriptable and b.) works not only with netapp storage. Or you could use the VMware converter for alignment, or you could one of the many gparted live cd´s .



  • 3.  RE: Partition Alignment PowerCLI Script for LINUX VM

    Posted Jan 12, 2015 09:39 PM

    Hi 

    FDISK command executed from a LINUX VM shell:

    /sbin/fdisk -lu /dev/sd[a-z] | grep -E sd[a-z][0-9]+ | sed s/*// |  awk '{printf ("%s %f ",$1,$2/512); if($2%512){ print "PARTITION IS NOT ALIGNED!" }else {print "PARTITION IS ALIGNED!"} }' | column -t

    /dev/sda1  4.000000     PARTITION  IS  ALIGNED!

    /dev/sda2  2004.000000  PARTITION  IS  ALIGNED!

    PowerCLI code part:

    $toolsStatus = (Get-VM $vm | Get-View).Guest.ToolsStatus

    if($toolsStatus -eq "toolsOk"){

        Write-Host "VMware Tools is Installed!"

        $script =  "/sbin/fdisk -lu /dev/sd[a-z] | grep -E sd[a-z][0-9]+ | sed s/*// |  awk '{printf ("%s %f ",$1,$2/512); if($2%512){ print "PARTITION IS NOT ALIGNED!" }else {print "PARTITION IS ALIGNED!"} }' | column -t"

           Invoke-VMScript -VM $vm -ScriptText $script -GuestCredential $gc

            }else{

        Write-Host "Virtual Machine $vm is not operational or it does not have VMware Tools Installed. Virtual Machine partitions can not be aligned. Please check virtual machine $vm status or install VMware Tools before proceeding!!!" -ForegroundColor Red

        exit

    }

    PowerCLI output (ERROR)


    You must provide a value expression on the right-hand side of the '%' operator.

    At C:\\invoke.ps1:19 char:101

    +     $script =  "/sbin/fdisk -lu /dev/sd[a-z] | grep -E sd[a-z][0-9]+ | sed s/

    *// |  awk '{printf ("% <<<< s %f ",$1,$2/512); if($2%512){ print "PARTITION IS

    NOT ALIGNED!" }else {print "PARTITION IS ALIGNED!"} }' | column -t"

        + CategoryInfo          : ParserError: (:) [], ParseException

        + FullyQualifiedErrorId : ExpectedValueExpression

    Have you guys seem something similar?

    Any help is really appreciated! tks



  • 4.  RE: Partition Alignment PowerCLI Script for LINUX VM

    Posted Jan 13, 2015 08:10 AM

    May I ask why you want to do this? Aligning partitions makes sense only in physical servers, or VMs with pass-through raw disks. But I'm pretty sure every common linux-distro not older than 2 years does this automatically unless you are using some trully pre-historical util-linux version (for disk-access libblkid block-device library is used by fdisk).

    But if your VMs do not use pass-through disks (VM-disk resides on esxi-storage), it does not make any sense, because "sectors" inside VM are not the same as physical sectors on the disk (there is vmfs-layer in between). You would not gain anything even if you "aligned" your partitions to such "sectors"...



  • 5.  RE: Partition Alignment PowerCLI Script for LINUX VM

    Posted Jan 13, 2015 08:42 AM

    There is a lot "religious" discussion around whether guest aligment in Vsphere 5 is useful or not. In earlier version it used to be a recommendation by vmware in their best practices pdf´s. Here is a nice blog post about VSphere 5 regarding that matter:

    Partition Alignment and block size VMware 5 | Virtual Me



  • 6.  RE: Partition Alignment PowerCLI Script for LINUX VM

    Posted Jan 13, 2015 09:11 AM

    But that post does not say it makes much sense to align partitions in VM. All it says is the topic is quite difficult.

    BTW, except for sector-size, raid-strip and vmfs, there is one more layer he omitted (was maybe not so important at the time of writing): "block-size" of SSD. R/W-operations on SSD are done on "blocks". This size is not the same over different models/vendors, and not easy to find. And to make things a little more complicated, every SSD is basically raid-array of its own (multi-channel controllers).

    Another complication is vmfs5: it has block-size 1MB and sub-block 8kB. But it also supports very small files and stores them in meta-data. How can you "translate" this to VM (and align this operation to sector-boundary)?

    So unless there is correct "VM_filesystem -> vmfs5 -> raid_strip -> ssd_block -> sector" translation on every interface, partition alignment does not make much sense. We can only hope that "everybody does his best" (hdd/ssd-vendors, raid-controller vendors, VMware Inc., OS-vendors). And that's what I mean: raid-controller aligns raid-strips on 4k-boundary (unfortunatelly does not know anything about block-size of different ssd-controllers), ESXi (I hope) aligns vmfs and vdisk-files on 4k-boundary, and OS aligns partitions on 4k-boundary. Then we should be safe and can not do much more...



  • 7.  RE: Partition Alignment PowerCLI Script for LINUX VM

    Posted Jan 13, 2015 09:46 AM

    Pre VSphere 5 i´ve met people who had made horrible experiences performance wise by not aligning their guests and they had to realign all their vm´s in their infrastructure which can be a lot of work and load on your storage depending on the infrastructure size. I´ve done it since we started with vmware in 2006 and i´ve made good experience with it, so it used to be an established process in vm deployment in my company. Now of course with modern OS it is more and more negligible.



  • 8.  RE: Partition Alignment PowerCLI Script for LINUX VM

    Posted Jan 13, 2015 08:10 AM

    To be honest i did not have to deal with alignment inside the guest. We use an automated OS deploy solution and in the past with XP and 2003 this software was unable to create aligned partitions so i had to align the disks of the vm after the operating system was deployed. mbralign (or mbrscan just to check alignment status ) did exactly that, without any issues for years. Nowadays we use 2008R2 and also Linux distributions which create aligned partitions by default, so today it is not so much of a deal anymore. Unfortunately i have no experiance with doing that with powershell on linux.



  • 9.  RE: Partition Alignment PowerCLI Script for LINUX VM

    Posted Jan 13, 2015 12:24 PM

    Hi there, the problem is in how PowerShell handles single and double quotes. Use escape character: ` in front of any special character:

    $script = "/sbin/fdisk -lu /dev/sd[a-z] | grep -E sd[a-z][0-9]+ `| sed s/*// `|  awk `'{printf (`"`%s `%f `",`$1,`$2/512); if(`$2`%512) `{ print `"PARTITION IS NOT ALIGNED!`" `}else `{print `"PARTITION IS ALIGNED!`"`} `}`' `| column -t"

    A Per Cent operator is a short for ForEach-Object in PowerShell, so the parser thinks you are trying to invoke that operation. When you put this escape character in front of a special character, it tells PowerShell to ignore the parse.

    OR even better, store the command in a text file, for example command.txt and import it as a string via Get-Content:

    $script = Get-Content .\command.txt

    Try replacing the block with what I provided above and post feedback please :smileyhappy:



  • 10.  RE: Partition Alignment PowerCLI Script for LINUX VM

    Posted Jan 13, 2015 04:57 PM

    Hi Alistar, thank you for the information.

    If I try to work without Get-Content I get the error presented below.

    awk: {printf ("%s %f ",,/512); if(%512) { print "PARTITION IS NOT ALIGNED!" }else {print "PARTITION IS ALIGNED!"} }

    awk:                   ^ syntax error

    awk: {printf ("%s %f ",,/512); if(%512) { print "PARTITION IS NOT ALIGNED!" }else {print "PARTITION IS ALIGNED!"} }

    awk:                     ^ unterminated regexp

    awk: cmd. line:1: {printf ("%s %f ",,/512); if(%512) { print "PARTITION IS NOT ALIGNED!" }else {print "PARTITION IS ALIGNED!"} }

    awk: cmd. line:1:        

    Using Get-Content Approach seems to have a small error, which is listed below.

    bash: -c: line 0: unexpected EOF while looking for matching `"'

    bash: -c: line 1: syntax error: unexpected end of file

    Do you have some idea of what could be causing the EOF error?

    Thank you again for your help!

    I really appreciate!



  • 11.  RE: Partition Alignment PowerCLI Script for LINUX VM

    Posted Jan 14, 2015 07:20 AM

    Hi again,

    try checking the command that is present in the text file whether you will be able to determine any missing quotes or a special character - the command in the text file should be exactly like it works in the linux shell.

    Anyways, there is a load of posts on the error that you get with the get-content - try googling it up - it means that there is an error in syntax, so double-check your shell command (or slowly build it up) and you should be all set.