helper-scripts/ddusb.py

39 lines
1.1 KiB
Python
Raw Normal View History

#!/usr/bin/python3
2018-10-23 15:33:04 -07:00
"""Write an ISO image to a usb drive using dd."""
2018-10-13 13:07:39 -07:00
import argparse
import pathlib
import subprocess
2019-01-20 22:02:39 -08:00
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument("-b", "--bs",
default=512,
help="block size",
metavar="bs")
parser.add_argument("input_file", help="input file to write")
parser.add_argument("output_file", help="output block device")
args = parser.parse_args()
2018-10-23 15:33:04 -07:00
2019-01-20 22:02:39 -08:00
block_size = args.bs
input_file = args.input_file
block_device = args.output_file
2018-10-13 13:07:39 -07:00
2019-01-20 22:02:39 -08:00
if not pathlib.Path(block_device).is_block_device():
print(f"Error: {block_device} is not a block device")
exit(1)
2018-10-13 13:07:39 -07:00
2019-01-20 22:02:39 -08:00
print(f"Input file: {input_file}")
print(f"Block device: {block_device}")
print(f"Block size: {block_size}")
2018-10-13 13:07:39 -07:00
2019-01-20 22:02:39 -08:00
try:
subprocess.run(["dd", f"if={input_file}",
f"of={block_device}",
f"bs={block_size}",
"status=progress"], check=True)
except subprocess.CalledProcessError:
exit(1)
else:
subprocess.run(['sync'])