2021-01-07 22:30:30 +00:00
|
|
|
# Copyright 2021 Oliver Smith
|
2020-02-20 20:07:28 +00:00
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
2017-05-26 20:08:45 +00:00
|
|
|
import logging
|
2017-08-30 18:58:51 +00:00
|
|
|
import os
|
|
|
|
import time
|
2017-05-26 20:08:45 +00:00
|
|
|
import pmb.chroot
|
|
|
|
import pmb.config
|
|
|
|
import pmb.install.losetup
|
|
|
|
|
|
|
|
|
2020-06-09 13:43:25 +00:00
|
|
|
def partitions_mount(args, root_id, sdcard):
|
2017-05-26 20:08:45 +00:00
|
|
|
"""
|
|
|
|
Mount blockdevices of partitions inside native chroot
|
2020-06-06 17:05:11 +00:00
|
|
|
:param root_id: root partition id
|
2020-06-09 13:43:25 +00:00
|
|
|
:param sdcard: path to sdcard device (e.g. /dev/mmcblk0) or None
|
2017-05-26 20:08:45 +00:00
|
|
|
"""
|
2020-06-09 13:43:25 +00:00
|
|
|
prefix = sdcard
|
|
|
|
if not sdcard:
|
2017-10-12 20:08:10 +00:00
|
|
|
img_path = "/home/pmos/rootfs/" + args.device + ".img"
|
2017-05-26 20:08:45 +00:00
|
|
|
prefix = pmb.install.losetup.device_by_back_file(args, img_path)
|
2017-06-04 00:57:37 +00:00
|
|
|
|
|
|
|
partition_prefix = None
|
2017-08-30 18:58:51 +00:00
|
|
|
tries = 20
|
|
|
|
for i in range(tries):
|
|
|
|
for symbol in ["p", ""]:
|
|
|
|
if os.path.exists(prefix + symbol + "1"):
|
|
|
|
partition_prefix = symbol
|
|
|
|
if partition_prefix is not None:
|
|
|
|
break
|
2021-02-28 19:01:31 +00:00
|
|
|
logging.debug(f"NOTE: ({i + 1}/{tries}) failed to find the install "
|
|
|
|
"partition. Retrying...")
|
2017-08-30 18:58:51 +00:00
|
|
|
time.sleep(0.1)
|
|
|
|
|
2017-06-09 22:29:48 +00:00
|
|
|
if partition_prefix is None:
|
2017-06-04 00:57:37 +00:00
|
|
|
raise RuntimeError("Unable to find the partition prefix,"
|
|
|
|
" expected the first partition of " +
|
2017-06-04 02:27:49 +00:00
|
|
|
prefix + " to be located at " + prefix +
|
|
|
|
"1 or " + prefix + "p1!")
|
2017-06-04 00:57:37 +00:00
|
|
|
|
2020-06-06 17:05:11 +00:00
|
|
|
for i in [1, root_id]:
|
2017-06-04 00:57:37 +00:00
|
|
|
source = prefix + partition_prefix + str(i)
|
|
|
|
target = args.work + "/chroot_native/dev/installp" + str(i)
|
2020-01-20 12:27:40 +00:00
|
|
|
pmb.helpers.mount.bind_file(args, source, target)
|
2017-05-26 20:08:45 +00:00
|
|
|
|
|
|
|
|
2020-06-06 17:05:11 +00:00
|
|
|
def partition(args, size_boot, size_reserve):
|
2017-05-26 20:08:45 +00:00
|
|
|
"""
|
2020-06-06 17:05:11 +00:00
|
|
|
Partition /dev/install and create /dev/install{p1,p2,p3}:
|
|
|
|
* /dev/installp1: boot
|
|
|
|
* /dev/installp2: root (or reserved space)
|
|
|
|
* /dev/installp3: (root, if reserved space > 0)
|
2017-05-26 20:08:45 +00:00
|
|
|
|
2020-12-19 10:52:08 +00:00
|
|
|
When adjusting this function, make sure to also adjust
|
|
|
|
ondev-prepare-internal-storage.sh in postmarketos-ondev.git!
|
|
|
|
|
2020-06-06 16:51:15 +00:00
|
|
|
:param size_boot: size of the boot partition in MiB
|
2020-06-06 17:05:11 +00:00
|
|
|
:param size_reserve: empty partition between root and boot in MiB (pma#463)
|
2017-08-18 19:19:48 +00:00
|
|
|
"""
|
|
|
|
# Convert to MB and print info
|
2020-06-06 17:05:11 +00:00
|
|
|
mb_boot = f"{round(size_boot)}M"
|
|
|
|
mb_reserved = f"{round(size_reserve)}M"
|
|
|
|
mb_root_start = f"{round(size_boot) + round(size_reserve)}M"
|
|
|
|
logging.info(f"(native) partition /dev/install (boot: {mb_boot},"
|
|
|
|
f" reserved: {mb_reserved}, root: the rest)")
|
2017-08-18 19:19:48 +00:00
|
|
|
|
2018-05-29 19:33:40 +00:00
|
|
|
filesystem = args.deviceinfo["boot_filesystem"] or "ext2"
|
|
|
|
|
2017-08-25 17:39:52 +00:00
|
|
|
# Actual partitioning with 'parted'. Using check=False, because parted
|
|
|
|
# sometimes "fails to inform the kernel". In case it really failed with
|
|
|
|
# partitioning, the follow-up mounting/formatting will not work, so it
|
|
|
|
# will stop there (see #463).
|
2020-01-12 03:24:37 +00:00
|
|
|
boot_part_start = args.deviceinfo["boot_part_start"] or "2048"
|
|
|
|
|
2017-05-26 20:08:45 +00:00
|
|
|
commands = [
|
|
|
|
["mktable", "msdos"],
|
2020-01-12 03:24:37 +00:00
|
|
|
["mkpart", "primary", filesystem, boot_part_start + 's', mb_boot],
|
2020-06-06 17:05:11 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
if size_reserve:
|
|
|
|
commands += [["mkpart", "primary", mb_boot, mb_reserved]]
|
|
|
|
|
|
|
|
commands += [
|
|
|
|
["mkpart", "primary", mb_root_start, "100%"],
|
2017-05-26 20:08:45 +00:00
|
|
|
["set", "1", "boot", "on"]
|
|
|
|
]
|
2020-06-06 17:05:11 +00:00
|
|
|
|
2017-05-26 20:08:45 +00:00
|
|
|
for command in commands:
|
|
|
|
pmb.chroot.root(args, ["parted", "-s", "/dev/install"] +
|
2017-08-25 17:39:52 +00:00
|
|
|
command, check=False)
|