Guest User

convert archboot iso to efi bootable iso

a guest
Jun 29th, 2012
92
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/bash
  2.  
  3. # Convert the archboot iso to efi bootable iso
  4. # Confirmed working with archlinux-2012.06-1-archboot-x86_64.iso
  5. # Must be run as root
  6. #
  7. # I use it for MacBookAir3,1 which requires an additional kernel option
  8. #    nouveau.blacklist=1
  9. # Either add this by editing /boot/grub/grub_archboot.cfg or just
  10. # edit the options via grub before boot
  11.  
  12.  
  13. [[ $# -ne 2 ]] && echo "Usage: $0 <archboot-iso-file> <target-file>" && exit
  14.  
  15. ARCHBOOT_ISO=$1
  16. TARGET_FILE=$2
  17.  
  18. ARCHBOOT_DIR=$(mktemp --tmpdir=. -d)
  19. ESP_DIR=$(mktemp --tmpdir=. -d)
  20. SYSTEM_DIR=$(mktemp --tmpdir=. -d)
  21. TARGET_DIR=$(mktemp --tmpdir=. -d)
  22.  
  23. echo ========================Mounting archboot
  24. # Set up archboot for copying
  25. mount $ARCHBOOT_ISO -o loop $ARCHBOOT_DIR
  26.  
  27. echo ========================Preparing archboot files
  28. # We will use two partitions to store archboot in the target image.
  29. # The first partition is for EFI boot files, it MUST be fat32;
  30. # fat32 does not support soft links, which I have seen used in archboot,
  31. # so we use a second ext2 partition.
  32.  
  33. # First partition
  34. cp -r $ARCHBOOT_DIR/EFI $ESP_DIR
  35. # Second partition
  36. cp -r $ARCHBOOT_DIR/{arch,boot,packages} $SYSTEM_DIR
  37. cp -r $ARCHBOOT_DIR/EFI $SYSTEM_DIR # in case you want the efi shell
  38.  
  39. # Calculate target image size.
  40. # According to http://rodsbooks.com/gdisk/
  41. # we will need 2048*512 at the beginning of the disk,
  42. # 200MiB for the ESP boot partition.
  43. # Duplicate header/partition table stored at end of disk, we assume 2048*512 for symmetry.
  44. # According to http://projects.archlinux.org/archboot.git/tree/usr/bin/archboot-usbimage-helper.sh
  45. # the overhead for ext2 is <6%.  Assuming 4096-byte physical sector size
  46. # we calculate as follows.
  47. SYSTEM_SIZE=$(du -bs ${SYSTEM_DIR}|cut -f1)
  48. SYSTEM_PARTITION_SIZE=$(( ((${SYSTEM_SIZE}*106)/100/4096 + 1)*4096))
  49. IMAGE_SIZE=$(( ${SYSTEM_PARTITION_SIZE}+2*2048*512+200*2**20))
  50. IMAGE_SECTORS=$(( ${IMAGE_SIZE}/512 ))
  51.  
  52. echo SYSTEM_SIZE=$SYSTEM_SIZE
  53. echo SYSTEM_PARTITION_SIZE=$SYSTEM_PARTITION_SIZE
  54. echo IMAGE_SIZE=$IMAGE_SIZE
  55. echo IMAGE_SECTORS=$IMAGE_SECTORS
  56.  
  57. echo ========================Creating target file
  58. dd if=/dev/zero of=$TARGET_FILE bs=512 count=${IMAGE_SECTORS}
  59.  
  60. # Create GPT partition table
  61. # Partition 1 starts at sector 2048, size is 200MiB, type EFI System Partition
  62. # Partition 2 fills the rest of the image
  63. echo ========================Setting up partition table
  64. sgdisk -o -n 1:2048:+200M -t 1:ef00 -n 2:0:0 $TARGET_FILE
  65.  
  66. # calculate extents, needed to create the filesystems
  67. BASE1=$(( 512 * $(sgdisk -i 1 $TARGET_FILE | grep 'First sector:' | cut -f 3 -d ' ') ))
  68. SIZE1=$(( 512 * $(sgdisk -i 1 $TARGET_FILE | grep 'Partition size:' | cut -f 3 -d ' ') ))
  69. BASE2=$(( 512 * $(sgdisk -i 2 $TARGET_FILE | grep 'First sector:' | cut -f 3 -d ' ') ))
  70. SIZE2=$(( 512 * $(sgdisk -i 2 $TARGET_FILE | grep 'Partition size:' | cut -f 3 -d ' ') ))
  71.  
  72. echo ========================PARTITION1
  73. # create fat32 file system on partition 1, copy bootloader files there
  74. LOOP=$(losetup -f --show --offset=$BASE1 --sizelimit=$SIZE1 $TARGET_FILE)
  75. mkfs.vfat -S 512 -F 32 $LOOP
  76. mount $LOOP $TARGET_DIR
  77. cp -r $ESP_DIR/* $TARGET_DIR
  78. umount $TARGET_DIR
  79. losetup -d $LOOP
  80.  
  81. echo ========================PARTITION2
  82. # create ext2 file system on partition 2, copy the rest of the system there
  83. LOOP=$(losetup -f --show --offset=$BASE2 --sizelimit=$SIZE2 $TARGET_FILE)
  84. mkfs.ext2 $LOOP
  85. mount $LOOP $TARGET_DIR
  86. cp -r $SYSTEM_DIR/* $TARGET_DIR
  87. umount $TARGET_DIR
  88. losetup -d $LOOP
  89.  
  90. echo ========================CLEANING UP
  91. # final cleanup
  92. umount $ARCHBOOT_DIR
  93. rmdir $ARCHBOOT_DIR $TARGET_DIR
  94. rm -rf $ESP_DIR $SYSTEM_DIR
  95.  
  96. echo ========================DONE
  97. echo You can overwrite your USB disk with
  98. echo "    dd if=$TARGET_FILE of=<device>"
RAW Paste Data