diff options
Diffstat (limited to 'perl-install/partition_table')
-rw-r--r-- | perl-install/partition_table/bsd.pm | 5 | ||||
-rw-r--r-- | perl-install/partition_table/dmcrypt.pm | 41 | ||||
-rw-r--r-- | perl-install/partition_table/dos.pm | 111 | ||||
-rw-r--r-- | perl-install/partition_table/empty.pm | 2 | ||||
-rw-r--r-- | perl-install/partition_table/gpt.pm | 343 | ||||
-rw-r--r-- | perl-install/partition_table/lvm.pm | 2 | ||||
-rw-r--r-- | perl-install/partition_table/mac.pm | 4 | ||||
-rw-r--r-- | perl-install/partition_table/raw.pm | 81 | ||||
-rw-r--r-- | perl-install/partition_table/readonly.pm | 2 | ||||
-rw-r--r-- | perl-install/partition_table/sun.pm | 4 |
10 files changed, 322 insertions, 273 deletions
diff --git a/perl-install/partition_table/bsd.pm b/perl-install/partition_table/bsd.pm index 9421eae17..ec2a2edf4 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; use diagnostics; use strict; @@ -90,7 +90,7 @@ sub read_one { # 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, $_handle, $sector, $pt, $info) = @_; #- handle testing for writing partition table on file only! my $F; @@ -144,7 +144,6 @@ 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..eb35eb41c --- /dev/null +++ b/perl-install/partition_table/dmcrypt.pm @@ -0,0 +1,41 @@ +package partition_table::dmcrypt; + +# 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 2376fce11..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,12 +6,24 @@ 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; +=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"; @@ -29,7 +41,7 @@ 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}; } @@ -93,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); @@ -212,6 +229,7 @@ sub read_one { sysread $F, $tmp, psizeof($format) or die "error while reading partition table in sector $sector"; 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); @@ -220,13 +238,30 @@ sub read_one { 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, pt_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}"); @@ -237,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; } @@ -251,10 +307,51 @@ sub write { 1; } +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; } diff --git a/perl-install/partition_table/empty.pm b/perl-install/partition_table/empty.pm index fe4550f47..fe4ad4317 100644 --- a/perl-install/partition_table/empty.pm +++ b/perl-install/partition_table/empty.pm @@ -1,4 +1,4 @@ -package partition_table::empty; # $Id$ +package partition_table::empty; #- this is a mainly dummy partition table. If we find it's empty, we just call - #- ->clear which will take care of bless'ing us to the partition table type best diff --git a/perl-install/partition_table/gpt.pm b/perl-install/partition_table/gpt.pm index 5c46e25ca..af5509f27 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; use diagnostics; use strict; @@ -6,263 +6,156 @@ use vars qw(@ISA); @ISA = qw(partition_table::raw); -use common; -use partition_table::raw; -use partition_table::dos; -use partition_table; use fs::type; +use partition_table::raw; 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}); -} +=head1 SYNOPSYS -sub crc32 { - my ($buffer) = @_; +Read/write GUID partition tables (GPT) - 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; - } - $crc = ($crc >> 8) ^ $subcrc; - } - $crc ^ 0xFFFFFFFF; -} +See https://en.wikipedia.org/wiki/GUID_Partition_Table#Partition_type_GUIDs for a list of exitings GUIDs -sub compute_headerCRC32 { - my ($info) = @_; - local $info->{headerCRC32} = 0; - crc32(pack($main_format, @$info{@$main_fields})); -} +=head1 Functions -sub read_header { - my ($sector, $F) = @_; - my $tmp; +=over - c::lseek_sector(fileno($F), $sector, 0) or die "reading of partition in sector $sector failed"; +=cut +my $nb_primary = 128; - 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 first_usable_sector { 34 } -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"; - - 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 last_usable_sector { + my ($hd) = @_; + #- do not use totalsectors because backup GPT is at end + $hd->{totalsectors} - 33; } -sub read_one { - 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; -} +my %parted_mapping = ( + 'linux-swap(v1)' => 'swap', + 'ntfs' => 'ntfs-3g', + 'fat16' => 'vfat', + 'fat32' => 'vfat', + ); +my %rev_parted_mapping = reverse %parted_mapping; +# prefer 'fat32' over 'fat16': +$rev_parted_mapping{vfat} = 'fat32'; -# 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) = @_; +sub read_one { + my ($hd, $_sector) = @_; + + $hd->{current_pt_table_type} eq "gpt" or die "$hd->{device} not a GPT disk ($hd->{file})"; + + my @pt; + foreach (c::get_disk_partitions($hd->{file})) { + # compatibility with MBR partitions tables: + $_->{pt_type} = 0x82 if $_->{fs_type} eq 'swap'; + $_->{pt_type} = 0x0b if $_->{fs_type} eq 'vfat'; + $_->{pt_type} = 0x83 if $_->{fs_type} =~ /^ext/; + + # fix detecting ESP (special case are they're detected through pt_type): + if ($_->{flag} eq 'ESP') { + $_->{pt_type} = 0xef; + } elsif ($_->{flag} eq 'BIOS_GRUB') { + $_->{fs_type} = $_->{flag}; # hack to prevent it to land in hd->{raw} + $_->{pt_type} = $_->{flag}; # hack... + } elsif ($_->{flag} eq 'LVM') { + $_->{pt_type} = 0x8e; + } elsif ($_->{flag} eq 'RAID') { + $_->{pt_type} = 0xfd; + } elsif ($_->{flag} eq 'RECOVERY') { + $_->{pt_type} = 0x12; + } + $_->{fs_type} = $parted_mapping{$_->{fs_type}} if $parted_mapping{$_->{fs_type}}; - foreach (@$pt) { - $_->{ending} = $_->{start} + $_->{size} - 1; - $_->{guid} ||= generate_guid(); - $_->{gpt_type} = $gpt_types{$_->{pt_type}} || $_->{gpt_type} || $gpt_types{0x83}; + @pt[$_->{part_number}-1] = $_; } - 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::empty_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}); + + 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 }; } - 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; + \@pt; +} - 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; +sub write { + my ($hd, $_handle, $_sector, $pt, $_info) = @_; - 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; + my $ped_disk; + my $partitions_killed; - common::sync(); - 1; -} + # Initialize the disk if current partition table is not gpt + if ($hd->{current_pt_table_type} ne "gpt") { + $ped_disk = c::disk_open($hd->{file}, "gpt") or die "failed to create new partition table on $hd->{file}"; + $partitions_killed = 1; + } else { + $ped_disk = c::disk_open($hd->{file}) or die "failed to open partition table on $hd->{file}"; + } -sub raw_removed { - my ($_hd, $raw) = @_; - @$raw = grep { $_->{size} && $_->{pt_type} } @$raw; -} -sub can_add { &can_raw_add } -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; + foreach (@{$hd->{will_tell_kernel}}) { + my ($action, $part_number, $o_start, $o_size) = @$_; + my ($part) = grep { $_->{start} == $o_start && $_->{size} == $o_size } @$pt; + log::l("GPT partitioning: ($action, $part_number, $o_start, $o_size)"); + if ($action eq 'add') { + local $part->{fs_type} = $rev_parted_mapping{$part->{fs_type}} if $rev_parted_mapping{$part->{fs_type}}; + c::disk_add_partition($ped_disk, $o_start, $o_size, $part->{fs_type}) or die "failed to add partition #$part_number on $hd->{file}"; + my $flag; + if (isESP($part)) { + $flag = 'ESP'; + } elsif (isBIOS_GRUB($part)) { + $flag = 'BIOS_GRUB'; + } elsif (isRawLVM($part)) { + $flag = 'LVM'; + } elsif (isRawRAID($part)) { + $flag = 'RAID'; + } + if ($flag) { + c::set_partition_flag($ped_disk, $part_number, $flag) + or die "failed to set type '$flag' for $part->{file} on $part->{mntpoint}"; + } + } elsif ($action eq 'del' && !$partitions_killed) { + c::disk_del_partition($ped_disk, $part_number) or die "failed to del partition #$part_number on $hd->{file}"; + } elsif ($action eq 'init' && !$partitions_killed) { + c::disk_delete_all($ped_disk) or die "failed to delete all partitions on $hd->{file}"; + } + } + + # Commit changes to the disk and inform the kernel + my $commit_level = c::disk_commit($ped_disk); + # Updating the kernel partition table will fail if any of the deleted partitions were mounted + $hd->{rebootNeeded} = 1 if $commit_level < 2; + $commit_level; } -sub use_pt_type { 1 } +=item need_to_tell_kernel($hd) -sub adjustStart {} -sub adjustEnd {} +Hint partition_table::write() that telling the kernel to reread the +partition_table is not needed if we already succeeded in that -sub first_usable_sector { - my ($hd) = @_; - $hd->{primary}{info}{firstUsableLBA}; -} -sub last_usable_sector { - my ($hd) = @_; - $hd->{primary}{info}{lastUsableLBA} + 1; -} +=cut -sub info { +sub need_to_tell_kernel { 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), - }; + # If we failed, try again (partion_table::tell_kernel() will unmount some partitions first) + $hd->{rebootNeeded}; } sub initialize { my ($class, $hd) = @_; - $hd->{primary} = { raw => [], info => info($hd) }; + # part_number starts at 1 + my @raw = map { +{ part_number => $_ + 1 } } 0..$nb_primary-2; + $hd->{primary} = { raw => \@raw }; bless $hd, $class; } +sub can_add { &can_raw_add } +sub adjustStart {} +sub adjustEnd {} + +=back + +=cut + 1; diff --git a/perl-install/partition_table/lvm.pm b/perl-install/partition_table/lvm.pm index 8a28a4c2a..920507fa5 100644 --- a/perl-install/partition_table/lvm.pm +++ b/perl-install/partition_table/lvm.pm @@ -1,4 +1,4 @@ -package partition_table::lvm; # $Id: $ +package partition_table::lvm; # LVM on full disk diff --git a/perl-install/partition_table/mac.pm b/perl-install/partition_table/mac.pm index 2922bc7a3..78e5469cd 100644 --- a/perl-install/partition_table/mac.pm +++ b/perl-install/partition_table/mac.pm @@ -1,4 +1,4 @@ -package partition_table::mac; # $Id$ +package partition_table::mac; use diagnostics; #use strict; - fixed other PPC code to comply, but program bails on empty partition table - sbenedict @@ -193,7 +193,7 @@ sub read_one { } sub write($$$;$) { - my ($hd, $sector, $pt, $info) = @_; + my ($hd, $_handle, $sector, $pt, $info) = @_; #- handle testing for writing partition table on file only! my $F; diff --git a/perl-install/partition_table/raw.pm b/perl-install/partition_table/raw.pm index 0a878107c..453a19c6c 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; use diagnostics; use strict; @@ -11,16 +11,12 @@ use log; use c; my @MBR_signatures = ( -if_(arch() =~ /ppc/, - (map { [ 'yaboot', 0, "PM", 0x200 * $_ + 0x10, "bootstrap\0" ] } 0 .. 61), #- "PM" is a Partition Map - [ 'yaboot', 0x400, "BD", 0x424, "\011bootstrap" ], #- "BD" is a HFS filesystem -), [ 'empty', 0, "\0\0\0\0" ], [ 'grub', 0, "\xEBG", 0x17d, "stage1 \0" ], [ 'grub', 0, "\xEBH", 0x17e, "stage1 \0" ], [ 'grub', 0, "\xEBH", 0x18a, "stage1 \0" ], sub { my ($F) = @_; - #- standard grub has no good magic (Mandriva's grub is patched to have "GRUB" at offset 6) + #- 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"); @@ -29,6 +25,14 @@ if_(arch() =~ /ppc/, substr($tmp, 0, 2) eq "\xEBH" or return; index($tmp, $magic, $min) >= 0 && "grub"; }, + sub { my ($F) = @_; + #- similar to grub-legacy, grub2 doesn't seem to have good magic + #- so scanning a range of possible places where grub can have its string + my ($min, $max, $magic) = (0x176, 0x188, "GRUB"); + my $tmp; + sysseek($F, 0, 0) && sysread($F, $tmp, $max + length($magic)) or return; + index($tmp, $magic, $min) >= 0 && "grub2"; + }, [ 'lilo', 0x2, "LILO" ], [ 'lilo', 0x6, "LILO" ], [ 'lilo', 0x6 + 0x40, "LILO" ], #- when relocated in lilo's bsect_update(), variable "space" on paragraph boundary gives 0x40 @@ -73,18 +77,9 @@ sub last_usable_sector { 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. @@ -122,7 +117,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 { @@ -219,23 +226,25 @@ sub raw_add { 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; -} - #- deprecated sub zero_MBR_and_dirty { my ($hd) = @_; fsedit::partition_table_clear_and_initialize([], $hd); } +#- by default, we assume the kernel doesn't automatically reread partition table: +sub need_to_tell_kernel { + my ($_hd) = @_; + 1; +} + sub read_primary { my ($hd) = @_; - my ($pt, $info) = eval { $hd->read_one(0) } or return; + my ($pt, $info) = eval { $hd->read_one(0) }; + $pt or return; my $primary = partition_table::raw::pt_info_to_primary($hd, $pt, $info); + $primary->{is_hybrid_iso} = $hd->{current_pt_table_type} eq 'dos' && $hd->{fs_type_from_magic} eq 'iso9660'; $hd->{primary} = $primary; undef $hd->{extended}; partition_table::verifyPrimary($primary); @@ -259,7 +268,7 @@ sub pt_info_to_primary { #- 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) = @_; @@ -268,23 +277,33 @@ sub test_for_bad_drives { 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."); } +sub start_write { + my ($_hd) = @_; + 1; +} + +sub end_write { + my ($_hd, $_handle) = @_; + 1; +} + 1; diff --git a/perl-install/partition_table/readonly.pm b/perl-install/partition_table/readonly.pm index f36b60e4a..ee3624f88 100644 --- a/perl-install/partition_table/readonly.pm +++ b/perl-install/partition_table/readonly.pm @@ -1,4 +1,4 @@ -package partition_table::readonly; # $Id: $ +package partition_table::readonly; use diagnostics; use strict; diff --git a/perl-install/partition_table/sun.pm b/perl-install/partition_table/sun.pm index 1532c04a9..e665d0168 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; use diagnostics; use strict; @@ -119,7 +119,7 @@ sub read_one { # 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, $_handle, $sector, $pt, $info) = @_; # my ($csize, $wdsize) = (0, 0); #- handle testing for writing partition table on file only! |