3 # Convert disk image or file to nbdkit-data-plugin command line.
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions are
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
38 disk2data.pl - convert disk image or file to nbdkit-data-plugin command line
46 =head2 Create a 1 MB MBR-partitioned disk command line
50 $ echo start=1 | sfdisk disk
53 The above command will print out the full nbdkit command to use,
57 @0x1b8 202 127 39 107 0 0 0 0 2 0 131 32 32 0 1 0 0 0 255 7
61 =head2 Create a 1 GB GPT-partitioned disk command line
68 A command similar to this is printed:
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
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.
100 L<nbdkit-data-plugin(1)>,
115 die "$0: expecting a single disk image parameter\n" unless @ARGV == 1;
118 my $size = (stat ($disk))[7];
120 open FH
, "<:raw", $disk or die "$0: $disk: $!\n";
122 print "nbdkit data data=\"\n ";
128 while ($offset < $size) {
129 my $old_offset = $offset;
132 # Find the next non-zero data.
133 while ($r = read FH
, $c, 1) {
135 last unless $c eq "\0";
137 die "$0: $disk: read: $!" unless defined $r;
142 # Go back one character to unconsume the !\0.
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;
151 for (my $i = 0; $i < $d; ++$i) {
156 emit
(sprintf ('@0x%x', $offset));
159 # Look for short-period repeated data.
161 my $old_offset = $offset;
162 $r = read FH
, $c, 2*$max_period;
164 seek FH
, $old_offset, 0;
165 if ($r == 2*$max_period) {
168 for ($period = 1; $period <= $max_period; ++$period) {
169 my $pattern = substr ($c, 0, $period);
170 if ($pattern eq substr ($c, $period, $period)) {
172 while ($r = read FH
, $c, $period) {
173 if ($c ne $pattern) {
180 die if $repeats <= 1;
182 emit
(sprintf ("%d*%d", ord ($pattern), $repeats));
186 for (my $i = 0; $i < $period; ++$i) {
187 emit
(sprintf ("%d", ord (substr ($pattern, $i, 1))));
189 emit
(sprintf (")*%d", $repeats));
198 # Emit non-zero data.
199 while ($r = read FH
, $c, 1) {
202 emit
(sprintf ("%d", ord ($c)));
204 die "$0: $disk: read: $!" unless defined $r;
209 # Go back one character to unconsume the \0.
217 # It would be possible to be smarter about when to print the size, but
218 # it's safest and simplest to always print it.
219 print "\" size=$size\n";
224 my $is_offset = substr ($s, 0, 1) eq "@";
227 # This means we prefer to start an offset on a new line if the
228 # line is already over half way across.
230 if ($is_offset) { $limit = 40 } else { $limit = 72 }
232 if ($col + $n + 1 > $limit) {