Patchwork [1/1] mkefidisk.sh: Add script to do an EFI install on the host

login
register
mail settings
Submitter Darren Hart
Date Sept. 18, 2012, 6:45 p.m.
Message ID <f3da9f86ab39674b636ac0681a726a545ac19e1a.1347993827.git.dvhart@linux.intel.com>
Download mbox | patch
Permalink /patch/36841/
State Accepted
Commit 7225c6739f9f1e51741a42437692868165aa1dfe
Headers show

Comments

Darren Hart - Sept. 18, 2012, 6:45 p.m.
Sometimes it is convenient to prepare a bootable image from the
host rather than using a live-image to install to a disk on the
target.

This script takes a live image as input, partitions a device, and
performs the installation just as the installer would if run on
the target.

Signed-off-by: Darren Hart <dvhart@linux.intel.com>
---
 scripts/contrib/mkefidisk.sh | 255 +++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 255 insertions(+)
 create mode 100755 scripts/contrib/mkefidisk.sh

Patch

diff --git a/scripts/contrib/mkefidisk.sh b/scripts/contrib/mkefidisk.sh
new file mode 100755
index 0000000..38e2217
--- /dev/null
+++ b/scripts/contrib/mkefidisk.sh
@@ -0,0 +1,255 @@ 
+#!/bin/sh
+#
+# Copyright (c) 2012, Intel Corporation.
+# All rights reserved.
+#
+# This program is free software;  you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY;  without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
+# the GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program;  if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+#
+
+#
+# Defaults
+#
+# 20 Mb for the boot partition
+BOOT_SIZE=20
+# 5% for swap
+SWAP_RATIO=5
+
+function usage() {
+	echo "Usage: $(basename $0) DEVICE HDDIMG TARGET_DEVICE"
+	echo "       DEVICE: The device to write the image to, e.g. /dev/sdh"
+	echo "       HDDIMG: The hddimg file to generate the efi disk from"
+	echo "       TARGET_DEVICE: The device the target will boot from, e.g.  /dev/mmcblk0"
+}
+
+function image_details() {
+	IMG=$1
+	echo "Image details"
+	echo "============="
+	echo "    image: $(stat --printf '%N\n' $IMG)"
+	echo "     size: $(stat -L --printf '%s bytes\n' $IMG)"
+	echo " modified: $(stat -L --printf '%y\n' $IMG)"
+	echo "     type: $(file -L -b $IMG)"
+	echo ""
+}
+
+function device_details() {
+	DEV=$1
+	BLOCK_SIZE=512
+
+	echo "Device details"
+	echo "=============="
+	echo "  device: $DEVICE"
+	if [ -f "/sys/class/block/$DEV/device/vendor" ]; then
+		echo "  vendor: $(cat /sys/class/block/$DEV/device/vendor)"
+	else
+		echo "  vendor: UNKOWN"
+	fi
+	if [ -f "/sys/class/block/$DEV/device/model" ]; then
+		echo "   model: $(cat /sys/class/block/$DEV/device/model)"
+	else
+		echo "   model: UNKNOWN"
+	fi
+	if [ -f "/sys/class/block/$DEV/size" ]; then
+		echo "    size: $[$(cat /sys/class/block/$DEV/size)*BLOCK_SIZE] bytes"
+	else
+		echo "    size: UNKNOWN"
+	fi
+	echo ""
+}
+
+
+#
+# Parse and validate arguments
+#
+if [ $# -ne 3 ]; then
+	usage
+	exit 1
+fi
+
+DEVICE=$1
+HDDIMG=$2
+TARGET_DEVICE=$3
+
+if [ ! -w "$DEVICE" ]; then
+	echo "ERROR: Device $DEVICE does not exist or is not writable"
+	usage
+	exit 1
+fi
+
+if [ ! -e "$HDDIMG" ]; then
+	echo "ERROR: HDDIMG $HDDIMG does not exist"
+	usage
+	exit 1
+fi
+
+
+#
+# Check if any $DEVICE partitions are mounted
+#
+grep -q $DEVICE /proc/mounts
+if [ $? -eq 0 ]; then
+	echo "ERROR: $DEVICE partitions mounted:"
+	grep $DEVICE /proc/mounts | cut -f 1 -d " "
+	echo "Unmount the partitions listed and try again."
+	exit 1
+fi
+
+
+#
+# Confirm device with user
+#
+image_details $HDDIMG
+device_details $(basename $DEVICE)
+echo -n "Prepare EFI image on $DEVICE [y/N]? "
+read RESPONSE
+if [ "$RESPONSE" != "y" ]; then
+	echo "Image creation aborted"
+	exit 0
+fi
+
+
+#
+# Partition $DEVICE
+#
+DEVICE_SIZE=$(parted $DEVICE unit mb print | grep Disk | cut -d" " -f 3 | sed -e "s/MB//")
+SWAP_SIZE=$((DEVICE_SIZE*SWAP_RATIO/100))
+ROOTFS_SIZE=$((DEVICE_SIZE-BOOT_SIZE-SWAP_SIZE))
+ROOTFS_START=$((BOOT_SIZE))
+ROOTFS_END=$((ROOTFS_START+ROOTFS_SIZE))
+SWAP_START=$((ROOTFS_END))
+
+# MMC devices are special in a couple of ways
+# 1) they use a partition prefix character 'p'
+# 2) they are detected asynchronously (need ROOTWAIT)
+PART_PREFIX=""
+if [ ! "${DEVICE#/dev/mmcblk}" = "${DEVICE}" ]; then
+	PART_PREFIX="p"
+fi
+BOOTFS=$DEVICE${PART_PREFIX}1
+ROOTFS=$DEVICE${PART_PREFIX}2
+SWAP=$DEVICE${PART_PREFIX}3
+
+ROOTWAIT=""
+TARGET_PART_PREFIX=""
+if [ ! "${TARGET_DEVICE#/dev/mmcblk}" = "${TARGET_DEVICE}" ]; then
+	TARGET_PART_PREFIX="p"
+	ROOTWAIT="rootwait"
+fi
+TARGET_ROOTFS=$TARGET_DEVICE${TARGET_PART_PREFIX}2
+TARGET_SWAP=$TARGET_DEVICE${TARGET_PART_PREFIX}3
+
+echo "*****************"
+echo "Boot partition size:   $BOOT_SIZE MB ($BOOTFS)"
+echo "ROOTFS partition size: $ROOTFS_SIZE MB ($ROOTFS)"
+echo "Swap partition size:   $SWAP_SIZE MB ($SWAP)"
+echo "*****************"
+echo "Deleting partition table on $DEVICE ..."
+dd if=/dev/zero of=$DEVICE bs=512 count=2
+
+echo "Creating new partition table (GPT) on $DEVICE ..."
+parted $DEVICE mklabel gpt
+
+echo "Creating boot partition on $BOOTFS"
+parted $DEVICE mkpart primary 0% $BOOT_SIZE
+
+echo "Creating ROOTFS partition on $ROOTFS"
+parted $DEVICE mkpart primary $ROOTFS_START $ROOTFS_END
+
+echo "Creating swap partition on $SWAP"
+parted $DEVICE mkpart primary $SWAP_START 100%
+
+parted $DEVICE print
+
+
+#
+# Format $DEVICE partitions
+#
+echo ""
+echo "Formatting $BOOTFS as vfat..."
+mkfs.vfat $BOOTFS
+
+echo "Formatting $ROOTFS as ext3..."
+mkfs.ext3 $ROOTFS
+
+echo "Formatting swap partition...($SWAP)"
+mkswap $SWAP
+
+
+#
+# Installing to $DEVICE
+#
+echo ""
+echo "Mounting images and device in preparation for installation..."
+TMPDIR=$(mktemp -d mkefidisk-XXX)
+if [ $? -ne 0 ]; then
+	echo "ERROR: Failed to create temporary mounting directory."
+	exit 1
+fi
+HDDIMG_MNT=$TMPDIR/hddimg
+HDDIMG_ROOTFS_MNT=$TMPDIR/hddimg_rootfs
+ROOTFS_MNT=$TMPDIR/rootfs
+BOOTFS_MNT=$TMPDIR/bootfs
+mkdir $HDDIMG_MNT
+mkdir $HDDIMG_ROOTFS_MNT
+mkdir $ROOTFS_MNT
+mkdir $BOOTFS_MNT
+
+mount -o loop $HDDIMG $HDDIMG_MNT
+mount -o loop $HDDIMG_MNT/rootfs.img $HDDIMG_ROOTFS_MNT
+mount $ROOTFS $ROOTFS_MNT
+mount $BOOTFS $BOOTFS_MNT
+
+echo "Copying ROOTFS files..."
+cp -a $HDDIMG_ROOTFS_MNT/* $ROOTFS_MNT
+
+echo "$TARGET_SWAP     swap             swap       defaults              0 0" >> $ROOTFS_MNT/etc/fstab
+
+# We dont want udev to mount our root device while we're booting...
+if [ -d $ROOTFS_MNT/etc/udev/ ] ; then
+	echo "$TARGET_DEVICE" >> $ROOTFS_MNT/etc/udev/mount.blacklist
+fi
+
+umount $ROOTFS_MNT
+umount $HDDIMG_ROOTFS_MNT
+
+echo "Preparing boot partition..."
+EFIDIR="$BOOTFS_MNT/EFI/BOOT"
+mkdir -p $EFIDIR
+GRUBCFG="$EFIDIR/grub.cfg"
+
+cp $HDDIMG_MNT/vmlinuz $BOOTFS_MNT
+# Copy the efi loader and config (booti*.efi and grub.cfg)
+cp $HDDIMG_MNT/EFI/BOOT/* $EFIDIR
+
+# Update grub config for the installed image
+# Delete the install entry
+sed -i "/menuentry 'install'/,/^}/d" $GRUBCFG
+# Delete the initrd lines
+sed -i "/initrd /d" $GRUBCFG
+# Delete any LABEL= strings
+sed -i "s/ LABEL=[^ ]*/ /" $GRUBCFG
+# Replace the ramdisk root with the install device and include other options
+sed -i "s@ root=[^ ]*@ root=$TARGET_ROOTFS rw $ROOTWAIT quiet@" $GRUBCFG
+
+# Provide a startup.nsh script for older firmware with non-standard boot
+# directories and paths.
+echo "bootia32.efi" > $BOOTFS_MNT/startup.nsh
+
+umount $BOOTFS_MNT
+umount $HDDIMG_MNT
+rm -rf $TMPDIR
+sync
+
+echo "Installation complete."