package install_interactive; # $Id$ use diagnostics; use strict; use vars; use common qw(:common :functional); use partition_table qw(:types); use partition_table_raw; use detect_devices; use install_steps; use devices; use fsedit; use log; use fs; sub tellAboutProprietaryModules { my ($o) = @_; my @l = grep {$_} map { $_->{driver} =~ /^Bad:(.*)/ && $1 } detect_devices::probeall(); $o->ask_warn('', _("Some hardware on your computer needs ``proprietary'' drivers to work. You can find some information about them at: %s", join(", ", @l))) if @l; } sub partition_with_diskdrake { my ($o, $hds, $nowizard) = @_; my $ok; $o->set_help('partition_with_diskdrake'); do { $ok = 1; require diskdrake; diskdrake::main($hds, $o->{lvms}, $o->{raid}, interactive_gtk->new, $nowizard); delete $o->{wizard} and return partitionWizard($o, 'nodiskdrake'); my @fstab = fsedit::get_fstab(@$hds, @{$o->{lvms}}, $o->{raid}); unless (fsedit::get_root_(\@fstab)) { $ok = 0; $o->ask_okcancel('', _("You must have a root partition. For this, create a partition (or click on an existing one). Then choose action ``Mount point'' and set it to `/'"), 1) or return; } if (!grep { isSwap($_) } @fstab) { $o->ask_warn('', _("You must have a swap partition")), $ok=0 if !$::expert; $ok &&= $::expert || $o->ask_okcancel('', _("You don't have a swap partition\n\nContinue anyway?")); } } until $ok; 1; } sub partitionWizardSolutions { my ($o, $hds, $fstab, $readonly) = @_; my @wizlog; my (@solutions, %solutions); my $min_linux = 400 << 11; my $max_linux = 3000 << 11; my $min_swap = 50 << 11; my $max_swap = 300 << 11; my $min_freewin = 100 << 11; # each solution is a [ score, text, function ], where the function retunrs true if succeeded my @good_hds = grep { partition_table::can_raw_add($_) } @$hds; if (fsedit::free_space(@good_hds) > $min_linux and !$readonly) { $solutions{free_space} = [ 20, _("Use free space"), sub { fsedit::auto_allocate($hds); 1 } ] } else { push @wizlog, _("Not enough free space to allocate new partitions") . ": " . (@good_hds ? fsedit::free_space(@good_hds) . " < $min_linux" : "no harddrive on which partitions can be added") if !$readonly; } if (my @truefs = grep { isTrueFS($_) } @$fstab) { #- value twice the ext2 partitions $solutions{existing_part} = [ 6 + @truefs + @$fstab, _("Use existing partition"), sub { $o->ask_mntpoint_s($fstab) } ] } else { push @wizlog, _("There is no existing partition to use"); } my @fats = grep { isFat($_) } @$fstab; fs::df($_) foreach @fats; if (my @ok_forloopback = sort { $b->{free} <=> $a->{free} } grep { $_->{free} > $min_linux + $min_swap + $min_freewin } @fats) { $solutions{loopback} = [ -10 - @fats, _("Use the Windows partition for loopback"), sub { my ($s_root, $s_swap); my $part = $o->ask_from_listf('', _("Which partition do you want to use for Linux4Win?"), \&partition_table_raw::description, \@ok_forloopback) or return; $max_swap = $min_swap + 1 if $part->{free} - $max_swap < $min_linux; $o->ask_from_entries_refH('', _("Choose the sizes"), [ { label => _("Root partition size in MB: "), val => \$s_root, min => $min_linux >> 11, max => min($part->{free} - $max_swap, $max_linux) >> 11, type => 'range' }, { label => _("Swap partition size in MB: "), val => \$s_swap, min => $min_swap >> 11, max => $max_swap >> 11, type => 'range' }, ]) or return; push @{$part->{loopback}}, { type => 0x83, loopback_file => '/lnx4win/linuxsys.img', mntpoint => '/', size => $s_root << 11, device => $part, notFormatted => 1 }, { type => 0x82, loopback_file => '/lnx4win/swapfile', mntpoint => 'swap', size => $s_swap << 11, device => $part, notFormatted => 1 }; 1; } ]; $solutions{resize_fat} = [ 6 - @fats, _("Use the free space on the Windows partition"), sub { $o->set_help('resizeFATChoose'); my $part = $o->ask_from_listf('', _("Which partition do you want to resize?"), \&partition_table_raw::description, \@ok_forloopback) or return; $o->set_help('resizeFATWait'); my $w = $o->wait_message(_("Resizing"), _("Computing Windows filesystem bounds")); require resize_fat::main; my $resize_fat = eval { resize_fat::main->new($part->{device}, devices::make($part->{device})) }; $@ and die _("The FAT resizer is unable to handle your partition, the following error occured: %s", $@); my $min_win = $resize_fat->min_size; $part->{size} > $min_linux + $min_swap + $min_freewin + $min_win or die _("Your Windows partition is too fragmented, please run ``defrag'' first"); $o->ask_okcancel('', _("WARNING! DrakX will now resize your Windows partition. Be careful: this operation is dangerous. If you have not already done so, you should first exit the installation, run scandisk under Windows (and optionally run defrag), then restart the installation. You should also backup your data. When sure, press Ok.")) or return; my $size = $part->{size}; $o->ask_from_entries_refH('', _("Which size do you want to keep for windows on"), [ { label => _("partition %s", partition_table_raw::description($part)), val => \$size, min => $min_win >> 11, max => ($part->{size} - $min_linux - $min_swap) >> 11, type => 'range' }, ]) or return; $size <<= 11; local *log::l = sub { $w->set(join(' ', @_)) }; eval { $resize_fat->resize($size) }; $@ and die _("FAT resizing failed: %s", $@); $part->{size} = $size; $part->{isFormatted} = 1; my ($hd) = grep { $_->{device} eq $part->{rootDevice} } @$hds; $hd->{isDirty} = $hd->{needKernelReread} = 1; $hd->adjustEnd($part); partition_table::adjust_local_extended($hd, $part); partition_table::adjust_main_extended($hd); fsedit::auto_allocate($hds); 1; } ] if !$readonly; } else { push @wizlog, _("There is no FAT partitions to resize or to use as loopback (or not enough space left)") . @fats ? "\nFAT partitions:" . join('', map { "\n $_->{device} $_->{free} (" . ($min_linux + $min_swap + $min_freewin) . ")" } @fats) : ''; } if (@$fstab && !$readonly) { $solutions{wipe_drive} = [ 10, fsedit::is_one_big_fat($hds) ? _("Remove Windows(TM)") : _("Erase entire disk"), sub { $o->set_help('takeOverHdChoose'); my $hd = $o->ask_from_listf('', _("You have more than one hard drive, which one do you install linux on?"), \&partition_table_raw::description, $hds) or return; $o->set_help('takeOverHdConfirm'); $o->ask_okcancel('', _("ALL existing partitions and their data will be lost on drive %s", partition_table_raw::description($hd))) or return; partition_table_raw::zero_MBR($hd); fsedit::auto_allocate($hds); 1; } ]; } if (!$readonly && ref($o) =~ /gtk/) { #- diskdrake only available in gtk for now $solutions{diskdrake} = [ 0, _("Custom disk partitioning"), sub { partition_with_diskdrake($o, $hds, 'nowizard') } ]; } $solutions{fdisk} = [ -10, _("Use fdisk"), sub { $o->enter_console; foreach (@$hds) { print "\n" x 10, _("You can now partition %s. When you are done, don't forget to save using `w'", partition_table_raw::description($_)); print "\n\n"; my $pid = 0; if (arch() =~ /ppc/) { $pid = fork or exec "pdisk", devices::make($_->{device}); } else { $pid = fork or exec "fdisk", devices::make($_->{device}); } waitpid($pid, 0); } $o->leave_console; 0; } ] if $o->{partitioning}{fdisk}; log::l("partitioning wizard log:\n", (map { ">>wizlog>>$_\n" } @wizlog)); %solutions; } sub partitionWizard { my ($o, $nodiskdrake) = @_; $o->set_help('doPartitionDisks'); my %solutions = partitionWizardSolutions($o, $o->{hds}, $o->{fstab}, $o->{partitioning}{readonly}); if ($o->{lnx4win}) { if ($solutions{loopback}) { %solutions = (loopback => $solutions{loopback}); } else { $o->ask_warn('', _("You don't have enough free space on your Windows partition")) if grep { isFat($_) } @{$o->{fstab}} } } delete $solutions{diskdrake} if $nodiskdrake; my @solutions = sort { $b->[0] <=> $a->[0] } values %solutions; my $level = $::expert ? -9999 : 0; my @sol = grep { $_->[0] >= $level } @solutions; log::l( "solutions found: " . join('', map {$_->[1]} @sol) . " (all solutions found: " . join('', map {$_->[1]} @solutions) . ")"); @solutions = @sol if @sol > 1; log::l("solutions: ", int @solutions); @solutions or $o->ask_warn('', _("I can't find any room for installing")), die 'already displayed'; my $ok; while (!$ok) { log::l('HERE: ', join(',', map { $_->[1] } @solutions)); my $sol = $o->ask_from_listf('', _("The DrakX Partitioning wizard found the following solutions:"), sub { $_[0][1] }, \@solutions) or redo; log::l("partitionWizard calling solution $sol->[1]"); eval { $ok = $sol->[2]->() }; die if $@ =~ /setstep/; $ok &&= !$@; $@ and $o->ask_warn('', _("Partitioning failed: %s", $@)); } } sub upNetwork { my ($o, $pppAvoided) = @_; my $w = $o->wait_message('', _("Bringing up the network")); install_steps::upNetwork($o, $pppAvoided); } sub downNetwork { my ($o, $pppOnly) = @_; my $w = $o->wait_message('', _("Bringing down the network")); install_steps::downNetwork($o, $pppOnly); } 1; 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 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
package detect_devices; # $Id$

use diagnostics;
use strict;
use vars qw($pcitable_addons $usbtable_addons);

#-######################################################################################
#- misc imports
#-######################################################################################
use log;
use common;
use devices;
use c;

#-#####################################################################################
#- Globals
#-#####################################################################################
my @netdevices = map { my $l = $_; map { "$l$_" } (0..3) } qw(eth tr fddi plip);
my %serialprobe = ();

#-######################################################################################
#- Functions
#-######################################################################################
sub dev_is_devfs { -e "/dev/.devfsd" }


sub get {
    #- Detect the default BIOS boot harddrive is kind of tricky. We may have IDE,
    #- SCSI and RAID devices on the same machine. From what I see so far, the default
    #- BIOS boot harddrive will be
    #- 1. The first IDE device if IDE exists. Or
    #- 2. The first SCSI device if SCSI exists. Or
    #- 3. The first RAID device if RAID exists.

    getIDE(), getSCSI(), getDAC960(), getCompaqSmartArray(), getATARAID();
}
sub hds         { grep { $_->{media_type} eq 'hd' && ($::isStandalone || !isRemovableDrive($_)) } get() }
sub cdroms      { grep { $_->{media_type} eq 'cdrom' } get() }
sub burners     { grep { $_->{media_type} eq 'cdrom' && isBurner($_) } get() }
sub dvdroms     { grep { $_->{media_type} eq 'cdrom' && isDvdDrive($_) } get() }
sub zips        { grep { member($_->{media_type}, 'fd', 'hd') && isZipDrive($_) } get() }
#-sub jazzs     { grep { member($_->{media_type}, 'fd', 'hd') && isJazDrive($_) } get() }
sub ls120s      { grep { member($_->{media_type}, 'fd', 'hd') && isLS120Drive($_) } get() }

sub cdroms__faking_ide_scsi {
    my @l = cdroms();
    return @l if $::isStandalone;
    if (my @l_ide = grep { $_->{interface_type} eq 'ide' && isBurner($_) } @l) {
	require modules;
	modules::add_alias('scsi_hostadapter', 'ide-scsi');
	my $nb = 1 + max(-1, map { $_->{device} =~ /scd(\d+)/ } @l);
	foreach my $e (@l_ide) {	    
	    log::l("IDEBurner: $e->{device}");
	    $e->{device} = "scd" . $nb++;
	}
    }
    @l;
}
sub zips__faking_ide_scsi {
    my @l = zips();
    return @l if $::isStandalone;
    if (my @l_ide = grep { $_->{interface_type} eq 'ide' } @l) {
	require modules;
	modules::add_alias('scsi_hostadapter', 'ide-scsi');
	my $nb = 1 + max(-1, map { if_($_->{device} =~ /sd(\s+)/, ord($1) - ord('a')) } getSCSI());
	foreach my $e (@l_ide) {	    
	    log::l("IDE Zip: $e->{device}");
	    $e->{device} = "sd" . chr(ord('a') + $nb++);
	}
    }
    @l;
}

sub floppies() {
    require modules;
    eval { modules::load("floppy") };
    my @fds = map {
	my $info = (!dev_is_devfs() || -e "/dev/$_") && c::floppy_info(devices::make($_));
	if_($info && $info ne '(null)', { device => $_, media_type => 'fd', info => $info })
    } qw(fd0 fd1);
    my @ide = ls120s() and eval { modules::load("ide-floppy") };

    eval { modules::load("usb-storage") } if usbStorage();
    my @scsi = grep { $_->{media_type} eq 'fd' } getSCSI();
    @ide, @scsi, @fds;
}
sub floppies_dev() { map { $_->{device} } floppies() }
sub floppy { first(floppies_dev()) }
#- example ls120, model = "LS-120 SLIM 02 UHD Floppy"

sub get_sys_cdrom_info {
    my (@drives) = @_;

    my @drives_order;
    foreach (cat_("/proc/sys/dev/cdrom/info")) {
	my ($t, $l) = split ':';
	my @l = split ' ', $l;
	if ($t eq 'drive name') {
	    @drives_order = map {
		s/^sr/scd/;
		my $dev = $_;
		first(grep { $_->{device} eq $dev } @drives);
	    } @l;
	} else {
	    my $capacity;
	    if ($t eq 'Can write CD-R') {
		$capacity = 'burner';
	    } elsif ($t eq 'Can read DVD') {
		$capacity = 'DVD';
	    }
	    if ($capacity) {
		each_index {
		    ($drives_order[$::i] || {})->{capacity} .= "$capacity " if $_;
		} @l;
	    }
	}
    }
}

sub isBurner { 
    my ($e) = @_;
    $e->{capacity} =~ /burner/ and return 1;
      
    #- do not work for SCSI
    my $f = tryOpen($e->{device}); #- SCSI burner are not detected this way.
    $f && c::isBurner(fileno($f));
}
sub isDvdDrive {
    my ($e) = @_;
    $e->{capacity} =~ /DVD/ || $e->{info} =~ /DVD/ and return 1;

    #- do not work for SCSI
    my $f = tryOpen($e->{device});
    $f && c::isDvdDrive(fileno($f));
}
sub isZipDrive { $_[0]->{info} =~ /ZIP\s+\d+/ } #- accept ZIP 100, untested for bigger ZIP drive.
#-sub isJazzDrive { $_[0]->{info} =~ /JAZZ?\s+/ } #- untested.
sub isLS120Drive { $_[0]->{info} =~ /LS-?120|144MB/ }
sub isRemovableDrive { &isZipDrive || &isLS120Drive || $_[0]->{media_type} eq 'fd' } #-or &isJazzDrive }

sub isFloppyOrHD {
    my ($dev) = @_;
    require partition_table_raw;
    my $geom = partition_table_raw::get_geometry(devices::make($dev));
    $geom->{totalsectors} < 10 << 11 ? 'fd' : 'hd';
}

sub getSCSI() {
    my @drives;
    my ($driveNum, $cdromNum, $tapeNum) = qw(0 0 0);
    my $err = sub { chop; die "unexpected line in /proc/scsi/scsi: $_"; };
    local $_;

    local *F;
    open F, "/proc/scsi/scsi" or return;
    local $_ = <F>; /^Attached devices:/ or return &$err();
    while ($_ = <F>) {
	my ($id) = /^Host:.*?Id: (\d+)/ or return &$err();
	$_ = <F>; my ($vendor, $model) = /^\s*Vendor:\s*(.*?)\s+Model:\s*(.*?)\s+Rev:/ or return &$err();
	$_ = <F>; my ($type) = /^\s*Type:\s*(.*)/ or &$err();
	my $device;
	if ($type =~ /Direct-Access/) { #- what about LS-120 floppy drive, assuming there are Direct-Access...
	    $device = "sd" . chr($driveNum++ + ord('a'));
	    $type = isFloppyOrHD($device);
	} elsif ($type =~ /Sequential-Access/) {
	    $device = "st" . $tapeNum++;
	    $type = 'tape';
	} elsif ($type =~ /(CD-ROM|WORM)/) {
	    $device = "scd" . $cdromNum++;
	    $type = 'cdrom';
	}
	$device and push @drives, { device => $device, media_type => $type, info => "$vendor $model", id => $id, bus => 0 };
    }
    get_sys_cdrom_info(@drives);
    @drives;
}

sub getIDE() {
    my @idi;

    #- what about a system with absolutely no IDE on it, like some sparc machine.
    -e "/proc/ide" or return ();

    #- Great. 2.2 kernel, things are much easier and less error prone.
    foreach my $d (sort @{[glob_('/proc/ide/hd*')]}) {
	cat_("$d/driver") =~ /ide-scsi/ and next; #- already appears in /proc/scsi/scsi
	my $t = chomp_(cat_("$d/media"));
	my $type = $ {{disk => 'hd', cdrom => 'cdrom', tape => 'tape', floppy => 'fd'}}{$t} or next;
	my $info = chomp_(cat_("$d/model")) || "(none)";

	my $num = ord (($d =~ /(.)$/)[0]) - ord 'a';
	push @idi, { media_type => $type, device => basename($d), info => $info, bus => $num/2, id => $num%2, interface_type => 'ide' };
    }
    get_sys_cdrom_info(@idi);
    @idi;
}

sub getCompaqSmartArray() {
    my (@idi, $f);

    foreach ('array/ida', 'cpqarray/ida', 'cciss/cciss') {
	my $prefix = "/proc/driver/$_"; #- kernel 2.4 places it here
	$prefix = "/proc/$_" if !-e "${prefix}0"; #- kernel 2.2

	my ($name) = m|/(.*)|;
	for (my $i = 0; -r ($f = "${prefix}$i"); $i++) {
	    foreach (cat_($f)) {
		if (m|^\s*($name/.*?):|) {
		    push @idi, { device => $1, info => "Compaq RAID logical disk", media_type => 'hd', interface_type => 'ida' };
		}
	    }
	}
    }
    @idi;
}

sub getDAC960() {
    my %idi;

    #- We are looking for lines of this format:DAC960#0:
    #- /dev/rd/c0d0: RAID-7, Online, 17928192 blocks, Write Thru0123456790123456789012
    foreach (syslog()) {
	my ($device, $info) = m|/dev/(rd/.*?): (.*?),| or next;
	$idi{$device} = { info => $info, media_type => 'hd', device => $device, interface_type => 'dac960' };
    }
    values %idi;
}

sub getATARAID {
    my %l;
    foreach (syslog()) {
	my ($device) = m|^\s*(ataraid/d\d+):| or next;
	$l{$device} = { info => 'ATARAID block device', media_type => 'hd', device => $device, interface_type => 'ataraid' };
	log::l("ATARAID: $device");
    }
    values %l;
}

sub getNet() {
#      my @a;
#      foreach (@netdevices) {
#  	$::isStandalone && /plip/ and next;
#  	print (" hhhhh $_ \n");
#  	/ippp/ and run_program::rooted("", "/sbin/isdnctrl addif $_");
#  	c::hasNetDevice($_) and push @a, $_;
#      }
#      /ippp/ and run_program::rooted("", "/sbin/isdnctrl delif $_") foreach @netdevices;
#      @a;
    grep { !(($::isStandalone || $::live) && /plip/) && c::hasNetDevice($_) } @netdevices;
}

#sub getISDN() {
#    mapgrep(sub {member (($_[0] =~ /\s*(\w*):/), @netdevices), $1 }, split(/\n/, cat_("/proc/net/dev")));
#}

$pcitable_addons = <<'EOF';
# add here lines conforming the pcitable format (0xXXXX\t0xXXXX\t"\w+"\t".*")
EOF

$usbtable_addons = <<'EOF';
# add here lines conforming the usbtable format (0xXXXX\t0xXXXX\t"\w+"\t".*")
EOF

sub add_addons {
    my ($addons, @l) = @_;

    foreach (split "\n", $addons) {
	/^\s/ and die "bad detect_devices::probeall_addons line \"$_\"";
	s/^#.*//;
	s/"(.*?)"/$1/g;
	next if /^$/;
	my ($vendor, $id, $driver, $description) = split("\t", $_, 4) or die "bad detect_devices::probeall_addons line \"$_\"";
	foreach (@l) {
	    $_->{vendor} == hex $vendor && $_->{id} == hex $id or next;
	    put_in_hash($_, { driver => $driver, description => $description });
	}
    }
    @l;
}

sub pci_probe {
    my ($probe_type) = @_;
    log::l("full pci_probe") if $probe_type;
    add_addons($pcitable_addons, map {
	my %l;
	@l{qw(vendor id subvendor subid pci_bus pci_device pci_function media_type driver description)} = split "\t";
	$l{$_} = hex $l{$_} foreach qw(vendor id subvendor subid);
	$l{bus} = 'PCI';
	\%l