Haiku UEFI USB Installer Creation Scripts

Create a UEFI bootable Haiku USB installer using Windows and Haiku.
Automate Your Life

PowerShell - Stage I


$WorkingDriveLetter = "H:"
$EFISize   =  100mb
$HaikuSize = 8192mb

sl $PSScriptRoot
$ErrorActionPreference = "Stop"

$SelectedDrive = gwmi win32_diskdrive | ?{$_.interfacetype -eq "USB"} |
    Select DeviceID, Status,Partitions, Index, InterfaceType,Size, caption, model,name |
    Out-GridView -Title "Please Select the USB to erase for the Haiku UEFI USB (16Gb or greater)" -PassThru

Write-Host ("=" * 80)

if($SelectedDrive){
Write-Host "Using $($SelectedDrive.deviceID + " " + $SelectedDrive.Model)"

    try{
        Write-Host "1/7 Clear everything from the disk"
        $bob = Clear-Disk -number $SelectedDrive.index -RemoveData -ErrorAction Stop -PassThru
        if($bob -eq $Null){Write-Warning "Cancelled at the last second, phew!" -ErrorAction Stop ; exit}

        # Check if the last command was successful
        if ($?) {Write-Host "Stage 1 completed successfully." -ForegroundColor Green} 
    }
    catch{
        Write-host "Houston, we have a problem with stage 1." -ForegroundColor Red
        write-host $Error[0].InvocationInfo.PositionMessage
        Exit
        }
    $error.Clear()
    
    Write-Host "2/7 Make $($SelectedDrive.index) GPT patition style"
    try{
        Set-Disk -Number $SelectedDrive.index -PartitionStyle GPT 

        # Check if the last command was successful
        if ($?) {Write-Host "Stage 2 completed successfully." -ForegroundColor Green} 

    }
    catch{
        Write-Warning -Message $?
        Write-host "Houston, we have a problem with stage 2." -ForegroundColor Red
        write-host $Error[0].InvocationInfo.PositionMessage
        $Error | fl
        Exit
        }

    try{
        Write-Host "3/7 Create 100Mb partition - EFI System Data"
        New-Partition -DiskNumber $SelectedDrive.index -Size $EFISize `
                      -GptType "{c12a7328-f81f-11d2-ba4b-00a0c93ec93b}" |
            Format-Volume -filesystem FAT32 -newfilesystemlabel "EFIBOOT" | Out-Null
        }
    catch{
        Write-host "Houston, we have a problem with stage 3." -ForegroundColor Red
        write-host $Error[0].InvocationInfo.PositionMessage
        Exit
        }

    try{
        Write-Host "4/7 Mount the Drive as $WorkingDriveLetter to copy EFI files into place"
        get-partition -disknumber $SelectedDrive.index -PartitionNumber 1 | 
            set-partition -newdriveletter $WorkingDriveLetter.Substring(0,1) 
        }
    catch{
        Write-host "Houston, we have a problem with stage 4." -ForegroundColor Red
        write-host $Error[0].InvocationInfo.PositionMessage
        Exit
        }

    Try{
        Write-Host "5/7 Need to copy Haiku EFI files into place"
        write-host `t$($WorkingDriveLetter + "\EFI\BOOT\BOOTX64.EFI")
        New-Item -ItemType Directory -Force -Path $($WorkingDriveLetter + "\EFI\BOOT\") | Out-Null
        Copy-Item -Path "$PSScriptRoot\haiku_loader.efi" `
                  -Destination $($WorkingDriveLetter + "\EFI\BOOT\BOOTX64.EFI") | Out-Null

        write-host `t$($WorkingDriveLetter + "\Haiku UEFI USB Prep Stage II (Haiku App)")
        Copy-Item -Path "$PSScriptRoot\Haiku UEFI USB Prep Stage II" `
                  -Destination $($WorkingDriveLetter + "\") | Out-Null

        Test-Path $($WorkingDriveLetter + "\EFI\BOOT\BOOTX64.EFI")
        }
    catch{
        Write-host "Houston, we have a problem with stage 5." -ForegroundColor Red
        write-host $Error[0].InvocationInfo.PositionMessage
        Exit
        }

    Try{
        Write-Host "6/7 Create 8Gb (Haiku GPT) Partition for Haiku (temporary HAIKU-DEST label)"

        # Create a normal Windows data partition temporarily
        $Bob = New-Partition -DiskNumber $SelectedDrive.index `
                                -Size $HaikuSize `
                                -AssignDriveLetter

        # Give it a temporary filesystem label so we can identify it later
        Format-Volume -Partition $Bob `
                        -FileSystem FAT32 `
                        -NewFileSystemLabel "HAIKU-DEST" | Out-Null
               
        Start-Sleep -Seconds 2

        # Change the GPT partition type to Haiku BFS GPT
        Set-Partition -DiskNumber $SelectedDrive.index `
                        -PartitionNumber $Bob.PartitionNumber `
                        -GptType "{42465331-3ba3-10f1-802a-4861696b7521}"
    }
    catch{
        Write-host "Houston, we have a problem with stage 6." -ForegroundColor Red
        write-host $Error[0].InvocationInfo.PositionMessage
        Exit
    }

    Write-Host ("=" * 80)
    Write-Host "7/7 Need to complete on a Haiku computer" -ForegroundColor Cyan
    Write-Host "`tA. Run DriveSetup"
    Write-Host "`tB. Select the USB 0/0/0/1 and in DriveSetup > Partition > Format"
    Write-Host "`tC. as a Be File System... and a name like [ Haiku USB UEFI System ]"
    write-host "A,B & C can be done by running the included " -ForegroundColor Green -NoNewline
    write-host "Haiku UEFI USB Prep Stage II" -ForegroundColor Yellow  -NoNewline
    write-host "(Haiku App)" -ForegroundColor green
    Write-Host "`t`t Feel free to then select the remaining Empty Space and create"
    Write-Host "`t`t  and then format another Be File System and name it [ Haiku USB Data ]"
    Write-Host "`tD. Then run Haiku Installer from a Mounted ISO to the 8Gb Partition"
    Write-Host ("=" * 80)
    Write-host "Long term goal - to rewrite this script in Haiku" -ForegroundColor yellow

} else {Write-Warning "No USB drive Found/Selected - exiting"}

If (-not $psISE) {write-host "Press any key to close window..."; [void][System.Console]::ReadKey($true)}

		

Haiku Stage II (Bash)


#!/bin/bash
clear

#relaunch in Terminal, if double clicked in the Traker
if [ "$1" != "--terminal" ]; then
	#alert --idea "You need to run this from the terminal"
    Terminal -t "Haiku EFI USB Maker II" /bin/bash "$0" --terminal 
    exit 0
fi

#Show a list of drives including the USB drives
mountvolume -l 

#Look for oour special haiku-destination partition
DEVICE=$(mountvolume -l | grep -i "haiku-dest" | sed 's/.*(\(.*\))/\1/')
echo "------------------------------------------------------------------------------"

if [ -z "$DEVICE" ]; then
    echo "Temporary HAIKU-DEST volume not found - Process Abandoned!"
    read
    exit 1
fi

echo "Found HAIKU-DEST on device: $DEVICE"
if mount | grep -i -q "/haiku-dest"; then
    echo "Drive is mounted. Unmounting now..."
    unmount -f "/haiku-dest"
fi

echo "We need to initialize with BFS"
if mkfs -t bfs /dev/disk/$DEVICE "Haiku USB Installer"; then
    echo "------------------------------------------------------------------------------"
    echo "Filesystem created successfully. Volume name: HAIKU USB INSTALLER"
else
    echo "------------------------------------------------------------------------------"
    echo "Filesystem creation failed. - Process Abandoned!"
    Read
    exit 1
fi

echo "------------------------------------------------------------------------------"
echo "When using the Haiku Installer, set the destination option..."
echo "                  Onto: [ HAIKU USB INSTALLER     ]"
echo "------------------------------------------------------------------------------"
echo "(about to launch: Haiku Installer...)"
sleep 4

if mount | grep -i -q "/Haiku USB Installer"; then
    echo "Drive is mounted. Unmounting now..."
    unmount -f "/Haiku USB Installer"
fi

/system/apps/Installer &
echo "Installer exited with a status of $?"
read