mkefidisk updates and gummiboot support
cover mkefidisk updates and gummiboot support
mkefidisk was in need of some improvements. This adds gummiboot support, but also adds improved logging, output formatting, robustness, error trapping, cleanup, and verbosity control.
The following changes since commit 03c6fd0caab0ea8dcc2e0c3ab827a287a4f36910:
ltp: use "foreign" automake strictness (2014-07-16 10:31:17 +0100)
are available in the git repository at:
git://git.yoctoproject.org/poky-contrib dvhart/mkefidisk
http://git.yoctoproject.org/cgit.cgi/poky-contrib/log/?h=dvhart/mkefidisk
Darren Hart (8):
mkefidisk.sh: Use all caps for volume names mkefidisk.sh: Add mount error checking mkefidisk.sh: Add gummiboot support mkefidisk.sh: Copy the EFI dir recursively mkefidisk.sh: Improve logging mkefidisk.sh: Add die() and cleanup() routines mkefidisk.sh: Make the rootfs copy the last step mkefidisk.sh: Reduce output and add verbose flag
scripts/contrib/mkefidisk.sh | 306 ++++++++++++++++++++++++++++--------------- 1 file changed, 204 insertions(+), 102 deletions(-)
Signature · 1 line
2.0.0
PATCH 1/8 mkefidisk.sh: Use all caps for volume names
Avoid some mkfs warnings by using all caps in the volume names.
--- scripts/contrib/mkefidisk.sh | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
scripts/contrib/mkefidisk.sh +3 -3
@@ -208,14 +208,14 @@ unmount_device echo "" echo "Formatting $BOOTFS as vfat..." if [ ! "${DEVICE#/dev/loop}" = "${DEVICE}" ]; then - mkfs.vfat -I $BOOTFS -n "efi" + mkfs.vfat -I $BOOTFS -n "EFI" else - mkfs.vfat $BOOTFS -n "efi" + mkfs.vfat $BOOTFS -n "EFI" fi echo "Formatting $ROOTFS as ext3..." -mkfs.ext3 $ROOTFS -L "root" +mkfs.ext3 -F $ROOTFS -L "ROOT" echo "Formatting swap partition...($SWAP)" mkswap $SWAP
Signature · 1 line
2.0.0
PATCH 2/8 mkefidisk.sh: Add mount error checking
In reply to Darren HartProvide reasonable feedabck if the mount commands fail.
--- scripts/contrib/mkefidisk.sh | 4 ++++ 1 file changed, 4 insertions(+)
scripts/contrib/mkefidisk.sh +4 -0
@@ -241,7 +241,11 @@ mkdir $ROOTFS_MNT mkdir $BOOTFS_MNT mount -o loop $HDDIMG $HDDIMG_MNT +if [ $? -ne 0 ]; then echo "ERROR: Failed to mount $HDDIMG"; fi + mount -o loop $HDDIMG_MNT/rootfs.img $HDDIMG_ROOTFS_MNT +if [ $? -ne 0 ]; then echo "ERROR: Failed to mount rootfs.img"; fi + mount $ROOTFS $ROOTFS_MNT mount $BOOTFS $BOOTFS_MNT
Signature · 1 line
2.0.0
PATCH 3/8 mkefidisk.sh: Add gummiboot support
In reply to Darren HartFixes [YOCTO 6295]
Add gummiboot support for images built using:
EFI_PROVIDER="gummiboot"
Add conditional configuration for GRUB and gummiboot. Provide some messaging about which is being performed.
--- scripts/contrib/mkefidisk.sh | 53 ++++++++++++++++++++++++++++++++++---------- 1 file changed, 41 insertions(+), 12 deletions(-)
scripts/contrib/mkefidisk.sh +41 -12
@@ -265,31 +265,60 @@ 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) +# Copy the efi loader and configs (booti*.efi and grub.cfg if it exists) cp $HDDIMG_MNT/EFI/BOOT/* $EFIDIR +# Silently ignore a missing gummiboot loader dir (we might just be a GRUB image) +cp -r $HDDIMG_MNT/loader $BOOTFS_MNT 2> /dev/null -# 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 +# Update the boot loaders configurations for an installed image # Remove any existing root= kernel parameters and: # o Add a root= parameter with the target rootfs # o Specify ro so fsck can be run during boot # o Specify rootwait in case the target media is an asyncronous block device # such as MMC or USB disks # o Specify "quiet" to minimize boot time when using slow serial consoles -sed -i "s@ root=[^ ]*@ @" $GRUBCFG -sed -i "s@vmlinuz @vmlinuz root=$TARGET_ROOTFS ro rootwait quiet @" $GRUBCFG + +# Look for a GRUB installation +GRUB_CFG="$EFIDIR/grub.cfg" +if [ -e "$GRUB_CFG" ]; then + echo "Configuring GRUB" + # Delete the install entry + sed -i "/menuentry 'install'/,/^}/d" $GRUB_CFG + # Delete the initrd lines + sed -i "/initrd /d" $GRUB_CFG + # Delete any LABEL= strings + sed -i "s/ LABEL=[^ ]*/ /" $GRUB_CFG + + sed -i "s@ root=[^ ]*@ @" $GRUB_CFG + sed -i "s@vmlinuz @vmlinuz root=$TARGET_ROOTFS ro rootwait quiet @" $GRUB_CFG +fi + +# Look for a gummiboot installation +GUMMI_ENTRIES="$BOOTFS_MNT/loader/entries" +GUMMI_CFG="$GUMMI_ENTRIES/boot.conf" +if [ -d "$GUMMI_ENTRIES" ]; then + echo "Configuring Gummiboot" + # remove the install target if it exists + rm $GUMMI_ENTRIES/install.conf &> /dev/null + + if [ ! -e "$GUMMI_CFG" ]; then + echo "ERROR: $GUMMI_CFG not found" + fi + + sed -i "s@ root=[^ ]*@ @" $GUMMI_CFG + sed -i "s@options *LABEL=boot @options LABEL=Boot root=$TARGET_ROOTFS ro rootwait quiet @" $GUMMI_CFG +fi + +# Ensure we have at least one EFI bootloader configured +if [ ! -e $GRUB_CFG ] && [ ! -e $GUMMI_CFG ]; then + echo "ERROR: No EFI bootloader configuration found" +fi umount $BOOTFS_MNT umount $HDDIMG_MNT rm -rf $TMPDIR sync -echo "Installation complete." +echo "Installation complete"
Signature · 1 line
2.0.0
PATCH 4/8 mkefidisk.sh: Copy the EFI dir recursively
In reply to Darren HartRather than only copying the EFI/BOOT dir, copy the entire EFI dir recursively. This allows for custom configurations to be enabled implicitly with no extra work required.
--- scripts/contrib/mkefidisk.sh | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-)
scripts/contrib/mkefidisk.sh +1 -3
@@ -264,11 +264,9 @@ umount $HDDIMG_ROOTFS_MNT echo "Preparing boot partition..." EFIDIR="$BOOTFS_MNT/EFI/BOOT" -mkdir -p $EFIDIR - cp $HDDIMG_MNT/vmlinuz $BOOTFS_MNT # Copy the efi loader and configs (booti*.efi and grub.cfg if it exists) -cp $HDDIMG_MNT/EFI/BOOT/* $EFIDIR +cp -r $HDDIMG_MNT/EFI $BOOTFS_MNT # Silently ignore a missing gummiboot loader dir (we might just be a GRUB image) cp -r $HDDIMG_MNT/loader $BOOTFS_MNT 2> /dev/null
Signature · 1 line
2.0.0
PATCH 5/8 mkefidisk.sh: Improve logging
In reply to Darren HartAdd logging functions: error, warn, info, and success, using tput to add color highlighting.
Use these routines throughout the script, replacing echo statements and adding "|| error" in several places to eliminate silent failures. Add a simple exit block which checks for issues encountered while running.
--- scripts/contrib/mkefidisk.sh | 92 +++++++++++++++++++++++++++++--------------- 1 file changed, 60 insertions(+), 32 deletions(-)
scripts/contrib/mkefidisk.sh +60 -32
@@ -28,6 +28,29 @@ BOOT_SIZE=20 # 5% for swap SWAP_RATIO=5 +# Logging routines +WARNINGS=0 +ERRORS=0 +CLEAR="$(tput sgr0)" +INFO="$(tput bold)" +RED="$(tput setaf 1)$(tput bold)" +GREEN="$(tput setaf 2)$(tput bold)" +YELLOW="$(tput setaf 3)$(tput bold)" +info() { + echo "${INFO}$1${CLEAR}" +} +error() { + ERRORS=$((ERRORS+1)) + echo "${RED}$1${CLEAR}" +} +warn() { + WARNINGS=$((WARNINGS+1)) + echo "${YELLOW}$1${CLEAR}" +} +success() { + echo "${GREEN}$1${CLEAR}" +} + usage() { echo "Usage: $(basename $0) DEVICE HDDIMG TARGET_DEVICE" echo " DEVICE: The device to write the image to, e.g. /dev/sdh"
@@ -37,7 +60,7 @@ usage() { image_details() { IMG=$1 - echo "Image details" + info "Image details" echo "=============" echo " image: $(stat --printf '%N\n' $IMG)" echo " size: $(stat -L --printf '%s bytes\n' $IMG)"
@@ -50,7 +73,7 @@ device_details() { DEV=$1 BLOCK_SIZE=512 - echo "Device details" + info "Device details" echo "==============" echo " device: $DEVICE" if [ -f "/sys/class/block/$DEV/device/vendor" ]; then
@@ -74,14 +97,13 @@ device_details() { unmount_device() { grep -q $DEVICE /proc/mounts if [ $? -eq 0 ]; then - echo -n "$DEVICE listed in /proc/mounts, attempting to unmount..." + warn "$DEVICE listed in /proc/mounts, attempting to unmount..." umount $DEVICE* 2>/dev/null grep -q $DEVICE /proc/mounts if [ $? -eq 0 ]; then - echo "FAILED" + error "Failed to unmount $DEVICE" exit 1 fi - echo "OK" fi }
@@ -104,13 +126,13 @@ if [ $? -eq 0 ]; then fi if [ ! -w "$DEVICE" ]; then - echo "ERROR: Device $DEVICE does not exist or is not writable" + 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" + error "HDDIMG $HDDIMG does not exist" usage exit 1 fi
@@ -127,7 +149,7 @@ unmount_device # image_details $HDDIMG device_details $(basename $DEVICE) -echo -n "Prepare EFI image on $DEVICE [y/N]? " +echo -n "${INFO}Prepare EFI image on $DEVICE [y/N]?${CLEAR} " read RESPONSE if [ "$RESPONSE" != "y" ]; then echo "Image creation aborted"
@@ -208,17 +230,16 @@ unmount_device echo "" echo "Formatting $BOOTFS as vfat..." if [ ! "${DEVICE#/dev/loop}" = "${DEVICE}" ]; then - mkfs.vfat -I $BOOTFS -n "EFI" + mkfs.vfat -I $BOOTFS -n "EFI" || error "Failed to format $BOOTFS" else - mkfs.vfat $BOOTFS -n "EFI" - + mkfs.vfat $BOOTFS -n "EFI" || error "Failed to format $BOOTFS" fi echo "Formatting $ROOTFS as ext3..." -mkfs.ext3 -F $ROOTFS -L "ROOT" +mkfs.ext3 -F $ROOTFS -L "ROOT" || error "Failed to format $ROOTFS" echo "Formatting swap partition...($SWAP)" -mkswap $SWAP +mkswap $SWAP || error "Failed to prepare swap" #
@@ -228,7 +249,7 @@ 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." + error "Failed to create temporary mounting directory." exit 1 fi HDDIMG_MNT=$TMPDIR/hddimg
@@ -240,17 +261,15 @@ mkdir $HDDIMG_ROOTFS_MNT mkdir $ROOTFS_MNT mkdir $BOOTFS_MNT -mount -o loop $HDDIMG $HDDIMG_MNT -if [ $? -ne 0 ]; then echo "ERROR: Failed to mount $HDDIMG"; fi +mount -o loop $HDDIMG $HDDIMG_MNT || error "Failed to mount $HDDIMG" -mount -o loop $HDDIMG_MNT/rootfs.img $HDDIMG_ROOTFS_MNT -if [ $? -ne 0 ]; then echo "ERROR: Failed to mount rootfs.img"; fi +mount -o loop $HDDIMG_MNT/rootfs.img $HDDIMG_ROOTFS_MNT || error "Failed to mount rootfs.img" -mount $ROOTFS $ROOTFS_MNT -mount $BOOTFS $BOOTFS_MNT +mount $ROOTFS $ROOTFS_MNT || error "Failed to mount $ROOTFS on $ROOTFS_MNT" +mount $BOOTFS $BOOTFS_MNT || error "Failed to mount $BOOTFS on $BOOTFS_MNT" echo "Copying ROOTFS files..." -cp -a $HDDIMG_ROOTFS_MNT/* $ROOTFS_MNT +cp -a $HDDIMG_ROOTFS_MNT/* $ROOTFS_MNT || error "Root FS copy failed" echo "$TARGET_SWAP swap swap defaults 0 0" >> $ROOTFS_MNT/etc/fstab
@@ -259,14 +278,14 @@ if [ -d $ROOTFS_MNT/etc/udev/ ] ; then echo "$TARGET_DEVICE" >> $ROOTFS_MNT/etc/udev/mount.blacklist fi -umount $ROOTFS_MNT -umount $HDDIMG_ROOTFS_MNT +umount $ROOTFS_MNT || error "Failed to unmount $ROOTFS_MNT" +umount $HDDIMG_ROOTFS_MNT || error "Failed to unmount $HDDIMG_ROOTFS_MNT" echo "Preparing boot partition..." EFIDIR="$BOOTFS_MNT/EFI/BOOT" -cp $HDDIMG_MNT/vmlinuz $BOOTFS_MNT +cp $HDDIMG_MNT/vmlinuz $BOOTFS_MNT || error "Failed to copy vmlinuz" # Copy the efi loader and configs (booti*.efi and grub.cfg if it exists) -cp -r $HDDIMG_MNT/EFI $BOOTFS_MNT +cp -r $HDDIMG_MNT/EFI $BOOTFS_MNT || error "Failed to copy EFI dir" # Silently ignore a missing gummiboot loader dir (we might just be a GRUB image) cp -r $HDDIMG_MNT/loader $BOOTFS_MNT 2> /dev/null
@@ -281,7 +300,7 @@ cp -r $HDDIMG_MNT/loader $BOOTFS_MNT 2> /dev/null # Look for a GRUB installation GRUB_CFG="$EFIDIR/grub.cfg" if [ -e "$GRUB_CFG" ]; then - echo "Configuring GRUB" + info "Configuring GRUB" # Delete the install entry sed -i "/menuentry 'install'/,/^}/d" $GRUB_CFG # Delete the initrd lines
@@ -297,7 +316,7 @@ fi GUMMI_ENTRIES="$BOOTFS_MNT/loader/entries" GUMMI_CFG="$GUMMI_ENTRIES/boot.conf" if [ -d "$GUMMI_ENTRIES" ]; then - echo "Configuring Gummiboot" + info "Configuring Gummiboot" # remove the install target if it exists rm $GUMMI_ENTRIES/install.conf &> /dev/null
@@ -311,12 +330,21 @@ fi # Ensure we have at least one EFI bootloader configured if [ ! -e $GRUB_CFG ] && [ ! -e $GUMMI_CFG ]; then - echo "ERROR: No EFI bootloader configuration found" + error "No EFI bootloader configuration found" fi -umount $BOOTFS_MNT -umount $HDDIMG_MNT -rm -rf $TMPDIR +umount $BOOTFS_MNT || error "Failed to unmount $BOOTFS_MNT" +umount $HDDIMG_MNT || error "Failed to unmount $HDDIMG_MNT" +rm -rf $TMPDIR || error "Failed to cleanup $TMPDIR" sync -echo "Installation complete" +if [ $WARNINGS -ne 0 ] && [ $ERRORS -eq 0 ]; then + echo "${YELLOW}Installation completed with warnings${CLEAR}" + echo "${YELLOW}Warnings: $WARNINGS${CLEAR}" +elif [ $ERRORS -ne 0 ]; then + echo "${RED}Installation encountered errors${CLEAR}" + echo "${RED}Errors: $ERRORS${CLEAR}" + echo "${YELLOW}Warnings: $WARNINGS${CLEAR}" +else + success "Installation completed successfully" +fi
Signature · 1 line
2.0.0
PATCH 6/8 mkefidisk.sh: Add die() and cleanup() routines
In reply to Darren HartCurrently the script will attempt to continue even after a fatal error. Add a die() routine which will abort in the case of a fatal error and call a cleanup() routine to unmount any images or devices and remove the TMPDIR.
Move the variable assignment and directory creation earlier in the script, making it more obvious what we need to clean up.
--- scripts/contrib/mkefidisk.sh | 115 +++++++++++++++++++++++++------------------ 1 file changed, 68 insertions(+), 47 deletions(-)
scripts/contrib/mkefidisk.sh +68 -47
@@ -28,6 +28,22 @@ BOOT_SIZE=20 # 5% for swap SWAP_RATIO=5 +# Cleanup after die() +cleanup() { + echo "Syncing and unmounting devices..." + # Unmount anything we mounted + unmount $ROOTFS_MNT || error "Failed to unmount $ROOTFS_MNT" + unmount $BOOTFS_MNT || error "Failed to unmount $BOOTFS_MNT" + unmount $HDDIMG_ROOTFS_MNT || error "Failed to unmount $HDDIMG_ROOTFS_MNT" + unmount $HDDIMG_MNT || error "Failed to unmount $HDDIMG_MNT" + + # Remove the TMPDIR + echo "Removing temporary files..." + if [ -d "$TMPDIR" ]; then + rm -rf $TMPDIR || error "Failed to remove $TMPDIR" + fi +} + # Logging routines WARNINGS=0 ERRORS=0
@@ -50,6 +66,11 @@ warn() { success() { echo "${GREEN}$1${CLEAR}" } +die() { + error $1 + cleanup + exit 1 +} usage() { echo "Usage: $(basename $0) DEVICE HDDIMG TARGET_DEVICE"
@@ -99,14 +120,20 @@ unmount_device() { if [ $? -eq 0 ]; then warn "$DEVICE listed in /proc/mounts, attempting to unmount..." umount $DEVICE* 2>/dev/null - grep -q $DEVICE /proc/mounts - if [ $? -eq 0 ]; then - error "Failed to unmount $DEVICE" - exit 1 - fi + return $? fi + return 0 } +unmount() { + grep -q $1 /proc/mounts + if [ $? -eq 0 ]; then + echo "Unmounting $1..." + umount $1 + return $? + fi + return 0 +} # # Parse and validate arguments
@@ -126,23 +153,24 @@ if [ $? -eq 0 ]; then fi if [ ! -w "$DEVICE" ]; then - error "Device $DEVICE does not exist or is not writable" usage - exit 1 + die "Device $DEVICE does not exist or is not writable" fi if [ ! -e "$HDDIMG" ]; then - error "HDDIMG $HDDIMG does not exist" usage - exit 1 + die "HDDIMG $HDDIMG does not exist" fi +# +# Ensure the hddimg is not mounted +# +unmount "$HDDIMG" || die "Failed to unmount $HDDIMG" # # Check if any $DEVICE partitions are mounted # -unmount_device - +unmount_device || die "Failed to unmount $DEVICE" # # Confirm device with user
@@ -158,12 +186,26 @@ fi # +# Prepare the temporary working space +# +TMPDIR=$(mktemp -d mkefidisk-XXX) || die "Failed to create temporary mounting directory." +HDDIMG_MNT=$TMPDIR/hddimg +HDDIMG_ROOTFS_MNT=$TMPDIR/hddimg_rootfs +ROOTFS_MNT=$TMPDIR/rootfs +BOOTFS_MNT=$TMPDIR/bootfs +mkdir $HDDIMG_MNT || die "Failed to create $HDDIMG_MNT" +mkdir $HDDIMG_ROOTFS_MNT || die "Failed to create $HDDIMG_ROOTFS_MNT" +mkdir $ROOTFS_MNT || die "Failed to create $ROOTFS_MNT" +mkdir $BOOTFS_MNT || die "Failed to create $BOOTFS_MNT" + + +# # Partition $DEVICE # DEVICE_SIZE=$(parted $DEVICE unit mb print | grep ^Disk | cut -d" " -f 3 | sed -e "s/MB//") # If the device size is not reported there may not be a valid label if [ "$DEVICE_SIZE" = "" ] ; then - parted $DEVICE mklabel msdos + parted $DEVICE mklabel msdos || die "Failed to create MSDOS partition table" DEVICE_SIZE=$(parted $DEVICE unit mb print | grep ^Disk | cut -d" " -f 3 | sed -e "s/MB//") fi SWAP_SIZE=$((DEVICE_SIZE*SWAP_RATIO/100))
@@ -195,25 +237,25 @@ 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 +dd if=/dev/zero of=$DEVICE bs=512 count=2 > /dev/null || die "Failed to zero beginning of $DEVICE" # Use MSDOS by default as GPT cannot be reliably distributed in disk image form # as it requires the backup table to be on the last block of the device, which # of course varies from device to device. echo "Creating new partition table (MSDOS) on $DEVICE ..." -parted $DEVICE mklabel msdos +parted $DEVICE mklabel msdos || die "Failed to create MSDOS partition table" echo "Creating boot partition on $BOOTFS" -parted $DEVICE mkpart primary 0% $BOOT_SIZE +parted $DEVICE mkpart primary 0% $BOOT_SIZE || die "Failed to create BOOT partition" echo "Enabling boot flag on $BOOTFS" -parted $DEVICE set 1 boot on +parted $DEVICE set 1 boot on || die "Failed to enable boot flag" echo "Creating ROOTFS partition on $ROOTFS" -parted $DEVICE mkpart primary $ROOTFS_START $ROOTFS_END +parted $DEVICE mkpart primary $ROOTFS_START $ROOTFS_END || die "Failed to create ROOTFS partition" echo "Creating swap partition on $SWAP" -parted $DEVICE mkpart primary $SWAP_START 100% +parted $DEVICE mkpart primary $SWAP_START 100% || die "Failed to create SWAP partition" parted $DEVICE print
@@ -221,7 +263,7 @@ parted $DEVICE print # # Check if any $DEVICE partitions are mounted after partitioning # -unmount_device +unmount_device || die "Failed to unmount $DEVICE partitions" #
@@ -230,16 +272,16 @@ unmount_device echo "" echo "Formatting $BOOTFS as vfat..." if [ ! "${DEVICE#/dev/loop}" = "${DEVICE}" ]; then - mkfs.vfat -I $BOOTFS -n "EFI" || error "Failed to format $BOOTFS" + mkfs.vfat -I $BOOTFS -n "EFI" || die "Failed to format $BOOTFS" else - mkfs.vfat $BOOTFS -n "EFI" || error "Failed to format $BOOTFS" + mkfs.vfat $BOOTFS -n "EFI" || die "Failed to format $BOOTFS" fi echo "Formatting $ROOTFS as ext3..." -mkfs.ext3 -F $ROOTFS -L "ROOT" || error "Failed to format $ROOTFS" +mkfs.ext3 -F $ROOTFS -L "ROOT" || die "Failed to format $ROOTFS" echo "Formatting swap partition...($SWAP)" -mkswap $SWAP || error "Failed to prepare swap" +mkswap $SWAP || die "Failed to prepare swap" #
@@ -247,24 +289,8 @@ mkswap $SWAP || error "Failed to prepare swap" # echo "" echo "Mounting images and device in preparation for installation..." -TMPDIR=$(mktemp -d mkefidisk-XXX) -if [ $? -ne 0 ]; then - 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 || error "Failed to mount $HDDIMG" - mount -o loop $HDDIMG_MNT/rootfs.img $HDDIMG_ROOTFS_MNT || error "Failed to mount rootfs.img" - mount $ROOTFS $ROOTFS_MNT || error "Failed to mount $ROOTFS on $ROOTFS_MNT" mount $BOOTFS $BOOTFS_MNT || error "Failed to mount $BOOTFS on $BOOTFS_MNT"
@@ -278,9 +304,6 @@ if [ -d $ROOTFS_MNT/etc/udev/ ] ; then echo "$TARGET_DEVICE" >> $ROOTFS_MNT/etc/udev/mount.blacklist fi -umount $ROOTFS_MNT || error "Failed to unmount $ROOTFS_MNT" -umount $HDDIMG_ROOTFS_MNT || error "Failed to unmount $HDDIMG_ROOTFS_MNT" - echo "Preparing boot partition..." EFIDIR="$BOOTFS_MNT/EFI/BOOT" cp $HDDIMG_MNT/vmlinuz $BOOTFS_MNT || error "Failed to copy vmlinuz"
@@ -330,13 +353,11 @@ fi # Ensure we have at least one EFI bootloader configured if [ ! -e $GRUB_CFG ] && [ ! -e $GUMMI_CFG ]; then - error "No EFI bootloader configuration found" + die "No EFI bootloader configuration found" fi -umount $BOOTFS_MNT || error "Failed to unmount $BOOTFS_MNT" -umount $HDDIMG_MNT || error "Failed to unmount $HDDIMG_MNT" -rm -rf $TMPDIR || error "Failed to cleanup $TMPDIR" -sync +# Call cleanup to unmount devices and images and remove the TMPDIR +cleanup if [ $WARNINGS -ne 0 ] && [ $ERRORS -eq 0 ]; then echo "${YELLOW}Installation completed with warnings${CLEAR}"
Signature · 1 line
2.0.0
PATCH 7/8 mkefidisk.sh: Make the rootfs copy the last step
In reply to Darren HartCopying the rootfs is the most time intensive task. Move it last so if we are to encounter other errors, we do so quickly and error out.
--- scripts/contrib/mkefidisk.sh | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-)
scripts/contrib/mkefidisk.sh +12 -10
@@ -294,16 +294,6 @@ mount -o loop $HDDIMG_MNT/rootfs.img $HDDIMG_ROOTFS_MNT || error "Failed to moun mount $ROOTFS $ROOTFS_MNT || error "Failed to mount $ROOTFS on $ROOTFS_MNT" mount $BOOTFS $BOOTFS_MNT || error "Failed to mount $BOOTFS on $BOOTFS_MNT" -echo "Copying ROOTFS files..." -cp -a $HDDIMG_ROOTFS_MNT/* $ROOTFS_MNT || error "Root FS copy failed" - -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 - echo "Preparing boot partition..." EFIDIR="$BOOTFS_MNT/EFI/BOOT" cp $HDDIMG_MNT/vmlinuz $BOOTFS_MNT || error "Failed to copy vmlinuz"
@@ -356,6 +346,18 @@ if [ ! -e $GRUB_CFG ] && [ ! -e $GUMMI_CFG ]; then die "No EFI bootloader configuration found" fi + +info "Copying ROOTFS files (this may take a while)" +cp -a $HDDIMG_ROOTFS_MNT/* $ROOTFS_MNT || die "Root FS copy failed" + +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 + + # Call cleanup to unmount devices and images and remove the TMPDIR cleanup
Signature · 1 line
2.0.0
PATCH 8/8 mkefidisk.sh: Reduce output and add verbose flag
In reply to Darren HartRemove superfluous output from commands, add a -v verbose flag, and cleanup output.
--- scripts/contrib/mkefidisk.sh | 114 +++++++++++++++++++++++++------------------ 1 file changed, 67 insertions(+), 47 deletions(-)
scripts/contrib/mkefidisk.sh +67 -47
@@ -20,6 +20,10 @@ LANG=C +# Set to 1 to enable additional output +DEBUG=0 +OUT="/dev/null" + # # Defaults #
@@ -30,7 +34,7 @@ SWAP_RATIO=5 # Cleanup after die() cleanup() { - echo "Syncing and unmounting devices..." + debug "Syncing and unmounting devices" # Unmount anything we mounted unmount $ROOTFS_MNT || error "Failed to unmount $ROOTFS_MNT" unmount $BOOTFS_MNT || error "Failed to unmount $BOOTFS_MNT"
@@ -38,7 +42,7 @@ cleanup() { unmount $HDDIMG_MNT || error "Failed to unmount $HDDIMG_MNT" # Remove the TMPDIR - echo "Removing temporary files..." + debug "Removing temporary files" if [ -d "$TMPDIR" ]; then rm -rf $TMPDIR || error "Failed to remove $TMPDIR" fi
@@ -71,9 +75,15 @@ die() { cleanup exit 1 } +debug() { + if [ $DEBUG -eq 1 ]; then + echo "$1" + fi +} usage() { - echo "Usage: $(basename $0) DEVICE HDDIMG TARGET_DEVICE" + echo "Usage: $(basename $0) [-v] DEVICE HDDIMG TARGET_DEVICE" + echo " -v: Verbose debug" 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"
@@ -82,7 +92,6 @@ usage() { image_details() { IMG=$1 info "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)"
@@ -95,7 +104,6 @@ device_details() { BLOCK_SIZE=512 info "Device details" - echo "==============" echo " device: $DEVICE" if [ -f "/sys/class/block/$DEV/device/vendor" ]; then echo " vendor: $(cat /sys/class/block/$DEV/device/vendor)"
@@ -118,7 +126,7 @@ device_details() { unmount_device() { grep -q $DEVICE /proc/mounts if [ $? -eq 0 ]; then - warn "$DEVICE listed in /proc/mounts, attempting to unmount..." + warn "$DEVICE listed in /proc/mounts, attempting to unmount" umount $DEVICE* 2>/dev/null return $? fi
@@ -128,7 +136,7 @@ unmount_device() { unmount() { grep -q $1 /proc/mounts if [ $? -eq 0 ]; then - echo "Unmounting $1..." + debug "Unmounting $1" umount $1 return $? fi
@@ -138,11 +146,17 @@ unmount() { # # Parse and validate arguments # -if [ $# -ne 3 ]; then +if [ $# -lt 3 ] || [ $# -gt 4 ]; then usage exit 1 fi +if [ "$1" = "-v" ]; then + DEBUG=1 + OUT="1" + shift +fi + DEVICE=$1 HDDIMG=$2 TARGET_DEVICE=$3
@@ -230,34 +244,39 @@ 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 > /dev/null || die "Failed to zero beginning of $DEVICE" +echo "" +info "Boot partition size: $BOOT_SIZE MB ($BOOTFS)" +info "ROOTFS partition size: $ROOTFS_SIZE MB ($ROOTFS)" +info "Swap partition size: $SWAP_SIZE MB ($SWAP)" +echo "" # Use MSDOS by default as GPT cannot be reliably distributed in disk image form # as it requires the backup table to be on the last block of the device, which # of course varies from device to device. -echo "Creating new partition table (MSDOS) on $DEVICE ..." -parted $DEVICE mklabel msdos || die "Failed to create MSDOS partition table" -echo "Creating boot partition on $BOOTFS" -parted $DEVICE mkpart primary 0% $BOOT_SIZE || die "Failed to create BOOT partition" +info "Partitioning installation media ($DEVICE)" -echo "Enabling boot flag on $BOOTFS" -parted $DEVICE set 1 boot on || die "Failed to enable boot flag" +debug "Deleting partition table on $DEVICE" +dd if=/dev/zero of=$DEVICE bs=512 count=2 >$OUT 2>1 || die "Failed to zero beginning of $DEVICE" -echo "Creating ROOTFS partition on $ROOTFS" -parted $DEVICE mkpart primary $ROOTFS_START $ROOTFS_END || die "Failed to create ROOTFS partition" +debug "Creating new partition table (MSDOS) on $DEVICE" +parted $DEVICE mklabel msdos >$OUT 2>1 || die "Failed to create MSDOS partition table" -echo "Creating swap partition on $SWAP" -parted $DEVICE mkpart primary $SWAP_START 100% || die "Failed to create SWAP partition" +debug "Creating boot partition on $BOOTFS" +parted $DEVICE mkpart primary 0% $BOOT_SIZE >$OUT 2>1 || die "Failed to create BOOT partition" -parted $DEVICE print +debug "Enabling boot flag on $BOOTFS" +parted $DEVICE set 1 boot on >$OUT 2>1 || die "Failed to enable boot flag" + +debug "Creating ROOTFS partition on $ROOTFS" +parted $DEVICE mkpart primary $ROOTFS_START $ROOTFS_END >$OUT 2>1 || die "Failed to create ROOTFS partition" + +debug "Creating swap partition on $SWAP" +parted $DEVICE mkpart primary $SWAP_START 100% >$OUT 2>1 || die "Failed to create SWAP partition" + +if [ $DEBUG -eq 1 ]; then + parted $DEVICE print +fi #
@@ -269,38 +288,37 @@ unmount_device || die "Failed to unmount $DEVICE partitions" # # Format $DEVICE partitions # -echo "" -echo "Formatting $BOOTFS as vfat..." +info "Formating partitions" +debug "Formatting $BOOTFS as vfat" if [ ! "${DEVICE#/dev/loop}" = "${DEVICE}" ]; then - mkfs.vfat -I $BOOTFS -n "EFI" || die "Failed to format $BOOTFS" + mkfs.vfat -I $BOOTFS -n "EFI" >$OUT 2>1 || die "Failed to format $BOOTFS" else - mkfs.vfat $BOOTFS -n "EFI" || die "Failed to format $BOOTFS" + mkfs.vfat $BOOTFS -n "EFI" >$OUT 2>1 || die "Failed to format $BOOTFS" fi -echo "Formatting $ROOTFS as ext3..." -mkfs.ext3 -F $ROOTFS -L "ROOT" || die "Failed to format $ROOTFS" +debug "Formatting $ROOTFS as ext3" +mkfs.ext3 -F $ROOTFS -L "ROOT" >$OUT 2>1 || die "Failed to format $ROOTFS" -echo "Formatting swap partition...($SWAP)" -mkswap $SWAP || die "Failed to prepare swap" +debug "Formatting swap partition ($SWAP)" +mkswap $SWAP >$OUT 2>1 || die "Failed to prepare swap" # # Installing to $DEVICE # -echo "" -echo "Mounting images and device in preparation for installation..." -mount -o loop $HDDIMG $HDDIMG_MNT || error "Failed to mount $HDDIMG" -mount -o loop $HDDIMG_MNT/rootfs.img $HDDIMG_ROOTFS_MNT || error "Failed to mount rootfs.img" -mount $ROOTFS $ROOTFS_MNT || error "Failed to mount $ROOTFS on $ROOTFS_MNT" -mount $BOOTFS $BOOTFS_MNT || error "Failed to mount $BOOTFS on $BOOTFS_MNT" +debug "Mounting images and device in preparation for installation" +mount -o loop $HDDIMG $HDDIMG_MNT >$OUT 2>1 || error "Failed to mount $HDDIMG" +mount -o loop $HDDIMG_MNT/rootfs.img $HDDIMG_ROOTFS_MNT >$OUT 2>1 || error "Failed to mount rootfs.img" +mount $ROOTFS $ROOTFS_MNT >$OUT 2>1 || error "Failed to mount $ROOTFS on $ROOTFS_MNT" +mount $BOOTFS $BOOTFS_MNT >$OUT 2>1 || error "Failed to mount $BOOTFS on $BOOTFS_MNT" -echo "Preparing boot partition..." +info "Preparing boot partition" EFIDIR="$BOOTFS_MNT/EFI/BOOT" -cp $HDDIMG_MNT/vmlinuz $BOOTFS_MNT || error "Failed to copy vmlinuz" +cp $HDDIMG_MNT/vmlinuz $BOOTFS_MNT >$OUT 2>1 || error "Failed to copy vmlinuz" # Copy the efi loader and configs (booti*.efi and grub.cfg if it exists) -cp -r $HDDIMG_MNT/EFI $BOOTFS_MNT || error "Failed to copy EFI dir" +cp -r $HDDIMG_MNT/EFI $BOOTFS_MNT >$OUT 2>1 || error "Failed to copy EFI dir" # Silently ignore a missing gummiboot loader dir (we might just be a GRUB image) -cp -r $HDDIMG_MNT/loader $BOOTFS_MNT 2> /dev/null +cp -r $HDDIMG_MNT/loader $BOOTFS_MNT >$OUT 2>1 # Update the boot loaders configurations for an installed image # Remove any existing root= kernel parameters and:
@@ -331,7 +349,7 @@ GUMMI_CFG="$GUMMI_ENTRIES/boot.conf" if [ -d "$GUMMI_ENTRIES" ]; then info "Configuring Gummiboot" # remove the install target if it exists - rm $GUMMI_ENTRIES/install.conf &> /dev/null + rm $GUMMI_ENTRIES/install.conf >$OUT 2>1 if [ ! -e "$GUMMI_CFG" ]; then echo "ERROR: $GUMMI_CFG not found"
@@ -348,7 +366,7 @@ fi info "Copying ROOTFS files (this may take a while)" -cp -a $HDDIMG_ROOTFS_MNT/* $ROOTFS_MNT || die "Root FS copy failed" +cp -a $HDDIMG_ROOTFS_MNT/* $ROOTFS_MNT >$OUT 2>1 || die "Root FS copy failed" echo "$TARGET_SWAP swap swap defaults 0 0" >> $ROOTFS_MNT/etc/fstab
@@ -361,6 +379,7 @@ fi # Call cleanup to unmount devices and images and remove the TMPDIR cleanup +echo "" if [ $WARNINGS -ne 0 ] && [ $ERRORS -eq 0 ]; then echo "${YELLOW}Installation completed with warnings${CLEAR}" echo "${YELLOW}Warnings: $WARNINGS${CLEAR}"
@@ -371,3 +390,4 @@ elif [ $ERRORS -ne 0 ]; then else success "Installation completed successfully" fi +echo ""
Signature · 1 line
2.0.0
Re: [PATCH 3/8] mkefidisk.sh: Add gummiboot support
In reply to Darren HartQuoted 52 lines · Darren Hart
Fixes [YOCTO 6295]
Add gummiboot support for images built using:
EFI_PROVIDER="gummiboot"
Add conditional configuration for GRUB and gummiboot. Provide some messaging about which is being performed.
Signed-off-by Darren Hart <[email protected]>--- scripts/contrib/mkefidisk.sh | 53++++++++++++++++++++++++++++++++++---------- 1 file changed, 41 insertions(+), 12 deletions(-)
(quoted diff) +0
-0
b/scripts/contrib/mkefidisk.shindex 0d0674b..9b13041 100755scripts/contrib/mkefidisk.sh +1
-2@@ -265,31 +265,60 @@ 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) +# Copy the efi loader and configs (booti*.efi and grub.cfg if itexists) cp $HDDIMG_MNT/EFI/BOOT/* $EFIDIR
+# Silently ignore a missing gummiboot loader dir (we might just be a GRUB image) +cp -r $HDDIMG_MNT/loader $BOOTFS_MNT 2>/dev/nullscripts/contrib/mkefidisk.sh +1
-7-# 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 +# Update the boot loaders configurations for an installed image # Remove any existing root= kernel parameters and: # o Add a root= parameter with the target rootfs # o Specify ro so fsck can be run during boot # o Specify rootwait in case the target media is an asyncronous blockdevice # such as MMC or USB disks
# o Specify "quiet" to minimize boot time when using slow serial consoles -sed -i "s@ root=[^ ]*@ @" $GRUBCFG -sed -i "s@vmlinuz @vmlinuz root=$TARGET_ROOTFS ro rootwait quiet @" $GRUBCFG +scripts/contrib/mkefidisk.sh +3
-0+# Look for a GRUB installation +GRUB_CFG="$EFIDIR/grub.cfg" +if [ -e "$GRUB_CFG" ]; then
scripts/contrib/mkefidisk.sh umount $HDDIMG_ROOTFS_MNT
Quoted 10 lines
scripts/contrib/mkefidisk.sh +10
-0+ echo "Configuring GRUB" + # Delete the install entry + sed -i "/menuentry 'install'/,/^}/d" $GRUB_CFG + # Delete the initrd lines + sed -i "/initrd /d" $GRUB_CFG + # Delete any LABEL= strings + sed -i "s/ LABEL=[^ ]*/ /" $GRUB_CFG + + sed -i "s@ root=[^ ]*@ @" $GRUB_CFG + sed -i "s@vmlinuz @vmlinuz root=$TARGET_ROOTFS ro rootwait quiet
@"
Quoted 4 lines
$GRUB_CFG +fi
scripts/contrib/mkefidisk.sh +3
-0+ +# Look for a gummiboot installation +GUMMI_ENTRIES="$BOOTFS_MNT/loader/entries"
scripts/contrib/mkefidisk.sh umount $HDDIMG_ROOTFS_MNT · line 309
Quoted 10 lines
scripts/contrib/mkefidisk.sh +10
-0+GUMMI_CFG="$GUMMI_ENTRIES/boot.conf" +if [ -d "$GUMMI_ENTRIES" ]; then + echo "Configuring Gummiboot" + # remove the install target if it exists + rm $GUMMI_ENTRIES/install.conf &> /dev/null + + if [ ! -e "$GUMMI_CFG" ]; then + echo "ERROR: $GUMMI_CFG not found" + fi +
Oops, this is missing:
+ sed -i "/initrd /d" $GUMMI_CFG
I have updated this git branch with this change.
--
Darren
Quoted 16 lines
scripts/contrib/mkefidisk.sh +2
-0+ sed -i "s@ root=[^ ]*@ @" $GUMMI_CFG + sed -i "s@options *LABEL=boot @options LABEL=Bootroot=$TARGET_ROOTFS ro rootwait quiet @" $GUMMI_CFG +fi
scripts/contrib/mkefidisk.sh +6
-1+ +# Ensure we have at least one EFI bootloader configured +if [ ! -e $GRUB_CFG ] && [ ! -e $GUMMI_CFG ]; then + echo "ERROR: No EFI bootloader configuration found" +fi umount $BOOTFS_MNT umount $HDDIMG_MNT rm -rf $TMPDIR sync -echo "Installation complete." +echo "Installation complete"
Signature · 2 lines
Darren Hart Intel Open Source Technology Center