diff options
Diffstat (limited to 'perl-install/partition_table/dos.pm')
| -rw-r--r-- | perl-install/partition_table/dos.pm | 170 |
1 files changed, 151 insertions, 19 deletions
diff --git a/perl-install/partition_table/dos.pm b/perl-install/partition_table/dos.pm index 86f2c0256..aa020131a 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; use diagnostics; use strict; @@ -6,18 +6,32 @@ use vars qw(@ISA); @ISA = qw(partition_table::raw); +use Time::HiRes qw(usleep); use common; use partition_table::raw; use partition_table; +use fs::proc_partitions; +use fs::type; use c; -my @fields = qw(active start_head start_sec start_cyl type end_head end_sec end_cyl start size); +=head1 SYNOPSYS + +Read/write MBR partition tables + +=head1 Functions + +=over + +=cut + +my @fields = qw(active start_head start_sec start_cyl pt_type end_head end_sec end_cyl start size); my $format = "C8 V2"; my $magic = "\x55\xAA"; 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 { @@ -27,12 +41,19 @@ sub geometry_to_string { sub last_usable_sector { my ($hd) = @_; - #- do not use totalsectors, see gi/docs/Partition-ends-after-end-of-disk.txt for more + #- do not use totalsectors, see docs/Partition-ends-after-end-of-disk.txt for more $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) = @_; + + exists $part->{start_cyl} or internal_error("get_rawCHS $part->{device}"); + [ $part->{start_cyl}, $part->{start_head}, $part->{start_sec} ], [ $part->{end_cyl}, $part->{end_head}, $part->{end_sec} ]; } @@ -52,7 +73,7 @@ sub compute_CHS { sub CHS_from_part_rawCHS { my ($part) = @_; - $part->{start} || $part->{type} or return; + $part->{start} || $part->{pt_type} or return; my ($raw_chs_start, $raw_chs_end) = get_rawCHS($part); rawCHS2CHS($raw_chs_start), rawCHS2CHS($raw_chs_end); @@ -61,7 +82,7 @@ sub CHS_from_part_rawCHS { sub CHS_from_part_linear { my ($geom, $part) = @_; - $part->{start} || $part->{type} or return; + $part->{start} || $part->{pt_type} or return; sector2CHS($geom, $part->{start}), sector2CHS($geom, $part->{start} + $part->{size} - 1); } @@ -84,7 +105,12 @@ sub CHS2rawCHS { [ $c & 0xff, $h, ($s + 1) | (($c >> 2) & 0xc0) ]; } -# returns (cylinder, head, sector) +=item sector2CHS($geom, $start) + +returns (cylinder, head, sector) + +=cut + sub sector2CHS { my ($geom, $start) = @_; my ($s, $h); @@ -100,8 +126,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} || []}; @@ -109,7 +135,7 @@ sub is_geometry_valid_for_the_partition_table { #- from parted, thanks! my @valid_nb_sectors = (63, 61, 48, 32, 16); -my @valid_nb_heads = (255, 192, 128, 96, 64, 61, 32, 17, 16); +my @valid_nb_heads = (255, 240, 192, 128, 96, 64, 61, 32, 17, 16); sub guess_geometry_from_partition_table { my ($hd) = @_; @@ -120,10 +146,26 @@ sub guess_geometry_from_partition_table { partition_table::raw::compute_nb_cylinders($geom, $hd->{totalsectors}); log::l("guess_geometry_from_partition_table $hd->{device}: " . geometry_to_string($geom)); - member($geom->{heads}, @valid_nb_heads) && member($geom->{sectors}, @valid_nb_sectors) or return { invalid => 1 }; + member($geom->{sectors}, @valid_nb_sectors) or return { invalid => 1 }; $geom; } +sub geometry_from_edd { + my ($hd, $edd_dir) = @_; + + my $sectors = cat_("$edd_dir/legacy_sectors_per_track") or return; + my $heads = cat_("$edd_dir/legacy_max_head") or return; + + my $geom = { sectors => 0 + $sectors, heads => 1 + $heads, from_edd => 1 }; + is_geometry_valid_for_the_partition_table($hd, $geom, 0) or return; + partition_table::raw::compute_nb_cylinders($geom, $hd->{totalsectors}); + + log::l("geometry_from_edd $hd->{device} $hd->{volume_id}: " . geometry_to_string($geom)); + + $geom; +} + + sub try_every_geometry { my ($hd) = @_; @@ -148,7 +190,7 @@ sub set_best_geometry_for_the_partition_table { my $guessed_geom = guess_geometry_from_partition_table($hd); if ($guessed_geom->{empty}) { - log::l("$hd->{device}: would need looking at BIOS info to find out geometry"); + log::l("$hd->{device}: would need looking at BIOS info to find out geometry") if !$hd->{geom}{from_edd}; return; } my $default_ok = is_geometry_valid_for_the_partition_table($hd, $hd->{geom}, 0); @@ -176,7 +218,7 @@ sub set_best_geometry_for_the_partition_table { } } -sub read { +sub read_one { my ($hd, $sector) = @_; my $tmp; @@ -185,7 +227,10 @@ sub read { my @pt = map { sysread $F, $tmp, psizeof($format) or die "error while reading partition table in sector $sector"; - my %h; @h{@fields} = unpack $format, $tmp; + my %h; + @h{@fields} = unpack $format, $tmp; + $h{pt_type} = 'BIOS_GRUB' if $h{pt_type} == 0 && $h{size} > 0; + fs::type::set_pt_type(\%h, $h{pt_type}); \%h; } (1..$nb_primary); @@ -193,13 +238,32 @@ 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 ($hd->{current_pt_table_type} ne "dos") { + # 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 ]; } -# write the partition table (and extended ones) -# for each entry, it uses fields: start, size, type, active -sub write($$$;$) { - my ($hd, $sector, $pt) = @_; +=item start_write($hd) + +Prepare to write the partition table (and extended ones) + +=cut + +sub start_write { + my ($hd) = @_; + + log::l("partition::dos::write $hd->{device}"); #- handle testing for writing partition table on file only! my $F; @@ -208,6 +272,27 @@ sub write($$$;$) { open $F, ">$file" or die "error opening test file $file"; } else { $F = partition_table::raw::openit($hd, 2) or die "error opening device $hd->{device} for writing"; + if ($hd->{was_hybrid_iso}) { + log::l("partition::dos::start_write erasing hybrid iso9660 signature"); + c::lseek_sector(fileno($F), 0, 0x8001) or return 0; + syswrite $F, "\0\0\0\0\0", 5 or return 0; + $hd->{was_hybrid_iso} = 0; + } + } + $F; +} + +=item start_write($hd, $F, $sector, $pt) + +Write the partition table (and extended ones). +For each entry, it uses fields: start, size, pt_type, active + +=cut + +sub write { + my ($hd, $F, $sector, $pt) = @_; + + if (!$::testing) { c::lseek_sector(fileno($F), $sector, $offset) or return 0; } @@ -215,13 +300,60 @@ sub write($$$;$) { foreach (@$pt) { compute_CHS($hd, $_); local $_->{start} = $_->{local_start} || 0; - $_->{active} ||= 0; $_->{type} ||= 0; $_->{size} ||= 0; #- for no warning + $_->{active} ||= 0; $_->{pt_type} ||= 0; $_->{size} ||= 0; #- for no warning syswrite $F, pack($format, @$_{@fields}), psizeof($format) or return 0; } syswrite $F, $magic, length $magic or return 0; 1; } -sub clear_raw { { raw => [ ({}) x $nb_primary ] } } +sub end_write { + my ($hd, $F) = @_; + close $F; +} + +sub need_to_tell_kernel { + my ($hd) = @_; + # Whenever udevd receives a change event for a raw disk device it is watching, it asks the kernel to + # rescan the partition table on that device by calling the BLKRRPART ioctl. This is only successful + # if none of the partitions on that device are currently mounted. So if any partitions are mounted, + # we need to tell the kernel what has changed ourselves. + # The udev/rules.d/60-block.rules file causes the raw disk devices to be watched by udev. This file is + # not present in the cut-down system used to run the classic installer, so we always need to tell the + # kernel in that case. + # diskdrake will not let the user delete an individual partition that is mounted, but will let the + # user clear all partitions. So initialize() records if any partitions were mounted and we take note + # of that here. + return 1 if ! -e '/usr/lib/udev/rules.d/60-block.rules' || delete $hd->{hadMountedPartitions} || any { $_->{isMounted} } partition_table::get_normal_parts($hd); + + # No further actions must be performed until the kernel has been informed of the changes. There is + # no easy way to check that udevd has received the uevent and called the BLKRRPART ioctl, so do it + # the hard way. + my $tries = 0; + do { + usleep(100000); + log::l("checking that udevd has informed the kernel of partition table changes"); + eval { fs::proc_partitions::compare($hd) }; + return 0 if !$@; + $tries++; + } while $tries < 5; + + # We don't expect to get here, but fail safe if so. + log::l("udevd failed to inform kernel of partition table changes"); + $hd->{rebootNeeded} = 1; + 1; +} + +sub empty_raw { { raw => [ ({}) x $nb_primary ] } } + +sub initialize { + my ($class, $hd) = @_; + # Remember whether any existing partitions are mounted, for use by need_to_tell_kernel(). + $hd->{hadMountedPartitions} = 1 if any { $_->{isMounted} } partition_table::get_normal_parts($hd); + # Remember whether this was a hybrid ISO so we can wipe the iso9660 signature. + $hd->{was_hybrid_iso} = 1 if $hd->{primary}{is_hybrid_iso}; + $hd->{primary} = empty_raw(); + bless $hd, $class; +} 1; |
