Partition alignment in RHEL environments – ks

July 11, 2010

In the last few months, I recurringly stumbled upon mentions and articles of the infamous “partition alignment problem”, that can cause serious performance hits on i/o, particularly in virtualized environments and with RAID based disk subsystems.

I won’t start from scratch trying to explain you what the problem is, since many gurus already did an amazing job of describing the issue. My favorite post in this case is Duncan’s : http://www.yellow-bricks.com/2010/04/08/aligning-your-vms-virtual-harddisks/ , where he clearly explains what’s the issue behind partition alignment, and (particularly for vmware environments) what you should and could do to prevent/fix the issue.

The easy part in this scenario is how to align vmfs partitions: as long as you use vcenter server to create the partition, it takes care of proper alignment, and you can get rid of that side of the problem. Now on the “guest side” of the issue, where each and every OS take a different approach to partition alignment. Basically, partitioning has always been approached using the CHS (Cylinder,Head,Sector) technique, which doesn’t consider the blocks and tracks where the actual data is written to. It’s good for its simplicity, but definitely not good performance-wyse.

For example, common Linux distros and even MS Windows Server 2003 misalign partitions, since they rely on cylinder boundaries. Windows 2008 server, by contrast, aligns partitions to 1MB, which is safe and cool generally, regardless of the storage array that sits behind your virtualized environment (different storage arrays have different chunk sizes, thus your “alignment needs” may vary – refer to your storage vendor’s docs to know what your chunk size is).

Ok, so we’re talking about Linux here, and in my case, RHEL environments. What should I do to:

a) install properly-aligned systems

b) fix already installed systems w/ misaligned partitions.

Before I go on, let me  underline the fact that while this may seem a “tweak”, and maybe not an easy/cheap one, this can make a huge difference, particularly if you consider many guests that waste i/o resources by unnecessarily stressing your consolidated storage array. Using partition alignment as a best practice can improve your performance and save you bucks! 

 In this post I’m gonna show you how we solved the first scenario, by preparing a customized kickstart file that takes care of the partition aligment process. At least this allows us to avoid creating new vms that will need to be fixed sooner or later 🙂 .

In a future post I will describe the procedure we’re taking in order to fix already installed and misaligned systems (we’re still evaluating the different possibilities).

The kickstart file

Our approach to linux installs is to use a pxe server to boot the newly created vm, and to install the linux guest via network. Setting up a pxe boot server is easy and covered with great detail throughout the web, so I won’ t bother you with this part of the process. The good thing here is that RHEL systems provide you with a way of customizing beforehand the installation process, so that it can be automated in nearly every aspect of it (partition, grub installation, package selection, post installation activities, etc.). In one way this speeds up A LOT your installations, and on the other it provides the chance to configure an automated/repeatable setup (and maybe align partitions properly 😉 ).

Regarding the “partitioning issue”, We discovered that the default partitioning methods available with the RHEL installer simply can’t be used, since they rely on the CHS mechanism, and thus align partitions to cylinder boundaries, which is not what we need. We need to align to sector boundaries, and to choose the exact sector that we want our partitions to start.

The “%pre” section in the kickstart file allows to execute operations that are executed before the installation process (I’ll let you guess what the %post section does 😉 ). So we decided to dig more into this, and to use GNU parted to define the partitions the way we wanted.

So, here’s a sample %pre section that does all the job:

%pre

#section 1

TOTAL_RAM=`expr $(free | grep ^Mem: | awk ‘{print $2}’) / 1024`

if [ $TOTAL_RAM -lt 2048 ];

then

        SWAP_SIZE=2048

else

       SWAP_SIZE=4096

fi

# section 2

dd if=/dev/zero of=/dev/sda bs=512 count=1

parted -s /dev/sda mklabel msdos

# section 3

TOTAL=`parted -s /dev/sda unit s print free | grep Free | awk ‘{print $3}’ | cut -d “s” -f1`

SWAP_START=`expr $TOTAL – $SWAP_SIZE \* 1024 \* 2`

SWAP_START=`expr $SWAP_START / 64 \* 64`

ROOT_STOP=`expr $SWAP_START – 1`

parted -s /dev/sda unit s mkpart primary ext3 128 $ROOT_STOP

parted -s /dev/sda unit s mkpart primary linux-swap $SWAP_START 100%

What are we doing here?

In this simple scenario, we want to guess the amount of RAM available on the system, in order to size the swap partition. Then, we want to split our disk into two partitions, one holding / , and the other holding the swap.

The first section calculates the amount of available RAM and defines two possible sizes of the swap partition: 2GB for system with <2GB of RAM, 4GB for larger systems.

Then we go on the interesting part:

The second section starts by erasing the partition table (by using dd) and creating an empty one.

The third section calculates the amount of sectors available in the disk, then calculates the start sector for the swap partition, which is one sector ahead of the end of the root partition.

if you look at the math being done here (okok, I know… it's a quickie 😉 ), we're substracting N GB (in sectors) from the end of the disk, divide by 64 to get an int number, and multiply back to 64, to get a sector number that is aligned. This gives us also an aligned swap partition! Substracting one sector to this figure gives us the end sector of the root partition. The remaining stuff is easy: We define the two partitions: the first starts from sector 128 and goes on to SWAP_START – 1, the second is the swap and starts from SWAP_START up to the whole disk

Advertisements

4 Responses to “Partition alignment in RHEL environments – ks”

  1. Pierre B Says:

    Can you explain why you start the ROOT partition at sector 128? Thank you, PBR

  2. Damion Says:

    Did you ever post the follow up to fix already installed and misaligned systems?

    • Michael Kruger Says:

      I think you might be able to simply use gparted via parted magic boot disk and slightly shrink then align the partitions. Be sure to uncheck the box that aligns partitions to cylinders though.

  3. Randall Says:

    Writing articles is pain in the ass.I know how you can get unlimited articles for your blog, search in google:

    Anightund’s rewriter


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: