Debian Codegraph @

scripts/prepare-internal-storage

raw
1 #!/bin/sh -e
2 # Copyright 2020 Oliver Smith, Undef
3 # SPDX-License-Identifier: GPL-3.0-or-later
4 # Modified from https://gitlab.com/postmarketOS/postmarketos-ondev/-/blob/master/ondev-prepare-internal-storage.sh
5
6 # Sanity check
7 part_install="$(df -T / | awk '/^\/dev/ {print $1}')"
8 installer_part="$(realpath /dev/disk/by-label/installer)"
9 if [ "$installer_part" != "$part_install" ]; then
10 echo "ERROR: Unexpected installer partition; installer is not" \
11 "running from detected partition. Bailing!"
12 echo "This situation typically arises when the installer image" \
13 "exists on both embedded and removable storage."
14 echo "(installer_part: '$installer_part')"
15 echo "(part_install: '$part_install')"
16 exit 1
17 fi
18
19 echo " === prepare-internal-storage === "
20 set -x
21
22 dev=${INTERNAL_TARGET}
23 dev_boot=""
24 dev_root=""
25 boot_filesystem="ext4"
26 mb_boot_part_start="8"
27 mb_boot="512"
28
29 # Create partition table
30 partitions_create() {
31 parted -s "${dev}" mktable gpt
32 # Boot Partition
33 parted -s "${dev}" mkpart primary "${boot_filesystem}" \
34 "${mb_boot_part_start}M" "${mb_boot}M"
35 # Root Partition
36 parted -s "${dev}" mkpart primary "${mb_boot}M" "100%"
37 parted -s "${dev}" set 1 boot on
38 if grep -q "purism,librem5" /proc/device-tree/compatible; then
39 # This ensures that /dev/disk/by-partlabel/u-boot points to the installer
40 partprobe "${dev}"
41 # Retrieve u-boot from the current dedicated partition
42 dd if=/dev/disk/by-partlabel/u-boot of=/tmp/u-boot-librem5.imx
43 # We use parted for adding a "protective" partition for u-boot:
44 # * mkpart u-boot 66s ${BOOTSTART}: create "u-boot" partition from sector 66
45 # (33KiB) up to the start of the `/boot`
46 # partition
47 # * toggle 3 hidden: set flag "hidden" on partition 3 (the one we just created)
48 parted -s "${dev}" mkpart u-boot 66s "${mb_boot_part_start}M"
49 parted -s "${dev}" toggle 3 hidden
50 # Write u-boot to the this protective partition by skipping the first 33KiB of the disk
51 partprobe "${dev}"
52 if [ -e "${dev}p3" ]; then
53 uboot_part="${dev}p3"
54 else
55 uboot_part="${dev}3"
56 fi
57 dd if=/tmp/u-boot-librem5.imx of="${uboot_part}"
58 fi
59 partprobe "${dev}"
60 }
61
62 # Try to find the boot and root partitions for 10 seconds
63 partitions_find() {
64 for i in $(seq 1 100); do
65 if [ -e "${dev}p1" ] && [ -e "${dev}p2" ]; then
66 dev_boot="${dev}p1"
67 dev_root="${dev}p2"
68 return
69 elif [ -e "${dev}1" ] && [ -e "${dev}2" ]; then
70 dev_boot="${dev}1"
71 dev_root="${dev}2"
72 return
73 fi
74 sleep 0.1
75 done
76 echo "Failed to find boot and root partition after creating" \
77 "partition table (dev: '$dev')"
78 exit 1
79 }
80
81 # Configure the GPT Discoverable Partition GUIDs. See
82 # https://uapi-group.org/specifications/specs/discoverable_partitions_specification/
83 # for more information.
84 configure_dps_labels() {
85 case "$(uname -m)" in
86 x86_64)
87 part_type_root="4f68bce3-e8cd-4db1-96e7-fbcaf984b709"
88 ;;
89 aarch64)
90 part_type_root="b921b045-1df0-41c3-af44-4c6f280d3fae"
91 ;;
92 riscv64)
93 part_type_root="72ec70a6-cf74-40e6-bd49-4bda08e8f224"
94 ;;
95 armhf)
96 part_type_root="69dad710-2ce4-4e3c-b16c-21a1d49abed3"
97 ;;
98 esac
99 sfdisk --part-type "${dev}" 2 "${part_type_root}"
100 }
101
102 # Format the boot partition
103 boot_format() {
104 mkfs.ext4 -F -q -L boot "${dev_boot}"
105 }
106
107 # Copy the boot partition contents from /boot
108 boot_copy() {
109 local target_path="/mnt/install-boot"
110 local source_path="/boot"
111
112 mkdir -p "${target_path}"
113 mount "${dev_boot}" "${target_path}"
114 cp -a -r "${source_path}"/* "${target_path}"
115 umount "${target_path}"
116 }
117
118 partitions_create
119 partitions_find
120 configure_dps_labels
121 boot_format
122 boot_copy