예제 #1
0
def wipe_file(path, reader=None, buflen=4 * 1024 * 1024, exclusive=True):
    """
    wipe the existing file at path.
    if reader is provided, it will be called as a 'reader(buflen)'
    to provide data for each write.  Otherwise, zeros are used.
    writes will be done in size of buflen.
    """
    if reader:
        readfunc = reader
    else:
        buf = buflen * b'\0'

        def readfunc(size):
            return buf

    size = util.file_size(path)
    LOG.debug("%s is %s bytes. wiping with buflen=%s", path, size, buflen)

    with exclusive_open(path, exclusive=exclusive) as fp:
        while True:
            pbuf = readfunc(buflen)
            pos = fp.tell()
            if len(pbuf) != buflen and len(pbuf) + pos < size:
                raise ValueError(
                    "short read on reader got %d expected %d after %d" %
                    (len(pbuf), buflen, pos))

            if pos + buflen >= size:
                fp.write(pbuf[0:size - pos])
                break
            else:
                fp.write(pbuf)
예제 #2
0
def check_dos_signature(device):
    """
    check if there is a dos partition table signature present on device
    """
    # the last 2 bytes of a dos partition table have the signature with the
    # value 0xAA55. the dos partition table is always 0x200 bytes long, even if
    # the underlying disk uses a larger logical block size, so the start of
    # this signature must be at 0x1fe
    # https://en.wikipedia.org/wiki/Master_boot_record#Sector_layout
    devname = dev_path(path_to_kname(device))
    return (is_block_device(devname) and util.file_size(devname) >= 0x200 and
            (util.load_file(devname, decode=False, read_len=2, offset=0x1fe)
             == b'\x55\xAA'))
예제 #3
0
def check_efi_signature(device):
    """
    check if there is a gpt partition table signature present on device
    """
    # the gpt partition table header is always on lba 1, regardless of the
    # logical block size used by the underlying disk. therefore, a static
    # offset cannot be used, the offset to the start of the table header is
    # always the sector size of the disk
    # the start of the gpt partition table header shoult have the signaure
    # 'EFI PART'.
    # https://en.wikipedia.org/wiki/GUID_Partition_Table
    devname = dev_path(path_to_kname(device))
    sector_size = get_blockdev_sector_size(devname)[0]
    return (is_block_device(devname)
            and util.file_size(devname) >= 2 * sector_size and (util.load_file(
                devname, decode=False, read_len=8,
                offset=sector_size) == b'EFI PART'))