scripts: Add python script to read SPDK's GPT GUID from device

Signed-off-by: Michal Berger <michalx.berger@intel.com>
Change-Id: Ib24714363405f926ac8483244b24296fe35d5a86
Reviewed-on: https://review.spdk.io/gerrit/c/spdk/spdk/+/9591
Reviewed-by: Konrad Sztyber <konrad.sztyber@gmail.com>
Reviewed-by: Tomasz Zawadzki <tomasz.zawadzki@intel.com>
Reviewed-by: Jim Harris <james.r.harris@intel.com>
Community-CI: Broadcom CI <spdk-ci.pdl@broadcom.com>
Tested-by: SPDK CI Jenkins <sys_sgci@intel.com>
This commit is contained in:
Michal Berger 2021-09-23 11:49:08 +02:00 committed by Jim Harris
parent f6c2ad904e
commit 84a9119bb0

70
scripts/spdk-gpt.py Executable file
View File

@ -0,0 +1,70 @@
#!/usr/bin/env python3
import argparse
import os
import stat
import struct
def block_exists(block):
return os.path.exists(block) and stat.S_ISBLK(os.stat(block).st_mode)
def lbsize(block):
if not os.path.exists("/sys/block/%s/queue/logical_block_size"):
return 512
with open("/sys/block/%s/queue/logical_block_size" % block) as lbs:
return int(lbs.read())
def readb(block, offset, length, format="Q"):
b = os.open(block, os.O_RDONLY)
os.lseek(b, offset, os.SEEK_SET)
data = os.read(b, length)
os.close(b)
return struct.unpack(format, data)[0]
def is_spdk_gpt(block, entry):
block_path = "/dev/" + block
if not block_exists(block_path):
print("%s is not a block device" % block)
return False
disk_lbsize = lbsize(block)
gpt_sig = 0x5452415020494645 # EFI PART
spdk_guid = [0x7c5222bd, 0x8f5d, 0x4087, 0x9c00, 0xbf9843c7b58c]
if readb(block_path, disk_lbsize, 8) != gpt_sig:
print("No valid GPT data, bailing")
return False
part_entry_lba = disk_lbsize * readb(block_path, disk_lbsize + 72, 8)
part_entry_lba = part_entry_lba + (entry - 1) * 128
guid = [
readb(block_path, part_entry_lba, 4, "I"),
readb(block_path, part_entry_lba + 4, 2, "H"),
readb(block_path, part_entry_lba + 6, 2, "H"),
readb(block_path, part_entry_lba + 8, 2, ">H"),
readb(block_path, part_entry_lba + 10, 8, ">Q") >> 16
]
return guid == spdk_guid
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Checks if SPDK GUID is present on given block device')
parser.add_argument('block', type=str, help='block device to check')
parser.add_argument('-e', '--entry', dest='entry', help='GPT partition entry',
required=False, type=int, default=1)
args = parser.parse_args()
try:
if is_spdk_gpt(args.block.replace("/dev/", ""), args.entry):
exit(0)
exit(1)
except Exception as e:
print("Failed to read GPT data from %s (%s)" % (args.block, e))
exit(1)