Categories
FreeBSD/Unix

Recovering GPT partitions with gpart

If one manages to destroy the GPT scheme partition tables you’ll still have few options to recover them. Since FreeBSD 9.x the default used by the installation CD is GPT. Just grab the FreeBSD installation CD and boot the system using that. When it boot’s up it gives you options to install, shell, and live CD. Select Live CD and log in as root.

Now if you have previously made a backup of the partition table using gpart backup command then you are lucky and most likely few shell commands away from getting the system back online. If not then you have more things to do.

The backup created by gpart backup command looks like this (backup_partition_table.ada0):

GPT 128
1   freebsd-boot         40        128  
2    freebsd-ufs        168 5851053944  
3   freebsd-swap 5851054112    8388607 

Restoring partitions and bootcode from backup:

gpart restore -F ada0 < backup_partition_table.ada0
gpart bootcode -b /boot/pmbr -p /boot/gptboot -i 1 ada0

In the restore command -F option first flushes completely the existing data from the partition table and then recovers the table from the file.

In case you haven’t saved the partition table then if you have used the installation defaults then the first partition should be the freebsd-boot and the size is most likely 64k then second partition is the root partition and the third is the swap. By default the boot partition is 64K size other partitions depend on your configuration.

gpart create -s gpt ada0
gpart add -s 64k -t freebsd-boot ada0
gpart add -s 2.7T -t freebsd-ufs ada0
gpart add -t freebsd-swap ada0
gpart bootcode -b /boot/pmbr -p /boot/gptboot -i 1 ada0