OSImage.pm.in 36.4 KB
Newer Older
1 2
#!/usr/bin/perl -wT
#
3
# Copyright (c) 2007-2017 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 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
# 
# {{{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/>.
# 
# }}}
#
package OSImage;

use strict;
use Carp;
use English;
use Data::Dumper;
use File::Basename;
use Exporter;
use vars qw(@ISA @EXPORT $AUTOLOAD %IMAGE_ARCHITECTURES);

@ISA    = "Exporter";
@EXPORT = qw ( );

# Must come after package declaration!
use libdb;
use EmulabConstants;
use libtestbed;
use Image;
42
use NodeType;
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
use OSinfo;
use overload ('""' => 'Stringify');

# Configure variables
my $TB		  = "@prefix@";
my $BOSSNODE      = "@BOSSNODE@";
my $CONTROL	  = "@USERNODE@";
my $TBOPS         = "@TBOPSEMAIL@";
my $TBAUDIT       = "@TBAUDITEMAIL@";
my $TBBASE        = "@TBBASE@";
my $WWWHOST       = "@WWWHOST@";
my $OURDOMAIN     = "@OURDOMAIN@";
my $PGENISUPPORT  = @PROTOGENI_SUPPORT@;
my $DOPROVENANCE  = @IMAGEPROVENANCE@;
my $TBWWW         = "@TBWWW@";

#
# Valid architectures. Short list.
#
%IMAGE_ARCHITECTURES = (
    "i386"    => "i386",
    "x86_64"  => "x86_64",
    "aarch64" => "aarch64",
);

# Cache of instances to avoid regenerating them.
my %images    = ();
my $debug      = 0;

#
# Lookup by idx or pid,imagename[:version] depending on the args. We always 
# return highest numbered version on this path, if no version specified.
#
sub Lookup($$;$$)
{
    my ($class, $arg1, $arg2, $arg3) = @_;
    my $token = "$arg1";
    $token .= ",$arg2" if (defined($arg2));
    $token .= ",$arg3" if (defined($arg3));

    #
    # See if an image alias first.
    #
    if (!defined($arg3)) {
	my $image = OSImageAlias->Lookup($arg1, $arg2);
	return $image
	    if (defined($image));
    }

    #
    # First do the image table lookup. If we find it there, we can look
    # for the osinfo. If we do not find it, it might still be an MFS
    # which has just the osinfo.
    #
    my $image = Image->Lookup($arg1, $arg2, $arg3);

    #
    # If this is a dataset, then there is no osinfo. It is convenient
    # to allow this to work, just return the underlying image. The
    # caller always knows it is messing with a dataset.
    #
    if (defined($image) && $image->isdataset()) {
	return $image;
    }

    #
    # We only support single OS, EZID images now.
    # MFSs are osinfo objects with no image, that is okay too.
    # Generic Images are osinfo objects that point to another (nextosid).
    #
    my $osinfo;
    if (!defined($image)) {
	$osinfo = OSinfo->Lookup($arg1, $arg2, $arg3);
	if (defined($osinfo)) {
	    if (! ($osinfo->mfs() || $osinfo->IsGeneric())) {
118 119 120 121
		if (0) {
		    print STDERR
			"OSImage::Lookup($token) is not an EZID or MFS image\n";
		}
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 153 154
	    }
	}
	else {
	    if (0) {
		print STDERR "OSImage::Lookup($token) does not exist\n";
	    }
	    return undef;
	}
    }
    else {
	$osinfo = OSinfo->Lookup($image->imageid(), $image->version());
	#
	#
	#
	if (!defined($osinfo)) {
	    $osinfo = OSinfo->Lookup($image->pid(),
				     $image->imagename(), $image->version());
	}
    }
    if (!defined($osinfo)) {
	print STDERR "OSImage::Lookup($token): There is no OSinfo for $image\n";
	return undef;
    }
    my $self = {};
    $self->{'OSINFO'} = $osinfo;
    $self->{'IMAGE'}  = $image;
    $self->{'HASH'}   = {};    
    bless($self, $class);
    
    return $self;
}
sub image($)		{ return $_[0]->{'IMAGE'}; }
sub osinfo($)		{ return $_[0]->{'OSINFO'}; }
155
sub temphash($)		{ return $_[0]->{'HASH'}; }
156
sub isImage($)          { return defined($_[0]->image()) ? 1 : 0; }
157 158 159 160 161 162 163 164 165 166 167 168 169 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 199 200 201 202 203 204 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 239 240 241 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 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350
sub isImageAlias($)	{ return 0; }

# Break circular references to avoid exit errors.
sub DESTROY {
    my $self = shift;

    $self->{'IMAGE'}  = undef;
    $self->{'OSINFO'} = undef;
    $self->{'HASH'}   = undef;
}

sub Stringify($)
{
    my ($self) = @_;
    my $pid = $self->pid();
    
    if (defined($self->image())) {
	my $imageid   = $self->image()->imageid();
	my $version   = $self->image()->version();
	my $imagename = $self->image()->imagename();

	return "[OSImage $pid,$imagename $imageid:$version]";
    }
    else {
	my $tag    = $self->osinfo()->mfs() ? "MFS" : "Generic";
	my $osid   = $self->osinfo()->osid();
	my $osname = $self->osinfo()->osname();
	
	return "[$tag $pid,$osname $osid]";
    }
}

AUTOLOAD {
    my $self  = $_[0];
    my $type  = ref($self) or croak "$self is not an object";
    my $name  = $AUTOLOAD;
    $name =~ s/.*://;   # strip fully-qualified portion

    #
    # There is overlap in names between Image and OSinfo. All of those can
    # come from the Images table, except this conflict. In the OSinfo table
    # it is a version string from the OS, but otherwise it is the version
    # of the Image.
    #
    if ($name eq "osversion") {
	return $self->osinfo()->field("version");
    }
    #
    # Check the images table. Might be an MFS ...
    #
    if (defined($self->image()) && $self->image()->fieldExists($name)) {
	# Allow update.
	if (scalar(@_) == 2) {
	    $self->image()->fieldSet($name, $_[1]);
	}
	return $self->image()->field($name);
    }
    
    #
    # If it is not in the Images table, look in the OSinfo for it. But some
    # of these names should no longer be used, I am going to flag these for
    # warnings.
    #
    if ($self->osinfo()->fieldExists($name)) {
	my %osinfo_warn = (
	    "vers"	    => 1,
	    "parent_osid"   => 1,
	    "parent_vers"   => 1,
	    "uuid"          => 1,
	    "old_osid"      => 1,
	    "osname"        => 1,
	);
	return $self->osinfo()->field($name);
    }
    
    # Or it is for a local storage slot. We use the image hash until
    # sure we have all the code changed. 
    if ($name =~ /^_.*$/) {
	if (scalar(@_) == 2) {
	    return $self->image()->{'HASH'}->{$name} = $_[1];
	}
	elsif (exists($self->image()->{'HASH'}->{$name})) {
	    return $self->image()->{'HASH'}->{$name};
	}
    }
    carp("No such slot '$name' field in class $type");
    return undef;
}
sub field($$) {
    my ($self, $name) = @_;

    if ($self->image()->fieldExists($name)) {
	return $self->image()->field($name);
    }
    return undef;
}

#
# Lookup by URL.
#
sub LookupByURL($$)
{
    my ($class, $url) = @_;

    my $image = Image->LookupByURL($url);
    return undef
	if (!defined($image));

    return OSImage->Lookup($image->imageid(), $image->version());
}

# Image is local if the metadata_url is null.
sub IsLocal($)
{
    return $_[0]->image()->IsLocal();
}

#
# Lookup a "system" osid, meaning one in the emulab-ops project.
#
sub LookupByName($$)
{
    my ($class, $imagename) = @_;

    #
    # Look for an image alias first.
    #
    my $image = OSImageAlias->LookupByName($imagename);
    return $image
	if (defined($image));

    $image = Image->LookupByName($imagename);
    if (!defined($image)) {
	my $osinfo = OSinfo->LookupByName($imagename);
	return undef
	    if (!defined($osinfo));

	return OSImage->Lookup($osinfo->osid(), $osinfo->vers());
    }
    return OSImage->Lookup($image->imageid(), $image->version());
}

#
# Lookup by Origin UUID.
#
sub LookupByOriginUUID($$$)
{
    my ($class, $pid, $uuid) = @_;

    my $image = Image->LookupByOriginUUID($pid, $uuid);
    return undef
	if (!defined($image));

    return OSImage->Lookup($image->imageid(), $image->version());
}

#
# Lookup by Origin URN.
#
sub LookupByOriginURN($$)
{
    my ($class, $urn) = @_;

    my $image = Image->LookupByOriginURN($urn);
    return undef
	if (!defined($image));

    return OSImage->Lookup($image->imageid(), $image->version());
}

#
# Lookup by Authority URN. Only for datasets right now.
#
sub LookupByAuthorityURN($$)
{
    my ($class, $urn) = @_;
    my $image = Image->LookupByAuthorityURN($urn);
    return undef
	if (!defined($image));

    return OSImage->Lookup($image->imageid(), $image->version());
}

sub versid($)
{
    return $_[0]->image()->versid();
}
sub versname($)
{
    return $_[0]->image()->versname();
}
sub urn($)
{
    return $_[0]->image()->urn();
351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
}

#
# Global is odd. If an image, we use that slot. If just an OSinfo, we
# use shared.
#
sub global($)
{
    my ($self) = @_;

    return $self->image()->global()
	if (defined($self->image()));
    
    return $self->osinfo()->shared();
}
366

367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383
#
# Version is a bit of a pain. version() in OSinfo is an OS string we rarely
# care about (IsGeneric() does). vers() is the actual version. Normally this
# does not matter, but if the OSImage is encapsulating an OSinfo without
# an Image (which happens, think MFS), then version() will get the wrong
# thing (from the osinfo instead of the image).
#
sub version()
{
    my ($self) = @_;

    return $self->image()->version()
	if (defined($self->image()));

    return $self->osinfo()->vers();
}

384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461
#
# Return a list of all images of the given format for the given pid.
# If format is NULL, return all formats. If pid is NULL, return for all pids.
# List is names of the form "pid/imagename".
#
sub ListAll($$$)
{
    return Image->ListAll($_[1], $_[2]);
}

#
# Return a list of all versions of all images of the given format for the
# given pid.
#
# If format is NULL, return all formats. If pid is NULL, return for all pids.
# List is names of the form "pid/imagename:version".
#
sub ListAllVersions($$$)
{
    return Image->ListAllVersions($_[1], $_[2]);
}

#
# Refresh instance by reloading from the DB.
#
sub Refresh($)
{
    return -1
	if ($_[0]->image()->Refresh() || $_[0]->osinfo()->Refresh());
    
    return 0;
}

#
# Clone an image descriptor from the DB, bumping the version number
#
sub NewVersion($@)
{
    my ($self, @args) = @_;

    my $image = $self->image()->NewVersion(@args);
    return undef
	if (!defined($image));
    
    return OSImage->Lookup($image->imageid(), $image->version());
}

#
# For a newly created image, set the provenance pointers back to
# the image from which it was derived.
#
sub SetProvenance($$)
{
    my ($self, $base) = @_;

    return -1
	if ($self->image()->SetProvenance($base->image()) ||
	    $self->osinfo()->SetProvenance($base->osinfo()));

    return 0;
}

#
# Highest number version, rather then what is deemed most recent by the
# images table.
#
sub LookupMostRecent($)
{
    my ($self) = @_;
    my $image  = $self->image()->LookupMostRecent();
    return undef
	if (!defined($image));
    return OSImage->Lookup($image->imageid(), $image->version());
}

#
# Return a list of all image versions.
#
462
sub AllVersions($$;$)
463
{
464
    my ($self, $pref, $deleted)  = @_;
465 466 467 468 469
    my $imageid = $self->image()->imageid();
    my @images  = ();
    my @result  = ();

    return -1
470
	if ($self->image()->AllVersions(\@images, $deleted));
471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 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 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572

    foreach my $image (@images) {
	my $version = $image->version();
	my $tmp = OSImage->Lookup($imageid, $version);
	if (!defined($tmp)) {
	    print STDERR "Could not lookup image $imageid,$version\n";
	    return -1;
	}
	push(@result, $tmp);
    }
    @$pref = @result;
    return 0;
}

#
# Release an image; all this does is make the highest numbered version
# in the image_versions table, the default version. It does this by
# updating the version number in the images table. An unreleased version
# can be used by explicitly giving the version number, but unqualified
# use always get the version that is set in the images table. 
#
sub Release($)
{
    return $_[0]->image()->Release();
}

#
# Worker class method to edit image descriptor.
# Assumes most argument checking was done elsewhere.
#
sub EditImageid($$$$)
{
    my ($class, $image, $argref, $usrerr_ref) = @_;

    return Image->EditImageid($image, $argref, $usrerr_ref);
}

#
# Return the internal DB rowref. Used for code that has not been converted.
#
sub DBData($)
{
    croak("OSImage::DBData($_[0])");
}

#
# Perform some updates ... We have to watch for updates to either the
# the underlying image or the osinfo, and redirect to the right place.
# Error if a mixed update (should not happen).
#
sub Update($;$)
{
    my ($self, $argref, $allvers) = @_;
    my $forimage  = 0;
    my $forosinfo = 0;

    foreach my $name (keys(%{ $argref })) {
	if (defined($self->image()) &&
	    $self->image()->fieldExists($name)) {
	    $forimage = 1;
	}
	elsif (defined($self->osinfo()) &&
	       $self->osinfo()->fieldExists($name)) {
	    $forosinfo = 1;
	}
    }
    if ($forimage && $forosinfo) {
	cluck("OSImage::Update: update for both image and osinfo");
	return -1;
    }
    elsif (! ($forimage || $forosinfo)) {
	cluck("OSImage::Update: update for neither image or osinfo");
	return -1;
    }
    elsif ($forimage) {
	return $self->image()->Update($argref, $allvers);
    }
    else {
	return $self->osinfo()->Update($argref, $allvers);
    }
}

#
# Delete an image,
#
sub Delete($;$)
{
    my ($self, $purge) = @_;

    return $self->image()->Delete($purge);
}

#
# Delete a version of an image. This is not to be used, except when there
# is an error during clone, and we want to undo the creation of a new version.
#
sub DeleteVersion($)
{
    my ($self) = @_;

    return $self->image()->DeleteVersion();
}
Leigh B Stoller's avatar
Leigh B Stoller committed
573 574 575 576 577 578
sub PurgeVersion($)
{
    my ($self) = @_;

    return $self->image()->PurgeVersion();
}
579 580 581 582 583 584 585 586 587 588 589

#
# Load the project object for an image
#
sub GetProject($)
{
    my ($self) = @_;

    return $self->image()->GetProject();
}

590 591 592 593 594 595 596 597 598 599
#
# Load the user object for an image
#
sub GetCreator($)
{
    my ($self) = @_;

    return $self->image()->GetCreator();
}

600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651
#
# Load the group object for an image
#
sub GetGroup($)
{
    my ($self) = @_;

    return $self->image()->GetGroup();
}

#
# Check permissions. Note that root may ask permission, which comes in
# as an undef user.
#
sub AccessCheck($$$)
{
    my ($self, $user, $access_type) = @_;

    return $self->image()->AccessCheck($user, $access_type);
}

#
# LockTables simply locks the given tables, and then refreshes the
# instance (thereby getting the data from the DB after the tables are locked).
#
sub LockTables($;$)
{
    my ($self, $spec) = @_;

    return $self->image()->LockTables($spec);
}
sub UnLockTables($)
{
    my ($self) = @_;

    return $self->image()->UnLockTables($self);
}

#
# Get the disk offset at which the image will be loaded.
# For a "whole disk" (loadpart == 0) image, it is always 0.
# For a "partition" image, it depends on the MBR type of the image.
#
# Returns -1 if the offset could not be determined (e.g., unknown MBR).
#
sub GetDiskOffset($)
{
    my ($self) = @_;

    return $self->image()->GetDiskOffset();
}

652 653 654 655 656 657 658 659 660 661 662 663 664
sub PartitionSize($)
{
    my ($self) = @_;

    return $self->image()->PartitionSize();
}
sub Chunks($)
{
    my ($self) = @_;

    return $self->image()->Chunks();
}

665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685
#
# Return updated time for image as a UNIX timestamp via the passed ref.
# Return 0 on success, non-zero otherwise.
#
sub GetUpdate($$)
{
    my ($self,$stampp) = @_;

    return $self->image()->GetUpdate($stampp);
}

#
# Mark the update time in the record,
#
sub MarkUpdate($$;$)
{
    my ($self, $user, $stamp) = @_;

    return $self->image()->MarkUpdate($user, $stamp);
}

Mike Hibler's avatar
Mike Hibler committed
686 687 688 689 690 691 692
sub MarkDeleted($;$)
{
    my ($self, $stamp) = @_;

    return $self->image()->MarkDeleted($stamp);
}

693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764
#
# Mark the IMS update time in the record,
#
sub MarkIMSReported($)
{
    my ($self) = @_;

    return $self->image()->MarkIMSReported();
}
sub ClearIMSReported($)
{
    my ($self) = @_;

    return $self->image()->ClearIMSReported();
}

#
# Set the hash.
#
sub SetFullHash($$)
{
    my ($self, $hash) = @_;

    return $self->image()->Update({"hash" => $hash});
}
sub SetDeltaHash($$)
{
    my ($self, $hash) = @_;

    return $self->image()->Update({"deltahash" => $hash});
}

#
# Set the size.
#
sub SetFullSize($$)
{
    my ($self, $size) = @_;

    return $self->image()->Update({"size" => $size});
}
sub SetDeltaSize($$)
{
    my ($self, $size) = @_;

    return $self->image()->Update({"deltasize" => $size});
}

sub SetUploaderPath($$)
{
    my ($self, $path) = @_;

    return $self->image()->Update({"uploader_path" => $path});
}
sub ClearUploaderPath($)
{
    my ($self) = @_;

    return $self->image()->Update({"uploader_path" => ''});
}

#
# Set the sector range of an image.
# Note that lba_size and reloc flag are left alone unless explicitly specified.
#
sub SetRange($$$;$$)
{
    my ($self,$start,$end,$ssize,$isreloc) = @_;

    return $self->image()->SetRange($start,$end,$ssize,$isreloc);
}

765 766 767 768 769 770 771 772
# WebTask
sub SetWebTask($$)
{
    my ($self,$webtask) = @_;

    return $self->image()->SetWebTask($webtask);
}

773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 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 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967
#
# Lock and Unlock. We lock the entire chain (all versions) of the
# image. 
#
sub Lock($)
{
    my ($self) = @_;

    return $self->image()->Lock();
}

sub Unlock($)
{
    my ($self) = @_;

    return $self->image()->Unlock();
}

sub GotLock($)
{
    my ($self) = @_;

    return $self->image()->GotLock();
}

#
# Wait to get lock.
#
sub WaitLock($$)
{
    my ($self, $seconds) = @_;

    return $self->image()->WaitLock($seconds);
}

#
# Get the type list.
#
sub TypeList($;$)
{
    my ($self, $osinfo) = @_;

    #
    # Since there are no longer any multi osid images or non-ezid images,
    # we do not need the osinfo that was passed in (from legacy code). We
    # return the type list for the image. 
    #
    return $self->image()->TypeList();
}

#
# Grant/Revoke permission to access an image.
#
sub GrantAccess($$$)
{
    my ($self, $target, $writable) = @_;

    return $self->image()->GrantAccess($target, $writable);
}

sub LookupAccess($$$$)
{
    my ($self, $target, $paccess, $pwriteable) = @_;

    return $self->image()->LookupAccess($target, $paccess, $pwriteable);
}

sub RevokeAccess($$)
{
    my ($self, $target) = @_;

    return $self->image()->RevokeAccess($target);
}

#
# Class method to record image history info. 
#
sub RecordImageHistory($$$$$$)
{
    my ($class, $experiment, $action, $log_session, $impotent, $noderef) = @_;

    return Image->RecordImageHistory($experiment, $action,
				     $log_session, $impotent, $noderef);
}

#
# Record more concise success/failure of an image each time it boots.
#
sub RecordImageStatus($$$$)
{
    my ($self, $experiment, $node, $status) = @_;

    return $self->image()->RecordImageStatus($experiment, $node, $status);
}

#
# List of images created by a user urn.
#
sub ListForURN($$)
{
    my ($class, $urn) = @_;
    my @result  = ();

    return undef
	if (! $PGENISUPPORT);

    require GeniHRN;
    return ()
	if (! GeniHRN::IsValid($urn));

    my @images = Image->ListForURN($urn);
    
    foreach my $image (@images) {
	my $imageid = $image->imageid();
	my $version = $image->version();
	my $tmp = OSImage->Lookup($imageid, $version);
	if (!defined($tmp)) {
	    print STDERR "Could not lookup image $imageid,$version\n";
	    return -1;
	}
	push(@result, $tmp);
    }
    return @result;
}

#
# Set to use the logfile. It becomes the "current" spew.
#
sub SetLogFile($$)
{
    my ($self, $logfile) = @_;

    return $self->image()->SetLogFile($logfile);
}

# Ready bit.
sub SetReady($$)
{
    my ($self, $ready) = @_;

    return $self->image()->SetReady($ready);
}
sub MarkReady($)   { return SetReady($_[0], 1); }
sub ClearReady($) { return SetReady($_[0], 0); }
# Released bit.
sub MarkReleased($)
{
    my ($self) = @_;

    return $self->image()->MarkReleased();
}
# Last used, for datasets.
sub BumpLastUsed($)
{
    my ($self) = @_;
    
    return $self->image()->BumpLastUsed();
}

# Are two images the same.
sub SameImage($$)
{
    my ($this, $that) = @_;

    return (($this->image()->imageid() == $that->image()->imageid() &&
	     $this->image()->version() == $that->image()->version()) ? 1 : 0);
}

#
# Get the logfile.
#
sub GetLogFile($)
{
    my ($self) = @_;

    return $self->image()->GetLogFile();
}

#
# And clear it ...
#
sub ClearLogFile($)
{
    my ($self) = @_;

    return $self->image()->ClearLogFile();
}

#
# Mark an image as running on a type. I am assuming EZ images these days.
#
sub SetRunsOnNodeType($$)
{
    my ($self, $type) = @_;

968 969 970
    return 0
	if (!defined($self->image()));

971 972 973 974 975 976
    return $self->image()->SetRunsOnNodeType($type);
}

#
# If using the image tracker, have to notify the IMS.
#
977
sub SchedIMSDeletion($$)
978
{
979
    my ($self,$allversions) = @_;
980

981
    return $self->image()->SchedIMSDeletion($allversions);
982 983 984 985 986 987 988 989 990 991 992 993
}

#
# Is the image the newest version of the image.
#
sub IsNewest($)
{
    my ($self) = @_;

    return $self->image()->IsNewest();
}

994 995 996 997 998 999 1000 1001 1002 1003
#
# Grab webtask from image.
#
sub webtask($)
{
    my ($self) = @_;

    return $self->image()->webtask();
}

1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065
#
# Grab parent of an image, or undef if there is no parent. The
# parent might be an earlier revision or it might be another
# image.
#
sub Parent($)
{
    my ($self) = @_;

    return undef
	if (!defined($self->image()->parent_imageid()));

    return OSImage->Lookup($self->image()->parent_imageid(),
			   $self->image()->parent_version());
}

sub SetDelta($$)
{
    my ($self, $delta) = @_;

    return $self->image()->SetDelta($delta);
}

sub SetSize($$)
{
    my ($self, $size) = @_;

    return $self->image()->SetSize($size);
}

sub SetHash($$)
{
    my ($self, $hash) = @_;

    return $self->image()->SetHash($hash);
}

sub SetNoVersioning($$)
{
    my ($self, $value) = @_;

    return $self->image()->SetNoVersioning($value);
}

sub SetImageMetadataURL($$)
{
    my ($self, $url) = @_;

    return $self->image()->SetImageMetadataURL($url);
}

sub LocalURL($)
{
    my ($self) = @_;

    return $self->image()->LocalURL();
}

sub LocalVersionURL($)
{
    my ($self) = @_;
    
Leigh B Stoller's avatar
Leigh B Stoller committed
1066
    return $self->image()->LocalVersionURL();
1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169
}

sub HasCredential($)
{
    my ($self) = @_;

    return $self->image()->HasCredential();
}
sub GetCredential($)
{
    my ($self) = @_;

    return $self->image()->GetCredential();
}
sub SetCredential($$)
{
    my ($self, $credstr) = @_;

    return $self->image()->SetCredential($credstr);
}

#
# Path and Directory stuff.
#
# Images are stored as directories now. Inside the directory are base
# and delta images for each version, as well as sig and sha1 files. 
#
sub IsDirPath($)
{
    my ($self) = @_;

    return $self->image()->IsDirPath();    
}
sub FullImagePath($)
{
    my ($self) = @_;

    return $self->image()->FullImagePath();
}
sub DeltaImagePath($)
{
    my ($self) = @_;

    return $self->image()->DeltaImagePath();
}
sub FullImageFile($)
{
    my ($self) = @_;
    return $self->FullImagePath();
}
sub DeltaImageFile($)
{
    my ($self) = @_;
    return $self->DeltaImagePath();
}
sub TempImageFile($)
{
    my ($self) = @_;

    return $self->image()->TempImageFile();
}
sub FullImageSHA1File($)
{
    my ($self) = @_;

    return $self->image()->FullImageSHA1File();
}
sub DeltaImageSHA1File($)
{
    my ($self) = @_;

    return $self->image()->DeltaImageSHA1File();
}
sub FullImageSigFile($)
{
    my ($self) = @_;

    return $self->image()->FullImageSigFile();
}
sub DeltaImageSigFile($)
{
    my ($self) = @_;

    return $self->image()->DeltaImageSigFile();
}
sub HaveFullImage($)
{
    my ($self) = @_;

    return $self->image()->HaveFullImage();
}
sub HaveDeltaImage($)
{
    my ($self) = @_;

    return $self->image()->HaveDeltaImage();
}
sub CreateImageDir($)
{
    my ($self) = @_;

    return $self->image()->CreateImageDir();
}
Leigh B Stoller's avatar
Leigh B Stoller committed
1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181
sub SaveDir($)
{
    my ($self) = @_;

    return $self->image()->SaveDir();
}
sub IsSystemImage($)
{
    my ($self) = @_;

    return $self->image()->IsSystemImage();
}
1182

Leigh B Stoller's avatar
Leigh B Stoller committed
1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207
sub Deprecate($;$$)
{
    my ($self,$message,$iserror) = @_;

    return $self->image()->Deprecate($message, $iserror);
}
sub IsDeprecated($$;$$)
{
    my ($self,$val,$message,$iserror) = @_;

    return $self->image()->IsDeprecated($val, $message, $iserror);
}
sub DeprecatedMessage($;$)
{
    my ($self, $experiment) = @_;

    return $self->image()->DeprecatedMessage($experiment);
}
sub SendDeprecatedMail($$;$)
{
    my ($self, $user, $experiment) = @_;

    return $self->image()->SendDeprecatedMail($user, $experiment);
}

1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218
################################################################################
# This is the stuff from OSinfo.pm
################################################################################

#
# Map an osinfo and node type to the actual image.
#
sub MapToImage($$)
{
    my ($self, $type) = @_;

1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231
    #
    # If we are using architectures this is not necessary. In fact, since
    # we do not do multi-os images, I think we can flush it completely.
    #
    if (!ref($type)) {
	my $nodetype = NodeType->Lookup($type);
	if (!defined($nodetype)) {
	    print STDERR "Could not map $type to an object\n";
	    return undef;
	}
	$type = $nodetype;
    }
    return $self
1232 1233
	if ($self->isImage() &&
	    $self->architecture() && $type->architecture());
1234 1235
    
    # This returns an OSimage.
1236
    return $self->osinfo()->MapToImage($type);
1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458
}

#
# Resolve a 'generic' OSID (ie. FBSD-STD) to a real OSID
#
# Note: It's okay to call this function with a 'real' OSID, but it would be
# waseful to do so.
#
# returns: The 'real' OSID that the OSID resolves to, or undef if there is a
#          problem (ie. unknown OSID)
#
sub ResolveNextOSID($;$)
{
    my ($self, $experiment) = @_;

    my $osinfo = $self->osinfo()->ResolveNextOSID($experiment);
    return undef
	if (!defined($osinfo));

    return OSImage->Lookup($osinfo->osid(), $osinfo->vers());
}

#
# Lookup newest version of osinfo.
#
sub LookupNewest($)
{
    my ($self) = @_;

    return $self->LookupMostRecent();
}

#
# Default reboot time. Class method.
#
sub RebootWaitTime($$)
{
    my ($class, $os) = @_;

    return OSinfo->RebootWaitTime($os);
}

#
# Class method to check the OS is legal.
#
sub ValidOS($$)
{
    my ($class, $os) = @_;

    return OSinfo->RebootWaitTime($os);
}

#
# Class method to check the OPmode is legal.
#
sub ValidOpMode($$)
{
    my ($class, $opmode) = @_;

    return OSinfo->ValidOpMode($opmode);
}

#
# Is the osinfo "generic"
#
sub IsGeneric($)
{
    my ($self)  = @_;

    return $self->osinfo()->IsGeneric();
}

sub IsNfsMfs($)
{
    my ($self)  = @_;

    return $self->osinfo()->IsNfsMfs();
}

#
# Boot command line. The caller supplies the default in $pref.
#
sub OSBootCmd($$$)
{
    my ($self, $role, $pref) = @_;

    return $self->osinfo()->OSBootCmd($role, $pref);
}

#
# Set the nextosid.
#
sub SetNextOS($$)
{
    my ($self, $nextosinfo) = @_;

    return $self->osinfo()->SetNextOS($nextosinfo);
}

sub SetParentOS($$)
{
    my ($self, $parent) = @_;

    return $self->osinfo()->SetParentOS($parent);
}

#
# Check if a particular feature is supported.
#
sub FeatureSupported($$)
{
    my ($self, $feature) = @_;

    return $self->osinfo()->FeatureSupported($feature);
}

#
# Add a feature.
#
sub AddFeature($$)
{
    my ($self, $feature) = @_;

    return $self->osinfo()->AddFeature($feature);
}

#
# Return 1 if OS is a sub OS (i.e., has a def_parentosid).
#
sub IsSubOS($)
{
    my ($self) = @_;

    return $self->osinfo()->IsSubOS();
}

#
# Return 1 if OS is a sub OS and runs on the given parent.
#
sub RunsOnParent($$)
{
    my ($self,$parent) = @_;

    return $self->osinfo()->RunsOnParent($parent);
}

#
# Really, def_parentosid mapped to the image.
#
sub DefaultParent($)
{
    my ($self) = @_;
    my $parent = $self->osinfo()->DefaultParent();
    return undef
	if (!defined($parent));

    return OSImage->Lookup($parent->osid(), $parent->vers());
}

#
# Set an entry in the submap that says it runs on a parent.
#
sub SetRunsOnParent($$)
{
    my ($self,$parent) = @_;

    return $self->osinfo()->SetRunsOnParent($parent);
}

# Stubs for calling "libTaintStates" common taint handling code
sub GetTaintStates($)
{
    my ($self) = @_;
    
    return $self->osinfo()->GetTaintStates();
}
sub IsTainted($;$)
{
    my ($self, $taint) = @_;

    return $self->osinfo()->IsTainted($taint);
}
sub SetTaintStates($@)
{
    my ($self, @taint_states) = @_;

    return $self->osinfo()->SetTaintStates(@taint_states);
}
sub AddTaintState($$)
{
    my ($self, $taint) = @_;

    return $self->osinfo()->AddTaintState($taint);
}
sub RemoveTaintState($;$)
{
    my ($self, $taint) = @_;

    return $self->osinfo()->RemoveTaintState($taint);
}
sub InheritTaintStates($$)
{
    my ($self, $osinfo) = @_;

    return $self->osinfo()->InheritTaintStates($osinfo);
}

#############################################################################
#
# Image Aliases are groups of images that work on different architectures.
#
package OSImageAlias;
use Carp;
use English;
use Data::Dumper;
use File::Basename;
use Exporter;
use vars qw(@ISA @EXPORT $AUTOLOAD);

@ISA    = "Exporter";
@EXPORT = qw ( );

1459
use libtestbed;
1460 1461
use libdb;
use EmulabConstants;
1462
use emutil;
1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531
use Image;
use OSinfo;
use overload ('""' => 'Stringify');

#
# No versioning on aliases. Just pid,eid. 
#
sub Lookup($$;$)
{
    my ($class, $arg1, $arg2) = @_;
    my $query_result;

    #
    # A single arg is either an index or "pid,imagename[:version]" or
    # "pid/imagename[:version]" string.
    #
    if (!defined($arg2)) {
	if ($arg1 =~ /^(\d*)$/) {
	    $query_result =
		DBQueryWarn("select * from image_aliases ".
			    "where imageid='$arg1'");
	}
	elsif ($arg1 =~ /^([-\w]*),([-\w\.\+]*)$/ ||
	       $arg1 =~ /^([-\w]*)\/([-\w\.\+]*)$/) {
	    $query_result =
		DBQueryWarn("select * from image_aliases ".
			    "where pid='$1' and imagename='$2'");
	}
    }
    elsif ($arg1 =~ /^[-\w]*$/ && $arg2 =~ /^[-\w\.\+]*$/) {
    	$query_result =
	    DBQueryWarn("select * from image_aliases ".
			"where pid='$arg1' and imagename='$arg2'");
    }
    return undef
	if (!defined($query_result) || !$query_result->numrows);

    my $self = {};
    $self->{'IMAGES'} = {};

    while (my $row = $query_result->fetchrow_hashref()) {
	
	#
	# Info about the alias, use the first row.
	#
	if (!exists($self->{'ALIAS'})) {
	    $self->{'ALIAS'} = $row;
	}
	my $target_imageid = $row->{'target_imageid'};
	my $image = OSImage->Lookup($target_imageid);
	if (!defined($image)) {
	    print STDERR "ImageAlias Lookup; $target_imageid does not exist!\n";
	    return undef;
	}
	$self->{'IMAGES'}->{$target_imageid} = $image;
    }
    bless($self, $class);
    return $self;
}
sub imagename($)    { return $_[0]->{'ALIAS'}->{'imagename'}; }
sub osname($)       { return $_[0]->{'ALIAS'}->{'imagename'}; }
sub imageid($)	    { return $_[0]->{'ALIAS'}->{'imageid'}; }
sub osid($)	    { return $_[0]->{'ALIAS'}->{'imageid'}; }
sub pid($)	    { return $_[0]->{'ALIAS'}->{'pid'}; }
sub pid_idx($)	    { return $_[0]->{'ALIAS'}->{'pid_idx'}; }
sub gid($)	    { return $_[0]->{'ALIAS'}->{'gid'}; }
sub gid_idx($)	    { return $_[0]->{'ALIAS'}->{'gid_idx'}; }
sub uuid($)	    { return $_[0]->{'ALIAS'}->{'uuid'}; }
sub imagelist($)    { return values(%{ $_[0]->{'IMAGES'} }); }
1532
sub isImage($)      { return 0; }
1533 1534
sub isImageAlias($) { return 1; }
sub IsGeneric($)    { return 0; }
1535
sub mfs($)          { return 0; }
1536 1537
sub path($)         { return undef; }

1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586
#
# Create a new Image Alias.
#
sub Create($$$@)
{
    my ($class, $name, $group, @targets) = @_;

    #
    # Grab unique ID. Table already locked.
    # 
    my $imageid = TBGetUniqueIndex("next_osid");
    my $uuid    = NewUUID();
    my $pid     = $group->pid();
    my $pid_idx = $group->pid_idx();
    my $gid     = $group->gid();
    my $gid_idx = $group->gid_idx();

    foreach my $target (@targets) {
	my $target_name    = $target->imagename();
	my $target_imageid = $target->imageid();

	DBQueryWarn("insert into image_aliases set ".
		    "  imagename='$name',imageid='$imageid',uuid='$uuid', ".
		    "  pid='$pid',pid_idx='$pid_idx',".
		    "  gid='$gid',gid_idx='$gid_idx',".
		    "  target_imagename='$target_name',".
		    "  target_imageid='$target_imageid'")
	    or goto bad;
    }
    return OSImageAlias->Lookup($imageid);
  bad:
    DBQueryWarn("delete from image_aliases where imageid='$imageid'");
    return -1;
}

#
# Delete.
#
sub Delete($)
{
    my ($self) = @_;
    my $imageid = $self->imageid();

    return -1
	if (!DBQueryWarn("delete from image_aliases ".
			 "where imageid='$imageid'"));
    return 0;
}

1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730
#
# Lookup a "system" image, meaning one in the emulab-ops project.
#
sub LookupByName($$)
{
    my ($class, $imagename) = @_;

    return OSImageAlias->Lookup(TBOPSPID(), $imagename);
}

AUTOLOAD {
    my $self  = $_[0];
    my $type  = ref($self) or croak "$self is not an object";
    my $name  = $AUTOLOAD;
    $name =~ s/.*://;   # strip fully-qualified portion

    carp("ImageAlias: wanted slot '$name' field in $self");
    return undef;
}

# Break circular references to avoid exit errors.
sub DESTROY {
    my $self = shift;

    $self->{'ALIAS'}  = undef;
    $self->{'IMAGES'} = undef;
}

sub Stringify($)
{
    my ($self) = @_;
    my $pid  = $self->pid();
    my $name = $self->imagename();
    my $id   = $self->imageid();
    
    return "[ImageAlias $pid,$name $id]";
}

sub urn($)
{
    my ($self) = @_;

    return undef
	if (! $PGENISUPPORT);    

    require GeniHRN;
    my $pid    = $self->pid();
    my $name   = $self->imagename();
    my $domain = $OURDOMAIN;

    return GeniHRN::Generate($domain, "image", $pid . "//" . $name);
}    

#
# Given a node type, map the alias to the proper image.
# 
sub MapToImage($$)
{
    my ($self, $type) = @_;

    if (ref($type)) {
	$type = $type->type();
    }
    #
    # Look through each of the images and stop when we find one that
    # runs on this type.
    #
    foreach my $image ($self->imagelist()) {
	my @typelist = $image->TypeList();
	if (@typelist && grep {$_->type() eq $type} @typelist) {
	    return $image;
	}
    }
    return undef;
}

#
# List all aliases.
#
sub ListAll($)
{
    my ($class) = @_;
    my @result  = ();

    my $query_result =
	DBQueryWarn("select distinct imageid from image_aliases");

    while (my ($imageid) = $query_result->fetchrow_array()) {
	my $image = OSImageAlias->Lookup($imageid);
	if (!defined($image)) {
	    print STDERR "Could not lookup image aliase for $imageid\n";
	    next;
	}
	push(@result, $image);
    }
    return @result;
}

#
# Iterate over images in the list, calling function.
#
sub ImageIterate($$)
{
    my ($self, $function) = @_;
    my $retval = 0;

    foreach my $image ($self->imagelist()) {
	$retval++
	    if (&$function($image));
    }
    return $retval;
}

#
# List the images.
#
sub ShowImages($)
{
    my ($self) = @_;

    my $coderef = sub {
	print $_[0] . "\n";
	return 0;
    };
    $self->ImageIterate($coderef);
}

#
# The access check checks all of the images. It would make no sense to
# have mixed access.
#
sub AccessCheck($$$)
{
    my ($self, $user, $access_type) = @_;
    my $allowed = 1;
    my $coderef = sub {
	$allowed = 0
	    if (!$_[0]->AccessCheck($user, $access_type));
	return 0;
    };
    $self->ImageIterate($coderef);
    return $allowed;
}

1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743
# All images have to be shared.
sub shared($)
{
    my ($self) = @_;
    my $shared = 1;
    my $coderef = sub {
	$shared = 0
	    if (!$_[0]->shared());
	return 0;
    };
    $self->ImageIterate($coderef);
    return $shared;
}
1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756
# All images have to be global.
sub global($)
{
    my ($self) = @_;
    my $global = 1;
    my $coderef = sub {
	$global = 0
	    if (!$_[0]->global());
	return 0;
    };
    $self->ImageIterate($coderef);
    return $global;
}
1757

1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833
#
# Get the combined type list.
#
sub TypeList($)
{
    my ($self)  = @_;

    my %types   = ();
    my $coderef = sub {
	foreach my $type ($_[0]->TypeList()) {
	    $types{$type->type()} = $type;
	}
    };
    $self->ImageIterate($coderef);
    return values(%types);
}

#
# A feature is supported if all of the images support that feature.
#
sub FeatureSupported($$)
{
    my ($self, $feature) = @_;
    my $supported = 1;
    my $coderef   = sub {
	$supported = 0
	    if (!$_[0]->FeatureSupported($feature));
	return 0;
    };
    $self->ImageIterate($coderef);
    return $supported;
}

#
# So this is a little strange. Given an image, we often want the parent
# image (as for virtnodes and subnodes). But if the image is an alias,
# what is the parent? We can't just pick one of them, that seems wrong.
#
# So lets make up an alias for the parents and see how it goes. Unless
# of course, there is only one actual parent.
#
sub DefaultParent($)
{
    my ($self) = @_;
    my %parents = ();

    foreach my $image ($self->imagelist()) {
	my $parent = $image->DefaultParent();
	return undef
	    if (!defined($parent));

	$parents{$parent->imageid()} = $parent;
    }
    my $image0 = (values(%parents))[0]->image();

    # Just return the one image.
    if (scalar(keys(%parents)) == 1) {
	return OSImage->Lookup($image0->imageid(), $image0->version());
    }

    my $parent = {};
    $parent->{'IMAGES'} = \%parents;
    $parent->{'ALIAS'}  = {
	"imagename" => "Parent of $self",
	"imageid"   => -1,
	"pid"	    => $image0->pid(),
	"pid_idx"   => $image0->pid_idx(),
	"gid"	    => $image0->gid(),
	"gid_idx"   => $image0->gid_idx(),
    };
    bless($parent, "OSImageAlias");
    return $parent;
}

# _Always_ make sure that this 1 is at the end of the file...
1;