| 423 | |
| 424 | |
| 425 | Future<Option<ContainerLaunchInfo>> CgroupsIsolatorProcess::prepare( |
| 426 | const ContainerID& containerId, |
| 427 | const ContainerConfig& containerConfig) |
| 428 | { |
| 429 | // If the nested container shares cgroups with its parent container, |
| 430 | // we don't need to prepare cgroups. In this case, the nested container |
| 431 | // will inherit cgroups from its ancestor, so here we just |
| 432 | // need to do the container-specific cgroups mounts. |
| 433 | const bool shareCgroups = |
| 434 | (containerConfig.has_container_info() && |
| 435 | containerConfig.container_info().has_linux_info() && |
| 436 | containerConfig.container_info().linux_info().has_share_cgroups()) |
| 437 | ? containerConfig.container_info().linux_info().share_cgroups() |
| 438 | : true; |
| 439 | |
| 440 | if (containerId.has_parent() && shareCgroups) { |
| 441 | return __prepare(containerId, containerConfig); |
| 442 | } |
| 443 | |
| 444 | if (infos.contains(containerId)) { |
| 445 | return Failure("Container has already been prepared"); |
| 446 | } |
| 447 | |
| 448 | CHECK(containerConfig.container_class() != ContainerClass::DEBUG); |
| 449 | |
| 450 | CHECK(!containerId.has_parent() || !containerId.parent().has_parent()) |
| 451 | << "2nd level or greater nested cgroups are not supported"; |
| 452 | |
| 453 | // We save 'Info' into 'infos' first so that even if 'prepare' |
| 454 | // fails, we can properly cleanup the *side effects* created below. |
| 455 | infos[containerId] = Owned<Info>(new Info( |
| 456 | containerId, |
| 457 | containerizer::paths::getCgroupPath(flags.cgroups_root, containerId))); |
| 458 | |
| 459 | vector<Future<Nothing>> prepares; |
| 460 | |
| 461 | // TODO(haosdent): Use foreachkey once MESOS-5037 is resolved. |
| 462 | foreach (const string& hierarchy, subsystems.keys()) { |
| 463 | string path = path::join(hierarchy, infos[containerId]->cgroup); |
| 464 | |
| 465 | VLOG(1) << "Creating cgroup at '" << path << "' " |
| 466 | << "for container " << containerId; |
| 467 | |
| 468 | if (cgroups::exists(hierarchy, infos[containerId]->cgroup)) { |
| 469 | return Failure("The cgroup at '" + path + "' already exists"); |
| 470 | } |
| 471 | |
| 472 | Try<Nothing> create = cgroups::create( |
| 473 | hierarchy, |
| 474 | infos[containerId]->cgroup, |
| 475 | true); |
| 476 | |
| 477 | if (create.isError()) { |
| 478 | return Failure( |
| 479 | "Failed to create the cgroup at " |
| 480 | "'" + path + "': " + create.error()); |
| 481 | } |
| 482 | |