| 1 | package Illumos::Zones; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | # version | ||
| 7 | our $VERSION = '0.1.3'; | ||
| 8 | |||
| 9 | # commands | ||
| 10 | my $ZONEADM = '/usr/sbin/zoneadm'; | ||
| 11 | my $ZONECFG = '/usr/sbin/zonecfg'; | ||
| 12 | my $ZONENAME = '/usr/bin/zonename'; | ||
| 13 | |||
| 14 | my %ZMAP = ( | ||
| 15 | zoneid => 0, | ||
| 16 | zonename => 1, | ||
| 17 | state => 2, | ||
| 18 | zonepath => 3, | ||
| 19 | uuid => 4, | ||
| 20 | brand => 5, | ||
| 21 | 'ip-type' => 6, | ||
| 22 | ); | ||
| 23 | |||
| 24 | # properties that can only be set on creation | ||
| 25 | my @CREATEPROP = qw(zonename zonepath brand ip-type); | ||
| 26 | my @LXNETPROPS = qw(gateway ips primary); | ||
| 27 | |||
| 28 | my $regexp = sub { | ||
| 29 | my $rx = shift; | ||
| 30 | my $msg = shift; | ||
| 31 | |||
| 32 | return sub { | ||
| 33 | my $value = shift; | ||
| 34 | return $value =~ /$rx/ ? undef : "$msg ($value)"; | ||
| 35 | } | ||
| 36 | }; | ||
| 37 | |||
| 38 | my $elemOf = sub { | ||
| 39 | my $elems = [ @_ ]; | ||
| 40 | |||
| 41 | return sub { | ||
| 42 | my $value = shift; | ||
| 43 | return (grep { $_ eq $value } @$elems) ? undef | ||
| 44 | : 'expected a value from the list: ' . join(', ', @$elems); | ||
| 45 | } | ||
| 46 | }; | ||
| 47 | |||
| 48 | my $TEMPLATE = { | ||
| 49 | zonename => '', | ||
| 50 | zonepath => '', | ||
| 51 | brand => 'lipkg', | ||
| 52 | 'ip-type' => 'exclusive', | ||
| 53 | }; | ||
| 54 | |||
| 55 | my $SCHEMA = { | ||
| 56 | zonename => { | ||
| 57 | description => 'name of zone', | ||
| 58 | validator => $regexp->(qr/^[-\w]+$/, 'zonename not valid'), | ||
| 59 | }, | ||
| 60 | zonepath => { | ||
| 61 | description => 'path to zone root', | ||
| 62 | example => '"zonepath" : "/zones/mykvm"', | ||
| 63 | validator => $regexp->(qr/^\/[-\w\/]+$/, 'zonepath is not a valid path'), | ||
| 64 | }, | ||
| 65 | autoboot => { | ||
| 66 | optional => 1, | ||
| 67 | description => 'boot zone automatically', | ||
| 68 | validator => $elemOf->(qw(true false)), | ||
| 69 | }, | ||
| 70 | bootargs => { | ||
| 71 | optional => 1, | ||
| 72 | description => 'boot arguments for zone', | ||
| 73 | validator => sub { return undef }, | ||
| 74 | }, | ||
| 75 | pool => { | ||
| 76 | optional => 1, | ||
| 77 | description => 'name of the resource pool this zone must be bound to', | ||
| 78 | validator => sub { return undef }, | ||
| 79 | }, | ||
| 80 | limitpriv => { | ||
| 81 | description => 'the maximum set of privileges any process in this zone can obtain', | ||
| 82 | default => 'default', | ||
| 83 | validator => $regexp->(qr/^[-\w,]+$/, 'limitpriv not valid'), | ||
| 84 | }, | ||
| 85 | brand => { | ||
| 86 | description => "the zone's brand type", | ||
| 87 | default => 'lipkg', | ||
| 88 | validator => $elemOf->(qw(ipkg lipkg lx)), | ||
| 89 | }, | ||
| 90 | 'ip-type' => { | ||
| 91 | description => 'ip-type of zone. can either be "exclusive" or "shared"', | ||
| 92 | default => 'exclusive', | ||
| 93 | validator => $elemOf->(qw(exclusive shared)), | ||
| 94 | }, | ||
| 95 | hostid => { | ||
| 96 | optional => 1, | ||
| 97 | description => 'emulated 32-bit host identifier', | ||
| 98 | validator => $regexp->(qr/^(?:[\da-f]{1,8}|)$/i, 'hostid not valid'), | ||
| 99 | }, | ||
| 100 | 'cpu-shares' => { | ||
| 101 | optional => 1, | ||
| 102 | description => 'the number of Fair Share Scheduler (FSS) shares', | ||
| 103 | validator => $regexp->(qr/^\d+$/, 'cpu-shares not valid'), | ||
| 104 | }, | ||
| 105 | 'max-lwps' => { | ||
| 106 | optional => 1, | ||
| 107 | description => 'the maximum number of LWPs simultaneously available', | ||
| 108 | validator => $regexp->(qr/^\d+$/, 'max-lwps not valid'), | ||
| 109 | }, | ||
| 110 | 'max-msg-ids' => { | ||
| 111 | optional => 1, | ||
| 112 | description => 'the maximum number of message queue IDs allowed', | ||
| 113 | validator => $regexp->(qr/^\d+$/, 'max-msg-ids not valid'), | ||
| 114 | }, | ||
| 115 | 'max-sem-ids' => { | ||
| 116 | optional => 1, | ||
| 117 | description => 'the maximum number of semaphore IDs allowed', | ||
| 118 | validator => $regexp->(qr/^\d+$/, 'max-sem-ids not valid'), | ||
| 119 | }, | ||
| 120 | 'max-shm-ids' => { | ||
| 121 | optional => 1, | ||
| 122 | description => 'the maximum number of shared memory IDs allowed', | ||
| 123 | validator => $regexp->(qr/^\d+$/, 'max-shm-ids not valid'), | ||
| 124 | }, | ||
| 125 | 'max-shm-memory' => { | ||
| 126 | optional => 1, | ||
| 127 | description => 'the maximum amount of shared memory allowed', | ||
| 128 | validator => $regexp->(qr/^\d+[KMGT]?$/i, 'max-shm-memory not valid'), | ||
| 129 | }, | ||
| 130 | 'scheduling-class' => { | ||
| 131 | optional => 1, | ||
| 132 | description => 'Specifies the scheduling class used for processes running', | ||
| 133 | validator => sub { return undef }, | ||
| 134 | }, | ||
| 135 | 'fs-allowed' => { | ||
| 136 | optional => 1, | ||
| 137 | description => 'a comma-separated list of additional filesystems that may be mounted', | ||
| 138 | validator => $regexp->(qr/^(?:[-\w,]+|)$/, 'fs-allowed not valid'), | ||
| 139 | }, | ||
| 140 | attr => { | ||
| 141 | optional => 1, | ||
| 142 | array => 1, | ||
| 143 | description => 'generic attributes', | ||
| 144 | members => { | ||
| 145 | name => { | ||
| 146 | description => 'attribute name', | ||
| 147 | validator => sub { return undef }, | ||
| 148 | }, | ||
| 149 | type => { | ||
| 150 | description => 'attribute type', | ||
| 151 | validator => sub { return undef }, | ||
| 152 | }, | ||
| 153 | value => { | ||
| 154 | description => 'attribute value', | ||
| 155 | validator => sub { return undef }, | ||
| 156 | }, | ||
| 157 | }, | ||
| 158 | }, | ||
| 159 | 'capped-cpu' => { | ||
| 160 | optional => 1, | ||
| 161 | description => 'limits for CPU usage', | ||
| 162 | members => { | ||
| 163 | ncpus => { | ||
| 164 | description => 'sets the limit on the amount of CPU time. value is the percentage of a single CPU', | ||
| 165 | validator => $regexp->(qr/^(?:\d*\.\d+|\d+\.\d*)$/, 'ncpus value not valid. check man zonecfg'), | ||
| 166 | }, | ||
| 167 | }, | ||
| 168 | }, | ||
| 169 | 'capped-memory' => { | ||
| 170 | optional => 1, | ||
| 171 | description => 'limits for physical, swap, and locked memory', | ||
| 172 | members => { | ||
| 173 | physical => { | ||
| 174 | optional => 1, | ||
| 175 | description => 'limits of physical memory. can be suffixed by (K, M, G, T)', | ||
| 176 | validator => $regexp->(qr/^\d+[KMGT]?$/i, 'physical capped-memory is not valid. check man zonecfg'), | ||
| 177 | }, | ||
| 178 | swap => { | ||
| 179 | optional => 1, | ||
| 180 | description => 'limits of swap memory. can be suffixed by (K, M, G, T)', | ||
| 181 | validator => $regexp->(qr/^\d+[KMGT]?$/i, 'swap capped-memory is not valid. check man zonecfg'), | ||
| 182 | }, | ||
| 183 | locked => { | ||
| 184 | optional => 1, | ||
| 185 | description => 'limits of locked memory. can be suffixed by (K, M, G, T)', | ||
| 186 | validator => $regexp->(qr/^\d+[KMGT]?$/i, 'locked capped-memory is not valid. check man zonecfg'), | ||
| 187 | }, | ||
| 188 | }, | ||
| 189 | }, | ||
| 190 | dataset => { | ||
| 191 | optional => 1, | ||
| 192 | array => 1, | ||
| 193 | description => 'ZFS dataset', | ||
| 194 | members => { | ||
| 195 | name => { | ||
| 196 | description => 'the name of a ZFS dataset to be accessed from within the zone', | ||
| 197 | validator => $regexp->(qr/^\w[-\w\/]+$/, 'dataset name not valid. check man zfs'), | ||
| 198 | }, | ||
| 199 | }, | ||
| 200 | }, | ||
| 201 | 'dedicated-cpu' => { | ||
| 202 | optional => 1, | ||
| 203 | description => "subset of the system's processors dedicated to this zone while it is running", | ||
| 204 | members => { | ||
| 205 | ncpus => { | ||
| 206 | description => "the number of cpus that should be assigned for this zone's exclusive use", | ||
| 207 | validator => $regexp->(qr/^\d+(?:-\d+)?$/, 'dedicated-cpu ncpus not valid. check man zonecfg'), | ||
| 208 | }, | ||
| 209 | importance => { | ||
| 210 | optional => 1, | ||
| 211 | description => 'specifies the pset.importance value for use by poold', | ||
| 212 | validator => sub { return undef }, | ||
| 213 | }, | ||
| 214 | }, | ||
| 215 | }, | ||
| 216 | device => { | ||
| 217 | optional => 1, | ||
| 218 | array => 1, | ||
| 219 | description => 'device', | ||
| 220 | members => { | ||
| 221 | match => { | ||
| 222 | description => 'device name to match', | ||
| 223 | validator => sub { return undef }, | ||
| 224 | }, | ||
| 225 | }, | ||
| 226 | }, | ||
| 227 | fs => { | ||
| 228 | optional => 1, | ||
| 229 | array => 1, | ||
| 230 | description => 'file-system', | ||
| 231 | members => { | ||
| 232 | dir => { | ||
| 233 | description => 'directory of the mounted filesystem', | ||
| 234 | validator => $regexp->(qr/^\/[-\w\/\.]+$/, 'dir is not a valid directory'), | ||
| 235 | }, | ||
| 236 | special => { | ||
| 237 | description => 'path of fs to be mounted', | ||
| 238 | validator => $regexp->(qr/^[-\w\/\.]+$/, 'special is not valid'), | ||
| 239 | }, | ||
| 240 | raw => { | ||
| 241 | optional => 1, | ||
| 242 | description => 'path of raw disk', | ||
| 243 | validator => $regexp->(qr/^\/[-\w\/]+$/, 'raw is not valid'), | ||
| 244 | }, | ||
| 245 | type => { | ||
| 246 | description => 'type of fs', | ||
| 247 | validator => $elemOf->(qw(lofs zfs)), | ||
| 248 | }, | ||
| 249 | options => { | ||
| 250 | optional => 1, | ||
| 251 | description => 'mounting options', | ||
| 252 | validator => $regexp->(qr/^\[?[\w,]+\]?$/, 'options not valid'), | ||
| 253 | }, | ||
| 254 | }, | ||
| 255 | }, | ||
| 256 | net => { | ||
| 257 | optional => 1, | ||
| 258 | array => 1, | ||
| 259 | description => 'network interface', | ||
| 260 | members => { | ||
| 261 | address => { | ||
| 262 | optional => 1, | ||
| 263 | description => 'IP address of network interface', | ||
| 264 | validator => $regexp->(qr/^\d{1,3}(?:\.\d{1,3}){3}(?:\/\d{1,2})?$/, 'IP address not valid'), | ||
| 265 | }, | ||
| 266 | physical => { | ||
| 267 | description => 'network interface', | ||
| 268 | validator => $regexp->(qr/^[-\w]+/, 'physical not valid'), | ||
| 269 | }, | ||
| 270 | defrouter => { | ||
| 271 | optional => 1, | ||
| 272 | description => 'IP address of default router', | ||
| 273 | validator => $regexp->(qr/^\d{1,3}(?:\.\d{1,3}){3}$/, 'IP address not valid'), | ||
| 274 | }, | ||
| 275 | ips => { | ||
| 276 | optional => 1, | ||
| 277 | array => 1, | ||
| 278 | description => 'IPs for LX zones', | ||
| 279 | validator => $regexp->(qr/^\d{1,3}(?:\.\d{1,3}){3}(?:\/\d{1,2})$/, 'Not a valid CIDR IP address'), | ||
| 280 | }, | ||
| 281 | gateway => { | ||
| 282 | optional => 1, | ||
| 283 | description => 'Gateway for LX zones', | ||
| 284 | validator => $regexp->(qr/^\d{1,3}(?:\.\d{1,3}){3}$/, 'IP address not valid'), | ||
| 285 | }, | ||
| 286 | primary => { | ||
| 287 | optional => 1, | ||
| 288 | description => 'Primary Interface for LX zones', | ||
| 289 | validator => $elemOf->(qw(true false)), | ||
| 290 | }, | ||
| 291 | }, | ||
| 292 | }, | ||
| 293 | rctl => { | ||
| 294 | optional => 1, | ||
| 295 | array => 1, | ||
| 296 | description => 'resource control', | ||
| 297 | members => { | ||
| 298 | name => { | ||
| 299 | description => 'resource name', | ||
| 300 | validator => sub { return undef }, | ||
| 301 | }, | ||
| 302 | value => { | ||
| 303 | description => 'resource value', | ||
| 304 | validator => sub { return undef }, | ||
| 305 | }, | ||
| 306 | }, | ||
| 307 | }, | ||
| 308 | }; | ||
| 309 | |||
| 310 | # private methods | ||
| 311 | my $RESOURCES = sub { | ||
| 312 | return [ map { $SCHEMA->{$_}->{members} ? $_ : () } keys %$SCHEMA ]; | ||
| 313 | }; | ||
| 314 | |||
| 315 | my $resIsArray = sub { | ||
| 316 | my $self = shift; | ||
| 317 | my $res = shift; | ||
| 318 | |||
| 319 | return $SCHEMA->{$res}->{array}; | ||
| 320 | }; | ||
| 321 | |||
| 322 | my $RESARRAYS = sub { | ||
| 323 | return [ map { $SCHEMA->{$_}->{array} ? $_ : () } @{$RESOURCES->()} ]; | ||
| 324 | }; | ||
| 325 | |||
| 326 | my $zoneCmd = sub { | ||
| 327 | my $self = shift; | ||
| 328 | my $zoneName = shift; | ||
| 329 | my $cmd = shift; | ||
| 330 | my @opts = @_; | ||
| 331 | |||
| 332 | my @cmd = ($ZONEADM, '-z', $zoneName, $cmd, @opts); | ||
| 333 | |||
| 334 | print STDERR '# ' . join(' ', @cmd) . "\n" if $self->{debug}; | ||
| 335 | system(@cmd) and die "ERROR: cannot $cmd zone $zoneName\n"; | ||
| 336 | }; | ||
| 337 | |||
| 338 | my $encodeLXnetProp = sub { | ||
| 339 | my $self = shift; | ||
| 340 | my $prop = shift; | ||
| 341 | my $value = shift; | ||
| 342 | |||
| 343 | $value = ref $value eq 'ARRAY' ? "(name=$prop,value=\"" . join (',', @$value) . '")' | ||
| 344 | : "(name=$prop,value=\"$value\")"; | ||
| 345 | $prop = 'property'; | ||
| 346 | |||
| 347 | return ($prop, $value); | ||
| 348 | }; | ||
| 349 | |||
| 350 | my $decodeLXnetProp = sub { | ||
| 351 | my $self = shift; | ||
| 352 | my $prop = shift; | ||
| 353 | my $value = shift; | ||
| 354 | |||
| 355 | return ($prop, $value) if !($prop eq 'property'); | ||
| 356 | |||
| 357 | ($prop) = $value =~ /name=(\w+)/; | ||
| 358 | my @values = split /,/, ($value =~ /value="([^"]+)"/)[0]; | ||
| 359 | if (!$SCHEMA->{net}->{members}->{$prop}->{array}) { | ||
| 360 | return ($prop, $values[0]); | ||
| 361 | } | ||
| 362 | return ($prop, [ @values ]); | ||
| 363 | }; | ||
| 364 | |||
| 365 | # constructor | ||
| 366 | sub new { | ||
| 367 | my $class = shift; | ||
| 368 | my $self = { @_ }; | ||
| 369 | return bless $self, $class | ||
| 370 | } | ||
| 371 | |||
| 372 | # public methods | ||
| 373 | sub schema { | ||
| 374 | return $SCHEMA; | ||
| 375 | } | ||
| 376 | |||
| 377 | sub template { | ||
| 378 | return $TEMPLATE; | ||
| 379 | } | ||
| 380 | |||
| 381 | sub resources { | ||
| 382 | return $RESOURCES->(); | ||
| 383 | } | ||
| 384 | |||
| 385 | sub resourceArrays { | ||
| 386 | return $RESARRAYS->(); | ||
| 387 | } | ||
| 388 | |||
| 389 | # zoneName is a static method | ||
| 390 | sub zoneName { | ||
| 391 | my @cmd = ($ZONENAME); | ||
| 392 | |||
| 393 | open my $zones, '-|', @cmd | ||
| 394 | or die "ERROR: cannot get zonename\n"; | ||
| 395 | |||
| 396 | chomp (my $zonename = <$zones>); | ||
| 397 | |||
| 398 | return $zonename; | ||
| 399 | } | ||
| 400 | |||
| 401 | # isGZ is a static method | ||
| 402 | sub isGZ { | ||
| 403 | return zoneName() eq 'global'; | ||
| 404 | } | ||
| 405 | |||
| 406 | sub listZones { | ||
| 407 | my $self = shift; | ||
| 408 | |||
| 409 | my @cmd = ($ZONEADM, qw(list -cp)); | ||
| 410 | |||
| 411 | print STDERR '# ' . join(' ', @cmd) . "\n" if $self->{debug}; | ||
| 412 | open my $zones, '-|', @cmd | ||
| 413 | or die "ERROR: cannot get list of Zones\n"; | ||
| 414 | |||
| 415 | my $zoneList = []; | ||
| 416 | while (my $zone = <$zones>) { | ||
| 417 | chomp $zone; | ||
| 418 | push @$zoneList, { map { $_ => (split /:/, $zone, 7)[$ZMAP{$_}] } keys %ZMAP }; | ||
| 419 | } | ||
| 420 | |||
| 421 | return $zoneList; | ||
| 422 | } | ||
| 423 | |||
| 424 | sub listZone { | ||
| 425 | my $self = shift; | ||
| 426 | my $zoneName = shift; | ||
| 427 | |||
| 428 | my ($zone) = grep { $_->{zonename} eq $zoneName } @{$self->listZones}; | ||
| 429 | |||
| 430 | return $zone; | ||
| 431 | } | ||
| 432 | |||
| 433 | sub zoneState { | ||
| 434 | my $self = shift; | ||
| 435 | my $zoneName = shift; | ||
| 436 | |||
| 437 | my $zone = $self->listZone($zoneName); | ||
| 438 | |||
| 439 | return $zone ? $zone->{state} : undef; | ||
| 440 | } | ||
| 441 | |||
| 442 | sub boot { | ||
| 443 | my $self = shift; | ||
| 444 | |||
| 445 | $self->$zoneCmd(shift, 'boot'); | ||
| 446 | } | ||
| 447 | |||
| 448 | sub shutdown { | ||
| 449 | my $self = shift; | ||
| 450 | my $zoneName = shift; | ||
| 451 | my @reboot = $_[0] ? qw(-r) : (); | ||
| 452 | |||
| 453 | $self->$zoneCmd($zoneName, 'shutdown', @reboot); | ||
| 454 | } | ||
| 455 | |||
| 456 | sub reboot { | ||
| 457 | my $self = shift; | ||
| 458 | |||
| 459 | $self->shutdown(shift, 1); | ||
| 460 | }; | ||
| 461 | |||
| 462 | sub createZone { | ||
| 463 | my $self = shift; | ||
| 464 | my $zoneName = shift; | ||
| 465 | my $props = shift; | ||
| 466 | |||
| 467 | my @cmd = ($ZONECFG, '-z', $zoneName, qw(create -b ;)); | ||
| 468 | |||
| 469 | for my $prop (keys %$props) { | ||
| 470 | push @cmd, ('set', $prop, '=', $props->{$prop}, ';'); | ||
| 471 | } | ||
| 472 | |||
| 473 | print STDERR '# ' . join(' ', @cmd) . "\n" if $self->{debug}; | ||
| 474 | system(@cmd) and die "ERROR: cannot create zone $zoneName\n"; | ||
| 475 | } | ||
| 476 | |||
| 477 | sub deleteZone { | ||
| 478 | my $self = shift; | ||
| 479 | my $zoneName = shift; | ||
| 480 | |||
| 481 | my @cmd = ($ZONECFG, '-z', $zoneName, 'delete'); | ||
| 482 | |||
| 483 | print STDERR '# ' . join(' ', @cmd) . "\n" if $self->{debug}; | ||
| 484 | system(@cmd) and die "ERROR: cannot delete zone $zoneName\n"; | ||
| 485 | } | ||
| 486 | |||
| 487 | sub installZone { | ||
| 488 | my $self = shift; | ||
| 489 | my $zoneName = shift; | ||
| 490 | my $img = shift; | ||
| 491 | |||
| 492 | $self->$zoneCmd($zoneName, 'install', ($img ? ('-s', $img) : ())); | ||
| 493 | } | ||
| 494 | |||
| 495 | sub uninstallZone { | ||
| 496 | my $self = shift; | ||
| 497 | |||
| 498 | $self->$zoneCmd(shift, 'uninstall'); | ||
| 499 | } | ||
| 500 | |||
| 501 | sub zoneExists { | ||
| 502 | my $self = shift; | ||
| 503 | my $zoneName = shift; | ||
| 504 | |||
| 505 | my $zoneList = $self->listZones(); | ||
| 506 | |||
| 507 | for my $zone (@$zoneList){ | ||
| 508 | return 1 if $zone->{zonename} eq $zoneName; | ||
| 509 | } | ||
| 510 | |||
| 511 | return 0; | ||
| 512 | } | ||
| 513 | |||
| 514 | sub getZoneProperties { | ||
| 515 | my $self = shift; | ||
| 516 | my $zoneName = shift; | ||
| 517 | my $properties = {}; | ||
| 518 | |||
| 519 | return {} if !$self->zoneExists($zoneName); | ||
| 520 | |||
| 521 | my @cmd = ($ZONECFG, '-z', $zoneName, 'info'); | ||
| 522 | |||
| 523 | print STDERR '# ' . join(' ', @cmd) . "\n" if $self->{debug}; | ||
| 524 | open my $props, '-|', @cmd | ||
| 525 | or die "ERROR: cannot get properties of zone '$zoneName'\n"; | ||
| 526 | |||
| 527 | my $resName; | ||
| 528 | while (<$props>) { | ||
| 529 | chomp; | ||
| 530 | my ($isres, $property, $value) = /^(\s+)?([^:]+):(?:\s+(.*))?$/; | ||
| 531 | # at least property must be valid | ||
| 532 | $property or next; | ||
| 533 | |||
| 534 | if (defined $isres && length $isres > 0) { | ||
| 535 | # transform net properties for LX zones | ||
| 536 | ($property, $value) = $self->$decodeLXnetProp($property, $value) if $resName eq 'net'; | ||
| 537 | |||
| 538 | # check if property exists in schema | ||
| 539 | grep { $_ eq $property } keys %{$SCHEMA->{$resName}->{members}} or next; | ||
| 540 | if ($self->$resIsArray($resName)) { | ||
| 541 | $properties->{$resName}->[-1]->{$property} = $value; | ||
| 542 | } | ||
| 543 | else { | ||
| 544 | $properties->{$resName}->{$property} = $value; | ||
| 545 | } | ||
| 546 | } | ||
| 547 | else { | ||
| 548 | # check if property exists in schema | ||
| 549 | grep { $_ eq $property } keys %$SCHEMA or next; | ||
| 550 | # check if property is a resource | ||
| 551 | grep { $_ eq $property } @{$RESOURCES->()} and do { | ||
| 552 | $resName = $property; | ||
| 553 | if ($self->$resIsArray($property)) { | ||
| 554 | push @{$properties->{$property}}, {}; | ||
| 555 | } | ||
| 556 | next; | ||
| 557 | }; | ||
| 558 | $properties->{$property} = $value; | ||
| 559 | } | ||
| 560 | } | ||
| 561 | |||
| 562 | return $properties; | ||
| 563 | } | ||
| 564 | |||
| 565 | sub setZoneProperties { | ||
| 566 | my $self = shift; | ||
| 567 | my $zoneName = shift; | ||
| 568 | my $props = shift; | ||
| 569 | my $img = shift; | ||
| 570 | my $oldProps = $self->getZoneProperties($zoneName); | ||
| 571 | |||
| 572 | $self->zoneExists($zoneName) || $self->createZone($zoneName, | ||
| 573 | { map { $_ => $props->{$_} } @CREATEPROP }); | ||
| 574 | |||
| 575 | # remove props that cannot be changed after creation | ||
| 576 | delete $props->{$_} for @CREATEPROP; | ||
| 577 | |||
| 578 | my $state = $self->zoneState($zoneName); | ||
| 579 | $self->installZone($zoneName, $img) if $state eq 'configured'; | ||
| 580 | |||
| 581 | # clean up all resources | ||
| 582 | $self->clearResources($zoneName); | ||
| 583 | |||
| 584 | for my $prop (keys %$props) { | ||
| 585 | if (ref $props->{$prop} eq 'ARRAY') { | ||
| 586 | for my $elem (@{$props->{$prop}}) { | ||
| 587 | $self->addResource($zoneName, $prop, $elem); | ||
| 588 | } | ||
| 589 | } | ||
| 590 | elsif (grep { $_ eq $prop } @{$RESOURCES->()}) { | ||
| 591 | $self->addResource($zoneName, $prop, $props->{$prop}); | ||
| 592 | } | ||
| 593 | else { | ||
| 594 | next if $oldProps->{$prop} && $oldProps->{$prop} eq $props->{$prop}; | ||
| 595 | if ($props->{$prop}) { | ||
| 596 | $self->setProperty($zoneName, $prop, $props->{$prop}); | ||
| 597 | } | ||
| 598 | else { | ||
| 599 | $self->clearProperty($zoneName, $prop); | ||
| 600 | } | ||
| 601 | } | ||
| 602 | } | ||
| 603 | } | ||
| 604 | |||
| 605 | sub resourceExists { | ||
| 606 | my $self = shift; | ||
| 607 | my $zoneName = shift; | ||
| 608 | my $resource = shift; | ||
| 609 | my $property = shift; | ||
| 610 | my $value = shift; | ||
| 611 | |||
| 612 | my @cmd = ($ZONECFG, '-z', $zoneName, 'info', $resource); | ||
| 613 | |||
| 614 | print STDERR '# ' . join(' ', @cmd) . "\n" if $self->{debug}; | ||
| 615 | open my $res, '-|', @cmd | ||
| 616 | or die "ERROR: cannot get resource '$resource' of zone '$zoneName'\n"; | ||
| 617 | |||
| 618 | chomp (my @resources = <$res>); | ||
| 619 | |||
| 620 | return $property && $value ? grep { /\s+$property:\s+$value/ } @resources : @resources; | ||
| 621 | } | ||
| 622 | |||
| 623 | sub addResource { | ||
| 624 | my $self = shift; | ||
| 625 | my $zoneName = shift; | ||
| 626 | my $resource = shift; | ||
| 627 | my $props = shift; | ||
| 628 | |||
| 629 | my @cmd = ($ZONECFG, '-z', $zoneName, 'add', "$resource;"); | ||
| 630 | |||
| 631 | for my $property (keys %$props) { | ||
| 632 | # check if it is an LX net property | ||
| 633 | if (grep { $_ eq $property } @LXNETPROPS) { | ||
| 634 | my ($prop, $value) = $self->$encodeLXnetProp($property, $props->{$property}); | ||
| 635 | push @cmd, ('add', $prop, $value, ';'); | ||
| 636 | } | ||
| 637 | else { | ||
| 638 | push @cmd, ('set', "$property=$props->{$property};"); | ||
| 639 | } | ||
| 640 | } | ||
| 641 | push @cmd, qw(end); | ||
| 642 | |||
| 643 | print STDERR '# ' . join(' ', @cmd) . "\n" if $self->{debug}; | ||
| 644 | system(@cmd) and die "ERROR: cannot set properties for resource '$resource' of $zoneName\n"; | ||
| 645 | } | ||
| 646 | |||
| 647 | sub delResource { | ||
| 648 | my $self = shift; | ||
| 649 | my $zoneName = shift; | ||
| 650 | my $resource = shift; | ||
| 651 | my $property = shift; | ||
| 652 | my $value = shift; | ||
| 653 | |||
| 654 | return if !$self->resourceExists($zoneName, $resource, $property, $value); | ||
| 655 | |||
| 656 | my @cmd = ($ZONECFG, '-z', $zoneName, 'remove'); | ||
| 657 | if ($property && $value) { | ||
| 658 | push @cmd, ($resource, $property, '=', $value); | ||
| 659 | } | ||
| 660 | else { | ||
| 661 | push @cmd, ('-F', $resource); | ||
| 662 | } | ||
| 663 | |||
| 664 | print STDERR '# ' . join(' ', @cmd) . "\n" if $self->{debug}; | ||
| 665 | system(@cmd) and die "ERROR: cannot remove resource '$resource' of $zoneName\n"; | ||
| 666 | } | ||
| 667 | |||
| 668 | sub clearResources { | ||
| 669 | my $self = shift; | ||
| 670 | my $zoneName = shift; | ||
| 671 | |||
| 672 | for my $res (@{$RESOURCES->()}) { | ||
| 673 | $self->delResource($zoneName, $res); | ||
| 674 | } | ||
| 675 | } | ||
| 676 | |||
| 677 | sub setProperty { | ||
| 678 | my $self = shift; | ||
| 679 | my $zoneName = shift; | ||
| 680 | my $property = shift; | ||
| 681 | my $value = shift; | ||
| 682 | |||
| 683 | my @cmd = ($ZONECFG, '-z', $zoneName, 'set', $property, '=', "\"$value\""); | ||
| 684 | |||
| 685 | print STDERR '# ' . join(' ', @cmd) . "\n" if $self->{debug}; | ||
| 686 | system(@cmd) and die "ERROR: cannot set property $property of $zoneName\n"; | ||
| 687 | } | ||
| 688 | |||
| 689 | sub clearProperty { | ||
| 690 | my $self = shift; | ||
| 691 | my $zoneName = shift; | ||
| 692 | my $property = shift; | ||
| 693 | |||
| 694 | my @cmd = ($ZONECFG, '-z', $zoneName, 'clear', $property); | ||
| 695 | |||
| 696 | print STDERR '# ' . join(' ', @cmd) . "\n" if $self->{debug}; | ||
| 697 | system(@cmd) and die "ERROR: cannot remove property $property of $zoneName\n"; | ||
| 698 | } | ||
| 699 | |||
| 700 | 1; | ||
| 701 | |||
| 702 | __END__ |