diff options
Diffstat (limited to 'perl-install/partition_table.pm')
| -rw-r--r-- | perl-install/partition_table.pm | 319 | 
1 files changed, 225 insertions, 94 deletions
| diff --git a/perl-install/partition_table.pm b/perl-install/partition_table.pm index 1e594ddf5..09c8f74d9 100644 --- a/perl-install/partition_table.pm +++ b/perl-install/partition_table.pm @@ -1,4 +1,4 @@ -package partition_table; # $Id: partition_table.pm 268438 2010-05-10 14:25:50Z pterjan $ +package partition_table;  use diagnostics;  use strict; @@ -9,6 +9,20 @@ use partition_table::raw;  use detect_devices;  use log; +=head1 SYNOPSYS + +B<partition_table> enables to read & write partitions on various partition schemes (DOS, GPT, BSD, ...) + +It holds base partition table management methods, it manages +appriopriate partition_table_XXX object according to what has been read +as XXX partition table type. + +=head1 Functions + +=over + +=cut +  sub hd2minimal_part {      my ($hd) = @_; @@ -18,7 +32,12 @@ sub hd2minimal_part {      };  } -#- works for both hard disk drives and partitions ;p +=item description($hd) + +Works for both hard disk drives and partitions ;p + +=cut +  sub description {      my ($hd) = @_;      my $win = $hd->{device_windobe}; @@ -32,7 +51,12 @@ sub description {  	       $hd->{info}, $hd->{mntpoint}, $hd->{fs_type});  } -#- align partition start to the next MB boundary +=item align_to_MB_boundaries($part) + +Align partition start to the next MB boundary + +=cut +  sub align_to_MB_boundaries {      my ($part) = @_; @@ -64,12 +88,8 @@ sub verifyInside {  sub verifyParts_ {      foreach my $i (@_) {  	foreach (@_) { -	    next if !$i || !$_ || $i == $_ || isWholedisk($i) || isExtended($i); #- avoid testing twice for simplicity :-) -	    if (isWholedisk($_)) { -		verifyInside($i, $_) or -		  cdie sprintf("partition sector #$i->{start} (%s) is not inside whole disk (%s)!", -			       formatXiB($i->{size}, 512), formatXiB($_->{size}, 512)); -	    } elsif (isExtended($_)) { +	    next if !$i || !$_ || $i == $_ || isExtended($i); #- avoid testing twice for simplicity :-) +	    if (isExtended($_)) {  		verifyNotOverlap($i, $_) or  		  log::l(sprintf("warning partition sector #$i->{start} (%s) is overlapping with extended partition!",  				 formatXiB($i->{size}, 512))); #- only warning for this one is acceptable @@ -87,7 +107,9 @@ sub verifyParts {  }  sub verifyPrimary {      my ($pt) = @_; -    $_->{start} > 0 || arch() =~ /^sparc/ || die "partition must NOT start at sector 0" foreach @{$pt->{normal}}; +    if (!$pt->{is_hybrid_iso}) { +        $_->{start} > 0 || die "partition must NOT start at sector 0" foreach @{$pt->{normal}}; +    }      verifyParts_(@{$pt->{normal}}, $pt->{extended});  } @@ -108,24 +130,7 @@ sub assign_device_numbers {      my $i = 1;      my $start = 1;  -    #- on PPC we need to assign device numbers to the holes too - big FUN! -    #- not if it's an IBM machine using a DOS partition table though -    if (arch() =~ /ppc/ && detect_devices::get_mac_model() !~ /^IBM/) { -	#- first sort the normal parts -	$hd->{primary}{normal} = [ sort { $a->{start} <=> $b->{start} } @{$hd->{primary}{normal}} ]; -     -	#- now loop through them, assigning partition numbers - reserve one for the holes -	foreach (@{$hd->{primary}{normal}}) { -	    if ($_->{start} > $start) { -		log::l("PPC: found a hole on $hd->{device} before $_->{start}, skipping device...");  -		$i++; -	    } -	    $_->{part_number} = $i; -	    compute_device_name($_, $hd); -	    $start = $_->{start} + $_->{size}; -	    $i++; -	} -    } else { +    {  	foreach (@{$hd->{primary}{raw}}) {  	    $_->{part_number} = $i;  	    compute_device_name($_, $hd); @@ -154,11 +159,11 @@ sub assign_device_numbers {      #- first verify there's at least one primary dos partition, otherwise it      #- means it is a secondary disk and all will be false :(      #- -    my ($c, @others) = grep { isFat_or_NTFS($_) } @{$hd->{primary}{normal}}; +    my ($c, @others) = grep { isnormal_Fat_or_NTFS($_) } @{$hd->{primary}{normal}};      $i = ord 'C';      $c->{device_windobe} = chr($i++) if $c; -    $_->{device_windobe} = chr($i++) foreach grep { isFat_or_NTFS($_) } map { $_->{normal} } @{$hd->{extended}}; +    $_->{device_windobe} = chr($i++) foreach grep { isnormal_Fat_or_NTFS($_) } map { $_->{normal} } @{$hd->{extended}};      $_->{device_windobe} = chr($i++) foreach @others;  } @@ -234,26 +239,52 @@ sub get_normal_parts_and_holes {  	my $hole = { start => $current, size => $_->{start} - $current, %$minimal_hole };  	put_in_hash($hole, hd2minimal_part($hd));  	$hole, $_; -    } sort { $a->{start} <=> $b->{start} } grep { !isWholedisk($_) } get_normal_parts($hd); +    } sort { $a->{start} <=> $b->{start} } get_normal_parts($hd);      push @l, { start => $start, size => min($last - $start, $hd->max_partition_size), %$minimal_hole } if $start < $hd->max_partition_start;      grep { !isEmpty($_) || $_->{size} >= $hd->cylinder_size } @l;  } -sub _default_type { + +=item default_type($hd) + +Returns the default type of $hd ('gpt' or 'dos' depending on whether we're running under UEFI or +whether the disk size is too big for a MBR partition table. + +=cut + +sub default_type {      my ($hd) = @_; -    arch() =~ /ia64/ ? 'gpt' :  -      arch() eq "alpha" ? "bsd" :  -      arch() =~ /^sparc/ ? "sun" :  -      arch() eq "ppc" && detect_devices::get_mac_model() !~ /^IBM/ ? "mac" :  -	$hd->{totalsectors} > 4 * 1024 * 1024 * 2048 ? 'lvm' : "dos"; #- default to LVM on full disk when >4TB +    # default to GPT on UEFI systems and disks > 2TB +    is_uefi() || $hd->{totalsectors} > 2 * 1024 * 1024 * 2048 ? 'gpt' : "dos"; +} + +sub _get_disk_type { +    my ($hd) = @_; +    my $current = c::get_disk_type($hd->{file}); +    $current = 'dos' if $current eq 'msdos'; +    # When a disk contains a FS directly (no partition table) parted calls it loop +    $current = '' if $current eq 'loop'; +    $hd->{current_pt_table_type} = $current;  } +=item initialize($hd, $o_type) + +Initialize a $hd object. + +Expect $hd->{file} to point to the raw device disk. + +The optional $o_type parameter enables to override the detected disk type (eg: 'dos', 'gpt', ...). + +=cut +  sub initialize {      my ($hd, $o_type) = @_; -    my $type = $o_type || _default_type($hd); +    my $current = _get_disk_type($hd); +    my $type = $o_type || $current || default_type($hd); +    $hd->{pt_table_type} = $type;      require "partition_table/$type.pm";      "partition_table::$type"->initialize($hd); @@ -264,39 +295,58 @@ sub initialize {          partition_table::dos::compute_CHS($hd, $part);  	$hd->{primary}{raw}[0] = $part;      } + +    will_tell_kernel($hd, 'init');  } +=item read_primary($hd) + +Identify the partition table type of $hd and return a blessed $pt of type partition_table::TYPE. + +=cut +  sub read_primary {      my ($hd) = @_; -    #- it can be safely considered that the first sector is used to probe the partition table -    #- but other sectors (typically for extended partition ones) have to match this type! -	my @parttype = ( -	  if_(arch() =~ /^ia64/, 'gpt'), -          # 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}; - -		# perl_checker: require partition_table::bsd -		# perl_checker: require partition_table::dos -		# perl_checker: require partition_table::empty -		# perl_checker: require partition_table::dmcrypt -		# perl_checker: require partition_table::lvm -		# perl_checker: require partition_table::gpt -		# perl_checker: require partition_table::mac -		# perl_checker: require partition_table::sun -		require "partition_table/$_.pm"; -		bless $hd, "partition_table::$_"; -	        if ($hd->read_primary) { -		    log::l("found a $_ partition table on $hd->{file} at sector 0"); -		    return 1; -		} -	} +    #- The libparted ped_disk_probe() function opens the raw device for R/W, which causes a +    #- change event to be sent for every partition when the raw device is closed again. So +    #- be careful not to call this function more than once. (mga#15752) +    _get_disk_type($hd); + +    my @parttype = ( +      # gpt must be tried before dos as it presents a fake compatibility mbr +      'gpt', 'lvm', 'dmcrypt', 'dos', 'bsd', 'sun', 'mac', +    ); +    foreach ('empty', @parttype, 'unknown') { +        /unknown/ and die "unknown partition table format on disk " . $hd->{file}; + +        # perl_checker: require partition_table::bsd +        # perl_checker: require partition_table::dos +        # perl_checker: require partition_table::empty +        # perl_checker: require partition_table::dmcrypt +        # perl_checker: require partition_table::lvm +        # perl_checker: require partition_table::gpt +        # perl_checker: require partition_table::mac +        # perl_checker: require partition_table::sun +        require "partition_table/$_.pm"; +        bless $hd, "partition_table::$_"; +        if ($hd->read_primary) { +            log::l("found a $_ partition table on $hd->{file} at sector 0"); +            #- Don't rely on the type returned by libparted - use what we have discovered. +            $hd->{pt_table_type} = $_ if $_ ne 'empty'; +            return 1; +        } +    }      0;  } + +=item read($hd) + +Read the partition table of $hd. + +=cut +  sub read {      my ($hd) = @_;      read_primary($hd) or return 0; @@ -321,6 +371,14 @@ sub read {      1;  } +=item read_extended($hd, $extended, $need_removing_empty_extended) + +Actually load the partition list from the blessed $pt of type partition_table::TYPE. + +It uses partition_table::TYPE::read_one() + +=cut +  sub read_extended {      my ($hd, $extended, $need_removing_empty_extended) = @_; @@ -361,12 +419,25 @@ sub read_extended {      }  } +=item will_tell_kernel($hd, $action, $o_part, $o_delay) + +Rembmer the actions to perform on the partition table that the kernel will later be made aware of. + +=cut +  sub will_tell_kernel {      my ($hd, $action, $o_part, $o_delay) = @_;      if ($action eq 'resize') {  	will_tell_kernel($hd, del => $o_part);  	will_tell_kernel($hd, add => $o_part); +    } elsif ($action eq 'init') { +	# We will tell the kernel to reread the partition table, so no need to remember +	# previous changes. +	delete $hd->{will_tell_kernel}; +	delete $hd->{will_tell_kerneldelay_add}; +	delete $hd->{will_tell_kerneldelay_del}; +	push @{$hd->{will_tell_kernel} ||= []}, [ $action, () ];      } else {  	my $part_number;  	if ($o_part) { @@ -376,52 +447,61 @@ sub will_tell_kernel {  	}  	my @para = -	  $action eq 'force_reboot' ? () :  	  $action eq 'add' ? ($part_number, $o_part->{start}, $o_part->{size}) :  	  $action eq 'del' ? $part_number :  	  internal_error("unknown action $action");  	push @{$hd->{'will_tell_kernel' . ($o_delay || '')} ||= []}, [ $action, @para ];      } -    if (!$o_delay) { -	foreach my $delay ('delay_del', 'delay_add') { -	    my $l = delete $hd->{"will_tell_kernel$delay"} or next; -	    push @{$hd->{will_tell_kernel} ||= []}, @$l; -	} -    }      $hd->{isDirty} = 1;  } +sub will_tell_kernel_delayed { +    my ($hd) = @_; +    foreach my $delay ('delay_del', 'delay_add') { +	my $l = delete $hd->{"will_tell_kernel$delay"} or next; +	push @{$hd->{will_tell_kernel} ||= []}, @$l; +    } +} + +=item tell_kernel($hd, $tell_kernel) + +Tell the kernel that the partition layout has changed. + +Take a list of [$action, $part_number, $o_start, $o_size]. +Action can be either 'add' or 'del'. +Size is not needed when deleting a partition. + +eg: ['add', '3', '5000', '1000'] + +=cut +  sub tell_kernel {      my ($hd, $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; +    my $force_reboot = $hd->{rebootNeeded} || any { $_->[0] eq 'init' } @$tell_kernel;      if (!$force_reboot) {  	foreach (@$tell_kernel) {  	    my ($action, $part_number, $o_start, $o_size) = @$_;  	    if ($action eq 'add') { -		$force_reboot ||= !c::add_partition(fileno $F, $part_number, $o_start, $o_size); +		$force_reboot ||= !c::add_partition(fileno($F), $part_number, $o_start, $o_size);  	    } elsif ($action eq 'del') { -		$force_reboot ||= !c::del_partition(fileno $F, $part_number); +		$force_reboot ||= !c::del_partition(fileno($F), $part_number);  	    }  	    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}, $!));  	} -	$hd->{rebootNeeded} = !ioctl($F, c::BLKRRPART(), 0); +	$hd->{rebootNeeded} = !c::tell_kernel_to_reread_partition_table($hd->{file});  	log::l("tell kernel force_reboot ($hd->{device}), rebootNeeded=$hd->{rebootNeeded}");  	foreach (@magic_parts) { @@ -430,7 +510,39 @@ sub tell_kernel {      }  } -# write the partition table +=item write($hd) + +Write the partition table + +The partition_table_XXX object is expected to provide three functions to +support writing the partition table: + +=over + +=item * start_write() + +start_write() is called once at the beginning to initiate the write operation, + +=item * write() + +write() is then called one or more times (depending on whether there are any +extended partitions), + +=item * end_write(). + +and end_write() is called once to complete the write operation. + +=back + +For partition table types that support extended partitions (e.g.  DOS), +start_write() is expected to return a file handle to the raw device which is +then passed to write() and end_write(), allowing the entire table to be written +before closing the raw device. For partition table types that don't support +extended partitions, this is optional, and the entire write operation can be +performed in the single call to write(). + +=cut +  sub write {      my ($hd) = @_;      $hd->{isDirty} or return; @@ -449,18 +561,18 @@ sub write {      #- it will never be writed back on partition table.      verifyParts($hd); -    $hd->write(0, $hd->{primary}{raw}, $hd->{primary}{info}) or die "writing of partition table failed"; - +    my $handle = $hd->start_write(); +    $hd->write($handle, 0, $hd->{primary}{raw}, $hd->{primary}{info}) or die "writing of partition table failed";      #- should be fixed but a extended exist with no real extended partition, that blanks mbr! -    if (arch() !~ /^sparc/) {  	foreach (@{$hd->{extended}}) {  	    # in case of extended partitions, the start sector must be local to the partition  	    $_->{normal}{local_start} = $_->{normal}{start} - $_->{start};  	    $_->{extended} and $_->{extended}{local_start} = $_->{extended}{start} - $hd->{primary}{extended}{start}; -	    $hd->write($_->{start}, $_->{raw}) or die "writing of partition table failed"; +	    $hd->write($handle, $_->{start}, $_->{raw}) or die "writing of partition table failed";  	} -    } +    $hd->end_write($handle); +    $hd->{current_pt_table_type} = $hd->{pt_table_type};      $hd->{isDirty} = 0;      if (my $tell_kernel = delete $hd->{will_tell_kernel}) { @@ -468,7 +580,7 @@ sub write {  	    fs::dmraid::call_dmraid('-an');  	    fs::dmraid::call_dmraid('-ay');  	} else { -	    tell_kernel($hd, $tell_kernel); +	    tell_kernel($hd, $tell_kernel) if $hd->need_to_tell_kernel();  	}      }      # get major/minor again after writing the partition table so that we got them for dynamic devices @@ -484,8 +596,12 @@ sub active {      $hd->{isDirty} = 1;  } +=item remove($hd, $part) + +Remove a normal partition from hard disk drive $hd + +=cut -# remove a normal partition from hard disk drive hd  sub remove {      my ($hd, $part) = @_;      my $i; @@ -518,12 +634,19 @@ sub remove {  	assign_device_numbers($hd);  	will_tell_kernel($hd, del => $part); +	#- schedule renumbering after deleting the partition +	will_tell_kernel_delayed($hd);  	return 1;      }      0;  } -# create of partition at starting at `start', of size `size' and of type `pt_type' (nice comment, uh?) +=item add_primary($hd, $part) + +Create of partition at starting at `start', of size `size' and of type `pt_type' + +=cut +  sub add_primary {      my ($hd, $part) = @_; @@ -537,8 +660,6 @@ sub add_primary {  }  sub add_extended { -    arch() =~ /^sparc|ppc/ and die N("Extended partition not supported on this platform"); -      my ($hd, $part, $extended_type) = @_;      $extended_type =~ s/Extended_?//; @@ -588,17 +709,16 @@ 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} =~ /^(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)/ ? 15 : 63) and cdie "maximum number of partitions handled by linux reached";      set_isFormatted($part, 0);      put_in_hash($part, hd2minimal_part($hd)); -    $part->{start} ||= 1 if arch() !~ /^sparc/; #- starting at sector 0 is not allowed +    $part->{start} ||= 1; #- starting at sector 0 is not allowed      adjustStartAndEnd($hd, $part) unless $b_forceNoAdjust;      my $nb_primaries = $hd->{device} =~ /^rd/ ? 3 : 1; -    if (arch() =~ /^sparc|ppc/ || -	$b_primaryOrExtended eq 'Primary' || +    if ($b_primaryOrExtended eq 'Primary' ||  	$b_primaryOrExtended !~ /Extended/ && @{$hd->{primary}{normal} || []} < $nb_primaries) {  	eval { add_primary($hd, $part) };  	goto success if !$@; @@ -612,10 +732,17 @@ sub add {      }    success:      assign_device_numbers($hd); +    #- schedule renumbering before adding the partition +    will_tell_kernel_delayed($hd);      will_tell_kernel($hd, add => $part);  } -# search for the next partition +=item next($hd, $part) + +Search for the next partition + +=cut +  sub next {      my ($hd, $part) = @_; @@ -631,4 +758,8 @@ sub next_start {      $next ? $next->{start} : $hd->last_usable_sector;  } +=back + +=cut +  1; | 
