Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Jul 31st, 2011  |  syntax: Bash  |  size: 3.29 KB  |  views: 389  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #!/bin/bash
  2.  
  3. # convert archboot.iso to gpt bootable for MacBookAir
  4. # works with archboot 2011.07-2
  5.  
  6. [[ $# -ne 2 ]] && echo "Usage: $0 <archboot-iso-file> <target-file>" && exit
  7.  
  8. ARCHBOOT_ISO=$1
  9. TARGET_FILE=$2
  10.  
  11. ARCHBOOT_DIR=$(mktemp -d)
  12. ESP_DIR=$(mktemp -d)
  13. TARGET_DIR=$(mktemp -d)
  14.  
  15. echo ========================Mounting archboot
  16. # Set up archboot for copying
  17. mount $ARCHBOOT_ISO -o loop $ARCHBOOT_DIR
  18.  
  19. echo ========================Preparing archboot files
  20. # We only need x86_64, strip out i686 etc.
  21.  
  22. cp -r $ARCHBOOT_DIR/efi $ESP_DIR
  23. cat >$ESP_DIR/efi/grub2/grub.cfg <<EOF
  24. search --file --no-floppy --set=archboot /arch/archboot.txt
  25. set pager=1
  26. menuentry "Arch Linux MacBookAir3" {
  27. linux (\${archboot})/boot/vm64 nouveau.blacklist=1 noexec=off noefi nomodeset add_efi_memmap reboot=pci elevator=noop none=UEFI_ARCH_\${_UEFI_ARCH}
  28. initrd (\${archboot})/boot/initrd64.img
  29. }
  30. menuentry "Arch Linux MacBookAir4" {
  31. linux (\${archboot})/boot/vm64 i915.modeset=0 noexec=off noefi nomodeset add_efi_memmap reboot=pci elevator=noop none=UEFI_ARCH_\${_UEFI_ARCH}
  32. initrd (\${archboot})/boot/initrd64.img
  33. }
  34. EOF
  35. cp -r $ARCHBOOT_DIR/{arch,boot,packages} $ESP_DIR
  36. # No need for i686, can't EFI boot to that, nor the (old) lts kernel
  37. rm $ESP_DIR/packages/archboot_packages_i686.squashfs
  38. rm $ESP_DIR/boot/{initrd64lts.img,initrd.img,initrdlts.img,memtest,vm64lts,vmlinuz,vmlts}
  39. rm -r $ESP_DIR/boot/syslinux
  40.  
  41. # Calculate target image size.
  42. # According to http://rodsbooks.com/gdisk/
  43. # we will need 2048*512 at the beginning of the disk,
  44. # minimum 200MiB for the ESP boot partition.
  45. # Duplicate header/partition table stored at end of disk, we assume 2048*512 for symmetry.
  46. # For ESP boot partition, assume the overhead of fat32 is <6%, and we want to
  47. # round to a 4096-byte physical sector size for the disk, we calculate as follows.
  48. declare -i ESP_SIZE=$(du -bs ${ESP_DIR}|cut -f1)
  49. declare -i ESP_PARTITION_SIZE=$(( ((${ESP_SIZE}*106)/100/4096 + 1)*4096))
  50. if (( ${ESP_PARTITION_SIZE} < 200*2**20 ))
  51. then
  52.     # Minimum is 200MiB
  53.     ESP_PARTITION_SIZE = 200*2**20
  54. fi
  55. IMAGE_SIZE=$(( ${ESP_PARTITION_SIZE}+2*2048*512))
  56.  
  57. echo ESP_SIZE=$ESP_SIZE
  58. echo ESP_PARTITION_SIZE=$ESP_PARTITION_SIZE
  59. echo IMAGE_SIZE=$IMAGE_SIZE
  60.  
  61. echo ========================Creating target file
  62. dd if=/dev/zero of=$TARGET_FILE bs=${IMAGE_SIZE} count=1
  63.  
  64. # Create GPT partition table
  65. # Only one partition, starts at sector 2048, type EFI System Partition
  66. echo ========================Setting up partition table
  67. sgdisk -o -n 1:2048:0 -t 1:ef00 $TARGET_FILE
  68.  
  69. # calculate extents, needed to create the filesystems
  70. BASE1=$(( 512 * $(sgdisk -i 1 $TARGET_FILE | grep 'First sector:' | cut -f 3 -d ' ') ))
  71. SIZE1=$(( 512 * $(sgdisk -i 1 $TARGET_FILE | grep 'Partition size:' | cut -f 3 -d ' ') ))
  72.  
  73. echo ========================PARTITION1
  74. # create fat32 file system on partition 1, copy files there
  75. LOOP=$(losetup -f --show --offset=$BASE1 --sizelimit=$SIZE1 $TARGET_FILE)
  76. mkfs.vfat -S 512 -F 32 $LOOP
  77. mount $LOOP $TARGET_DIR
  78. cp -r $ESP_DIR/* $TARGET_DIR
  79. umount $TARGET_DIR
  80. losetup -d $LOOP
  81.  
  82. echo ========================CLEANING UP
  83. # final cleanup
  84. umount $ARCHBOOT_DIR
  85. rmdir $ARCHBOOT_DIR $TARGET_DIR
  86. rm -rf $ESP_DIR
  87.  
  88. echo ========================DONE
  89. echo You can overwrite your USB disk with
  90. echo "    dd if=$TARGET_FILE of=<device>"