pmb/install: warn if the target disk is larger than expected (MR 1956)

This commit is contained in:
Martijn Braam 2020-07-07 13:16:48 +02:00 committed by Oliver Smith
parent 72e24f7f96
commit 5d540ad4fb
No known key found for this signature in database
GPG Key ID: 5AE7F5513E0885CB
2 changed files with 32 additions and 4 deletions

View File

@ -86,13 +86,15 @@ def ask(args, question="Continue?", choices=["y", "n"], default="n",
validation_regex + "). Please try again.")
def confirm(args, question="Continue?", default=False):
def confirm(args, question="Continue?", default=False, no_assumptions=False):
"""
Convenience wrapper around ask for simple yes-no questions with validation.
:param no_assumptions: ask for confirmation, even if "pmbootstrap -y' is set
:returns: True for "y", False for "n"
"""
default_str = "y" if default else "n"
if (args.assume_yes):
if args.assume_yes and not no_assumptions:
logging.info(question + " (y/n) [" + default_str + "]: y")
return True
answer = ask(args, question, ["y", "n"], default_str, True, "(y|n)")

View File

@ -405,7 +405,8 @@ def embed_firmware(args):
"bs=" + str(step), "seek=" + str(offset)])
def sanity_check_sdcard(device):
def sanity_check_sdcard(args):
device = args.sdcard
device_name = os.path.basename(device)
if not os.path.exists(device):
raise RuntimeError("{} doesn't exist, is the sdcard plugged?".format(device))
@ -416,6 +417,30 @@ def sanity_check_sdcard(device):
raise RuntimeError("{} is read-only, is the sdcard locked?".format(device))
def sanity_check_sdcard_size(args):
device = args.sdcard
devpath = os.path.realpath(device)
sysfs = '/sys/class/block/{}/size'.format(devpath.replace('/dev/', ''))
if not os.path.isfile(sysfs):
# This is a best-effort sanity check, continue if it's not checkable
return
with open(sysfs) as handle:
raw = handle.read()
# Size is in 512-byte blocks
size = int(raw.strip())
human = "{:.2f} GiB".format(size / 2 / 1024 / 1024)
# Warn if the size is larger than 100GiB
if size > (100 * 2 * 1024 * 1024):
if not pmb.helpers.cli.confirm(args, f"WARNING: The target disk ({devpath}) is"
" larger than a usual SD card (>100GiB)."
" Are you sure you want to overwrite"
f" this {human} disk?", no_assumptions=True):
raise RuntimeError("Aborted.")
def sanity_check_ondev_version(args):
arch = args.deviceinfo["arch"]
package = pmb.helpers.package.get(args, "postmarketos-ondev", arch)
@ -612,7 +637,8 @@ def install_on_device_installer(args, step, steps):
def install(args):
# Sanity checks
if not args.android_recovery_zip and args.sdcard:
sanity_check_sdcard(args.sdcard)
sanity_check_sdcard(args)
sanity_check_sdcard_size(args)
if args.on_device_installer:
sanity_check_ondev_version(args)