Commit 2e5deae9 authored by Leigh B Stoller's avatar Leigh B Stoller
Browse files

Add image caching support (as for tutorials).

parent 2d216a7a
......@@ -536,10 +536,9 @@ sub vnodeCreate($$$$)
print STDERR "xen_vnodeCreate: ".
"no image specified, using default ('$imagename')\n";
my $vname = $imagename . ".0";
$lvname = "image+" . $vname;
$lvname = "image+" . $imagename;
if (!findLVMLogicalVolume($lvname)) {
createRootDisk($vname);
createRootDisk($imagename);
}
$imagemetadata = \%defaultImage;
}
......@@ -746,26 +745,44 @@ sub vnodeCreate($$$$)
}
}
else {
my $lv_size = lvSize($basedisk);
if (!defined($lv_size)) {
TBScriptUnlock();
fatal("libvnode_xen: could not get size of $basedisk");
}
if (mysystem2("lvcreate -L ${lv_size} -n $vnode_id $VGNAME")) {
TBScriptUnlock();
fatal("libvnode_xen: could not create disk for $vnode_id");
}
#
# Hacky attempt to determine if its a freebsd or linux disk.
# Need to create a new disk for the container. But lets see
# if we have a disk cached. We still have the imagelock at
# this point.
#
mysystem2("$IMAGEZIP -i -b $basedisk > /dev/null 2>&1");
my $ptypeopt = ($? ? "-l" : "-b");
if (my (@files) = glob("/dev/$VGNAME/_C_${imagename}_*")) {
#
# Grab the first file and rename it. It becomes ours.
# Then drop the lock.
#
my $file = $files[0];
if (mysystem2("lvrename $file $rootvndisk")) {
TBScriptUnlock();
fatal("libvnode_xen: could not rename cache file");
}
}
else {
my $lv_size = lvSize($basedisk);
if (!defined($lv_size)) {
TBScriptUnlock();
fatal("libvnode_xen: could not get size of $basedisk");
}
if (mysystem2("lvcreate -L ${lv_size} -n $vnode_id $VGNAME")) {
TBScriptUnlock();
fatal("libvnode_xen: could not create disk for $vnode_id");
}
#
# Hacky attempt to determine if its a freebsd or linux disk.
#
mysystem2("$IMAGEZIP -i -b $basedisk > /dev/null 2>&1");
my $ptypeopt = ($? ? "-l" : "-b");
mysystem2("nice $IMAGEZIP $ptypeopt $basedisk - | ".
"nice $IMAGEUNZIP -f -o -W 128 - $rootvndisk");
if ($?) {
TBScriptUnlock();
fatal("libvnode_xen: could no clone $basedisk");
mysystem2("nice $IMAGEZIP $ptypeopt $basedisk - | ".
"nice $IMAGEUNZIP -f -o -W 128 - $rootvndisk");
if ($?) {
TBScriptUnlock();
fatal("libvnode_xen: could no clone $basedisk");
}
}
}
}
......
#!/usr/bin/perl -w
#
# Copyright (c) 2000-2013 University of Utah and the Flux Group.
#
# {{{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/>.
#
# }}}
#
use English;
use Getopt::Std;
use strict;
# Drag in path stuff so we can find emulab stuff.
BEGIN { require "/etc/emulab/paths.pm"; import emulabpaths; }
my $IMAGEZIP = "/usr/local/bin/imagezip";
my $IMAGEUNZIP = "/usr/local/bin/imageunzip";
#
# Create a disk image cache.
#
sub usage()
{
print STDERR "Usage: mkimagecache [-c | -t count] [-s size] <imagename>\n";
print STDERR " mkimagecache -r <imagename>\n";
print STDERR "options:\n";
print STDERR " -c count - Create count\n";
print STDERR " -t count - Create as many as needed to reach count\n";
print STDERR " -s size - Size of lvm in GB; overrides base lvm\n";
print STDERR " -r - Remove all cached images\n";
exit(-1);
}
my $optlist = "c:s:t:r";
my $count = 1;
my $total = 0;
my $size = 0;
my $clear = 0;
#
# Turn off line buffering on output
#
$| = 1;
# Need this for predicates.
use libsetup;
use libutil;
use libtestbed;
use libvnode_xen;
my $VGNAME = libvnode_xen::VGNAME();
#
# 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 (@ARGV != 1) {
usage();
}
if (defined($options{"c"})) {
$count = $options{"c"};
}
if (defined($options{"t"})) {
$count = $total = $options{"t"};
}
if (defined($options{"s"})) {
$size = $options{"s"};
}
if (defined($options{"r"})) {
$clear = 1;
}
my $imagename = $ARGV[0];
# See libvnode_xen ...
my $imagelockname = "xenimage.$imagename";
my $imagelvmname = "image+" . $imagename;
my $imagelvmpath = libvnode_xen::lvmVolumePath($imagelvmname);
if ((my $locked = TBScriptLock($imagelockname,
TBSCRIPTLOCK_GLOBALWAIT(), 1800))
!= TBSCRIPTLOCK_OKAY()) {
die("Could not get the $imagelockname lock after a long time!\n");
}
#
# Look for existing cache files.
#
my @files = glob("/dev/$VGNAME/_C_${imagename}_*");
my $idx = 0;
#
# Delete all existing cache files.
#
if ($clear) {
foreach my $file (@files) {
system("lvremove -f $file");
if ($?) {
TBScriptUnlock();
exit(1);
}
}
TBScriptUnlock();
exit(0);
}
#
# Base must exist.
#
if (! -e $imagelvmpath) {
print STDERR "No base lvm exists for $imagename!\n";
TBScriptUnlock();
usage();
}
#
# Need the base size for copy.
#
my $lvsize;
if ($size) {
$lvsize = "$size" . "G";
}
else {
$lvsize = `lvs $imagelvmpath --noheadings -o lv_size`;
if ($?) {
TBScriptUnlock();
die("Could not get lv size of $imagelvmpath\n");
}
chomp($lvsize);
}
#
# Hacky attempt to determine if its a freebsd or linux disk.
#
system("$IMAGEZIP -i -b $imagelvmpath > /dev/null 2>&1");
my $zipopt = ($? ? "-l" : "-b");
while ($count > 0) {
if (grep {$_ eq "/dev/$VGNAME/_C_${imagename}_${idx}"} @files) {
$count--
if ($total);
$idx++;
next;
}
my $cachename = "_C_${imagename}_${idx}";
my $cachefile = "/dev/$VGNAME/$cachename";
if (mysystem2("lvcreate -d -v -L${lvsize} -n $cachename $VGNAME")) {
TBScriptUnlock();
exit(1);
}
if (mysystem2("nice $IMAGEZIP $zipopt $imagelvmpath - | ".
"nice $IMAGEUNZIP -f -o -W 128 - $cachefile")) {
mysystem2("lvremove -f $cachefile");
TBScriptUnlock();
exit(1);
}
$idx++;
$count--;
}
TBScriptUnlock();
exit(0);
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment