-
Notifications
You must be signed in to change notification settings - Fork 208
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
build: Use mtools instead of libguestfs to generate image (#28)
- Loading branch information
Showing
4 changed files
with
73 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,67 @@ | ||
#!/usr/bin/env bash | ||
set -Eeuo pipefail | ||
|
||
function msg() { | ||
local txt="$1" | ||
local bold="\x1b[1m" | ||
local normal="\x1b[0m" | ||
echo -e "${bold}### ${txt}${normal}" | ||
} | ||
|
||
function cleanup() { | ||
if test "$GUESTFISH_PID" != ""; then | ||
guestfish --remote -- exit >/dev/null 2>&1 || true | ||
fi | ||
} | ||
|
||
function fish() { | ||
echo "#" "$@" | ||
guestfish --remote -- "$@" || exit 1 | ||
} | ||
|
||
rm -rf /images | ||
msg "Mounting template ISO..." | ||
|
||
trap 'cleanup' EXIT | ||
|
||
export LIBGUESTFS_BACKEND=direct | ||
# shellcheck disable=SC2046 | ||
eval $(guestfish --listen) | ||
if test "$GUESTFISH_PID" = ""; then | ||
echo "ERROR: Starting Guestfish failed!" | ||
exit 1 | ||
echo "Extracting template image..." | ||
|
||
DST="/images" | ||
OUT="/tmp/extract" | ||
|
||
rm -rf "$OUT" | ||
rm -rf "$DST" | ||
|
||
mkdir -p "$OUT" | ||
mkdir -p "$DST" | ||
|
||
if [ ! -f "$1" ] || [ ! -s "$1" ]; then | ||
error "Could not find image file \"$1\"." && exit 10 | ||
fi | ||
|
||
fish add "$1" | ||
fish run | ||
fish mount /dev/sda1 / | ||
fish ls /EFI | ||
START=$(sfdisk -l "$1" | grep -i -m 1 "EFI System" | awk '{print $2}') | ||
mcopy -bspmQ -i "$1@@${START}S" ::EFI "$OUT" | ||
|
||
cp "$2" "$OUT/EFI/OC/" | ||
|
||
echo "Creating OpenCore image..." | ||
|
||
START=2048 | ||
COUNT=522207 | ||
SIZE=268435456 | ||
|
||
FILE="OpenCore.img" | ||
IMG="/tmp/$SIZE.img" | ||
NAME=$(basename "$IMG") | ||
|
||
rm -f "$IMG" | ||
|
||
if ! truncate -s "$SIZE" "$IMG"; then | ||
rm -f "$IMG" | ||
error "Could not allocate file $IMG for the OpenCore image." && exit 11 | ||
fi | ||
|
||
PART="/tmp/partition.fdisk" | ||
|
||
{ echo "label: gpt" | ||
echo "label-id: 1ACB1E00-3B8F-4B2A-86A4-D99ED21DCAEB" | ||
echo "device: $NAME" | ||
echo "unit: sectors" | ||
echo "first-lba: 34" | ||
echo "last-lba: 524254" | ||
echo "sector-size: 512" | ||
echo "" | ||
echo "${NAME}1 : start=$START, size=$COUNT, type=C12A7328-F81F-11D2-BA4B-00A0C93EC93B, uuid=05157F6E-0AE8-4D1A-BEA5-AC172453D02C, name=\"primary\"" | ||
|
||
} > "$PART" | ||
|
||
sfdisk -q "$IMG" < "$PART" | ||
|
||
echo "drive c: file=\"$IMG\" partition=0 offset=1048576" > /etc/mtools.conf | ||
|
||
mformat -F -M 512 -c 4 -T "$COUNT" -v "EFI" "C:" | ||
|
||
msg "Overriding config..." | ||
echo "Copying files to image..." | ||
|
||
fish copy-in "$2" /EFI/OC/ | ||
fish umount-all | ||
mcopy -bspmQ "$OUT/EFI" "C:" | ||
|
||
mkdir -p /images | ||
mv "$1" /images/OpenCore.img | ||
mv -f "$IMG" "$DST/$FILE" | ||
|
||
msg "Finished succesfully!" | ||
echo "Finished succesfully!" |