Fix a partition table, recover a deleted partition
Recover the GUID Partition Table from its backup
Locate partitions which are inaccessible / ignored by the OS
Verify the integrity of the primary GPT against its backup
Usage
var GPT = require( 'gpt' )
NOTE: For brevity in the examples, error handling may be omitted, and synchronous methods used.
Examples
The following usage examples are consolidated into example/verify.js and runnable, where device would be a block device (i.e. /dev/rdisk2 on Mac OS, \\.\PhysicalDrive2 on Windows, or /dev/sdb on Linux);
The indicator for a GPT formatted device is a protective or hybrid Master Boot Record, containing a partition marked with a type of either 0xEE or 0xEF respectively. Thus we need to read & parse the MBR to determine whether a GPT is present or not:
function readPrimaryGPT( efiPart ) { // NOTE: You'll need to know / determine the logical block size of the storage device; // For the sake of brevity, we'll just go with the still most common 512 bytes var gpt = new GPT({ blockSize: 512 }) // NOTE: For protective GPTs (0xEF), the MBR's partitions // attempt to span as much of the device as they can to protect // against systems attempting to action on the device, // so the GPT is then located at LBA 1, not the EFI partition's first LBA var offset = efiPart.type == 0xEE ? efiPart.firstLBA * gpt.blockSize : gpt.blockSize // The default GPT is 33 blocks in length (1 block header, 32 block table) var buffer = Buffer.alloc( 33 * gpt.blockSize )
NOTE: Reading & parsing a GPT like above will just work in most cases, but to cover all situations (i.e. padding between header & table, custom table entry sizes, etc.), see the "Accounting for everything" example below:
Accounting for everything
function readPrimaryGPT( efiPart ) { // NOTE: You'll need to know / determine the logical block size of the storage device; // For the sake of brevity, we'll just go with the still most common 512 bytes var gpt = new GPT({ blockSize: 512 }) // NOTE: For protective GPTs (0xEF), the MBR's partitions // attempt to span as much of the device as they can to protect // against systems attempting to action on the device, // so the GPT is then located at LBA 1, not the EFI partition's first LBA var offset = efiPart.type == 0xEE ? efiPart.firstLBA * gpt.blockSize : gpt.blockSize // First, we need to read & parse the GPT header, which will declare various // sizes and offsets for us to calculate where & how long the table and backup are var headerBuffer = Buffer.alloc
Reading the Backup GPT
function readBackupGPT(primaryGPT) { var backupGPT = new GPT({ blockSize: primaryGPT.blockSize }) var buffer = Buffer.alloc( 33 * primaryGPT.blockSize ) var offset = ( ( primaryGPT.backupLBA - 32 ) * blockSize ) fs.readSync( fd, buffer, 0, buffer.length, offset ) backupGPT.parseBackup( buffer ) return backupGPT}var backupGPT = readBackupGPT(primaryGPT)
Accounting for everything
function readBackupGPT(primaryGPT) { var backupGPT = new GPT({ blockSize: primaryGPT.blockSize }) return backupGPT}var backupGPT = readBackupGPT(primaryGPT)
Verifying the GPT
// Check header & table checksums for primary and backup GPTvar isPrimaryValid = primaryGPT.verify() // truevar isBackupValid = backupGPT.verify() // true// Compare primary & backup GPT table checksums to// verify both are in the same state// NOTE: If there's a `gpt.headerChecksum` match, something's wrong,// as the primary and backup should refer to each other in offsetsvar checksumsMatch = primaryGPT.tableChecksum === backupGPT.tableChecksum && primaryGPT.headerChecksum !== backupGPT.headerChecksumif( isPrimaryValid && isBackupValid && checksumsMatch ) { console.log('Everything\'s alright')}
Creating a GPT
var gpt = new GPT({ // The block size of the device // the GPT will be written to // (optional, defaults to 512) blockSize: 512, // The GUID of the GPT (needs to be generated when creating) guid: '00000000-0000-0000-0000-000000000000' // Header size in bytes // (min. 92, defaults to 92) headerSize: 92, // LBA of current GPT copy currentLBA: 1n, // LBA of backup GPT backupLBA: 123456n, // LBA of first "user-space" block firstLBA: 34n, // LBA of last "user-space" block lastLBA: 556789n, // LBA of partition table // (defaults to 2) tableOffset:
gpt.partitions.push( new GPT.PartitionEntry({ type: 'C12A7328-F81F-11D2-BA4B-00A0C93EC93B', guid: 'BC7E4D81-59CC-40A6-84BF-43253C95AE0D', name: 'EFI System Partition', firstLBA: 40n, lastLBA: 409639n,}))
Writing the GPT
You can either let the module create the buffers for you;
var primaryGPTBuffer = gpt.write()var backupGPTBuffer = gpt.writeBackupFromPrimary()
Or pass in a buffer & optional offset (i.e. to write MBR & primary GPT in one go);
// Write out MBR & primary GPTfs.writeSync( fd, buffer, 0, buffer.length, 0 )// Write out backup GPT to end of device// NOTE: remeber to set `gpt.backupLBA` accordinglyfs.writeSync( fd, backupGPTBuffer, 0, backupGPTBuffer.length, gpt.backupLBA * blockSize )