update-mfs.in 4.35 KB
Newer Older
1 2 3 4
#!/usr/bin/perl -w

#
# Copyright (c) 2011 University of Utah and the Flux Group.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
# 
# {{{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/>.
# 
# }}}
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 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
#

#
# Update an MFS using a specific tarball
#
use lib '@srcdir@';
use English;
use libinstall;
use Getopt::Std;

my $MFSDIR = "/tftpboot/frisbee/boot";
my $UPDATEVERS = "33";

if ($UID != 0) {
    die("This script must be run as root.\n");
}
if (! -d "/mnt") {
   die("Need directory /mnt.\n");
}
if (! -d "/mnt/frisbee" && !mkdir("/mnt/frisbee")) {
   die("Cannot create directory /mnt/frisbee.\n");
}
if (! -e "$MFSDIR/mfsroot") {
   die("No $MFSDIR/mfsroot; where is your MFS?\n");
}
if (!chdir($MFSDIR)) {
   die("Cannot cd to $MFSDIR.\n");
}

# make a backup
print("Backing up mfsroot to mfsroot.bak ...\n");
if (system("cp -p mfsroot mfsroot.bak")) {
    die("Could not backup mfsroot.\n");
}

print("Configuring md device ...\n");
my $unit = `/sbin/mdconfig -a -t vnode -f mfsroot`;
if ($?) {
    die("Could not run mdconfig.\n");
}
if ($unit =~ /^md(\d+)$/) {
    $unit = $1;
} else {
    die("Malformed mdconfig output!?\n");
}

print("Mounting MFS ...\n");
if (system("mount /dev/md$unit /mnt/frisbee")) {
    system("mdconfig -d -u $unit");
    die("Could not mount /dev/md$unit.\n");
}

# check the current version
print("Checking version of MFS ...\n");
my $version = 0;
if (-e "/mnt/frisbee/etc/emulab/version") {
    my $vers = `cat /mnt/frisbee/etc/emulab/version`;
    if ($vers =~ /^(\d+)$/) {
	$version = $1;
    }
}
if ($version eq $UPDATEVERS) {
    print "MFS is already up to date (version $UPDATEVERS)\n";
    if (system("umount /mnt/frisbee") || system("mdconfig -d -u $unit")) {
	warn("Could not umount/unconfig MFS; do it manually.\n");
    } else {
	unlink("mfsroot.bak");
    }
    exit(0);
}

# Rename the backup mfsroot
my $mfsbackup = "mfsroot.v$version";
print("Rename old MFS backup ...\n");
if (system("mv mfsroot.bak $mfsbackup")) {
    if (system("umount /mnt/frisbee") || system("mdconfig -d -u $unit")) {
	warn("Could not umount/unconfig MFS; do it manually.\n");
    }
    die("Could not rename backup\n");
}

# Find the latest version of libc installed
print("Determining MFS libc version ...\n");
my $libc = `ls /mnt/frisbee/lib/libc.so.* | tail -1`;
if ($? || ($libc !~ /\.so\.(\d+)$/)) {
    if (system("umount /mnt/frisbee") || system("mdconfig -d -u $unit")) {
	warn("Could not umount/unconfig MFS.\n");
    }
    die("No libc in MFS!?");
} else {
    $libc = $1;
}

# fetch update tarball
my $tarball = "frisbee-$UPDATEVERS-libc$libc.tar.gz";
print("Fetching update tarball $tarball ...\n");
if (system("fetch -o /var/tmp http://www.emulab.net/downloads/$tarball")) {
    if (system("umount /mnt/frisbee") || system("mdconfig -d -u $unit")) {
	warn("Could not umount/unconfig MFS.\n");
    }
    die("No libc $libc update for MFS $UPDATEVERS.");
}

print("Installing tarball $tarball ...\n");
if (system("tar -C /mnt/frisbee -xvzf /var/tmp/$tarball")) {
    if (system("umount /mnt/frisbee") || system("mdconfig -d -u $unit")) {
	warn("Could not umount/unconfig MFS; fix it and ",
	     "move $mfsbackup to mfsroot.\n");
    } elsif (system("mv $mfsbackup mfsroot")) {
	warn("Could not restore old mfsroot; ",
	     "move $mfsbackup to mfsroot.\n");
    }
    die("Could not update to MFS $UPDATEVERS (libc version $libc).\n");
}

print("Update version ...\n");
system("echo $UPDATEVERS > /mnt/frisbee/etc/emulab/version");

print("Unmounting MFS ...\n");
if (system("umount /mnt/frisbee") || system("mdconfig -d -u $unit")) {
    die("Could not umount/unconfig MFS; fix and run ./prepare\n");
}

print("Preparing MFS ...\n");
system("./prepare");

print("MFS successfully updated!\n",
      "If it doesn't work, move mfsroot.v$version to mfsroot and run ./prepare\n");
exit(0);