ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Linux-NBD/bin/attach-bincue
Revision: 1.6
Committed: Fri Aug 29 23:31:55 2003 UTC (20 years, 8 months ago) by root
Branch: MAIN
CVS Tags: rel-1_0, rel-0_91, rel-0_9, HEAD
Changes since 1.5: +3 -3 lines
Log Message:
*** empty log message ***

File Contents

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