ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Linux-NBD/bin/attach-bincue
Revision: 1.4
Committed: Fri Jun 6 08:26:57 2003 UTC (21 years ago) by root
Branch: MAIN
Changes since 1.3: +19 -6 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
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 root 1.4 my $mount = undef;
18 root 1.3 my $verbose = 0;
19 root 1.1
20     GetOptions (
21     "device|d=s" => \$device,
22     "image|i=s" => \$image,
23 root 1.3 "offset|o=i" => \$offset,
24     "size|s=i" => \$size,
25     "verbose|v" => \$verbose,
26 root 1.4 "mount|m=s" => \$mount,
27 root 1.1 "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 root 1.2 $client->clear_queue;
41 root 1.1 $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 root 1.3 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/g or die "unable to guess: no volume descriptor found";
57     my $ofs1 = $-[0];
58     $buf =~ /[\001\002\377]CD001/g or die "unable to guess: no second volume descriptor found";
59     my $ofs2 = $-[0];
60     $size = $ofs2 - $ofs1;
61     $offset = $ofs1 % $size;
62     printf "\nGuessed blocksize %d and offset 0x%04x\n", $size, $offset;
63     }
64    
65 root 1.1 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 root 1.4 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 root 1.1
89 root 1.4 Everything seems to be set up now, try something like:
90 root 1.1
91 root 1.4 mount -tiso9660 $device /mnt
92 root 1.1
93 root 1.4 Press ^C to exit...
94 root 1.1 EOF
95 root 1.4 }
96 root 1.1
97     # now create a server instance and handle
98     # the requests.
99    
100     my $server = Server->new(socket => $b);
101     $server->run;
102 root 1.4 # never returns
103 root 1.1 }
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 root 1.3 printf "READ %08x \@%08x (block $ofs_h+$ofs_l)\n", $len, $ofs if $verbose > 0;
117 root 1.1
118     $self->reply($handle);
119    
120     while ($len) {
121 root 1.3 sysseek IMAGE, ($ofs_h * $size) + $ofs_l + $offset, 0
122 root 1.1 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 root 1.4 attach-bincue -i iso9660.bin -m /mnt & # start server
147 root 1.1
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     =head1 ARGUMENTS
162    
163     =over 4
164    
165     =item --image binfile
166    
167     Attached the given file to the network block device and serve requests. The command will only
168     exit on disconnect or any fatal errors.
169    
170 root 1.3 =item --offset bytes
171    
172     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>.
173    
174     =item --size blocksize
175    
176     The blocksize in bytes. The default is to guess. Common values are C<2352>, C<2332> and C<2340>.
177    
178 root 1.1 =item --device /dev/ndx
179    
180     The nbd device node to use. C<attach-bincue> automatically looks for a
181     unused one if this switch isn't given.
182    
183 root 1.3 =item --verbose
184    
185     Increase verbosity. With this switch, read requests will be logged to stdout.
186 root 1.4
187     =item --mount path
188    
189     Try to mount the CD as iso image on the given path.
190 root 1.3
191 root 1.1 =back
192 root 1.3
193     =head1 EXAMPLES
194    
195     Common combinations of offset and blocksize
196    
197     attach-bincue -b 2352 -o 24
198     attach-bincue -b 2332 -o 8
199     attach-bincue -b 2340 -o 0
200 root 1.1
201     =head1 AUTHOR
202    
203     Marc Lehmann <nbd@plan9.de>.
204