The last few steps of livecd-iso-to-disk.sh usually take a long time
While waiting, one sees the message "Copying live image to USB stick"
without any indication of progress. It would be great if a progress
bar is displayed.

The attached patch implements a progress bar with ETA information. (I
was inspired by http://www.theiling.de/projects/bar.html for this
patch.) Hope everyone finds this useful.

Regards,
Swee Heng
From e1146b9d31879dd99440b418efcdb4357094df75 Mon Sep 17 00:00:00 2001
From: Tan Swee Heng <[EMAIL PROTECTED]>
Date: Thu, 20 Dec 2007 11:35:05 +0800
Subject: [PATCH] Added a progress bar to livecd-iso-to-disk.sh.


Signed-off-by: Tan Swee Heng <[EMAIL PROTECTED]>
---
 tools/livecd-iso-to-disk.sh |   62 +++++++++++++++++++++++++++++++++++++++---
 1 files changed, 57 insertions(+), 5 deletions(-)

diff --git a/tools/livecd-iso-to-disk.sh b/tools/livecd-iso-to-disk.sh
index b62792e..1edaf51 100644
--- a/tools/livecd-iso-to-disk.sh
+++ b/tools/livecd-iso-to-disk.sh
@@ -142,6 +142,58 @@ checkSyslinuxVersion() {
     fi
 }
 
+copy() { # src dst [width]
+    srcsize=$(stat -c %s $1) || return $?
+    dstsize=0
+    width=${3:-25}
+    mega=$(( 1024 * 1024 ))
+    start=$(date +%s)
+
+    cat $1 | (
+    while [[ dstsize -lt srcsize ]]
+    do
+       dd bs=512 count=2048 2>/dev/null || return $?
+
+       (( dstsize += $mega ))
+       [[ dstsize -gt srcsize ]] && dstsize=$srcsize
+
+       # print truncated filename
+       name=$(basename $1 | cut -b -20)
+       printf "\r%-20s " $name 1>&2
+
+       # print percentage
+       percent=$(( 100 * $dstsize / $srcsize ))
+       printf "%3d%% [" $percent 1>&2
+
+       # print progress bar
+       bar=$(( $width * $dstsize / $srcsize ))
+       for i in $(seq 1 $bar); do printf "=" 1>&2; done
+       for i in $(seq $bar $(( $width-1 ))); do printf " " 1>&2; done
+
+       # print size of file copied
+       if [[ $dstsize -le 1024 ]]; then
+           printf -v size "%d" $dstsize;
+       elif [[ $dstsize -le $mega ]]; then
+           printf -v size "%d kB" $(( $dstsize / 1024  ));
+       else
+           printf -v size "%d MB" $(( $dstsize / $mega ));
+       fi
+       printf "] %7s" "$size" 1>&2
+
+       # print estimated time of arrival
+       elapsed=$(( $(date +%s) - $start ))
+       remain=$(( $srcsize - $dstsize ))
+       eta=$(( ($elapsed * $remain) / $dstsize + 1))
+       if [[ $remain == 0 ]]; then eta=0; fi
+       etamin=$(( $eta / 60 ))
+       etasec=$(( $eta % 60 ))
+       if [[ $eta > 0 ]]; then etastr="ETA"; else etastr="   "; fi
+       printf "   %02d:%02d $etastr" $etamin $etasec 1>&2
+    done
+    echo 1>&2
+    ) | cat >$2
+}
+
 if [ $(id -u) != 0 ]; then 
     echo "You need to be root to run this script"
     exit 1
@@ -211,16 +263,16 @@ if [ ! -d $USBMNT/$SYSLINUXPATH ]; then mkdir 
$USBMNT/$SYSLINUXPATH ; fi
 if [ ! -d $USBMNT/LiveOS ]; then mkdir $USBMNT/LiveOS ; fi
 # cases without /LiveOS are legacy detection, remove for F10
 if [ -f $CDMNT/LiveOS/squashfs.img ]; then
-    cp $CDMNT/LiveOS/squashfs.img $USBMNT/LiveOS/squashfs.img || exitclean
+    copy $CDMNT/LiveOS/squashfs.img $USBMNT/LiveOS/squashfs.img || exitclean
 elif [ -f $CDMNT/squashfs.img ]; then
-    cp $CDMNT/squashfs.img $USBMNT/LiveOS/squashfs.img || exitclean 
+    copy $CDMNT/squashfs.img $USBMNT/LiveOS/squashfs.img || exitclean 
 elif [ -f $CDMNT/LiveOS/ext3fs.img ]; then
-    cp $CDMNT/LiveOS/ext3fs.img $USBMNT/LiveOS/ext3fs.img || exitclean
+    copy $CDMNT/LiveOS/ext3fs.img $USBMNT/LiveOS/ext3fs.img || exitclean
 elif [ -f $CDMNT/ext3fs.img ]; then
-    cp $CDMNT/ext3fs.img $USBMNT/LiveOS/ext3fs.img || exitclean 
+    copy $CDMNT/ext3fs.img $USBMNT/LiveOS/ext3fs.img || exitclean 
 fi
 if [ -f $CDMNT/osmin.img ]; then
-    cp $CDMNT/osmin.img $USBMNT/LiveOS/osmin.img || exitclean
+    copy $CDMNT/osmin.img $USBMNT/LiveOS/osmin.img || exitclean
 fi
 
 cp $CDMNT/isolinux/* $USBMNT/$SYSLINUXPATH
-- 
1.5.3.6

--
Fedora-livecd-list mailing list
[email protected]
https://www.redhat.com/mailman/listinfo/fedora-livecd-list

Reply via email to