/[42-fs]/42.pl
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /42.pl

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 3 by dpavlin, Sun Jul 19 11:55:06 2009 UTC revision 4 by dpavlin, Sun Jul 19 13:08:33 2009 UTC
# Line 11  use Data::Dump qw/dump/; Line 11  use Data::Dump qw/dump/;
11  my $dev = '/dev/sdb';  my $dev = '/dev/sdb';
12  my $mnt = '/mnt/42';  my $mnt = '/mnt/42';
13    
14  my ( $init, $debug );  my ( $verbose, $init, $debug ) = ( 1 );
15    
16  GetOptions(  GetOptions(
17          'init!' => \$init,          'init!' => \$init,
# Line 19  GetOptions( Line 19  GetOptions(
19  ) or die "unknown options: $!";  ) or die "unknown options: $!";
20    
21    
22    sub size {
23            my $size = shift;
24            $size =~ s{MB}{};
25            $size;
26    }
27    
28  my @part;  my @part;
29    
30  sub partitions {  sub partitions {
31            @part = ();
32          open(my $parted, '-|', "parted -s $dev unit mb print free") || die "parted: $!";          open(my $parted, '-|', "parted -s $dev unit mb print free") || die "parted: $!";
33          while(<$parted>) {          while(<$parted>) {
34                  warn "## $_" if $debug;                  chomp;
                 if ( m{^\s+([\d\.]+)MB\s+([\d\.]+)MB\s+([\d\.]+)MB\s+Free Space} ) {  
                         $part[0] = [ $1, $2, $3, 'free' ];  
                 }  
                 next unless m{^\s+\d+\s+};  
35                  s{^\s+}{};                  s{^\s+}{};
36                  s{([\d\.]+)MB}{$1}g;                  next unless $_;
37                  my ( $nr, $start, $end, $size, undef ) = split(/\s+/, $_, 5);                  my @p = map { size($_) } split(/\s+/, $_ );
38                  $part[$nr] = [ $start, $end, $size ];                  warn "## $_ ",dump( @p ) if $debug;
39                    if ( $p[3] && $p[3] eq 'Free' ) {
40                            $part[0] = [ @p ];
41                    } elsif ( $p[0] =~ m{^\d+$} ) {
42                            my $nr = shift @p;
43                            $part[$nr] = [ @p ];
44                    } else {
45                            warn "SKIP ",dump( @p ) if $debug;
46                    }
47          }          }
48          warn "## part = ",dump( @part ) if $debug;          warn "# part = ",dump( @part ) if $verbose;
49          return @part;          return @part;
50  }  }
51    
52    sub mount_42 {
53            my $node = $dev . '1';
54            if ( ! -e $node ) {
55                    print STDERR "wait for $node";
56                    sleep 1;
57                    while ( ! -e $node ) {
58                            print STDERR ".";
59                            sleep 1;
60                    }
61            }
62            warn "+ mount $node $mnt";
63            system("mount $node $mnt") == 0 or die "can't mount: $!";
64    }
65    
66    my $remount_on = qr/(mkpart|rm)/;
67    
68  sub parted {  sub parted {
69          my $command = shift;          my $command = shift;
70            my @before = partitions();
71    
72            system "umount $mnt" if $command =~ $remount_on;
73    
74          warn "+ $command\n";          warn "+ $command\n";
75          system("parted -s $dev unit mb $command") == 0 or die "parted: $!";          system("parted -s $dev unit mb $command") == 0 or die "parted: $?";
76            if ( $command =~ $remount_on ) {
77                    my @part = partitions();
78                    while ( $#before == $#part ) {
79                            warn "re-read partition table\n";
80                            sleep 1;
81                            @part = partitions();
82                    }
83            mount_42 if $part[1];
84            }
85  }  }
86    
87    partitions();
88    
89  if ( $init ) {  if ( $init ) {
90          system("umount $mnt");          parted("rm $_") foreach grep { defined $part[$_] } map { $#part - $_ + 1 } ( 1 .. $#part );
         partitions();  
         parted("rm $_") foreach grep { defined $part[$_] } ( 1 .. 4 );  
91          parted("mkpartfs primary ext2 0 42MB");          parted("mkpartfs primary ext2 0 42MB");
         partitions();  
92          parted("mkpart extended 42MB " . $part[0]->[1] . 'MB');          parted("mkpart extended 42MB " . $part[0]->[1] . 'MB');
         system "sync;sync";  
         partitions();  
         system("mount ${dev}1 $mnt") == 0 or die "can't mount: $!";  
93          my $cmd = read_file($0);          my $cmd = read_file($0);
94          write_file( "$mnt/42.pl", $cmd );          write_file( "$mnt/42.pl", $cmd );
95          chmod 0755, "$mnt/42.pl";          chmod 0755, "$mnt/42.pl";
# Line 77  my @stat = stat($path); Line 112  my @stat = stat($path);
112  die "can't stat $path: $!" unless @stat;  die "can't stat $path: $!" unless @stat;
113  warn "# $path ",$stat[7],$/;  warn "# $path ",$stat[7],$/;
114    
115  partitions();  my $size_mb = int( $stat[7] / 1000 / 1000 ) + 1; # FIXME correctly round to something?
   
 my $size_mb = int( $stat[7] / 1000 / 1000 ) + 1;  
116  my ( $free_start, undef, $free_size ) = @{$part[0]};  my ( $free_start, undef, $free_size ) = @{$part[0]};
117    
118  my $part_end = $free_start + $size_mb;  my $part_end = $free_start + $size_mb;
119    
120  my $last_part = $#part;  my $last_part = $#part;
121    
122  system("umount $mnt");  parted("mkpart logical ${free_start}MB ${part_end}MB ");
 parted("mkpart logical ${free_start}MB ${part_end}MB");  
 partitions();  
 system("mount ${dev}1 $mnt") == 0 or die "can't mount $mnt: $!";  
123    
124  my $part_size = $part[$#part]->[2] || die "can't get size of new partition";  my $part_size = $part[$#part]->[2] || die "can't get size of new partition";
125    
# Line 97  die "not enough space on $dev $size_mb > Line 127  die "not enough space on $dev $size_mb >
127    
128  my $part_nr = $#part;  my $part_nr = $#part;
129    
 die "can't create partition $last_part == $part_nr" if $last_part == $part_nr;  
   
130  write_file( "$mnt/stat/$part_nr", join("\n",@stat) );  write_file( "$mnt/stat/$part_nr", join("\n",@stat) );
131    
132  my $log = "$mnt/log/$part_nr";  my $log = "$mnt/log/$part_nr";

Legend:
Removed from v.3  
changed lines
  Added in v.4

  ViewVC Help
Powered by ViewVC 1.1.26