mkefidisk.sh 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  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. LANG=C
  21. # Set to 1 to enable additional output
  22. DEBUG=0
  23. OUT="/dev/null"
  24. #
  25. # Defaults
  26. #
  27. # 20 Mb for the boot partition
  28. BOOT_SIZE=20
  29. # 5% for swap
  30. SWAP_RATIO=5
  31. # Cleanup after die()
  32. cleanup() {
  33. debug "Syncing and unmounting devices"
  34. # Unmount anything we mounted
  35. unmount $ROOTFS_MNT || error "Failed to unmount $ROOTFS_MNT"
  36. unmount $BOOTFS_MNT || error "Failed to unmount $BOOTFS_MNT"
  37. unmount $HDDIMG_ROOTFS_MNT || error "Failed to unmount $HDDIMG_ROOTFS_MNT"
  38. unmount $HDDIMG_MNT || error "Failed to unmount $HDDIMG_MNT"
  39. # Remove the TMPDIR
  40. debug "Removing temporary files"
  41. if [ -d "$TMPDIR" ]; then
  42. rm -rf $TMPDIR || error "Failed to remove $TMPDIR"
  43. fi
  44. }
  45. trap 'die "Signal Received, Aborting..."' HUP INT TERM
  46. # Logging routines
  47. WARNINGS=0
  48. ERRORS=0
  49. CLEAR="$(tput sgr0)"
  50. INFO="$(tput bold)"
  51. RED="$(tput setaf 1)$(tput bold)"
  52. GREEN="$(tput setaf 2)$(tput bold)"
  53. YELLOW="$(tput setaf 3)$(tput bold)"
  54. info() {
  55. echo "${INFO}$1${CLEAR}"
  56. }
  57. error() {
  58. ERRORS=$((ERRORS+1))
  59. echo "${RED}$1${CLEAR}"
  60. }
  61. warn() {
  62. WARNINGS=$((WARNINGS+1))
  63. echo "${YELLOW}$1${CLEAR}"
  64. }
  65. success() {
  66. echo "${GREEN}$1${CLEAR}"
  67. }
  68. die() {
  69. error "$1"
  70. cleanup
  71. exit 1
  72. }
  73. debug() {
  74. if [ $DEBUG -eq 1 ]; then
  75. echo "$1"
  76. fi
  77. }
  78. usage() {
  79. echo "Usage: $(basename $0) [-v] DEVICE HDDIMG TARGET_DEVICE"
  80. echo " -v: Verbose debug"
  81. echo " DEVICE: The device to write the image to, e.g. /dev/sdh"
  82. echo " HDDIMG: The hddimg file to generate the efi disk from"
  83. echo " TARGET_DEVICE: The device the target will boot from, e.g. /dev/mmcblk0"
  84. }
  85. image_details() {
  86. IMG=$1
  87. info "Image details"
  88. echo " image: $(stat --printf '%N\n' $IMG)"
  89. echo " size: $(stat -L --printf '%s bytes\n' $IMG)"
  90. echo " modified: $(stat -L --printf '%y\n' $IMG)"
  91. echo " type: $(file -L -b $IMG)"
  92. echo ""
  93. }
  94. device_details() {
  95. DEV=$1
  96. BLOCK_SIZE=512
  97. info "Device details"
  98. echo " device: $DEVICE"
  99. if [ -f "/sys/class/block/$DEV/device/vendor" ]; then
  100. echo " vendor: $(cat /sys/class/block/$DEV/device/vendor)"
  101. else
  102. echo " vendor: UNKOWN"
  103. fi
  104. if [ -f "/sys/class/block/$DEV/device/model" ]; then
  105. echo " model: $(cat /sys/class/block/$DEV/device/model)"
  106. else
  107. echo " model: UNKNOWN"
  108. fi
  109. if [ -f "/sys/class/block/$DEV/size" ]; then
  110. echo " size: $(($(cat /sys/class/block/$DEV/size) * $BLOCK_SIZE)) bytes"
  111. else
  112. echo " size: UNKNOWN"
  113. fi
  114. echo ""
  115. }
  116. unmount_device() {
  117. grep -q $DEVICE /proc/mounts
  118. if [ $? -eq 0 ]; then
  119. warn "$DEVICE listed in /proc/mounts, attempting to unmount"
  120. umount $DEVICE* 2>/dev/null
  121. return $?
  122. fi
  123. return 0
  124. }
  125. unmount() {
  126. if [ "$1" = "" ] ; then
  127. return 0
  128. fi
  129. grep -q $1 /proc/mounts
  130. if [ $? -eq 0 ]; then
  131. debug "Unmounting $1"
  132. umount $1
  133. return $?
  134. fi
  135. return 0
  136. }
  137. #
  138. # Parse and validate arguments
  139. #
  140. if [ $# -lt 3 ] || [ $# -gt 4 ]; then
  141. if [ $# -eq 1 ]; then
  142. AVAILABLE_DISK=`lsblk | grep "disk" | cut -f 1 -d " "`
  143. X=0
  144. for disk in `echo $AVAILABLE_DISK`; do
  145. mounted=`lsblk /dev/$disk | awk {'print $7'} | sed "s/MOUNTPOINT//"`
  146. if [ -z "$mounted" ]; then
  147. UNMOUNTED_AVAILABLES="$UNMOUNTED_AVAILABLES /dev/$disk"
  148. info "$X - /dev/$disk"
  149. X=`expr $X + 1`
  150. fi
  151. done
  152. if [ $X -eq 0 ]; then
  153. die "No unmounted device found."
  154. fi
  155. read -p "Choose unmounted device number: " DISK_NUMBER
  156. X=0
  157. for line in `echo $UNMOUNTED_AVAILABLES`; do
  158. if [ $DISK_NUMBER -eq $X ]; then
  159. DISK_TO_BE_FLASHED=$line
  160. break
  161. else
  162. X=`expr $X + 1`
  163. fi
  164. done
  165. if [ -z "$DISK_TO_BE_FLASHED" ]; then
  166. die "Option \"$DISK_NUMBER\" is invalid. Choose a valid option"
  167. else
  168. if [ -z `echo $DISK_TO_BE_FLASHED | grep "mmc"` ]; then
  169. TARGET_TO_BE_BOOT="/dev/sda"
  170. else
  171. TARGET_TO_BE_BOOT="/dev/mmcblk0"
  172. fi
  173. fi
  174. echo ""
  175. echo "Choose a name of the device that will be boot from"
  176. echo -n "Recommended name is: "
  177. info "$TARGET_TO_BE_BOOT"
  178. read -p "Is target device okay? [y/N]: " RESPONSE
  179. if [ "$RESPONSE" != "y" ]; then
  180. read -p "Choose target device name: " TARGET_TO_BE_BOOT
  181. fi
  182. echo ""
  183. if [ -z "$TARGET_TO_BE_BOOT" ]; then
  184. die "Error: choose a valid target name"
  185. fi
  186. else
  187. usage
  188. exit 1
  189. fi
  190. fi
  191. if [ "$1" = "-v" ]; then
  192. DEBUG=1
  193. OUT="1"
  194. shift
  195. fi
  196. if [ -z "$AVAILABLE_DISK" ]; then
  197. DEVICE=$1
  198. HDDIMG=$2
  199. TARGET_DEVICE=$3
  200. else
  201. DEVICE=$DISK_TO_BE_FLASHED
  202. HDDIMG=$1
  203. TARGET_DEVICE=$TARGET_TO_BE_BOOT
  204. fi
  205. LINK=$(readlink $DEVICE)
  206. if [ $? -eq 0 ]; then
  207. DEVICE="$LINK"
  208. fi
  209. if [ ! -w "$DEVICE" ]; then
  210. usage
  211. if [ ! -e "${DEVICE}" ] ; then
  212. die "Device $DEVICE cannot be found"
  213. else
  214. die "Device $DEVICE is not writable (need to run under sudo?)"
  215. fi
  216. fi
  217. if [ ! -e "$HDDIMG" ]; then
  218. usage
  219. die "HDDIMG $HDDIMG does not exist"
  220. fi
  221. #
  222. # Ensure the hddimg is not mounted
  223. #
  224. unmount "$HDDIMG" || die "Failed to unmount $HDDIMG"
  225. #
  226. # Check if any $DEVICE partitions are mounted
  227. #
  228. unmount_device || die "Failed to unmount $DEVICE"
  229. #
  230. # Confirm device with user
  231. #
  232. image_details $HDDIMG
  233. device_details $(basename $DEVICE)
  234. echo -n "${INFO}Prepare EFI image on $DEVICE [y/N]?${CLEAR} "
  235. read RESPONSE
  236. if [ "$RESPONSE" != "y" ]; then
  237. echo "Image creation aborted"
  238. exit 0
  239. fi
  240. #
  241. # Prepare the temporary working space
  242. #
  243. TMPDIR=$(mktemp -d mkefidisk-XXX) || die "Failed to create temporary mounting directory."
  244. HDDIMG_MNT=$TMPDIR/hddimg
  245. HDDIMG_ROOTFS_MNT=$TMPDIR/hddimg_rootfs
  246. ROOTFS_MNT=$TMPDIR/rootfs
  247. BOOTFS_MNT=$TMPDIR/bootfs
  248. mkdir $HDDIMG_MNT || die "Failed to create $HDDIMG_MNT"
  249. mkdir $HDDIMG_ROOTFS_MNT || die "Failed to create $HDDIMG_ROOTFS_MNT"
  250. mkdir $ROOTFS_MNT || die "Failed to create $ROOTFS_MNT"
  251. mkdir $BOOTFS_MNT || die "Failed to create $BOOTFS_MNT"
  252. #
  253. # Partition $DEVICE
  254. #
  255. DEVICE_SIZE=$(parted -s $DEVICE unit mb print | grep ^Disk | cut -d" " -f 3 | sed -e "s/MB//")
  256. # If the device size is not reported there may not be a valid label
  257. if [ "$DEVICE_SIZE" = "" ] ; then
  258. parted -s $DEVICE mklabel msdos || die "Failed to create MSDOS partition table"
  259. DEVICE_SIZE=$(parted -s $DEVICE unit mb print | grep ^Disk | cut -d" " -f 3 | sed -e "s/MB//")
  260. fi
  261. SWAP_SIZE=$((DEVICE_SIZE*SWAP_RATIO/100))
  262. ROOTFS_SIZE=$((DEVICE_SIZE-BOOT_SIZE-SWAP_SIZE))
  263. ROOTFS_START=$((BOOT_SIZE))
  264. ROOTFS_END=$((ROOTFS_START+ROOTFS_SIZE))
  265. SWAP_START=$((ROOTFS_END))
  266. # MMC devices use a partition prefix character 'p'
  267. PART_PREFIX=""
  268. if [ ! "${DEVICE#/dev/mmcblk}" = "${DEVICE}" ] || [ ! "${DEVICE#/dev/loop}" = "${DEVICE}" ]; then
  269. PART_PREFIX="p"
  270. fi
  271. BOOTFS=$DEVICE${PART_PREFIX}1
  272. ROOTFS=$DEVICE${PART_PREFIX}2
  273. SWAP=$DEVICE${PART_PREFIX}3
  274. TARGET_PART_PREFIX=""
  275. if [ ! "${TARGET_DEVICE#/dev/mmcblk}" = "${TARGET_DEVICE}" ]; then
  276. TARGET_PART_PREFIX="p"
  277. fi
  278. TARGET_ROOTFS=$TARGET_DEVICE${TARGET_PART_PREFIX}2
  279. TARGET_SWAP=$TARGET_DEVICE${TARGET_PART_PREFIX}3
  280. echo ""
  281. info "Boot partition size: $BOOT_SIZE MB ($BOOTFS)"
  282. info "ROOTFS partition size: $ROOTFS_SIZE MB ($ROOTFS)"
  283. info "Swap partition size: $SWAP_SIZE MB ($SWAP)"
  284. echo ""
  285. # Use MSDOS by default as GPT cannot be reliably distributed in disk image form
  286. # as it requires the backup table to be on the last block of the device, which
  287. # of course varies from device to device.
  288. info "Partitioning installation media ($DEVICE)"
  289. debug "Deleting partition table on $DEVICE"
  290. dd if=/dev/zero of=$DEVICE bs=512 count=2 >$OUT 2>&1 || die "Failed to zero beginning of $DEVICE"
  291. debug "Creating new partition table (MSDOS) on $DEVICE"
  292. parted -s $DEVICE mklabel msdos >$OUT 2>&1 || die "Failed to create MSDOS partition table"
  293. debug "Creating boot partition on $BOOTFS"
  294. parted -s $DEVICE mkpart primary 0% $BOOT_SIZE >$OUT 2>&1 || die "Failed to create BOOT partition"
  295. debug "Enabling boot flag on $BOOTFS"
  296. parted -s $DEVICE set 1 boot on >$OUT 2>&1 || die "Failed to enable boot flag"
  297. debug "Creating ROOTFS partition on $ROOTFS"
  298. parted -s $DEVICE mkpart primary $ROOTFS_START $ROOTFS_END >$OUT 2>&1 || die "Failed to create ROOTFS partition"
  299. debug "Creating swap partition on $SWAP"
  300. parted -s $DEVICE mkpart primary $SWAP_START 100% >$OUT 2>&1 || die "Failed to create SWAP partition"
  301. if [ $DEBUG -eq 1 ]; then
  302. parted -s $DEVICE print
  303. fi
  304. #
  305. # Check if any $DEVICE partitions are mounted after partitioning
  306. #
  307. unmount_device || die "Failed to unmount $DEVICE partitions"
  308. #
  309. # Format $DEVICE partitions
  310. #
  311. info "Formatting partitions"
  312. debug "Formatting $BOOTFS as vfat"
  313. if [ ! "${DEVICE#/dev/loop}" = "${DEVICE}" ]; then
  314. mkfs.vfat -I $BOOTFS -n "EFI" >$OUT 2>&1 || die "Failed to format $BOOTFS"
  315. else
  316. mkfs.vfat $BOOTFS -n "EFI" >$OUT 2>&1 || die "Failed to format $BOOTFS"
  317. fi
  318. debug "Formatting $ROOTFS as ext3"
  319. mkfs.ext3 -F $ROOTFS -L "ROOT" >$OUT 2>&1 || die "Failed to format $ROOTFS"
  320. debug "Formatting swap partition ($SWAP)"
  321. mkswap $SWAP >$OUT 2>&1 || die "Failed to prepare swap"
  322. #
  323. # Installing to $DEVICE
  324. #
  325. debug "Mounting images and device in preparation for installation"
  326. mount -o ro,loop $HDDIMG $HDDIMG_MNT >$OUT 2>&1 || error "Failed to mount $HDDIMG"
  327. mount -o ro,loop $HDDIMG_MNT/rootfs.img $HDDIMG_ROOTFS_MNT >$OUT 2>&1 || error "Failed to mount rootfs.img"
  328. mount $ROOTFS $ROOTFS_MNT >$OUT 2>&1 || error "Failed to mount $ROOTFS on $ROOTFS_MNT"
  329. mount $BOOTFS $BOOTFS_MNT >$OUT 2>&1 || error "Failed to mount $BOOTFS on $BOOTFS_MNT"
  330. info "Preparing boot partition"
  331. EFIDIR="$BOOTFS_MNT/EFI/BOOT"
  332. cp $HDDIMG_MNT/vmlinuz $BOOTFS_MNT >$OUT 2>&1 || error "Failed to copy vmlinuz"
  333. # Copy the efi loader and configs (booti*.efi and grub.cfg if it exists)
  334. cp -r $HDDIMG_MNT/EFI $BOOTFS_MNT >$OUT 2>&1 || error "Failed to copy EFI dir"
  335. # Silently ignore a missing gummiboot loader dir (we might just be a GRUB image)
  336. cp -r $HDDIMG_MNT/loader $BOOTFS_MNT >$OUT 2>&1
  337. # Update the boot loaders configurations for an installed image
  338. # Remove any existing root= kernel parameters and:
  339. # o Add a root= parameter with the target rootfs
  340. # o Specify ro so fsck can be run during boot
  341. # o Specify rootwait in case the target media is an asyncronous block device
  342. # such as MMC or USB disks
  343. # o Specify "quiet" to minimize boot time when using slow serial consoles
  344. # Look for a GRUB installation
  345. GRUB_CFG="$EFIDIR/grub.cfg"
  346. if [ -e "$GRUB_CFG" ]; then
  347. info "Configuring GRUB"
  348. # Delete the install entry
  349. sed -i "/menuentry 'install'/,/^}/d" $GRUB_CFG
  350. # Delete the initrd lines
  351. sed -i "/initrd /d" $GRUB_CFG
  352. # Delete any LABEL= strings
  353. sed -i "s/ LABEL=[^ ]*/ /" $GRUB_CFG
  354. sed -i "s@ root=[^ ]*@ @" $GRUB_CFG
  355. sed -i "s@vmlinuz @vmlinuz root=$TARGET_ROOTFS ro rootwait console=ttyS0 console=tty0 @" $GRUB_CFG
  356. fi
  357. # Look for a gummiboot installation
  358. GUMMI_ENTRIES="$BOOTFS_MNT/loader/entries"
  359. GUMMI_CFG="$GUMMI_ENTRIES/boot.conf"
  360. if [ -d "$GUMMI_ENTRIES" ]; then
  361. info "Configuring Gummiboot"
  362. # remove the install target if it exists
  363. rm $GUMMI_ENTRIES/install.conf >$OUT 2>&1
  364. if [ ! -e "$GUMMI_CFG" ]; then
  365. echo "ERROR: $GUMMI_CFG not found"
  366. fi
  367. sed -i "/initrd /d" $GUMMI_CFG
  368. sed -i "s@ root=[^ ]*@ @" $GUMMI_CFG
  369. sed -i "s@options *LABEL=boot @options LABEL=Boot root=$TARGET_ROOTFS ro rootwait console=ttyS0 console=tty0 @" $GUMMI_CFG
  370. fi
  371. # Ensure we have at least one EFI bootloader configured
  372. if [ ! -e $GRUB_CFG ] && [ ! -e $GUMMI_CFG ]; then
  373. die "No EFI bootloader configuration found"
  374. fi
  375. info "Copying ROOTFS files (this may take a while)"
  376. cp -a $HDDIMG_ROOTFS_MNT/* $ROOTFS_MNT >$OUT 2>&1 || die "Root FS copy failed"
  377. echo "$TARGET_SWAP swap swap defaults 0 0" >> $ROOTFS_MNT/etc/fstab
  378. # We dont want udev to mount our root device while we're booting...
  379. if [ -d $ROOTFS_MNT/etc/udev/ ] ; then
  380. echo "$TARGET_DEVICE" >> $ROOTFS_MNT/etc/udev/mount.blacklist
  381. fi
  382. # Add startup.nsh script for automated boot
  383. echo "fs0:\EFI\BOOT\bootx64.efi" > $BOOTFS_MNT/startup.nsh
  384. # Call cleanup to unmount devices and images and remove the TMPDIR
  385. cleanup
  386. echo ""
  387. if [ $WARNINGS -ne 0 ] && [ $ERRORS -eq 0 ]; then
  388. echo "${YELLOW}Installation completed with warnings${CLEAR}"
  389. echo "${YELLOW}Warnings: $WARNINGS${CLEAR}"
  390. elif [ $ERRORS -ne 0 ]; then
  391. echo "${RED}Installation encountered errors${CLEAR}"
  392. echo "${RED}Errors: $ERRORS${CLEAR}"
  393. echo "${YELLOW}Warnings: $WARNINGS${CLEAR}"
  394. else
  395. success "Installation completed successfully"
  396. fi
  397. echo ""