1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
|
#! /usr/bin/python3
import shutil, os, io, sys, time
from subprocess import call, run
class MyLogger:
"""
Custom Logger, which can add a date information
Parameters for the constructor:
:param: t0 a date taken as origin of time. defaults to None
:type : float
"""
def __init__(self, t0 = None):
self.t0 = t0
return
def log(self, msg, end="\n"):
"""
Logging a message
:param: msg the message
:type : str
:param: end some string to append (default to newline)
:type : str
"""
# eventually prepend a time indication
if self.t0 is not None:
t = f"{int(time.time() - self.t0):5d} s: "
if msg[0] == "\r":
msg = "\r" + t + msg
else:
msg = t + msg
print(msg, end=end)
sys.stdout.flush()
def initImg(isoFile, imgFile, size="14G"):
"""
Création d'une image de clé USB complète de taille <size>,
à partir d'un fichier ISO de nom <isoFile> ou <isoFile>.iso,
vers un fichier nouveau <imgFile> ou <imgFile>-<size>.img
:param: isoFile nom de fichier ISO, terminé par .iso supposément
:type : str
:param: imgFile un nom pour l'image, terminé par .img supposément
:type : str
:param: size la taille, en Giga si le suffixe est G, ou Méga si
le suffice est M, sinon directement en octets
:type : str
"""
if not isoFile.endswith(".iso"):
isoFile += ".iso"
if not imgFile.endswith(".img"):
imgFile = f"{imgFile}-{size}.img"
isize = None
if size[-1] == "G":
isize = 1E9 * int(size[:-1])
elif size[-1] == "M":
isize = 1E6 * int(size[:-1])
else:
isize = int(size)
ISOsize = os.path.getsize(isoFile)
log = MyLogger(time.time())
cmd = f"dd if={isoFile} of={imgFile} bs=4M oflag=dsync status=progress"
log.log(f"Copying the ISO file : {cmd}")
call(cmd, shell=True)
toseek = ISOsize // (4*1024*1024) + 1
count = int((isize - ISOsize) // (4*1024*1024))
cmd = f"dd if=/dev/zero of={imgFile} seek={toseek} bs=4M count={count} oflag=dsync status=progress"
log.log(f"padding with zeros : {cmd}")
call(cmd, shell=True)
log.log("Finished")
return
def usage():
print(f"""\
USAGE:
{sys.argv[0]} ISOfile IMGfile
write ISO file to IMGfile, then pads with zeros to
grow the file length of IMGfile up to 14G.""")
return
if __name__ == "__main__":
try:
initImg(sys.argv[1], sys.argv[2])
except Exception as e:
print(e)
usage()
|