tool to copy the image to the blockdevice default tip
authorHeiko Schlittermann (JUMPER) <hs@schlittermann.de>
Tue, 02 Dec 2014 18:31:25 +0100
changeset 9 571516885a72
parent 8 2a3c9c60bed1
tool to copy the image to the blockdevice
cpi
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/cpi	Tue Dec 02 18:31:25 2014 +0100
@@ -0,0 +1,51 @@
+#! /usr/bin/perl
+use 5.014;
+use strict;
+use warnings;
+
+my $BS = 4 * 4096;
+
+die "Usage: $0 INFILE OUTDEV\n" unless @ARGV == 2;
+
+my ($inf, $outf) = @ARGV;
+
+my $in;
+
+if ($inf eq '-') { open($in, '<&STDIN') or die "$0: Can't open <&STDIN: $!\n" }
+else { open($in, '<', $inf) or die "$0: Can't open <$inf: $!\n" }
+
+open(my $out, '+<', $outf) or die "$0: Can't open +>$outf: $!\n";
+
+$/ = \$BS;
+
+{
+    my ($read, $skipped, $written) = (0, 0, 0);
+    my $t0 = time;
+
+while (defined ($_ = <$in>)) {
+    ++$read;
+
+    my $lastpos = tell $out;
+    my $block = <$out>;
+
+    if (defined $block and $block eq $_) {
+	++$skipped;
+	next;
+    }
+
+    seek($out, $lastpos, 0) or die "$0: Can't seek: $!\n";
+    print {$out} $_;
+
+    ++$written;
+
+} continue {
+    my $dt = time - $t0;
+    printf "\rskipped: %5.1fM, written: %5.1fM written, speed: %5.1fM/s", 
+	    $skipped * $BS / 2**20, 
+	    $written * $BS / 2**20, 
+	    $dt ?  $read * $BS / 2**20 / $dt : 0;
+}
+
+    say "\nDONE";
+
+}