/[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 6 by dpavlin, Sun Jul 19 14:31:08 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 $unit = 'MB';
15    
16    my ( $verbose, $skip, $init, $debug ) = ( 0, 1 );
17    
18  GetOptions(  GetOptions(
19          'init!' => \$init,          'verbose!' => \$verbose,
20          'debug!' => \$debug,          'skip!'    => \$skip,
21            'init!'    => \$init,
22            'debug!'   => \$debug,
23  ) or die "unknown options: $!";  ) or die "unknown options: $!";
24    
25    
26    sub size {
27            my $size = shift;
28            $size =~ s{$unit}{};
29            $size;
30    }
31    
32  my @part;  my @part;
33    
34  sub partitions {  sub partitions {
35          open(my $parted, '-|', "parted -s $dev unit mb print free") || die "parted: $!";          @part = ();
36            open(my $parted, '-|', "parted -s $dev unit $unit print free") || die "parted: $!";
37          while(<$parted>) {          while(<$parted>) {
38                  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+};  
39                  s{^\s+}{};                  s{^\s+}{};
40                  s{([\d\.]+)MB}{$1}g;                  next unless $_;
41                  my ( $nr, $start, $end, $size, undef ) = split(/\s+/, $_, 5);                  my @p = map { size($_) } split(/\s+/, $_ );
42                  $part[$nr] = [ $start, $end, $size ];                  warn "## $_ ",dump( @p ) if $debug;
43                    if ( $p[3] && $p[3] eq 'Free' ) {
44                            $part[0] = [ @p ];
45                    } elsif ( $p[0] =~ m{^\d+$} ) {
46                            my $nr = shift @p;
47                            $part[$nr] = [ @p ];
48                    } else {
49                            warn "SKIP ",dump( @p ) if $debug;
50                    }
51          }          }
52          warn "## part = ",dump( @part ) if $debug;          warn "# part = ",dump( @part ) if $verbose;
53          return @part;          return @part;
54  }  }
55    
56    sub mount_42 {
57            my $node = $dev . '1';
58            if ( ! -e $node ) {
59                    print STDERR "wait for $node";
60                    sleep 1;
61                    while ( ! -e $node ) {
62                            print STDERR ".";
63                            sleep 1;
64                    }
65                    print STDERR " ready\n";
66            }
67            warn "+ mount $node $mnt\n";
68            system("mount $node $mnt") == 0 or die "can't mount: $!";
69    }
70    
71    my $remount_on = qr/(mkpart|rm)/;
72    
73  sub parted {  sub parted {
74          my $command = shift;          my $command = shift;
75            my @before = partitions();
76    
77            system "umount $mnt" if $command =~ $remount_on;
78    
79          warn "+ $command\n";          warn "+ $command\n";
80          system("parted -s $dev unit mb $command") == 0 or die "parted: $!";          system("parted -s $dev unit $unit $command") == 0 or die "parted: $?";
81            if ( $command =~ $remount_on ) {
82                    my @part = partitions();
83                    while ( $#before == $#part ) {
84                            warn "re-read partition table\n";
85                            sleep 1;
86                            @part = partitions();
87                    }
88            mount_42 if $part[1];
89            }
90  }  }
91    
92    partitions();
93    
94  if ( $init ) {  if ( $init ) {
95            #parted("rm $_") foreach grep { defined $part[$_] } map { $#part - $_ + 1 } ( 1 .. $#part );
96          system("umount $mnt");          system("umount $mnt");
97          partitions();          parted("mklabel msdos");
98          parted("rm $_") foreach grep { defined $part[$_] } ( 1 .. 4 );  
99          parted("mkpartfs primary ext2 0 42MB");          parted("mkpartfs primary ext2 0M 42MB");
100          partitions();          parted("mkpart extended 42MB " . $part[0]->[1] . $unit);
101          parted("mkpart extended 42MB " . $part[0]->[1] . 'MB');  
         system "sync;sync";  
         partitions();  
         system("mount ${dev}1 $mnt") == 0 or die "can't mount: $!";  
102          my $cmd = read_file($0);          my $cmd = read_file($0);
103          write_file( "$mnt/42.pl", $cmd );          my $path = "$mnt/42.pl";
104          chmod 0755, "$mnt/42.pl";          write_file( $path, $cmd );
105            chmod 0755, $path;
106            print "created $path ",-s $path, " bytes\n";
107          exit;          exit;
108  }  }
109    
# Line 71  my $fs = "$mnt/fs/$dir"; Line 117  my $fs = "$mnt/fs/$dir";
117  mkpath $fs unless -e $fs;  mkpath $fs unless -e $fs;
118    
119  $fs .= '/' . $file;  $fs .= '/' . $file;
120  die "$fs exists!" if -e $fs;  if ( -e $fs ) {
121            if ( $skip ) {
122                    warn "SKIP $fs\n";
123                    exit 0;
124            } else {
125                    die "$fs exists! re-run with --skip\n";
126            }
127    }
128    
129  my @stat = stat($path);  my @stat = stat($path);
130  die "can't stat $path: $!" unless @stat;  die "can't stat $path: $!" unless @stat;
131  warn "# $path ",$stat[7],$/;  warn "# $path ",$stat[7],$/;
132    
133  partitions();  my $size = int( ( $stat[7] + 1024 ) / 1024 / 1024 );
134    $size += $size % 64;    # 16k allocation cluster
 my $size_mb = int( $stat[7] / 1000 / 1000 ) + 1;  
135  my ( $free_start, undef, $free_size ) = @{$part[0]};  my ( $free_start, undef, $free_size ) = @{$part[0]};
136    
137  my $part_end = $free_start + $size_mb;  my $part_end = $free_start + $size;
   
 my $last_part = $#part;  
138    
139  system("umount $mnt");  parted("mkpart logical $free_start$unit $part_end$unit ");
 parted("mkpart logical ${free_start}MB ${part_end}MB");  
 partitions();  
 system("mount ${dev}1 $mnt") == 0 or die "can't mount $mnt: $!";  
140    
141  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";
142    die "not enough space on $dev $size > $part_size" if $size > $part_size;
 die "not enough space on $dev $size_mb > $part_size" if $size_mb > $part_size;  
143    
144  my $part_nr = $#part;  my $part_nr = $#part;
145    
 die "can't create partition $last_part == $part_nr" if $last_part == $part_nr;  
   
146  write_file( "$mnt/stat/$part_nr", join("\n",@stat) );  write_file( "$mnt/stat/$part_nr", join("\n",@stat) );
147    
148  my $log = "$mnt/log/$part_nr";  my $log = "$mnt/log/$part_nr";

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

  ViewVC Help
Powered by ViewVC 1.1.26