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 os
|
|
|
|
import logging
|
|
|
|
import pmb.chroot
|
|
|
|
|
|
|
|
|
2021-03-07 13:37:59 +00:00
|
|
|
def install_fsprogs(args, filesystem):
|
|
|
|
""" Install the package required to format a specific filesystem. """
|
|
|
|
fsprogs = pmb.config.filesystems.get(filesystem)
|
|
|
|
if not fsprogs:
|
|
|
|
raise RuntimeError(f"Unsupported filesystem: {filesystem}")
|
|
|
|
pmb.chroot.apk.install(args, [fsprogs])
|
|
|
|
|
|
|
|
|
2020-12-18 21:14:13 +00:00
|
|
|
def format_and_mount_boot(args, boot_label):
|
|
|
|
"""
|
|
|
|
:param boot_label: label of the root partition (e.g. "pmOS_boot")
|
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-12-18 21:14:13 +00:00
|
|
|
"""
|
2017-05-26 20:08:45 +00:00
|
|
|
mountpoint = "/mnt/install/boot"
|
2017-06-22 10:19:53 +00:00
|
|
|
device = "/dev/installp1"
|
2018-05-29 19:33:40 +00:00
|
|
|
filesystem = args.deviceinfo["boot_filesystem"] or "ext2"
|
2021-03-07 13:37:59 +00:00
|
|
|
install_fsprogs(args, filesystem)
|
2020-12-19 10:55:24 +00:00
|
|
|
logging.info(f"(native) format {device} (boot, {filesystem}), mount to"
|
|
|
|
" mountpoint")
|
2018-05-29 19:33:40 +00:00
|
|
|
if filesystem == "fat16":
|
2020-12-18 21:14:13 +00:00
|
|
|
pmb.chroot.root(args, ["mkfs.fat", "-F", "16", "-n", boot_label,
|
|
|
|
device])
|
2019-10-09 18:21:43 +00:00
|
|
|
elif filesystem == "fat32":
|
2020-12-18 21:14:13 +00:00
|
|
|
pmb.chroot.root(args, ["mkfs.fat", "-F", "32", "-n", boot_label,
|
|
|
|
device])
|
2018-05-29 19:33:40 +00:00
|
|
|
elif filesystem == "ext2":
|
2020-12-18 21:14:13 +00:00
|
|
|
pmb.chroot.root(args, ["mkfs.ext2", "-F", "-q", "-L", boot_label,
|
|
|
|
device])
|
2018-05-29 19:33:40 +00:00
|
|
|
else:
|
|
|
|
raise RuntimeError("Filesystem " + filesystem + " is not supported!")
|
2017-05-26 20:08:45 +00:00
|
|
|
pmb.chroot.root(args, ["mkdir", "-p", mountpoint])
|
2017-06-22 10:19:53 +00:00
|
|
|
pmb.chroot.root(args, ["mount", device, mountpoint])
|
2017-05-26 20:08:45 +00:00
|
|
|
|
|
|
|
|
2020-10-11 11:23:27 +00:00
|
|
|
def format_luks_root(args, device):
|
2020-06-06 17:05:11 +00:00
|
|
|
"""
|
|
|
|
:param device: root partition on install block device (e.g. /dev/installp2)
|
|
|
|
"""
|
2017-05-26 20:08:45 +00:00
|
|
|
mountpoint = "/dev/mapper/pm_crypt"
|
2020-10-11 11:23:27 +00:00
|
|
|
|
|
|
|
logging.info(f"(native) format {device} (root, luks), mount to"
|
|
|
|
f" {mountpoint}")
|
|
|
|
logging.info(" *** TYPE IN THE FULL DISK ENCRYPTION PASSWORD (TWICE!) ***")
|
|
|
|
|
2020-10-11 11:35:44 +00:00
|
|
|
# Avoid cryptsetup warning about missing locking directory
|
|
|
|
pmb.chroot.root(args, ["mkdir", "-p", "/run/cryptsetup"])
|
|
|
|
|
2020-10-11 11:23:27 +00:00
|
|
|
pmb.chroot.root(args, ["cryptsetup", "luksFormat",
|
|
|
|
"-q",
|
|
|
|
"--cipher", args.cipher,
|
|
|
|
"--iter-time", args.iter_time,
|
2020-11-23 07:48:26 +00:00
|
|
|
"--use-random",
|
2020-10-11 11:23:27 +00:00
|
|
|
device], output="interactive")
|
|
|
|
pmb.chroot.root(args, ["cryptsetup", "luksOpen", device, "pm_crypt"],
|
|
|
|
output="interactive")
|
|
|
|
|
|
|
|
if not os.path.exists(f"{args.work}/chroot_native/{mountpoint}"):
|
|
|
|
raise RuntimeError("Failed to open cryptdevice!")
|
|
|
|
|
|
|
|
|
|
|
|
def format_and_mount_root(args, device, root_label, sdcard):
|
2020-06-06 17:05:11 +00:00
|
|
|
"""
|
|
|
|
:param device: root partition on install block device (e.g. /dev/installp2)
|
2020-06-07 06:50:28 +00:00
|
|
|
:param root_label: label of the root partition (e.g. "pmOS_root")
|
2020-06-09 13:43:25 +00:00
|
|
|
:param sdcard: path to sdcard device (e.g. /dev/mmcblk0) or None
|
2020-06-06 17:05:11 +00:00
|
|
|
"""
|
2018-01-28 23:25:21 +00:00
|
|
|
# Format
|
|
|
|
if not args.rsync:
|
2021-03-07 13:38:37 +00:00
|
|
|
filesystem = (args.filesystem or args.deviceinfo["root_filesystem"] or
|
|
|
|
"ext4")
|
|
|
|
|
|
|
|
if filesystem == "ext4":
|
|
|
|
# Some downstream kernels don't support metadata_csum (#1364).
|
|
|
|
# When changing the options of mkfs.ext4, also change them in the
|
|
|
|
# recovery zip code (see 'grep -r mkfs\.ext4')!
|
|
|
|
mkfs_root_args = ["mkfs.ext4", "-O", "^metadata_csum", "-F",
|
|
|
|
"-q", "-L", 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 sdcard:
|
|
|
|
mkfs_root_args = mkfs_root_args + ["-N", "100000"]
|
|
|
|
elif filesystem == "f2fs":
|
|
|
|
mkfs_root_args = ["mkfs.f2fs", "-f", "-l", root_label]
|
|
|
|
else:
|
|
|
|
raise RuntimeError(f"Don't know how to format {filesystem}!")
|
|
|
|
|
|
|
|
install_fsprogs(args, filesystem)
|
|
|
|
logging.info(f"(native) format {device} (root, {filesystem})")
|
|
|
|
pmb.chroot.root(args, mkfs_root_args + [device])
|
2018-01-28 23:25:21 +00:00
|
|
|
|
|
|
|
# Mount
|
2017-05-26 20:08:45 +00:00
|
|
|
mountpoint = "/mnt/install"
|
2018-01-28 23:25:21 +00:00
|
|
|
logging.info("(native) mount " + device + " to " + mountpoint)
|
2017-05-26 20:08:45 +00:00
|
|
|
pmb.chroot.root(args, ["mkdir", "-p", mountpoint])
|
2017-06-22 10:19:53 +00:00
|
|
|
pmb.chroot.root(args, ["mount", device, mountpoint])
|
2017-05-26 20:08:45 +00:00
|
|
|
|
|
|
|
|
2020-12-18 21:14:13 +00:00
|
|
|
def format(args, size_reserve, boot_label, root_label, sdcard):
|
2020-06-06 17:05:11 +00:00
|
|
|
"""
|
|
|
|
:param size_reserve: empty partition between root and boot in MiB (pma#463)
|
2020-12-18 21:14:13 +00:00
|
|
|
:param boot_label: label of the boot partition (e.g. "pmOS_boot")
|
2020-06-07 06:50:28 +00:00
|
|
|
:param root_label: label of the root partition (e.g. "pmOS_root")
|
2020-06-09 13:43:25 +00:00
|
|
|
:param sdcard: path to sdcard device (e.g. /dev/mmcblk0) or None
|
2020-06-06 17:05:11 +00:00
|
|
|
"""
|
|
|
|
root_dev = "/dev/installp3" if size_reserve else "/dev/installp2"
|
2020-10-11 11:23:27 +00:00
|
|
|
|
|
|
|
if args.full_disk_encryption:
|
|
|
|
format_luks_root(args, root_dev)
|
|
|
|
root_dev = "/dev/mapper/pm_crypt"
|
|
|
|
|
|
|
|
format_and_mount_root(args, root_dev, root_label, sdcard)
|
2020-12-18 21:14:13 +00:00
|
|
|
format_and_mount_boot(args, boot_label)
|