image: add LUKS support and --[no-]encryption CLI flag to build & inspect subcommands

This commit is contained in:
InsanePrawn 2023-07-09 03:20:33 +02:00
parent 6de8137c90
commit a9cd8178c8
5 changed files with 236 additions and 49 deletions

View file

@ -5,6 +5,7 @@ import logging
from typing import Optional
from config.state import config
from constants import FLASH_PARTS, LOCATIONS, FASTBOOT, JUMPDRIVE
from exec.cmd import run_root_cmd
from exec.file import get_temp_dir
@ -15,6 +16,7 @@ from wrapper import enforce_wrap
from .fastboot import fastboot_flash
from .image import dd_image, dump_aboot, dump_lk2nd, dump_qhypstub, get_image_path, losetup_destroy, losetup_rootfs_image, partprobe, shrink_fs
from .cryptsetup import encryption_option
ABOOT = FLASH_PARTS['ABOOT']
LK2ND = FLASH_PARTS['LK2ND']
@ -47,7 +49,7 @@ def test_blockdev(path: str):
'microSD inserted or no microSD card slot installed in the device) or corrupt or defect')
def prepare_minimal_image(source_path: str, sector_size: int) -> str:
def prepare_minimal_image(source_path: str, sector_size: int, encrypted: Optional[bool], encryption_password: Optional[str]) -> str:
minimal_image_dir = get_temp_dir(register_cleanup=True)
minimal_image_path = os.path.join(minimal_image_dir, f'minimal-{os.path.basename(source_path)}')
logging.info(f"Copying image {os.path.basename(source_path)} to {minimal_image_dir} for shrinking")
@ -55,7 +57,7 @@ def prepare_minimal_image(source_path: str, sector_size: int) -> str:
loop_device = losetup_rootfs_image(minimal_image_path, sector_size)
partprobe(loop_device)
shrink_fs(loop_device, minimal_image_path, sector_size)
shrink_fs(loop_device, minimal_image_path, sector_size, encrypted, encryption_password)
losetup_destroy(loop_device)
return minimal_image_path
@ -67,6 +69,7 @@ def prepare_minimal_image(source_path: str, sector_size: int) -> str:
@click.option('--shrink/--no-shrink', is_flag=True, default=True, help="Copy and shrink the image file to minimal size")
@click.option('-b', '--sector-size', type=int, help="Override the device's sector size", default=None)
@click.option('--confirm', is_flag=True, help="Ask for confirmation before executing fastboot commands")
@encryption_option
@click.argument('what', type=click.Choice(list(FLASH_PARTS.values())))
@click.argument('location', type=str, required=False)
def cmd_flash(
@ -78,6 +81,7 @@ def cmd_flash(
shrink: bool = True,
sector_size: Optional[int] = None,
confirm: bool = False,
encryption: Optional[bool] = None,
):
"""
Flash a partition onto a device.
@ -115,7 +119,12 @@ def cmd_flash(
if not location:
raise Exception(f'You need to specify a location to flash {what} to')
path = ''
image_path = prepare_minimal_image(device_image_path, sector_size) if shrink else device_image_path
image_path = prepare_minimal_image(
device_image_path,
sector_size,
encrypted=encryption,
encryption_password=config.get_profile(profile).encryption_password,
) if shrink else device_image_path
if method == FASTBOOT:
fastboot_flash(
partition=location,