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