ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Linux-NBD/bin/attach-bincue
Revision: 1.1
Committed: Thu May 8 23:43:43 2003 UTC (23 years, 4 months ago) by root
Branch: MAIN
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    
16     GetOptions (
17     "device|d=s" => \$device,
18     "image|i=s" => \$image,
19     "help|h|?" => sub {
20     require Pod::Usage;
21     Pod::Usage::pod2usage(-exitstatus => 0, verbose => 2);
22     },
23     ) or exit 1;
24    
25     defined $device || defined $image
26     or die "You must specify one (or both) of the -d or -i switches. Use --help.\n";
27    
28     # open an nbd device
29     my $client = new Linux::NBD::Client device => $device;
30    
31     # clear it
32     $client->disconnect;
33     $client->clear_queue;
34     $client->socket(undef);
35    
36     if (defined $image) {
37     # now attach the file
38     open IMAGE, "<", $image
39     or die "$image: $!";
40    
41     my ($a, $b) = Linux::NBD::tcp_socketpair;
42    
43     # cleanup cleanly. oops ;)
44     $SIG{INT} =
45     $SIG{TERM} =
46     $SIG{QUIT} =
47     $SIG{HUP} = sub { exit };
48    
49     # set the socket and fork the client service process
50     $client->set_blocksize(2048);
51     $client->set_size(-s IMAGE);
52     $client->socket($a);
53     $client->run_async;
54    
55     $device = $client->device;
56    
57     print <<EOF;
58    
59     Everything seems to be set up now, try something like:
60    
61     mount -tiso9660 $device /mnt
62    
63     Press ^C to exit...
64    
65     EOF
66    
67     # now create a server instance and handle
68     # the requests.
69    
70     my $server = Server->new(socket => $b);
71     $server->run;
72     }
73    
74     BEGIN {
75     @Server::ISA = Linux::NBD::Server::;
76     };
77    
78     # implement only read requests (we emulate a primitive cdrom...)
79    
80     sub Server::req_read {
81     my ($self, $handle, $ofs, $len) = @_;
82    
83     my ($ofs_l, $ofs_h) = ($ofs & 2047, $ofs >> 11);
84    
85     printf "READ %08x \@%08x (block $ofs_h+$ofs_l)\n", $len, $ofs;
86    
87     $self->reply($handle);
88    
89     while ($len) {
90     sysseek IMAGE, ($ofs_h * 2352) + $ofs_l + 0x18, 0
91     or die "$image: $!";
92    
93     $ofs_l = 2048 - $ofs_l;
94     $ofs_l < $len or $ofs_l = $len;
95    
96     sysread IMAGE, my $buf, $ofs_l;
97    
98     $ofs_l == length $buf or $buf = "\x0" x $ofs_l;
99    
100     $self->send($buf);
101    
102     $len -= $ofs_l;
103    
104     $ofs_l = 0;
105     $ofs_h++;
106     }
107     }
108    
109     =head1 NAME
110    
111     attach-bincue - attach a .bin (cdrom image) containing an iso9660 fs to nbd
112    
113     =head1 SYNOPSIS
114    
115     attach-bincue -i iso9660.bin & # start server
116     mount -tiso9660 /dev/ndx /mnt
117    
118     # kill server
119     umount /mnt
120     attach-bincue -d /dev/ndx # disconnect server
121    
122     =head1 DESCRIPTION
123    
124     With the C<-i> switch, attaches a C<.bin> file (containing an iso 9660
125     filesystem image in the bin/cue "fileformat") to a nbd instance and serve
126     requests.
127    
128     Otherwise the nbd device specified with C<-d> is sent a disconnect
129     request, usually causing it to exit cleanly.
130    
131     =head1 ARGUMENTS
132    
133     =over 4
134    
135     =item --image binfile
136    
137     Attached the given file to the network block device and serve requests. The command will only
138     exit on disconnect or any fatal errors.
139    
140     =item --device /dev/ndx
141    
142     The nbd device node to use. C<attach-bincue> automatically looks for a
143     unused one if this switch isn't given.
144    
145     =back
146    
147     =head1 AUTHOR
148    
149     Marc Lehmann <nbd@plan9.de>.
150