From 34ee9d7a4528ac87efc76fea4d1879d5ce2bb16d Mon Sep 17 00:00:00 2001 From: Antoine Ginies Date: Mon, 14 Feb 2011 12:53:06 +0000 Subject: backport partition_table from 2010.1 --- perl-install/partition_table.pm | 20 ++- perl-install/partition_table/dos.pm | 15 +- perl-install/partition_table/gpt.pm | 268 ++++++------------------------------ perl-install/partition_table/raw.pm | 28 +++- 4 files changed, 88 insertions(+), 243 deletions(-) diff --git a/perl-install/partition_table.pm b/perl-install/partition_table.pm index 86795e8e6..e62cd3dab 100644 --- a/perl-install/partition_table.pm +++ b/perl-install/partition_table.pm @@ -1,4 +1,4 @@ -package partition_table; # $Id: partition_table.pm 245972 2008-09-18 17:00:11Z pixel $ +package partition_table; # $Id: partition_table.pm 268438 2010-05-10 14:25:50Z pterjan $ use diagnostics; use strict; @@ -260,7 +260,8 @@ sub read_primary { #- but other sectors (typically for extended partition ones) have to match this type! my @parttype = ( if_(arch() =~ /^ia64/, 'gpt'), - arch() =~ /^sparc/ ? ('sun', 'bsd') : ('lvm', 'dmcrypt', 'dos', 'bsd', 'sun', 'mac'), + # gpt must be tried before dos as it presents a fake compatibility mbr + arch() =~ /^sparc/ ? ('sun', 'bsd') : ('gpt', 'lvm', 'dmcrypt', 'dos', 'bsd', 'sun', 'mac'), ); foreach ('empty', @parttype, 'unknown') { /unknown/ and die "unknown partition table format on disk " . $hd->{file}; @@ -383,11 +384,11 @@ sub tell_kernel { my $F = partition_table::raw::openit($hd); + run_program::run('udevadm', 'control', '--stop-exec-queue') unless $::isInstall; + my $force_reboot = any { $_->[0] eq 'force_reboot' } @$tell_kernel; if (!$force_reboot) { - # only keep the last action on the partition number - # that way we do not del/add the same partition, and this helps udev :) - foreach (reverse(uniq_ { $_->[1] } reverse @$tell_kernel)) { + foreach (@$tell_kernel) { my ($action, $part_number, $o_start, $o_size) = @$_; if ($action eq 'add') { @@ -398,7 +399,11 @@ sub tell_kernel { log::l("tell kernel $action ($hd->{device} $part_number $o_start $o_size) force_reboot=$force_reboot rebootNeeded=$hd->{rebootNeeded}"); } } + + run_program::run('udevadm', 'control', '--start-exec-queue') unless $::isInstall; + if ($force_reboot) { + # FIXME Handle LVM/dmcrypt/RAID my @magic_parts = grep { $_->{isMounted} && $_->{real_mntpoint} } get_normal_parts($hd); foreach (@magic_parts) { syscall_('umount', $_->{real_mntpoint}) or log::l(N("error unmounting %s: %s", $_->{real_mntpoint}, $!)); @@ -453,6 +458,9 @@ sub write { tell_kernel($hd, $tell_kernel); } } + # get major/minor again after writing the partition table so that we got them for dynamic devices + # (eg: for SCSI like devices with kernel-2.6.28+): + fs::get_major_minor([ get_normal_parts($hd) ]); } sub active { @@ -567,7 +575,7 @@ The only solution is to move your primary partitions to have the hole next to th sub add { my ($hd, $part, $b_primaryOrExtended, $b_forceNoAdjust) = @_; - get_normal_parts($hd) >= ($hd->{device} =~ /^rd/ ? 7 : $hd->{device} =~ /^(sd|ida|cciss|ataraid)/ ? 15 : 63) and cdie "maximum number of partitions handled by linux reached"; + get_normal_parts($hd) >= ($hd->{device} =~ /^rd/ ? 7 : $hd->{device} =~ /^(ida|cciss|ataraid)/ ? 15 : 63) and cdie "maximum number of partitions handled by linux reached"; set_isFormatted($part, 0); put_in_hash($part, hd2minimal_part($hd)); diff --git a/perl-install/partition_table/dos.pm b/perl-install/partition_table/dos.pm index 00b3c531e..2cd6fe311 100644 --- a/perl-install/partition_table/dos.pm +++ b/perl-install/partition_table/dos.pm @@ -1,4 +1,4 @@ -package partition_table::dos; # $Id: dos.pm 228577 2007-09-19 15:34:17Z pixel $ +package partition_table::dos; # $Id: dos.pm 263707 2009-11-26 13:26:41Z pterjan $ use diagnostics; use strict; @@ -220,6 +220,19 @@ 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(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 ]; } diff --git a/perl-install/partition_table/gpt.pm b/perl-install/partition_table/gpt.pm index e50624410..c0f748b06 100644 --- a/perl-install/partition_table/gpt.pm +++ b/perl-install/partition_table/gpt.pm @@ -1,4 +1,4 @@ -package partition_table::gpt; # $Id: gpt.pm 240647 2008-03-25 17:19:28Z pixel $ +package partition_table::gpt; # $Id: gpt.pm 252200 2009-01-27 18:08:22Z pterjan $ use diagnostics; use strict; @@ -6,263 +6,73 @@ 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 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; - } - $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"; - - 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 use_pt_type { 1 } sub read_one { my ($hd, $sector) = @_; + my $info; + + c::get_disk_type($hd->{file}) eq "gpt" or die "not a GPT disk"; + my @pt = map { + my %p; + print $_; + if (/^([^ ]*) ([^ ]*) ([^ ]*) (.*) \((\d*),(\d*),(\d*)\)$/) { + $p{part_number} = $1; + $p{real_device} = $2; + $p{fs_type} = $3; + $p{pt_type} = 0xba; + $p{start} = $5; + $p{size} = $7; + } + \%p; + } c::get_disk_partitions($hd->{file}); - 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); + use Data::Dumper; + print Dumper(@pt); - $pt, $info; + [ @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) = @_; - 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::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}); + 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 initialize { + my ($class, $hd) = @_; + $hd->{primary} = { raw => [] }; + bless $hd, $class; } + sub can_add { &can_raw_add } -sub can_raw_add { - my ($hd) = @_; - @{$hd->{primary}{raw}} < $hd->{primary}{info}{nbPartitions}; -} +sub can_raw_add { 1 } sub raw_add { my ($hd, $raw, $part) = @_; $hd->can_raw_add or die "raw_add: partition table already full"; push @$raw, $part; } -sub use_pt_type { 1 } - 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 initialize { - my ($class, $hd) = @_; - $hd->{primary} = { raw => [], info => info($hd) }; - bless $hd, $class; -} - 1; diff --git a/perl-install/partition_table/raw.pm b/perl-install/partition_table/raw.pm index a43cdc2ee..a4ad09046 100644 --- a/perl-install/partition_table/raw.pm +++ b/perl-install/partition_table/raw.pm @@ -1,4 +1,4 @@ -package partition_table::raw; # $Id: raw.pm 244309 2008-08-21 14:36:52Z pixel $ +package partition_table::raw; # $Id: raw.pm 266069 2010-02-09 19:47:35Z pterjan $ use diagnostics; use strict; @@ -79,11 +79,13 @@ sub adjustStart($$) { my ($hd, $part) = @_; my $end = $part->{start} + $part->{size}; - $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"; + if (cylinder_size($hd)) { + $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 @@ -122,7 +124,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 { -- cgit v1.2.1