--- 42.pl 2009/07/19 13:33:51 5 +++ 42.pl 2009/07/19 14:31:08 6 @@ -11,6 +11,8 @@ my $dev = '/dev/sdb'; my $mnt = '/mnt/42'; +my $unit = 'MB'; + my ( $verbose, $skip, $init, $debug ) = ( 0, 1 ); GetOptions( @@ -23,7 +25,7 @@ sub size { my $size = shift; - $size =~ s{MB}{}; + $size =~ s{$unit}{}; $size; } @@ -31,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+}{}; @@ -60,8 +62,9 @@ print STDERR "."; sleep 1; } + print STDERR " ready\n"; } - warn " ready\n+ mount $node $mnt\n"; + warn "+ mount $node $mnt\n"; system("mount $node $mnt") == 0 or die "can't mount: $!"; } @@ -74,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 ) { @@ -89,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; } @@ -121,18 +130,16 @@ die "can't stat $path: $!" unless @stat; warn "# $path ",$stat[7],$/; -my $size_mb = $stat[7] / 1000 / 1000; # 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 = int( $free_start + $size_mb + 1 ); +my $part_end = $free_start + $size; -my $last_part = $#part; - -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;