Created
July 11, 2011 02:35
-
-
Save tobert/1075234 to your computer and use it in GitHub Desktop.
GPT partitioning function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Compute partition sizes based on system memory (swap) and disk size. | |
# There is no /boot because grub2 uses the BIOS Boot Partition (EF02) | |
# to write out the payloads that used to have to be in /boot at the | |
# front of the disks. | |
# The EFI Partition will end up unused on all of our legacy BIOS-only | |
# systems, but it's only 128MB and can remain consistent as the EFI/UEFI | |
# systems take over. | |
write_gpt () { | |
device=$1 | |
name=$(basename $device) | |
# get the size of the disk in bytes | |
disk_bytes=$(blockdev --getsize64 $device) | |
# the last sector isn't always (or ever) size/sectorsize | |
last_sector=$(blockdev --getsize $device) | |
# this should support 4k sectors automagically | |
ss=$(blockdev --getss $device) | |
# measure RAM to use for swap size | |
ramkb=$(awk '/MemTotal:/{print $2}' < /proc/meminfo) | |
rambytes=$((ramkb * 1024)) | |
# BIOS Boot (for grub2), 1MB | |
bios_start=2048 | |
bios_end=$((bios_start + 1048576/ss)) | |
# EFI partition, 128MB | |
efi_start=$((bios_end + 1)) | |
efi_end=$((efi_start + 134217728/ss)) | |
# Linux root RAID1, 40GB or 1/2 remaining disk, whichever is smaller | |
remaining=$((last_sector - swap_end)) | |
half_remaining=$(((remaining-remaining%2)/2)) | |
root_start=$((efi_end + 1)) | |
root_end=$((root_start + 42949672960/ss)) | |
# when testing, I may have way less than 40GB for speed, so | |
# split remaining space in half between root/data if that happens | |
if [ $((root_end - root_start)) -gt $half_remaining ] ; then | |
root_end=$((root_start + half_remaining)) | |
fi | |
# Linux swap, 1x RAM or 10% of HDD, whichever is smaller | |
swap_start=$((root_end + 1)) | |
swap_sectors=$(((rambytes-rambytes%ss)/ss)) | |
max_swap_sectors=$(((last_sector-last_sector%10)/10)) | |
if [ $swap_sectors -gt $max_swap_sectors ] ; then | |
swap_end=$((swap_start + max_swap_sectors)) # 1/10th disk, probably only in test VM's | |
else | |
swap_end=$((swam_start + swap_sectors)) # 1x RAM | |
fi | |
# RAID1 Data Partition | |
data_start=$((swap_end + 1)) | |
data_end=0 | |
# clear & create a new GPT label | |
sgdisk -Z $device | |
sgdisk -og $device | |
# create partitions | |
sgdisk -n 1:$bios_start:$bios_end -c 1:"BIOS Boot Partition" -t 1:ef02 $device | |
sgdisk -n 2:$efi_start:$efi_end -c 2:"EFI System Partition" -t 2:ef00 $device | |
sgdisk -n 3:$root_start:$root_end -c 3:"Linux Root RAID1" -t 3:fd00 $device | |
sgdisk -n 4:$swap_start:$swap_end -c 4:"Linux Swap" -t 4:8200 $device | |
sgdisk -n 5:$data_start:$data_end -c 5:"DATA RAID1" -t 5:fd00 $device | |
sgdisk -p $device | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment