Skip to content

Instantly share code, notes, and snippets.

@NP-chaonay
Last active December 26, 2020 14:16
Show Gist options
  • Select an option

  • Save NP-chaonay/c29f4cb5df7998786cec76175ac6efc9 to your computer and use it in GitHub Desktop.

Select an option

Save NP-chaonay/c29f4cb5df7998786cec76175ac6efc9 to your computer and use it in GitHub Desktop.
Bytes Overwriting on Entire Disk, using Python
# [USER_PARAM] Path to device file
path='/dev/sda'
# [USER_PARAM] Physical Sector Size of Disk (Used only in buffer_size and information displaying, can be ignore)
physical_sector_size=4096
# [USER_PARAM] Buffer Size
buffer_size=physical_sector_size*2
# [USER_PARAM] Writing Bytes Value (Set to 0 to overwrite disk to zero, else set to 255 for overwriting to ones)
writing_bytes_value=0
# User Confirmation
print('[Confirmation]')
print(' (IMPORTANT) Selected Device Path: '+path)
print(' Defined Physical Sector Size: '+str(physical_sector_size))
print(' Defined Buffer Size: '+str(buffer_size))
print(' Defined Writing Bytes Value: '+str(writing_bytes_value))
# Handle EOFError,KeyboardInterrupt
try:
print(' Type \'YES\' then press enter to starting writing operation; else, type something else...')
msg=input(' : ')
except (EOFError,KeyboardInterrupt):
print('\n < CANCELLED > ')
# Cancelled by user, should return 1
exit(1)
if msg=='YES':
print('\n[Operation]')
else:
print(' < CANCELLED > ')
# Cancelled by user, should return 1
exit(1)
# Code Block A: For percentage progress calculation & displaying
def code_block_a():
# Global Variable
global written_disk_bytes
# Get written disk bytes amount
written_disk_bytes=disk.seek(0,1)
# Calculation
percent=round(100*(written_disk_bytes/total_disk_bytes),1)
# Displaying
print('\b'*6+str(percent).rjust(5)+'%',end='',flush=True)
# In case error is occurred
try:
# Get device I/O object
disk=open(path,mode='r+b')
# Ensure that seeking position is on start of disk
disk.seek(0)
# Get total disk bytes
total_disk_bytes=disk.seek(0,2)
# Set round amount according to given parameters (excluding the last round of remaining-bytes, if have)
round_amount=total_disk_bytes//buffer_size
# Preparing writing bytes buffer
writing_bytes=bytes([writing_bytes_value]*buffer_size)
# Ensure that seeking position is on start of disk
disk.seek(0)
# Percentage Progress Displaying Initialization
print('Overwriting: '+'0.0'.rjust(5)+'%',end='',flush=True)
# [DANGER ZONE] Start overwriting
for i in range(round_amount):
# Code Block A
code_block_a()
# Writing Operation
_=disk.write(writing_bytes)
# Code Block A
code_block_a()
# Get remaining unwritten disk bytes amount
remained_disk_bytes=total_disk_bytes-written_disk_bytes
# Check if there the last round of remaining-bytes or not
if remained_disk_bytes:
# Preparing writing bytes buffer for the last round of remaining-bytes
writing_bytes=bytes([writing_bytes_value]*remained_disk_bytes)
# Writing Operation
_=disk.write(writing_bytes)
# Code Block A
code_block_a()
# Ending percentage printing
print()
# Ensure that every write operation is flushed
disk.flush()
except:
# Special charecter to erase existed percentage progress displaying
print('\r',end='')
# Tell user in case of I/O wait
print('(NOTE) You might have to waiting for I/O after exception is shown.')
# Then raise the exception
raise
@NP-chaonay
Copy link
Copy Markdown
Author

TBH, this could be applied to file

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment