mkefidisk.sh 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. #!/bin/sh
  2. #
  3. # Copyright (c) 2012, Intel Corporation.
  4. # All rights reserved.
  5. #
  6. # This program is free software; you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation; either version 2 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
  14. # the GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with this program; if not, write to the Free Software
  18. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. #
  20. #
  21. # Defaults
  22. #
  23. # 20 Mb for the boot partition
  24. BOOT_SIZE=20
  25. # 5% for swap
  26. SWAP_RATIO=5
  27. function usage() {
  28. echo "Usage: $(basename $0) DEVICE HDDIMG TARGET_DEVICE"
  29. echo " DEVICE: The device to write the image to, e.g. /dev/sdh"
  30. echo " HDDIMG: The hddimg file to generate the efi disk from"
  31. echo " TARGET_DEVICE: The device the target will boot from, e.g. /dev/mmcblk0"
  32. }
  33. function image_details() {
  34. IMG=$1
  35. echo "Image details"
  36. echo "============="
  37. echo " image: $(stat --printf '%N\n' $IMG)"
  38. echo " size: $(stat -L --printf '%s bytes\n' $IMG)"
  39. echo " modified: $(stat -L --printf '%y\n' $IMG)"
  40. echo " type: $(file -L -b $IMG)"
  41. echo ""
  42. }
  43. function device_details() {
  44. DEV=$1
  45. BLOCK_SIZE=512
  46. echo "Device details"
  47. echo "=============="
  48. echo " device: $DEVICE"
  49. if [ -f "/sys/class/block/$DEV/device/vendor" ]; then
  50. echo " vendor: $(cat /sys/class/block/$DEV/device/vendor)"
  51. else
  52. echo " vendor: UNKOWN"
  53. fi
  54. if [ -f "/sys/class/block/$DEV/device/model" ]; then
  55. echo " model: $(cat /sys/class/block/$DEV/device/model)"
  56. else
  57. echo " model: UNKNOWN"
  58. fi
  59. if [ -f "/sys/class/block/$DEV/size" ]; then
  60. echo " size: $[$(cat /sys/class/block/$DEV/size)*BLOCK_SIZE] bytes"
  61. else
  62. echo " size: UNKNOWN"
  63. fi
  64. echo ""
  65. }
  66. #
  67. # Parse and validate arguments
  68. #
  69. if [ $# -ne 3 ]; then
  70. usage
  71. exit 1
  72. fi
  73. DEVICE=$1
  74. HDDIMG=$2
  75. TARGET_DEVICE=$3
  76. if [ ! -w "$DEVICE" ]; then
  77. echo "ERROR: Device $DEVICE does not exist or is not writable"
  78. usage
  79. exit 1
  80. fi
  81. if [ ! -e "$HDDIMG" ]; then
  82. echo "ERROR: HDDIMG $HDDIMG does not exist"
  83. usage
  84. exit 1
  85. fi
  86. #
  87. # Check if any $DEVICE partitions are mounted
  88. #
  89. grep -q $DEVICE /proc/mounts
  90. if [ $? -eq 0 ]; then
  91. echo "ERROR: $DEVICE partitions mounted:"
  92. grep $DEVICE /proc/mounts | cut -f 1 -d " "
  93. echo "Unmount the partitions listed and try again."
  94. exit 1
  95. fi
  96. #
  97. # Confirm device with user
  98. #
  99. image_details $HDDIMG
  100. device_details $(basename $DEVICE)
  101. echo -n "Prepare EFI image on $DEVICE [y/N]? "
  102. read RESPONSE
  103. if [ "$RESPONSE" != "y" ]; then
  104. echo "Image creation aborted"
  105. exit 0
  106. fi
  107. #
  108. # Partition $DEVICE
  109. #
  110. DEVICE_SIZE=$(parted $DEVICE unit mb print | grep Disk | cut -d" " -f 3 | sed -e "s/MB//")
  111. SWAP_SIZE=$((DEVICE_SIZE*SWAP_RATIO/100))
  112. ROOTFS_SIZE=$((DEVICE_SIZE-BOOT_SIZE-SWAP_SIZE))
  113. ROOTFS_START=$((BOOT_SIZE))
  114. ROOTFS_END=$((ROOTFS_START+ROOTFS_SIZE))
  115. SWAP_START=$((ROOTFS_END))
  116. # MMC devices are special in a couple of ways
  117. # 1) they use a partition prefix character 'p'
  118. # 2) they are detected asynchronously (need ROOTWAIT)
  119. PART_PREFIX=""
  120. if [ ! "${DEVICE#/dev/mmcblk}" = "${DEVICE}" ]; then
  121. PART_PREFIX="p"
  122. fi
  123. BOOTFS=$DEVICE${PART_PREFIX}1
  124. ROOTFS=$DEVICE${PART_PREFIX}2
  125. SWAP=$DEVICE${PART_PREFIX}3
  126. ROOTWAIT=""
  127. TARGET_PART_PREFIX=""
  128. if [ ! "${TARGET_DEVICE#/dev/mmcblk}" = "${TARGET_DEVICE}" ]; then
  129. TARGET_PART_PREFIX="p"
  130. ROOTWAIT="rootwait"
  131. fi
  132. TARGET_ROOTFS=$TARGET_DEVICE${TARGET_PART_PREFIX}2
  133. TARGET_SWAP=$TARGET_DEVICE${TARGET_PART_PREFIX}3
  134. echo "*****************"
  135. echo "Boot partition size: $BOOT_SIZE MB ($BOOTFS)"
  136. echo "ROOTFS partition size: $ROOTFS_SIZE MB ($ROOTFS)"
  137. echo "Swap partition size: $SWAP_SIZE MB ($SWAP)"
  138. echo "*****************"
  139. echo "Deleting partition table on $DEVICE ..."
  140. dd if=/dev/zero of=$DEVICE bs=512 count=2
  141. echo "Creating new partition table (GPT) on $DEVICE ..."
  142. parted $DEVICE mklabel gpt
  143. echo "Creating boot partition on $BOOTFS"
  144. parted $DEVICE mkpart primary 0% $BOOT_SIZE
  145. echo "Creating ROOTFS partition on $ROOTFS"
  146. parted $DEVICE mkpart primary $ROOTFS_START $ROOTFS_END
  147. echo "Creating swap partition on $SWAP"
  148. parted $DEVICE mkpart primary $SWAP_START 100%
  149. parted $DEVICE print
  150. #
  151. # Format $DEVICE partitions
  152. #
  153. echo ""
  154. echo "Formatting $BOOTFS as vfat..."
  155. mkfs.vfat $BOOTFS
  156. echo "Formatting $ROOTFS as ext3..."
  157. mkfs.ext3 $ROOTFS
  158. echo "Formatting swap partition...($SWAP)"
  159. mkswap $SWAP
  160. #
  161. # Installing to $DEVICE
  162. #
  163. echo ""
  164. echo "Mounting images and device in preparation for installation..."
  165. TMPDIR=$(mktemp -d mkefidisk-XXX)
  166. if [ $? -ne 0 ]; then
  167. echo "ERROR: Failed to create temporary mounting directory."
  168. exit 1
  169. fi
  170. HDDIMG_MNT=$TMPDIR/hddimg
  171. HDDIMG_ROOTFS_MNT=$TMPDIR/hddimg_rootfs
  172. ROOTFS_MNT=$TMPDIR/rootfs
  173. BOOTFS_MNT=$TMPDIR/bootfs
  174. mkdir $HDDIMG_MNT
  175. mkdir $HDDIMG_ROOTFS_MNT
  176. mkdir $ROOTFS_MNT
  177. mkdir $BOOTFS_MNT
  178. mount -o loop $HDDIMG $HDDIMG_MNT
  179. mount -o loop $HDDIMG_MNT/rootfs.img $HDDIMG_ROOTFS_MNT
  180. mount $ROOTFS $ROOTFS_MNT
  181. mount $BOOTFS $BOOTFS_MNT
  182. echo "Copying ROOTFS files..."
  183. cp -a $HDDIMG_ROOTFS_MNT/* $ROOTFS_MNT
  184. echo "$TARGET_SWAP swap swap defaults 0 0" >> $ROOTFS_MNT/etc/fstab
  185. # We dont want udev to mount our root device while we're booting...
  186. if [ -d $ROOTFS_MNT/etc/udev/ ] ; then
  187. echo "$TARGET_DEVICE" >> $ROOTFS_MNT/etc/udev/mount.blacklist
  188. fi
  189. umount $ROOTFS_MNT
  190. umount $HDDIMG_ROOTFS_MNT
  191. echo "Preparing boot partition..."
  192. EFIDIR="$BOOTFS_MNT/EFI/BOOT"
  193. mkdir -p $EFIDIR
  194. GRUBCFG="$EFIDIR/grub.cfg"
  195. cp $HDDIMG_MNT/vmlinuz $BOOTFS_MNT
  196. # Copy the efi loader and config (booti*.efi and grub.cfg)
  197. cp $HDDIMG_MNT/EFI/BOOT/* $EFIDIR
  198. # Update grub config for the installed image
  199. # Delete the install entry
  200. sed -i "/menuentry 'install'/,/^}/d" $GRUBCFG
  201. # Delete the initrd lines
  202. sed -i "/initrd /d" $GRUBCFG
  203. # Delete any LABEL= strings
  204. sed -i "s/ LABEL=[^ ]*/ /" $GRUBCFG
  205. # Replace the ramdisk root with the install device and include other options
  206. sed -i "s@ root=[^ ]*@ root=$TARGET_ROOTFS rw $ROOTWAIT quiet@" $GRUBCFG
  207. # Provide a startup.nsh script for older firmware with non-standard boot
  208. # directories and paths.
  209. echo "bootia32.efi" > $BOOTFS_MNT/startup.nsh
  210. umount $BOOTFS_MNT
  211. umount $HDDIMG_MNT
  212. rm -rf $TMPDIR
  213. sync
  214. echo "Installation complete."