Created
February 23, 2022 17:13
-
-
Save jowagner/4fa63bfedf0b3d9389550995505871c9 to your computer and use it in GitHub Desktop.
Helper for punching holes into a disk image where partclone says that blocks are not in use
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import sys | |
offset = int(sys.argv[1]) | |
img_name = sys.argv[2] | |
# skip current_pos | |
while True: | |
line = sys.stdin.readline() | |
if not line: break | |
if line.startswith('#'): continue | |
# reached first non-comment line | |
# --> this is the current_pos line | |
break | |
# process ranges | |
while True: | |
line = sys.stdin.readline() | |
if not line: break | |
if line.startswith('#'): continue | |
fields = line.split() | |
if fields[2] == '+': continue | |
assert fields[2] == '?' | |
# found free area | |
start = int(fields[0], 16) | |
length = int(fields[1], 16) | |
sys.stdout.write('fallocate -p -l %d -o %d %s\n' %( | |
length, start + offset, img_name | |
)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Creates
fallocate --punch-hole
commands for each byte range reported as unused (status?
) by a domain mapfile (provided on stdin) such as produced bypartclone.[fstype] -D
. A non-zero offset can be specified to account for headers, e.g. when punching holes into a LUKS-encrypted container. If using with LVM or software RAID carefully check that the payload is indeed stored sequentially.This can be used to make file areas sparse that are not in use by the filesystem in the image file, freeing disk space. For plain images, a frequently suggested alternative is to first write zero bytes to the free space (by writing large, non-sparse files), then using
fallocate --dig-holes
orcp --sparse=always
to create a sparse image and finally deleting the zero files inside. For LUKS containers, this approach cannot work as one does not know what data one must write in order for a block of zero bytes to come out of the encryption.Example:
Known issue: Thomas-Tsai/partclone#180