#!/bin/bash # # Copyright (C) 2022 The Android Open Source Project # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. #
set -e
set -u
SCRIPT_DIR=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd -P)
# Validate format of the input disk
/sbin/sgdisk -p "${input}" | grep -q "Disk identifier (GUID)" || \
( echo"${input} is not a GUID partitioned disk!" && exit 2 )
partitions="$(/sbin/sgdisk -p "${input}" | \
grep -m1 -A2 "Number Start (sector)" | tail -n2)"
( IFS=$'\n' for line in $partitions; do
IFS=' ' read -r -a partition <<< "$line" if [[ "${partition[0]}" = "1" && "${partition[5]}" != "EF00" ]]; then echo"${input} partition 1 is not an ESP!" && exit 3 fi if [[ "${partition[0]}" = "2" && "${partition[6]}" != "rootfs" ]]; then echo"${input} partition 2 is not rootfs!" && exit 4 fi done )
# Build a grub.cfg for CD booting cat >"${workdir}"/grub.cfg <<EOF
set timeout=0
menuentry "Linux" {
linux /vmlinuz ${grub_cmdline} root=${grub_rootfs}
initrd /initrd.img
}
EOF
# Build harddisk install script cat >"${workdir}"/install.sh << EOF #!/bin/sh
set -e
set -u
SCRIPT_DIR=\$(CDPATH= cd -- "\$(dirname -- "\${0}")" && pwd -P) if [ "\${1#*nvme}" != "\${1}" ]; then
partition=p else
partition= fi
sgdisk --load-backup="\${SCRIPT_DIR}"/gpt.img \${1}
sgdisk --delete=2 \${1}
sgdisk --new=2:129M:0 --typecode=2:8305 --change-name=2:rootfs --attributes=2:set:2 \${1}
partx -v --update \${1}
dd if="\${SCRIPT_DIR}"/esp.img of=\${1}\${partition}1 bs=16M
mkfs.ext4 -L ROOT -U \$(cat \${SCRIPT_DIR}/rootfs_uuid) \${1}\${partition}2
mount \${1}\${partition}2 /media
tar -C /media -Spxf \${SCRIPT_DIR}/rootfs.tar.xz
umount /media
EOF
chmod a+x "${workdir}"/install.sh
# Back up the ESP so we can restore it when installing
touch "${workdir}"/esp.img
dd if="${input}" of="${workdir}"/esp.img bs=512 skip=${efi_partition_start} count=${efi_partition_num_sectors} status=none >/dev/null
# Determine the architecture of the disk from the portable GRUB image path
sudo mount -o loop,offset=${efi_partition_offset} "${input}""${mount}"
unmount() {
sudo umount "${mount}"
}
trap unmount EXIT
grub_blob=$(cd "${mount}" && echo EFI/Boot/*)
case "${grub_blob}" in
EFI/Boot/BOOTAA64.EFI)
grub_arch=arm64-efi
grub_cd=gcdaa64.efi
;;
EFI/Boot/BOOTIA64.EFI)
grub_arch=x86_64-efi
grub_cd=gcdx64.efi
;;
*) echo"Unknown GRUB architecture for ${grub_blob}!"
exit 5
;;
esac
sudo umount "${mount}"
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.