restorevm.pl 5.88 KB
Newer Older
1 2 3
#!/usr/bin/perl -w
#
# Copyright (c) 2009-2012 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 29 30 31 32 33 34 35 36
#
use strict;
use Getopt::Std;
use English;
use Errno;
use Data::Dumper;

sub usage()
{
    print "Usage: restorevm.pl [-d] vnodeid path\n" . 
	  "  -d   Debug mode.\n".
	  "  -i   Info mode only\n";
    exit(-1);
}
Leigh B Stoller's avatar
Leigh B Stoller committed
37
my $optlist     = "dix";
38 39 40 41
my $debug       = 1;
my $infomode    = 0;
my $VMPATH      = "/var/xen/configs";
my $VGNAME	= "xen-vg";
42 43
my $IMAGEUNZIP  = "imageunzip";
my $IMAGEDUMP   = "imagedump";
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

#
# Turn off line buffering on output
#
$| = 1;

# Locals
my %xminfo = ();

# Protos
sub Fatal($);

#
# Parse command arguments. Once we return from getopts, all that should be
# left are the required arguments.
#
my %options = ();
if (! getopts($optlist, \%options)) {
    usage();
}
if (defined($options{"d"})) {
    $debug = 1;
}
if (defined($options{"i"})) {
    $infomode = 1;
}
usage()
    if (@ARGV != 2);

my $vnodeid = $ARGV[0];
my $path    = $ARGV[1];
my $XMINFO  = "$path/xm.conf";

77 78 79 80 81
# Must supply an absolute path.
if (! ($path =~ /^\//)) {
    Fatal("Must supply an absolute path");
}

Leigh B Stoller's avatar
Leigh B Stoller committed
82 83 84 85 86
if (! -e $IMAGEUNZIP) {
    $IMAGEUNZIP  = "/usr/local/bin/imageunzip";
    $IMAGEDUMP   = "/usr/local/bin/imagedump";
}

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
#
# We need this file to figure out the disk info.
#
if (! -e "$XMINFO") {
    Fatal("$XMINFO does not exist");
}
open(XM, $XMINFO)
    or Fatal("Could not open $XMINFO: $!");
while (<XM>) {
    if ($_ =~ /^([-\w]*)\s*=\s*(.*)$/) {
	my $key = $1;
	my $val = $2;
	if ($val =~ /^'(.*)'$/) {
	    $val = $1;
	}
	$xminfo{$key} = "$val";
    }
    elsif ($_ =~ /^([-\w]*)\s*[\+\=]+\s*(.*)$/) {
	my $key = $1;
	my $val = $2;
	if ($val =~ /^'(.*)'$/) {
	    $val = $1;
	}
	$xminfo{$key} .= $val;
    }
}
close(XM);

115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
#
# Localize the path to the kernel.
#
$xminfo{"kernel"} = $path . "/" . $xminfo{"kernel"};

#
# Fix up the network interfaces.
#
my $ifacelist = eval $xminfo{'vif'};
my @newifaces = ();
foreach my $vif (@$ifacelist) {
    my ($mac, $bridge) = split(',', $vif);
    my (undef, $iface) = split('=', $bridge);

    $iface =~ s/eth/xenbr/;
    push(@newifaces, "$mac, bridge=$iface");
}
# XXX Ick!
Leigh B Stoller's avatar
Leigh B Stoller committed
133 134
if ($vnodeid eq "boss" && !defined($options{"x"})) {
    for (my $i = 1; $i <= 4; $i++) {
135 136 137 138 139 140 141
	my $iface = "xenbr$i";
	my $mac   = "00:00:99:98:97:0$i";
	push(@newifaces, "mac=$mac, bridge=$iface");
    }
}
$xminfo{'vif'} = "[" . join(",", map {"'$_'" } @newifaces) . "]";

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
#
# Parse the disk info.
#
if (!exists($xminfo{'disk'})) {
    Fatal("No disk info in config file!");
}
my $disklist = eval $xminfo{'disk'};
my %diskinfo = ();
my %disksize = ();
foreach my $disk (@$disklist) {
    if ($disk =~ /^phy:([^,]*)/) {
	$diskinfo{$1} = $disk;
    }
    else {
	Fatal("Cannot parse disk: $disk");
    }
}

#
# And the size info.
#
foreach my $spec (split(',', $xminfo{'disksizes'})) {
    my ($dev,$size) = split(':', $spec);

    $disksize{$dev} = $size;
}
print Dumper(\%disksize);

foreach my $physinfo (keys(%diskinfo)) {
    my $spec = $diskinfo{$physinfo};
    my $dev;
    my $filename;
    if ($spec =~ /,(sd\w+),/) {
	$dev = $1;
    }
    else {
Leigh B Stoller's avatar
Leigh B Stoller committed
178
	Fatal("Could not parse $spec");
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
    }
    #
    # Figure out the size of the LVM.
    #
    my $lvmsize = $disksize{$dev};
    Fatal("Could not get lvsize for $dev")
	if (!defined($lvmsize));

    #
    # Form a new lvmname and create the LVM using the size.
    #
    my $lvmname = "${vnodeid}.${dev}";
    my $device  = "/dev/$VGNAME/$lvmname";

    if (! -e $device) {
	if (!$infomode) {
Leigh B Stoller's avatar
Leigh B Stoller committed
195 196
	    system("lvcreate -n $lvmname -L $lvmsize $VGNAME") == 0
		or Fatal("Could not create lvm for $lvmname");
197 198 199 200 201 202 203 204 205 206 207 208 209
	}
    }
    # Rewrite the diskinfo path for new xm.conf
    delete($diskinfo{$physinfo});
    $diskinfo{$device} = "phy:$device,$dev,w";

    #
    # For swap, just need to mark it as a linux swap partition.
    #
    if ($spec =~ /swap/) {
	#
	# Mark it as a linux swap partition. 
	#
Leigh B Stoller's avatar
Leigh B Stoller committed
210 211 212
	if (!$infomode &&
	    system("echo ',,S' | sfdisk $device -N0")) {
	    Fatal("Could not mark $device as linux swap");
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
	}
	next;
    }
    $filename = "$path/$dev";
    Fatal("$filename does not exist")
	if (! -e $filename);

    print "Working on $filename.\n";
    print "Size is $lvmsize. Writing to $device\n";
    
    #
    # The root FS is a single partition image, while the aux disks
    # have a real MBR in them. 
    #
    my $opts = "-W 256";
    if ($infomode) {
Leigh B Stoller's avatar
Leigh B Stoller committed
229
	system("$IMAGEDUMP $filename");
230 231
    }
    else {
Leigh B Stoller's avatar
Leigh B Stoller committed
232
	system("$IMAGEUNZIP -o $opts $filename $device");
233 234 235 236 237 238 239 240
    }
}

#
# Write out the config file.
#
delete($xminfo{"disksizes"});
$xminfo{"name"}   = $vnodeid;
241
$xminfo{"memory"} = "2048";
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
$xminfo{"disk"}   = "[" . join(",", map {"'$_'" } values(%diskinfo)) . "]";

if ($infomode) {
    print Dumper(\%xminfo);
}
else {
    #
    # Before we write it out, need to munge the vif spec since there is
    # no need for the script. Use just the default.
    #
    $XMINFO = "/var/tmp/${vnodeid}.conf";
    print "Writing new xen config to $XMINFO\n";
    
    open(XM, ">$XMINFO")
	or fatal("Could not open $XMINFO: $!");
    foreach my $key (keys(%xminfo)) {
	my $val = $xminfo{$key};
	if ($val =~ /^\[/) {
	    print XM "$key = $val\n";
	}
	else {
	    print XM "$key = '$val'\n";
	}
    }
    close(XM);
}

exit(0);

sub Fatal($)
{
    my ($msg) = @_;

    die("*** $0:\n".
	"    $msg\n");
}