Add separate boot partition
This commit is contained in:
parent
7de8803032
commit
955546c918
4 changed files with 237 additions and 105 deletions
121
flash.py
121
flash.py
|
@ -6,12 +6,10 @@ import click
|
|||
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 fastboot import fastboot_flash
|
||||
from image import shrink_fs, losetup_rootfs_image, dump_bootimg, dump_lk2nd, dump_qhypstub, get_device_and_flavour, get_image_name, get_image_path
|
||||
from wrapper import enforce_wrap
|
||||
from image import resize_fs
|
||||
from utils import mount
|
||||
|
||||
BOOTIMG = FLASH_PARTS['BOOTIMG']
|
||||
LK2ND = FLASH_PARTS['LK2ND']
|
||||
|
@ -29,30 +27,37 @@ def cmd_flash(what, location):
|
|||
device_image_name = get_image_name(chroot)
|
||||
device_image_path = get_image_path(chroot)
|
||||
|
||||
# TODO: PARSE DEVICE SECTOR SIZE
|
||||
sector_size = 4096
|
||||
|
||||
if what not in FLASH_PARTS.values():
|
||||
raise Exception(f'Unknown what "{what}", must be one of {", ".join(FLASH_PARTS.values())}')
|
||||
|
||||
if what == ROOTFS:
|
||||
if location is None:
|
||||
raise Exception(f'You need to specify a location to flash {what} to')
|
||||
if location not in LOCATIONS:
|
||||
raise Exception(f'Invalid location {location}. Choose one of {", ".join(LOCATIONS)}')
|
||||
|
||||
path = ''
|
||||
dir = '/dev/disk/by-id'
|
||||
for file in os.listdir(dir):
|
||||
sanitized_file = file.replace('-', '').replace('_', '').lower()
|
||||
if f'jumpdrive{location.split("-")[0]}' in sanitized_file:
|
||||
path = os.path.realpath(os.path.join(dir, file))
|
||||
result = subprocess.run(['lsblk', path, '-o', 'SIZE'], capture_output=True)
|
||||
if result.returncode != 0:
|
||||
raise Exception(f'Failed to lsblk {path}')
|
||||
if result.stdout == b'SIZE\n 0B\n':
|
||||
raise Exception(
|
||||
f'Disk {path} has a size of 0B. That probably means it is not available (e.g. no microSD inserted or no microSD card slot installed in the device) or corrupt or defect'
|
||||
)
|
||||
if path == '':
|
||||
raise Exception('Unable to discover Jumpdrive')
|
||||
if location.startswith("/dev/"):
|
||||
path = location
|
||||
else:
|
||||
if location not in LOCATIONS:
|
||||
raise Exception(f'Invalid location {location}. Choose one of {", ".join(LOCATIONS)}')
|
||||
|
||||
dir = '/dev/disk/by-id'
|
||||
for file in os.listdir(dir):
|
||||
sanitized_file = file.replace('-', '').replace('_', '').lower()
|
||||
if f'jumpdrive{location.split("-")[0]}' in sanitized_file:
|
||||
path = os.path.realpath(os.path.join(dir, file))
|
||||
result = subprocess.run(['lsblk', path, '-o', 'SIZE'], capture_output=True)
|
||||
if result.returncode != 0:
|
||||
raise Exception(f'Failed to lsblk {path}')
|
||||
if result.stdout == b'SIZE\n 0B\n':
|
||||
raise Exception(
|
||||
f'Disk {path} has a size of 0B. That probably means it is not available (e.g. no microSD inserted or no microSD card slot installed in the device) or corrupt or defect'
|
||||
)
|
||||
if path == '':
|
||||
raise Exception('Unable to discover Jumpdrive')
|
||||
|
||||
minimal_image_dir = tempfile.gettempdir()
|
||||
minimal_image_path = os.path.join(minimal_image_dir, f'minimal-{device_image_name}')
|
||||
|
@ -64,55 +69,31 @@ def cmd_flash(what, location):
|
|||
|
||||
shutil.copyfile(device_image_path, minimal_image_path)
|
||||
|
||||
resize_fs(minimal_image_path, shrink=True)
|
||||
loop_device = losetup_rootfs_image(minimal_image_path, sector_size)
|
||||
shrink_fs(loop_device, minimal_image_path, sector_size)
|
||||
|
||||
if location.endswith('-file'):
|
||||
part_mount = '/mnt/kupfer/fs'
|
||||
if not os.path.exists(part_mount):
|
||||
os.makedirs(part_mount)
|
||||
|
||||
result = mount(path, part_mount, options=[])
|
||||
if result.returncode != 0:
|
||||
raise Exception(f'Failed to mount {path} to {part_mount}')
|
||||
|
||||
dir = os.path.join(part_mount, '.stowaways')
|
||||
if not os.path.exists(dir):
|
||||
os.makedirs(dir)
|
||||
|
||||
result = subprocess.run([
|
||||
'rsync',
|
||||
'--archive',
|
||||
'--inplace',
|
||||
'--partial',
|
||||
'--progress',
|
||||
'--human-readable',
|
||||
minimal_image_path,
|
||||
os.path.join(dir, 'kupfer.img'),
|
||||
])
|
||||
if result.returncode != 0:
|
||||
raise Exception(f'Failed to mount {path} to {part_mount}')
|
||||
else:
|
||||
result = subprocess.run([
|
||||
'dd',
|
||||
f'if={minimal_image_path}',
|
||||
f'of={path}',
|
||||
'bs=20M',
|
||||
'iflag=direct',
|
||||
'oflag=direct',
|
||||
'status=progress',
|
||||
'conv=sync,noerror',
|
||||
])
|
||||
if result.returncode != 0:
|
||||
raise Exception(f'Failed to flash {minimal_image_path} to {path}')
|
||||
|
||||
elif what == BOOTIMG:
|
||||
path = dump_bootimg(device_image_path)
|
||||
fastboot_flash('boot', path)
|
||||
elif what == LK2ND:
|
||||
path = dump_lk2nd(device_image_path)
|
||||
fastboot_flash('lk2nd', path)
|
||||
elif what == QHYPSTUB:
|
||||
path = dump_qhypstub(device_image_path)
|
||||
fastboot_flash('qhypstub', path)
|
||||
result = subprocess.run([
|
||||
'dd',
|
||||
f'if={minimal_image_path}',
|
||||
f'of={path}',
|
||||
'bs=20M',
|
||||
'iflag=direct',
|
||||
'oflag=direct',
|
||||
'status=progress',
|
||||
'conv=sync,noerror',
|
||||
])
|
||||
if result.returncode != 0:
|
||||
raise Exception(f'Failed to flash {minimal_image_path} to {path}')
|
||||
else:
|
||||
raise Exception(f'Unknown what "{what}", this must be a bug in kupferbootstrap!')
|
||||
loop_device = losetup_rootfs_image(device_image_path, sector_size)
|
||||
if what == BOOTIMG:
|
||||
path = dump_bootimg(f'{loop_device}p1')
|
||||
fastboot_flash('boot', path)
|
||||
elif what == LK2ND:
|
||||
path = dump_lk2nd(f'{loop_device}p1')
|
||||
fastboot_flash('lk2nd', path)
|
||||
elif what == QHYPSTUB:
|
||||
path = dump_qhypstub(f'{loop_device}p1')
|
||||
fastboot_flash('qhypstub', path)
|
||||
else:
|
||||
raise Exception(f'Unknown what "{what}", this must be a bug in kupferbootstrap!')
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue