Skip to content

Commit

Permalink
feat: Generate unique machine serials (#132)
Browse files Browse the repository at this point in the history
  • Loading branch information
kroese authored Nov 16, 2024
1 parent ddff7a3 commit c6ec08c
Show file tree
Hide file tree
Showing 5 changed files with 163 additions and 145 deletions.
45 changes: 19 additions & 26 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,35 +1,22 @@
FROM --platform=linux/amd64 debian:trixie-slim AS builder
FROM --platform=$BUILDPLATFORM alpine AS builder

ARG VERSION_KVM_OPENCORE="v21"
ARG REPO_KVM_OPENCORE="https://github.com/thenickdude/KVM-Opencore"
ARG VERSION_OPENCORE="1.0.2"
ARG REPO_OPENCORE="https://github.com/acidanthera/OpenCorePkg"
ADD $REPO_OPENCORE/releases/download/$VERSION_OPENCORE/OpenCore-$VERSION_OPENCORE-RELEASE.zip /tmp/opencore.zip

ARG DEBCONF_NOWARNINGS="yes"
ARG DEBIAN_FRONTEND="noninteractive"
ARG DEBCONF_NONINTERACTIVE_SEEN="true"

RUN set -eu && \
apt-get update && \
apt-get --no-install-recommends -y install \
fdisk \
mtools && \
apt-get clean && \
rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

COPY --chmod=755 ./src/build.sh /run
COPY --chmod=644 ./config.plist /run

ADD $REPO_KVM_OPENCORE/releases/download/$VERSION_KVM_OPENCORE/OpenCore-$VERSION_KVM_OPENCORE.iso.gz /tmp/opencore.iso.gz

RUN gzip -d /tmp/opencore.iso.gz && \
run/build.sh /tmp/opencore.iso /run/config.plist && \
rm -rf /tmp/*
RUN apk --update add unzip && \
unzip /tmp/opencore.zip -d /tmp/oc && \
cp /tmp/oc/Utilities/macserial/macserial.linux /macserial && \
rm -rf /tmp/* /var/tmp/* /var/cache/apk/*

FROM scratch AS runner
COPY --from=qemux/qemu-docker:6.07 / /
COPY --from=qemux/qemu-docker:6.08 / /

ARG VERSION_ARG="0.0"
ARG VERSION_KVM_OPENCORE="v21"
ARG VERSION_OSX_KVM="326053dd61f49375d5dfb28ee715d38b04b5cd8e"
ARG REPO_OSX_KVM="https://raw.githubusercontent.com/kholia/OSX-KVM"
ARG REPO_KVM_OPENCORE="https://github.com/thenickdude/KVM-Opencore"

ARG DEBCONF_NOWARNINGS="yes"
ARG DEBIAN_FRONTEND="noninteractive"
Expand All @@ -38,22 +25,28 @@ ARG DEBCONF_NONINTERACTIVE_SEEN="true"
RUN set -eu && \
apt-get update && \
apt-get --no-install-recommends -y install \
xxd \
fdisk \
mtools \
python3 && \
apt-get clean && \
echo "$VERSION_ARG" > /run/version && \
rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

COPY --chmod=755 ./src /run/
COPY --chmod=644 --from=builder /images /images
COPY --chmod=644 ./config.plist /
COPY --chmod=755 --from=builder /macserial /usr/local/bin/

ADD --chmod=644 \
$REPO_OSX_KVM/$VERSION_OSX_KVM/OVMF_CODE.fd \
$REPO_OSX_KVM/$VERSION_OSX_KVM/OVMF_VARS.fd \
$REPO_OSX_KVM/$VERSION_OSX_KVM/OVMF_VARS-1024x768.fd \
$REPO_OSX_KVM/$VERSION_OSX_KVM/OVMF_VARS-1920x1080.fd /usr/share/OVMF/

EXPOSE 8006 5900
ADD $REPO_KVM_OPENCORE/releases/download/$VERSION_KVM_OPENCORE/OpenCore-$VERSION_KVM_OPENCORE.iso.gz /opencore.iso.gz

VOLUME /storage
EXPOSE 8006 5900

ENV VERSION="13"
ENV RAM_SIZE="4G"
Expand Down
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ kubectl apply -f https://raw.githubusercontent.com/dockur/macos/refs/heads/maste

- 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.
- Click the `Erase` button to format the disk to APFS, and give it any recognizable name you like.

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

Expand Down
129 changes: 98 additions & 31 deletions src/boot.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,28 @@
set -Eeuo pipefail

# Docker environment variables
: "${BOOT_MODE:="full"}" # Boot mode
: "${BOOT_MODE:="macos"}" # Boot mode

BOOT_DESC=""
BOOT_OPTS=""
SECURE="off"
OVMF="/usr/share/OVMF"

case "${BOOT_MODE,,}" in
"full" )
case "${HEIGHT,,}" in
"1080" )
DEST="$PROCESS"
BOOT_DESC=" 1920x1080"
ROM="OVMF_CODE.fd"
VARS="OVMF_VARS-1920x1080.fd"
;;
"hd" )
"768" )
DEST="${PROCESS}_hd"
BOOT_DESC=" 1024x768"
ROM="OVMF_CODE.fd"
VARS="OVMF_VARS-1024x768.fd"
;;
"default" )
BOOT_DESC=""
*)
ROM="OVMF_CODE.fd"
VARS="OVMF_VARS.fd"
DEST="${PROCESS}_default"
;;
*)
error "Unknown BOOT_MODE, value \"${BOOT_MODE}\" is not recognized!" && exit 33
DEST="${PROCESS}_${HEIGHT}"
;;
esac

Expand Down Expand Up @@ -58,31 +52,104 @@ fi
BOOT_OPTS+=" -drive if=pflash,format=raw,readonly=on,file=$DEST.rom"
BOOT_OPTS+=" -drive if=pflash,format=raw,file=$DEST.vars"

# OpenCoreBoot
BOOT_DRIVE_ID="OpenCore"
BOOT_DRIVE="$STORAGE/boot.img"
BOOT_VERSION="$STORAGE/boot.version"
BOOT_FILE="/images/OpenCore.img.gz"
BOOT_SIZE=$(stat -c%s "$BOOT_FILE")

CURRENT_SIZE=""
if [ -f "$BOOT_VERSION" ]; then
CURRENT_SIZE=$(<"$BOOT_VERSION")
fi
IMG="$STORAGE/boot.img"

if [ "$CURRENT_SIZE" != "$BOOT_SIZE" ]; then
rm -f "$BOOT_DRIVE" 2>/dev/null || true
fi
if [ ! -f "$IMG" ]; then

FILE="OpenCore.img"
IMG="/tmp/$FILE"
rm -f "$IMG"

if [ ! -f "$BOOT_DRIVE" ] || [ ! -s "$BOOT_DRIVE" ]; then
msg="Extracting boot image"
# OpenCoreBoot
ISO="/opencore.iso"
OUT="/tmp/extract"

rm -rf "$OUT"
mkdir -p "$OUT"

msg="Building boot image"
info "$msg..." && html "$msg..."
gzip -dkc "$BOOT_FILE" > "$BOOT_DRIVE"
echo "$BOOT_SIZE" > "$BOOT_VERSION"

[ ! -f "$ISO" ] && gzip -dk "$ISO.gz"

if [ ! -f "$ISO" ] || [ ! -s "$ISO" ]; then
error "Could not find image file \"$ISO\"." && exit 10
fi

START=$(sfdisk -l "$ISO" | grep -i -m 1 "EFI System" | awk '{print $2}')
mcopy -bspmQ -i "$ISO@@${START}S" ::EFI "$OUT"

CFG="$OUT/EFI/OC/config.plist"
cp /config.plist "$CFG"

ROM="${MAC//[^[:alnum:]]/}"
ROM="${ROM,,}"
BROM=$(echo "$ROM" | xxd -r -p | base64)
RESOLUTION="${WIDTH}x${HEIGHT}@32"

sed -r -i -e 's|<data>m7zhIYfl</data>|<data>'"${BROM}"'</data>|g' "$CFG"
sed -r -i -e 's|<string>iMacPro1,1</string>|<string>'"${MODEL}"'</string>|g' "$CFG"
sed -r -i -e 's|<string>C02TM2ZBHX87</string>|<string>'"${SN}"'</string>|g' "$CFG"
sed -r -i -e 's|<string>C02717306J9JG361M</string>|<string>'"${MLB}"'</string>|g' "$CFG"
sed -r -i -e 's|<string>1920x1080@32</string>|<string>'"${RESOLUTION}"'</string>|g' "$CFG"
sed -r -i -e 's|<string>007076A6-F2A2-4461-BBE5-BAD019F8025A</string>|<string>'"${UUID}"'</string>|g' "$CFG"

# Build image

MB=256
CLUSTER=4
START=2048
SECTOR=512
FIRST_LBA=34

SIZE=$(( MB*1024*1024 ))
OFFSET=$(( START*SECTOR ))
TOTAL=$(( SIZE-(FIRST_LBA*SECTOR) ))
LAST_LBA=$(( TOTAL/SECTOR ))
COUNT=$(( LAST_LBA-(START-1) ))

if ! truncate -s "$SIZE" "$IMG"; then
rm -f "$IMG"
error "Could not allocate space to create image $IMG ." && exit 11
fi

PART="/tmp/partition.fdisk"

{ echo "label: gpt"
echo "label-id: 1ACB1E00-3B8F-4B2A-86A4-D99ED21DCAEB"
echo "device: $FILE"
echo "unit: sectors"
echo "first-lba: $FIRST_LBA"
echo "last-lba: $LAST_LBA"
echo "sector-size: $SECTOR"
echo ""
echo "${FILE}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=$OFFSET" > /etc/mtools.conf

mformat -F -M "$SECTOR" -c "$CLUSTER" -T "$COUNT" -v "EFI" "C:"
mcopy -bspmQ "$OUT/EFI" "C:"

rm -rf "$OUT"

if [[ "$DEBUG" == [Yy1]* ]]; then
info ""
info "Model: $MODEL"
info "Rom: $ROM"
info "Serial: $SN"
info "Board: $MLB"
info ""
fi

fi

BOOT_DRIVE_ID="OpenCore"

DISK_OPTS+=" -device virtio-blk-pci,drive=${BOOT_DRIVE_ID},bus=pcie.0,addr=0x5,bootindex=$BOOT_INDEX"
DISK_OPTS+=" -drive file=$BOOT_DRIVE,id=$BOOT_DRIVE_ID,format=raw,cache=unsafe,readonly=on,if=none"
DISK_OPTS+=" -drive file=$IMG,id=$BOOT_DRIVE_ID,format=raw,cache=unsafe,readonly=on,if=none"

CPU_VENDOR=$(lscpu | awk '/Vendor ID/{print $3}')
DEFAULT_FLAGS="vendor=GenuineIntel,vmware-cpuid-freq=on,-pdpe1gb"
Expand Down
77 changes: 0 additions & 77 deletions src/build.sh

This file was deleted.

Loading

0 comments on commit c6ec08c

Please sign in to comment.