sgdisk disk script to split disk into 2 partitions
####################################

#!/bin/bash

# WHAT THIS DOES:
# imagine last good sector (with regards to alignment is) is 2000000 on drive /dev/sdf (about a gig is 2 mil sectors)
# we need to partition to 2 equal partitions
# dd if=/dev/zero of=${DEV2} bs=1M count=100
# sgdisk -z /dev/sdf
# sgdisk -Z /dev/sdf --- thats a 500 mb swap partitions
# sgdisk -g /dev/sdf
# sgdisk -og /dev/sdf --- thats a 500 mb swap partitions
# sgdisk -n 1:2048:1000000 -c 1:"SWAP" -t 1:8200 /dev/sdf --- thats a 500 mb swap partitions
# sgdisk -n 2:1000001:2000000 -c 2:"LINUX" -t 2:8300 /dev/sdf
# sgdisk -p ${DEV2}
# --- about sgdisk -E ---
# we use -E to give us last good sector of the free space
# which if it was partitioned the last sector would be the last sector of 1st partition
# but in this case it will be last sector of the over all device

# READ ADVICE: http://www.rodsbooks.com/gdisk/advice.html

echo "=====MISSION========="
echo "PARTITIONING: ${DEV2}"
echo "INTO 2 EQUAL PARTITIONS"
echo "OR CLOSE TO IT"
echo "=====GIVEN SIZES====="

# set the device that you want to partition (not the partitions, but the device - so dont put /dev/sda5, but put /dev/sda - see your available devices with "cat /proc/partitions")
# set the start sector (multiple of 2k is good for alignment, picking 2048 is great, read the advice link above)
DEV2="/dev/sdf"
STARTSECTOR=2048
echo "START SECTOR IS: ${STARTSECTOR}"

# now the system calculates the current end sector - this number might change after the disk is wiped clean of partitions
# it might change because currently the sgdisk -E command shows the last good sector in device, and if there is a partition, it shows the last good sector in the first partition (as I understand)
# thats why we calculate it here and show you the half way points
# then we calculate it again after the disk is wiped
# we use the final new number after the disk is wiped clean of partitions, because those are the more accurate numbers
ENDSECTOR=`sgdisk -E $DEV2`
echo "END SECTOR IS: $ENDSECTOR"
HALFSECTOR=$((ENDSECTOR/2))
echo "HALF POINT SECTOR IS: $HALFSECTOR (END LOCATION OF 1st PARTITION)"
HALFSECTOR1=$((HALFSECTOR+1))
echo "HALF POINT + 1: $HALFSECTOR1 (START LOCATION OF 2nd PARTITION)"

# set what you want your 1st partitions name to be, and type (types are listed below)
# set what you want your 2nd partitions name to be, and type (types are listed below)
PART1NAME="SWAP"
PART1TYPE=8200
PART2NAME="EXTRA"
PART2TYPE=8300

# FOR PART TYPE (meaning partition type): -t, --typecode=partnum:{hexcode|GUID}
# Change a single partition's type code. You enter the type code using either a two-byte hexadecimal number, as described earlier, or a # fully-specified GUID value, such as EBD0A0A2-B9E5-4433-87C0-68B6B72699C7.

# each row has 3 type in this fashion:
# hexcode name hexcode name hexcode name (the name might have spaces so dont let throw you off)
# 0700 Microsoft basic data 0c01 Microsoft reserved 2700 Windows RE 
# 4200 Windows LDM data 4201 Windows LDM metadata 7501 IBM GPFS 
# 7f00 ChromeOS kernel 7f01 ChromeOS root 7f02 ChromeOS reserved 
# 8200 Linux swap 8300 Linux filesystem 8301 Linux reserved 
# 8e00 Linux LVM a500 FreeBSD disklabel a501 FreeBSD boot 
# a502 FreeBSD swap a503 FreeBSD UFS a504 FreeBSD ZFS 
# a505 FreeBSD Vinum/RAID a800 Apple UFS a901 NetBSD swap 
# a902 NetBSD FFS a903 NetBSD LFS a904 NetBSD concatenated 
# a905 NetBSD encrypted a906 NetBSD RAID ab00 Apple boot 
# af00 Apple HFS/HFS+ af01 Apple RAID af02 Apple RAID offline 
# af03 Apple label af04 AppleTV recovery af05 Apple Core Storage 
# be00 Solaris boot bf00 Solaris root bf01 Solaris /usr & Mac Z
# bf02 Solaris swap bf03 Solaris backup bf04 Solaris /var 
# bf05 Solaris /home bf06 Solaris alternate se bf07 Solaris Reserved 1 
# bf08 Solaris Reserved 2 bf09 Solaris Reserved 3 bf0a Solaris Reserved 4 
# bf0b Solaris Reserved 5 c001 HP-UX data c002 HP-UX service 
# ef00 EFI System ef01 MBR partition scheme ef02 BIOS boot partition 
# fd00 Linux RAID

sleep 5

echo "=======BEFORE======"
sgdisk -p ${DEV2}
echo "=======CLEARING======"
echo "clearing 100 meg with zeros, this will clear old partition info and fs info"
dd if=/dev/zero of=${DEV2} bs=1M count=100
sgdisk -z ${DEV2}
sgdisk -Z ${DEV2}
sgdisk -g ${DEV2}
echo "=======CALCULATING NEW SIZES======"
echo "START SECTOR IS: ${STARTSECTOR}"
ENDSECTOR=`sgdisk -E $DEV2`
echo "END SECTOR IS: $ENDSECTOR"
HALFSECTOR=$((ENDSECTOR/2))
echo "HALF POINT SECTOR IS: $HALFSECTOR (END LOCATION OF 1st PARTITION)"
HALFSECTOR1=$((HALFSECTOR+1))
echo "HALF POINT + 1: $HALFSECTOR1 (START LOCATION OF 2nd PARTITION)"
echo "PART1NAME: ${PART1NAME}"
echo "PART1TYPE: ${PART1TYPE}"
echo "PART2NAME: ${PART2NAME}"
echo "PART2TYPE: ${PART2TYPE}"
echo "=======CREATING======"
sgdisk -og ${DEV2}
sgdisk -n 1:${STARTSECTOR}:${HALFSECTOR} -c 1:${PART1NAME} -t 1:${PART1TYPE} ${DEV2}
sgdisk -n 2:${HALFSECTOR1}:${ENDSECTOR} -c 2:${PART2NAME} -t 2:${PART2TYPE} ${DEV2}
echo "=======RESULT======"
sgdisk -p ${DEV2}

might need to reread partition table after:

# hdparm -z $DEV2

if your using mdev, you will need to rescan your media with:

# mdev -s

if your on udev (an automatic mdev) then your good to go

# cat /proc/partitions

should show all of your partitions

you can now put filesystems or raid up or lvm your partitions

Leave a Reply

Your email address will not be published. Required fields are marked *