newclient 19.4 KB
Newer Older
1 2
#!/usr/bin/perl -w
#
3
# Copyright (c) 2003-2013 University of Utah and the Flux Group.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
# 
# {{{EMULAB-LICENSE
# 
# This file is part of the Emulab network testbed software.
# 
# This file is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
# 
# This file is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public
# License for more details.
# 
# You should have received a copy of the GNU Affero General Public License
# along with this file.  If not, see <http://www.gnu.org/licenses/>.
# 
# }}}
23 24 25 26 27 28
#

#
# Client-side script to report a new node into a testbed
#
#
29 30 31 32 33 34 35 36

use strict;

#
# Constants and the like
#
my $PREFIX;
my $TMCC;
37 38

if (-e "/usr/local/etc/emulab") {
39 40
    $PREFIX = "/usr/local/etc/emulab";
    $TMCC = "$PREFIX/tmcc.bin";
41 42
}
else {
43 44
    $PREFIX = "/etc/emulab";
    $TMCC = "$PREFIX/tmcc";
45
}
46

47
my $FETCH = "/usr/bin/fetch";
48
my $WGET = '/usr/bin/wget';
49

50
my $FORMURL = "newnodecheckin.php";
51

52 53 54
my $IFCONFIG = "/sbin/ifconfig";
my $DMESG = "/var/run/dmesg.boot";
my $TEACHSWITCH = "$PREFIX/teachswitch";
55

56 57 58 59 60 61

my $FLOPPYDEV  = "/dev/fd0";
my $FORMAT     = "/sbin/newfs_msdos";
my $MOUNTPOINT = "/mnt";
my $MOUNT      = "/sbin/mount";
my $UNMOUNT    = "/sbin/umount";
62
my $IBSTAT     = "/usr/bin/ibstat";
63

64
#
65
# Error codes, and how many times we beep to indicate each one.
66
#
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
my $ERROR_USAGE       = 0;
my $ERROR_NOBOSS      = 1;
my $ERROR_HWPROB      = 2;
my $ERROR_NODHCP      = 3;
my $ERROR_FLOPPYWRITE = 4;
my $ERROR_CHECKIN     = 5;

my %BEEP_CODES = (
    # 1 beep means okay, 2 means stick in a floppy
    $ERROR_NODHCP      => 3,
    $ERROR_NOBOSS      => 3,
    $ERROR_CHECKIN     => 3,
    $ERROR_FLOPPYWRITE => 4,
    $ERROR_HWPROB      => 4,
    $ERROR_USAGE       => 4
);
my $UNKNOWN_ERROR_BEEPS = 6;

85 86 87
my $MAXIMUM_BEEP_TIME   = 60 * 60; # In seconds - one hour
my $BEEP_SLEEP          = 3; # In seconds

88 89 90 91 92 93
#
# Constants for use in determining if the floppy is in or not
#
my $FLOPPY_REMOVED  = 0;
my $FLOPPY_INSERTED = 1;

94 95 96 97 98
my $FBSD_VERS = 8;
if (`uname -r` =~ /^(\d+)\.\d+/) {
    $FBSD_VERS = $1;
}

99 100 101 102 103 104 105
#
# What type of client this node is. Possible values:
# testnode - The default, a regular testbed node
# ops      - The node checking in is actually ops
#
my $client_type = "testnode";

106 107 108 109 110 111 112 113 114
#
# Handle command-line arguments - we do them ourselves unstead of using getopt,
# because we want to be able to run this on minimalist environments where that
# may not exist.
#
my $headless	= 0;
my $writefloppy	= 0;
my $testing	= 0;
my $bossnode	= "";
115
my $checkib	= 0;
116

117 118 119 120 121 122 123 124
my $uniquifier;
foreach my $arg (@ARGV) {
    if ($arg =~ s/^-//) {
	foreach my $letter (split('',$arg)) {
	    SWITCH: for ($letter) {
		/h/ && do { $headless = 1; last; };
		/w/ && do { $writefloppy = 1; last; };
		/t/ && do { $testing = 1; last; };
125
		/I/ && do { $checkib = 1; last; };
126
		/o/ && do { $client_type = "ops"; last; };
127 128
	    };
	}
129
    } else {
130 131 132 133 134
	if ($uniquifier) {
	    exit usage();
	} else {
	    $uniquifier = $arg;
	}
135 136 137
    }
}

138
sub usage {
139
    print "Usage: $0 [-h] [-w] [-t] [-o] [identifer]\n";
140 141 142
    print " -h         - Run headless (beep codes for errors)\n";
    print " -w         - Write an the node's unique identifier to a floppy\n";
    print " -t         - Test mode\n";
143
    print " -o         - 'ops' mode\n";
144 145 146 147
    print " identifier - supply the given identifier to boss\n";
    1;
}

148 149 150 151 152 153 154 155 156 157
#
# XXX for now, auto detect whether we should check for IB.
# Otherwise you have to modify the FS of the MFS to add the option..messy!
#
if (system("sysctl -q sys.class.infiniband_verbs.abi_version") == 0) {
    $checkib = 1;
} else {
    $checkib = 0;
}

158
#
159 160 161
# Find out what our boss node was, so we don't have to hardcode it here - allow
# the user to provide one on the command line, though, so that we can run tests
# on nodes that aren't in the testbed
162 163
#
if (!$bossnode) {
164 165
    my $bossinfo = `$TMCC bossinfo`;
    $bossinfo =~ /^([\w\-.]+)/;
166
    $bossnode = $1;
167 168 169
    if (!$bossnode) {
	error_fatal($ERROR_NOBOSS,"Unable to parse boss name from '$bossinfo'\n");
    }
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
#
# XXX we can only handle infiniband user FreeBSD 9 or above and
# only if we have the ibstat tool installed.
#
if ($checkib) {
    if ($FBSD_VERS < 9 || ! -x "$IBSTAT") {
	message("WARNING: not running IB-enabled newnode MFS, disabled IB collection.\n");
	$checkib = 0;
    }
    #
    # XXX if IB checking is enabled and we have a Mellanox VPI-enabled
    # card, put the card in autodetect.
    #
    elsif (!$testing) {
	my @out = `sysctl sys.device | grep mlx4_core`;
	foreach my $line (@out) {
	    if ($line =~ /^(sys.device.mlx4_core0.mlx4_port)(\d+):\s+(\S+)/) {
		if (($3 eq "ib" || $3 eq "eth") && system("sysctl $1$2=auto")) {
		    message("WARNING: Could not put mlx4 port $2 in auto\n");
		} else {
		    sleep(1);
		}
	    }
	}
    }
}

199
#
200
# Get physical information about this node
201
#
202 203 204
my @ifaces = find_interfaces();
my ($diskdev, $disksize) = get_disksize();
my $speed = get_cpuspeed();
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
#
# For some node types, we may already know the node_id we're supposed to use
#
my $node_id;
if ($client_type eq "ops") {
    $node_id = "ops";
}

#
# For some node types, we know we're supposed to use our 'temporary' IP as the
# permanent one - take care of those now
#
my $use_temp_IP = 0;
if ($client_type eq "ops") {
    $use_temp_IP = 1;
}

#
# Also, keep track of the role this node will have in the testbed
#
my $role = "testnode";
if ($client_type eq "ops") {
    $role = "ctrlnode";
}

#
# In some cases, we may know the node's type in advance
#
my $type;
if ($client_type eq "ops") {
    $type = "ops";
}

239 240 241 242
#
# Start the program that will annouce us to the switch, so that it learns
# MAC addresses, etc.
#
243
if (!$testing && ($client_type ne "ops")) {
244 245
    teachswitch();
}
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
#
# Report this stuff back to the web script on boss - build up a URL to do so
#
my $URL = buildURL();

print "URL is $URL\n";
my $identifier;
if (!$testing) {
    $identifier = checkin($URL);
} else {
    # Just make one up for testing purposes
    $identifier = 314;
}

print "This node's identifier is: $identifier\n";

if ($writefloppy) {
    writefloppy($identifier);
}

#
# Little subroutine to URL encode data we're sending through the web interface
#
sub urlencode {
    my ($string) = @_;
    my @chars = split //, $string;
    my $encoded = "";
    foreach my $char (@chars) {
	if ($char =~ /[0-9a-zA-Z]/) {
	    $encoded .= $char;
	} else {
	    $encoded .= sprintf "%%%02X", ord($char);
	}
280 281
    }

282
    return $encoded;
283 284 285
}

#
286 287
# Return a URL containing the data we've collected, to be fetched from boss to
# check in
288
#
289 290 291 292 293
sub buildURL {
    my $URL = "http://$bossnode/$FORMURL?";
    $URL .= "cpuspeed=" . urlencode($speed);
    $URL .= "&diskdev=" . urlencode($diskdev);
    $URL .= "&disksize=" . urlencode($disksize);
294 295 296 297
    $URL .= "&role=" . urlencode($role);
    if ($node_id) {
	$URL .= "&node_id=" . urlencode($node_id);
    }
298 299 300
    if ($uniquifier) {
	$URL .= "&identifier=". urlencode($uniquifier);
    }
301 302 303 304 305 306
    if ($use_temp_IP) {
	$URL .= "&use_temp_IP=1";
    }
    if ($type) {
	$URL .= "&type=" . urlencode($type);
    }
307 308 309 310

    my $ifaceindex = 0;
    foreach my $aref (@ifaces) {
	my ($iface, $mac, $status) = @$aref;
311 312
	my $driver = get_iface_driver($iface);
	$URL .= "&ifacedriver$ifaceindex=" . urlencode($driver);
313 314 315 316 317 318 319 320
	$URL .= "&ifacename$ifaceindex=" . urlencode($iface);
	$URL .= "&ifacemac$ifaceindex=" . urlencode($mac);
	$ifaceindex++;
    }

    $URL .= "&messages=" . urlencode(join "", @::messages);

    return $URL;
321 322 323
}

#
324
# Actually check in - return this node's ID if we get one
325
#
326 327
sub checkin {
    my ($URL) = @_;
328
    my $checkin_command = "$FETCH -o '-' '$URL'";
329 330
    
    if (! -x $FETCH) {
331
        $checkin_command = "$WGET -O '-' --quiet '$URL'";
332
    }
333
    open(CHECKIN,"$checkin_command|")
334 335 336 337 338 339 340
	or error_fatal($ERROR_CHECKIN,"Unable to check in with boss!\n");
    my $id = undef;
    while (<CHECKIN>) {
	print;
	if (/Node ID is (\d+)/) {
	    $id = $1;
	}
341
    }
342
    return $id;
343 344
}

345 346 347 348
######################################################################
# Node hardware functions
######################################################################

349
#
350 351
# Grab a list of all interfaces on this node - returns a list of [$iface, $mac,
# $status] for each one
352
#
353 354 355 356 357 358 359 360 361 362
sub find_interfaces
{
    if ($^O eq 'freebsd') {
        freebsd_find_interfaces(@_);
    }
    elsif ($^O eq 'linux') {
        linux_find_interfaces(@_);
    }
}

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
sub linux_get_iface_driver
{
    my ($iface) = @_;
    my $link = "/sys/class/net/$iface/device/driver";
    my $driver = undef;
    
    if (-l $link) {
    	$driver = (split(/\//, readlink($link)))[-1];
    }
    
    return $driver;
}

sub freebsd_get_iface_driver
{
    my ($iface) = @_;
    
    $iface =~ s/\d+$//;
    
    return $iface;
}

sub get_iface_driver
{
    if ($^O eq 'freebsd') {
        freebsd_get_iface_driver(@_);
    }
    elsif ($^O eq 'linux') {
        linux_get_iface_driver(@_);
    }
}

395
sub freebsd_find_interfaces {
396 397 398 399 400 401
    #
    # Grab this node's MAC addresses - we'll just parse them from the output of
    # ifconfig for now.
    #
    my @ifconfig_lines = `$IFCONFIG`;
    my @ifaces;
402
    my @ibifaces;
403 404
    my $iface = "";
    my $mac;
405
    my $isib = 0;
406 407 408 409 410
    my $status;
    foreach my $line (@ifconfig_lines) {
	chomp $line;
	SWITCH: for ($line) {
	    #
411
	    # A line beginning a new interface.
412 413 414 415 416 417 418
	    #
	    (/^(\w+):/) && do {
		if ($iface) {
		    #
		    # We have an old interface to save away, but only if it's
		    # Ethernet
		    #
419
		    if ($mac && !$isib) {
420 421 422 423 424 425
			push @ifaces, [$iface, $mac, $status];
		    }
		}

		$iface = $1;
		$mac = $status = "";
426 427 428 429 430 431 432 433 434 435 436 437

		#
		# XXX we ignore IPoIB (ibN) here if we are checking for IB.
		# We will use ibstat below to collect the info.
		#
		if ($checkib && $iface =~ /^ib\d+/) {
		    push @ibifaces, $iface;
		    $isib = 1;
		} else {
		    $isib = 0;
		}

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
		last SWITCH;
	    };

	    #
	    # A line containing a MAC address
	    #
	    (/\s+ether ([0-9a-f:]+)/) && do {
		$mac = $1;
		$mac =~ s/://g;
		if (length($mac) != 12) {
		    error($ERROR_HWPROB,"Malformed MAC $mac\n");
		    $mac = undef;
		}
		last SWITCH;
	    };
	
	    #
	    # A line containing the interface status
	    #
	    (/^\s+status: (.*)/) && do {
		$status = $1;
		last SWITCH;
	    };
	}

    }

    #
    # Get the last one
467
    #
468
    if ($iface && $mac && !$isib) {
469 470 471
	push @ifaces, [$iface, $mac, $status];
    }

472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491
    #
    # For infiniband, interfaces will show up as IPoIB (ibN) devices.
    # We attempt to canonicalize the info here, by returning "ib?" and
    # using ibstat to get the port GUID and returning that for the MAC.
    #
    if (@ibifaces > 0) {
	my $ibix = 0;
	my @ibout = `$IBSTAT -p`;
	chomp @ibout;
	foreach my $line (@ibout) {
	    if ($line =~ /^0x([0-9a-fA-F]{16})/) {
		my $mac = $1;
		my $iface = "ib$ibix";
		$ibix++;
		push @ifaces, [$iface, $mac, "down"];
		next;
	    }
	}
    }

492 493
    #
    # Warn about any that don't have carrier
494
    #
495 496 497 498 499
    foreach my $aref (@ifaces) {
	my ($iface, $mac, $status) = @$aref;
	if ($status ne "active") {
	    message("WARNING: $iface has no carrier!\n");
	}
500
    }
501 502

    return @ifaces;
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
sub linux_find_interfaces {
    my @ifconfig_lines = `$IFCONFIG -a`;
    my @ifaces;
    my $status;

    for (@ifconfig_lines) {
        chomp;
        next if /^\s+/ || /^$/;
        next unless /^(\S+)\s+Link encap:Ethernet\s+HWaddr\s+([0-9a-fA-F:]+).*$/;
        my ($iface, $mac) = ($1, $2);
        my $status;
        $mac =~ s/://g;
        if (length($mac) != 12) {
            error($ERROR_HWPROB,"Malformed MAC $mac\n");
        }
        if ( -f "/sys/class/net/$iface/carrier" ) {
            $status = 0;
            if (open CARRIER, "/sys/class/net/$iface/carrier") {
                $status = <CARRIER>;
                if (defined $status) {
                    chomp $status;
                }
                else {
                    $status = 0;
                }
                close CARRIER;
            }
        }

        push @ifaces, [$iface, $mac, $status];
    }
    #
    # Warn about any that don't have carrier
    #
    foreach my $aref (@ifaces) {
        my ($iface, $mac, $status) = @$aref;
        if ($status ne "1") {
            message("WARNING: $iface has no carrier!\n");
        }
    }

    return @ifaces;
}

548

549 550 551 552
#
# Figure out the disk drive size - returns a pair containing the disk device
# (ie. ad0 or da1) and the size in megabytes
#
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
sub get_disksize
{
    if ($^O eq 'freebsd') {
        freebsd_get_disksize(@_);
    }
    elsif ($^O eq 'linux') {
        linux_get_disksize(@_);
    }
}

sub linux_get_disksize {
    my @disks;
    my $size;

    @disks = sort(glob "/sys/block/sd*");
    push @disks, sort(glob "/sys/block/hd*");
    @disks = map { s#/sys/block/##; $_ } @disks;

    if (@disks == 0) {
        message("WARNING: Unable to find disk drive\n");
        return ("unknown", 0);
    }

    if (open SIZE, "/sys/block/$disks[0]/size") {
        $size = <SIZE>;
        chomp $size;
        close SIZE;
    }
    else {
        message("WARNING: Unable to get size of disk drive\n");
        return ("unknown", 0);
    }

    $size = int($size * 512 / 1024 / 1024);

    return ($disks[0], $size);
}

sub freebsd_get_disksize {
592 593 594 595 596 597 598 599
    if (!open(DMESG,"<$DMESG")) {
	error($ERROR_HWPROB,"Unable to open $DMESG\n");
	return ("",0);
    }
    my ($diskdev, $disksize);
    while (<DMESG>) {
	chomp;
	#
600 601
	# FreeBSD 9.x and above have renamed IDE and SATA devs.
	# Use the compat name for now.
602
	#
603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620
	if (/^(ada\d+): (\d+)MB/) {
	    $diskdev = $1;
	    $disksize = $2;
	    next;
	}
	if (/^(ada\d+): Previously was known as (ad\d+)/) {
	    if (defined($disksize) && defined($diskdev) && $1 eq $diskdev) {
		$diskdev = $2;
	    } else {
		$diskdev = "";
	    }
	    last;
	}

	#
	# Take the first disk of a recognized type
	#
	if (/^((ad|da|ar|aacd|amrd|mfid|mfisyspd)\d+): (\d+)MB/) {
621 622 623 624 625 626 627 628 629 630 631 632 633
	    $diskdev = $1;
	    $disksize = $3;
	    last;
	}
    }

    if (!$diskdev) {
	message("WARNING: Unable to find disk drive\n");
	$diskdev = "unknown";
	$disksize = 0;
    }

    return ($diskdev, $disksize);
634 635 636
}

#
637
# Find this node's CPU speed - returns the speed in MHz
638
#
639 640 641 642 643 644 645 646 647 648 649
sub get_cpuspeed
{
    if ($^O eq 'freebsd') {
        freebsd_get_cpuspeed(@_);
    }
    elsif ($^O eq 'linux') {
        linux_get_cpuspeed(@_);
    }
}

sub freebsd_get_cpuspeed {
650 651 652 653 654 655 656 657 658 659 660 661 662
    if (!open(DMESG,"<$DMESG")) {
	error($ERROR_HWPROB,"Unable to open $DMESG\n");
	return (0);
    }
    my $speed = 0;
    while (<DMESG>) {
	if (/^CPU:.*\((\d+(\.\d+)?)+\-MHz/) {
	    $speed = $1;
	    last;
	}
    }
    return $speed;
}
663

664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679
sub linux_get_cpuspeed {
    if (!open(CPUINFO,"/proc/cpuinfo")) {
        error($ERROR_HWPROB,"Unable to open /proc/cpuinfo\n");
        return (0);
    }
    my $speed = 0;
    while (<CPUINFO>) {
        if (/^cpu MHz\s*:\s*(\d+)(:?\.\d+)?.*$/) {
            $speed = $1;
            last;
        }
    }
    return $speed;
}


680
#
681
# Teach the switch where we are.
682
#
683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705
sub teachswitch {
    system "$TEACHSWITCH &" and message("Unable to start teachswitch: $!\n");

    #
    # Sleep for a few seconds to give teachswitch the chance to do its thing
    # before anyone comes looking
    #
    sleep(10);
}

######################################################################
# Error functions
######################################################################

# 
# Print a message, and send it on to the script we're checking in with
# 
sub message {
    my ($message) = @_;
    print $message;
    push @::messages, $message;
}

706

707
#
708
# Report an error, which is non-fatal
709
#
710 711 712
sub error {
    my ($errno, $string) = @_;

713 714 715 716
    my $errstr = "*** Error $errno: $string\n"; 

    print STDERR $errstr;
    push @::messages, $errstr;
717 718 719 720 721

    #
    # Maybe we ought to beep when headless - right now, we do this only for
    # fatal errors, but maybe we should do it for all of them.
    #
722

723
}
724

725
#
726
# Same as above, but a fatal error
727
#
728
sub error_fatal {
729
    my ($errno, $string) = @_;
730 731 732
    my $errstr =  "*** Fatal Error $errno: $string\n";
    print STDERR $errstr;
    push @::messages, $errstr;
733 734 735 736 737

    #
    # If headless, beep to let the user know what went wrong
    #
    if ($headless) {
738 739 740 741 742
	# Try to write the messages to the floppy
	if ($writefloppy) {
	    writefloppy_messages();
	}

743 744 745 746 747
	my $beeps = $BEEP_CODES{$errno} || $UNKNOWN_ERROR_BEEPS;
	beep($beeps);
    } else {
	exit 1;
    }
748
}
749

750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766
######################################################################
# Functions for dealing with the floppy drive
######################################################################

#
# Beep to let the user know that they are expected to insert or remove the
# floppy.
#
sub wait_for_floppy {
    my ($state) = @_;
    if ($state == $FLOPPY_INSERTED) {
	print "Waiting for user to insert a floppy\n";
	beep(2,$state);
    } else {
	print "Waiting for user to remove the floppy\n";
	beep(1,$state);
    }
767 768
}

769 770 771 772 773
#
# Write out the node's identifier to a floppy
#
sub writefloppy {
    my ($id) = @_;
774

775 776 777 778 779 780
    #
    # Make sure the floppy is readable
    #
    if (!checkfloppy()) {
	wait_for_floppy($FLOPPY_INSERTED);
    }
781

782 783 784 785 786 787
    #
    # Format that sucker
    #
    if (system "$FORMAT $FLOPPYDEV") {
	error_fatal($ERROR_FLOPPYWRITE,"Floppy format failed: $!\n");
    }
788
    $::floppy_formatted = 1;
789

790 791 792 793 794 795
    #
    # Drop a file down with the unique identifier we were provided
    #
    if (system "$MOUNT -t msdos $FLOPPYDEV $MOUNTPOINT") {
	error_fatal($ERROR_FLOPPYWRITE,"Failed to mount floppy: $!\n");
    }
796
    $::floppy_mounted = 1;
797 798 799 800 801 802 803

    if (!open(IDFILE,">$MOUNTPOINT/node.id")) {
	error_fatal($ERROR_FLOPPYWRITE,"Failed to write floppy: $!\n");
    }

    print IDFILE "$id\n";
    close(IDFILE);
804 805 806 807 808 809

    #
    # Write out any messages we had, too
    #
    writefloppy_messages();

810
    system "$UNMOUNT $MOUNTPOINT";
811
    $::floppy_mounted = 0;
812 813 814 815 816 817 818

    #
    # Let the user know we're done
    #
    wait_for_floppy($FLOPPY_REMOVED);

    print "Floppy written!\n";
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
#
# Write out a log of messages to the floppy - check to see if any of the basic
# tasks have already been done by writefloppy, and avoid re-doing them if they
# have. If we fail, just silently return, we have no way to tell anyone why
# we failed.
#
sub writefloppy_messages {

    #
    # Format that sucker
    #
    if (!$::floppy_mounted && !$::floppy_formatted) {
	if (system "$FORMAT $FLOPPYDEV") {
	    return;
	}
    }

    #
    # Drop a file down with the unique identifier we were provided
    #
    my $mounted_floppy = 0;
    if (!$::floppy_mounted) {
	$mounted_floppy = 1;
	if (system "$MOUNT -t msdos $FLOPPYDEV $MOUNTPOINT") {
	    return;
	}
    }

    if (!open(MFILE,">$MOUNTPOINT/messages")) {
	return;
    }

    print MFILE join("",@::messages), "\n";
    close(MFILE);

    if ($mounted_floppy) {
	system "$UNMOUNT $MOUNTPOINT";
    }

    return;

}

864
#
865 866 867
# Beep for a while - first argument is the number of beeps in each group.  If
# the second argument is non-zero, will stop beeping and return when the floppy
# is inserted or removed
868
#
869 870 871 872 873 874
sub beep {
    my ($count, $exitOnChange) = @_;

    $count = 1 unless $count;

    my $floppystate;
875 876
    my $iters = 0;
    my $maxiters = $MAXIMUM_BEEP_TIME / $BEEP_SLEEP;
877 878 879 880 881 882 883 884
    while (1) {
	foreach my $i (1 .. $count) {
	    syswrite STDOUT, "\a";
	    select(undef, undef, undef,.2);
	}

	if (defined($exitOnChange) && (checkfloppy() == $exitOnChange)) {
	    return;
885
	}
886

887 888 889 890
	sleep($BEEP_SLEEP);
	if ($iters++ > $maxiters) {
	    die("Timed out\n");
	}
891
    }
892
}
893

894 895 896 897 898 899 900
#
# Check to see if the floppy is readable - 1 if it is, 0 if not
#
sub checkfloppy {
    open(FLOPPY,"<$FLOPPYDEV") or return $FLOPPY_REMOVED;
    close(FLOPPY);
    return $FLOPPY_INSERTED;
901
}