This is a small script to check GUID partition alignment to the EBS (Erase block size) of Solid State Disks. The EBS is expressed in KB and will be converted into sectors (x 2). Don’t give an EBS of 2048! It would match 4096 sectors. AFAIK the EBS should be 512 KB for SLC NAND (Single Level cell) and 1024 KB for MLC NAND (Multi Level cell). But you should check on manufacturers’ websites if they provide this info (which seems to be yet another hidden one).
The script uses gdisk](http://www.rodsbooks.com/gdisk/) to list the partitions installs it if needed. You’ll likely run this script on a live CD and install gdisk on the live system.
I branched gdisk from the author’s project in my repo to make it easier to install for me (and for you).
Syntax is simple:
checkalign /dev/sda
which is the same as:
checkalign -e 1024 /dev/sda
You can specify any other multiple of 512 as block size.
Example:
Partitions are aligned on 1024 KB (2048 sector) boundaries:
# checkalign /dev/sda
1: 321535 - 2048 + 1 = 319488 / 2048 = 156.00 => aligned
2: 4530175 - 321536 + 1 = 4208640 / 2048 = 2055.00 => aligned
3: 88422399 - 4530176 + 1 = 83892224 / 2048 = 40963.00 => aligned
4: 234440703 - 88422400 + 1 = 146018304 / 2048 = 71298.00 => aligned
In this case, the are also aligned on 512 KB (1024 sector) boundaries:
# checkalign -e 512 /dev/sda
1: 321535 - 2048 + 1 = 319488 / 1024 = 312.00 => aligned
2: 4530175 - 321536 + 1 = 4208640 / 1024 = 4110.00 => aligned
3: 88422399 - 4530176 + 1 = 83892224 / 1024 = 81926.00 => aligned
4: 234440703 - 88422400 + 1 = 146018304 / 1024 = 142596.00 => aligned
They might even be aligned on 2048 KB (4096 sector) boundaries:
# checkalign -e 2048 /dev/sda
1: 321535 - 2048 + 1 = 319488 / 4096 = 78.00 => aligned
2: 4530175 - 321536 + 1 = 4208640 / 4096 = 1027.50 => aligned
3: 88422399 - 4530176 + 1 = 83892224 / 4096 = 20481.50 => aligned
4: 234440703 - 88422400 + 1 = 146018304 / 4096 = 35649.00 => aligned
But probably not on 1536 KB (3072 sector) boundaries:
# checkalign -e 1536 /dev/sda
1: 321535 - 2048 + 1 = 319488 / 3072 = 104.00 => aligned
2: 4530175 - 321536 + 1 = 4208640 / 3072 = 1370.00 => aligned
3: 88422399 - 4530176 + 1 = 83892224 / 3072 = 27308.67 => not aligned
4: 234440703 - 88422400 + 1 = 146018304 / 3072 = 47532.00 => aligned
Here’s the script:
#!/bin/bash
#: Title : checkalign
#: Date Created : Wed Jan 25 00:41:44 PST 2012
#: Last Edit : Wed Jan 25 15:02:25 PST 2012
#: Author : please_try_again
#: Version : 1.0
#: Description : Checks if GUID partitions are aligned to the given EBS of SSD isk (default is 1024)
#: Options : EBS in kb (1024 or 512)
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# current version
version="1.0"
EBS=1024
EBSMIN=512
VER=$(lsb_release -rs 2>/dev/null)
VER=${VER:-12.1}
# -------------------------------------------------
function gdisk_install {
while "$yesno" != "y" -a "$yesno" != "n" ] ; do
read -p "$(tput setaf 1)gdisk not installed. Do you want to install it? (y|n) $(tput sgr0)$(tput bold)" -N 1 yesno
echo
"$yesno" ] && yesno=$(echo $yesno | tr "YN" "yn")
"$yesno" == "n" ] && exit
"$yesno" == "y" ] && break
printf "%sPlease answer with n or y.%s
" "$(tput setaf 1)" "$(tput sgr0)"
done
sudo zypper ar http://download.opensuse.org/repositories/home:/please_try_again/openSUSE_$VER/ PTA
sudo zypper --non-interactive --gpg-auto-import-keys refresh -r PTA
sudo zypper --non-interactive in -r PTA gdisk
echo
}
args=`getopt -q -u -o e: -l ebs: -- "$@"`
set -- $args
for i; do
case "$i" in
-e|--ebs) shift ; EBS=$1 ; shift ;;
-h|--help) syntax ; shift ;;
--) shift ; break ;;
esac
done
dev=$1
shift
# Possible Errors
"$dev" ] || exec echo "missing argument"
"$1" ] && exec echo "too many arguments"
-b $dev ] || exec echo "invalide block device"
-e /sbin/udevadm ] && eval $(/sbin/udevadm info --query=property --name=$dev | awk '/DEVTYPE|ID_PART_TABLE_TYPE/ { printf "%s;", $0 }') || exec echo "udevadm not found"
"x$DEVTYPE" == "xdisk" ] || exec echo "not a disk device"
"x$ID_PART_TABLE_TYPE" == "xgpt" ] || exec echo "no GPT found on disk device"
$(($EBS % $EBSMIN)) -gt 0 ] && exec echo "invalid EBS: $EBS"
-e /usr/sbin/gdisk ] || gdisk_install
# Finally run gdisk
sudo /usr/sbin/gdisk -l $dev | sed '1,/^Number/d' | awk '\
BEGIN { EBS = "'"$EBS"'" ; EBS *= 2 } ;\
/primary/ { S = $3 - $2 + 1 ; D = S / EBS ; M = S % D ; if ( M > 0 ) A="not aligned" ; else A="aligned" ;\
printf "%s: %12i - %10d + 1 = %12d / %d = %12.2f => %s
", $1, $3, $2, S, EBS, D, A }'
And no, I’m not going to write a fdisk/MBR version … But feel free to write it. It’s just one line of code in awk (3 lines with line breaks here).