diff options
Diffstat (limited to 'perl-install/partition_table/gpt.pm')
| -rw-r--r-- | perl-install/partition_table/gpt.pm | 161 | 
1 files changed, 122 insertions, 39 deletions
| diff --git a/perl-install/partition_table/gpt.pm b/perl-install/partition_table/gpt.pm index 10eab253b..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,73 +6,156 @@ use vars qw(@ISA);  @ISA = qw(partition_table::raw); +use fs::type;  use partition_table::raw;  use c; -#sub use_pt_type { 1 } +=head1 SYNOPSYS + +Read/write GUID partition tables (GPT) + +See https://en.wikipedia.org/wiki/GUID_Partition_Table#Partition_type_GUIDs for a list of exitings GUIDs + +=head1 Functions + +=over + +=cut +my $nb_primary = 128; + + +sub first_usable_sector { 34 } + +sub last_usable_sector { +    my ($hd) = @_; +    #- do not use totalsectors because backup GPT is at end +    $hd->{totalsectors} - 33; +} + +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';  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; +    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;          } -        \%p; -    } c::get_disk_partitions($hd->{file}); +        $_->{fs_type} = $parted_mapping{$_->{fs_type}} if $parted_mapping{$_->{fs_type}}; -    use Data::Dumper; -    print Dumper(@pt); +        @pt[$_->{part_number}-1] = $_; +    } -    [ @pt ], $info; +    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 }; +    } + +    \@pt;  }  sub write { -    my ($hd, $sector, $pt, $info) = @_; +    my ($hd, $_handle, $_sector, $pt, $_info) = @_; + +    my $ped_disk; +    my $partitions_killed;      # 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"); +    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}";      }      foreach (@{$hd->{will_tell_kernel}}) {          my ($action, $part_number, $o_start, $o_size) = @$_; -        my $part; -        print "($action, $part_number, $o_start, $o_size)\n"; +        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') { -            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"; +            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}";          }      } -    common::sync(); -    1; + +    # 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; +} + +=item need_to_tell_kernel($hd) + +Hint partition_table::write() that telling the kernel to reread the +partition_table is not needed if we already succeeded in that + +=cut + +sub need_to_tell_kernel { +    my ($hd) = @_; +    # If we failed, try again (partion_table::tell_kernel() will unmount some partitions first) +    $hd->{rebootNeeded};  }  sub initialize {      my ($class, $hd) = @_; -    $hd->{primary} = { raw => [] }; +    # 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 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 adjustStart {}  sub adjustEnd {} +=back + +=cut +  1; | 
