diff options
author | Darren Hart <dvhart@linux.intel.com> | 2014-07-16 14:16:02 +0000 |
---|---|---|
committer | Richard Purdie <richard.purdie@linuxfoundation.org> | 2014-07-19 00:09:00 +0100 |
commit | db162c6f564218b678b68f72e1dd4cdab3c3110f (patch) | |
tree | 26774807fe065890cba345ac6de3c1fcef8d1c41 | |
parent | fd8a230e1acda64e7cd1ad4fb7668b9c58b9228a (diff) | |
download | poky-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>
-rwxr-xr-x | scripts/contrib/mkefidisk.sh | 115 |
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 |
29 | SWAP_RATIO=5 | 29 | SWAP_RATIO=5 |
30 | 30 | ||
31 | # Cleanup after die() | ||
32 | cleanup() { | ||
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 |
32 | WARNINGS=0 | 48 | WARNINGS=0 |
33 | ERRORS=0 | 49 | ERRORS=0 |
@@ -50,6 +66,11 @@ warn() { | |||
50 | success() { | 66 | success() { |
51 | echo "${GREEN}$1${CLEAR}" | 67 | echo "${GREEN}$1${CLEAR}" |
52 | } | 68 | } |
69 | die() { | ||
70 | error $1 | ||
71 | cleanup | ||
72 | exit 1 | ||
73 | } | ||
53 | 74 | ||
54 | usage() { | 75 | usage() { |
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 | ||
128 | unmount() { | ||
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 | |||
126 | fi | 153 | fi |
127 | 154 | ||
128 | if [ ! -w "$DEVICE" ]; then | 155 | if [ ! -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" |
132 | fi | 158 | fi |
133 | 159 | ||
134 | if [ ! -e "$HDDIMG" ]; then | 160 | if [ ! -e "$HDDIMG" ]; then |
135 | error "HDDIMG $HDDIMG does not exist" | ||
136 | usage | 161 | usage |
137 | exit 1 | 162 | die "HDDIMG $HDDIMG does not exist" |
138 | fi | 163 | fi |
139 | 164 | ||
165 | # | ||
166 | # Ensure the hddimg is not mounted | ||
167 | # | ||
168 | unmount "$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 | # |
144 | unmount_device | 173 | unmount_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 | # | ||
191 | TMPDIR=$(mktemp -d mkefidisk-XXX) || die "Failed to create temporary mounting directory." | ||
192 | HDDIMG_MNT=$TMPDIR/hddimg | ||
193 | HDDIMG_ROOTFS_MNT=$TMPDIR/hddimg_rootfs | ||
194 | ROOTFS_MNT=$TMPDIR/rootfs | ||
195 | BOOTFS_MNT=$TMPDIR/bootfs | ||
196 | mkdir $HDDIMG_MNT || die "Failed to create $HDDIMG_MNT" | ||
197 | mkdir $HDDIMG_ROOTFS_MNT || die "Failed to create $HDDIMG_ROOTFS_MNT" | ||
198 | mkdir $ROOTFS_MNT || die "Failed to create $ROOTFS_MNT" | ||
199 | mkdir $BOOTFS_MNT || die "Failed to create $BOOTFS_MNT" | ||
200 | |||
201 | |||
202 | # | ||
161 | # Partition $DEVICE | 203 | # Partition $DEVICE |
162 | # | 204 | # |
163 | DEVICE_SIZE=$(parted $DEVICE unit mb print | grep ^Disk | cut -d" " -f 3 | sed -e "s/MB//") | 205 | DEVICE_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 |
165 | if [ "$DEVICE_SIZE" = "" ] ; then | 207 | if [ "$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//") |
168 | fi | 210 | fi |
169 | SWAP_SIZE=$((DEVICE_SIZE*SWAP_RATIO/100)) | 211 | SWAP_SIZE=$((DEVICE_SIZE*SWAP_RATIO/100)) |
@@ -195,25 +237,25 @@ echo "Swap partition size: $SWAP_SIZE MB ($SWAP)" | |||
195 | echo "*****************" | 237 | echo "*****************" |
196 | 238 | ||
197 | echo "Deleting partition table on $DEVICE ..." | 239 | echo "Deleting partition table on $DEVICE ..." |
198 | dd if=/dev/zero of=$DEVICE bs=512 count=2 | 240 | dd 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. |
203 | echo "Creating new partition table (MSDOS) on $DEVICE ..." | 245 | echo "Creating new partition table (MSDOS) on $DEVICE ..." |
204 | parted $DEVICE mklabel msdos | 246 | parted $DEVICE mklabel msdos || die "Failed to create MSDOS partition table" |
205 | 247 | ||
206 | echo "Creating boot partition on $BOOTFS" | 248 | echo "Creating boot partition on $BOOTFS" |
207 | parted $DEVICE mkpart primary 0% $BOOT_SIZE | 249 | parted $DEVICE mkpart primary 0% $BOOT_SIZE || die "Failed to create BOOT partition" |
208 | 250 | ||
209 | echo "Enabling boot flag on $BOOTFS" | 251 | echo "Enabling boot flag on $BOOTFS" |
210 | parted $DEVICE set 1 boot on | 252 | parted $DEVICE set 1 boot on || die "Failed to enable boot flag" |
211 | 253 | ||
212 | echo "Creating ROOTFS partition on $ROOTFS" | 254 | echo "Creating ROOTFS partition on $ROOTFS" |
213 | parted $DEVICE mkpart primary $ROOTFS_START $ROOTFS_END | 255 | parted $DEVICE mkpart primary $ROOTFS_START $ROOTFS_END || die "Failed to create ROOTFS partition" |
214 | 256 | ||
215 | echo "Creating swap partition on $SWAP" | 257 | echo "Creating swap partition on $SWAP" |
216 | parted $DEVICE mkpart primary $SWAP_START 100% | 258 | parted $DEVICE mkpart primary $SWAP_START 100% || die "Failed to create SWAP partition" |
217 | 259 | ||
218 | parted $DEVICE print | 260 | parted $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 | # |
224 | unmount_device | 266 | unmount_device || die "Failed to unmount $DEVICE partitions" |
225 | 267 | ||
226 | 268 | ||
227 | # | 269 | # |
@@ -230,16 +272,16 @@ unmount_device | |||
230 | echo "" | 272 | echo "" |
231 | echo "Formatting $BOOTFS as vfat..." | 273 | echo "Formatting $BOOTFS as vfat..." |
232 | if [ ! "${DEVICE#/dev/loop}" = "${DEVICE}" ]; then | 274 | if [ ! "${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" |
234 | else | 276 | else |
235 | mkfs.vfat $BOOTFS -n "EFI" || error "Failed to format $BOOTFS" | 277 | mkfs.vfat $BOOTFS -n "EFI" || die "Failed to format $BOOTFS" |
236 | fi | 278 | fi |
237 | 279 | ||
238 | echo "Formatting $ROOTFS as ext3..." | 280 | echo "Formatting $ROOTFS as ext3..." |
239 | mkfs.ext3 -F $ROOTFS -L "ROOT" || error "Failed to format $ROOTFS" | 281 | mkfs.ext3 -F $ROOTFS -L "ROOT" || die "Failed to format $ROOTFS" |
240 | 282 | ||
241 | echo "Formatting swap partition...($SWAP)" | 283 | echo "Formatting swap partition...($SWAP)" |
242 | mkswap $SWAP || error "Failed to prepare swap" | 284 | mkswap $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 | # |
248 | echo "" | 290 | echo "" |
249 | echo "Mounting images and device in preparation for installation..." | 291 | echo "Mounting images and device in preparation for installation..." |
250 | TMPDIR=$(mktemp -d mkefidisk-XXX) | ||
251 | if [ $? -ne 0 ]; then | ||
252 | error "Failed to create temporary mounting directory." | ||
253 | exit 1 | ||
254 | fi | ||
255 | HDDIMG_MNT=$TMPDIR/hddimg | ||
256 | HDDIMG_ROOTFS_MNT=$TMPDIR/hddimg_rootfs | ||
257 | ROOTFS_MNT=$TMPDIR/rootfs | ||
258 | BOOTFS_MNT=$TMPDIR/bootfs | ||
259 | mkdir $HDDIMG_MNT | ||
260 | mkdir $HDDIMG_ROOTFS_MNT | ||
261 | mkdir $ROOTFS_MNT | ||
262 | mkdir $BOOTFS_MNT | ||
263 | |||
264 | mount -o loop $HDDIMG $HDDIMG_MNT || error "Failed to mount $HDDIMG" | 292 | mount -o loop $HDDIMG $HDDIMG_MNT || error "Failed to mount $HDDIMG" |
265 | |||
266 | mount -o loop $HDDIMG_MNT/rootfs.img $HDDIMG_ROOTFS_MNT || error "Failed to mount rootfs.img" | 293 | mount -o loop $HDDIMG_MNT/rootfs.img $HDDIMG_ROOTFS_MNT || error "Failed to mount rootfs.img" |
267 | |||
268 | mount $ROOTFS $ROOTFS_MNT || error "Failed to mount $ROOTFS on $ROOTFS_MNT" | 294 | mount $ROOTFS $ROOTFS_MNT || error "Failed to mount $ROOTFS on $ROOTFS_MNT" |
269 | mount $BOOTFS $BOOTFS_MNT || error "Failed to mount $BOOTFS on $BOOTFS_MNT" | 295 | mount $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 |
279 | fi | 305 | fi |
280 | 306 | ||
281 | umount $ROOTFS_MNT || error "Failed to unmount $ROOTFS_MNT" | ||
282 | umount $HDDIMG_ROOTFS_MNT || error "Failed to unmount $HDDIMG_ROOTFS_MNT" | ||
283 | |||
284 | echo "Preparing boot partition..." | 307 | echo "Preparing boot partition..." |
285 | EFIDIR="$BOOTFS_MNT/EFI/BOOT" | 308 | EFIDIR="$BOOTFS_MNT/EFI/BOOT" |
286 | cp $HDDIMG_MNT/vmlinuz $BOOTFS_MNT || error "Failed to copy vmlinuz" | 309 | cp $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 |
332 | if [ ! -e $GRUB_CFG ] && [ ! -e $GUMMI_CFG ]; then | 355 | if [ ! -e $GRUB_CFG ] && [ ! -e $GUMMI_CFG ]; then |
333 | error "No EFI bootloader configuration found" | 356 | die "No EFI bootloader configuration found" |
334 | fi | 357 | fi |
335 | 358 | ||
336 | umount $BOOTFS_MNT || error "Failed to unmount $BOOTFS_MNT" | 359 | # Call cleanup to unmount devices and images and remove the TMPDIR |
337 | umount $HDDIMG_MNT || error "Failed to unmount $HDDIMG_MNT" | 360 | cleanup |
338 | rm -rf $TMPDIR || error "Failed to cleanup $TMPDIR" | ||
339 | sync | ||
340 | 361 | ||
341 | if [ $WARNINGS -ne 0 ] && [ $ERRORS -eq 0 ]; then | 362 | if [ $WARNINGS -ne 0 ] && [ $ERRORS -eq 0 ]; then |
342 | echo "${YELLOW}Installation completed with warnings${CLEAR}" | 363 | echo "${YELLOW}Installation completed with warnings${CLEAR}" |