Skip to content

Instantly share code, notes, and snippets.

@coderjo
Created September 26, 2019 09:46
Show Gist options
  • Save coderjo/cee7e4ab193d02dc75a8707d9607092f to your computer and use it in GitHub Desktop.
Save coderjo/cee7e4ab193d02dc75a8707d9607092f to your computer and use it in GitHub Desktop.
Quick Python script to generate a linux device mapper table from a ddrescue map file, placing errors where ddrescue had bad blocks.
#!/usr/bin/env python
import argparse
parser = argparse.ArgumentParser(description='Create a device mapper table from a ddrescue map file. '
'Blocks that are marked as bad will be mapped to the error target, while successfully-read '
'blocks will be passed through to the device. The dm table will be written to stdout so it can '
'be piped directly into dmsetup.')
parser.add_argument('mapfile', help='The ddrescue map file to convert')
parser.add_argument('device', help='The device containing the image that corresponds to the map file. (typically a loop device)')
args = parser.parse_args()
mapfile = args.mapfile
imagefile = args.device
with open(mapfile) as file:
statusline = None
for line in file:
if line.startswith("#"):
continue
if not statusline:
statusline = line
continue
block = line.split()
start = int(block[0], 0) / 512
len = int(block[1], 0) / 512
module = None
if block[2] == '-':
module = "error"
elif block[2] == '+':
module = "linear %s %d" % (imagefile, start)
else:
raise ('Unimplemented block status type: "%s"' % (block[2]))
print "%d %d %s" % (start, len, module)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment