package fsedit; use diagnostics; use strict; #-###################################################################################### #- misc imports #-###################################################################################### use common qw(:common :constant :functional :file); use partition_table qw(:types); use partition_table_raw; use detect_devices; use Data::Dumper; use fsedit; use devices; use fs; use log; #-##################################################################################### #- Globals #-##################################################################################### my @suggestions = ( { mntpoint => "/boot", size => 16 << 11, type => 0x83, maxsize => 30 << 11 }, { mntpoint => "/", size => 50 << 11, type => 0x83, ratio => 1, maxsize => 300 << 11 }, { mntpoint => "swap", size => 30 << 11, type => 0x82, ratio => 1, maxsize => 250 << 11 }, { mntpoint => "/usr", size => 200 << 11, type => 0x83, ratio => 6, maxsize =>1500 << 11 }, { mntpoint => "/home", size => 50 << 11, type => 0x83, ratio => 3 }, { mntpoint => "/var", size => 200 << 11, type => 0x83, ratio => 1, maxsize =>1000 << 11 }, { mntpoint => "/tmp", size => 50 << 11, type => 0x83, ratio => 3, maxsize => 500 << 11 }, { mntpoint => "/mnt/iso", size => 700 << 11, type => 0x83 }, ); my @suggestions_mntpoints = qw(/mnt/dos); my @partitions_signatures = ( [ 0x83, 0x438, "\x53\xEF" ], [ 0x82, 4086, "SWAP-SPACE" ], [ 0xc, 0x1FE, "\x55\xAA", 0x52, "FAT32" ], [ 0x6, 0x1FE, "\x55\xAA", 0x36, "FAT" ], ); sub typeOfPart($) { typeFromMagic(devices::make($_[0]), @partitions_signatures) } #-###################################################################################### #- Functions #-###################################################################################### sub hds($$) { my ($drives, $flags) = @_; my @hds; my $rc; foreach (@$drives) { my $file = devices::make($_->{device}); my $hd = partition_table_raw::get_geometry($file) or log::l("An error occurred while getting the geometry of block device $file: $!"), next; $hd = { (%$_, %$hd) }; $hd->{file} = $file; $hd->{prefix} = $hd->{device}; # for RAID arrays of format c0d0p1 $hd->{prefix} .= "p" if $hd->{prefix} =~ m,(rd|ida)/,; eval { partition_table::read($hd, $flags->{clearall}) }; if ($@) { cdie($@) unless $flags->{eraseBadPartitions}; partition_table_raw::zero_MBR($hd); } push @hds, $hd; } [ @hds ]; } sub readProcPartitions { my ($hds) = @_; my @parts; foreach (cat_("/proc/partitions")) { my (undef, undef, $size, $device) = split; next if $size eq "1"; #- extended partitions foreach (@$hds) { push @parts, { start => 0, size => $size * 2, device => $device, type => typeOfPart($device), rootDevice => $_->{device} } if $device =~ /^$_->{device}./; } } @parts; } sub get_fstab(@) { map { partition_table::get_normal_parts($_) } @_; } sub free_space(@) { sum map { $_->{size} } map { partition_table::get_holes($_) } @_; } sub hasRAID { my $b = 0; map { $b ||= isRAID($_) } get_fstab(@_); $b; } sub get_root($;$) { my ($fstab, $boot) = @_; if ($boot) { $_->{mntpoint} eq "/boot" and return $_ foreach @$fstab; } $_->{mntpoint} eq "/" and return $_ foreach @$fstab; undef; } sub get_root_ { get_root([ get_fstab(@{$_[0]}) ], $_[1]) } sub is_one_big_fat { my ($hds) = @_; @$hds == 1 or return; my @l = fsedit::get_fstab(@$hds); @l == 1 && isFat($l[0]) && fsedit::free_space(@$hds) < 10 << 11; } sub computeSize($$$$) { my ($part, $best, $hds, $suggestions) = @_; my $max = $part->{maxsize} || $part->{size}; return min($max, $best->{size}) unless $best->{ratio}; my $free_space = free_space(@$hds); my @l = my @L = grep { if (!has_mntpoint($_->{mntpoint}, $hds) && $free_space >= $_->{size}) { $free_space -= $_->{size}; 1; } else { 0 } } @$suggestions; my $tot_ratios = 0; while (1) { my $old_free_space = $free_space; my $old_tot_ratios = $tot_ratios; $tot_ratios = sum(map { $_->{ratio} } @l); last if $tot_ratios == $old_tot_ratios; @l = grep { if ($_->{ratio} && $_->{maxsize} && $tot_ratios && $_->{size} + $_->{ratio} / $tot_ratios * $old_free_space >= $_->{maxsize}) { return min($max, $best->{maxsize}) if $best->{mntpoint} eq $_->{mntpoint}; $free_space -= $_->{maxsize} - $_->{size}; 0; } else { $_->{ratio}; } } @l; } my $size = min($max, $best->{size} + $free_space * ($tot_ratios && $best->{ratio} / $tot_ratios)); #- verify other entry can fill the hole if (grep { $_->{size} < $max - $size } @L) { $size } else { $max } } sub suggest_part($$$;$) { my ($hd, $part, $hds, $suggestions) = @_; $suggestions ||= \@suggestions; my $has_swap = grep { isSwap($_) } get_fstab(@$hds); my ($best, $second) = grep { !$_->{maxsize} || $part->{size} <= $_->{maxsize} } grep { $_->{size} <= ($part->{maxsize} || $part->{size}) } grep { !has_mntpoint($_->{mntpoint}, $hds) || isSwap($_) && !$has_swap } grep { !$part->{type} || $part->{type} == $_->{type} } @$suggestions or return; $best = $second if $best->{mntpoint} eq '/boot' && $part->{start} + $best->{size} > 1024 * partition_table::cylinder_size($hd); #- if the empty slot is beyond the 1024th cylinder, no use having /boot defined $best or return; #- sorry no suggestion :( $part->{mntpoint} = $best->{mntpoint}; $part->{type} = $best->{type}; $part->{size} = computeSize($part, $best, $hds, $suggestions); 1; } sub suggestions_mntpoint($) { my ($hds) = @_; sort grep { !/swap/ && !has_mntpoint($_, $hds) } (@suggestions_mntpoints, map { $_->{mntpoint} } @suggestions); } #-sub partitionDrives { #- #- my $cmd = "/sbin/fdisk"; #- -x $cmd or $cmd = "/usr/bin/fdisk"; #- #- my $drives = findDrivesPresent() or die "You don't have any hard drives available! You probably forgot to configure a SCSI controller."; #- #- foreach (@$drives) { #- my $text = "/dev/" . $_->{device}; #- $text .= " - SCSI ID " . $_->{id} if $_->{device} =~ /^sd/; #- $text .= " - Model " . $_->{info}; #- $text .= " array" if $_->{device} =~ /^c.d/; #- #- #- truncate at 50 columns for now #- $text = substr $text, 0, 50; #- } #- #-TODO TODO #-} sub has_mntpoint($$) { my ($mntpoint, $hds) = @_; scalar grep { $mntpoint eq $_->{mntpoint} } get_fstab(@$hds); } #- do this before modifying $part->{mntpoint} #- $part->{mntpoint} should not be used here, use $mntpoint instead sub check_mntpoint { my ($mntpoint, $hd, $part, $hds) = @_; $mntpoint eq '' || isSwap($part) || isRAID($part) and return; local $_ = $mntpoint; m|^/| or die _("Mount points must begin with a leading /"); #- m|(.)/$| and die "The mount point $_ is illegal.\nMount points may not end with a /"; has_mntpoint($mntpoint, $hds) and die _("There is already a partition with mount point %s", $mntpoint); if ($part->{start} + $part->{size} > 1024 * partition_table::cylinder_size($hd)) { die "/boot ending on cylinder > 1024" if $mntpoint eq "/boot"; die "/ ending on cylinder > 1024" if $mntpoint eq "/" && !has_mntpoint("/boot", $hds); } } sub add($$$;$) { my ($hd, $part, $hds, $options) = @_; isSwap($part) ? ($part->{mntpoint} = 'swap') : $options->{force} || check_mntpoint($part->{mntpoint}, $hd, $part, $hds); delete $part->{maxsize}; partition_table::add($hd, $part, $options->{primaryOrExtended}); } sub allocatePartitions($$) { my ($hds, $to_add) = @_; foreach my $hd (@$hds) { foreach (partition_table::get_holes($hd)) { my ($start, $size) = @$_{"start", "size"}; my $part; while (suggest_part($hd, $part = { start => $start, size => 0, maxsize => $size }, $hds, $to_add)) { add($hd, $part, $hds); $start = $part->{start} + $part->{size}; $size -= $part->{size}; } $start = $_->{start} + $_->{size}; } } } sub auto_allocate($;$) { my ($hds, $suggestions) = @_; allocatePartitions($hds, $suggestions || \@suggestions); map { partition_table::assign_device_numbers($_) } @$hds; } sub undo_prepare($) { my ($hds) = @_; $Data::Dumper::Purity = 1; foreach (@$hds) { my @h = @{$_}{@partition_table::fields2save}; push @{$_->{undo}}, Data::Dumper->Dump([\@h], ['$h']); } } sub undo_forget($) { my ($hds) = @_; pop @{$_->{undo}} foreach @$hds; } sub undo($) { my ($hds) = @_; foreach (@$hds) { my $h; eval pop @{$_->{undo}} || next; @{$_}{@partition_table::fields2save} = @$h; $_->{isDirty} = $_->{needKernelReread} = 1; } } sub move { my ($hd, $part, $hd2, $sector2) = @_; my $part1 = { %$part }; my $part2 = { %$part }; $part2->{start} = $sector2; $part2->{size} += partition_table::cylinder_size($hd2) - 1; partition_table::remove($hd, $part); { local ($part2->{notFormatted}, $part2->{isFormatted}); #- do not allow partition::add to change this partition_table::add($hd2, $part2); } return if $part2->{notFormatted} && !$part2->{isFormatted} || $::testing; local (*F, *G); sysopen F, $hd->{file}, 0 or die ''; sysopen G, $hd2->{file}, 2 or die _("Error opening %s for writing: %s", $hd2->{file}, "$!"); my $base = $part1->{start}; my $base2 = $part2->{start}; my $step = 10; if ($hd eq $hd2) { $base == $base2 and return; $step = min($step, abs($base2 - $base)); if ($base < $base2) { $base += $part1->{size} - $step; $base2 += $part1->{size} - $step; $step = -$step; } } my $f = sub { $base < 0 and $base2 += -$base, $base = 0; $base2 < 0 and $base += -$base2, $base2 = 0; c::lseek_sector(fileno(F), $base, 0) or die "seeking to sector $base failed on drive $hd->{device}"; c::lseek_sector(fileno(G), $base2, 0) or die "seeking to sector $base2 failed on drive $hd2->{device}"; my $buf; sysread F, $buf, $SECTORSIZE * abs($_[0]) or die ''; syswrite G, $buf; }; for (my $i = 0; $i < $part1->{size} / abs($step); $i++, $base += $step, $base2 += $step) { print "$base $base2\n"; &$f($step); } if (my $v = ($part1->{size} % abs($step)) * sign($step)) { $base += $v; $base2 += $v; &$f($v); } } sub change_type($$$) { my ($hd, $part, $type) = @_; $type != $part->{type} or return; $hd->{isDirty} = 1; $part->{mntpoint} = '' if isSwap($part) && $part->{mntpoint} eq "swap"; $part->{type} = $type; $part->{notFormatted} = 1; $part->{isFormatted} = 0; } sub rescuept($) { my ($hd) = @_; my ($ext, @hd); my $dev = devices::make($hd->{device}); open F, "rescuept $dev|"; foreach () { my ($st, $si, $id) = /start=\s*(\d+),\s*size=\s*(\d+),\s*Id=\s*(\d+)/ or next; my $part = { start => $st, size => $si, type => hex($id) }; if (isExtended($part)) { $ext = $part; } else { push @hd, $part; } } close F or die "rescuept failed"; partition_table_raw::zero_MBR($hd); foreach (@hd) { my $b = partition_table::verifyInside($_, $ext); if ($b) { $_->{start}--; $_->{size}++; } local $_->{notFormatted}; partition_table::add($hd, $_, ($b ? 'Extended' : 'Primary'), 1); } } sub verifyHds { my ($hds, $readonly, $ok) = @_; if (is_empty_array_ref($hds)) { #- no way die _("An error has occurred - no valid devices were found on which to create new filesystems. Please check your hardware for the cause of this problem"); } my @parts = readProcPartitions($hds); $ok &&= @parts == listlength(get_fstab(@$hds)); if ($readonly && !$ok) { log::l("using /proc/partitions as diskdrake failed :("); foreach my $hd (@$hds) { partition_table_raw::zero_MBR($hd); $hd->{primary} = { normal => [ grep { $hd->{device} eq $_->{rootDevice} } @parts ] }; } } my $fstab = [ get_fstab(@$hds) ]; if (is_empty_array_ref($fstab) && $readonly) { die _("You don't have any partitions!"); } ($hds, $fstab, $ok); } #-###################################################################################### #- Wonderful perl :( #-###################################################################################### 1; # 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936
package install::steps; # $Id$

use diagnostics;
use strict;
use vars qw(@filesToSaveForUpgrade @filesNewerToUseAfterUpgrade);

#-######################################################################################
#- misc imports
#-######################################################################################
use common;
use install::any 'addToBeDone';
use partition_table;
use detect_devices;
use fs::any;
use fs::type;
use fs::partitioning;
use modules;
use run_program;
use lang;
use keyboard;
use fsedit;
use do_pkgs;
use install::pkgs;
use any;
use log;

our @ISA = qw(do_pkgs);

@filesToSaveForUpgrade = qw(
/etc/ld.so.conf /etc/fstab /etc/hosts /etc/conf.modules /etc/modules.conf
);

@filesNewerToUseAfterUpgrade = qw(
/etc/profile
);

#-######################################################################################
#- OO Stuff
#-######################################################################################
sub new($$) {
    my ($type, $o) = @_;

    bless $o, ref($type) || $type;
    return $o;
}

sub charsetChanged {
    my ($_o) = @_;
}

#-######################################################################################
#- In/Out Steps Functions
#-######################################################################################
sub enteringStep {
    my ($_o, $step) = @_;
    log::l("starting step `$step'");
}
sub leavingStep {
    my ($o, $step) = @_;
    log::l("step `$step' finished");

    if (-d "$::prefix/root/drakx") {
	eval { cp_af("/tmp/ddebug.log", "$::prefix/root/drakx") };
	output(install::any::auto_inst_file(), install::any::g_auto_install(1));
    }

    foreach my $s (@{$o->{orderedSteps}}) {
	#- the reachability property must be recomputed each time to take
	#- into account failed step.
	next if $o->{steps}{$s}{done} && !$o->{steps}{$s}{redoable};

	my $reachable = 1;
	if (my $needs = $o->{steps}{$s}{needs}) {
	    my @l = ref($needs) ? @$needs : $needs;
	    $reachable = min(map { $o->{steps}{$_}{done} || 0 } @l);
	}
	$o->{steps}{$s}{reachable} = 1 if $reachable;
    }
    $o->{steps}{$step}{reachable} = $o->{steps}{$step}{redoable};

    while (my $f = shift @{$o->{steps}{$step}{toBeDone} || []}) {
	eval { &$f() };
	if (my $err = $@) {
	    $o->ask_warn(N("Error"), [
N("An error occurred, but I do not know how to handle it nicely.
Continue at your own risk."), formatError($err) || $err ]);
	}
    }
}

sub errorInStep { 
    my ($_o, $err) = @_;
    print "error :(\n"; 
    print "$err\n\n";
    c::_exit(1);
}
sub kill_action {}

#-######################################################################################
#- Steps Functions
#-######################################################################################
#------------------------------------------------------------------------------
sub selectLanguage {
    my ($o) = @_;

    $o->{locale}{langs} ||= { $o->{locale}{lang} => 1 };

    if (!exists $o->{locale}{country}) {
	lang::lang_changed($o->{locale});
    }

    add2hash_($o->{locale}, { utf8 => lang::utf8_should_be_needed($o->{locale}) });
    lang::set($o->{locale}, !$o->isa('interactive::gtk'));

    log::l("selectLanguage: pack_langs: ", lang::pack_langs($o->{locale}{langs}), " utf8-flag: ", to_bool($o->{locale}{utf8}));

    #- for auto_install compatibility with old $o->{keyboard} containing directly $o->{keyboard}{KEYBOARD}
    $o->{keyboard} = { KEYBOARD => $o->{keyboard} } if $o->{keyboard} && !ref($o->{keyboard});

    if (!$o->{keyboard} || $o->{keyboard}{unsafe}) {
	$o->{keyboard} = keyboard::default($o->{locale});
	$o->{keyboard}{unsafe} = 1;
	keyboard::setup_install($o->{keyboard});
    }

    $o->charsetChanged;

    addToBeDone {
	lang::write_langs($o->{locale}{langs});
    } 'formatPartitions';
    addToBeDone {
	lang::write_and_install($o->{locale}, $o->do_pkgs);
    } 'installPackages';
}
#------------------------------------------------------------------------------
sub selectKeyboard {
    my ($o) = @_;
    keyboard::setup_install($o->{keyboard});

    addToBeDone {
	#- the bkmap keymaps in installer are deficient, we need to load the real one before keyboard::write which will generate /etc/sysconfig/console/default.kmap
	run_program::rooted($::prefix, 'loadkeys', keyboard::keyboard2kmap($o->{keyboard}))
	    or log::l("loadkeys failed");
	keyboard::write($o->{keyboard});
    } 'installPackages' if !$o->{isUpgrade} || !$o->{keyboard}{unsafe};

    if ($o->{raw_X}) {
	require Xconfig::default;
	Xconfig::default::config_keyboard($o->{raw_X}, $o->{keyboard});
	$o->{raw_X}->write;
    }
}
#------------------------------------------------------------------------------
sub acceptLicense {}

#------------------------------------------------------------------------------
sub setupSCSI {
    my ($o) = @_;
    install::any::configure_pcmcia($o);
    modules::load(modules::category2modules('disk/cdrom'));
    modules::load_category($o->{modules_conf}, 'bus/firewire');
    modules::load_category($o->{modules_conf}, 'disk/scsi');
    #- load disk/scsi before disk/ide since libata is now the default
    #- (to prevent modules::load_category from loading ide-generic too early)
    modules::load_category($o->{modules_conf}, 'disk/ide|hardware_raid|sata|firewire');

    install::any::getHds($o);
}

#------------------------------------------------------------------------------
sub selectInstallClass {
    my ($o) = @_;

    if ($o->{partitioning}{use_existing_root} || $o->{isUpgrade}) {
	# either one root is defined (and all is ok), or we take the first one we find
	my $p = fs::get::root_($o->{fstab}) || (first(install::any::find_root_parts($o->{fstab}, $::prefix)) || die)->{part};
	$o->{migrate_device_names} = install::any::use_root_part($o->{all_hds}, $p);
	$o->{previous_release} = $p if $o->{isUpgrade};
    } 
}