OSinfo.pm.in 20.8 KB
Newer Older
1
2
#!/usr/bin/perl -wT
#
3
# Copyright (c) 2007-2015 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
148
149
150
151
152
153
154
155
156
157
	elsif ($arg1 =~ /^(\d+):(\d+)$/) {
	    my $result =
		DBQueryWarn("select v.* from os_info as o ".
			    "left join os_info_versions as v on ".
			    "     v.osid=o.osid ".
			    "where o.osid='$1' and v.vers='$2'");
	    return undef
		if (! $result || !$result->numrows);

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

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

	    return BlessRow($class, $result->fetchrow_hashref());
183
	}
184
185
	elsif ($arg1 =~ /^\w+\-\w+\-\w+\-\w+\-\w+$/) {
	    my $result =
186
		DBQueryWarn("select * from os_info_versions ".
187
188
189
190
191
192
193
194
			    "where uuid='$arg1' and deleted is null");

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

	    return BlessRow($class, $result->fetchrow_hashref());
	}
	return undef;
195
    }
196
197
    elsif (!defined($arg3)) {
	if ($arg1 =~ /^\d+$/ && $arg2 =~ /^\d+$/) {
198
199
200
	    #
	    # This will get deleted OSs, but that is okay.
	    #
201
	    my $result =
202
203
		DBQueryWarn("select v.* from os_info_versions as v ".
			    "where v.osid='$arg1' and v.vers='$arg2'");
204
205
206
207
208
209
210
	    return undef
		if (! $result || !$result->numrows);

	    return BlessRow($class, $result->fetchrow_hashref());
	}
	elsif ($arg1 =~ /^[-\w]*$/ && $arg2 =~ /^([-\w\.\+]*):(\d+)$/) {
	    my $result =
211
		DBQueryWarn("select v.* from os_info as o ".
212
213
214
215
216
217
218
219
220
221
222
			    "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 =
223
		DBQueryWarn("select v.* from os_info as o ".
224
225
226
227
228
229
230
231
			    "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());
	}
232
233
	return undef;
    }
234
235
236
237
    else {
	if ($arg1 =~ /^[-\w]*$/ &&
	    $arg2 =~ /^[-\w\.\+]*$/ && $arg3 =~ /^\d+$/) {
	    my $result =
238
		DBQueryWarn("select v.* from os_info as o ".
239
240
241
242
243
244
245
246
247
248
249
			    "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;
250
    }
251
    return undef;
252
253
}

254
255
256
257
#
# Lookup newest version of osinfo.
#
sub LookupNewest($)
258
{
259
    my ($self) = @_;
260

261
    return OSinfo->Lookup($self->pid(), $self->osname());
262
}
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283

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;
}
284

285
#
Russ Fish's avatar
Russ Fish committed
286
# Create a new os_info. This installs the new record in the DB,
287
288
# and returns an instance. There is some bookkeeping along the way.
#
289
sub Create($$$$$$)
290
{
Russ Fish's avatar
Russ Fish committed
291
    my ($class, $project, $creator, $osname, $argref, $usrerr_ref) = @_;
292
293
294
295
296
297
298
299
300
301
302
303
304
305
    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.
    #
306
307
    DBQueryWarn("lock tables os_info write, os_info_versions write, ".
		"            emulab_indicies write")
308
309
310
311
312
313
314
315
	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
316
	$$usrerr_ref = "Error: OS $osname in project $pid already exists!";
317
318
319
320
321
322
323
324
325
	return undef;
    }

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

328
    #
329
330
331
332
333
334
335
336
337
    # Some fields special cause of quoting.
    #
    if (exists($argref->{'description'})) {
	$desc = DBQuoteSpecial($argref->{'description'});
    }
    if (exists($argref->{'magic'})) {
	$magic = DBQuoteSpecial($argref->{'magic'});
    }
    
338
339
340
341
342
    # 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",
343
344
		     "max_concurrent", "mfs", "reboot_waittime",
		     "def_parentosid") {
345
346
347
348
	if (exists($argref->{$key})) {
	    push(@arg_slots, $key);
	}
    }
349
350
351
352
    my $bquery = "osname='$osname'";
    $bquery .= ",osid='$osid'";
    $bquery .= ",uuid='$uuid'";
    $bquery .= ",pid='$pid',pid_idx='$pid_idx'";
353

354
    my $query = "insert into os_info_versions set $bquery, ".
355
	join(",", map("$_='" . $argref->{$_} . "'", @arg_slots));
356
357
358
359
360

    # Append the rest
    $query .= ",creator='$uid',creator_idx='$uid_idx'";
    $query .= ",created=now()";
    $query .= ",description=$desc";    
361
362
363
364
365
    $query .= ",magic=$magic";

    if (! (DBQueryWarn("insert into os_info set $bquery") &&
	   DBQueryWarn($query))) {
	DBQueryWarn("delete from os_info where osid='$osid'");
366
367
368
369
370
371
372
373
374
	DBQueryWarn("unlock tables");
	tberror("Error inserting new os_info record for $pid/$osname!");
	return undef;
    }
    DBQueryWarn("unlock tables");

    return OSinfo->Lookup($osid);
}

375
376
377
378
379
380
381
382
383
384
#
# Lookup a "system" osid, meaning one in the emulab-ops project.
#
sub LookupByName($$)
{
    my ($class, $osname) = @_;

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

385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
#
# 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;
}

402
403
404
405
406
407
408
409
410
411
412
#
# Refresh a class instance by reloading from the DB.
#
sub Refresh($)
{
    my ($self) = @_;

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

    my $osid = $self->osid();
413
    my $vers = $self->vers();
414
415
    
    my $query_result =
416
417
	DBQueryWarn("select * from os_info_versions ".
		    "where osid=$osid and vers=$vers");
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435

    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();
436
    my $vers   = $self->vers();
437
438
    my $osname = $self->osname();

439
    return "[OS $osid:$vers $pid,$osname]";
440
441
442
443
444
445
446
447
448
449
450
451
452
453
}

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

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

    my $osid = $self->osid();
454
455
456
    my $vers = $self->vers();
    
    my $query = "update os_info_versions set ".
457
	join(",", map("$_=" . DBQuoteSpecial($argref->{$_}), keys(%{$argref})));
458
    $query .= " where osid='$osid' and vers='$vers'";
459
460
461
462
463
464
465

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

    return Refresh($self);
}

466
467
468
469
470
471
#
# Load the project object for an osid
#
sub GetProject($)
{
    my ($self) = @_;
472
    require Project;
473
474
475
476
477
478
479
480

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

    my $project = Project->Lookup($self->pid_idx());
    
    if (! defined($project)) {
481
	print("*** WARNING: Could not lookup project object for $self!\n");
482
483
484
485
486
	return undef;
    }
    return $project;
}

487
488
489
490
491
492
493
494
#
# Check permissions.
#
sub AccessCheck($$$)
{
    my ($self, $user, $access_type) = @_;

    # Must be a real reference. 
495
    return 0
496
497
	if (! ref($self));

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
    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;
    }
531

532
    return TBMinTrust($project->Trust($user), $mintrust);
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
#
# 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);
}

568
569
570
571
572
573
574
575
576
577
578
#
# Is the osinfo "generic"
#
sub IsGeneric($)
{
    my ($self)  = @_;
    my $version = $self->version();
    
    return ((defined($version) && $version ne "") ? 0 : 1);
}

579
580
581
582
583
584
585
586
587
sub IsNfsMfs($)
{
    my ($self)  = @_;

    return 0
	if (!ref($self));
    return ($self->mfs() && $self->path() eq TB_OSID_PATH_NFS());
}

588
589
590
591
592
593
594
595
596
597
598
#
# 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();
599
    my $vers = $self->vers();
600
601
602

    my $query_result =
	DBQueryWarn("select ob.boot_cmd_line from os_info as oi ".
603
604
		    "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
605
		    "left join os_boot_cmd as ob on ob.OS=v.OS and ".
606
		    "  ob.version=v.version ".
607
608
		    "where oi.osid='$osid' and oi.version='$vers' and ".
		    "       ob.role='$role'");
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

    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) {
646
	    warn "ResolveNextOSID $input_osid: Circular reference\n";
647
648
649
	}

	$osid = $next_osid;
650
651
	my $osinfo = OSinfo->Lookup($osid);
	if (!defined($osinfo)) {
652
653
654
	    warn "Resolving $input_osid: Unable to fetch os_info for $osid!\n";
	    return undef;
	}
655
656
	$osinfo = $osinfo->LookupNewest();
	($next_osid) = $osinfo->nextosid();
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699

	#
	# 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()";
	    }

700
701
702
	    my $result = DBQueryWarn("select nextosid from $map ".
				     "where osid='$osid' and ".
				     "$timestr between btime and etime");
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
	    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
719
720
721
722
723
724
725
726
727
728
#
# Set the nextosid.
#
sub SetNextOS($$)
{
    my ($self, $nextosinfo) = @_;

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

729
730
731
732
733
734
735
sub SetParentOS($$)
{
    my ($self, $parent) = @_;

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

736
737
738
739
740
741
742
743
744
745
746
747
748
749
#
# 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
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
#
# 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});
}

770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
#
# 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) = @_;

791
792
    my ($osid,$osname)   = ($self->osid(),$self->osname());
    my $def_parentosid   = $self->def_parentosid();
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
    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;
}

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

818
819
    my $osid  = $self->osid();
    my $posid = $parent->osid();
820
821
822
823
824

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

    DBQueryWarn("replace into os_submap set ".
825
		"  osid=$osid,parent_osid=$posid")
826
827
828
829
830
	or return -1;

    return 0;
}

831
832
833
834
835
836
#
# Map an osinfo and node type to the actual image.
#
sub MapToImage($$)
{
    my ($self, $type) = @_;
837
    require Image;
838
839
840
841
842
843
844
845
846
847
848
849
850

    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();
851
852
853
854
855
856
    # Look for corresponding image version. 
    my $image = Image->Lookup($imageid, $self->vers());
    if (!defined($image)) {
	$image = Image->Lookup($imageid);
    }
    return $image;
857
858
}

859
860
# Stubs for calling "libTaintStates" common taint handling code
sub GetTaintStates($) {
861
    my ($self) = @_;
862
    require libTaintStates;
863

864
    return libTaintStates::GetTaintStates($self);
865
}
866
867
868
sub IsTainted($;$) {
    my ($self, $taint) = @_;
    require libTaintStates;
869

870
871
    return libTaintStates::IsTainted($self, $taint);
}
872
873
sub SetTaintStates($@) {
    my ($self, @taint_states) = @_;
874
    require libTaintStates;
875

876
    return libTaintStates::SetTaintStates($self, @taint_states);
877
}
878
sub AddTaintState($$) {
879
    my ($self, $taint) = @_;
880
    require libTaintStates;
881

882
    return libTaintStates::AddTaintState($self, $taint);
883
}
884
sub RemoveTaintState($;$) {
885
    my ($self, $taint) = @_;
886
    require libTaintStates;
887

888
    return libTaintStates::RemoveTaintState($self, $taint);
889
}
890
891
892
893
894
895
sub InheritTaintStates($$) {
    my ($self, $osinfo) = @_;
    require libTaintStates;

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

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