Additional Virtualization Discussions

Creating Properly Aligned Disks for Linux Virtual Machines

bjon
9,477 Views

As documented in NetApp TR-3747 “Best Practices for File System Alignment in Virtual Environments”, it is necessary to align properly each layer of storage between the virtual server and the underlying storage.  For anyone not familiar with why proper disk alignment is crucial for virtual environments, please review TR-3747.  In the context of a Linux disk image, this means that each partition needs to start at a sector number that is cleanly divisible by 8. For contrast, a legacy partitioning tool will create a default starting at sector 63, but an aligned first partition will start at 64 or 128.  As a side note, the information posted in this blog will be used to update TR-3747 in the very near future.

In truth, any of the existing legacy partitioning tools can be used to create properly aligned disk images, but specifically using the "parted" tool is perhaps the most elegant way to script the process.  This is especially within the context of using Kickstart for repeatable installations.  The Linux disk images that are aligned with this process can then be used within KVM and RHEV (Red Hat Enterprise Virtualization).

I won't actually be discussing how to setup a Kickstart, only the 2 sections of a Kickstart file that are relevant to disk alignment.  For anyone that needs a refresher on Kickstart, the information is widely available, including at http://www.redhat.com/docs/en-US/Red_Hat_Enterprise_Linux/5.4/html/Deployment_Guide/index.htmlhttp://www.redhat.com/docs/en-US/Red_Hat_Enterprise_Linux/5.4/html/Deployment_Guide/index.html

The 2 sections of the Kickstart file that we are concerned with are the optional "%pre" section at the end of the file and the section that specifies the disk layout.

After creating or modifying a Kickstart file, add in a "%pre" section at the end of the file, such as the following:

%pre
parted /dev/sda mklabel msdos
parted /dev/sda mkpart primary ext3 64s 208718s
parted /dev/sda mkpart primary 208848s 100%
parted /dev/sda set 2 lvm on


The first “parted” command creates a disk label on disk “/dev/sda”, and the next creates a primary partion of just over 100MB that starts at sector 64.  The third command then creates a second primary partition starting at the next available “alignment friendly sector” of 208848 that takes up whatever disk space is left over.  The final command simply changes the partition type to “LVM” for the second partition.  Keep in mind that additional partitions can be created, provided they each start on alignment friendly sectors (those that are cleanly divisible by 8).  Also, the “/dev/sda” can be edited to “/dev/hda” or “/dev/vda” depending on how the operating system sees the disk.

The other important section in the Kickstart file is where the disk layout is specified.  See the difference between this (note that all options below are “--” or 2 dashes):

zerombr yes
clearpart --linux --drives=sda
part /boot --fstype ext3 --size=100 --ondisk=sda
part pv.2 --size=0 --grow --ondisk=sda
volgroup VolGroup00 --pesize=32768 pv.2
logvol swap --fstype swap --name=LogVol01 --vgname=VolGroup00 --size=1008 --grow --maxsize=2016
logvol / --fstype ext3 --name=LogVol00 --vgname=VolGroup00 --size=1024 --grow


And this (note that all options below are “--” or 2 dashes):

zerombr yes
##clearpart --linux --drives=sda
part /boot --fstype ext3 --onpart sda1
part pv.2 —onpart sda2
volgroup VolGroup00 --pesize=32768 pv.2
logvol swap --fstype swap --name=LogVol01 --vgname=VolGroup00 --size=1008 \--grow --maxsize=2016
logvol / --fstype ext3 --name=LogVol00 --vgname=VolGroup00 --size=1024 --grow


The first difference is that the “clearpart” line is commented out (it can also be deleted).  If left in, it will simply wipe out the partitions created in the “%pre” section that were just created.  The next difference is the 2 partitions don’t have size directives; the sizes were already predetermined in the “%pre”, thus we only need to specify filesystem type and partition.  Keep in mind that if the “%pre” section specifies “/dev/sda” or “/dev/vda”, the disk layout in the main section will need to follow suit.

For those that don’t have easy access to a Kickstart server, this can easily be converted to a script that is not dependant on Kickstart.   Here is a rudimentary Bash script called “align_me.sh” that takes a single disk image file and lays out the aligned partitions:

#!/bin/bash

DISK=$1

if [ -z "$1" ]; then
     echo -e "\nA disk image file needs to be specified...\n”\"
     echo -e "Usage; align_me.sh <disk_image_file>\n"
     exit 1
  fi

parted $DISK mklabel msdos
parted $DISK mkpart primary ext3 64s 208718s
parted $DISK mkpart primary 208848s 100%
parted $DISK set 2 lvm on

echo -e "\nNew Disk Layout:"
fdisk -lu $DISK


To use it, create a disk image file with whichever toolset your particular distribution comes with, such as “qemu-img”, then use the script shown against the disk file.  Here is an example:

[user@host ~]# qemu-img create –f raw disk_file.img 10G
[user@host ~]# align_me.sh disk_file.img

(If the scripts above are going to be copied and pasted, be sure that single dashes (-), double-dashes (--), and parenthesis (") are copied properly, the scripts will error out.)

Qemu-img is first used to create a 10GB virtual disk image named “disk_file.img”, and then the align_me.sh script is used to layout the aligned partitions.  The last step would then be to manually install the operating system.  On Red Hat and Fedora systems, do things the default way with the install CD or DVD, except when the disk utility comes up.  At that point, the "custom layout" option will need to be chosen so that the newly created partitions can be matched up with a mount point. 

Hopefully, this sheds some light on how to properly align Linux disk images for use with NetApp storage.

2 REPLIES 2

brasbehlph
9,477 Views

Great Post, however I did run into a  problem with the kickstart %pre section hanging on "running kickstart %pre scripts" on CentOs 5.5.

After further research I found that the "parted" command needed the -s or --script to never prompt the user.

The modified %pre section in the kickstart file is below.


%pre
parted -s /dev/sda mklabel msdos
parted -s /dev/sda mkpart primary ext3 64s 208718s
parted -s /dev/sda mkpart primary 208848s 100%
parted -s /dev/sda set 2 lvm on
%end

Hopefully this helps someone not get stuck during kickstart a kick start install.

joerg_loges
9,477 Views

the %pre section part worked fine for me up to RHEL/Centos 5.6, but I am supposed to install 5.8 now and running into trouble that the installer always complains about not enough space for /boot. Does somebody have experience with that version? The kickstart seems to have now options like --start --end which I couldn't get to work out for me

Public