11 |
my $dev = '/dev/sdb'; |
my $dev = '/dev/sdb'; |
12 |
my $mnt = '/mnt/42'; |
my $mnt = '/mnt/42'; |
13 |
|
|
14 |
|
my $unit = 'MB'; |
15 |
|
|
16 |
my ( $verbose, $skip, $init, $debug ) = ( 0, 1 ); |
my ( $verbose, $skip, $init, $debug ) = ( 0, 1 ); |
17 |
|
|
18 |
GetOptions( |
GetOptions( |
25 |
|
|
26 |
sub size { |
sub size { |
27 |
my $size = shift; |
my $size = shift; |
28 |
$size =~ s{MB}{}; |
$size =~ s{$unit}{}; |
29 |
$size; |
$size; |
30 |
} |
} |
31 |
|
|
33 |
|
|
34 |
sub partitions { |
sub partitions { |
35 |
@part = (); |
@part = (); |
36 |
open(my $parted, '-|', "parted -s $dev unit mb print free") || die "parted: $!"; |
open(my $parted, '-|', "parted -s $dev unit $unit print free") || die "parted: $!"; |
37 |
while(<$parted>) { |
while(<$parted>) { |
38 |
chomp; |
chomp; |
39 |
s{^\s+}{}; |
s{^\s+}{}; |
62 |
print STDERR "."; |
print STDERR "."; |
63 |
sleep 1; |
sleep 1; |
64 |
} |
} |
65 |
|
print STDERR " ready\n"; |
66 |
} |
} |
67 |
warn " ready\n+ mount $node $mnt\n"; |
warn "+ mount $node $mnt\n"; |
68 |
system("mount $node $mnt") == 0 or die "can't mount: $!"; |
system("mount $node $mnt") == 0 or die "can't mount: $!"; |
69 |
} |
} |
70 |
|
|
77 |
system "umount $mnt" if $command =~ $remount_on; |
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 ) { |
if ( $command =~ $remount_on ) { |
82 |
my @part = partitions(); |
my @part = partitions(); |
83 |
while ( $#before == $#part ) { |
while ( $#before == $#part ) { |
92 |
partitions(); |
partitions(); |
93 |
|
|
94 |
if ( $init ) { |
if ( $init ) { |
95 |
parted("rm $_") foreach grep { defined $part[$_] } map { $#part - $_ + 1 } ( 1 .. $#part ); |
#parted("rm $_") foreach grep { defined $part[$_] } map { $#part - $_ + 1 } ( 1 .. $#part ); |
96 |
parted("mkpartfs primary ext2 0 42MB"); |
system("umount $mnt"); |
97 |
parted("mkpart extended 42MB " . $part[0]->[1] . 'MB'); |
parted("mklabel msdos"); |
98 |
|
|
99 |
|
parted("mkpartfs primary ext2 0M 42MB"); |
100 |
|
parted("mkpart extended 42MB " . $part[0]->[1] . $unit); |
101 |
|
|
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 |
|
|
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 |
my $size_mb = $stat[7] / 1000 / 1000; # FIXME correctly round to something? |
my $size = int( ( $stat[7] + 1024 ) / 1024 / 1024 ); |
134 |
|
$size += $size % 64; # 16k allocation cluster |
135 |
my ( $free_start, undef, $free_size ) = @{$part[0]}; |
my ( $free_start, undef, $free_size ) = @{$part[0]}; |
136 |
|
|
137 |
my $part_end = int( $free_start + $size_mb + 1 ); |
my $part_end = $free_start + $size; |
138 |
|
|
139 |
my $last_part = $#part; |
parted("mkpart logical $free_start$unit $part_end$unit "); |
|
|
|
|
parted("mkpart logical ${free_start}MB ${part_end}MB "); |
|
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 |
|
|