250 lines
12 KiB
Bash
250 lines
12 KiB
Bash
set -ex
|
|
|
|
DISK_NAME=${PWD}/decode-sgdisk.img
|
|
# cleanup
|
|
touch $DISK_NAME
|
|
rm "${DISK_NAME}"
|
|
|
|
# Partition sizes in MiB
|
|
IDBLOADER_SIZE=4
|
|
UBOOT_SIZE=8
|
|
TRUST_SIZE=12
|
|
BOOT_SIZE=16
|
|
ROOT_SIZE=128
|
|
|
|
# Define max sizes and offsets
|
|
IDBLOADER_START=64 # was 64 but report it is not aligned, 2048, which is 1MB alignment.
|
|
IDBLOADER_OFFSET=$((${IDBLOADER_SIZE} * 1024 * 1024 / 512)) # 8192 4MiB 8192
|
|
UBOOT_OFFSET=$((${UBOOT_SIZE} * 1024 * 1024 / 512)) # 16382 8MiB 16384
|
|
TRUST_OFFSET=$((${TRUST_SIZE} * 1024 * 1024 / 512)) # 24575 12MiB 24575
|
|
BOOT_OFFSET=$((${BOOT_SIZE} * 1024 * 1024 / 512)) # 32768 16MiB 32768
|
|
ROOT_OFFSET=$((${ROOT_SIZE} * 1024 * 1024 / 512)) # 524288 128MiB 262144
|
|
|
|
MIN_SIZE=$((500 * 1000 * 1000 / 512)) # 976562 500MiB
|
|
SIZE_STEP=$((100 * 1000 * 1000 / 512)) # 195312 100MiB
|
|
MAX_SIZE=$((8 * 1000 * 1000 * 1000 / 512)) # 15625000 8GiB
|
|
|
|
# Create
|
|
rm -f "$DISK_NAME"
|
|
truncate --size "$((MAX_SIZE * 512))" "$DISK_NAME"
|
|
|
|
bootfs="fat32"
|
|
rootfs="ext2"
|
|
alignment_type="optimal" # none, cylinder, minimal, optimal
|
|
|
|
gpt_loader1=(64 8063) # name:loader1 type=Linux data 8300 label=""
|
|
gpt_reserved1=(8064 8191) # name:reserved1 type=Linux data 8300 label=""
|
|
gpt_reserved2=(8192 16383) # name:reserved2 type=Linux data 8300 label=""
|
|
gpt_loader2=(16384 24575) # name:reserved2 type=Linux data 8300 label=""
|
|
gpt_atf=(24576 32767) # name:atf type=Linux data 8300 label=""
|
|
gpt_boot=(32768 262143) # name:boot type=Basic data 0700 label="boot" Attr=msftdata
|
|
gpt_root=(262144) # name:root type=Linux data 8300 label="linux-root" Attr=legacy-boot=1 # NB change to type=Linux ARM64 root 8305
|
|
|
|
gpt_loader1_size=4
|
|
gpt_reserved1_size=64 # in k
|
|
gpt_reserved2_size=4
|
|
gpt_loader2_size=4
|
|
gpt_atf_size=4
|
|
gpt_boot_size=112
|
|
|
|
# Create partitions
|
|
echo Updating GPT...
|
|
|
|
TARGET=${DISK_NAME}
|
|
|
|
/sbin/sgdisk --zap-all ${TARGET}
|
|
|
|
partition_loader1() {
|
|
## Partition 1 - Create partition using start and end values for partitioning the `loader1` partition.
|
|
# STARTSECTOR=$(/sbin/sgdisk --first-aligned-in-largest ${TARGET}|sed -sn 2p)
|
|
STARTSECTOR=${gpt_loader[1]}
|
|
/sbin/sgdisk --print --set-alignment=64 --mbrtogpt --new=1:${STARTSECTOR}:+${gpt_loader1_size}MB ${TARGET}
|
|
/sbin/sgdisk --print --change-name=1:"loader1" --typecode=1:8300 ${TARGET}
|
|
}
|
|
|
|
partition_reserved1() {
|
|
## Partition 2 - Create partition using start and end values for partitioning the `reserved1` partition.
|
|
# ENDSECTOR=$(/sbin/sgdisk --end-of-largest ${TARGET}|sed -sn 2p)
|
|
# /sbin/sgdisk --print --new=2:-${gpt_reserved1_size}KB:${ENDSECTOR} ${TARGET}
|
|
STARTSECTOR=$(/sbin/sgdisk --first-in-largest ${TARGET} | sed -sn 2p)
|
|
/sbin/sgdisk --print --new=2:${STARTSECTOR}:+${gpt_reserved1_size}KB ${TARGET}
|
|
/sbin/sgdisk --print --change-name=2:"reserved1" --typecode=2:8300 ${TARGET}
|
|
}
|
|
|
|
partition_reserved2() {
|
|
## Partition 3 - Create partition using start and end values for partitioning the `reserved2` partition.
|
|
# ENDSECTOR=$(/sbin/sgdisk --end-of-largest ${TARGET}|sed -sn 2p)
|
|
# /sbin/sgdisk --print --new=3:-${gpt_reserved1_size}MB:${ENDSECTOR} ${TARGET}
|
|
STARTSECTOR=$(/sbin/sgdisk --first-in-largest ${TARGET} | sed -sn 2p)
|
|
/sbin/sgdisk --print --new=3:${STARTSECTOR}:+${gpt_reserved2_size}MB ${TARGET}
|
|
/sbin/sgdisk --print --change-name=3:"reserved2" --typecode=3:8300 ${TARGET}
|
|
}
|
|
|
|
partition_loader2() {
|
|
## Partition 4 - Create partition using start and end values for partitioning the `loader2` partition.
|
|
# ENDSECTOR=$(/sbin/sgdisk --end-of-largest ${TARGET}|sed -sn 2p)
|
|
# /sbin/sgdisk --print --new=4:-${gpt_loader2_size}MB:${ENDSECTOR} ${TARGET}
|
|
STARTSECTOR=$(/sbin/sgdisk --first-in-largest ${TARGET} | sed -sn 2p)
|
|
/sbin/sgdisk --print --new=4:${STARTSECTOR}:+${gpt_loader2_size}MB ${TARGET}
|
|
/sbin/sgdisk --print --change-name=4:"loader2" --typecode=4:8300 ${TARGET}
|
|
}
|
|
|
|
partition_atf() {
|
|
## Partition 5 - Create partition using start and end values for partitioning the `atf` partition.
|
|
# ENDSECTOR=$(/sbin/sgdisk --end-of-largest ${TARGET}|sed -sn 2p)
|
|
# /sbin/sgdisk --print --new=4:-${gpt_atf_size}MB:${ENDSECTOR} ${TARGET}
|
|
STARTSECTOR=$(/sbin/sgdisk --first-in-largest ${TARGET} | sed -sn 2p)
|
|
/sbin/sgdisk --print --new=5:${STARTSECTOR}:+${gpt_atf_size}MB ${TARGET}
|
|
/sbin/sgdisk --print --change-name=5:"atf" --typecode=5:8300 ${TARGET}
|
|
}
|
|
|
|
partition_boot() {
|
|
## Partition 6 - the 112MB partition for boot
|
|
# ENDSECTOR=$(/sbin/sgdisk --end-of-largest ${TARGET}|sed -sn 2p)
|
|
# /sbin/sgdisk --print --new=6:-${gpt_boot_size}MB:${ENDSECTOR} ${TARGET}
|
|
STARTSECTOR=$(/sbin/sgdisk --first-in-largest ${TARGET} | sed -sn 2p)
|
|
/sbin/sgdisk --print --new=6:${STARTSECTOR}:+${gpt_boot_size}MB ${TARGET}
|
|
/sbin/sgdisk --print --change-name=6:"boot" --typecode=6:EF00 ${TARGET}
|
|
}
|
|
|
|
partition_root() {
|
|
## Partition 7 - the main partition for root
|
|
/sbin/sgdisk --print --largest-new=7 ${TARGET}
|
|
/sbin/sgdisk --print --change-name=7:root --typecode=7:8305 ${TARGET}
|
|
}
|
|
|
|
image_format_partitions() {
|
|
mkfs.ext4 -L "linux-boot" "${loopdevice}p6"
|
|
mkfs.ext4 -L "linux-root" "${loopdevice}p7"
|
|
tune2fs -o journal_data_writeback "${loopdevice}p7"
|
|
}
|
|
|
|
loop_it() {
|
|
loopdevice=$(sudo losetup -f --show $DISK_NAME)
|
|
echo "Loop device is: " $loopdevice
|
|
sudo partx -av $loopdevice || zerr
|
|
|
|
loader1="${loopdevice}p1"
|
|
reserved1="${loopdevice}p2"
|
|
reserved2="${loopdevice}p3"
|
|
loader2="${loopdevice}p4"
|
|
atf="${loopdevice}p5"
|
|
boot="${loopdevice}p6"
|
|
root="${loopdevice}p7"
|
|
|
|
mkfs.ext2 -L "linux-boot" "$boot"
|
|
mkfs.ext4 -L "linux-root" "$root"
|
|
tune2fs -o journal_data_writeback "$root"
|
|
}
|
|
|
|
partition_loader1
|
|
partition_reserved1
|
|
partition_reserved2
|
|
partition_loader2
|
|
partition_atf
|
|
partition_boot
|
|
partition_root
|
|
loop_it
|
|
|
|
#/sbin/sgdisk -print ${TARGET}
|
|
cgpt show ${TARGET}
|
|
|
|
# sgdisk -L
|
|
#
|
|
# 0700 Microsoft basic data 0c01 Microsoft reserved
|
|
# 2700 Windows RE 3000 ONIE boot
|
|
# 3001 ONIE config 3900 Plan 9
|
|
# 4100 PowerPC PReP boot 4200 Windows LDM data
|
|
# 4201 Windows LDM metadata 4202 Windows Storage Spaces
|
|
# 7501 IBM GPFS 7f00 ChromeOS kernel
|
|
# 7f01 ChromeOS root 7f02 ChromeOS reserved
|
|
# 8200 Linux swap 8300 Linux filesystem
|
|
# 8301 Linux reserved 8302 Linux /home
|
|
# 8303 Linux x86 root (/) 8304 Linux x86-64 root (/)
|
|
# 8305 Linux ARM64 root (/) 8306 Linux /srv
|
|
# 8307 Linux ARM32 root (/) 8308 Linux dm-crypt
|
|
# 8309 Linux LUKS 830a Linux IA-64 root (/)
|
|
# 830b Linux x86 root verity 830c Linux x86-64 root verity
|
|
# 830d Linux ARM32 root verity 830e Linux ARM64 root verity
|
|
# 830f Linux IA-64 root verity 8310 Linux /var
|
|
# 8311 Linux /var/tmp 8400 Intel Rapid Start
|
|
# 8500 Container Linux /usr 8501 Container Linux resizable rootfs
|
|
# 8502 Container Linux /OEM customization 8503 Container Linux root on RAID
|
|
# 8e00 Linux LVM a000 Android bootloader
|
|
# a001 Android bootloader 2 a002 Android boot 1
|
|
# a003 Android recovery 1 a004 Android misc
|
|
# a005 Android metadata a006 Android system 1
|
|
# a007 Android cache a008 Android data
|
|
# a009 Android persistent a00a Android factory
|
|
# a00b Android fastboot/tertiary a00c Android OEM
|
|
# a00d Android vendor a00e Android config
|
|
# a00f Android factory (alt) a010 Android meta
|
|
# a011 Android EXT a012 Android SBL1
|
|
# a013 Android SBL2 a014 Android SBL3
|
|
# a015 Android APPSBL a016 Android QSEE/tz
|
|
# a017 Android QHEE/hyp a018 Android RPM
|
|
# a019 Android WDOG debug/sdi a01a Android DDR
|
|
# a01b Android CDT a01c Android RAM dump
|
|
# a01d Android SEC a01e Android PMIC
|
|
# a01f Android misc 1 a020 Android misc 2
|
|
# a021 Android device info a022 Android APDP
|
|
# a023 Android MSADP a024 Android DPO
|
|
# a025 Android recovery 2 a026 Android persist
|
|
# a027 Android modem ST1 a028 Android modem ST2
|
|
# a029 Android FSC a02a Android FSG 1
|
|
# a02b Android FSG 2 a02c Android SSD
|
|
# a02d Android keystore a02e Android encrypt
|
|
# a02f Android EKSST a030 Android RCT
|
|
# a031 Android spare1 a032 Android spare2
|
|
# a033 Android spare3 a034 Android spare4
|
|
# a035 Android raw resources a036 Android boot 2
|
|
# a037 Android FOTA a038 Android system 2
|
|
# a039 Android cache a03a Android user data
|
|
# a03b LG (Android) advanced flasher a03c Android PG1FS
|
|
# a03d Android PG2FS a03e Android board info
|
|
# a03f Android MFG a040 Android limits
|
|
# a200 Atari TOS basic data a500 FreeBSD disklabel
|
|
# a501 FreeBSD boot a502 FreeBSD swap
|
|
# a503 FreeBSD UFS a504 FreeBSD ZFS
|
|
# a505 FreeBSD Vinum/RAID a580 Midnight BSD data
|
|
# a581 Midnight BSD boot a582 Midnight BSD swap
|
|
# a583 Midnight BSD UFS a584 Midnight BSD ZFS
|
|
# a585 Midnight BSD Vinum a600 OpenBSD disklabel
|
|
# a800 Apple UFS a901 NetBSD swap
|
|
# a902 NetBSD FFS a903 NetBSD LFS
|
|
# a904 NetBSD concatenated a905 NetBSD encrypted
|
|
# a906 NetBSD RAID ab00 Recovery HD
|
|
# af00 Apple HFS/HFS+ af01 Apple RAID
|
|
# af02 Apple RAID offline af03 Apple label
|
|
# af04 AppleTV recovery af05 Apple Core Storage
|
|
# af06 Apple SoftRAID Status af07 Apple SoftRAID Scratch
|
|
# af08 Apple SoftRAID Volume af09 Apple SoftRAID Cache
|
|
# af0a Apple APFS b300 QNX6 Power-Safe
|
|
# bc00 Acronis Secure Zone be00 Solaris boot
|
|
# bf00 Solaris root bf01 Solaris /usr & Mac ZFS
|
|
# bf02 Solaris swap bf03 Solaris backup
|
|
# bf04 Solaris /var bf05 Solaris /home
|
|
# bf06 Solaris alternate sector bf07 Solaris Reserved 1
|
|
# bf08 Solaris Reserved 2 bf09 Solaris Reserved 3
|
|
# bf0a Solaris Reserved 4 bf0b Solaris Reserved 5
|
|
# c001 HP-UX data c002 HP-UX service
|
|
# e100 ONIE boot e101 ONIE config
|
|
# e900 Veracrypt data ea00 Freedesktop $BOOT
|
|
# eb00 Haiku BFS ed00 Sony system partition
|
|
# ed01 Lenovo system partition ef00 EFI system partition
|
|
# ef01 MBR partition scheme ef02 BIOS boot partition
|
|
# f800 Ceph OSD f801 Ceph dm-crypt OSD
|
|
# f802 Ceph journal f803 Ceph dm-crypt journal
|
|
# f804 Ceph disk in creation f805 Ceph dm-crypt disk in creation
|
|
# f806 Ceph block f807 Ceph block DB
|
|
# f808 Ceph block write-ahead log f809 Ceph lockbox for dm-crypt keys
|
|
# f80a Ceph multipath OSD f80b Ceph multipath journal
|
|
# f80c Ceph multipath block 1 f80d Ceph multipath block 2
|
|
# f80e Ceph multipath block DB f80f Ceph multipath block write-ahead l
|
|
# f810 Ceph dm-crypt block f811 Ceph dm-crypt block DB
|
|
# f812 Ceph dm-crypt block write-ahead lo f813 Ceph dm-crypt LUKS journal
|
|
# f814 Ceph dm-crypt LUKS block f815 Ceph dm-crypt LUKS block DB
|
|
# f816 Ceph dm-crypt LUKS block write-ahe f817 Ceph dm-crypt LUKS OSD
|
|
# fb00 VMWare VMFS fb01 VMWare reserved
|
|
# fc00 VMWare kcore crash protection fd00 Linux RAID
|