From 8fb69f9c463ab1cd1977abe647054d26115a0787 Mon Sep 17 00:00:00 2001 From: Oliver Smith Date: Tue, 9 Jun 2020 15:43:25 +0200 Subject: [PATCH] install_system_image: add sdcard argument (MR 1946) The on-device installer will run install_system_image once with sdcard=None and the second time with sdcard=args.sdcard. --- pmb/install/_install.py | 14 +++++++------ pmb/install/blockdevice.py | 40 +++++++++++++++++++++----------------- pmb/install/format.py | 10 ++++++---- pmb/install/partition.py | 7 ++++--- 4 files changed, 40 insertions(+), 31 deletions(-) diff --git a/pmb/install/_install.py b/pmb/install/_install.py index f4626b53..d43a8ae5 100644 --- a/pmb/install/_install.py +++ b/pmb/install/_install.py @@ -374,7 +374,7 @@ def sanity_check_sdcard(device): def install_system_image(args, size_reserve, suffix, root_label="pmOS_root", - step=3, steps=5, split=False): + step=3, steps=5, split=False, sdcard=None): """ :param size_reserve: empty partition between root and boot in MiB (pma#463) :param suffix: the chroot suffix, where the rootfs that will be installed @@ -383,6 +383,7 @@ def install_system_image(args, size_reserve, suffix, root_label="pmOS_root", :param step: next installation step :param steps: total installation steps :param split: create separate images for boot and root partitions + :param sdcard: path to sdcard device (e.g. /dev/mmcblk0) or None """ # Partition and fill image/sdcard logging.info(f"*** ({step}/{steps}) PREPARE INSTALL BLOCKDEVICE ***") @@ -390,14 +391,14 @@ def install_system_image(args, size_reserve, suffix, root_label="pmOS_root", (size_boot, size_root) = get_subpartitions_size(args, suffix) if not args.rsync: pmb.install.blockdevice.create(args, size_boot, size_root, - size_reserve, split) + size_reserve, split, sdcard) if not split: pmb.install.partition(args, size_boot, size_reserve) if not split: root_id = 3 if size_reserve else 2 - pmb.install.partitions_mount(args, root_id) + pmb.install.partitions_mount(args, root_id, sdcard) - pmb.install.format(args, size_reserve, root_label) + pmb.install.format(args, size_reserve, root_label, sdcard) # Just copy all the files logging.info(f"*** ({step + 1}/{steps}) FILL INSTALL BLOCKDEVICE ***") @@ -413,7 +414,7 @@ def install_system_image(args, size_reserve, suffix, root_label="pmOS_root", if sparse is None: sparse = args.deviceinfo["flash_sparse"] == "true" - if sparse and not split and not args.sdcard: + if sparse and not split and not sdcard: logging.info("(native) make sparse rootfs") pmb.chroot.apk.install(args, ["android-tools"]) sys_image = args.device + ".img" @@ -571,5 +572,6 @@ def install(args): elif args.android_recovery_zip: return install_recovery_zip(args) - install_system_image(args, 0, f"rootfs_{args.device}", split=args.split) + install_system_image(args, 0, f"rootfs_{args.device}", split=args.split, + sdcard=args.sdcard) print_flash_info(args) diff --git a/pmb/install/blockdevice.py b/pmb/install/blockdevice.py index 8b95722b..6f86a3ea 100644 --- a/pmb/install/blockdevice.py +++ b/pmb/install/blockdevice.py @@ -9,14 +9,15 @@ import pmb.helpers.cli import pmb.config -def previous_install(args): +def previous_install(args, path): """ Search the sdcard for possible existence of a previous installation of pmOS. We temporarily mount the possible pmOS_boot partition as /dev/sdcardp1 inside the native chroot to check the label from there. + :param path: path to sdcard device (e.g. /dev/mmcblk0) """ label = "" - for blockdevice_outside in [args.sdcard + "1", args.sdcard + "p1"]: + for blockdevice_outside in [f"{path}1", f"{path}p1"]: if not os.path.exists(blockdevice_outside): continue blockdevice_inside = "/dev/sdcardp1" @@ -28,29 +29,31 @@ def previous_install(args): return "pmOS_boot" in label -def mount_sdcard(args): +def mount_sdcard(args, path): + """ + :param path: path to sdcard device (e.g. /dev/mmcblk0) + """ # Sanity checks if args.deviceinfo["external_storage"] != "true": raise RuntimeError("According to the deviceinfo, this device does" " not support a sdcard installation.") - if not os.path.exists(args.sdcard): - raise RuntimeError("The sdcard device does not exist: " + - args.sdcard) - for path in glob.glob(args.sdcard + "*"): - if pmb.helpers.mount.ismount(path): - raise RuntimeError(path + " is mounted! We will not attempt" - " to format this!") - logging.info("(native) mount /dev/install (host: " + args.sdcard + ")") - pmb.helpers.mount.bind_file(args, args.sdcard, + if not os.path.exists(path): + raise RuntimeError(f"The sdcard device does not exist: {path}") + for path_mount in glob.glob(f"{path}*"): + if pmb.helpers.mount.ismount(path_mount): + raise RuntimeError(f"{path_mount} is mounted! Will not attempt to" + " format this!") + logging.info(f"(native) mount /dev/install (host: {path})") + pmb.helpers.mount.bind_file(args, path, args.work + "/chroot_native/dev/install") - if previous_install(args): + if previous_install(args, path): if not pmb.helpers.cli.confirm(args, "WARNING: This device has a" " previous installation of pmOS." " CONTINUE?"): raise RuntimeError("Aborted.") else: - if not pmb.helpers.cli.confirm(args, "EVERYTHING ON " + args.sdcard + - " WILL BE ERASED! CONTINUE?"): + if not pmb.helpers.cli.confirm(args, f"EVERYTHING ON {path} WILL BE" + " ERASED! CONTINUE?"): raise RuntimeError("Aborted.") @@ -115,7 +118,7 @@ def create_and_mount_image(args, size_boot, size_root, size_reserve, args.work + "/chroot_native" + mount_point) -def create(args, size_boot, size_root, size_reserve, split): +def create(args, size_boot, size_root, size_reserve, split, sdcard): """ Create /dev/install (the "install blockdevice"). @@ -123,11 +126,12 @@ def create(args, size_boot, size_root, size_reserve, split): :param size_root: size of the root partition in MiB :param size_reserve: empty partition between root and boot in MiB (pma#463) :param split: create separate images for boot and root partitions + :param sdcard: path to sdcard device (e.g. /dev/mmcblk0) or None """ pmb.helpers.mount.umount_all( args, args.work + "/chroot_native/dev/install") - if args.sdcard: - mount_sdcard(args) + if sdcard: + mount_sdcard(args, sdcard) else: create_and_mount_image(args, size_boot, size_root, size_reserve, split) diff --git a/pmb/install/format.py b/pmb/install/format.py index acfa5767..85f3f82b 100644 --- a/pmb/install/format.py +++ b/pmb/install/format.py @@ -43,10 +43,11 @@ def format_and_mount_root(args, device): raise RuntimeError("Failed to open cryptdevice!") -def format_and_mount_pm_crypt(args, device, root_label): +def format_and_mount_pm_crypt(args, device, root_label, sdcard): """ :param device: root partition on install block device (e.g. /dev/installp2) :param root_label: label of the root partition (e.g. "pmOS_root") + :param sdcard: path to sdcard device (e.g. /dev/mmcblk0) or None """ # Block device if args.full_disk_encryption: @@ -64,7 +65,7 @@ def format_and_mount_pm_crypt(args, device, root_label): # When we don't know the file system size before hand like # with non-block devices, we need to explicitely set a number of # inodes. See #1717 and #1845 for details - if not args.sdcard: + if not sdcard: mkfs_ext4_args = mkfs_ext4_args + ["-N", "100000"] pmb.chroot.root(args, mkfs_ext4_args + [device]) @@ -76,12 +77,13 @@ def format_and_mount_pm_crypt(args, device, root_label): pmb.chroot.root(args, ["mount", device, mountpoint]) -def format(args, size_reserve, root_label): +def format(args, size_reserve, root_label, sdcard): """ :param size_reserve: empty partition between root and boot in MiB (pma#463) :param root_label: label of the root partition (e.g. "pmOS_root") + :param sdcard: path to sdcard device (e.g. /dev/mmcblk0) or None """ root_dev = "/dev/installp3" if size_reserve else "/dev/installp2" format_and_mount_root(args, root_dev) - format_and_mount_pm_crypt(args, root_dev, root_label) + format_and_mount_pm_crypt(args, root_dev, root_label, sdcard) format_and_mount_boot(args) diff --git a/pmb/install/partition.py b/pmb/install/partition.py index 56ef8e36..3a2177a3 100644 --- a/pmb/install/partition.py +++ b/pmb/install/partition.py @@ -8,13 +8,14 @@ import pmb.config import pmb.install.losetup -def partitions_mount(args, root_id): +def partitions_mount(args, root_id, sdcard): """ Mount blockdevices of partitions inside native chroot :param root_id: root partition id + :param sdcard: path to sdcard device (e.g. /dev/mmcblk0) or None """ - prefix = args.sdcard - if not args.sdcard: + prefix = sdcard + if not sdcard: img_path = "/home/pmos/rootfs/" + args.device + ".img" prefix = pmb.install.losetup.device_by_back_file(args, img_path)