- Pure-Go HFS+ offline helper disk (REIMSHELP) bypassing Recovery's missing pkgutil & read-only installer target, with automatic blank-disk APFS formatting and non-blank safety confirmation - Recovery mode UX: rename Finish install to Exit recovery mode, add Enter recovery mode via OpenCore boot picker (--boot-selection), show Attach offline image on VM page with ~1 GB Apple online recovery fallback download - Pre-apply WHPX kernel-irqchip=off when nested-virt probe fails and filter benign TSC deadline timer warnings - QMP keyboard typing (type clipboard / one-click Recovery command) and offline-install network toggle
406 lines
13 KiB
Go
406 lines
13 KiB
Go
package installer
|
|
|
|
import (
|
|
"archive/tar"
|
|
"bytes"
|
|
"compress/zlib"
|
|
"encoding/binary"
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/ulikunitz/xz"
|
|
)
|
|
|
|
func buildSyntheticCPIO(t *testing.T) []byte {
|
|
t.Helper()
|
|
var buf bytes.Buffer
|
|
writeEntry := func(name string, mode int, data []byte) {
|
|
hdr := fmt.Sprintf("070707%06o%06o%06o%06o%06o%06o%06o%011o%06o%011o",
|
|
0, 1, mode, 0, 0, 1, 0, 1700000000, len(name)+1, len(data))
|
|
buf.WriteString(hdr)
|
|
buf.WriteString(name)
|
|
buf.WriteByte(0)
|
|
buf.Write(data)
|
|
}
|
|
writeEntry("./Applications", 0o040755, nil)
|
|
writeEntry("./Applications/Install macOS Tahoe.app", 0o040755, nil)
|
|
writeEntry("./Applications/Install macOS Tahoe.app/Contents/Resources/startosinstall", 0o100755, []byte("#!/bin/sh\necho ok\n"))
|
|
writeEntry("./Applications/Install macOS Tahoe.app/Contents/Latest", 0o120777, []byte("Resources/startosinstall"))
|
|
writeEntry("TRAILER!!!", 0, nil)
|
|
return buf.Bytes()
|
|
}
|
|
|
|
func buildSyntheticPBZX(t *testing.T, raw []byte) []byte {
|
|
t.Helper()
|
|
var xzBuf bytes.Buffer
|
|
xw, err := xz.NewWriter(&xzBuf)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := xw.Write(raw); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := xw.Close(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
comp := xzBuf.Bytes()
|
|
|
|
var out bytes.Buffer
|
|
out.WriteString("pbzx")
|
|
_ = binary.Write(&out, binary.BigEndian, uint64(0x1000000)) // flags
|
|
_ = binary.Write(&out, binary.BigEndian, uint64(len(raw)))
|
|
_ = binary.Write(&out, binary.BigEndian, uint64(len(comp)))
|
|
out.Write(comp)
|
|
return out.Bytes()
|
|
}
|
|
|
|
func buildSyntheticXAR(t *testing.T, payload []byte) []byte {
|
|
t.Helper()
|
|
tocXML := fmt.Sprintf(`<?xml version="1.0" encoding="UTF-8"?>
|
|
<xar><toc><file id="1"><name>Payload</name><type>file</type><data><offset>0</offset><length>%d</length><size>%d</size></data></file></toc></xar>`,
|
|
len(payload), len(payload))
|
|
var tocComp bytes.Buffer
|
|
zw := zlib.NewWriter(&tocComp)
|
|
_, _ = zw.Write([]byte(tocXML))
|
|
_ = zw.Close()
|
|
|
|
var out bytes.Buffer
|
|
var hdr [28]byte
|
|
copy(hdr[0:4], "xar!")
|
|
binary.BigEndian.PutUint16(hdr[4:6], 28)
|
|
binary.BigEndian.PutUint16(hdr[6:8], 1)
|
|
binary.BigEndian.PutUint64(hdr[8:16], uint64(tocComp.Len()))
|
|
binary.BigEndian.PutUint64(hdr[16:24], uint64(len(tocXML)))
|
|
binary.BigEndian.PutUint32(hdr[24:28], 1)
|
|
out.Write(hdr[:])
|
|
out.Write(tocComp.Bytes())
|
|
out.Write(payload)
|
|
return out.Bytes()
|
|
}
|
|
|
|
func TestEnsureOfflineHelperImageRoundTrip(t *testing.T) {
|
|
cpio := buildSyntheticCPIO(t)
|
|
pbzx := buildSyntheticPBZX(t, cpio)
|
|
pkg := buildSyntheticXAR(t, pbzx)
|
|
|
|
tmp := t.TempDir()
|
|
offlineImg := filepath.Join(tmp, "macOS-Tahoe-26.7.3-25G342-offline-installer.img")
|
|
err := BuildHFSPlusDiskImage(offlineImg, "Installer", []HFSFile{
|
|
{
|
|
Name: "InstallAssistant.pkg",
|
|
Mode: 0o100644,
|
|
Size: int64(len(pkg)),
|
|
Open: func() (io.ReadCloser, error) { return io.NopCloser(bytes.NewReader(pkg)), nil },
|
|
},
|
|
{
|
|
Name: "install-macos.sh",
|
|
Mode: 0o100755,
|
|
Size: int64(len("#!/bin/sh\n")),
|
|
Open: func() (io.ReadCloser, error) { return io.NopCloser(strings.NewReader("#!/bin/sh\n")), nil },
|
|
},
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("BuildHFSPlusDiskImage(offline): %v", err)
|
|
}
|
|
|
|
cacheDir := filepath.Join(tmp, "cache")
|
|
helperPath, err := EnsureOfflineHelperImage(cacheDir, offlineImg)
|
|
if err != nil {
|
|
t.Fatalf("EnsureOfflineHelperImage: %v", err)
|
|
}
|
|
|
|
// Second call must hit cache cleanly.
|
|
helperPath2, err := EnsureOfflineHelperImage(cacheDir, offlineImg)
|
|
if err != nil || helperPath2 != helperPath {
|
|
t.Fatalf("cache reuse mismatch: %q vs %q (err=%v)", helperPath2, helperPath, err)
|
|
}
|
|
|
|
// Verify both install.sh and InstallApp.tar inside generated REIMSHELP HFS+ image.
|
|
hf, err := os.Open(helperPath)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer hf.Close()
|
|
|
|
shOff, shSize, err := FindFileInHFSPlusImage(hf, "install.sh")
|
|
if err != nil || shSize == 0 {
|
|
t.Fatalf("install.sh not found in helper image: err=%v size=%d", err, shSize)
|
|
}
|
|
shBytes := make([]byte, shSize)
|
|
if _, err := hf.ReadAt(shBytes, shOff); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
shText := string(shBytes)
|
|
for _, want := range []string{
|
|
"tar -xf /Volumes/REIMSHELP/InstallApp.tar",
|
|
"can_write()",
|
|
"is_reserved_vol()",
|
|
"diskutil list physical",
|
|
`diskutil eraseDisk APFS "Macintosh HD" GPT`,
|
|
"Refusing to erase a non-blank disk non-interactively.",
|
|
"--erase",
|
|
} {
|
|
if !strings.Contains(shText, want) {
|
|
t.Fatalf("install.sh missing expected fragment %q", want)
|
|
}
|
|
}
|
|
|
|
tarOff, tarSize, err := FindFileInHFSPlusImage(hf, "InstallApp.tar")
|
|
if err != nil || tarSize == 0 {
|
|
t.Fatalf("InstallApp.tar not found in helper image: err=%v size=%d", err, tarSize)
|
|
}
|
|
tr := tar.NewReader(io.NewSectionReader(hf, tarOff, tarSize))
|
|
foundBin := false
|
|
foundSym := false
|
|
for {
|
|
hdr, err := tr.Next()
|
|
if err == io.EOF {
|
|
break
|
|
}
|
|
if err != nil {
|
|
t.Fatalf("read tar entry: %v", err)
|
|
}
|
|
if hdr.Name == "Applications/Install macOS Tahoe.app/Contents/Resources/startosinstall" {
|
|
foundBin = true
|
|
if hdr.Mode&0o111 == 0 {
|
|
t.Errorf("startosinstall lost execute bit: mode=%o", hdr.Mode)
|
|
}
|
|
}
|
|
if hdr.Name == "Applications/Install macOS Tahoe.app/Contents/Latest" {
|
|
foundSym = true
|
|
if hdr.Typeflag != tar.TypeSymlink || hdr.Linkname != "Resources/startosinstall" {
|
|
t.Errorf("symlink mismatch: type=%v link=%q", hdr.Typeflag, hdr.Linkname)
|
|
}
|
|
}
|
|
}
|
|
if !foundBin || !foundSym {
|
|
t.Fatalf("expected both binary and symlink in InstallApp.tar (bin=%v sym=%v)", foundBin, foundSym)
|
|
}
|
|
}
|
|
|
|
func findPOSIXSh() string {
|
|
if p, err := exec.LookPath("sh"); err == nil {
|
|
return p
|
|
}
|
|
for _, cand := range []string{
|
|
`C:\Program Files\Git\bin\sh.exe`,
|
|
`C:\Program Files\Git\usr\bin\sh.exe`,
|
|
`D:\DevTools\msys64\usr\bin\sh.exe`,
|
|
} {
|
|
if _, err := os.Stat(cand); err == nil {
|
|
return cand
|
|
}
|
|
}
|
|
return ""
|
|
}
|
|
|
|
func TestHelperInstallScriptSyntaxAndDiskInspection(t *testing.T) {
|
|
shBin := findPOSIXSh()
|
|
if shBin == "" {
|
|
t.Skip("POSIX sh not available")
|
|
}
|
|
tmp := t.TempDir()
|
|
scriptPath := filepath.Join(tmp, "install.sh")
|
|
script := string(HelperInstallScript())
|
|
if err := os.WriteFile(scriptPath, []byte(script), 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
// 1. POSIX sh syntax check.
|
|
if out, err := exec.Command(shBin, "-n", scriptPath).CombinedOutput(); err != nil {
|
|
t.Fatalf("sh -n install.sh failed: %v\n%s", err, out)
|
|
}
|
|
|
|
// 2. Behavioral test of the disk-inspection helpers against realistic macOS
|
|
// Recovery diskutil output (blank VM disk + Installer + REIMSHELP +
|
|
// OPENCORE + Disk Image + non-blank partitioned disk).
|
|
cut := strings.Index(script, "\nerase_mode=0")
|
|
if cut < 0 {
|
|
t.Fatal("could not locate erase_mode=0 boundary in install.sh")
|
|
}
|
|
harness := script[:cut] + `
|
|
diskutil() {
|
|
case "$1 $2" in
|
|
"list physical")
|
|
cat <<'EOF'
|
|
/dev/disk0 (internal, physical):
|
|
#: TYPE NAME SIZE IDENTIFIER
|
|
0: *137.4 GB disk0
|
|
|
|
/dev/disk1 (internal, physical):
|
|
#: TYPE NAME SIZE IDENTIFIER
|
|
0: GUID_partition_scheme *19.8 GB disk1
|
|
1: Apple_HFS Installer 19.8 GB disk1s1
|
|
|
|
/dev/disk2 (external, physical):
|
|
#: TYPE NAME SIZE IDENTIFIER
|
|
0: GUID_partition_scheme *134.2 MB disk2
|
|
1: Apple_HFS REIMSHELP 134.2 MB disk2s1
|
|
|
|
/dev/disk3 (internal, physical):
|
|
#: TYPE NAME SIZE IDENTIFIER
|
|
0: GUID_partition_scheme *134.2 MB disk3
|
|
1: EFI OPENCORE 134.2 MB disk3s1
|
|
|
|
/dev/disk4 (disk image):
|
|
#: TYPE NAME SIZE IDENTIFIER
|
|
0: GUID_partition_scheme +2.1 GB disk4
|
|
|
|
/dev/disk5 (internal, physical):
|
|
#: TYPE NAME SIZE IDENTIFIER
|
|
0: GUID_partition_scheme *137.4 GB disk5
|
|
1: EFI EFI 209.7 MB disk5s1
|
|
2: Apple_APFS Container disk6 137.2 GB disk5s2
|
|
|
|
/dev/disk7 (internal, physical):
|
|
#: TYPE NAME SIZE IDENTIFIER
|
|
0: GUID_partition_scheme *64.0 GB disk7
|
|
1: Apple_HFS Installer 64.0 GB disk7s1
|
|
EOF
|
|
;;
|
|
"info /dev/disk0")
|
|
cat <<'EOF'
|
|
Device Identifier: disk0
|
|
Whole: Yes
|
|
Device / Media Name: QEMU HARDDISK
|
|
Virtual: No
|
|
Protocol: SATA
|
|
Read-Only Media: No
|
|
Content (IOContent): None
|
|
Disk Size: 137.4 GB (137438953472 Bytes) (exactly 268435456 512-Byte-Units)
|
|
EOF
|
|
;;
|
|
"list /dev/disk0")
|
|
cat <<'EOF'
|
|
/dev/disk0 (internal, physical):
|
|
#: TYPE NAME SIZE IDENTIFIER
|
|
0: *137.4 GB disk0
|
|
EOF
|
|
;;
|
|
"info /dev/disk1")
|
|
cat <<'EOF'
|
|
Whole: Yes
|
|
Virtual: No
|
|
Protocol: SATA
|
|
Read-Only Media: No
|
|
Content (IOContent): GUID_partition_scheme
|
|
Disk Size: 19.8 GB (19800000000 Bytes)
|
|
EOF
|
|
;;
|
|
"list /dev/disk1")
|
|
cat <<'EOF'
|
|
0: GUID_partition_scheme *19.8 GB disk1
|
|
1: Apple_HFS Installer 19.8 GB disk1s1
|
|
EOF
|
|
;;
|
|
"info /dev/disk2")
|
|
cat <<'EOF'
|
|
Whole: Yes
|
|
Virtual: No
|
|
Protocol: USB
|
|
Read-Only Media: Yes
|
|
Content (IOContent): GUID_partition_scheme
|
|
Disk Size: 134.2 MB (134217728 Bytes)
|
|
EOF
|
|
;;
|
|
"list /dev/disk2")
|
|
cat <<'EOF'
|
|
0: GUID_partition_scheme *134.2 MB disk2
|
|
1: Apple_HFS REIMSHELP 134.2 MB disk2s1
|
|
EOF
|
|
;;
|
|
"info /dev/disk3")
|
|
cat <<'EOF'
|
|
Whole: Yes
|
|
Virtual: No
|
|
Protocol: SATA
|
|
Read-Only Media: No
|
|
Content (IOContent): GUID_partition_scheme
|
|
Disk Size: 134.2 MB (134217728 Bytes)
|
|
EOF
|
|
;;
|
|
"list /dev/disk3")
|
|
cat <<'EOF'
|
|
0: GUID_partition_scheme *134.2 MB disk3
|
|
1: EFI OPENCORE 134.2 MB disk3s1
|
|
EOF
|
|
;;
|
|
"info /dev/disk5")
|
|
cat <<'EOF'
|
|
Whole: Yes
|
|
Device / Media Name: QEMU HARDDISK
|
|
Virtual: No
|
|
Protocol: SATA
|
|
Read-Only Media: No
|
|
Content (IOContent): GUID_partition_scheme
|
|
Disk Size: 137.4 GB (137438953472 Bytes)
|
|
EOF
|
|
;;
|
|
"list /dev/disk5")
|
|
cat <<'EOF'
|
|
0: GUID_partition_scheme *137.4 GB disk5
|
|
1: EFI EFI 209.7 MB disk5s1
|
|
2: Apple_APFS Container disk6 137.2 GB disk5s2
|
|
EOF
|
|
;;
|
|
"info /dev/disk7")
|
|
cat <<'EOF'
|
|
Whole: Yes
|
|
Virtual: No
|
|
Protocol: SATA
|
|
Read-Only Media: No
|
|
Content (IOContent): GUID_partition_scheme
|
|
Disk Size: 64.0 GB (64000000000 Bytes)
|
|
EOF
|
|
;;
|
|
"list /dev/disk7")
|
|
cat <<'EOF'
|
|
0: GUID_partition_scheme *64.0 GB disk7
|
|
1: Apple_HFS Installer 64.0 GB disk7s1
|
|
EOF
|
|
;;
|
|
*) return 1 ;;
|
|
esac
|
|
}
|
|
|
|
echo "MB1=$(bytes_to_mb 137438953472)"
|
|
echo "MB2=$(bytes_to_mb 500000)"
|
|
echo "MB3=$(bytes_to_mb invalid)"
|
|
for d in $(list_physical_disks); do
|
|
inspect_candidate_disk "$d"
|
|
echo "DISK=$d ok=$cand_ok blank=$cand_blank size=$cand_size_human reason=$cand_reason"
|
|
done
|
|
`
|
|
harnessPath := filepath.Join(tmp, "harness.sh")
|
|
if err := os.WriteFile(harnessPath, []byte(harness), 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
outBytes, err := exec.Command(shBin, harnessPath).CombinedOutput()
|
|
if err != nil {
|
|
t.Fatalf("harness failed: %v\n%s", err, outBytes)
|
|
}
|
|
out := string(outBytes)
|
|
for _, want := range []string{
|
|
"MB1=137438",
|
|
"MB2=0",
|
|
"MB3=0",
|
|
"DISK=/dev/disk0 ok=1 blank=1 size=137.4 GB reason=",
|
|
"DISK=/dev/disk1 ok=0 blank=0 size=19.8 GB reason=too small",
|
|
"DISK=/dev/disk2 ok=0 blank=0 size= reason=read-only media",
|
|
"DISK=/dev/disk3 ok=0 blank=0 size=134.2 MB reason=too small",
|
|
"DISK=/dev/disk5 ok=1 blank=0 size=137.4 GB reason=",
|
|
"DISK=/dev/disk7 ok=0 blank=0 size=64.0 GB reason=contains reserved/installer volume",
|
|
} {
|
|
if !strings.Contains(out, want) {
|
|
t.Errorf("missing expected output line %q\nfull output:\n%s", want, out)
|
|
}
|
|
}
|
|
if strings.Contains(out, "DISK=/dev/disk4") {
|
|
t.Errorf("disk image /dev/disk4 should have been filtered out by list_physical_disks\nfull output:\n%s", out)
|
|
}
|
|
}
|