diff options
Diffstat (limited to 'perl-install/partition_table')
| -rw-r--r-- | perl-install/partition_table/bsd.pm | 14 | ||||
| -rw-r--r-- | perl-install/partition_table/dmcrypt.pm | 41 | ||||
| -rw-r--r-- | perl-install/partition_table/dos.pm | 35 | ||||
| -rw-r--r-- | perl-install/partition_table/empty.pm | 8 | ||||
| -rw-r--r-- | perl-install/partition_table/gpt.pm | 273 | ||||
| -rw-r--r-- | perl-install/partition_table/lvm.pm | 38 | ||||
| -rw-r--r-- | perl-install/partition_table/mac.pm | 17 | ||||
| -rw-r--r-- | perl-install/partition_table/raw.pm | 155 | ||||
| -rw-r--r-- | perl-install/partition_table/readonly.pm | 20 | ||||
| -rw-r--r-- | perl-install/partition_table/sun.pm | 11 |
10 files changed, 298 insertions, 314 deletions
diff --git a/perl-install/partition_table/bsd.pm b/perl-install/partition_table/bsd.pm index 0e2421c25..55e598e42 100644 --- a/perl-install/partition_table/bsd.pm +++ b/perl-install/partition_table/bsd.pm @@ -1,4 +1,4 @@ -package partition_table::bsd; # $Id$ +package partition_table::bsd; # $Id: bsd.pm 228416 2007-09-18 10:32:56Z pixel $ use diagnostics; use strict; @@ -58,7 +58,10 @@ my $magic = 0x82564557; my $nb_primary = 8; my $offset = 0x40; -sub read($$) { + +sub use_pt_type { 1 } + +sub read_one { my ($hd, $sector) = @_; my $tmp; @@ -137,9 +140,10 @@ sub info { }; } -sub clear_raw { - my ($hd) = @_; - { raw => [ ({}) x $nb_primary ], info => info($hd) }; +sub initialize { + my ($class, $hd) = @_; + $hd->{primary} = { raw => [ ({}) x $nb_primary ], info => info($hd) }; + bless $hd, $class; } sub first_usable_sector { 2048 } diff --git a/perl-install/partition_table/dmcrypt.pm b/perl-install/partition_table/dmcrypt.pm new file mode 100644 index 000000000..3eae783c6 --- /dev/null +++ b/perl-install/partition_table/dmcrypt.pm @@ -0,0 +1,41 @@ +package partition_table::dmcrypt; # $Id: $ + +# dmcrypt on full disk + +use diagnostics; +use strict; + +our @ISA = qw(partition_table::readonly); + +use common; +use partition_table::readonly; +use fs::type; + +sub _parts { + my ($hd) = @_; + + my $part = { size => $hd->{totalsectors}, device => $hd->{device} }; + add2hash($part, fs::type::type_name2subpart('Encrypted')); + + require fs; + fs::get_major_minor([$part]); # to allow is_same_hd() in fs::dmcrypt + + [ $part ]; +} + +sub read_primary { + my ($hd) = @_; + + my $type = fs::type::type_subpart_from_magic($hd); + + $type && $type->{type_name} eq 'Encrypted' or return; + + partition_table::dmcrypt->initialize($hd); + 1; +} + +sub initialize { + my ($class, $hd) = @_; + + partition_table::readonly::initialize($class, $hd, _parts($hd)); +} diff --git a/perl-install/partition_table/dos.pm b/perl-install/partition_table/dos.pm index 8d8109723..59439e1d4 100644 --- a/perl-install/partition_table/dos.pm +++ b/perl-install/partition_table/dos.pm @@ -1,4 +1,4 @@ -package partition_table::dos; # $Id$ +package partition_table::dos; # $Id: dos.pm 263707 2009-11-26 13:26:41Z pterjan $ use diagnostics; use strict; @@ -9,6 +9,7 @@ use vars qw(@ISA); use common; use partition_table::raw; use partition_table; +use fs::type; use c; my @fields = qw(active start_head start_sec start_cyl pt_type end_head end_sec end_cyl start size); @@ -18,6 +19,7 @@ my $nb_primary = 4; my $offset = $common::SECTORSIZE - length($magic) - $nb_primary * common::psizeof($format); +sub use_pt_type { 1 } sub hasExtended { 1 } sub geometry_to_string { @@ -31,6 +33,10 @@ sub last_usable_sector { $hd->{geom}{sectors} * $hd->{geom}{heads} * $hd->{geom}{cylinders}; } +my $two_TB = 2 * 1024 * 1024 * 2048; +sub max_partition_start { $two_TB - 1 } +sub max_partition_size { $two_TB - 1 } + sub get_rawCHS { my ($part) = @_; @@ -103,8 +109,8 @@ sub is_geometry_valid_for_the_partition_table { my ($chs_start_v1, $chs_end_v1) = map { join(',', @$_) } CHS_from_part_rawCHS($_) or next; my ($chs_start_v2, $chs_end_v2) = map { join(',', @$_) } map { [ min($_->[0], 1023), $_->[1], $_->[2] ] } CHS_from_part_linear($geom, $_); if (!$no_log) { - $chs_start_v1 eq $chs_start_v2 or log::l("check_geometry_using_the_partition_table failed for ($_->{device}, $_->{start}): $chs_start_v1 vs $chs_start_v2 with geometry " . geometry_to_string($geom)); - $chs_end_v1 eq $chs_end_v2 or log::l("check_geometry_using_the_partition_table failed for ($_->{device}, " . ($_->{start} + $_->{size} - 1) . "): $chs_end_v1 vs $chs_end_v2 with geometry " . geometry_to_string($geom)); + $chs_start_v1 eq $chs_start_v2 or log::l("is_geometry_valid_for_the_partition_table failed for ($_->{device}, $_->{start}): $chs_start_v1 vs $chs_start_v2 with geometry " . geometry_to_string($geom)); + $chs_end_v1 eq $chs_end_v2 or log::l("is_geometry_valid_for_the_partition_table failed for ($_->{device}, " . ($_->{start} + $_->{size} - 1) . "): $chs_end_v1 vs $chs_end_v2 with geometry " . geometry_to_string($geom)); } $chs_start_v1 eq $chs_start_v2 && $chs_end_v1 eq $chs_end_v2; } @{$hd->{primary}{normal} || []}; @@ -195,7 +201,7 @@ sub set_best_geometry_for_the_partition_table { } } -sub read { +sub read_one { my ($hd, $sector) = @_; my $tmp; @@ -214,6 +220,19 @@ sub read { sysread $F, $tmp, length $magic or die "error reading magic number on disk $hd->{device}"; $tmp eq $magic or die "bad magic number on disk $hd->{device}"; + if (c::get_disk_type($hd->{file}) ne "msdos") { + # libparted may have ignored it because of overlapping partitions or other error + # while it is actually a partition table. + $hd->{fs_type_from_magic} and die "unpartitionned disk"; + my $primary = partition_table::raw::pt_info_to_primary($hd, [ @pt ]); + foreach my $i (@{$primary->{normal}}) { + if ($i->{active} && $i->{active} != 0x80 || + $hd->{totalsectors} && $i->{start} > $hd->{totalsectors}) { + die "Invalid DOS partition table"; + } + } + } + [ @pt ]; } @@ -245,6 +264,12 @@ sub write { 1; } -sub clear_raw { { raw => [ ({}) x $nb_primary ] } } +sub empty_raw { { raw => [ ({}) x $nb_primary ] } } + +sub initialize { + my ($class, $hd) = @_; + $hd->{primary} = empty_raw(); + bless $hd, $class; +} 1; diff --git a/perl-install/partition_table/empty.pm b/perl-install/partition_table/empty.pm index b34b9210c..5d45143f8 100644 --- a/perl-install/partition_table/empty.pm +++ b/perl-install/partition_table/empty.pm @@ -1,7 +1,7 @@ -package partition_table::empty; # $Id$ +package partition_table::empty; # $Id: empty.pm 228433 2007-09-18 12:30:17Z pixel $ #- this is a mainly dummy partition table. If we find it's empty, we just call - -#- zero_MBR which will take care of bless'ing us to the partition table type best +#- ->clear which will take care of bless'ing us to the partition table type best #- suited @@ -17,7 +17,7 @@ use partition_table; use c; -sub read($$) { +sub read_one { my ($hd, $sector) = @_; my $tmp; @@ -28,7 +28,7 @@ sub read($$) { sysread $F, $tmp, 1024 or die "error reading magic number on disk $hd->{device}"; $tmp eq substr($tmp, 0, 1) x 1024 or die "bad magic number on disk $hd->{device}"; - partition_table::raw::zero_MBR($hd); + partition_table::initialize($hd); $hd->{primary}{raw}, $hd->{primary}{info}; } diff --git a/perl-install/partition_table/gpt.pm b/perl-install/partition_table/gpt.pm index d5c05e8de..764fcf184 100644 --- a/perl-install/partition_table/gpt.pm +++ b/perl-install/partition_table/gpt.pm @@ -1,4 +1,4 @@ -package partition_table::gpt; # $Id$ +package partition_table::gpt; # $Id: gpt.pm 252200 2009-01-27 18:08:22Z pterjan $ use diagnostics; use strict; @@ -6,258 +6,69 @@ use vars qw(@ISA); @ISA = qw(partition_table::raw); -use common; use partition_table::raw; -use partition_table::dos; -use partition_table; use c; -my %gpt_types = ( - 0x00 => "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", - 0x82 => "\x6d\xfd\x57\x06\xab\xa4\xc4\x43\x84\xe5\x09\x33\xc8\x4b\x4f\x4f", - 0x83 => "\xA2\xA0\xD0\xEB\xE5\xB9\x33\x44\x87\xC0\x68\xB6\xB7\x26\x99\xC7", - 0x8e => "\x79\xd3\xd6\xe6\x07\xf5\xc2\x44\xa2\x3c\x23\x8f\x2a\x3d\xf9\x28", - 0xfd => "\x0f\x88\x9d\xa1\xfc\x05\x3b\x4d\xa0\x06\x74\x3f\x0f\x84\x91\x1e", - 0xef => "\x28\x73\x2A\xC1\x1F\xF8\xd2\x11\xBA\x4B\x00\xA0\xC9\x3E\xC9\x3B", - # legacy_partition_table => "\x41\xEE\x4D\x02\xE7\x33\xd3\x11\x9D\x69\x00\x08\xC7\x81\xF3\x9F" - # PARTITION_MSFT_RESERVED_GUID "\x16\xE3\xC9\xE3\x5C\x0B\xB8\x4D\x81\x7D\xF9\x2D\xF0\x02\x15\xAE" - #PARTITION_RESERVED_GUID "\x39\x33\xa6\x8d\x07\x00\xc0\x60\xc4\x36\x08\x3a\xc8\x23\x09\x08" -); - -my $current_revision = 0x00010200; -my ($main_format, $main_fields) = list2kv( - a8 => 'magic', - V => 'revision', - V => 'headerSize', - V => 'headerCRC32', - a4 => 'blank1', - Q => 'myLBA', - Q => 'alternateLBA', - Q => 'firstUsableLBA', - Q => 'lastUsableLBA', - a16 => 'guid', - Q => 'partitionEntriesLBA', - V => 'nbPartitions', - V => 'partitionEntrySize', - V => 'partitionEntriesCRC32', -); - -my ($partitionEntry_format, $partitionEntry_fields) = list2kv( - a16 => 'gpt_type', - a16 => 'guid', - Q => 'start', - Q => 'ending', - a8 => 'efi_attributes', - a72 => 'name', -); - -my ($guid_format, $guid_fields) = list2kv( - N => 'time_low', - n => 'time_mid', - n => 'time_hi_and_version', - n => 'clock_seq', - a6 => 'node', -); - -$_ = join('', @$_) foreach $main_format, $partitionEntry_format, $guid_format; - -my $magic = "EFI PART"; - -sub generate_guid() { - my $tmp; - open(my $F, devices::make("random")) or die "Could not open /dev/random for GUID generation"; - read $F, $tmp, psizeof($guid_format); - - my %guid; @guid{@$guid_fields} = unpack $guid_format, $tmp; - $guid{clock_seq} = ($guid{clock_seq} & 0x3fff) | 0x8000; - $guid{time_hi_and_version} = ($guid{time_hi_and_version} & 0x0fff) | 0x4000; - pack($guid_format, @guid{@$guid_fields}); -} - -sub crc32 { - my ($buffer) = @_; - - my $crc = 0xFFFFFFFF; - foreach (unpack "C*", $buffer) { - my $subcrc = ($crc ^ $_) & 0xFF; - for (my $j = 8; $j > 0; $j--) { - my $b = $subcrc & 1; - $subcrc = ($subcrc >> 1) & 0x7FFFFFFF; - $subcrc = $subcrc ^ 0xEDB88320 if $b; +my $nb_primary = 128; + +sub read_one { + my ($hd, $_sector) = @_; + + c::get_disk_type($hd->{file}) eq "gpt" or die "$hd->{device} not a GPT disk ($hd->{file})"; + + my @pt; + foreach (c::get_disk_partitions($hd->{file})) { + log::l($_); + if (/^([^ ]*) ([^ ]*) ([^ ]*) (.*) \((\d*),(\d*),(\d*)\)$/) { + my %p; + $p{part_number} = $1; + $p{real_device} = $2; + $p{fs_type} = $3; + $p{pt_type} = 0xba; + $p{start} = $5; + $p{size} = $7; + @pt[$p{part_number}-1] = \%p; } - $crc = ($crc >> 8) ^ $subcrc; } - $crc ^ 0xFFFFFFFF; -} -sub compute_headerCRC32 { - my ($info) = @_; - local $info->{headerCRC32} = 0; - crc32(pack($main_format, @$info{@$main_fields})); -} - -sub read_header { - my ($sector, $F) = @_; - my $tmp; - - c::lseek_sector(fileno($F), $sector, 0) or die "reading of partition in sector $sector failed"; - - sysread $F, $tmp, psizeof($main_format) or die "error while reading partition table in sector $sector"; - my %info; @info{@$main_fields} = unpack $main_format, $tmp; - - $info{magic} eq $magic or die "bad magic number"; - $info{myLBA} == $sector or die "myLBA is not the same"; - $info{headerSize} == psizeof($main_format) or die "bad partition table header size"; - $info{partitionEntrySize} == psizeof($partitionEntry_format) or die "bad partitionEntrySize"; - $info{revision} <= $current_revision or log::l("oops, this is a new GPT revision ($info{revision} > $current_revision)"); - - $info{headerCRC32} == compute_headerCRC32(\%info) or die "bad partition table checksum"; - \%info; -} - -sub read_partitionEntries { - my ($info, $F) = @_; - my $tmp; - - c::lseek_sector(fileno($F), $info->{partitionEntriesLBA}, 0) or die "can not seek to sector partitionEntriesLBA"; - sysread $F, $tmp, psizeof($partitionEntry_format) * $info->{nbPartitions} or die "error while reading partition table in sector $info->{partitionEntriesLBA}"; - $info->{partitionEntriesCRC32} == crc32($tmp) or die "bad partition entries checksum"; + for (my $part_number = 1; $part_number < $nb_primary; $part_number++) { + next if exists($pt[$part_number-1]); + $pt[$part_number-1] = { part_number => $part_number }; + } - c::lseek_sector(fileno($F), $info->{partitionEntriesLBA}, 0) or die "can not seek to sector partitionEntriesLBA"; - my %gpt_types_rev = reverse %gpt_types; - my @pt = - map { - sysread $F, $tmp, psizeof($partitionEntry_format) or die "error while reading partition table in sector $info->{partitionEntriesLBA}"; - my %h; @h{@$partitionEntry_fields} = unpack $partitionEntry_format, $tmp; - $h{size} = $h{ending} - $h{start} + 1; - my $pt_type = $gpt_types_rev{$h{gpt_type}}; - fs::type::set_pt_type(\%h, defined $pt_type ? $pt_type : 0x100); - \%h; - } (1 .. $info->{nbPartitions}); \@pt; } -sub read { - my ($hd, $sector) = @_; - - my $l = partition_table::dos::read($hd, $sector); - my @l = grep { $_->{size} && $_->{pt_type} && !partition_table::isExtended($_) } @$l; - @l == 1 or die "bad PMBR"; - $l[0]{pt_type} == 0xee or die "bad PMBR"; - my $myLBA = $l[0]{start}; - - my $F = partition_table::raw::openit($hd) or die "failed to open device"; - my $info1 = eval { read_header($myLBA, $F) }; - my $info2 = eval { read_header($info1->{alternateLBA} || $l[0]{start} + $l[0]{size} - 1, $F) }; #- what about using $hd->{totalsectors} ??? - my $info = $info1 || { %$info2, myLBA => $info2->{alternateLBA}, alternateLBA => $info2->{myLBA}, partitionEntriesLBA => $info2->{alternateLBA} + 1 } or die; - my $pt = $info1 && $info2 ? - eval { $info1 && read_partitionEntries($info1, $F) } || read_partitionEntries($info2, $F) : - read_partitionEntries($info, $F); - $hd->raw_removed($pt); - - $pt, $info; -} - -# write the partition table (and extended ones) -# for each entry, it uses fields: start, size, pt_type, active sub write { - my ($hd, $sector, $pt, $info) = @_; + my ($hd, $_sector, $_pt, $_info) = @_; - foreach (@$pt) { - $_->{ending} = $_->{start} + $_->{size} - 1; - $_->{guid} ||= generate_guid(); - $_->{gpt_type} = $gpt_types{$_->{pt_type}} || $_->{gpt_type} || $gpt_types{0x83}; + # Initialize the disk if current partition table is not gpt + if (c::get_disk_type($hd->{file}) ne "gpt") { + c::set_disk_type($hd->{file}, "gpt"); } - my $partitionEntries = join('', map { - pack($partitionEntry_format, @$_{@$partitionEntry_fields}); - } (@$pt, ({}) x ($info->{nbPartitions} - @$pt))); - - $info->{partitionEntriesCRC32} = crc32($partitionEntries); - $info->{headerCRC32} = compute_headerCRC32($info); - - my $info2 = { %$info, - myLBA => $info->{alternateLBA}, alternateLBA => $info->{myLBA}, - partitionEntriesLBA => $info->{alternateLBA} - psizeof($partitionEntry_format) * $info->{nbPartitions} / 512, - }; - $info2->{headerCRC32} = compute_headerCRC32($info2); - { - # write the PMBR - my $pmbr = partition_table::dos::clear_raw(); - $pmbr->{raw}[0] = { pt_type => 0xee, local_start => $info->{myLBA}, size => $info->{alternateLBA} - $info->{myLBA} + 1 }; - partition_table::dos::write($hd, $sector, $pmbr->{raw}); + foreach (@{$hd->{will_tell_kernel}}) { + my ($action, $part_number, $o_start, $o_size) = @$_; + my $part; + print "($action, $part_number, $o_start, $o_size)\n"; + if ($action eq 'add') { + c::disk_add_partition($hd->{file}, $o_start, $o_size, $part->{fs_type}) or die "failed to add partition"; + } elsif ($action eq 'del') { + c::disk_del_partition($hd->{file}, $part_number) or die "failed to del partition"; + } } - - my $F = partition_table::raw::openit($hd, 2) or die "error opening device $hd->{device} for writing"; - - c::lseek_sector(fileno($F), $info->{myLBA}, 0) or return 0; - #- pad with 0's - syswrite $F, pack($main_format, @$info{@$main_fields}) . "\0" x 512, 512 or return 0; - - c::lseek_sector(fileno($F), $info->{alternateLBA}, 0) or return 0; - #- pad with 0's - syswrite $F, pack($main_format, @$info2{@$main_fields}) . "\0" x 512, 512 or return 0; - - c::lseek_sector(fileno($F), $info->{partitionEntriesLBA}, 0) or return 0; - syswrite $F, $partitionEntries or return 0; - - c::lseek_sector(fileno($F), $info2->{partitionEntriesLBA}, 0) or return 0; - syswrite $F, $partitionEntries or return 0; - common::sync(); 1; } -sub raw_removed { - my ($_hd, $raw) = @_; - @$raw = grep { $_->{size} && $_->{pt_type} } @$raw; -} -sub can_raw_add { - my ($hd) = @_; - @{$hd->{primary}{raw}} < $hd->{primary}{info}{nbPartitions}; -} -sub raw_add { - my ($hd, $raw, $part) = @_; - $hd->can_raw_add or die "raw_add: partition table already full"; - push @$raw, $part; +sub initialize { + my ($class, $hd) = @_; + $hd->{primary} = { raw => [] }; + bless $hd, $class; } +sub can_add { &can_raw_add } sub adjustStart {} sub adjustEnd {} -sub first_usable_sector { - my ($hd) = @_; - $hd->{primary}{info}{firstUsableLBA}; -} -sub last_usable_sector { - my ($hd) = @_; - $hd->{primary}{info}{lastUsableLBA} + 1; -} - -sub info { - my ($hd) = @_; - my $nb_sect = 32; - - #- build a default suitable partition table, - #- checksum will be built when writing on disk. - { - magic => $magic, - revision => $current_revision, - headerSize => psizeof($main_format), - myLBA => 1, - alternateLBA => $hd->{totalsectors} - 1, - firstUsableLBA => $nb_sect + 2, - lastUsableLBA => $hd->{totalsectors} - $nb_sect - 2, - guid => generate_guid(), - partitionEntriesLBA => 2, - nbPartitions => $nb_sect * 512 / psizeof($partitionEntry_format), - partitionEntrySize => psizeof($partitionEntry_format), - }; -} - -sub clear_raw { - my ($hd) = @_; - { raw => [], info => info($hd) }; -} - 1; diff --git a/perl-install/partition_table/lvm.pm b/perl-install/partition_table/lvm.pm new file mode 100644 index 000000000..8a28a4c2a --- /dev/null +++ b/perl-install/partition_table/lvm.pm @@ -0,0 +1,38 @@ +package partition_table::lvm; # $Id: $ + +# LVM on full disk + +use diagnostics; +use strict; + +our @ISA = qw(partition_table::readonly); + +use common; +use partition_table::readonly; +use fs::type; + +sub _parts { + my ($hd) = @_; + + my $part = { size => $hd->{totalsectors}, device => $hd->{device} }; + add2hash($part, fs::type::type_name2subpart('Linux Logical Volume Manager')); + + [ $part ]; +} + +sub read_primary { + my ($hd) = @_; + + my $type = fs::type::type_subpart_from_magic($hd); + + $type && $type->{type_name} eq 'Linux Logical Volume Manager' or return; + + partition_table::lvm->initialize($hd); + 1; +} + +sub initialize { + my ($class, $hd) = @_; + + partition_table::readonly::initialize($class, $hd, _parts($hd)); +} diff --git a/perl-install/partition_table/mac.pm b/perl-install/partition_table/mac.pm index fc0310b74..015feaee6 100644 --- a/perl-install/partition_table/mac.pm +++ b/perl-install/partition_table/mac.pm @@ -1,8 +1,8 @@ -package partition_table::mac; # $Id$ +package partition_table::mac; # $Id: mac.pm 228416 2007-09-18 10:32:56Z pixel $ use diagnostics; #use strict; - fixed other PPC code to comply, but program bails on empty partition table - sbenedict -use vars qw(@ISA $freepart $bootstrap_part $macos_part); +use vars qw(@ISA $freepart $bootstrap_part $macos_part $new_bootstrap); @ISA = qw(partition_table::raw); @@ -79,6 +79,8 @@ $p_format = join '', @$p_format; my $magic = 0x4552; my $pmagic = 0x504D; +sub use_pt_type { 1 } + sub first_usable_sector { 1 } sub adjustStart($$) { @@ -96,7 +98,7 @@ sub adjustEnd($$) { my ($_hd, $_part) = @_; } -sub read($$) { +sub read_one { my ($hd, $sector) = @_; my $tmp; @@ -296,7 +298,7 @@ sub write($$$;$) { $_->{pFlags} = 0x33; $_->{isBoot} = 1; log::l("writing a bootstrap at /dev/$_->{device}"); - $install_steps_interactive::new_bootstrap = 1 if !(defined $partition_table::mac::bootstrap_part); + $new_bootstrap = 1 if !(defined $bootstrap_part); $bootstrap_part = "/dev/" . $_->{device}; } elsif (isSwap($_)) { $_->{pType} = "Apple_UNIX_SVR2"; @@ -355,8 +357,8 @@ sub info { $info; } -sub clear_raw { - my ($hd) = @_; +sub initialize { + my ($class, $hd) = @_; my @oldraw = @{$hd->{primary}{raw}}; my $pt = { raw => [ ({}) x 63 ], info => info($hd) }; @@ -387,7 +389,8 @@ sub clear_raw { } @{$pt->{info}{ddMap}} = @{$hd->{primary}{info}{ddMap}}; - $pt; + $hd->{primary} = $pt; + bless $hd, $class; } 1; diff --git a/perl-install/partition_table/raw.pm b/perl-install/partition_table/raw.pm index c290258f5..0438fda5a 100644 --- a/perl-install/partition_table/raw.pm +++ b/perl-install/partition_table/raw.pm @@ -1,4 +1,4 @@ -package partition_table::raw; # $Id$ +package partition_table::raw; # $Id: raw.pm 266069 2010-02-09 19:47:35Z pterjan $ use diagnostics; use strict; @@ -6,6 +6,7 @@ use strict; use common; use devices; use detect_devices; +use fs::type; use log; use c; @@ -18,7 +19,16 @@ if_(arch() =~ /ppc/, [ 'grub', 0, "\xEBG", 0x17d, "stage1 \0" ], [ 'grub', 0, "\xEBH", 0x17e, "stage1 \0" ], [ 'grub', 0, "\xEBH", 0x18a, "stage1 \0" ], - [ 'grub', 0, "\xEBH", 0x181, "GRUB \0" ], + sub { my ($F) = @_; + #- standard grub has no good magic (Mageia's grub is patched to have "GRUB" at offset 6) + #- so scanning a range of possible places where grub can have its string + #- 0x176 found on Conectiva 10 + my ($min, $max, $magic) = (0x176, 0x181, "GRUB \0"); + my $tmp; + sysseek($F, 0, 0) && sysread($F, $tmp, $max + length($magic)) or return; + substr($tmp, 0, 2) eq "\xEBH" or return; + index($tmp, $magic, $min) >= 0 && "grub"; + }, [ 'lilo', 0x2, "LILO" ], [ 'lilo', 0x6, "LILO" ], [ 'lilo', 0x6 + 0x40, "LILO" ], #- when relocated in lilo's bsect_update(), variable "space" on paragraph boundary gives 0x40 @@ -46,6 +56,7 @@ if_(arch() =~ /ppc/, sub typeOfMBR($) { typeFromMagic(devices::make($_[0]), @MBR_signatures) } sub typeOfMBR_($) { typeFromMagic($_[0], @MBR_signatures) } +sub use_pt_type { 0 } sub hasExtended { 0 } sub set_best_geometry_for_the_partition_table {} @@ -58,19 +69,13 @@ sub last_usable_sector { my ($hd) = @_; $hd->{totalsectors}; } +# no limit +sub max_partition_start { 1e99 } +sub max_partition_size { 1e99 } -#- default method for starting a partition, only head size or twice -#- is allowed for starting a partition after a cylinder boundarie. -sub adjustStart($$) { - my ($hd, $part) = @_; - my $end = $part->{start} + $part->{size}; +#- default method for starting a partition +sub adjustStart($$) {} - $part->{start} = round_up($part->{start}, - $part->{start} % cylinder_size($hd) < 2 * $hd->{geom}{sectors} ? - $hd->{geom}{sectors} : cylinder_size($hd)); - $part->{size} = $end - $part->{start}; - $part->{size} > 0 or die "adjustStart get a too small partition to handle correctly"; -} #- adjusting end to match a cylinder boundary, two methods are used and must #- match at the end, else something is wrong and nothing will be done on #- partition table. @@ -93,7 +98,9 @@ sub adjustEnd($$) { sub compute_nb_cylinders { my ($geom, $totalsectors) = @_; - $geom->{cylinders} = int $totalsectors / $geom->{heads} / $geom->{sectors}; + if ($geom->{heads} && $geom->{sectors}) { + $geom->{cylinders} = int $totalsectors / $geom->{heads} / $geom->{sectors}; + } } sub keep_non_duplicates { @@ -106,7 +113,19 @@ sub get_geometries { my (@hds) = @_; @hds = grep { - if (my $h = get_geometry($_->{file})) { + if ($_->{bus} =~ /dmraid/) { + sysopen(my $F, $_->{file}, 0); + my $total = c::total_sectors(fileno $F); + my %geom; + $geom{heads} = 255; + $geom{sectors} = 63; + $geom{start} = 1; + compute_nb_cylinders(\%geom, $total); + $geom{totalcylinders} = $geom{cylinders}; + log::l("Fake geometry on " . $_->{file} . ": heads=$geom{heads} sectors=$geom{sectors} cylinders=$geom{cylinders} start=$geom{start}"); + add2hash_($_, { totalsectors => $total, geom => \%geom }); + 1; + } elsif (my $h = get_geometry($_->{file})) { add2hash_($_, $h); 1; } else { @@ -126,7 +145,7 @@ sub get_geometries { } @hds); - my %id2edd = keep_non_duplicates(map { [ chomp_(cat_("$_/mbr_signature")), $_ ] } glob("/sys/firmware/edd/int13_dev*")); + my %id2edd = keep_non_duplicates(map { [ scalar(chomp_(cat_("$_/mbr_signature"))), $_ ] } glob("/sys/firmware/edd/int13_dev*")); log::l("id2hd: " . join(' ', map_each { "$::a=>$::b->{device}" } %id2hd)); log::l("id2edd: " . join(' ', map_each { "$::a=>$::b" } %id2edd)); @@ -149,22 +168,26 @@ sub get_geometries { sub get_geometry { my ($dev) = @_; - my $g = ""; - sysopen(my $F, $dev, 0) or return; - ioctl($F, c::HDIO_GETGEO(), $g) or return; - my %geom; @geom{qw(heads sectors cylinders start)} = unpack "CCSL", $g; - $geom{totalcylinders} = $geom{cylinders}; - - my $total; - #- $geom{cylinders} is no good (only a ushort, that means less than 2^16 => at best 512MB) - if ($total = c::total_sectors(fileno $F)) { - compute_nb_cylinders(\%geom, $total); - } else { - $total = $geom{heads} * $geom{sectors} * $geom{cylinders}; + + my $total = c::total_sectors(fileno $F); + + my $g = ""; + my %geom; + if (ioctl($F, c::HDIO_GETGEO(), $g)) { + @geom{qw(heads sectors cylinders start)} = unpack "CCSL", $g; + log::l("HDIO_GETGEO on $dev succeeded: heads=$geom{heads} sectors=$geom{sectors} cylinders=$geom{cylinders} start=$geom{start}"); + $geom{totalcylinders} = $geom{cylinders}; + + #- $geom{cylinders} is no good (only a ushort, that means less than 2^16 => at best 512MB) + if ($total) { + compute_nb_cylinders(\%geom, $total); + } else { + $total = $geom{heads} * $geom{sectors} * $geom{cylinders}; + } } - { geom => \%geom, totalsectors => $total }; + { totalsectors => $total, if_($geom{heads}, geom => \%geom) }; } sub openit { @@ -172,6 +195,12 @@ sub openit { my $F; sysopen($F, $hd->{file}, $o_mode || 0) && $F; } +sub can_add { + my ($hd) = @_; + !$_->{size} && !$_->{pt_type} || isExtended($_) and return 1 foreach @{$hd->{primary}{raw}}; + 0; +} + sub raw_removed { my ($_hd, $_raw) = @_; } @@ -191,62 +220,72 @@ sub raw_add { die "raw_add: partition table already full"; } -sub default_type { - my $type = arch() =~ /ia64/ ? 'gpt' : arch() eq "alpha" ? "bsd" : arch() =~ /^sparc/ ? "sun" : arch() eq "ppc" ? "mac" : "dos"; - #- override standard mac type on PPC for IBM machines to dos - $type = "dos" if arch() =~ /ppc/ && detect_devices::get_mac_model() =~ /^IBM/; - require "partition_table/$type.pm"; - "partition_table::$type"; +sub zero_MBR { &partition_table::initialize } #- deprecated + +sub clear_existing { + my ($hd) = @_; + my @parts = (partition_table::get_normal_parts($hd), if_($hd->{primary}{extended}, $hd->{primary}{extended})); + partition_table::will_tell_kernel($hd, del => $_) foreach @parts; } -sub zero_MBR { +#- deprecated +sub zero_MBR_and_dirty { my ($hd) = @_; - #- force the standard partition type for the architecture - bless $hd, default_type(); - $hd->{primary} = $hd->clear_raw; - delete $hd->{extended}; - if (is_xbox()) { - my $part = { start => 1, size => 15632048, pt_type => 0x0bf, isFormatted => 1 }; - partition_table::dos::compute_CHS($hd, $part); - $hd->{primary}{raw}[0] = $part; - } + fsedit::partition_table_clear_and_initialize([], $hd); } -sub zero_MBR_and_dirty { - my ($hd) = @_; - my @parts = (partition_table::get_normal_parts($hd), if_($hd->{primary}{extended}, $hd->{primary}{extended})); - partition_table::will_tell_kernel($hd, del => $_) foreach @parts; - zero_MBR($hd); +sub read_primary { + my ($hd) = @_; + + my ($pt, $info) = eval { $hd->read_one(0) } or return; + my $primary = partition_table::raw::pt_info_to_primary($hd, $pt, $info); + $hd->{primary} = $primary; + undef $hd->{extended}; + partition_table::verifyPrimary($primary); + 1; +} + +sub pt_info_to_primary { + my ($hd, $pt, $info) = @_; + + my @extended = $hd->hasExtended ? grep { isExtended($_) } @$pt : (); + my @normal = grep { $_->{size} && !isEmpty($_) && !isExtended($_) } @$pt; + my $nb_special_empty = int(grep { $_->{size} && isEmpty($_) } @$pt); + + @extended > 1 and die "more than one extended partition"; + + put_in_hash($_, partition_table::hd2minimal_part($hd)) foreach @normal, @extended; + { raw => $pt, extended => $extended[0], normal => \@normal, info => $info, nb_special_empty => $nb_special_empty }; } #- ugly stuff needed mainly for Western Digital IDE drives #- try writing what we've just read, yells if it fails #- testing on last sector of head #0 (unused in 99% cases) #- -#- return false if the device can not be written to (especially for Smartmedia) +#- return false if the device cannot be written to (especially for Smartmedia) sub test_for_bad_drives { my ($hd) = @_; - log::l("test_for_bad_drives($hd->{file})"); - my $sector = $hd->{geom}{sectors} - 1; + my $sector = $hd->{geom} ? $hd->{geom}{sectors} - 1 : 0; + log::l("test_for_bad_drives($hd->{file} on sector #$sector)"); sub error { die "$_[0] error: $_[1]" } - my $F = openit($hd, $::testing ? 0 : 2) or error(openit($hd) ? 'write' : 'read', "can not open device"); + my $F = openit($hd, $::testing ? 0 : 2) or error(openit($hd) ? 'write' : 'read', "cannot open device"); my $seek = sub { c::lseek_sector(fileno($F), $sector, 0) or error('read', "seeking to sector $sector failed"); }; my $tmp; - &$seek; sysread $F, $tmp, $SECTORSIZE or error('read', "can not even read ($!)"); + &$seek; sysread $F, $tmp, $SECTORSIZE or error('read', "cannot even read ($!)"); return if $hd->{readonly} || $::testing; - &$seek; syswrite $F, $tmp or error('write', "can not even write ($!)"); + &$seek; syswrite $F, $tmp or error('write', "cannot even write ($!)"); my $tmp2; - &$seek; sysread $F, $tmp2, $SECTORSIZE or die "test_for_bad_drives: can not even read again ($!)"; + &$seek; sysread $F, $tmp2, $SECTORSIZE or die "test_for_bad_drives: cannot even read again ($!)"; $tmp eq $tmp2 or die -N("Something bad is happening on your drive. +N("Something bad is happening on your hard disk drive. A test to check the integrity of data has failed. It means writing anything on the disk will end up with random, corrupted data."); } diff --git a/perl-install/partition_table/readonly.pm b/perl-install/partition_table/readonly.pm new file mode 100644 index 000000000..f36b60e4a --- /dev/null +++ b/perl-install/partition_table/readonly.pm @@ -0,0 +1,20 @@ +package partition_table::readonly; # $Id: $ + +use diagnostics; +use strict; + +our @ISA = qw(partition_table::raw); + +use common; +use partition_table::raw; +use fs::type; + +sub initialize { + my ($class, $hd, $parts) = @_; + + $hd->{readonly} = $hd->{getting_rid_of_readonly_allowed} = 1; + $hd->{primary} = { normal => $parts }; + delete $hd->{extended}; + + bless $hd, $class; +} diff --git a/perl-install/partition_table/sun.pm b/perl-install/partition_table/sun.pm index 54e734ca8..47d702621 100644 --- a/perl-install/partition_table/sun.pm +++ b/perl-install/partition_table/sun.pm @@ -1,4 +1,4 @@ -package partition_table::sun; # $Id$ +package partition_table::sun; # $Id: sun.pm 228416 2007-09-18 10:32:56Z pixel $ use diagnostics; use strict; @@ -39,6 +39,8 @@ my $magic = 0xDABE; my $nb_primary = 8; my $offset = 0; +sub use_pt_type { 1 } + sub adjustStart($$) { my ($hd, $part) = @_; my $end = $part->{start} + $part->{size}; @@ -71,7 +73,7 @@ sub compute_crc($) { $crc; } -sub read($$) { +sub read_one { my ($hd, $sector) = @_; my $tmp; @@ -184,7 +186,7 @@ sub info { $info; } -sub clear_raw { +sub initialize { my ($hd) = @_; my $pt = { raw => [ ({}) x $nb_primary ], info => info($hd) }; @@ -196,7 +198,8 @@ sub clear_raw { size => $hd->{geom}{cylinders} * $hd->cylinder_size, }; - $pt; + $hd->{primary} = $pt; + bless $hd, 'partition::sun'; } 1; |
