flash.py: adjust for Chroot class and paths['images']
This commit is contained in:
parent
fa427f2a17
commit
7de8803032
1 changed files with 23 additions and 34 deletions
57
flash.py
57
flash.py
|
@ -1,14 +1,17 @@
|
||||||
import atexit
|
import atexit
|
||||||
from constants import FLASH_PARTS, LOCATIONS
|
|
||||||
from fastboot import fastboot_flash
|
|
||||||
import shutil
|
import shutil
|
||||||
from image import dump_bootimg, dump_lk2nd, dump_qhypstub, get_device_and_flavour, get_image_name
|
|
||||||
import os
|
import os
|
||||||
import subprocess
|
import subprocess
|
||||||
import click
|
import click
|
||||||
import tempfile
|
import tempfile
|
||||||
|
|
||||||
|
from constants import FLASH_PARTS, LOCATIONS
|
||||||
|
from fastboot import fastboot_flash
|
||||||
|
from chroot import get_device_chroot
|
||||||
|
from image import dump_bootimg, dump_lk2nd, dump_qhypstub, get_device_and_flavour, get_image_name, get_image_path
|
||||||
from wrapper import enforce_wrap
|
from wrapper import enforce_wrap
|
||||||
from image import resize_fs
|
from image import resize_fs
|
||||||
|
from utils import mount
|
||||||
|
|
||||||
BOOTIMG = FLASH_PARTS['BOOTIMG']
|
BOOTIMG = FLASH_PARTS['BOOTIMG']
|
||||||
LK2ND = FLASH_PARTS['LK2ND']
|
LK2ND = FLASH_PARTS['LK2ND']
|
||||||
|
@ -17,12 +20,14 @@ ROOTFS = FLASH_PARTS['ROOTFS']
|
||||||
|
|
||||||
|
|
||||||
@click.command(name='flash')
|
@click.command(name='flash')
|
||||||
@click.argument('what')
|
@click.argument('what', type=click.Choice(list(FLASH_PARTS.values())))
|
||||||
@click.argument('location', required=False)
|
@click.argument('location', required=False, type=click.Choice(LOCATIONS))
|
||||||
def cmd_flash(what, location):
|
def cmd_flash(what, location):
|
||||||
enforce_wrap()
|
enforce_wrap()
|
||||||
device, flavour = get_device_and_flavour()
|
device, flavour = get_device_and_flavour()
|
||||||
image_name = get_image_name(device, flavour)
|
chroot = get_device_chroot(device, flavour, 'aarch64')
|
||||||
|
device_image_name = get_image_name(chroot)
|
||||||
|
device_image_path = get_image_path(chroot)
|
||||||
|
|
||||||
if what not in FLASH_PARTS.values():
|
if what not in FLASH_PARTS.values():
|
||||||
raise Exception(f'Unknown what "{what}", must be one of {", ".join(FLASH_PARTS.values())}')
|
raise Exception(f'Unknown what "{what}", must be one of {", ".join(FLASH_PARTS.values())}')
|
||||||
|
@ -49,40 +54,24 @@ def cmd_flash(what, location):
|
||||||
if path == '':
|
if path == '':
|
||||||
raise Exception('Unable to discover Jumpdrive')
|
raise Exception('Unable to discover Jumpdrive')
|
||||||
|
|
||||||
image_dir = tempfile.gettempdir()
|
minimal_image_dir = tempfile.gettempdir()
|
||||||
image_path = os.path.join(image_dir, f'minimal-{image_name}')
|
minimal_image_path = os.path.join(minimal_image_dir, f'minimal-{device_image_name}')
|
||||||
|
|
||||||
def clean_dir():
|
def clean_dir():
|
||||||
shutil.rmtree(image_dir)
|
shutil.rmtree(minimal_image_dir)
|
||||||
|
|
||||||
atexit.register(clean_dir)
|
atexit.register(clean_dir)
|
||||||
|
|
||||||
shutil.copyfile(image_name, image_path)
|
shutil.copyfile(device_image_path, minimal_image_path)
|
||||||
|
|
||||||
resize_fs(image_path, shrink=True)
|
resize_fs(minimal_image_path, shrink=True)
|
||||||
|
|
||||||
if location.endswith('-file'):
|
if location.endswith('-file'):
|
||||||
part_mount = '/mnt/kupfer/fs'
|
part_mount = '/mnt/kupfer/fs'
|
||||||
if not os.path.exists(part_mount):
|
if not os.path.exists(part_mount):
|
||||||
os.makedirs(part_mount)
|
os.makedirs(part_mount)
|
||||||
|
|
||||||
def umount():
|
result = mount(path, part_mount, options=[])
|
||||||
subprocess.run(
|
|
||||||
[
|
|
||||||
'umount',
|
|
||||||
'-lc',
|
|
||||||
part_mount,
|
|
||||||
],
|
|
||||||
stderr=subprocess.DEVNULL,
|
|
||||||
)
|
|
||||||
|
|
||||||
atexit.register(umount)
|
|
||||||
|
|
||||||
result = subprocess.run([
|
|
||||||
'mount',
|
|
||||||
path,
|
|
||||||
part_mount,
|
|
||||||
])
|
|
||||||
if result.returncode != 0:
|
if result.returncode != 0:
|
||||||
raise Exception(f'Failed to mount {path} to {part_mount}')
|
raise Exception(f'Failed to mount {path} to {part_mount}')
|
||||||
|
|
||||||
|
@ -97,7 +86,7 @@ def cmd_flash(what, location):
|
||||||
'--partial',
|
'--partial',
|
||||||
'--progress',
|
'--progress',
|
||||||
'--human-readable',
|
'--human-readable',
|
||||||
image_path,
|
minimal_image_path,
|
||||||
os.path.join(dir, 'kupfer.img'),
|
os.path.join(dir, 'kupfer.img'),
|
||||||
])
|
])
|
||||||
if result.returncode != 0:
|
if result.returncode != 0:
|
||||||
|
@ -105,7 +94,7 @@ def cmd_flash(what, location):
|
||||||
else:
|
else:
|
||||||
result = subprocess.run([
|
result = subprocess.run([
|
||||||
'dd',
|
'dd',
|
||||||
f'if={image_path}',
|
f'if={minimal_image_path}',
|
||||||
f'of={path}',
|
f'of={path}',
|
||||||
'bs=20M',
|
'bs=20M',
|
||||||
'iflag=direct',
|
'iflag=direct',
|
||||||
|
@ -114,16 +103,16 @@ def cmd_flash(what, location):
|
||||||
'conv=sync,noerror',
|
'conv=sync,noerror',
|
||||||
])
|
])
|
||||||
if result.returncode != 0:
|
if result.returncode != 0:
|
||||||
raise Exception(f'Failed to flash {image_path} to {path}')
|
raise Exception(f'Failed to flash {minimal_image_path} to {path}')
|
||||||
|
|
||||||
elif what == BOOTIMG:
|
elif what == BOOTIMG:
|
||||||
path = dump_bootimg(image_name)
|
path = dump_bootimg(device_image_path)
|
||||||
fastboot_flash('boot', path)
|
fastboot_flash('boot', path)
|
||||||
elif what == LK2ND:
|
elif what == LK2ND:
|
||||||
path = dump_lk2nd(image_name)
|
path = dump_lk2nd(device_image_path)
|
||||||
fastboot_flash('lk2nd', path)
|
fastboot_flash('lk2nd', path)
|
||||||
elif what == QHYPSTUB:
|
elif what == QHYPSTUB:
|
||||||
path = dump_qhypstub(image_name)
|
path = dump_qhypstub(device_image_path)
|
||||||
fastboot_flash('qhypstub', path)
|
fastboot_flash('qhypstub', path)
|
||||||
else:
|
else:
|
||||||
raise Exception(f'Unknown what "{what}", this must be a bug in kupferbootstrap!')
|
raise Exception(f'Unknown what "{what}", this must be a bug in kupferbootstrap!')
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue