Commit e2a45a1d authored by Leigh B Stoller's avatar Leigh B Stoller
Browse files

Some quickie code to pre-create openvz container images so that

they do not have to be created at swapin. This is mostly for the
tutorial sessions, where we have dozens of containers launching
at the same time.
parent 3978a215
......@@ -910,47 +910,49 @@ sub vz_vnodeCreate {
print STDERR "Using LVM with root size $rootSize MB, ".
"snapshot size $snapSize MB.\n";
# we must have the lock, so if we need to return right away, unlock
if (-e $imagelockpath) {
TBScriptUnlock();
}
else {
if (1) {
#
# If there is already a logical device for this image, then
# need to GC or rename it (might be in use). Note that a
# reload of the partition will cause the lock files to get
# deleted, which results in some needless work (recreating
# the lvm even if it did not change), but I do not see a
# way to stamp the lvm itself so that we can determine its
# creation date. Besides, it is an atypical case.
#
if (system("lvdisplay /dev/openvz/$image >& /dev/null") == 0) {
if (GClvm("$image")) {
fatal("Could not GC or rename $image");
}
#
# Got the lock; if the imagelock file does not exists, we have
# to create the base lvm for it. Otherwise, hold the lock until
# we check the existing lvm cache.
#
if (! -e $imagelockpath) {
#
# If there is already a logical device for this image, then
# need to GC or rename it (might be in use). Note that a
# reload of the partition will cause the lock files to get
# deleted, which results in some needless work (recreating
# the lvm even if it did not change), but I do not see a
# way to stamp the lvm itself so that we can determine its
# creation date. Besides, it is an atypical case.
#
if (system("lvdisplay /dev/openvz/$image >& /dev/null") == 0) {
if (GClvm("$image")) {
fatal("Could not GC or rename $image");
}
print "Creating LVM core logical device for image $image\n";
# ok, create the lvm logical volume for this image.
mysystem("lvcreate $LVMDEBUGOPTS ".
" -L${rootSize}M -n $image openvz");
mysystem("mkfs -t ext3 /dev/openvz/$image");
mysystem("mkdir -p /mnt/$image");
mysystem("mount /dev/openvz/$image /mnt/$image");
mysystem("mkdir -p /mnt/$image/root ".
" /mnt/$image/private");
mysystem("tar -xzf $imagepath -C /mnt/$image/private");
mysystem("umount /mnt/$image")
if ($DOSNAP);
}
print "Creating LVM core logical device for image $image\n";
# ok, create the lvm logical volume for this image.
mysystem("lvcreate $LVMDEBUGOPTS ".
" -L${rootSize}M -n $image openvz");
mysystem("mkfs -t ext3 /dev/openvz/$image");
mysystem("mkdir -p /mnt/$image");
mysystem("mount /dev/openvz/$image /mnt/$image");
mysystem("mkdir -p /mnt/$image/root ".
" /mnt/$image/private");
mysystem("tar -xzf $imagepath -C /mnt/$image/private");
mysystem("umount /mnt/$image")
if ($DOSNAP);
# ok, we're done
mysystem("mkdir -p /var/emulab/run");
mysystem("touch $imagelockpath");
TBScriptUnlock();
}
if ($DOSNAP) {
# Snapshots can happen in parallel.
TBScriptUnlock();
#
# Now take a snapshot of this image's logical device
#
......@@ -963,11 +965,25 @@ sub vz_vnodeCreate {
" -s -L${snapSize}M -n $vnode_id /dev/openvz/$image");
}
}
else {
elsif (system("lvdisplay /dev/openvz/$vnode_id >& /dev/null")) {
#
# No snapshot, create a new disk for each container.
#
if (system("lvdisplay /dev/openvz/$vnode_id >& /dev/null")) {
# 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.
#
if (my (@files) = glob("/dev/openvz/_C_${image}_*")) {
#
# Grab the first file and rename it. It becomes ours.
# Then drop the lock.
#
my $file = $files[0];
mysystem("lvrename $file /dev/openvz/$vnode_id");
TBScriptUnlock();
}
else {
# This can happen in parallel with other VMs.
TBScriptUnlock();
mysystem("lvcreate $LVMDEBUGOPTS ".
" -L${rootSize}M -n $vnode_id openvz");
mysystem("mkfs -t ext3 /dev/openvz/$vnode_id");
......
#!/usr/bin/perl -w
#
# Copyright (c) 2000-2012 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 $TAR = "/bin/tar";
#
# Create a disk image cache.
#
sub usage()
{
print STDOUT "Usage: mkimagecache [-c count] <imagename>\n";
exit(-1);
}
my $optlist = "c:";
my $count = 1;
#
# Turn off line buffering on output
#
$| = 1;
# Need this for predicates.
use libsetup;
use libvnode;
use libtestbed;
#
# 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"};
}
my $imagename = $ARGV[0];
# See libvnode_openvz ...
my $imagelockpath = "/var/emulab/db/openvz.image.$imagename.ready";
my $imagelockname = "vzimage.$imagename";
my $imagepath = "/vz/template/cache/${imagename}.tar.gz";
# Only LVM is supported right now.
if (! -e "/dev/openvz/$imagename") {
die("LVM does not exist!\n");
}
if ((my $locked = TBScriptLock($imagelockname,
TBSCRIPTLOCK_GLOBALWAIT(), 1800))
!= TBSCRIPTLOCK_OKAY()) {
die("Could not get the $imagelockname lock after a long time!\n");
}
#
# Need the base size for copy.
#
my $lvsize = `lvs /dev/openvz/$imagename --noheadings -o lv_size`;
if ($?) {
die("Could not get lv size of /dev/openvz/imagename\n");
}
chomp($lvsize);
#
# Look for existing cache files and skip those.
#
my @files = glob("/dev/openvz/_C_${imagename}_*");
my $idx = 0;
while ($count) {
if (grep {$_ eq "/dev/openvz/_C_${imagename}_${idx}"} @files) {
$idx++;
next;
}
my $cachename = "_C_${imagename}_${idx}";
mysystem("lvcreate -d -v -L${lvsize} -n $cachename openvz");
mysystem("mkfs -t ext3 /dev/openvz/$cachename");
mysystem("mkdir -p /mnt/$cachename")
if (! -e "/mnt/$cachename");
mysystem("mount /dev/openvz/$cachename /mnt/$cachename");
mysystem("mkdir -p /mnt/$cachename/root /mnt/$cachename/private");
if (! -e "/mnt/$imagename/private") {
# Backwards compat.
mysystem("tar -xzf $imagepath -C /mnt/$cachename/private");
}
else {
mysystem("cd /mnt/$imagename/private; ".
"tar -b 64 -cf - . | ".
"tar -b 64 -xf - -C /mnt/$cachename/private");
}
mysystem("umount /mnt/$cachename");
$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