daily pastebin goal
34%
SHARE
TWEET

Untitled

a guest Jul 21st, 2011 396 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/bash
  2.  
  3. # convert archboot.iso to gpt bootable
  4.  
  5. [[ $# -ne 2 ]] && echo "Usage: $0 <archboot-iso-file> <target-file>" && exit
  6.  
  7. ARCHBOOT_ISO=$1
  8. TARGET_FILE=$2
  9.  
  10. ARCHBOOT_DIR=$(mktemp -d)
  11. ESP_DIR=$(mktemp -d)
  12. SYSTEM_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 will use two partitions to store archboot in the target image.
  21. # The first partition is for EFI boot files, it MUST be fat32;
  22. # fat32 does not support soft links, which are used in Arch,
  23. # hence the need for a second partition.
  24. # Also, we only need x86_64, strip out i686 etc.
  25.  
  26. # First partition
  27. cp -r $ARCHBOOT_DIR/efi $ESP_DIR
  28. rm -r $ESP_DIR/efi/grub2/i386-efi
  29. rm $ESP_DIR/efi/boot/bootia32.efi
  30. cat >$ESP_DIR/efi/boot/grub.cfg <<EOF
  31. search --file --no-floppy --set=archboot /arch/archboot.txt
  32. set pager=1
  33. menuentry "Arch Linux MacBookAir3,1" {
  34. linux (\${archboot})/boot/vm64 noefi nomodeset add_efi_memmap reboot=pci none=EFI_ARCH_\${_EFI_ARCH}
  35. initrd (\${archboot})/boot/initrd64.img
  36. }
  37. EOF
  38. # Second partition
  39. cp -r $ARCHBOOT_DIR/{arch,boot,core-any,core-x86_64} $SYSTEM_DIR
  40. rm $SYSTEM_DIR/boot/{initrd64lts.img,initrd.img,initrdlts.img,memtest,splash.png,vm64lts,vmlinuz,vmlts}
  41. rm -r $SYSTEM_DIR/boot/syslinux
  42.  
  43. # Calculate target image size.
  44. # According to http://rodsbooks.com/gdisk/
  45. # we will need 2048*512 at the beginning of the disk,
  46. # 200MiB for the ESP boot partition.
  47. # Duplicate header/partition table stored at end of disk, we assume 2048*512 for symmetry.
  48. # According to http://projects.archlinux.org/archboot.git/tree/usr/bin/archboot-usbimage-helper.sh
  49. # the overhead for ext2 is <6%.  Assuming 4096-byte physical sector size
  50. # we calculate as follows.
  51. SYSTEM_SIZE=$(du -bs ${SYSTEM_DIR}|cut -f1)
  52. SYSTEM_PARTITION_SIZE=$(( ((${SYSTEM_SIZE}*106)/100/4096 + 1)*4096))
  53. IMAGE_SIZE=$(( ${SYSTEM_PARTITION_SIZE}+2*2048*512+200*2**20))
  54.  
  55. echo SYSTEM_SIZE=$SYSTEM_SIZE
  56. echo SYSTEM_PARTITION_SIZE=$SYSTEM_PARTITION_SIZE
  57. echo IMAGE_SIZE=$IMAGE_SIZE
  58.  
  59. echo ========================Creating target file
  60. dd if=/dev/zero of=$TARGET_FILE bs=${IMAGE_SIZE} count=1
  61.  
  62. # Create GPT partition table
  63. # Partition 1 starts at sector 2048, size is 200MiB, type EFI System Partition
  64. # Partition 2 fills the rest of the image
  65. echo ========================Setting up partition table
  66. sgdisk -o -n 1:2048:+200M -t 1:ef00 -n 2:0:0 $TARGET_FILE
  67.  
  68. # calculate extents, needed to create the filesystems
  69. BASE1=$(( 512 * $(sgdisk -i 1 $TARGET_FILE | grep 'First sector:' | cut -f 3 -d ' ') ))
  70. SIZE1=$(( 512 * $(sgdisk -i 1 $TARGET_FILE | grep 'Partition size:' | cut -f 3 -d ' ') ))
  71. BASE2=$(( 512 * $(sgdisk -i 2 $TARGET_FILE | grep 'First sector:' | cut -f 3 -d ' ') ))
  72. SIZE2=$(( 512 * $(sgdisk -i 2 $TARGET_FILE | grep 'Partition size:' | cut -f 3 -d ' ') ))
  73.  
  74. echo ========================PARTITION1
  75. # create fat32 file system on partition 1, copy bootloader files there
  76. LOOP=$(losetup -f --show --offset=$BASE1 --sizelimit=$SIZE1 $TARGET_FILE)
  77. mkfs.vfat -S 512 -F 32 $LOOP
  78. mount $LOOP $TARGET_DIR
  79. cp -r $ESP_DIR/* $TARGET_DIR
  80. umount $TARGET_DIR
  81. losetup -d $LOOP
  82.  
  83. echo ========================PARTITION2
  84. # create ext2 file system on partition 2, copy the rest of the system there
  85. LOOP=$(losetup -f --show --offset=$BASE2 --sizelimit=$SIZE2 $TARGET_FILE)
  86. mkfs.ext2 $LOOP
  87. mount $LOOP $TARGET_DIR
  88. cp -r $SYSTEM_DIR/* $TARGET_DIR
  89. umount $TARGET_DIR
  90. losetup -d $LOOP
  91.  
  92. echo ========================CLEANING UP
  93. # final cleanup
  94. umount $ARCHBOOT_DIR
  95. rmdir $ARCHBOOT_DIR $TARGET_DIR
  96. rm -rf $ESP_DIR $SYSTEM_DIR
  97.  
  98. echo ========================DONE
  99. echo You can overwrite your USB disk with
  100. echo "    dd if=$TARGET_FILE of=<device>"
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top