|
| 1 | +# gdisk Tutorial |
| 2 | + |
| 3 | +Tutorial using gdisk on jailbroken iOS/tvOS/bridgeOS. |
| 4 | + |
| 5 | +## Notes |
| 6 | + |
| 7 | +This tutorial is for people that do not have experience with command-line |
| 8 | +partitioning with gdisk on Linux/Unix. If you know already, you may skip |
| 9 | +reading. Keep in mind that the sector size is `4096`. |
| 10 | + |
| 11 | +### Common Partition codes |
| 12 | + |
| 13 | +These partition codes may be used later. |
| 14 | +``` |
| 15 | +0700 = Microsoft basic data |
| 16 | +8200 = Linux swap |
| 17 | +8300 = Linux filesystem |
| 18 | +AF00 = Apple HFS/HFS+ |
| 19 | +AF0A = Apple APFS |
| 20 | +``` |
| 21 | + |
| 22 | +## Partitioning |
| 23 | + |
| 24 | +You should have ran `resize_apfs` to resize the apfs filesystem already. |
| 25 | +After resizing the filesystem, the partition table needs to be updated. |
| 26 | + |
| 27 | +Start by running gdisk against the main disk. |
| 28 | +``` |
| 29 | +# ./gdisk /dev/disk0 |
| 30 | +``` |
| 31 | + |
| 32 | +You should get: |
| 33 | +``` |
| 34 | +GPT fdisk (gdisk) version 1.0.10 |
| 35 | +
|
| 36 | +Warning: Devices opened with shared lock will not have their |
| 37 | +partition table automatically reloaded! |
| 38 | +Partition table scan: |
| 39 | + MBR: protective |
| 40 | + BSD: not present |
| 41 | + APM: not present |
| 42 | + GPT: present |
| 43 | +
|
| 44 | +Found valid GPT with protective MBR; using GPT. |
| 45 | +
|
| 46 | +``` |
| 47 | + |
| 48 | +Type `p` and enter to print the current partition table: |
| 49 | + |
| 50 | +``` |
| 51 | +Command (? for help): p |
| 52 | +Disk /dev/disk0: 62500000 sectors, 238.4 GiB |
| 53 | +Sector size (logical): 4096 bytes |
| 54 | +Disk identifier (GUID): 37FE5887-699F-463D-A34F-98CA0D99FBA3 |
| 55 | +Partition table holds up to 128 entries |
| 56 | +Main partition table begins at sector 2 and ends at sector 5 |
| 57 | +First usable sector is 6, last usable sector is 62499994 |
| 58 | +Partitions will be aligned on 2-sector boundaries |
| 59 | +Total free space is 1 sectors (4.0 KiB) |
| 60 | +
|
| 61 | +Number Start (sector) End (sector) Size Code Name |
| 62 | + 1 6 62499993 238.4 GiB AF0A Apple APFS |
| 63 | +
|
| 64 | +``` |
| 65 | + |
| 66 | +Then we will actually delete this partition since there's no such |
| 67 | +thing as resizing partition in gdisk. The changes are not written |
| 68 | +out until a `w` command is issued. |
| 69 | + |
| 70 | +Type `d` and enter to delete a partition. Since there is only one |
| 71 | +partition, the partition will be automatically selected and |
| 72 | +deelted. |
| 73 | + |
| 74 | +``` |
| 75 | +Command (? for help): d |
| 76 | +Using 1 |
| 77 | +
|
| 78 | +``` |
| 79 | + |
| 80 | +Then we need to create a new partition that exactly matches the size |
| 81 | +of the resized filesystem. Start and end are inclusive and each |
| 82 | +sector is 4096 bytes long. |
| 83 | + |
| 84 | +Type `n`, enter, and `1`, enter. For start, keep it at `6` like it |
| 85 | +originally is. For end, suppose the filesystem is resized to be |
| 86 | +`192000000000` bytes (this size must be sector-aligned), then the |
| 87 | +last sector is `(6 + (192000000000) / 4096) - 1 = 46875005` In |
| 88 | +general for a 4096-byte aligned filesystem size `n`, the last sector |
| 89 | +is `(start + (size) / 4096) - 1`. |
| 90 | + |
| 91 | +Type start and end sector. As for type code, use `AF0A` for `Apple APFS`: |
| 92 | + |
| 93 | +``` |
| 94 | +Command (? for help): n |
| 95 | +Partition number (1-128, default 1): 1 |
| 96 | +First sector (6-62499994, default = 6) or {+-}size{KMGTP}: 6 |
| 97 | +Last sector (6-62499994, default = 62499993) or {+-}size{KMGTP}: 46875005 |
| 98 | +Current type is AF00 (Apple HFS/HFS+) |
| 99 | +Hex code or GUID (L to show codes, Enter = AF00): AF0A |
| 100 | +Changed type of partition to 'Apple APFS' |
| 101 | +``` |
| 102 | + |
| 103 | +Now the APFS partition is restored. Custom partitions can now be created, |
| 104 | +suppose we want a partition to hold the rootfs: |
| 105 | + |
| 106 | +``` |
| 107 | +Command (? for help): n |
| 108 | +Partition number (2-128, default 2): 2 |
| 109 | +First sector (46875006-62499994, default = 46875006) or {+-}size{KMGTP}: 46875006 |
| 110 | +Last sector (46875006-62499994, default = 62499993) or {+-}size{KMGTP}: 61035156 |
| 111 | +Current type is AF00 (Apple HFS/HFS+) |
| 112 | +Hex code or GUID (L to show codes, Enter = AF00): 8300 |
| 113 | +Changed type of partition to 'Linux filesystem' |
| 114 | +``` |
| 115 | + |
| 116 | +where 8300 = `Linux filesystem` |
| 117 | + |
| 118 | +And a partition to hold the boot files: |
| 119 | + |
| 120 | +``` |
| 121 | +Command (? for help): n |
| 122 | +Partition number (3-128, default 3): 3 |
| 123 | +First sector (61035157-62499994, default = 61035158) or {+-}size{KMGTP}: 61035264 |
| 124 | +Last sector (61035264-62499994, default = 62499993) or {+-}size{KMGTP}: 61157119 |
| 125 | +Current type is AF00 (Apple HFS/HFS+) |
| 126 | +Hex code or GUID (L to show codes, Enter = AF00): 8300 |
| 127 | +Changed type of partition to 'Linux filesystem' |
| 128 | +``` |
| 129 | + |
| 130 | +where 8300 = `Linux filesystem` |
| 131 | + |
| 132 | +A FAT32 partition to hold earlier boot files: |
| 133 | + |
| 134 | +``` |
| 135 | +Command (? for help): n |
| 136 | +Partition number (4-128, default 4): 4 |
| 137 | +First sector (61035157-62499994, default = 61157120) or {+-}size{KMGTP}: 61157120 |
| 138 | +Last sector (61157120-62499994, default = 62499993) or {+-}size{KMGTP}: 61222655 |
| 139 | +Current type is AF00 (Apple HFS/HFS+) |
| 140 | +Hex code or GUID (L to show codes, Enter = af00): 0700 |
| 141 | +Changed type of partition to 'Microsoft basic data' |
| 142 | +``` |
| 143 | + |
| 144 | +where 0700 = `Microsoft basic data` |
| 145 | + |
| 146 | +A swap partition: |
| 147 | + |
| 148 | +``` |
| 149 | +Command (? for help): n |
| 150 | +Partition number (5-128, default 5): 5 |
| 151 | +First sector (61035157-62499994, default = 61222656) or {+-}size{KMGTP}: 61222656 |
| 152 | +Last sector (61222656-62499994, default = 62499993) or {+-}size{KMGTP}: 62499839 |
| 153 | +Current type is af00 (Apple HFS/HFS+) |
| 154 | +Hex code or GUID (L to show codes, Enter = af00): 8200 |
| 155 | +Changed type of partition to 'Linux swap' |
| 156 | +``` |
| 157 | + |
| 158 | +In this example, 4 new partitions are created and the existing APFS partition is |
| 159 | +shrunk. Type `p` to print the new partition table and CHECK CAREFULLY to avoid |
| 160 | +data loss: |
| 161 | + |
| 162 | +``` |
| 163 | +Command (? for help): p |
| 164 | +Disk /dev/disk0: 62500000 sectors, 238.4 GiB |
| 165 | +Sector size (logical): 4096 bytes |
| 166 | +Disk identifier (GUID): 37FE5887-699F-463D-A34F-98CA0D99FBA3 |
| 167 | +Partition table holds up to 128 entries |
| 168 | +Main partition table begins at sector 2 and ends at sector 5 |
| 169 | +First usable sector is 6, last usable sector is 62499994 |
| 170 | +Partitions will be aligned on 2-sector boundaries |
| 171 | +Total free space is 262 sectors (1.0 MiB) |
| 172 | +
|
| 173 | +Number Start (sector) End (sector) Size Code Name |
| 174 | + 1 6 46875005 178.8 GiB AF0A Apple APFS |
| 175 | + 2 46875006 61035156 54.0 GiB 8300 Linux filesystem |
| 176 | + 3 61035264 61157119 476.0 MiB 8300 Linux filesystem |
| 177 | + 4 61157120 61222655 256.0 MiB 0700 Microsoft basic data |
| 178 | + 5 61222656 62499839 4.9 GiB 8200 Linux swap |
| 179 | +``` |
| 180 | + |
| 181 | +When everything is good, type `w`, enter, confirm with `y` to actually |
| 182 | +write the partition table: |
| 183 | + |
| 184 | +``` |
| 185 | +Command (? for help): w |
| 186 | +
|
| 187 | +Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING |
| 188 | +PARTITIONS!! |
| 189 | +
|
| 190 | +Do you want to proceed? (Y/N): y |
| 191 | +OK; writing new GUID partition table (GPT) to /dev/disk0. |
| 192 | +Warning: Devices opened with shared lock will not have their |
| 193 | +partition table automatically reloaded! |
| 194 | +Warning: The kernel may continue to use old or deleted partitions. |
| 195 | +You should reboot or remove the drive. |
| 196 | +The operation has completed successfully. |
| 197 | +``` |
| 198 | + |
| 199 | +Now, check `/dev` to ensure the partitions `/dev/disk0s2`, |
| 200 | +`/dev/disk0s3`, ... are created. |
| 201 | + |
| 202 | +Now the partitions has been created. However, there are no filesystem |
| 203 | +on them. You will need to flash prebuilt filesystem images with `dd`, |
| 204 | +and for some other partitions, format them once booted into Linux. |
| 205 | +The filesystem images likely has to have a block size of 4096. |
| 206 | + |
| 207 | +For example, to flash a filesystem image, `root.img` into the second |
| 208 | +partition: |
| 209 | + |
| 210 | +``` |
| 211 | +# dd if=root.img of=/dev/disk0s2 |
| 212 | +``` |
| 213 | + |
| 214 | +For Apple A11, the partition will show up as `/dev/nvme0n1p...` in Linux. |
| 215 | +For example `/dev/disk0s2` is `/dev/nvme0n1p2` in Linux. |
0 commit comments