diff options
Diffstat (limited to 'perl-install/partition_table.pm')
| -rw-r--r-- | perl-install/partition_table.pm | 945 |
1 files changed, 503 insertions, 442 deletions
diff --git a/perl-install/partition_table.pm b/perl-install/partition_table.pm index 5fb8368fa..09c8f74d9 100644 --- a/perl-install/partition_table.pm +++ b/perl-install/partition_table.pm @@ -1,261 +1,86 @@ -package partition_table; # $Id$ +package partition_table; -#use diagnostics; -#use strict; -#use vars qw(@ISA %EXPORT_TAGS @EXPORT_OK @important_types @important_types2 @fields2save); +use diagnostics; +use strict; -@ISA = qw(Exporter); -%EXPORT_TAGS = ( - types => [ qw(type2name type2fs name2type fs2type isExtended isExt2 isReiserfs isTrueFS isSwap isDos isWin isFat isSunOS isOtherAvailableFS isPrimary isNfs isSupermount isLVM isRAID isMDRAID isLVMBased isHFS isNT isMountableRW isNonMountable isApplePartMap isLoopback isApple isAppleBootstrap) ], -); -@EXPORT_OK = map { @$_ } values %EXPORT_TAGS; +use common; +use fs::type; +use partition_table::raw; +use detect_devices; +use log; +=head1 SYNOPSYS -use common qw(:common :system :functional); -use partition_table_raw; -use log; +B<partition_table> enables to read & write partitions on various partition schemes (DOS, GPT, BSD, ...) -if (arch() =~ /ppc/) { - @important_types = ('Linux native', 'Linux swap', 'Apple HFS Partition', 'Apple Bootstrap'); -} else { - @important_types = ('Linux native', 'Linux swap', if_(arch() =~ /i.86/, 'ReiserFS', 'DOS FAT16', 'Win98 FAT32')); -} -@important_types2 = ('Linux RAID', 'Linux Logical Volume Manager partition'); - -@fields2save = qw(primary extended totalsectors isDirty needKernelReread); - -@bad_types = ('Empty', 'DOS 3.3+ Extended Partition', 'Win95: Extended partition, LBA-mapped', 'Linux extended partition'); - -my %types = ( - 0x0 => 'Empty', -arch() =~ /^ppc/ ? ( - 0x401 => 'Apple Partition', - 0x401 => 'Apple Bootstrap', - 0x402 => 'Apple HFS Partition', -) : arch() =~ /^i.86/ ? ( - 0x183 => 'ReiserFS', -) : arch() =~ /^sparc/ ? ( - 0x1 => 'SunOS boot', - 0x2 => 'SunOS root', - 0x3 => 'SunOS swap', - 0x4 => 'SunOS usr', - 0x5 => 'Whole disk', - 0x6 => 'SunOS stand', - 0x7 => 'SunOS var', - 0x8 => 'SunOS home', -) : ( - 0x1 => 'DOS 12-bit FAT', - 0x2 => 'XENIX root', - 0x3 => 'XENIX /usr', - 0x4 => 'DOS 16-bit FAT (up to 32M)', - 0x5 => 'DOS 3.3+ Extended Partition', - 0x6 => 'DOS FAT16', - 0x7 => 'NTFS (or HPFS)', - 0x8 => 'OS/2 (v1.0-1.3 only) / AIX boot partition / SplitDrive / Commodore DOS / DELL partition spanning multiple drives / QNX 1.x and 2.x ("qny")', -), - 0x9 => 'AIX data partition / Coherent filesystem / QNX 1.x and 2.x ("qnz")', - 0xa => 'OS/2 Boot Manager / Coherent swap partition / OPUS', - 0xb => 'Win98 FAT32', - 0xc => 'Win98 FAT32, LBA-mapped', - 0xe => 'Win95: DOS 16-bit FAT, LBA-mapped', - 0xf => 'Win95: Extended partition, LBA-mapped', - 0x10 => 'OPUS (?)', - 0x11 => 'Hidden DOS 12-bit FAT', - 0x12 => 'Compaq/HP config partition', - 0x14 => 'Hidden DOS 16-bit FAT <32M', - 0x16 => 'Hidden DOS 16-bit FAT >=32M', - 0x17 => 'Hidden IFS (e.g., HPFS)', - 0x18 => 'AST Windows swapfile', - 0x1b => 'Hidden WIN95 OSR2 32-bit FAT', - 0x1c => 'Hidden WIN95 OSR2 32-bit FAT, LBA-mapped', - 0x1e => 'Hidden FAT95', - 0x22 => 'Used for Oxygen Extended Partition Table by ekstazya@sprint.ca.', - 0x24 => 'NEC DOS 3.x', - 0x38 => 'THEOS ver 3.2 2gb partition', - 0x39 => 'THEOS ver 4 spanned partition', - 0x3a => 'THEOS ver 4 4gb partition', - 0x3b => 'THEOS ver 4 extended partition', - 0x3c => 'PartitionMagic recovery partition', - 0x40 => 'Venix 80286', - 0x41 => 'Linux/MINIX (sharing disk with DRDOS) / Personal RISC Boot / PPC PReP (Power PC Reference Platform) Boot', - 0x42 => 'Linux swap (sharing disk with DRDOS) / SFS (Secure Filesystem) / W2K marker', - 0x43 => 'Linux native (sharing disk with DRDOS)', - 0x45 => 'EUMEL/Elan', - 0x46 => 'EUMEL/Elan 0x46', - 0x47 => 'EUMEL/Elan 0x47', - 0x48 => 'EUMEL/Elan 0x48', - 0x4d => 'QNX4.x', - 0x4e => 'QNX4.x 2nd part', - 0x4f => 'QNX4.x 3rd part / Oberon partition', - 0x50 => 'OnTrack Disk Manager (older versions) RO', - 0x51 => 'OnTrack Disk Manager RW (DM6 Aux1) / Novell', - 0x52 => 'CP/M / Microport SysV/AT', - 0x53 => 'Disk Manager 6.0 Aux3', - 0x54 => 'Disk Manager 6.0 Dynamic Drive Overlay', - 0x55 => 'EZ-Drive', - 0x56 => 'Golden Bow VFeature Partitioned Volume. / DM converted to EZ-BIOS', - 0x57 => 'DrivePro', - 0x5c => 'Priam EDisk', - 0x61 => 'SpeedStor', - 0x63 => 'Unix System V (SCO, ISC Unix, UnixWare, ...), Mach, GNU Hurd', - 0x64 => 'PC-ARMOUR protected partition / Novell Netware 2.xx', - 0x65 => 'Novell Netware 3.xx or 4.xx', - 0x67 => 'Novell', - 0x68 => 'Novell 0x68', - 0x69 => 'Novell 0x69', - 0x70 => 'DiskSecure Multi-Boot', - 0x75 => 'IBM PC/IX', - 0x80 => 'MINIX until 1.4a', - 0x81 => 'MINIX since 1.4b, early Linux / Mitac disk manager', - 0x82 => 'Linux swap', - 0x83 => 'Linux native', - 0x84 => 'OS/2 hidden C: drive / Hibernation partition', - 0x85 => 'Linux extended partition', - 0x86 => 'Old Linux RAID partition superblock / NTFS volume set', - 0x87 => 'NTFS volume set', - 0x8a => 'Linux Kernel Partition (used by AiR-BOOT)', - 0x8e => 'Linux Logical Volume Manager partition', - 0x93 => 'Amoeba', - 0x94 => 'Amoeba bad block table', - 0x99 => 'DCE376 logical drive', - 0xa0 => 'IBM Thinkpad hibernation partition / Phoenix NoteBIOS Power Management "Save-to-Disk" partition', - 0xa5 => 'BSD/386, 386BSD, NetBSD, FreeBSD', - 0xa6 => 'OpenBSD', - 0xa7 => 'NEXTSTEP', - 0xa9 => 'NetBSD', - 0xaa => 'Olivetti Fat 12 1.44Mb Service Partition', - 0xb7 => 'BSDI filesystem', - 0xb8 => 'BSDI swap partition', - 0xbe => 'Solaris boot partition', - 0xc0 => 'CTOS / REAL/32 secure small partition', - 0xc1 => 'DRDOS/secured (FAT-12)', - 0xc4 => 'DRDOS/secured (FAT-16, < 32M)', - 0xc6 => 'DRDOS/secured (FAT-16, >= 32M) / Windows NT corrupted FAT16 volume/stripe set', - 0xc7 => 'Windows NT corrupted NTFS volume/stripe set / Syrinx boot', - 0xcb => 'reserved for DRDOS/secured (FAT32)', - 0xcc => 'reserved for DRDOS/secured (FAT32, LBA)', - 0xcd => 'CTOS Memdump?', - 0xce => 'reserved for DRDOS/secured (FAT16, LBA)', - 0xd0 => 'REAL/32 secure big partition', - 0xd1 => 'Old Multiuser DOS secured FAT12', - 0xd4 => 'Old Multiuser DOS secured FAT16 <32M', - 0xd5 => 'Old Multiuser DOS secured extended partition', - 0xd6 => 'Old Multiuser DOS secured FAT16 >=32M', - 0xd8 => 'CP/M-86', - 0xdb => 'Digital Research CP/M, Concurrent CP/M, Concurrent DOS / CTOS (Convergent Technologies OS -Unisys) / KDG Telemetry SCPU boot', - 0xdd => 'Hidden CTOS Memdump?', - 0xe1 => 'DOS access or SpeedStor 12-bit FAT extended partition', - 0xe3 => 'DOS R/O or SpeedStor', - 0xe4 => 'SpeedStor 16-bit FAT extended partition < 1024 cyl.', - 0xeb => 'BeOS', - 0xee => 'Indication that this legacy MBR is followed by an EFI header', - 0xef => 'Partition that contains an EFI file system', - 0xf1 => 'SpeedStor 0xf1', - 0xf2 => 'DOS 3.3+ secondary partition', - 0xf4 => 'SpeedStor large partition / Prologue single-volume partition', - 0xf5 => 'Prologue multi-volume partition', - 0xfd => 'Linux RAID', - 0xfe => 'SpeedStor > 1024 cyl. or LANstep / IBM PS/2 IML (Initial Microcode Load) partition, located at the end of the disk. / Windows NT Disk Administrator hidden partition / Linux Logical Volume Manager partition (old)', - 0xff => 'Xenix Bad Block Table', -); - -my %type2fs = ( -arch() =~ /^ppc/ ? ( - 0x07 => 'hpfs', -) : ( - 0x07 => 'ntfs', -), -arch() !~ /sparc/ ? ( - 0x01 => 'vfat', - 0x04 => 'vfat', - 0x05 => 'ignore', - 0x06 => 'vfat', -) : ( - 0x01 => 'ufs', - 0x02 => 'ufs', - 0x04 => 'ufs', - 0x06 => 'ufs', - 0x07 => 'ufs', - 0x08 => 'ufs', -), - 0x0b => 'vfat', - 0x0c => 'vfat', - 0x0e => 'vfat', - 0x1b => 'vfat', - 0x1c => 'vfat', - 0x1e => 'vfat', - 0x82 => 'swap', - 0x83 => 'ext2', - 0x183=> 'reiserfs', - 0x401 => 'apple', - 0x402 => 'hfs', - nfs => 'nfs', #- hack -); - -my %types_rev = reverse %types; -my %fs2type = reverse %type2fs; +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 -1; +=over -sub important_types { - my @l = (@important_types, if_($::expert, @important_types2, sort values %types)); - difference2(\@l, \@bad_types); -} - -sub type2name($) { $types{$_[0]} || $_[0] } -sub type2fs($) { $type2fs{$_[0]} } -sub fs2type($) { $fs2type{$_[0]} } -sub name2type($) { - local ($_) = @_; - /0x(.*)/ ? hex $1 : $types_rev{$_} || $_; -} - -sub isWholedisk($) { arch() =~ /^sparc/ && $_[0]{type} == 5 } -sub isExtended($) { arch() !~ /^sparc/ && ($_[0]{type} == 5 || $_[0]{type} == 0xf || $_[0]{type} == 0x85) } -sub isLVM($) { $_[0]{type} == 0x8e } -sub isRAID($) { $_[0]{type} == 0xfd } -sub isMDRAID { $_[0]{device} =~ /^md/ } -sub isLVMBased { $_[0]{LVMname} } -sub isSwap($) { $type2fs{$_[0]{type}} eq 'swap' } -sub isExt2($) { $type2fs{$_[0]{type}} eq 'ext2' } -sub isReiserfs($) { $type2fs{$_[0]{type}} eq 'reiserfs' } -sub isDos($) { arch() !~ /^sparc/ && $ {{ 1=>1, 4=>1, 6=>1 }}{$_[0]{type}} } -sub isWin($) { $ {{ 0xb=>1, 0xc=>1, 0xe=>1, 0x1b=>1, 0x1c=>1, 0x1e=>1 }}{$_[0]{type}} } -sub isFat($) { isDos($_[0]) || isWin($_[0]) } -sub isSunOS($) { arch() =~ /sparc/ && $ {{ 0x1=>1, 0x2=>1, 0x4=>1, 0x6=>1, 0x7=>1, 0x8=>1 }}{$_[0]{type}} } -sub isSolaris($) { 0; } #- hack to search for getting the difference ? TODO -sub isOtherAvailableFS($) { isFat($_[0]) || isSunOS($_[0]) } #- other OS that linux can access its filesystem -sub isNfs($) { $_[0]{type} eq 'nfs' } #- small hack -sub isNT($) { arch() !~ /^sparc/ && $_[0]{type} == 0x7 } -sub isSupermount($) { $_[0]{type} eq 'supermount' } -sub isHFS($) { $type2fs{$_[0]{type}} eq 'hfs' } -sub isApple($) { $type2fs{$_[0]{type}} eq 'apple' && defined $_[0]{isDriver} } -sub isAppleBootstrap($) { $type2fs{$_[0]{type}} eq 'apple' && defined $_[0]{isBoot} } -sub isHiddenMacPart { defined $_[0]{isMap} } -sub isLoopback { defined $_[0]{loopback_file} } -sub isTrueFS { isExt2($_[0]) || isReiserfs($_[0]) } -sub isMountableRW { isTrueFS($_[0]) || isOtherAvailableFS($_[0]) } -sub isNonMountable { isRAID($_[0]) || isLVM($_[0]) } - -sub isPrimary($$) { - my ($part, $hd) = @_; - foreach (@{$hd->{primary}{raw}}) { $part eq $_ and return 1; } - 0; +=cut + + +sub hd2minimal_part { + my ($hd) = @_; + { + rootDevice => $hd->{device}, + if_($hd->{usb_media_type}, is_removable => 1), + }; } -sub adjustStartAndEnd($$) { +=item description($hd) + +Works for both hard disk drives and partitions ;p + +=cut + +sub description { + my ($hd) = @_; + my $win = $hd->{device_windobe}; + + sprintf "%s%s (%s)", + $hd->{device}, + $win && " [$win:]", + join(', ', + grep { $_ } + formatXiB($hd->{totalsectors} || $hd->{size}, 512), + $hd->{info}, $hd->{mntpoint}, $hd->{fs_type}); +} + +=item align_to_MB_boundaries($part) + +Align partition start to the next MB boundary + +=cut + +sub align_to_MB_boundaries { + my ($part) = @_; + + my $end = $part->{start} + $part->{size}; + $part->{start} = round_up($part->{start}, MB(1)); + $part->{size} = $end - $part->{start}; +} + +sub adjustStartAndEnd { my ($hd, $part) = @_; + # always align partition start to MB boundaries + # (this accounts for devices with non-512 physical sector sizes): + align_to_MB_boundaries($part); + $hd->adjustStart($part); $hd->adjustEnd($part); } -sub verifyNotOverlap($$) { +sub verifyNotOverlap { my ($a, $b) = @_; $a->{start} + $a->{size} <= $b->{start} || $b->{start} + $b->{size} <= $a->{start}; } -sub verifyInside($$) { +sub verifyInside { my ($a, $b) = @_; $b->{start} <= $a->{start} && $a->{start} + $a->{size} <= $b->{start} + $b->{size}; } @@ -263,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 @@ -280,36 +101,73 @@ sub verifyParts_ { } } } -sub verifyParts($) { +sub verifyParts { my ($hd) = @_; verifyParts_(get_normal_parts($hd)); } -sub verifyPrimary($) { +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}); } -sub assign_device_numbers($) { +sub compute_device_name { + my ($part, $hd) = @_; + $part->{device} = _compute_device_name($hd, $part->{part_number}); +} + +sub _compute_device_name { + my ($hd, $nb) = @_; + my $prefix = $hd->{prefix} || devices::prefix_for_dev($hd->{device}); + $prefix . $nb; +} + +sub assign_device_numbers { my ($hd) = @_; my $i = 1; - $_->{device} = $hd->{prefix} . $i++ foreach @{$hd->{primary}{raw}}, - map { $_->{normal} } @{$hd->{extended} || []}; + my $start = 1; + + { + foreach (@{$hd->{primary}{raw}}) { + $_->{part_number} = $i; + compute_device_name($_, $hd); + $i++; + } + foreach (map { $_->{normal} } @{$hd->{extended} || []}) { + my $dev = _compute_device_name($hd, $i); + my $renumbered = $_->{device} && $dev ne $_->{device}; + if ($renumbered) { + require fs::mount; + eval { fs::mount::umount_part($_) }; #- at least try to umount it + will_tell_kernel($hd, del => $_, 'delay_del'); + push @{$hd->{partitionsRenumbered}}, [ $_->{device}, $dev ]; + } + $_->{part_number} = $i; + compute_device_name($_, $hd); + if ($renumbered) { + will_tell_kernel($hd, add => $_, 'delay_add'); + } + $i++; + } + } #- try to figure what the windobe drive letter could be! # #- 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($_) } @{$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($_) } map { $_->{normal} } @{$hd->{extended}}; + $_->{device_windobe} = chr($i++) foreach grep { isnormal_Fat_or_NTFS($_) } map { $_->{normal} } @{$hd->{extended}}; $_->{device_windobe} = chr($i++) foreach @others; } -sub remove_empty_extended($) { +sub remove_empty_extended { my ($hd) = @_; my $last = $hd->{primary}{extended} or return; @{$hd->{extended}} = grep { @@ -323,7 +181,7 @@ sub remove_empty_extended($) { adjust_main_extended($hd); } -sub adjust_main_extended($) { +sub adjust_main_extended { my ($hd) = @_; if (!is_empty_array_ref $hd->{extended}) { @@ -333,175 +191,404 @@ sub adjust_main_extended($) { my $start = round_down($l->{normal}{start} - 1, $hd->{geom}{sectors}); my $end = $l->{normal}{start} + $l->{normal}{size}; my $only_linux = 1; my $has_win_lba = 0; - foreach (map $_->{normal}, $l, @l) { + foreach (map { $_->{normal} } $l, @l) { $start = min($start, $_->{start}); $end = max($end, $_->{start} + $_->{size}); - $only_linux &&= isTrueFS($_) || isSwap($_); - $has_win_lba ||= $_->{type} == 0xc || $_->{type} == 0xe; + $only_linux &&= isTrueLocalFS($_) || isSwap($_); + $has_win_lba ||= $_->{pt_type} == 0xc || $_->{pt_type} == 0xe; } $l->{start} = $hd->{primary}{extended}{start} = $start; $l->{size} = $hd->{primary}{extended}{size} = $end - $start; - $hd->{primary}{extended}{type} = $only_linux ? 0x85 : $has_win_lba ? 0xf : 0x5 if !$::expert; } - unless (@{$hd->{extended} || []} || !$hd->{primary}{extended}) { + if (!@{$hd->{extended} || []} && $hd->{primary}{extended}) { + will_tell_kernel($hd, del => $hd->{primary}{extended}); %{$hd->{primary}{extended}} = (); #- modify the raw entry delete $hd->{primary}{extended}; } verifyParts($hd); #- verify everything is all right } -sub adjust_local_extended($$) { +sub adjust_local_extended { my ($hd, $part) = @_; - - foreach (@{$hd->{extended} || []}) { - $_->{normal} == $part or next; - $_->{size} = $part->{size} + $part->{start} - $_->{start}; - last; - } + + my $extended = find { $_->{normal} == $part } @{$hd->{extended} || []} or return; + $extended->{size} = $part->{size} + $part->{start} - $extended->{start}; + + #- must write it there too because values are not shared + my $prev = find { $_->{extended}{start} == $extended->{start} } @{$hd->{extended} || []} or return; + $prev->{extended}{size} = $part->{size} + $part->{start} - $prev->{extended}{start}; } -sub get_normal_parts($) { +sub get_normal_parts { my ($hd) = @_; - #- HACK !! - $hd->{raid} and return grep {$_} @{$hd->{raid}}; - $hd->{loopback} and return grep {$_} @{$hd->{loopback}}; - - @{$hd->{primary}{normal} || []}, map { $_->{normal} } @{$hd->{extended} || []} + @{$hd->{primary}{normal} || []}, map { $_->{normal} } @{$hd->{extended} || []}; } -sub get_holes($) { +sub get_normal_parts_and_holes { my ($hd) = @_; + my ($start, $last) = ($hd->first_usable_sector, $hd->last_usable_sector); + + ref($hd) or print("get_normal_parts_and_holes: bad hd" . backtrace(), "\n"); - my $start = arch() eq "alpha" ? 2048 : 1; + my $minimal_hole = put_in_hash({ pt_type => 0 }, hd2minimal_part($hd)); - map { + my @l = map { my $current = $start; $start = $_->{start} + $_->{size}; - { start => $current, size => $_->{start} - $current } - } sort { $a->{start} <=> $b->{start} } grep { !isWholedisk($_) } get_normal_parts($hd), { start => $hd->{totalsectors}, size => 0 }; -} - - -sub read_one($$) { - my ($hd, $sector) = @_; - my ($pt, $info); - - #- 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! - if (!$sector) { - my @parttype = arch() =~ /^sparc/ ? ('sun', 'bsd', 'unknown') : ('dos', 'bsd', 'sun', 'mac', 'unknown'); - foreach ('empty', @parttype) { - /unknown/ and die "unknown partition table format"; - eval { - require("partition_table_$_.pm"); - bless $hd, "partition_table_$_"; - ($pt, $info) = $hd->read($sector); - log::l("found a $_ partition table on $hd->{file} at sector $sector"); - }; - $@ or last; - } - } else { - #- keep current blessed object for that, this means it is neccessary to read sector 0 before. - ($pt, $info) = $hd->read($sector); - } + my $hole = { start => $current, size => $_->{start} - $current, %$minimal_hole }; + put_in_hash($hole, hd2minimal_part($hd)); + $hole, $_; + } sort { $a->{start} <=> $b->{start} } get_normal_parts($hd); - my @extended = $hd->hasExtended ? grep { isExtended($_) } @$pt : (); - my @normal = grep { $_->{size} && $_->{type} && !isExtended($_) } @$pt; + 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; +} + + +=item default_type($hd) - @extended > 1 and die "more than one extended partition"; +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) = @_; - $_->{rootDevice} = $hd->{device} foreach @normal, @extended; - { raw => $pt, extended => $extended[0], normal => \@normal, info => $info }; + # default to GPT on UEFI systems and disks > 2TB + is_uefi() || $hd->{totalsectors} > 2 * 1024 * 1024 * 2048 ? 'gpt' : "dos"; } -sub read($;$) { - my ($hd, $clearall) = @_; - if ($clearall) { - partition_table_raw::zero_MBR_and_dirty($hd); - return 1; +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 $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); + + delete $hd->{extended}; + if (detect_devices::is_xbox()) { + my $part = { start => 1, size => 15632048, pt_type => 0x0bf, isFormatted => 1 }; + 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) = @_; + + #- 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; + } } - my $pt = read_one($hd, 0) or return 0; - $hd->{primary} = $pt; - undef $hd->{extended}; - verifyPrimary($pt); + 0; +} + + +=item read($hd) + +Read the partition table of $hd. + +=cut + +sub read { + my ($hd) = @_; + read_primary($hd) or return 0; eval { - $pt->{extended} and read_extended($hd, $pt->{extended}) || return 0; - }; die "extended partition: $@" if $@; + my $need_removing_empty_extended; + if ($hd->{primary}{extended}) { + read_extended($hd, $hd->{primary}{extended}, \$need_removing_empty_extended) or return 0; + } + if ($need_removing_empty_extended) { + #- special case when hda5 is empty, it must be skipped + #- (windows XP generates such partition tables) + remove_empty_extended($hd); #- includes adjust_main_extended + } + + }; + die "extended partition: $@" if $@; assign_device_numbers($hd); remove_empty_extended($hd); + + $hd->set_best_geometry_for_the_partition_table; 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) = @_; + my ($hd, $extended, $need_removing_empty_extended) = @_; - my $pt = read_one($hd, $extended->{start}) or return 0; + my $pt = do { + my ($pt, $info) = $hd->read_one($extended->{start}) or return 0; + partition_table::raw::pt_info_to_primary($hd, $pt, $info); + }; $pt = { %$extended, %$pt }; push @{$hd->{extended}}, $pt; @{$hd->{extended}} > 100 and die "oops, seems like we're looping here :( (or you have more than 100 extended partitions!)"; - @{$pt->{normal}} <= 1 or die "more than one normal partition in extended partition"; - @{$pt->{normal}} >= 1 or cdie "no normal partition in extended partition"; - $pt->{normal} = $pt->{normal}[0]; - #- in case of extended partitions, the start sector is local to the partition or to the first extended_part! - $pt->{normal}{start} += $pt->{start}; - - #- the following verification can broke an existing partition table that is - #- correctly read by fdisk or cfdisk. maybe the extended partition can be - #- recomputed to get correct size. - if (!verifyInside($pt->{normal}, $extended)) { - $extended->{size} = $pt->{normal}{start} + $pt->{normal}{size}; - verifyInside($pt->{normal}, $extended) or die "partition $pt->{normal}{device} is not inside its extended partition"; + if (@{$pt->{normal}} == 0) { + $$need_removing_empty_extended = 1; + delete $pt->{normal}; + print "need_removing_empty_extended\n"; + } elsif (@{$pt->{normal}} > 1) { + die "more than one normal partition in extended partition"; + } else { + $pt->{normal} = $pt->{normal}[0]; + #- in case of extended partitions, the start sector is local to the partition or to the first extended_part! + $pt->{normal}{start} += $pt->{start}; + + #- the following verification can broke an existing partition table that is + #- correctly read by fdisk or cfdisk. maybe the extended partition can be + #- recomputed to get correct size. + if (!verifyInside($pt->{normal}, $extended)) { + $extended->{size} = $pt->{normal}{start} + $pt->{normal}{size}; + verifyInside($pt->{normal}, $extended) or die "partition $pt->{normal}{device} is not inside its extended partition"; + } } if ($pt->{extended}) { $pt->{extended}{start} += $hd->{primary}{extended}{start}; - read_extended($hd, $pt->{extended}) or return 0; + return read_extended($hd, $pt->{extended}, $need_removing_empty_extended); + } else { + 1; + } +} + +=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) { + ($part_number) = $o_part->{device} =~ /(\d+)$/ or + #- do not die, it occurs when we zero_MBR_and_dirty a raw_lvm_PV + log::l("ERROR: will_tell_kernel bad device " . description($o_part)), return; + } + + my @para = + $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 ]; + } + $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); + + 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); + } elsif ($action eq 'del') { + $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}"); + } + } + + 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} = !c::tell_kernel_to_reread_partition_table($hd->{file}); + log::l("tell kernel force_reboot ($hd->{device}), rebootNeeded=$hd->{rebootNeeded}"); + + foreach (@magic_parts) { + syscall_('mount', $_->{real_mntpoint}, $_->{fs_type}, c::MS_MGC_VAL()) or log::l(N("mount failed: ") . $!); + } } - 1; } -# write the partition table -sub write($) { +=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; + $hd->{readonly} and internal_error("a read-only partition table should not be dirty ($hd->{device})!"); #- set first primary partition active if no primary partitions are marked as active. - for ($hd->{primary}{raw}) { - (grep { $_->{local_start} = $_->{start}; $_->{active} ||= 0 } @$_) or $_->[0]{active} = 0x80; + if (my @l = @{$hd->{primary}{raw}}) { + foreach (@l) { + $_->{local_start} = $_->{start}; + $_->{active} ||= 0; + } + $l[0]{active} = 0x80 if !any { $_->{active} } @l; } #- last chance for verification, this make sure if an error is detected, #- 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; - $hd->{hasBeenDirty} = 1; #- used in undo (to know if undo should believe isDirty or not) - #- now sync disk and re-read the partition table - if ($hd->{needKernelReread}) { - sync(); - $hd->kernel_read; - $hd->{needKernelReread} = 0; + if (my $tell_kernel = delete $hd->{will_tell_kernel}) { + if (fs::type::is_dmraid($hd)) { + fs::dmraid::call_dmraid('-an'); + fs::dmraid::call_dmraid('-ay'); + } else { + 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 + # (eg: for SCSI like devices with kernel-2.6.28+): + fs::get_major_minor([ get_normal_parts($hd) ]); } -sub active($$) { +sub active { my ($hd, $part) = @_; $_->{active} = 0 foreach @{$hd->{primary}{normal}}; @@ -509,26 +596,33 @@ 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 drive hd -sub remove($$) { +sub remove { my ($hd, $part) = @_; my $i; #- first search it in the primary partitions $i = 0; foreach (@{$hd->{primary}{normal}}) { if ($_ eq $part) { + will_tell_kernel($hd, del => $_); + splice(@{$hd->{primary}{normal}}, $i, 1); %$_ = (); #- blank it - return $hd->{isDirty} = $hd->{needKernelReread} = 1; + $hd->raw_removed($hd->{primary}{raw}); + return 1; } $i++; } my ($first, $second, $third) = map { $_->{normal} } @{$hd->{extended} || []}; if ($third && $first eq $part) { - die "Can't handle removing hda5 when hda6 is not the second partition" if $second->{start} > $third->{start}; + die "Cannot handle removing hda5 when hda6 is not the second partition" if $second->{start} > $third->{start}; } #- otherwise search it in extended partitions @@ -537,35 +631,42 @@ sub remove($$) { delete $_->{normal}; #- remove it remove_empty_extended($hd); + assign_device_numbers($hd); - return $hd->{isDirty} = $hd->{needKernelReread} = 1; + 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 `type' (nice comment, uh?) -sub add_primary($$) { +=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) = @_; { local $hd->{primary}{normal}; #- save it to fake an addition of $part, that way add_primary do not modify $hd if it fails push @{$hd->{primary}{normal}}, $part; adjust_main_extended($hd); #- verify - raw_add($hd->{primary}{raw}, $part); + $hd->raw_add($hd->{primary}{raw}, $part); } push @{$hd->{primary}{normal}}, $part; #- really do it } sub add_extended { - arch() =~ /^sparc/ and die _("Extended partition not supported on this platform"); - my ($hd, $part, $extended_type) = @_; $extended_type =~ s/Extended_?//; my $e = $hd->{primary}{extended}; if ($e && !verifyInside($part, $e)) { - #-die "sorry, can't add outside the main extended partition" unless $::unsafe; + #-die "sorry, cannot add outside the main extended partition" unless $::unsafe; my $end = $e->{start} + $e->{size}; my $start = min($e->{start}, $part->{start}); $end = max($end, $part->{start} + $part->{size}) - $start; @@ -575,27 +676,27 @@ sub add_extended { local $e->{size} = $end - $start; eval { verifyPrimary($hd->{primary}) }; $@ and die -_("You have a hole in your partition table but I can't use it. -The only solution is to move your primary partitions to have the hole next to the extended partitions"); +N("You have a hole in your partition table but I cannot use it. +The only solution is to move your primary partitions to have the hole next to the extended partitions."); } } if ($e && $part->{start} < $e->{start}) { - my $l = first (@{$hd->{extended}}); + my $l = first(@{$hd->{extended}}); #- the first is a special case, must recompute its real size - $l->{start} = round_down($l->{normal}{start} - 1, $hd->cylinder_size()); + $l->{start} = round_down($l->{normal}{start} - 1, $hd->cylinder_size); $l->{size} = $l->{normal}{start} + $l->{normal}{size} - $l->{start}; my $ext = { %$l }; - unshift @{$hd->{extended}}, { type => 5, raw => [ $part, $ext, {}, {} ], normal => $part, extended => $ext }; + unshift @{$hd->{extended}}, { pt_type => 5, raw => [ $part, $ext, {}, {} ], normal => $part, extended => $ext }; #- size will be autocalculated :) } else { my ($ext, $ext_size) = is_empty_array_ref($hd->{extended}) ? ($hd->{primary}, -1) : #- -1 size will be computed by adjust_main_extended (top(@{$hd->{extended}}), $part->{size}); - my %ext = ( type => $extended_type || 5, start => $part->{start}, size => $ext_size ); + my %ext = (pt_type => $extended_type || 5, start => $part->{start}, size => $ext_size); - raw_add($ext->{raw}, \%ext); + $hd->raw_add($ext->{raw}, \%ext); $ext->{extended} = \%ext; push @{$hd->{extended}}, { %ext, raw => [ $part, {}, {}, {} ], normal => $part }; } @@ -605,36 +706,44 @@ The only solution is to move your primary partitions to have the hole next to th adjust_main_extended($hd); } -sub add($$;$$) { - my ($hd, $part, $primaryOrExtended, $forceNoAdjust) = @_; +sub add { + my ($hd, $part, $b_primaryOrExtended, $b_forceNoAdjust) = @_; - get_normal_parts($hd) >= ($hd->{device} =~ /^rd/ ? 7 : $hd->{device} =~ /^(sd|ida|cciss)/ ? 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"; - $part->{notFormatted} = 1; - $part->{isFormatted} = 0; - $part->{rootDevice} = $hd->{device}; - $hd->{isDirty} = $hd->{needKernelReread} = 1; - $part->{start} ||= 1 if arch() !~ /^sparc/; #- starting at sector 0 is not allowed - adjustStartAndEnd($hd, $part) unless $forceNoAdjust; + set_isFormatted($part, 0); + put_in_hash($part, hd2minimal_part($hd)); + $part->{start} ||= 1; #- starting at sector 0 is not allowed + adjustStartAndEnd($hd, $part) unless $b_forceNoAdjust; - my $e = $hd->{primary}{extended}; my $nb_primaries = $hd->{device} =~ /^rd/ ? 3 : 1; - if (arch() =~ /^sparc|ppc/ || - $primaryOrExtended eq 'Primary' || - $primaryOrExtended !~ /Extended/ && @{$hd->{primary}{normal} || []} < $nb_primaries) { + if ($b_primaryOrExtended eq 'Primary' || + $b_primaryOrExtended !~ /Extended/ && @{$hd->{primary}{normal} || []} < $nb_primaries) { eval { add_primary($hd, $part) }; - return unless $@; + goto success if !$@; } - eval { add_extended($hd, $part, $primaryOrExtended) } if $hd->hasExtended; #- try adding extended - if ($@ || !$hd->hasExtended) { - eval { add_primary($hd, $part) }; - die $@ if $@; #- send the add extended error which should be better + if ($hd->hasExtended) { + eval { add_extended($hd, $part, $b_primaryOrExtended) }; + goto success if !$@; } + { + add_primary($hd, $part); + } + 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 -sub next($$) { +=item next($hd, $part) + +Search for the next partition + +=cut + +sub next { my ($hd, $part) = @_; first( @@ -643,62 +752,14 @@ sub next($$) { get_normal_parts($hd) ); } -sub next_start($$) { +sub next_start { my ($hd, $part) = @_; my $next = &next($hd, $part); - $next ? $next->{start} : $hd->{totalsectors}; + $next ? $next->{start} : $hd->last_usable_sector; } -sub can_raw_add { - my ($hd) = @_; - $_->{size} || $_->{type} or return 1 foreach @{$hd->{primary}{raw}}; - 0; -} -sub raw_add { - my ($raw, $part) = @_; - - foreach (@$raw) { - $_->{size} || $_->{type} and next; - $_ = $part; - return; - } - die "raw_add: partition table already full"; -} - -sub load($$;$) { - my ($hd, $file, $force) = @_; - - local *F; - open F, $file or die _("Error reading file %s", $file); +=back - my $h; - { - local $/ = "\0"; - eval <F>; - } - $@ and die _("Restoring from file %s failed: %s", $file, $@); - - ref $h eq 'ARRAY' or die _("Bad backup file"); - - my %h; @h{@fields2save} = @$h; - - $h{totalsectors} == $hd->{totalsectors} or $force or cdie "bad totalsectors"; - - #- unsure we don't modify totalsectors - local $hd->{totalsectors}; +=cut - @{$hd}{@fields2save} = @$h; - - delete @$_{qw(isMounted isFormatted notFormatted toFormat toFormatUnsure)} foreach get_normal_parts($hd); - $hd->{isDirty} = $hd->{needKernelReread} = 1; -} - -sub save($$) { - my ($hd, $file) = @_; - my @h = @{$hd}{@fields2save}; - local *F; - require Data::Dumper; - open F, ">$file" - and print F Data::Dumper->Dump([\@h], ['$h']), "\0" - or die _("Error writing to file %s", $file); -} +1; |
