SGDISK: (Use this for GPT drives of any size)

# --- sgdisk ---
# -p prints partition table (on the very left it will print before operations, on the very right it will print the results after the operations)
# operations are from done left to right
# -a is alignment of first partition, leave at 1 so that you can set the start sector easily
# -S is 128 meaning there can be a max of 128 partitions
# -n{PARTNUM}:startsection:lastsector
# use -p to find out sector size (in bytes)
sgdisk -p /dev/sda
# -t is to set Type: FD00 is linux RAID, 0700 is Linux/Windows Data
# -c is to set partition label (its optional)
# delete all partitions GPT and MBR (by zapping them with -Z)
sgdisk -Z /dev/sda
# NOTE: deleting all of the partitions will not delete your data in the partitions, if you were to recreate the partitions then you would have access to your data again
# NOTE: to get detailed partition information, combine the results of -p but also look at -i. For example to get all of the info about the 3rd partition of sdc do this:
sgdisk -p /dev/sdc
sgdisk -i3 /dev/sdc
# a,S,p,c,t are all optional, they have default values. t & c can be set at a future time for any partition. Also you can make 1 or 10 partitions right off the bat it doesnt matter (as long as its fits as a bash command)
# *** example: ****
# we are going to make 3 partitions (with a max of 128 partitions that we can make in the future). -a1 makes it easier to align the first partition at the start sector we want - or else it might go to another spot.
sgdisk -a1 -S128 -p -n1:64:8388671 -t1:FD00 -c1:"Linux RAID" -n2:8388672:9437247 -t2:FD00 -c2:"Linux RAID" -n3:9437248:1953525101 -t3:FD00 -c3:"Linux RAID" -p /dev/sda;
# delete one partition (partition 2)
sgdisk -d2 /dev/sda
# make it smaller (no need to set alignment or S as they are already set)
sgdisk -p /dev/sda  # <-- view partition table after deleting part#2
sgdisk -n2:8388672:5000000 -t2:FD00 -c2:"Linux RAID"  # <-- create new part#2
sgdisk -p /dev/sda  # <-- see new part table part#2
# or you can do all that in 1 command:
sgdisk -p -n2:8388672:5000000 -t2:FD00 -c2:"Linux RAID" -p /dev/sda
# *** example: ****
# creating a windows single GPT partition
sgdisk -a1 -S128 -p -n1:256:732566639 -t1:0700 -c1:"Linux/Windows data" -p /dev/sdc
# you can make the biggest partition possible to make using this method:
# -N{partnum} makes biggest partition
# imagine a 3TB drive with 1 partition in the beginning thats 300 Gig that was made like this
sgdisk -Z /dev/sda
sgdisk -a1 -S128 -p -n1:256:$((256*300*1024)) -t1:0700 -c1:"Linux/Windows data" -p /dev/sda 
# $((256*300*1024)) gives us the ending sector that would give us 300 MB, the sector size was 4096 bytes on sdc. so 256 sectors gives us 1 MB. There are 300*1024 MBs in 300 GB. So there are 256*300*1024 of these sectors in 300 GB.
# 256 start sector is a good start sector, it gives alot of room in the beginning for other system stuff/gpt stuff. Note that 64 is also a good start sector number (as seen in the examples up top)
# So now we have 1 300 gig partition in the front. Lets create the biggest possible partition in the biggest hole
sgdisk -p -N2 -p /dev/sdc
# note all of the -p were optional, I just used them to get a before and after picture/information of the partition table. -N2 means it made the biggest partition possible and it named it partition 2. I could of used 3 or 4 or any number and it would of made the same size partition. Its just IDing the new partition
# by default the paritition is made with Type: 0700 which Linux/Window Days, and it has no Label/Name (but thats optional, we can set that with -c later). We can also change the type with -t if we wanted to

 

SFDISK: Use this for MBR (drives 2TiB or smaller)

You can create a partition file with vi and then redirect that file to sfdisk to make your partitions.

echo "# partition table of /dev/hdc
unit: sectors
/dev/hdc1 : start=        2, size=  4096000, Id=83
/dev/hdc2 : start=  4096002, size=   512000, Id=82" > newpartitiontable

sfdisk /dev/sda < newpartitiontable

 

 CLONING / COPYING PARTITION TABLES:

To clone entire partition table (every partition) use this: http://www.infotinks.com/clone-partition-tables-gpt-mbr-sfdisk-sgdisk/

Currently there isnt a way im aware of to clone single partitions, however using all of the information from the source partition you can recreate it on the destination device. Get all of the info using “sgdisk -p /dev/sda” and “sgdisk -i4 /dev/sda“, assuming I wanted to recreate the 4th partition of sda on another drive, lets say sdz. Then after getting that information, I would backup the destination drives ,sdz’s , partition tables (in case I mess up I can revert). After backup I would proceed with recreating the 4th partition on sdz, the destination device, using the above sgdisk commands using the -n (to set partition number, start and end sector), -t (to set partition type equal to what it was on the source), and -c arguments (to set a label, this is optional, but if you want it to be like a genuine clone of the partition then I would include it). Also remember to use -a1  when first creating the partition table on the destination device so that your partition tables start and end at the same sector numbers as they did on the source device. Optionally also use -S128 to specify max partition numbers are 128

TYPE CODES

With -t<part number>:<typecode | hex guid> you specify the partition type of the partition number. So for example -t1:fd00 sets the first partition to Linux Raid (fd00). You can also use the full GUID format for fd00 which is alot more digits (that I dont want to look up right now). Its easier to just use the 2 byte (4 hex character) type codes. Here they all are (ignore the stuff in the comments, just look at the type codes):

#####################
# output from gdisk #
#####################

# Command (? for help): n
# Partition number (1-128, default 1): 
# First sector (34-15634398, default = 2048) or {+-}size{KMGTP}: 
# Last sector (2048-15634398, default = 15634398) or {+-}size{KMGTP}: +4G
# Current type is 'Apple HFS/HFS+'
# Hex code or GUID (L to show codes, Enter = AF00): L

0700 Microsoft basic data  0c01 Microsoft reserved    2700 Windows RE          
3000 ONIE boot             3001 ONIE config           3900 Plan 9              
4100 PowerPC PReP boot     4200 Windows LDM data      4201 Windows LDM metadata
4202 Windows Storage Spac  7501 IBM GPFS              7f00 ChromeOS kernel     
7f01 ChromeOS root         7f02 ChromeOS reserved     8200 Linux swap          
8300 Linux filesystem      8301 Linux reserved        8302 Linux /home         
8303 Linux x86 root (/)    8304 Linux x86-64 root (/  8305 Linux ARM64 root (/)
8306 Linux /srv            8307 Linux ARM32 root (/)  8400 Intel Rapid Start   
8e00 Linux LVM             a500 FreeBSD disklabel     a501 FreeBSD boot        
a502 FreeBSD swap          a503 FreeBSD UFS           a504 FreeBSD ZFS         
a505 FreeBSD Vinum/RAID    a580 Midnight BSD data     a581 Midnight BSD boot   
a582 Midnight BSD swap     a583 Midnight BSD UFS      a584 Midnight BSD ZFS    
a585 Midnight BSD Vinum    a600 OpenBSD disklabel     a800 Apple UFS           
a901 NetBSD swap           a902 NetBSD FFS            a903 NetBSD LFS          
a904 NetBSD concatenated   a905 NetBSD encrypted      a906 NetBSD RAID         
ab00 Recovery HD           af00 Apple HFS/HFS+        af01 Apple RAID          
af02 Apple RAID offline    af03 Apple label           af04 AppleTV recovery    
af05 Apple Core Storage    bc00 Acronis Secure Zone   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  
# Press the  key to see more codes: 
bf09 Solaris Reserved 3    bf0a Solaris Reserved 4    bf0b Solaris Reserved 5  
c001 HP-UX data            c002 HP-UX service         ea00 Freedesktop $BOOT   
eb00 Haiku BFS             ed00 Sony system partitio  ed01 Lenovo system partit
ef00 EFI System            ef01 MBR partition scheme  ef02 BIOS boot partition 
f800 Ceph OSD              f801 Ceph dm-crypt OSD     f802 Ceph journal        
f803 Ceph dm-crypt journa  f804 Ceph disk in creatio  f805 Ceph dm-crypt disk i
fb00 VMWare VMFS           fb01 VMWare reserved       fc00 VMWare kcore crash p
fd00 Linux RAID 

# Hex code or GUID (L to show codes, Enter = AF00): 0700
# Changed system type of partition to 'Microsoft basic data'

The common ones I use: 8300 Linux filesystem, fd00 linux raid, and 8200 linux swap.

The end

3 thoughts on “Writing Partition Tables with SGDISK (GPT) and SFDISK (MBR) – cheatsheet

  1. sgdisk sectors aren’t too hard if you suck at the bits and bytes understanding (like I do) using the `0` as a wildcard, you can easily set e.g. a gpt bios boot partition, a regular boot partition, a root and finally, everything else say home, by substitution of the second and third column values by 0 meaning either “next starting sector” or “fill up remaining sectors to end of space”

    -n 0:0:+2M -t “bios boot”
    -n 1:0:+200M -t “boot”
    -n 2:0:+3G -t “root”
    -n 3:0:0 -t “home”

Leave a Reply

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