Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

build: Use mtools instead of libguestfs to generate image #28

Merged
merged 6 commits into from
Jun 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ ARG DEBCONF_NONINTERACTIVE_SEEN="true"
RUN set -eu && \
apt-get update && \
apt-get --no-install-recommends -y install \
guestfish \
linux-image-generic && \
fdisk \
mtools && \
apt-get clean && \
rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

Expand Down
20 changes: 11 additions & 9 deletions config.plist
Original file line number Diff line number Diff line change
Expand Up @@ -1015,7 +1015,7 @@
<key>HibernateSkipsPicker</key>
<false/>
<key>HideAuxiliary</key>
<true/>
<false/>
<key>InstanceIdentifier</key>
<string></string>
<key>LauncherOption</key>
Expand All @@ -1037,7 +1037,7 @@
<key>TakeoffDelay</key>
<integer>0</integer>
<key>Timeout</key>
<integer>15</integer>
<integer>0</integer>
</dict>
<key>Debug</key>
<dict>
Expand All @@ -1063,7 +1063,7 @@
<key>Security</key>
<dict>
<key>AllowSetDefault</key>
<true/>
<false/>
<key>ApECID</key>
<integer>0</integer>
<key>AuthRestart</key>
Expand All @@ -1083,7 +1083,7 @@
<key>PasswordSalt</key>
<data></data>
<key>ScanPolicy</key>
<integer>17761027</integer>
<integer>18809603</integer>
<key>SecureBootModel</key>
<string>Disabled</string>
<key>Vault</key>
Expand Down Expand Up @@ -1189,7 +1189,7 @@
<key>SystemAudioVolume</key>
<data>Rg==</data>
<key>boot-args</key>
<string>-v keepsyms=1 amfi_get_out_of_my_way=1 tlbto_us=0 vti=9</string>
<string>keepsyms=1</string>
<key>csr-active-config</key>
<data>Jg8=</data>
<key>prev-lang:kbd</key>
Expand Down Expand Up @@ -1260,21 +1260,23 @@
<key>AdviseFeatures</key>
<false/>
<key>MLB</key>
<string>C02119700QXJG36JC</string>
<string>C02717306J9JG361M</string>
<key>MaxBIOSVersion</key>
<false/>
<key>ProcessorType</key>
<integer>0</integer>
<key>ROM</key>
<string>0022412e3d4a</string>
<data>m7zhIYfl</data>
<key>SpoofVendor</key>
<true/>
<key>SystemMemoryStatus</key>
<string>Auto</string>
<key>SystemProductName</key>
<string>iMacPro1,1</string>
<key>SystemSerialNumber</key>
<string>C02FPBZPHX87</string>
<string>C02TM2ZBHX87</string>
<key>SystemUUID</key>
<string>213FA768-A62C-4299-8598-47AA666E436F</string>
<string>007076A6-F2A2-4461-BBE5-BAD019F8025A</string>
</dict>
<key>UpdateDataHub</key>
<true/>
Expand Down
4 changes: 2 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,15 @@ kubectl apply -f kubernetes.yml

- Start the container and connect to [port 8006](http://localhost:8006) using your web browser.

- Select `macOS Base System` by pressing the enter key to begin the installation.
- Select `macOS Base System` (by pressing the enter key) to begin the installation.

- Choose `Disk Utility` and then select the largest `Apple Inc. VirtIO Block Media` disk.

- Click the `Erase` button to format the disk, and give it any recognizable name you like.

- Close the current window and proceed the installation by clicking `Reinstall macOS`.

- When prompted where you want to install macOS, select the disk you just created previously.
- When prompted where you want to install it, select the disk you just created previously.

- There will be several reboots, select `macOS Installer` each time until it shows your own disk.

Expand Down
97 changes: 58 additions & 39 deletions src/build.sh
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!"