tests: Avoid shell function named 'test'
[nbdkit/ericb.git] / plugins / data / disk2data.pl
blob0ece48688ddb4a88082d5db88ab402a66c372e1a
1 #!/usr/bin/env perl
2 # -*- perl -*-
3 # Convert disk image or file to nbdkit-data-plugin command line.
4 # Copyright (C) 2018 Red Hat Inc.
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions are
8 # met:
10 # * Redistributions of source code must retain the above copyright
11 # notice, this list of conditions and the following disclaimer.
13 # * Redistributions in binary form must reproduce the above copyright
14 # notice, this list of conditions and the following disclaimer in the
15 # documentation and/or other materials provided with the distribution.
17 # * Neither the name of Red Hat nor the names of its contributors may be
18 # used to endorse or promote products derived from this software without
19 # specific prior written permission.
21 # THIS SOFTWARE IS PROVIDED BY RED HAT AND CONTRIBUTORS ''AS IS'' AND
22 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
23 # THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
24 # PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL RED HAT OR
25 # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
28 # USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
29 # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 # OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 # SUCH DAMAGE.
34 =pod
36 =head1 NAME
38 disk2data.pl - convert disk image or file to nbdkit-data-plugin command line
40 =head1 SYNOPSIS
42 ./disk2data.pl DISK
44 =head1 EXAMPLE
46 =head2 Create a 1 MB MBR-partitioned disk command line
48 $ rm -f disk
49 $ truncate -s 1M disk
50 $ echo start=1 | sfdisk disk
51 $ ./disk2data.pl disk
53 The above command will print out the full nbdkit command to use,
54 similar to:
56 $ nbdkit data data="
57 @0x1b8 202 127 39 107 0 0 0 0 2 0 131 32 32 0 1 0 0 0 255 7
58 @0x1fe 85 170
59 " size=1048576
61 =head2 Create a 1 GB GPT-partitioned disk command line
63 $ rm -f disk
64 $ truncate -s 1G disk
65 $ sgdisk -n 1 disk
66 $ ./disk2data.pl disk
68 A command similar to this is printed:
70 $ nbdkit data data="
71 @0x1c0 2 0 238 138 8 130 1 0 0 0 255 255 31
72 @0x1fe 85 170 69 70 73 32 80 65 82 84 0 0 1 0 92 0 0 0 110 92 89 223
73 0 0 0 0 1 @0x220 255 255 31 @0x228 34
74 @0x230 222 255 31 @0x238 159 149 54 193 83 188 210 70 172 3 15 147
75 241 183 1 61 2 @0x250 128 0 0 0 128 0 0 0 193 75 104 199
76 @0x400 175 61 198 15 131 132 114 71 142 121 61 105 216 71 125 228 237
77 59 243 84 97 170 76 75 179 212 47 233 110 221 119 58 0 8
78 @0x428 222 255 31 @0x3fffbe00 175 61 198 15 131 132 114 71 142 121 61
79 105 216 71 125 228 237 59 243 84 97 170 76 75 179 212 47 233 110 221
80 119 58 0 8 @0x3fffbe28 222 255 31
81 @0x3ffffe00 69 70 73 32 80 65 82 84 0 0 1 0 92 0 0 0 178 45 163 71 0
82 0 0 0 255 255 31 @0x3ffffe20 1
83 @0x3ffffe28 34 @0x3ffffe30 222 255 31
84 @0x3ffffe38 159 149 54 193 83 188 210 70 172 3 15 147 241 183 1 61
85 223 255 31 @0x3ffffe50 128 0 0 0 128 0 0 0 193 75 104 199
86 " size=1073741824
88 =head1 DESCRIPTION
90 C<disk2data.pl> is a simple script which converts a disk image or file
91 to the C<data="..."> format used by L<nbdkit-data-plugin(1)>.
93 Most operating systems have command line size limits which are quite a
94 lot smaller than any desirable disk image, so specifying a large,
95 fully populated disk image on the command line is not be possible.
96 You can only use this with small or very sparse disk images.
98 =head1 SEE ALSO
100 L<nbdkit-data-plugin(1)>,
101 L<nbdkit(1)>.
103 =head1 AUTHORS
105 Richard W.M. Jones
107 =head1 COPYRIGHT
109 Copyright (C) 2018 Red Hat Inc.
111 =cut
113 use strict;
115 die "$0: expecting a single disk image parameter\n" unless @ARGV == 1;
117 my $disk = $ARGV[0];
118 my $size = (stat ($disk))[7];
120 open FH, $disk or die "$0: $disk: $!\n";
122 print "nbdkit data data=\"\n ";
123 my $col = 2;
125 my $offset = 0;
126 my $c;
128 while ($offset < $size) {
129 my $old_offset = $offset;
130 my $r;
132 # Find the next non-zero data.
133 while ($r = read FH, $c, 1) {
134 $offset++;
135 last unless $c eq "\0";
137 die "$0: $disk: read: $!" unless defined $r;
139 # End of the file?
140 last if $r == 0;
142 # Go back one character to unconsume the !\0.
143 seek FH, -1, 1;
144 $offset--;
146 # Did we move forwards in that loop? If so we must emit a new
147 # offset. But if we only moved forward a few bytes then emitting
148 # zeroes is more efficient.
149 my $d = $offset - $old_offset;
150 if ($d <= 4) {
151 for (my $i = 0; $i < $d; ++$i) {
152 emit ("0");
155 else {
156 emit (sprintf ('@0x%x', $offset));
159 # Emit non-zero data.
160 while ($r = read FH, $c, 1) {
161 $offset++;
162 last if $c eq "\0";
163 emit (sprintf ("%d", ord ($c)));
165 die "$0: $disk: read: $!" unless defined $r;
167 # End of the file?
168 last if $r == 0;
170 # Go back one character to unconsume the \0.
171 seek FH, -1, 1;
172 $offset--;
175 if ($col > 2) {
176 print "\n ";
178 # It would be possible to be smarter about when to print the size, but
179 # it's safest and simplest to always print it.
180 print "\" size=$size\n";
182 sub emit
184 my $s = shift;
185 my $is_offset = substr ($s, 0, 1) eq "@";
186 my $n = length $s;
188 # This means we prefer to start an offset on a new line if the
189 # line is already over half way across.
190 my $limit;
191 if ($is_offset) { $limit = 40 } else { $limit = 72 }
193 if ($col + $n + 1 > $limit) {
194 print "\n ";
195 $col = 2;
198 print $s, " ";
199 $col += $n + 1;