1
0
mirror of https://git.yoctoproject.org/poky synced 2026-06-02 13:29:49 +00:00

mkefidisk.sh: Add die() and cleanup() routines

Currently 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.

(From OE-Core rev: 40fe82fecf7a94b24893862ac17ee2bc749fc5e8)

Signed-off-by: Darren Hart <dvhart@linux.intel.com>
Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
This commit is contained in:
Darren Hart
2014-07-16 14:16:02 +00:00
committed by Richard Purdie
parent fd8a230e1a
commit db162c6f56
+68 -47
View File
@@ -28,6 +28,22 @@ BOOT_SIZE=20
# 5% for swap # 5% for swap
SWAP_RATIO=5 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 # Logging routines
WARNINGS=0 WARNINGS=0
ERRORS=0 ERRORS=0
@@ -50,6 +66,11 @@ warn() {
success() { success() {
echo "${GREEN}$1${CLEAR}" echo "${GREEN}$1${CLEAR}"
} }
die() {
error $1
cleanup
exit 1
}
usage() { usage() {
echo "Usage: $(basename $0) DEVICE HDDIMG TARGET_DEVICE" echo "Usage: $(basename $0) DEVICE HDDIMG TARGET_DEVICE"
@@ -99,14 +120,20 @@ unmount_device() {
if [ $? -eq 0 ]; then 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 umount $DEVICE* 2>/dev/null
grep -q $DEVICE /proc/mounts return $?
if [ $? -eq 0 ]; then
error "Failed to unmount $DEVICE"
exit 1
fi
fi 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 # Parse and validate arguments
@@ -126,23 +153,24 @@ if [ $? -eq 0 ]; then
fi fi
if [ ! -w "$DEVICE" ]; then if [ ! -w "$DEVICE" ]; then
error "Device $DEVICE does not exist or is not writable"
usage usage
exit 1 die "Device $DEVICE does not exist or is not writable"
fi fi
if [ ! -e "$HDDIMG" ]; then if [ ! -e "$HDDIMG" ]; then
error "HDDIMG $HDDIMG does not exist"
usage usage
exit 1 die "HDDIMG $HDDIMG does not exist"
fi fi
#
# Ensure the hddimg is not mounted
#
unmount "$HDDIMG" || die "Failed to unmount $HDDIMG"
# #
# Check if any $DEVICE partitions are mounted # Check if any $DEVICE partitions are mounted
# #
unmount_device unmount_device || die "Failed to unmount $DEVICE"
# #
# Confirm device with user # Confirm device with user
@@ -157,13 +185,27 @@ if [ "$RESPONSE" != "y" ]; then
fi 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 # Partition $DEVICE
# #
DEVICE_SIZE=$(parted $DEVICE unit mb print | grep ^Disk | cut -d" " -f 3 | sed -e "s/MB//") 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 the device size is not reported there may not be a valid label
if [ "$DEVICE_SIZE" = "" ] ; then 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//") DEVICE_SIZE=$(parted $DEVICE unit mb print | grep ^Disk | cut -d" " -f 3 | sed -e "s/MB//")
fi fi
SWAP_SIZE=$((DEVICE_SIZE*SWAP_RATIO/100)) SWAP_SIZE=$((DEVICE_SIZE*SWAP_RATIO/100))
@@ -195,25 +237,25 @@ echo "Swap partition size: $SWAP_SIZE MB ($SWAP)"
echo "*****************" echo "*****************"
echo "Deleting partition table on $DEVICE ..." 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 # 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 # as it requires the backup table to be on the last block of the device, which
# of course varies from device to device. # of course varies from device to device.
echo "Creating new partition table (MSDOS) on $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" 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" 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" 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" 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 parted $DEVICE print
@@ -221,7 +263,7 @@ parted $DEVICE print
# #
# Check if any $DEVICE partitions are mounted after partitioning # 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 ""
echo "Formatting $BOOTFS as vfat..." echo "Formatting $BOOTFS as vfat..."
if [ ! "${DEVICE#/dev/loop}" = "${DEVICE}" ]; then 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 else
mkfs.vfat $BOOTFS -n "EFI" || error "Failed to format $BOOTFS" mkfs.vfat $BOOTFS -n "EFI" || die "Failed to format $BOOTFS"
fi fi
echo "Formatting $ROOTFS as ext3..." 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)" 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 ""
echo "Mounting images and device in preparation for installation..." 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 $HDDIMG_MNT || error "Failed to mount $HDDIMG"
mount -o loop $HDDIMG_MNT/rootfs.img $HDDIMG_ROOTFS_MNT || error "Failed to mount rootfs.img" 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 $ROOTFS $ROOTFS_MNT || error "Failed to mount $ROOTFS on $ROOTFS_MNT"
mount $BOOTFS $BOOTFS_MNT || error "Failed to mount $BOOTFS on $BOOTFS_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 echo "$TARGET_DEVICE" >> $ROOTFS_MNT/etc/udev/mount.blacklist
fi 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..." echo "Preparing boot partition..."
EFIDIR="$BOOTFS_MNT/EFI/BOOT" EFIDIR="$BOOTFS_MNT/EFI/BOOT"
cp $HDDIMG_MNT/vmlinuz $BOOTFS_MNT || error "Failed to copy vmlinuz" 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 # Ensure we have at least one EFI bootloader configured
if [ ! -e $GRUB_CFG ] && [ ! -e $GUMMI_CFG ]; then if [ ! -e $GRUB_CFG ] && [ ! -e $GUMMI_CFG ]; then
error "No EFI bootloader configuration found" die "No EFI bootloader configuration found"
fi fi
umount $BOOTFS_MNT || error "Failed to unmount $BOOTFS_MNT" # Call cleanup to unmount devices and images and remove the TMPDIR
umount $HDDIMG_MNT || error "Failed to unmount $HDDIMG_MNT" cleanup
rm -rf $TMPDIR || error "Failed to cleanup $TMPDIR"
sync
if [ $WARNINGS -ne 0 ] && [ $ERRORS -eq 0 ]; then if [ $WARNINGS -ne 0 ] && [ $ERRORS -eq 0 ]; then
echo "${YELLOW}Installation completed with warnings${CLEAR}" echo "${YELLOW}Installation completed with warnings${CLEAR}"