diff options
author | Darren Hart <dvhart@linux.intel.com> | 2014-07-16 14:16:01 +0000 |
---|---|---|
committer | Richard Purdie <richard.purdie@linuxfoundation.org> | 2014-07-19 00:09:00 +0100 |
commit | fd8a230e1acda64e7cd1ad4fb7668b9c58b9228a (patch) | |
tree | cb4fde12588cef05cccd931a36f45f3717c1a301 | |
parent | da682642bae01e9717e5afdad4db62f4c7f406db (diff) | |
download | poky-fd8a230e1acda64e7cd1ad4fb7668b9c58b9228a.tar.gz |
mkefidisk.sh: Improve logging
Add 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.
(From OE-Core rev: b5a3f6465a7fd8e821b81da053bf7e11535f1652)
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 | 92 |
1 files changed, 60 insertions, 32 deletions
diff --git a/scripts/contrib/mkefidisk.sh b/scripts/contrib/mkefidisk.sh index 98c43f5d87..530b7842bb 100755 --- a/scripts/contrib/mkefidisk.sh +++ b/scripts/contrib/mkefidisk.sh | |||
@@ -28,6 +28,29 @@ BOOT_SIZE=20 | |||
28 | # 5% for swap | 28 | # 5% for swap |
29 | SWAP_RATIO=5 | 29 | SWAP_RATIO=5 |
30 | 30 | ||
31 | # Logging routines | ||
32 | WARNINGS=0 | ||
33 | ERRORS=0 | ||
34 | CLEAR="$(tput sgr0)" | ||
35 | INFO="$(tput bold)" | ||
36 | RED="$(tput setaf 1)$(tput bold)" | ||
37 | GREEN="$(tput setaf 2)$(tput bold)" | ||
38 | YELLOW="$(tput setaf 3)$(tput bold)" | ||
39 | info() { | ||
40 | echo "${INFO}$1${CLEAR}" | ||
41 | } | ||
42 | error() { | ||
43 | ERRORS=$((ERRORS+1)) | ||
44 | echo "${RED}$1${CLEAR}" | ||
45 | } | ||
46 | warn() { | ||
47 | WARNINGS=$((WARNINGS+1)) | ||
48 | echo "${YELLOW}$1${CLEAR}" | ||
49 | } | ||
50 | success() { | ||
51 | echo "${GREEN}$1${CLEAR}" | ||
52 | } | ||
53 | |||
31 | usage() { | 54 | usage() { |
32 | echo "Usage: $(basename $0) DEVICE HDDIMG TARGET_DEVICE" | 55 | echo "Usage: $(basename $0) DEVICE HDDIMG TARGET_DEVICE" |
33 | echo " DEVICE: The device to write the image to, e.g. /dev/sdh" | 56 | echo " DEVICE: The device to write the image to, e.g. /dev/sdh" |
@@ -37,7 +60,7 @@ usage() { | |||
37 | 60 | ||
38 | image_details() { | 61 | image_details() { |
39 | IMG=$1 | 62 | IMG=$1 |
40 | echo "Image details" | 63 | info "Image details" |
41 | echo "=============" | 64 | echo "=============" |
42 | echo " image: $(stat --printf '%N\n' $IMG)" | 65 | echo " image: $(stat --printf '%N\n' $IMG)" |
43 | echo " size: $(stat -L --printf '%s bytes\n' $IMG)" | 66 | echo " size: $(stat -L --printf '%s bytes\n' $IMG)" |
@@ -50,7 +73,7 @@ device_details() { | |||
50 | DEV=$1 | 73 | DEV=$1 |
51 | BLOCK_SIZE=512 | 74 | BLOCK_SIZE=512 |
52 | 75 | ||
53 | echo "Device details" | 76 | info "Device details" |
54 | echo "==============" | 77 | echo "==============" |
55 | echo " device: $DEVICE" | 78 | echo " device: $DEVICE" |
56 | if [ -f "/sys/class/block/$DEV/device/vendor" ]; then | 79 | if [ -f "/sys/class/block/$DEV/device/vendor" ]; then |
@@ -74,14 +97,13 @@ device_details() { | |||
74 | unmount_device() { | 97 | unmount_device() { |
75 | grep -q $DEVICE /proc/mounts | 98 | grep -q $DEVICE /proc/mounts |
76 | if [ $? -eq 0 ]; then | 99 | if [ $? -eq 0 ]; then |
77 | echo -n "$DEVICE listed in /proc/mounts, attempting to unmount..." | 100 | warn "$DEVICE listed in /proc/mounts, attempting to unmount..." |
78 | umount $DEVICE* 2>/dev/null | 101 | umount $DEVICE* 2>/dev/null |
79 | grep -q $DEVICE /proc/mounts | 102 | grep -q $DEVICE /proc/mounts |
80 | if [ $? -eq 0 ]; then | 103 | if [ $? -eq 0 ]; then |
81 | echo "FAILED" | 104 | error "Failed to unmount $DEVICE" |
82 | exit 1 | 105 | exit 1 |
83 | fi | 106 | fi |
84 | echo "OK" | ||
85 | fi | 107 | fi |
86 | } | 108 | } |
87 | 109 | ||
@@ -104,13 +126,13 @@ if [ $? -eq 0 ]; then | |||
104 | fi | 126 | fi |
105 | 127 | ||
106 | if [ ! -w "$DEVICE" ]; then | 128 | if [ ! -w "$DEVICE" ]; then |
107 | echo "ERROR: Device $DEVICE does not exist or is not writable" | 129 | error "Device $DEVICE does not exist or is not writable" |
108 | usage | 130 | usage |
109 | exit 1 | 131 | exit 1 |
110 | fi | 132 | fi |
111 | 133 | ||
112 | if [ ! -e "$HDDIMG" ]; then | 134 | if [ ! -e "$HDDIMG" ]; then |
113 | echo "ERROR: HDDIMG $HDDIMG does not exist" | 135 | error "HDDIMG $HDDIMG does not exist" |
114 | usage | 136 | usage |
115 | exit 1 | 137 | exit 1 |
116 | fi | 138 | fi |
@@ -127,7 +149,7 @@ unmount_device | |||
127 | # | 149 | # |
128 | image_details $HDDIMG | 150 | image_details $HDDIMG |
129 | device_details $(basename $DEVICE) | 151 | device_details $(basename $DEVICE) |
130 | echo -n "Prepare EFI image on $DEVICE [y/N]? " | 152 | echo -n "${INFO}Prepare EFI image on $DEVICE [y/N]?${CLEAR} " |
131 | read RESPONSE | 153 | read RESPONSE |
132 | if [ "$RESPONSE" != "y" ]; then | 154 | if [ "$RESPONSE" != "y" ]; then |
133 | echo "Image creation aborted" | 155 | echo "Image creation aborted" |
@@ -208,17 +230,16 @@ unmount_device | |||
208 | echo "" | 230 | echo "" |
209 | echo "Formatting $BOOTFS as vfat..." | 231 | echo "Formatting $BOOTFS as vfat..." |
210 | if [ ! "${DEVICE#/dev/loop}" = "${DEVICE}" ]; then | 232 | if [ ! "${DEVICE#/dev/loop}" = "${DEVICE}" ]; then |
211 | mkfs.vfat -I $BOOTFS -n "EFI" | 233 | mkfs.vfat -I $BOOTFS -n "EFI" || error "Failed to format $BOOTFS" |
212 | else | 234 | else |
213 | mkfs.vfat $BOOTFS -n "EFI" | 235 | mkfs.vfat $BOOTFS -n "EFI" || error "Failed to format $BOOTFS" |
214 | |||
215 | fi | 236 | fi |
216 | 237 | ||
217 | echo "Formatting $ROOTFS as ext3..." | 238 | echo "Formatting $ROOTFS as ext3..." |
218 | mkfs.ext3 -F $ROOTFS -L "ROOT" | 239 | mkfs.ext3 -F $ROOTFS -L "ROOT" || error "Failed to format $ROOTFS" |
219 | 240 | ||
220 | echo "Formatting swap partition...($SWAP)" | 241 | echo "Formatting swap partition...($SWAP)" |
221 | mkswap $SWAP | 242 | mkswap $SWAP || error "Failed to prepare swap" |
222 | 243 | ||
223 | 244 | ||
224 | # | 245 | # |
@@ -228,7 +249,7 @@ echo "" | |||
228 | echo "Mounting images and device in preparation for installation..." | 249 | echo "Mounting images and device in preparation for installation..." |
229 | TMPDIR=$(mktemp -d mkefidisk-XXX) | 250 | TMPDIR=$(mktemp -d mkefidisk-XXX) |
230 | if [ $? -ne 0 ]; then | 251 | if [ $? -ne 0 ]; then |
231 | echo "ERROR: Failed to create temporary mounting directory." | 252 | error "Failed to create temporary mounting directory." |
232 | exit 1 | 253 | exit 1 |
233 | fi | 254 | fi |
234 | HDDIMG_MNT=$TMPDIR/hddimg | 255 | HDDIMG_MNT=$TMPDIR/hddimg |
@@ -240,17 +261,15 @@ mkdir $HDDIMG_ROOTFS_MNT | |||
240 | mkdir $ROOTFS_MNT | 261 | mkdir $ROOTFS_MNT |
241 | mkdir $BOOTFS_MNT | 262 | mkdir $BOOTFS_MNT |
242 | 263 | ||
243 | mount -o loop $HDDIMG $HDDIMG_MNT | 264 | mount -o loop $HDDIMG $HDDIMG_MNT || error "Failed to mount $HDDIMG" |
244 | if [ $? -ne 0 ]; then echo "ERROR: Failed to mount $HDDIMG"; fi | ||
245 | 265 | ||
246 | mount -o loop $HDDIMG_MNT/rootfs.img $HDDIMG_ROOTFS_MNT | 266 | mount -o loop $HDDIMG_MNT/rootfs.img $HDDIMG_ROOTFS_MNT || error "Failed to mount rootfs.img" |
247 | if [ $? -ne 0 ]; then echo "ERROR: Failed to mount rootfs.img"; fi | ||
248 | 267 | ||
249 | mount $ROOTFS $ROOTFS_MNT | 268 | mount $ROOTFS $ROOTFS_MNT || error "Failed to mount $ROOTFS on $ROOTFS_MNT" |
250 | mount $BOOTFS $BOOTFS_MNT | 269 | mount $BOOTFS $BOOTFS_MNT || error "Failed to mount $BOOTFS on $BOOTFS_MNT" |
251 | 270 | ||
252 | echo "Copying ROOTFS files..." | 271 | echo "Copying ROOTFS files..." |
253 | cp -a $HDDIMG_ROOTFS_MNT/* $ROOTFS_MNT | 272 | cp -a $HDDIMG_ROOTFS_MNT/* $ROOTFS_MNT || error "Root FS copy failed" |
254 | 273 | ||
255 | echo "$TARGET_SWAP swap swap defaults 0 0" >> $ROOTFS_MNT/etc/fstab | 274 | echo "$TARGET_SWAP swap swap defaults 0 0" >> $ROOTFS_MNT/etc/fstab |
256 | 275 | ||
@@ -259,14 +278,14 @@ if [ -d $ROOTFS_MNT/etc/udev/ ] ; then | |||
259 | echo "$TARGET_DEVICE" >> $ROOTFS_MNT/etc/udev/mount.blacklist | 278 | echo "$TARGET_DEVICE" >> $ROOTFS_MNT/etc/udev/mount.blacklist |
260 | fi | 279 | fi |
261 | 280 | ||
262 | umount $ROOTFS_MNT | 281 | umount $ROOTFS_MNT || error "Failed to unmount $ROOTFS_MNT" |
263 | umount $HDDIMG_ROOTFS_MNT | 282 | umount $HDDIMG_ROOTFS_MNT || error "Failed to unmount $HDDIMG_ROOTFS_MNT" |
264 | 283 | ||
265 | echo "Preparing boot partition..." | 284 | echo "Preparing boot partition..." |
266 | EFIDIR="$BOOTFS_MNT/EFI/BOOT" | 285 | EFIDIR="$BOOTFS_MNT/EFI/BOOT" |
267 | cp $HDDIMG_MNT/vmlinuz $BOOTFS_MNT | 286 | cp $HDDIMG_MNT/vmlinuz $BOOTFS_MNT || error "Failed to copy vmlinuz" |
268 | # Copy the efi loader and configs (booti*.efi and grub.cfg if it exists) | 287 | # Copy the efi loader and configs (booti*.efi and grub.cfg if it exists) |
269 | cp -r $HDDIMG_MNT/EFI $BOOTFS_MNT | 288 | cp -r $HDDIMG_MNT/EFI $BOOTFS_MNT || error "Failed to copy EFI dir" |
270 | # Silently ignore a missing gummiboot loader dir (we might just be a GRUB image) | 289 | # Silently ignore a missing gummiboot loader dir (we might just be a GRUB image) |
271 | cp -r $HDDIMG_MNT/loader $BOOTFS_MNT 2> /dev/null | 290 | cp -r $HDDIMG_MNT/loader $BOOTFS_MNT 2> /dev/null |
272 | 291 | ||
@@ -281,7 +300,7 @@ cp -r $HDDIMG_MNT/loader $BOOTFS_MNT 2> /dev/null | |||
281 | # Look for a GRUB installation | 300 | # Look for a GRUB installation |
282 | GRUB_CFG="$EFIDIR/grub.cfg" | 301 | GRUB_CFG="$EFIDIR/grub.cfg" |
283 | if [ -e "$GRUB_CFG" ]; then | 302 | if [ -e "$GRUB_CFG" ]; then |
284 | echo "Configuring GRUB" | 303 | info "Configuring GRUB" |
285 | # Delete the install entry | 304 | # Delete the install entry |
286 | sed -i "/menuentry 'install'/,/^}/d" $GRUB_CFG | 305 | sed -i "/menuentry 'install'/,/^}/d" $GRUB_CFG |
287 | # Delete the initrd lines | 306 | # Delete the initrd lines |
@@ -297,7 +316,7 @@ fi | |||
297 | GUMMI_ENTRIES="$BOOTFS_MNT/loader/entries" | 316 | GUMMI_ENTRIES="$BOOTFS_MNT/loader/entries" |
298 | GUMMI_CFG="$GUMMI_ENTRIES/boot.conf" | 317 | GUMMI_CFG="$GUMMI_ENTRIES/boot.conf" |
299 | if [ -d "$GUMMI_ENTRIES" ]; then | 318 | if [ -d "$GUMMI_ENTRIES" ]; then |
300 | echo "Configuring Gummiboot" | 319 | info "Configuring Gummiboot" |
301 | # remove the install target if it exists | 320 | # remove the install target if it exists |
302 | rm $GUMMI_ENTRIES/install.conf &> /dev/null | 321 | rm $GUMMI_ENTRIES/install.conf &> /dev/null |
303 | 322 | ||
@@ -311,12 +330,21 @@ fi | |||
311 | 330 | ||
312 | # Ensure we have at least one EFI bootloader configured | 331 | # Ensure we have at least one EFI bootloader configured |
313 | if [ ! -e $GRUB_CFG ] && [ ! -e $GUMMI_CFG ]; then | 332 | if [ ! -e $GRUB_CFG ] && [ ! -e $GUMMI_CFG ]; then |
314 | echo "ERROR: No EFI bootloader configuration found" | 333 | error "No EFI bootloader configuration found" |
315 | fi | 334 | fi |
316 | 335 | ||
317 | umount $BOOTFS_MNT | 336 | umount $BOOTFS_MNT || error "Failed to unmount $BOOTFS_MNT" |
318 | umount $HDDIMG_MNT | 337 | umount $HDDIMG_MNT || error "Failed to unmount $HDDIMG_MNT" |
319 | rm -rf $TMPDIR | 338 | rm -rf $TMPDIR || error "Failed to cleanup $TMPDIR" |
320 | sync | 339 | sync |
321 | 340 | ||
322 | echo "Installation complete" | 341 | if [ $WARNINGS -ne 0 ] && [ $ERRORS -eq 0 ]; then |
342 | echo "${YELLOW}Installation completed with warnings${CLEAR}" | ||
343 | echo "${YELLOW}Warnings: $WARNINGS${CLEAR}" | ||
344 | elif [ $ERRORS -ne 0 ]; then | ||
345 | echo "${RED}Installation encountered errors${CLEAR}" | ||
346 | echo "${RED}Errors: $ERRORS${CLEAR}" | ||
347 | echo "${YELLOW}Warnings: $WARNINGS${CLEAR}" | ||
348 | else | ||
349 | success "Installation completed successfully" | ||
350 | fi | ||