#!/bin/bash
# convert archboot.iso to gpt bootable for MacBookAir
# works with archboot 2011.07-2
[[ $# -ne 2 ]] && echo "Usage: $0 <archboot-iso-file> <target-file>" && exit
ARCHBOOT_ISO=$1
TARGET_FILE=$2
ARCHBOOT_DIR=$(mktemp -d)
ESP_DIR=$(mktemp -d)
TARGET_DIR=$(mktemp -d)
echo ========================Mounting archboot
# Set up archboot for copying
mount $ARCHBOOT_ISO -o loop $ARCHBOOT_DIR
echo ========================Preparing archboot files
# We only need x86_64, strip out i686 etc.
cp -r $ARCHBOOT_DIR/efi $ESP_DIR
cat >$ESP_DIR/efi/grub2/grub.cfg <<EOF
search --file --no-floppy --set=archboot /arch/archboot.txt
set pager=1
menuentry "Arch Linux MacBookAir3" {
linux (\${archboot})/boot/vm64 nouveau.blacklist=1 noexec=off noefi nomodeset add_efi_memmap reboot=pci elevator=noop none=UEFI_ARCH_\${_UEFI_ARCH}
initrd (\${archboot})/boot/initrd64.img
}
menuentry "Arch Linux MacBookAir4" {
linux (\${archboot})/boot/vm64 i915.modeset=0 noexec=off noefi nomodeset add_efi_memmap reboot=pci elevator=noop none=UEFI_ARCH_\${_UEFI_ARCH}
initrd (\${archboot})/boot/initrd64.img
}
EOF
cp -r $ARCHBOOT_DIR/{arch,boot,packages} $ESP_DIR
# No need for i686, can't EFI boot to that, nor the (old) lts kernel
rm $ESP_DIR/packages/archboot_packages_i686.squashfs
rm $ESP_DIR/boot/{initrd64lts.img,initrd.img,initrdlts.img,memtest,vm64lts,vmlinuz,vmlts}
rm -r $ESP_DIR/boot/syslinux
# Calculate target image size.
# According to http://rodsbooks.com/gdisk/
# we will need 2048*512 at the beginning of the disk,
# minimum 200MiB for the ESP boot partition.
# Duplicate header/partition table stored at end of disk, we assume 2048*512 for symmetry.
# For ESP boot partition, assume the overhead of fat32 is <6%, and we want to
# round to a 4096-byte physical sector size for the disk, we calculate as follows.
declare -i ESP_SIZE=$(du -bs ${ESP_DIR}|cut -f1)
declare -i ESP_PARTITION_SIZE=$(( ((${ESP_SIZE}*106)/100/4096 + 1)*4096))
if (( ${ESP_PARTITION_SIZE} < 200*2**20 ))
then
# Minimum is 200MiB
ESP_PARTITION_SIZE = 200*2**20
fi
IMAGE_SIZE=$(( ${ESP_PARTITION_SIZE}+2*2048*512))
echo ESP_SIZE=$ESP_SIZE
echo ESP_PARTITION_SIZE=$ESP_PARTITION_SIZE
echo IMAGE_SIZE=$IMAGE_SIZE
echo ========================Creating target file
dd if=/dev/zero of=$TARGET_FILE bs=${IMAGE_SIZE} count=1
# Create GPT partition table
# Only one partition, starts at sector 2048, type EFI System Partition
echo ========================Setting up partition table
sgdisk -o -n 1:2048:0 -t 1:ef00 $TARGET_FILE
# calculate extents, needed to create the filesystems
BASE1=$(( 512 * $(sgdisk -i 1 $TARGET_FILE | grep 'First sector:' | cut -f 3 -d ' ') ))
SIZE1=$(( 512 * $(sgdisk -i 1 $TARGET_FILE | grep 'Partition size:' | cut -f 3 -d ' ') ))
echo ========================PARTITION1
# create fat32 file system on partition 1, copy files there
LOOP=$(losetup -f --show --offset=$BASE1 --sizelimit=$SIZE1 $TARGET_FILE)
mkfs.vfat -S 512 -F 32 $LOOP
mount $LOOP $TARGET_DIR
cp -r $ESP_DIR/* $TARGET_DIR
umount $TARGET_DIR
losetup -d $LOOP
echo ========================CLEANING UP
# final cleanup
umount $ARCHBOOT_DIR
rmdir $ARCHBOOT_DIR $TARGET_DIR
rm -rf $ESP_DIR
echo ========================DONE
echo You can overwrite your USB disk with
echo " dd if=$TARGET_FILE of=<device>"