summaryrefslogtreecommitdiffstats
path: root/scripts/contrib/mkefidisk.sh
diff options
context:
space:
mode:
authorDarren Hart <dvhart@linux.intel.com>2014-07-16 14:16:02 +0000
committerRichard Purdie <richard.purdie@linuxfoundation.org>2014-07-19 00:09:00 +0100
commitdb162c6f564218b678b68f72e1dd4cdab3c3110f (patch)
tree26774807fe065890cba345ac6de3c1fcef8d1c41 /scripts/contrib/mkefidisk.sh
parentfd8a230e1acda64e7cd1ad4fb7668b9c58b9228a (diff)
downloadpoky-db162c6f564218b678b68f72e1dd4cdab3c3110f.tar.gz
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>
Diffstat (limited to 'scripts/contrib/mkefidisk.sh')
-rwxr-xr-xscripts/contrib/mkefidisk.sh115
1 files changed, 68 insertions, 47 deletions
diff --git a/scripts/contrib/mkefidisk.sh b/scripts/contrib/mkefidisk.sh
index 530b7842bb..6cc6b78de2 100755
--- a/scripts/contrib/mkefidisk.sh
+++ b/scripts/contrib/mkefidisk.sh
@@ -28,6 +28,22 @@ BOOT_SIZE=20
28# 5% for swap 28# 5% for swap
29SWAP_RATIO=5 29SWAP_RATIO=5
30 30
31# Cleanup after die()
32cleanup() {
33 echo "Syncing and unmounting devices..."
34 # Unmount anything we mounted
35 unmount $ROOTFS_MNT || error "Failed to unmount $ROOTFS_MNT"
36 unmount $BOOTFS_MNT || error "Failed to unmount $BOOTFS_MNT"
37 unmount $HDDIMG_ROOTFS_MNT || error "Failed to unmount $HDDIMG_ROOTFS_MNT"
38 unmount $HDDIMG_MNT || error "Failed to unmount $HDDIMG_MNT"
39
40 # Remove the TMPDIR
41 echo "Removing temporary files..."
42 if [ -d "$TMPDIR" ]; then
43 rm -rf $TMPDIR || error "Failed to remove $TMPDIR"
44 fi
45}
46
31# Logging routines 47# Logging routines
32WARNINGS=0 48WARNINGS=0
33ERRORS=0 49ERRORS=0
@@ -50,6 +66,11 @@ warn() {
50success() { 66success() {
51 echo "${GREEN}$1${CLEAR}" 67 echo "${GREEN}$1${CLEAR}"
52} 68}
69die() {
70 error $1
71 cleanup
72 exit 1
73}
53 74
54usage() { 75usage() {
55 echo "Usage: $(basename $0) DEVICE HDDIMG TARGET_DEVICE" 76 echo "Usage: $(basename $0) DEVICE HDDIMG TARGET_DEVICE"
@@ -99,14 +120,20 @@ unmount_device() {
99 if [ $? -eq 0 ]; then 120 if [ $? -eq 0 ]; then
100 warn "$DEVICE listed in /proc/mounts, attempting to unmount..." 121 warn "$DEVICE listed in /proc/mounts, attempting to unmount..."
101 umount $DEVICE* 2>/dev/null 122 umount $DEVICE* 2>/dev/null
102 grep -q $DEVICE /proc/mounts 123 return $?
103 if [ $? -eq 0 ]; then
104 error "Failed to unmount $DEVICE"
105 exit 1
106 fi
107 fi 124 fi
125 return 0
108} 126}
109 127
128unmount() {
129 grep -q $1 /proc/mounts
130 if [ $? -eq 0 ]; then
131 echo "Unmounting $1..."
132 umount $1
133 return $?
134 fi
135 return 0
136}
110 137
111# 138#
112# Parse and validate arguments 139# Parse and validate arguments
@@ -126,23 +153,24 @@ if [ $? -eq 0 ]; then
126fi 153fi
127 154
128if [ ! -w "$DEVICE" ]; then 155if [ ! -w "$DEVICE" ]; then
129 error "Device $DEVICE does not exist or is not writable"
130 usage 156 usage
131 exit 1 157 die "Device $DEVICE does not exist or is not writable"
132fi 158fi
133 159
134if [ ! -e "$HDDIMG" ]; then 160if [ ! -e "$HDDIMG" ]; then
135 error "HDDIMG $HDDIMG does not exist"
136 usage 161 usage
137 exit 1 162 die "HDDIMG $HDDIMG does not exist"
138fi 163fi
139 164
165#
166# Ensure the hddimg is not mounted
167#
168unmount "$HDDIMG" || die "Failed to unmount $HDDIMG"
140 169
141# 170#
142# Check if any $DEVICE partitions are mounted 171# Check if any $DEVICE partitions are mounted
143# 172#
144unmount_device 173unmount_device || die "Failed to unmount $DEVICE"
145
146 174
147# 175#
148# Confirm device with user 176# Confirm device with user
@@ -158,12 +186,26 @@ fi
158 186
159 187
160# 188#
189# Prepare the temporary working space
190#
191TMPDIR=$(mktemp -d mkefidisk-XXX) || die "Failed to create temporary mounting directory."
192HDDIMG_MNT=$TMPDIR/hddimg
193HDDIMG_ROOTFS_MNT=$TMPDIR/hddimg_rootfs
194ROOTFS_MNT=$TMPDIR/rootfs
195BOOTFS_MNT=$TMPDIR/bootfs
196mkdir $HDDIMG_MNT || die "Failed to create $HDDIMG_MNT"
197mkdir $HDDIMG_ROOTFS_MNT || die "Failed to create $HDDIMG_ROOTFS_MNT"
198mkdir $ROOTFS_MNT || die "Failed to create $ROOTFS_MNT"
199mkdir $BOOTFS_MNT || die "Failed to create $BOOTFS_MNT"
200
201
202#
161# Partition $DEVICE 203# Partition $DEVICE
162# 204#
163DEVICE_SIZE=$(parted $DEVICE unit mb print | grep ^Disk | cut -d" " -f 3 | sed -e "s/MB//") 205DEVICE_SIZE=$(parted $DEVICE unit mb print | grep ^Disk | cut -d" " -f 3 | sed -e "s/MB//")
164# If the device size is not reported there may not be a valid label 206# If the device size is not reported there may not be a valid label
165if [ "$DEVICE_SIZE" = "" ] ; then 207if [ "$DEVICE_SIZE" = "" ] ; then
166 parted $DEVICE mklabel msdos 208 parted $DEVICE mklabel msdos || die "Failed to create MSDOS partition table"
167 DEVICE_SIZE=$(parted $DEVICE unit mb print | grep ^Disk | cut -d" " -f 3 | sed -e "s/MB//") 209 DEVICE_SIZE=$(parted $DEVICE unit mb print | grep ^Disk | cut -d" " -f 3 | sed -e "s/MB//")
168fi 210fi
169SWAP_SIZE=$((DEVICE_SIZE*SWAP_RATIO/100)) 211SWAP_SIZE=$((DEVICE_SIZE*SWAP_RATIO/100))
@@ -195,25 +237,25 @@ echo "Swap partition size: $SWAP_SIZE MB ($SWAP)"
195echo "*****************" 237echo "*****************"
196 238
197echo "Deleting partition table on $DEVICE ..." 239echo "Deleting partition table on $DEVICE ..."
198dd if=/dev/zero of=$DEVICE bs=512 count=2 240dd if=/dev/zero of=$DEVICE bs=512 count=2 > /dev/null || die "Failed to zero beginning of $DEVICE"
199 241
200# Use MSDOS by default as GPT cannot be reliably distributed in disk image form 242# Use MSDOS by default as GPT cannot be reliably distributed in disk image form
201# as it requires the backup table to be on the last block of the device, which 243# as it requires the backup table to be on the last block of the device, which
202# of course varies from device to device. 244# of course varies from device to device.
203echo "Creating new partition table (MSDOS) on $DEVICE ..." 245echo "Creating new partition table (MSDOS) on $DEVICE ..."
204parted $DEVICE mklabel msdos 246parted $DEVICE mklabel msdos || die "Failed to create MSDOS partition table"
205 247
206echo "Creating boot partition on $BOOTFS" 248echo "Creating boot partition on $BOOTFS"
207parted $DEVICE mkpart primary 0% $BOOT_SIZE 249parted $DEVICE mkpart primary 0% $BOOT_SIZE || die "Failed to create BOOT partition"
208 250
209echo "Enabling boot flag on $BOOTFS" 251echo "Enabling boot flag on $BOOTFS"
210parted $DEVICE set 1 boot on 252parted $DEVICE set 1 boot on || die "Failed to enable boot flag"
211 253
212echo "Creating ROOTFS partition on $ROOTFS" 254echo "Creating ROOTFS partition on $ROOTFS"
213parted $DEVICE mkpart primary $ROOTFS_START $ROOTFS_END 255parted $DEVICE mkpart primary $ROOTFS_START $ROOTFS_END || die "Failed to create ROOTFS partition"
214 256
215echo "Creating swap partition on $SWAP" 257echo "Creating swap partition on $SWAP"
216parted $DEVICE mkpart primary $SWAP_START 100% 258parted $DEVICE mkpart primary $SWAP_START 100% || die "Failed to create SWAP partition"
217 259
218parted $DEVICE print 260parted $DEVICE print
219 261
@@ -221,7 +263,7 @@ parted $DEVICE print
221# 263#
222# Check if any $DEVICE partitions are mounted after partitioning 264# Check if any $DEVICE partitions are mounted after partitioning
223# 265#
224unmount_device 266unmount_device || die "Failed to unmount $DEVICE partitions"
225 267
226 268
227# 269#
@@ -230,16 +272,16 @@ unmount_device
230echo "" 272echo ""
231echo "Formatting $BOOTFS as vfat..." 273echo "Formatting $BOOTFS as vfat..."
232if [ ! "${DEVICE#/dev/loop}" = "${DEVICE}" ]; then 274if [ ! "${DEVICE#/dev/loop}" = "${DEVICE}" ]; then
233 mkfs.vfat -I $BOOTFS -n "EFI" || error "Failed to format $BOOTFS" 275 mkfs.vfat -I $BOOTFS -n "EFI" || die "Failed to format $BOOTFS"
234else 276else
235 mkfs.vfat $BOOTFS -n "EFI" || error "Failed to format $BOOTFS" 277 mkfs.vfat $BOOTFS -n "EFI" || die "Failed to format $BOOTFS"
236fi 278fi
237 279
238echo "Formatting $ROOTFS as ext3..." 280echo "Formatting $ROOTFS as ext3..."
239mkfs.ext3 -F $ROOTFS -L "ROOT" || error "Failed to format $ROOTFS" 281mkfs.ext3 -F $ROOTFS -L "ROOT" || die "Failed to format $ROOTFS"
240 282
241echo "Formatting swap partition...($SWAP)" 283echo "Formatting swap partition...($SWAP)"
242mkswap $SWAP || error "Failed to prepare swap" 284mkswap $SWAP || die "Failed to prepare swap"
243 285
244 286
245# 287#
@@ -247,24 +289,8 @@ mkswap $SWAP || error "Failed to prepare swap"
247# 289#
248echo "" 290echo ""
249echo "Mounting images and device in preparation for installation..." 291echo "Mounting images and device in preparation for installation..."
250TMPDIR=$(mktemp -d mkefidisk-XXX)
251if [ $? -ne 0 ]; then
252 error "Failed to create temporary mounting directory."
253 exit 1
254fi
255HDDIMG_MNT=$TMPDIR/hddimg
256HDDIMG_ROOTFS_MNT=$TMPDIR/hddimg_rootfs
257ROOTFS_MNT=$TMPDIR/rootfs
258BOOTFS_MNT=$TMPDIR/bootfs
259mkdir $HDDIMG_MNT
260mkdir $HDDIMG_ROOTFS_MNT
261mkdir $ROOTFS_MNT
262mkdir $BOOTFS_MNT
263
264mount -o loop $HDDIMG $HDDIMG_MNT || error "Failed to mount $HDDIMG" 292mount -o loop $HDDIMG $HDDIMG_MNT || error "Failed to mount $HDDIMG"
265
266mount -o loop $HDDIMG_MNT/rootfs.img $HDDIMG_ROOTFS_MNT || error "Failed to mount rootfs.img" 293mount -o loop $HDDIMG_MNT/rootfs.img $HDDIMG_ROOTFS_MNT || error "Failed to mount rootfs.img"
267
268mount $ROOTFS $ROOTFS_MNT || error "Failed to mount $ROOTFS on $ROOTFS_MNT" 294mount $ROOTFS $ROOTFS_MNT || error "Failed to mount $ROOTFS on $ROOTFS_MNT"
269mount $BOOTFS $BOOTFS_MNT || error "Failed to mount $BOOTFS on $BOOTFS_MNT" 295mount $BOOTFS $BOOTFS_MNT || error "Failed to mount $BOOTFS on $BOOTFS_MNT"
270 296
@@ -278,9 +304,6 @@ if [ -d $ROOTFS_MNT/etc/udev/ ] ; then
278 echo "$TARGET_DEVICE" >> $ROOTFS_MNT/etc/udev/mount.blacklist 304 echo "$TARGET_DEVICE" >> $ROOTFS_MNT/etc/udev/mount.blacklist
279fi 305fi
280 306
281umount $ROOTFS_MNT || error "Failed to unmount $ROOTFS_MNT"
282umount $HDDIMG_ROOTFS_MNT || error "Failed to unmount $HDDIMG_ROOTFS_MNT"
283
284echo "Preparing boot partition..." 307echo "Preparing boot partition..."
285EFIDIR="$BOOTFS_MNT/EFI/BOOT" 308EFIDIR="$BOOTFS_MNT/EFI/BOOT"
286cp $HDDIMG_MNT/vmlinuz $BOOTFS_MNT || error "Failed to copy vmlinuz" 309cp $HDDIMG_MNT/vmlinuz $BOOTFS_MNT || error "Failed to copy vmlinuz"
@@ -330,13 +353,11 @@ fi
330 353
331# Ensure we have at least one EFI bootloader configured 354# Ensure we have at least one EFI bootloader configured
332if [ ! -e $GRUB_CFG ] && [ ! -e $GUMMI_CFG ]; then 355if [ ! -e $GRUB_CFG ] && [ ! -e $GUMMI_CFG ]; then
333 error "No EFI bootloader configuration found" 356 die "No EFI bootloader configuration found"
334fi 357fi
335 358
336umount $BOOTFS_MNT || error "Failed to unmount $BOOTFS_MNT" 359# Call cleanup to unmount devices and images and remove the TMPDIR
337umount $HDDIMG_MNT || error "Failed to unmount $HDDIMG_MNT" 360cleanup
338rm -rf $TMPDIR || error "Failed to cleanup $TMPDIR"
339sync
340 361
341if [ $WARNINGS -ne 0 ] && [ $ERRORS -eq 0 ]; then 362if [ $WARNINGS -ne 0 ] && [ $ERRORS -eq 0 ]; then
342 echo "${YELLOW}Installation completed with warnings${CLEAR}" 363 echo "${YELLOW}Installation completed with warnings${CLEAR}"