1 |
root |
1.1 |
#!/usr/bin/perl |
2 |
|
|
|
3 |
|
|
# (c)2003 Marc Alexander Lehmann <nbd@plan9.de> |
4 |
|
|
|
5 |
|
|
use Getopt::Long; |
6 |
|
|
|
7 |
|
|
use Linux::NBD; |
8 |
|
|
use Linux::NBD::Client; |
9 |
|
|
use Linux::NBD::Server; |
10 |
|
|
|
11 |
|
|
Getopt::Long::Configure ("bundling", "no_ignore_case"); |
12 |
|
|
|
13 |
|
|
my $device = undef; |
14 |
|
|
my $image = undef; |
15 |
root |
1.3 |
my $offset = undef; |
16 |
|
|
my $size = undef; |
17 |
|
|
my $verbose = 0; |
18 |
root |
1.1 |
|
19 |
|
|
GetOptions ( |
20 |
|
|
"device|d=s" => \$device, |
21 |
|
|
"image|i=s" => \$image, |
22 |
root |
1.3 |
"offset|o=i" => \$offset, |
23 |
|
|
"size|s=i" => \$size, |
24 |
|
|
"verbose|v" => \$verbose, |
25 |
root |
1.1 |
"help|h|?" => sub { |
26 |
|
|
require Pod::Usage; |
27 |
|
|
Pod::Usage::pod2usage(-exitstatus => 0, verbose => 2); |
28 |
|
|
}, |
29 |
|
|
) or exit 1; |
30 |
|
|
|
31 |
|
|
defined $device || defined $image |
32 |
|
|
or die "You must specify one (or both) of the -d or -i switches. Use --help.\n"; |
33 |
|
|
|
34 |
|
|
# open an nbd device |
35 |
|
|
my $client = new Linux::NBD::Client device => $device; |
36 |
|
|
|
37 |
|
|
# clear it |
38 |
root |
1.2 |
$client->clear_queue; |
39 |
root |
1.1 |
$client->disconnect; |
40 |
|
|
$client->socket(undef); |
41 |
|
|
|
42 |
|
|
if (defined $image) { |
43 |
|
|
# now attach the file |
44 |
|
|
open IMAGE, "<", $image |
45 |
|
|
or die "$image: $!"; |
46 |
|
|
|
47 |
root |
1.3 |
unless (defined $offset and defined $size) { |
48 |
|
|
require v5.8; |
49 |
|
|
|
50 |
|
|
# guess |
51 |
|
|
sysread IMAGE, my $buf, 2352*100; |
52 |
|
|
# find two volume descriptors... there _should_ be two ;) |
53 |
|
|
# ISO has <type>CD001, we ignore high sierra |
54 |
|
|
$buf =~ /\001CD001/g or die "unable to guess: no volume descriptor found"; |
55 |
|
|
my $ofs1 = $-[0]; |
56 |
|
|
$buf =~ /[\001\002\377]CD001/g or die "unable to guess: no second volume descriptor found"; |
57 |
|
|
my $ofs2 = $-[0]; |
58 |
|
|
$size = $ofs2 - $ofs1; |
59 |
|
|
$offset = $ofs1 % $size; |
60 |
|
|
printf "\nGuessed blocksize %d and offset 0x%04x\n", $size, $offset; |
61 |
|
|
} |
62 |
|
|
|
63 |
root |
1.1 |
my ($a, $b) = Linux::NBD::tcp_socketpair; |
64 |
|
|
|
65 |
|
|
# cleanup cleanly. oops ;) |
66 |
|
|
$SIG{INT} = |
67 |
|
|
$SIG{TERM} = |
68 |
|
|
$SIG{QUIT} = |
69 |
|
|
$SIG{HUP} = sub { exit }; |
70 |
|
|
|
71 |
|
|
# set the socket and fork the client service process |
72 |
|
|
$client->set_blocksize(2048); |
73 |
|
|
$client->set_size(-s IMAGE); |
74 |
|
|
$client->socket($a); |
75 |
|
|
$client->run_async; |
76 |
|
|
|
77 |
|
|
$device = $client->device; |
78 |
|
|
|
79 |
|
|
print <<EOF; |
80 |
|
|
|
81 |
root |
1.2 |
Everything seems to be set up now, try something like: |
82 |
root |
1.1 |
|
83 |
root |
1.2 |
mount -tiso9660 $device /mnt |
84 |
root |
1.1 |
|
85 |
root |
1.2 |
Press ^C to exit... |
86 |
root |
1.1 |
EOF |
87 |
|
|
|
88 |
|
|
# now create a server instance and handle |
89 |
|
|
# the requests. |
90 |
|
|
|
91 |
|
|
my $server = Server->new(socket => $b); |
92 |
|
|
$server->run; |
93 |
|
|
} |
94 |
|
|
|
95 |
|
|
BEGIN { |
96 |
|
|
@Server::ISA = Linux::NBD::Server::; |
97 |
|
|
}; |
98 |
|
|
|
99 |
|
|
# implement only read requests (we emulate a primitive cdrom...) |
100 |
|
|
|
101 |
|
|
sub Server::req_read { |
102 |
|
|
my ($self, $handle, $ofs, $len) = @_; |
103 |
|
|
|
104 |
|
|
my ($ofs_l, $ofs_h) = ($ofs & 2047, $ofs >> 11); |
105 |
|
|
|
106 |
root |
1.3 |
printf "READ %08x \@%08x (block $ofs_h+$ofs_l)\n", $len, $ofs if $verbose > 0; |
107 |
root |
1.1 |
|
108 |
|
|
$self->reply($handle); |
109 |
|
|
|
110 |
|
|
while ($len) { |
111 |
root |
1.3 |
sysseek IMAGE, ($ofs_h * $size) + $ofs_l + $offset, 0 |
112 |
root |
1.1 |
or die "$image: $!"; |
113 |
|
|
|
114 |
|
|
$ofs_l = 2048 - $ofs_l; |
115 |
|
|
$ofs_l < $len or $ofs_l = $len; |
116 |
|
|
|
117 |
|
|
sysread IMAGE, my $buf, $ofs_l; |
118 |
|
|
|
119 |
|
|
$ofs_l == length $buf or $buf = "\x0" x $ofs_l; |
120 |
|
|
|
121 |
|
|
$self->send($buf); |
122 |
|
|
|
123 |
|
|
$len -= $ofs_l; |
124 |
|
|
|
125 |
|
|
$ofs_l = 0; |
126 |
|
|
$ofs_h++; |
127 |
|
|
} |
128 |
|
|
} |
129 |
|
|
|
130 |
|
|
=head1 NAME |
131 |
|
|
|
132 |
|
|
attach-bincue - attach a .bin (cdrom image) containing an iso9660 fs to nbd |
133 |
|
|
|
134 |
|
|
=head1 SYNOPSIS |
135 |
|
|
|
136 |
|
|
attach-bincue -i iso9660.bin & # start server |
137 |
|
|
mount -tiso9660 /dev/ndx /mnt |
138 |
|
|
|
139 |
|
|
# kill server |
140 |
|
|
umount /mnt |
141 |
|
|
attach-bincue -d /dev/ndx # disconnect server |
142 |
|
|
|
143 |
|
|
=head1 DESCRIPTION |
144 |
|
|
|
145 |
|
|
With the C<-i> switch, attaches a C<.bin> file (containing an iso 9660 |
146 |
|
|
filesystem image in the bin/cue "fileformat") to a nbd instance and serve |
147 |
|
|
requests. |
148 |
|
|
|
149 |
|
|
Otherwise the nbd device specified with C<-d> is sent a disconnect |
150 |
|
|
request, usually causing it to exit cleanly. |
151 |
|
|
|
152 |
|
|
=head1 ARGUMENTS |
153 |
|
|
|
154 |
|
|
=over 4 |
155 |
|
|
|
156 |
|
|
=item --image binfile |
157 |
|
|
|
158 |
|
|
Attached the given file to the network block device and serve requests. The command will only |
159 |
|
|
exit on disconnect or any fatal errors. |
160 |
|
|
|
161 |
root |
1.3 |
=item --offset bytes |
162 |
|
|
|
163 |
|
|
The ISO image is offset by the given number of bytes. The default is to guess. Common values are C<24>, C<8> and C<0>. |
164 |
|
|
|
165 |
|
|
=item --size blocksize |
166 |
|
|
|
167 |
|
|
The blocksize in bytes. The default is to guess. Common values are C<2352>, C<2332> and C<2340>. |
168 |
|
|
|
169 |
root |
1.1 |
=item --device /dev/ndx |
170 |
|
|
|
171 |
|
|
The nbd device node to use. C<attach-bincue> automatically looks for a |
172 |
|
|
unused one if this switch isn't given. |
173 |
|
|
|
174 |
root |
1.3 |
=item --verbose |
175 |
|
|
|
176 |
|
|
Increase verbosity. With this switch, read requests will be logged to stdout. |
177 |
|
|
|
178 |
root |
1.1 |
=back |
179 |
root |
1.3 |
|
180 |
|
|
=head1 EXAMPLES |
181 |
|
|
|
182 |
|
|
Common combinations of offset and blocksize |
183 |
|
|
|
184 |
|
|
attach-bincue -b 2352 -o 24 |
185 |
|
|
attach-bincue -b 2332 -o 8 |
186 |
|
|
attach-bincue -b 2340 -o 0 |
187 |
root |
1.1 |
|
188 |
|
|
=head1 AUTHOR |
189 |
|
|
|
190 |
|
|
Marc Lehmann <nbd@plan9.de>. |
191 |
|
|
|