summaryrefslogtreecommitdiffstats
path: root/perl-install/fsedit.pm
blob: befe118d33c44429b241a96efa0fdcccfc750834 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
package fsedit;

use diagnostics;
use strict;

#-######################################################################################
#- misc imports
#-######################################################################################
use common qw(:common :constant :functional :file);
use partition_table qw(:types);
use partition_table_raw;
use detect_devices;
use Data::Dumper;
use fsedit;
use devices;
use fs;
use log;

#-#####################################################################################
#- Globals
#-#####################################################################################
my @suggestions = (
  { mntpoint => "/boot",    minsize =>  10 << 11, size =>  16 << 11, type => 0x83 },
  { mntpoint => "/",        minsize =>  50 << 11, size => 100 << 11, type => 0x83 },
  { mntpoint => "swap",     minsize =>  30 << 11, size =>  60 << 11, type => 0x82 },
  { mntpoint => "/usr",     minsize => 200 << 11, size => 600 << 11, type => 0x83 },
  { mntpoint => "/home",    minsize =>  50 << 11, size => 200 << 11, type => 0x83 },
  { mntpoint => "/var",     minsize => 200 << 11, size => 250 << 11, type => 0x83 },
  { mntpoint => "/tmp",     minsize =>  50 << 11, size => 100 << 11, type => 0x83 },
  { mntpoint => "/mnt/iso", minsize => 700 << 11, size => 800 << 11, type => 0x83 },
);
my @suggestions_mntpoints = qw(/mnt/dos);


my @partitions_signatures = (
    [ 0x83, 0x438, "\x53\xEF" ],
    [ 0x82, 4086, "SWAP-SPACE" ],
    [ 0xc,  0x1FE, "\x55\xAA", 0x52, "FAT32" ],
    [ 0x6,  0x1FE, "\x55\xAA", 0x36, "FAT" ],
);

sub typeOfPart($) { typeFromMagic(devices::make($_[0]), @partitions_signatures) }


sub typeOfPart_($) { typeFromMagic($_[0], @partitions_signatures) }

#-######################################################################################
#- Functions
#-######################################################################################
sub suggestions_mntpoint($) {
    my ($hds) = @_;
    sort grep { !/swap/ && !has_mntpoint($_, $hds) }
      (@suggestions_mntpoints, map { $_->{mntpoint} } @suggestions);
}

sub hds($$) {
    my ($drives, $flags) = @_;
    my @hds;
    my $rc;

    foreach (@$drives) {
	my $file = devices::make($_->{device});

	my $hd = partition_table_raw::get_geometry($file) or log::l("An error occurred while getting the geometry of block device $file: $!"), next;
	$hd = { (%$_, %$hd) };
	$hd->{file} = $file;
	$hd->{prefix} = $hd->{device};
	# for RAID arrays of format c0d0p1
	$hd->{prefix} .= "p" if $hd->{prefix} =~ m,(rd|ida)/,;

	eval { partition_table::read($hd, $flags->{clearall}) };
	if ($@) {
	    cdie($@) unless $flags->{eraseBadPartitions};
	    partition_table_raw::zero_MBR($hd);
	}
	push @hds, $hd;
    }
    [ @hds ];
}

sub readProcPartitions {
    my ($hds) = @_;
    my @parts;
    foreach (cat_("/proc/partitions")) {
	my (undef, undef, $size, $device) = split;
	next if $size eq "1"; #- extended partitions
	foreach (@$hds) {
	    push @parts, { start => 0, size => $size * 2, device => $device, 
			   type => typeOfPart($device), rootDevice => $_->{device} 
			 } if $device =~ /^$_->{device}./;
	}
    }
    @parts;
}

sub get_fstab(@) {
    map { partition_table::get_normal_parts($_) } @_;
}

sub get_root($) {
    my ($fstab) = @_;
    $_->{mntpoint} eq "/" and return $_ foreach @$fstab;
    undef;
}
sub get_root_ { get_root([ get_fstab(@{$_[0]}) ]) }

sub suggest_part($$$;$) {
    my ($hd, $part, $hds, $suggestions) = @_;
    $suggestions ||= \@suggestions;
    foreach (@$suggestions) { $_->{minsize} ||= $_->{size} }

    my $has_swap = grep { isSwap($_) } get_fstab(@$hds);

    my ($best, $second) =
      grep { $part->{size} >= $_->{minsize} }
      grep { ! has_mntpoint($_->{mntpoint}, $hds) || isSwap($_) && !$has_swap }
	@$suggestions or return;

    $best = $second if
      $best->{mntpoint} eq '/boot' &&
      $part->{start} + $best->{minsize} > 1024 * partition_table::cylinder_size($hd); #- if the empty slot is beyond the 1024th cylinder, no use having /boot

    defined $best or return; #- sorry no suggestion :(

    $part->{mntpoint} = $best->{mntpoint};
    $part->{type} = $best->{type};
    $part->{size} = min($part->{size}, $best->{size});
    1;
}


#-sub partitionDrives {
#-
#-    my $cmd = "/sbin/fdisk";
#-    -x $cmd or $cmd = "/usr/bin/fdisk";
#-
#-    my $drives = findDrivesPresent() or die "You don't have any hard drives available! You probably forgot to configure a SCSI controller.";
#-
#-    foreach (@$drives) {
#-	 my $text = "/dev/" . $_->{device};
#-	 $text .= " - SCSI ID " . $_->{id} if $_->{device} =~ /^sd/;
#-	 $text .= " - Model " . $_->{info};
#-	 $text .= " array" if $_->{device} =~ /^c.d/;
#-
#-	 #- truncate at 50 columns for now
#-	 $text = substr $text, 0, 50;
#-    }
#-    #-TODO TODO
#-}


sub has_mntpoint($$) {
    my ($mntpoint, $hds) = @_;
    scalar grep { $mntpoint eq $_->{mntpoint} } get_fstab(@$hds);
}

#- do this before modifying $part->{mntpoint}
#- $part->{mntpoint} should not be used here, use $mntpoint instead
sub check_mntpoint {
    my ($mntpoint, $hd, $part, $hds) = @_;

    $mntpoint eq '' || isSwap($part) and return;

    local $_ = $mntpoint;
    m|^/| or die _("Mount points must begin with a leading /");
#-    m|(.)/$| and die "The mount point $_ is illegal.\nMount points may not end with a /";

    has_mntpoint($mntpoint, $hds) and die _("There is already a partition with mount point %s", $mntpoint);

    if ($part->{start} + $part->{size} > 1024 * partition_table::cylinder_size($hd)) {
	die "/boot ending on cylinder > 1024" if $mntpoint eq "/boot";
	die     "/ ending on cylinder > 1024" if $mntpoint eq "/" && !has_mntpoint("/boot", $hds);
    }
}

sub add($$$;$) {
    my ($hd, $part, $hds, $options) = @_;

    isSwap($part) ?
      ($part->{mntpoint} = 'swap') :
      $options->{force} || check_mntpoint($part->{mntpoint}, $hd, $part, $hds);

    partition_table::add($hd, $part, $options->{primaryOrExtended});
}

sub removeFromList($$$) {
    my ($start, $end, $list) = @_;
    my $err = "error in removeFromList: removing an non-free block";

    for (my $i = 0; $i < @$list; $i += 2) {
	$start < $list->[$i] and die $err;
	$start > $list->[$i + 1] and next;

	if ($start == $list->[$i]) {
	    $end > $list->[$i + 1] and die $err;
	    if ($end == $list->[$i + 1]) {
		#- the free block is just the same size, removing it
		splice(@$list, 0, 2);
	    } else {
		#- the free block now start just after this block
		$list->[$i] = $end;
	    }
	} else {
	    $end <= $list->[$i + 1] or die $err;
	    if ($end < $list->[$i + 1]) {
		splice(@$list, $i + 2, 0, $end, $list->[$i + 1]);
	    }
	    $list->[$i + 1] = $start; #- shorten the free block
	}
	return;
    }
}


sub allocatePartitions($$) {
    my ($hds, $to_add) = @_;
    my %free_sectors = map { $_->{device} => [1, $_->{totalsectors} ] } @$hds; #- first sector is always occupied by the MBR
    my $remove = sub { removeFromList($_[0]{start}, $_[0]->{start} + $_[0]->{size}, $free_sectors{$_[0]->{rootDevice}}) };
    my $success = 0;

    foreach (get_fstab(@$hds)) { &$remove($_); }

    FSTAB: foreach (@$to_add) {
	my %e = %$_;
	foreach my $hd (@$hds) {
	    my $v = $free_sectors{$hd->{device}};
	    for (my $i = 0; $i < @$v; $i += 2) {
		my $size = $v->[$i + 1] - $v->[$i];
		$e{size} > $size and next;

		if ($v->[$i] + $e{size} > 1024 * partition_table::cylinder_size($hd)) {
		    next if $e{mntpoint} eq "/boot" || 
		            $e{mntpoint} eq "/" && !has_mntpoint("/boot", $hds);
		}
		$e{start} = $v->[$i];
		$e{rootDevice} = $hd->{device};
		partition_table::adjustStartAndEnd($hd, \%e);
		&$remove(\%e);
		partition_table::add($hd, \%e);
		$success++;
		next FSTAB;
	    }
	}
	log::ld("can't allocate partition $e{mntpoint} of size $e{size}, not enough room");
    }
    $success;
}

sub auto_allocate($;$) {
    my ($hds, $suggestions) = @_;
    allocatePartitions($hds, [
			      grep { ! has_mntpoint($_->{mntpoint}, $hds) }
			      @{ $suggestions || \@suggestions }
			     ]);
    map { partition_table::assign_device_numbers($_) } @$hds;
}

sub undo_prepare($) {
    my ($hds) = @_;
    $Data::Dumper::Purity = 1;
    foreach (@$hds) {
	my @h = @{$_}{@partition_table::fields2save};
	push @{$_->{undo}}, Data::Dumper->Dump([\@h], ['$h']);
    }
}
sub undo_forget($) {
    my ($hds) = @_;
    pop @{$_->{undo}} foreach @$hds;
}

sub undo($) {
    my ($hds) = @_;
    foreach (@$hds) {
	my $h; eval pop @{$_->{undo}} || next;
	@{$_}{@partition_table::fields2save} = @$h;

	$_->{isDirty} = $_->{needKernelReread} = 1;
    }
}

sub move {
    my ($hd, $part, $hd2, $sector2) = @_;

    my $part2 = { %$part };
    $part2->{start} = $sector2;
    $part2->{size} += partition_table::cylinder_size($hd2) - 1;
    partition_table::remove($hd, $part);
    {
	local ($part2->{notFormatted}, $part2->{isFormatted}); #- do not allow partition::add to change this
	partition_table::add($hd2, $part2);
    }

    return if $part2->{notFormatted} && !$part2->{isFormatted} || $::testing;

    local (*F, *G);
    sysopen F, $hd->{file}, 0 or die '';
    sysopen G, $hd2->{file}, 2 or die _("Error opening %s for writing: %s", $hd2->{file}, "$!");

    my $base = $part->{start};
    my $base2 = $part2->{start};
    my $step = 1 << 10;
    if ($hd eq $hd2) {
	$part->{start} == $part2->{start} and return;
	$step = min($step, abs($part->{start} - $part2->{start}));

	if ($part->{start} < $part2->{start}) {
	    $base  += $part->{size} - $step;
	    $base2 += $part->{size} - $step;
	    $step = -$step;
	}
    }

    my $f = sub {
	c::lseek_sector(fileno(F), $base,  0) or die "seeking to sector $base failed on drive $hd->{device}";
	c::lseek_sector(fileno(G), $base2, 0) or die "seeking to sector $base2 failed on drive $hd2->{device}";

	my $buf;
	sysread F, $buf, $SECTORSIZE * abs($_[0]) or die '';
	syswrite G, $buf;
    };

    for (my $i = 0; $i < $part->{size} / abs($step); $i++, $base += $step, $base2 += $step) {
	&$f($step);
    }
    if (my $v = $part->{size} % abs($step) * sign($step)) {
	$base += $v;
	$base2 += $v;
	&$f($v);
    }
}

sub rescuept($) {
    my ($hd) = @_;
    my ($ext, @hd);

    my $dev = devices::make($hd->{device});
    open F, "rescuept $dev|";
    foreach (<F>) {
	my ($st, $si, $id) = /start=\s*(\d+),\s*size=\s*(\d+),\s*Id=\s*(\d+)/ or next;
	my $part = { start => $st, size => $si, type => hex($id) };
	if (isExtended($part)) {
	    $ext = $part;
	} else {
	    push @hd, $part;
	}
    }
    close F or die "rescuept failed";

    partition_table_raw::zero_MBR($hd);
    foreach (@hd) {
	my $b = partition_table::verifyInside($_, $ext);
	if ($b) {
	    $_->{start}--;
	    $_->{size}++;
	}
	local $b->{notFormatted};

	partition_table::add($hd, $_, ($b ? 'Extended' : 'Primary'), 1);
    }
}

sub verifyHds {
    my ($hds, $readonly, $ok) = @_;

    if (is_empty_array_ref($hds)) { #- no way
	die _("An error has occurred - no valid devices were found on which to create new filesystems. Please check your hardware for the cause of this problem");
    }

    my @parts = readProcPartitions($hds);
    $ok &&= @parts == listlength(get_fstab(@$hds));

    if ($readonly && !$ok) {
	log::l("using /proc/partitions as diskdrake failed :(");
	foreach my $hd (@$hds) {
	    partition_table_raw::zero_MBR($hd);
	    $hd->{primary} = { normal => [ grep { $hd->{device} eq $_->{rootDevice} } @parts ] };
	}
    }
    my $fstab = [ get_fstab(@$hds) ];
    if (is_empty_array_ref($fstab) && $readonly) {
	die _("You don't have any partitions!");
    }
    ($hds, $fstab, $ok);
}

#-######################################################################################
#- Wonderful perl :(
#-######################################################################################
1; #