diff options
Diffstat (limited to 'rpi2-debian-stdkernel/copy-to-sdcard.py')
-rwxr-xr-x | rpi2-debian-stdkernel/copy-to-sdcard.py | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/rpi2-debian-stdkernel/copy-to-sdcard.py b/rpi2-debian-stdkernel/copy-to-sdcard.py new file mode 100755 index 0000000..2717838 --- /dev/null +++ b/rpi2-debian-stdkernel/copy-to-sdcard.py @@ -0,0 +1,71 @@ +#!/usr/bin/python3 + +import glob +import os +import shutil +import subprocess +import sys +import tempfile +import time + +def reexec_root(): + # run as root + if os.geteuid() != 0: + os.execvp("sudo", ["sudo"] + sys.argv) + + +def run(*l, **d): + print('run:', *l, d) + return subprocess.run(*l, **d) + + +def out_of_date(path, timeout): + 'true if path mtime is greater than timeout in seconds' + return (time.time() - os.path.getmtime(path)) > timeout + + +def get_fresh_firmware(firmware_dir): + firmware_timeout = 24*3600 # 1d timeout + if (not os.path.exists(firmware_dir) + or out_of_date(firmware_dir, firmware_timeout)): + shutil.rmtree(firmware_dir, ignore_errors=True) + run(['git', 'clone', '--depth=1', + 'https://github.com/raspberrypi/firmware', + firmware_dir], check=True) + + +def copy_to_partitions(firmware_dir, boot_mountpoint, root_mountpoint): + for path in glob.glob(firmware_dir + '/boot/*'): + if os.path.isfile(path): + shutil.copy(path, boot_mountpoint) + for path in glob.glob('other_boot_files/*'): + shutil.copy(path, boot_mountpoint) + run(['rsync', '-aHX', 'debootstrapdir/.', root_mountpoint + '/.'], + check=True) + #shutil.copyfile('debootstrapdir/vmlinuz', boot_mountpoint + '/zImage', + # follow_symlinks=True) + + +def main(): + if len(sys.argv) < 2: + print('Usage: {} <device>'.format(sys.argv[0]), file=sys.stderr) + raise SystemExit(1) + device = sys.argv[1] + reexec_root() + firmware_dir = 'rpi-firmware' + boot_mountpoint = tempfile.TemporaryDirectory() + root_mountpoint = tempfile.TemporaryDirectory() + try: + run(['mount', device + '1', boot_mountpoint.name], check=True) + run(['mount', device + '2', root_mountpoint.name], check=True) + get_fresh_firmware(firmware_dir) + copy_to_partitions(firmware_dir, + boot_mountpoint.name, + root_mountpoint.name) + finally: + run(['umount', boot_mountpoint.name]) + run(['umount', root_mountpoint.name]) + + +if __name__ == '__main__': + main() |