OSinfo.pm.in 20.1 KB
Newer Older
1
2
#!/usr/bin/perl -wT
#
3
# Copyright (c) 2007-2014 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
#
package OSinfo;

use strict;
27
use Carp;
28
use Exporter;
29
use vars qw(@ISA @EXPORT $AUTOLOAD);
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50

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

# Must come after package declaration!
use libdb;
use libtestbed;
use English;
use Data::Dumper;
use overload ('""' => 'Stringify');

# Configure variables
my $TB		  = "@prefix@";
my $BOSSNODE      = "@BOSSNODE@";
my $CONTROL	  = "@USERNODE@";
my $TBOPS         = "@TBOPSEMAIL@";
my $TBAUDIT       = "@TBAUDITEMAIL@";
my $TBBASE        = "@TBBASE@";
my $TBWWW         = "@TBWWW@";

# Cache of instances to avoid regenerating them.
51
52
53
54
55
56
57
58
59
60
61
my %osids	= ();
my $debug	= 0;

# Valid features. Mirrored in the web interface. The value is a user-okay flag.
my %FEATURES	= ( "ping"	 => 1,
		    "ssh",	 => 1,
		    "ipod"	 => 1,
		    "isup"	 => 1,
		    "veths"	 => 0,
		    "mlinks"	 => 0,
		    "linktest"	 => 1,
62
63
		    "linkdelays" => 0,
		    "suboses"  => 0 );
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87

# Valid OS names. Mirrored in the web interface. The value is a user-okay flag.
my %OSLIST	= ( "Linux"	 => 1,
		    "Fedora"	 => 1,
		    "FreeBSD"	 => 1,
		    "NetBSD"	 => 1,
		    "Windows"	 => 1,
		    "TinyOS"	 => 1,
		    "Oskit"	 => 0,
		    "Other"	 => 1 );

# Default OSID boot wait timeouts in seconds. Mirrored in the web interface. 
my %WAITTIMES    = ("Linux"	 => 120,
		    "Fedora"	 => 120,
		    "FreeBSD"	 => 120,
		    "NetBSD"	 => 120,
		    "Windows"	 => 240,
		    "TinyOS"	 => 60,
		    "Oskit"	 => 60,
		    "Other"	 => 60 );

# OP modes. Mirrored in the web interface. The value is a user-okay flag.
my %OPMODES	 = ("NORMALv2"	 => 1,
		    "NORMALv1"	 => 0,
Leigh B Stoller's avatar
Leigh B Stoller committed
88
89
		    "PXEFBSD"    => 0,
		    "RELOAD"     => 0,
90
		    "RELOAD-PCVM"=> 0,
Leigh B Stoller's avatar
Leigh B Stoller committed
91
		    "OPSNODEBSD" => 0,
Leigh B Stoller's avatar
Leigh B Stoller committed
92
		    "PCVM" 	 => 0,
93
94
95
		    "MINIMAL"	 => 1,
		    "NORMAL"	 => 1,
		    "ALWAYSUP"	 => 1 );
96
97
98
99
100
101
102
103
104
105
106

# Little helper and debug function.
sub mysystem($)
{
    my ($command) = @_;

    print STDERR "Running '$command'\n"
	if ($debug);
    return system($command);
}

107
108
109
110
111
112
113
114
115
116
117
118
119
sub BlessRow($$)
{
    my ($class, $row) = @_;
    
    my $self           = {};
    my $osid           = $row->{"osid"};
    $self->{'OSINFO'}  = $row;

    bless($self, $class);
    
    return $self;
}

120
#
121
122
# Lookup by idx or pid,osname[:version] depending on the args. We always 
# return highest numbered version on this path, if no version specified.
123
#
124
sub Lookup($$;$$)
125
{
126
    my ($class, $arg1, $arg2, $arg3) = @_;
127

Mike Hibler's avatar
Mike Hibler committed
128
129
130
    return undef
	if (!defined($arg1));

131
    #
132
133
    # A single arg is either an index or "pid,osname[:version]" or
    # "pid/osname[:version]" string.
134
135
136
    #
    if (!defined($arg2)) {
	if ($arg1 =~ /^(\d*)$/) {
137
	    my $result =
138
		DBQueryWarn("select v.* from os_info as o ".
139
140
141
142
143
144
145
			    "left join os_info_versions as v on ".
			    "     v.osid=o.osid and v.vers=o.version ".
			    "where o.osid='$arg1'");
	    return undef
		if (! $result || !$result->numrows);

	    return BlessRow($class, $result->fetchrow_hashref());
146
147
	}
	elsif ($arg1 =~ /^([-\w]*),([-\w\.\+]*)$/ ||
148
149
		$arg1 =~ /^([-\w]*)\/([-\w\.\+]*)$/) {
	    my $result =
150
		DBQueryWarn("select v.* from os_info as o ".
151
152
153
154
155
156
157
			    "left join os_info_versions as v on ".
			    "     v.osid=o.osid and v.vers=o.version ".
			    "where o.pid='$1' and o.osname='$2'");
	    return undef
		if (! $result || !$result->numrows);

	    return BlessRow($class, $result->fetchrow_hashref());
158
	}
159
160
161
	elsif ($arg1 =~ /^([-\w]*),([-\w\.\+]*):(\d*)$/ ||
		$arg1 =~ /^([-\w]*)\/([-\w\.\+]*):(\d*)$/) {
	    my $result =
162
		DBQueryWarn("select v.* from os_info as o ".
163
164
			    "left join os_info_versions as v on ".
			    "     v.osid=o.osid ".
165
166
			    "where o.pid='$1' and o.osname='$2' and ".
			    "      v.vers='$3' and v.deleted is null");
167
168
169
170
171

	    return undef
		if (!$result || !$result->numrows);

	    return BlessRow($class, $result->fetchrow_hashref());
172
	}
173
174
	elsif ($arg1 =~ /^\w+\-\w+\-\w+\-\w+\-\w+$/) {
	    my $result =
175
		DBQueryWarn("select * from os_info_versions ".
176
177
178
179
180
181
182
183
			    "where uuid='$arg1' and deleted is null");

	    return undef
		if (! $result || !$result->numrows);

	    return BlessRow($class, $result->fetchrow_hashref());
	}
	return undef;
184
    }
185
186
    elsif (!defined($arg3)) {
	if ($arg1 =~ /^\d+$/ && $arg2 =~ /^\d+$/) {
187
188
189
	    #
	    # This will get deleted OSs, but that is okay.
	    #
190
	    my $result =
191
192
		DBQueryWarn("select v.* from os_info_versions as v ".
			    "where v.osid='$arg1' and v.vers='$arg2'");
193
194
195
196
197
198
199
	    return undef
		if (! $result || !$result->numrows);

	    return BlessRow($class, $result->fetchrow_hashref());
	}
	elsif ($arg1 =~ /^[-\w]*$/ && $arg2 =~ /^([-\w\.\+]*):(\d+)$/) {
	    my $result =
200
		DBQueryWarn("select v.* from os_info as o ".
201
202
203
204
205
206
207
208
209
210
211
			    "left join os_info_versions as v on ".
			    "     v.osid=o.osid  ".
			    "where o.pid='$arg1' and o.osname='$1' and ".
			    "      v.vers='$2'");
	    return undef
		if (! $result || !$result->numrows);

	    return BlessRow($class, $result->fetchrow_hashref());
	}
	elsif ($arg1 =~ /^[-\w]*$/ && $arg2 =~ /^[-\w\.\+]*$/) {
	    my $result =
212
		DBQueryWarn("select v.* from os_info as o ".
213
214
215
216
217
218
219
220
			    "left join os_info_versions as v on ".
			    "     v.osid=o.osid and v.vers=o.version ".
			    "where o.pid='$arg1' and o.osname='$arg2'");
	    return undef
		if (! $result || !$result->numrows);

	    return BlessRow($class, $result->fetchrow_hashref());
	}
221
222
	return undef;
    }
223
224
225
226
    else {
	if ($arg1 =~ /^[-\w]*$/ &&
	    $arg2 =~ /^[-\w\.\+]*$/ && $arg3 =~ /^\d+$/) {
	    my $result =
227
		DBQueryWarn("select v.* from os_info as o ".
228
229
230
231
232
233
234
235
236
237
238
			    "left join os_info_versions as v on ".
			    "     v.osid=o.osid ".
			    "where o.pid='$arg1' and o.osname='$arg2' and ".
			    "      v.vers='$arg3' and v.deleted is null");

	    return undef
		if (!$result || !$result->numrows);

	    return BlessRow($class, $result->fetchrow_hashref());
	}
	return undef;
239
    }
240
    return undef;
241
242
}

243
244
245
246
#
# Lookup newest version of osinfo.
#
sub LookupNewest($)
247
{
248
    my ($self) = @_;
249

250
    return OSinfo->Lookup($self->pid(), $self->osname());
251
}
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272

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

    # A DB row proxy method call.
    if (exists($self->{'OSINFO'}->{$name})) {
	return $self->{'OSINFO'}->{$name};
    }
    carp("No such slot '$name' field in class $type");
    return undef;
}

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

    $self->{'OSINFO'} = undef;
}
273

274
#
Russ Fish's avatar
Russ Fish committed
275
# Create a new os_info. This installs the new record in the DB,
276
277
# and returns an instance. There is some bookkeeping along the way.
#
278
sub Create($$$$$$)
279
{
Russ Fish's avatar
Russ Fish committed
280
    my ($class, $project, $creator, $osname, $argref, $usrerr_ref) = @_;
281
282
283
284
285
286
287
288
289
290
291
292
293
294
    my $idx;
    my $now = time();

    return undef
	if (ref($class) || !ref($project));

    my $pid     = $project->pid();
    my $pid_idx = $project->pid_idx();
    my $uid     = $creator->uid();
    my $uid_idx = $creator->uid_idx();

    #
    # The pid/osid has to be unique, so lock the table for the check/insert.
    #
295
296
    DBQueryWarn("lock tables os_info write, os_info_versions write, ".
		"            emulab_indicies write")
297
298
299
300
301
302
303
304
	or return undef;

    my $query_result =
	DBQueryWarn("select osname from os_info ".
		    "where pid_idx='$pid_idx' and osname='$osname'");

    if ($query_result->numrows) {
	DBQueryWarn("unlock tables");
Russ Fish's avatar
Russ Fish committed
305
	$$usrerr_ref = "Error: OS $osname in project $pid already exists!";
306
307
308
309
310
311
312
313
314
	return undef;
    }

    #
    # Grab unique ID. Table already locked.
    # 
    my $osid  = TBGetUniqueIndex("next_osid", undef, 1);
    my $uuid  = NewUUID();
    my $desc  = "''";
315
    my $magic = "''";
316

317
    #
318
319
320
321
322
323
324
325
326
    # Some fields special cause of quoting.
    #
    if (exists($argref->{'description'})) {
	$desc = DBQuoteSpecial($argref->{'description'});
    }
    if (exists($argref->{'magic'})) {
	$magic = DBQuoteSpecial($argref->{'magic'});
    }
    
327
328
329
330
331
    # Filter arg array to just valid slots that we don't explicitly set below.
    my @arg_slots;
    foreach my $key ("old_osid", "OS", "version", "path", "machinetype", 
		     "osfeatures", "ezid", "shared", "mustclean",
		     "op_mode", "nextosid", "old_nextosid",
332
333
		     "max_concurrent", "mfs", "reboot_waittime",
		     "def_parentosid") {
334
335
336
337
	if (exists($argref->{$key})) {
	    push(@arg_slots, $key);
	}
    }
338
339
340
341
    my $bquery = "osname='$osname'";
    $bquery .= ",osid='$osid'";
    $bquery .= ",uuid='$uuid'";
    $bquery .= ",pid='$pid',pid_idx='$pid_idx'";
342

343
    my $query = "insert into os_info_versions set $bquery, ".
344
	join(",", map("$_='" . $argref->{$_} . "'", @arg_slots));
345
346
347
348
349

    # Append the rest
    $query .= ",creator='$uid',creator_idx='$uid_idx'";
    $query .= ",created=now()";
    $query .= ",description=$desc";    
350
351
352
353
354
    $query .= ",magic=$magic";

    if (! (DBQueryWarn("insert into os_info set $bquery") &&
	   DBQueryWarn($query))) {
	DBQueryWarn("delete from os_info where osid='$osid'");
355
356
357
358
359
360
361
362
363
	DBQueryWarn("unlock tables");
	tberror("Error inserting new os_info record for $pid/$osname!");
	return undef;
    }
    DBQueryWarn("unlock tables");

    return OSinfo->Lookup($osid);
}

364
365
366
367
368
369
370
371
372
373
#
# Lookup a "system" osid, meaning one in the emulab-ops project.
#
sub LookupByName($$)
{
    my ($class, $osname) = @_;

    return OSinfo->Lookup(TBOPSPID(), $osname);
}

374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
#
# For a newly created os_info, set the provenance pointers back to
# the os_info from which it was derived.
#
sub SetProvenance($$)
{
    my ($self, $base) = @_;
    my $parent_osid = $base->osid();
    my $parent_vers = $base->vers();

    $self->Update({"parent_osid" => $parent_osid,
		   "parent_vers" => $parent_vers})
	== 0 or return -1;

    return 0;
}

391
392
393
394
395
396
397
398
399
400
401
#
# Refresh a class instance by reloading from the DB.
#
sub Refresh($)
{
    my ($self) = @_;

    return -1
	if (! ref($self));

    my $osid = $self->osid();
402
    my $vers = $self->vers();
403
404
    
    my $query_result =
405
406
	DBQueryWarn("select * from os_info_versions ".
		    "where osid=$osid and vers=$vers");
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424

    return -1
	if (!$query_result || !$query_result->numrows);

    $self->{'OSINFO'} = $query_result->fetchrow_hashref();

    return 0;
}

#
# Stringify for output.
#
sub Stringify($)
{
    my ($self) = @_;
    
    my $pid    = $self->pid();
    my $osid   = $self->osid();
425
    my $vers   = $self->vers();
426
427
    my $osname = $self->osname();

428
    return "[OS $osid:$vers $pid,$osname]";
429
430
431
432
433
434
435
436
437
438
439
440
441
442
}

#
# Perform some updates ...
#
sub Update($$)
{
    my ($self, $argref) = @_;

    # Must be a real reference. 
    return -1
	if (! ref($self));

    my $osid = $self->osid();
443
444
445
    my $vers = $self->vers();
    
    my $query = "update os_info_versions set ".
446
447
	join(",", map("$_='" . $argref->{$_} . "'", keys(%{$argref})));

448
    $query .= " where osid='$osid' and vers='$vers'";
449
450
451
452
453
454
455

    return -1
	if (! DBQueryWarn($query));

    return Refresh($self);
}

456
457
458
459
460
461
#
# Load the project object for an osid
#
sub GetProject($)
{
    my ($self) = @_;
462
    require Project;
463
464
465
466
467
468
469
470
471
472
473
474
475
476

    # Must be a real reference. 
    return undef
	if (! ref($self));

    my $project = Project->Lookup($self->pid_idx());
    
    if (! defined($project)) {
	print("*** WARNING: Could not lookup project object for $self!", 1);
	return undef;
    }
    return $project;
}

477
478
479
480
481
482
483
484
#
# Check permissions.
#
sub AccessCheck($$$)
{
    my ($self, $user, $access_type) = @_;

    # Must be a real reference. 
485
    return 0
486
487
	if (! ref($self));

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
    my $mintrust;

    if ($access_type < TB_OSID_MIN || $access_type > TB_OSID_MAX) {
	print "*** Invalid access type $access_type!\n";
	return 0;
    }
    # Admins do whatever they want!
    return 1
	if ($user->IsAdmin());

    #
    # Global OSIDs can be read by anyone, but must be admin to write.
    #
    if ($self->shared()) {
	if ($access_type == TB_OSID_READINFO) {
	    return 1;
	}
	return 0;
    }

    my $project = $self->GetProject();
    return 0
	if (!defined($project));

    #
    # Otherwise must have proper trust in the project.
    #
    if ($access_type == TB_OSID_READINFO) {
	$mintrust = PROJMEMBERTRUST_USER;
    }
    else {
	$mintrust = PROJMEMBERTRUST_LOCALROOT;
    }
521

522
    return TBMinTrust($project->Trust($user), $mintrust);
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
#
# Class method to get the default reboot time for an os type.
#
sub RebootWaitTime($$)
{
    my ($self, $os) = @_;

    return $WAITTIMES{"other"}
	if (!exists($WAITTIMES{$os}));

    return $WAITTIMES{$os};
}

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

    return (exists($OSLIST{$os}) ? 1 : 0);
}

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

    return (exists($OPMODES{$opmode}) ? 1 : 0);
}

558
559
560
561
562
563
564
565
566
567
568
#
# Is the osinfo "generic"
#
sub IsGeneric($)
{
    my ($self)  = @_;
    my $version = $self->version();
    
    return ((defined($version) && $version ne "") ? 0 : 1);
}

569
570
571
572
573
574
575
576
577
578
579
#
# Boot command like. The caller supplies the default in $pref.
#
sub OSBootCmd($$$)
{
    my ($self, $role, $pref) = @_;

    return -1
	if (! (ref($self) && ref($pref)));

    my $osid = $self->osid();
580
    my $vers = $self->vers();
581
582
583

    my $query_result =
	DBQueryWarn("select ob.boot_cmd_line from os_info as oi ".
584
585
		    "left join os_info_versions as v on ".
		    "     v.osid=oi.osid and v.vers=oi.version ".
Leigh B Stoller's avatar
Leigh B Stoller committed
586
		    "left join os_boot_cmd as ob on ob.OS=v.OS and ".
587
		    "  ob.version=v.version ".
588
589
		    "where oi.osid='$osid' and oi.version='$vers' and ".
		    "       ob.role='$role'");
590
591
592
593
594
595
596
597
598
599
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

    return -1
	if (!$query_result || $query_result->numrows > 1);
    
    if ($query_result->numrows) {
	my ($cmdline) = $query_result->fetchrow_array();
	$$pref = $cmdline;
    }
    return 0;
}

#
# 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) = @_;

    return undef
	if (! ref($self));

    my $osid       = $self->osid();
    my $next_osid  = $self->osid();
    my $input_osid = $self->osid();

    my $count = 0;
    do {
	#
	# Just a guard to make sure we don't end up in a loop
	#
	if ($count++ > 10) {
627
	    warn "ResolveNextOSID $input_osid: Circular reference\n";
628
629
630
	}

	$osid = $next_osid;
631
632
	my $osinfo = OSinfo->Lookup($osid);
	if (!defined($osinfo)) {
633
634
635
	    warn "Resolving $input_osid: Unable to fetch os_info for $osid!\n";
	    return undef;
	}
636
637
	$osinfo = $osinfo->LookupNewest();
	($next_osid) = $osinfo->nextosid();
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680

	#
	# See if we need to resolve using a map.
	# Maps currently are only indexed by modification time;
	# i.e., we look at the last modification time of the experiment to
	# determine what OSID should be used.
	#
	# NOTE: mapping used to be done based on experiment *creation* time
	# but that left no ability to "update" an experiment to use current
	# images, at least short of creating a new experiment with the same
	# ns file.
	#
	# next_osid used to be MAP:osid_map, but now its an integer field
	# so just look for a 0 index, which is not a "valid" osid.
	#
	if (defined($next_osid) && $next_osid == 0) {
	    my $map = "osid_map";

	    my $timestr;
	    if (defined($experiment)) {
		my $pid = $experiment->pid();
		my $eid = $experiment->eid();
		
		my $m_result =
		    DBQueryWarn("select e.expt_created, s.swapmod_last ".
				"    from experiments as e, ".
				"         experiment_stats as s ".
				"where e.idx=s.exptidx and ".
				"e.pid='$pid' and e.eid='$eid'");
		if (!$m_result || $m_result->num_rows() == 0) {
		    warn "Resolving $input_osid: no experiment $pid/$eid!\n";
		    return undef;
		}
		my ($ctime,$mtime) = $m_result->fetchrow();
		if (defined($mtime) && $mtime ne "") {
		    $timestr = "'$mtime'";
		} else {
		    $timestr = "'$ctime'";
		}
	    } else {
		$timestr = "now()";
	    }

681
682
683
	    my $result = DBQueryWarn("select nextosid from $map ".
				     "where osid='$osid' and ".
				     "$timestr between btime and etime");
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
	    if (!$result) {
		warn "No such osid map $map!\n";
		return undef;
	    }

	    if ($result->num_rows() == 0) {
		warn "Resolving $input_osid: Unable to map $osid!\n";
		return undef;
	    }
	    ($next_osid) = $result->fetchrow();
	}
    } while ($next_osid);
    
    return OSinfo->Lookup($osid);
}

Leigh B Stoller's avatar
Leigh B Stoller committed
700
701
702
703
704
705
706
707
708
709
#
# Set the nextosid.
#
sub SetNextOS($$)
{
    my ($self, $nextosinfo) = @_;

    return $self->Update({"nextosid" => $nextosinfo->osid()});
}

710
711
712
713
714
715
716
sub SetParentOS($$)
{
    my ($self, $parent) = @_;

    return $self->Update({"def_parentosid" => $parent->osid()});
}

717
718
719
720
721
722
723
724
725
726
727
728
729
730
#
# Check if a particular feature is supported.
#
sub FeatureSupported($$)
{
    my ($self, $feature) = @_;

    my $osfeatures = $self->osfeatures();
    return 0
	if (!defined($osfeatures) || $osfeatures eq "");

    return grep {$_ eq $feature} split(',', $osfeatures);
}

Leigh B Stoller's avatar
Leigh B Stoller committed
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
#
# Add a feature.
#
sub AddFeature($$)
{
    my ($self, $feature) = @_;

    return 0
	if ($self->FeatureSupported($feature));

    my $osfeatures = $self->osfeatures();
    if (!defined($osfeatures) || $osfeatures eq "") {
	$osfeatures = $feature;
    }
    else {
	$osfeatures .= ",$feature";
    }
    return $self->Update({"osfeatures" => $osfeatures});
}

751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
#
# Return 1 if OS is a sub OS (i.e., has a def_parentosid).
#
sub IsSubOS($)
{
    my ($self,) = @_;

    my $def_parentosid = $self->def_parentosid();
    return 0
	if (!defined($def_parentosid) || $def_parentosid eq "");

    return 1;
}

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

772
773
    my ($osid,$osname)   = ($self->osid(),$self->osname());
    my $def_parentosid   = $self->def_parentosid();
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
    my ($posid,$posname) = ($parent->osid(),$parent->osname());

    return 0
	if (!$self->IsSubOS());

    return 1
	if ($posid == $def_parentosid);

    my $result = DBQueryWarn("select osid from os_submap".
			     " where osid=$osid and parent_osid=$posid");
    if (!$result || $result->num_rows() == 0) {
	warn "Child OS $osname cannot run on any OSes!\n";
	return 0;
    }

    return 1;
}

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

799
800
    my $osid  = $self->osid();
    my $posid = $parent->osid();
801
802
803
804
805

    return 0
	if (!$self->IsSubOS());

    DBQueryWarn("replace into os_submap set ".
806
		"  osid=$osid,parent_osid=$posid")
807
808
809
810
811
	or return -1;

    return 0;
}

812
813
814
815
816
817
#
# Map an osinfo and node type to the actual image.
#
sub MapToImage($$)
{
    my ($self, $type) = @_;
818
    require Image;
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835

    if (ref($type)) {
	$type = $type->type();
    }
    my $osid = $self->osid();

    my $query_result =
	DBQueryWarn("select imageid from osidtoimageid ".
		    "where type='$type' and osid='$osid'");
    return undef
	if (!defined($query_result) || !$query_result->numrows);

    my ($imageid) = $query_result->fetchrow_array();

    return Image->Lookup($imageid);
}

836
837
# Stubs for calling "libTaintStates" common taint handling code
sub GetTaintStates($) {
838
    my ($self) = @_;
839
    require libTaintStates;
840

841
    return libTaintStates::GetTaintStates($self);
842
}
843
844
845
sub IsTainted($;$) {
    my ($self, $taint) = @_;
    require libTaintStates;
846

847
848
    return libTaintStates::IsTainted($self, $taint);
}
849
850
sub SetTaintStates($@) {
    my ($self, @taint_states) = @_;
851
    require libTaintStates;
852

853
    return libTaintStates::SetTaintStates($self, @taint_states);
854
}
855
sub AddTaintState($$) {
856
    my ($self, $taint) = @_;
857
    require libTaintStates;
858

859
    return libTaintStates::AddTaintState($self, $taint);
860
}
861
sub RemoveTaintState($;$) {
862
    my ($self, $taint) = @_;
863
    require libTaintStates;
864

865
    return libTaintStates::RemoveTaintState($self, $taint);
866
}
867
868
869
870
871
872
sub InheritTaintStates($$) {
    my ($self, $osinfo) = @_;
    require libTaintStates;

    return libTaintStates::InheritTaintStates($self, $osinfo);
}
873

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