mkefidisk.sh 13 KB

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