--- 42.pl 2009/07/19 13:08:33 4 +++ 42.pl 2009/07/19 14:31:08 6 @@ -11,17 +11,21 @@ my $dev = '/dev/sdb'; my $mnt = '/mnt/42'; -my ( $verbose, $init, $debug ) = ( 1 ); +my $unit = 'MB'; + +my ( $verbose, $skip, $init, $debug ) = ( 0, 1 ); GetOptions( - 'init!' => \$init, - 'debug!' => \$debug, + 'verbose!' => \$verbose, + 'skip!' => \$skip, + 'init!' => \$init, + 'debug!' => \$debug, ) or die "unknown options: $!"; sub size { my $size = shift; - $size =~ s{MB}{}; + $size =~ s{$unit}{}; $size; } @@ -29,7 +33,7 @@ sub partitions { @part = (); - open(my $parted, '-|', "parted -s $dev unit mb print free") || die "parted: $!"; + open(my $parted, '-|', "parted -s $dev unit $unit print free") || die "parted: $!"; while(<$parted>) { chomp; s{^\s+}{}; @@ -58,8 +62,9 @@ print STDERR "."; sleep 1; } + print STDERR " ready\n"; } - warn "+ mount $node $mnt"; + warn "+ mount $node $mnt\n"; system("mount $node $mnt") == 0 or die "can't mount: $!"; } @@ -72,7 +77,7 @@ system "umount $mnt" if $command =~ $remount_on; warn "+ $command\n"; - system("parted -s $dev unit mb $command") == 0 or die "parted: $?"; + system("parted -s $dev unit $unit $command") == 0 or die "parted: $?"; if ( $command =~ $remount_on ) { my @part = partitions(); while ( $#before == $#part ) { @@ -87,12 +92,18 @@ partitions(); if ( $init ) { - parted("rm $_") foreach grep { defined $part[$_] } map { $#part - $_ + 1 } ( 1 .. $#part ); - parted("mkpartfs primary ext2 0 42MB"); - parted("mkpart extended 42MB " . $part[0]->[1] . 'MB'); + #parted("rm $_") foreach grep { defined $part[$_] } map { $#part - $_ + 1 } ( 1 .. $#part ); + system("umount $mnt"); + parted("mklabel msdos"); + + parted("mkpartfs primary ext2 0M 42MB"); + parted("mkpart extended 42MB " . $part[0]->[1] . $unit); + my $cmd = read_file($0); - write_file( "$mnt/42.pl", $cmd ); - chmod 0755, "$mnt/42.pl"; + my $path = "$mnt/42.pl"; + write_file( $path, $cmd ); + chmod 0755, $path; + print "created $path ",-s $path, " bytes\n"; exit; } @@ -106,24 +117,29 @@ mkpath $fs unless -e $fs; $fs .= '/' . $file; -die "$fs exists!" if -e $fs; +if ( -e $fs ) { + if ( $skip ) { + warn "SKIP $fs\n"; + exit 0; + } else { + die "$fs exists! re-run with --skip\n"; + } +} my @stat = stat($path); die "can't stat $path: $!" unless @stat; warn "# $path ",$stat[7],$/; -my $size_mb = int( $stat[7] / 1000 / 1000 ) + 1; # FIXME correctly round to something? +my $size = int( ( $stat[7] + 1024 ) / 1024 / 1024 ); +$size += $size % 64; # 16k allocation cluster my ( $free_start, undef, $free_size ) = @{$part[0]}; -my $part_end = $free_start + $size_mb; - -my $last_part = $#part; +my $part_end = $free_start + $size; -parted("mkpart logical ${free_start}MB ${part_end}MB "); +parted("mkpart logical $free_start$unit $part_end$unit "); my $part_size = $part[$#part]->[2] || die "can't get size of new partition"; - -die "not enough space on $dev $size_mb > $part_size" if $size_mb > $part_size; +die "not enough space on $dev $size > $part_size" if $size > $part_size; my $part_nr = $#part;