class VirtDisk::PartitionType::GptPartition

Constants

GPT_HEADER
GPT_PARTITION_ENTRY

Public Class Methods

discover_gpt_partitions(disk) click to toggle source
# File lib/virt_disk/partition_type/gpt_partition.rb, line 52
def self.discover_gpt_partitions(disk) # rubocop:disable AbcSize
  _log.info "Parsing GPT disk ..."
  gpt_header = disk.mod_read(MBR_SIZE, GPT_HEADER.size)
  header = GPT_HEADER.decode(gpt_header)

  partitions = []
  pte = GPT_HEADER.size + MBR_SIZE
  (1..header[:part_num]).each do |n|
    gpt = disk.mod_read(pte, GPT_PARTITION_ENTRY.size)
    pt_entry = GPT_PARTITION_ENTRY.decode(gpt)
    ptype = UUIDTools::UUID.parse_raw(pt_entry[:ptype]).to_s

    partitions.push(new(disk, ptype, n, pt_entry[:first_lba], pt_entry[:last_lba])) if pt_entry[:first_lba] != 0
    pte += header[:part_size]
  end
  partitions
end
discover_partitions(disk) click to toggle source
# File lib/virt_disk/partition_type/gpt_partition.rb, line 36
def self.discover_partitions(disk) # rubocop:disable AbcSize
  mbr = disk.mod_read(0, MBR_SIZE)
  if mbr.length < MBR_SIZE
    _log.info "<#{disk.object_id}> disk does not contain a master boot record"
    return []
  end

  sig = mbr[510..511].unpack('H4')

  pt_entry = DOS_PARTITION_ENTRY.decode(mbr[DOS_PT_START, PTE_LEN])
  ptype = pt_entry[:ptype]

  return [] if sig[0] != DOS_SIG || ptype != GPT_SIG
  discover_gpt_partitions(disk)
end