2014-03-31 18:21:36 +01:00
|
|
|
|
#! @perl@
|
|
|
|
|
|
|
|
|
|
use strict;
|
2014-04-03 15:25:21 +01:00
|
|
|
|
use POSIX;
|
2014-03-31 18:21:36 +01:00
|
|
|
|
use File::Path;
|
|
|
|
|
use File::Slurp;
|
|
|
|
|
use Fcntl ':flock';
|
|
|
|
|
use Getopt::Long qw(:config gnu_getopt);
|
2016-06-24 15:02:26 +01:00
|
|
|
|
use Cwd 'abs_path';
|
2014-03-31 18:21:36 +01:00
|
|
|
|
|
2014-05-09 12:24:42 +01:00
|
|
|
|
# Ensure a consistent umask.
|
|
|
|
|
umask 0022;
|
|
|
|
|
|
2014-03-31 18:21:36 +01:00
|
|
|
|
# Parse the command line.
|
|
|
|
|
|
|
|
|
|
sub showHelp {
|
|
|
|
|
print <<EOF;
|
|
|
|
|
Usage: nixos-container list
|
2016-11-27 10:37:50 +00:00
|
|
|
|
nixos-container create <container-name> [--nixos-path <path>] [--system-path <path>] [--config-file <path>] [--config <string>] [--ensure-unique-name] [--auto-start] [--bridge <iface>]
|
2014-03-31 18:21:36 +01:00
|
|
|
|
nixos-container destroy <container-name>
|
|
|
|
|
nixos-container start <container-name>
|
|
|
|
|
nixos-container stop <container-name>
|
2016-08-03 17:54:19 +01:00
|
|
|
|
nixos-container terminate <container-name>
|
2014-08-18 12:46:54 +01:00
|
|
|
|
nixos-container status <container-name>
|
2016-06-24 15:02:26 +01:00
|
|
|
|
nixos-container update <container-name> [--config <string>] [--config-file <path>]
|
2014-03-31 18:21:36 +01:00
|
|
|
|
nixos-container login <container-name>
|
2014-04-10 12:12:34 +01:00
|
|
|
|
nixos-container root-login <container-name>
|
|
|
|
|
nixos-container run <container-name> -- args...
|
2014-03-31 18:21:36 +01:00
|
|
|
|
nixos-container show-ip <container-name>
|
2014-08-19 15:57:02 +01:00
|
|
|
|
nixos-container show-host-key <container-name>
|
2014-03-31 18:21:36 +01:00
|
|
|
|
EOF
|
|
|
|
|
exit 0;
|
|
|
|
|
}
|
|
|
|
|
|
2014-08-19 14:47:51 +01:00
|
|
|
|
my $systemPath;
|
2016-05-31 17:29:54 +01:00
|
|
|
|
my $nixosPath;
|
2014-03-31 18:21:36 +01:00
|
|
|
|
my $ensureUniqueName = 0;
|
2014-08-18 14:56:27 +01:00
|
|
|
|
my $autoStart = 0;
|
2016-11-27 10:37:50 +00:00
|
|
|
|
my $bridge;
|
2014-06-05 17:38:38 +01:00
|
|
|
|
my $extraConfig;
|
2016-07-20 19:43:34 +01:00
|
|
|
|
my $signal;
|
2016-06-24 15:02:26 +01:00
|
|
|
|
my $configFile;
|
2014-03-31 18:21:36 +01:00
|
|
|
|
|
|
|
|
|
GetOptions(
|
|
|
|
|
"help" => sub { showHelp() },
|
|
|
|
|
"ensure-unique-name" => \$ensureUniqueName,
|
2014-08-18 14:56:27 +01:00
|
|
|
|
"auto-start" => \$autoStart,
|
2016-11-27 10:37:50 +00:00
|
|
|
|
"bridge=s" => \$bridge,
|
2014-08-19 14:47:51 +01:00
|
|
|
|
"system-path=s" => \$systemPath,
|
2016-08-07 13:28:23 +01:00
|
|
|
|
"signal=s" => \$signal,
|
2016-05-31 17:29:54 +01:00
|
|
|
|
"nixos-path=s" => \$nixosPath,
|
2016-06-24 15:02:26 +01:00
|
|
|
|
"config=s" => \$extraConfig,
|
|
|
|
|
"config-file=s" => \$configFile
|
2014-03-31 18:21:36 +01:00
|
|
|
|
) or exit 1;
|
|
|
|
|
|
|
|
|
|
my $action = $ARGV[0] or die "$0: no action specified\n";
|
|
|
|
|
|
2016-06-24 15:02:26 +01:00
|
|
|
|
if (defined $configFile and defined $extraConfig) {
|
|
|
|
|
die "--config and --config-file are mutually incompatible. " .
|
|
|
|
|
"Please define on or the other, but not both";
|
|
|
|
|
}
|
2014-03-31 18:21:36 +01:00
|
|
|
|
|
|
|
|
|
# Execute the selected action.
|
|
|
|
|
|
|
|
|
|
mkpath("/etc/containers", 0, 0755);
|
|
|
|
|
mkpath("/var/lib/containers", 0, 0700);
|
|
|
|
|
|
|
|
|
|
if ($action eq "list") {
|
|
|
|
|
foreach my $confFile (glob "/etc/containers/*.conf") {
|
|
|
|
|
$confFile =~ /\/([^\/]+).conf$/ or next;
|
|
|
|
|
print "$1\n";
|
|
|
|
|
}
|
|
|
|
|
exit 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
my $containerName = $ARGV[1] or die "$0: no container name specified\n";
|
|
|
|
|
$containerName =~ /^[a-zA-Z0-9\-]+$/ or die "$0: invalid container name\n";
|
|
|
|
|
|
2014-04-10 10:32:50 +01:00
|
|
|
|
sub writeNixOSConfig {
|
|
|
|
|
my ($nixosConfigFile) = @_;
|
|
|
|
|
|
2016-06-24 15:02:26 +01:00
|
|
|
|
my $localExtraConfig = "";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if ($extraConfig) {
|
|
|
|
|
$localExtraConfig = $extraConfig
|
|
|
|
|
} elsif ($configFile) {
|
|
|
|
|
my $resolvedFile = abs_path($configFile);
|
|
|
|
|
$localExtraConfig = "imports = [ $resolvedFile ];"
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-10 10:32:50 +01:00
|
|
|
|
my $nixosConfig = <<EOF;
|
2014-04-14 15:26:48 +01:00
|
|
|
|
{ config, lib, pkgs, ... }:
|
2014-04-10 10:32:50 +01:00
|
|
|
|
|
2014-04-14 15:26:48 +01:00
|
|
|
|
with lib;
|
2014-04-10 10:32:50 +01:00
|
|
|
|
|
|
|
|
|
{ boot.isContainer = true;
|
|
|
|
|
networking.hostName = mkDefault "$containerName";
|
|
|
|
|
networking.useDHCP = false;
|
2016-06-24 15:02:26 +01:00
|
|
|
|
$localExtraConfig
|
2014-04-10 10:32:50 +01:00
|
|
|
|
}
|
|
|
|
|
EOF
|
|
|
|
|
|
|
|
|
|
write_file($nixosConfigFile, $nixosConfig);
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-31 18:21:36 +01:00
|
|
|
|
if ($action eq "create") {
|
|
|
|
|
# Acquire an exclusive lock to prevent races with other
|
|
|
|
|
# invocations of ‘nixos-container create’.
|
|
|
|
|
my $lockFN = "/run/lock/nixos-container";
|
|
|
|
|
open(my $lock, '>>', $lockFN) or die "$0: opening $lockFN: $!";
|
|
|
|
|
flock($lock, LOCK_EX) or die "$0: could not lock $lockFN: $!";
|
|
|
|
|
|
|
|
|
|
my $confFile = "/etc/containers/$containerName.conf";
|
|
|
|
|
my $root = "/var/lib/containers/$containerName";
|
|
|
|
|
|
|
|
|
|
# Maybe generate a unique name.
|
|
|
|
|
if ($ensureUniqueName) {
|
|
|
|
|
my $base = $containerName;
|
|
|
|
|
for (my $nr = 0; ; $nr++) {
|
|
|
|
|
$confFile = "/etc/containers/$containerName.conf";
|
|
|
|
|
$root = "/var/lib/containers/$containerName";
|
|
|
|
|
last unless -e $confFile || -e $root;
|
2015-11-18 17:47:25 +00:00
|
|
|
|
$containerName = "$base-$nr";
|
2014-03-31 18:21:36 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
die "$0: container ‘$containerName’ already exists\n" if -e $confFile;
|
|
|
|
|
|
2015-01-28 15:12:05 +00:00
|
|
|
|
# Due to interface name length restrictions, container names must
|
|
|
|
|
# be restricted too.
|
|
|
|
|
die "$0: container name ‘$containerName’ is too long\n" if length $containerName > 11;
|
|
|
|
|
|
2014-03-31 18:21:36 +01:00
|
|
|
|
# Get an unused IP address.
|
|
|
|
|
my %usedIPs;
|
|
|
|
|
foreach my $confFile2 (glob "/etc/containers/*.conf") {
|
|
|
|
|
my $s = read_file($confFile2) or die;
|
|
|
|
|
$usedIPs{$1} = 1 if $s =~ /^HOST_ADDRESS=([0-9\.]+)$/m;
|
|
|
|
|
$usedIPs{$1} = 1 if $s =~ /^LOCAL_ADDRESS=([0-9\.]+)$/m;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
my ($ipPrefix, $hostAddress, $localAddress);
|
|
|
|
|
for (my $nr = 1; $nr < 255; $nr++) {
|
|
|
|
|
$ipPrefix = "10.233.$nr";
|
|
|
|
|
$hostAddress = "$ipPrefix.1";
|
|
|
|
|
$localAddress = "$ipPrefix.2";
|
|
|
|
|
last unless $usedIPs{$hostAddress} || $usedIPs{$localAddress};
|
|
|
|
|
$ipPrefix = undef;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
die "$0: out of IP addresses\n" unless defined $ipPrefix;
|
|
|
|
|
|
|
|
|
|
my @conf;
|
|
|
|
|
push @conf, "PRIVATE_NETWORK=1\n";
|
|
|
|
|
push @conf, "HOST_ADDRESS=$hostAddress\n";
|
|
|
|
|
push @conf, "LOCAL_ADDRESS=$localAddress\n";
|
2016-11-27 10:37:50 +00:00
|
|
|
|
push @conf, "HOST_BRIDGE=$bridge\n";
|
2014-08-18 14:56:27 +01:00
|
|
|
|
push @conf, "AUTO_START=$autoStart\n";
|
2014-03-31 18:21:36 +01:00
|
|
|
|
write_file($confFile, \@conf);
|
|
|
|
|
|
|
|
|
|
close($lock);
|
|
|
|
|
|
|
|
|
|
print STDERR "host IP is $hostAddress, container IP is $localAddress\n";
|
|
|
|
|
|
|
|
|
|
# The per-container directory is restricted to prevent users on
|
|
|
|
|
# the host from messing with guest users who happen to have the
|
|
|
|
|
# same uid.
|
|
|
|
|
my $profileDir = "/nix/var/nix/profiles/per-container";
|
|
|
|
|
mkpath($profileDir, 0, 0700);
|
|
|
|
|
$profileDir = "$profileDir/$containerName";
|
|
|
|
|
mkpath($profileDir, 0, 0755);
|
|
|
|
|
|
2014-08-19 14:47:51 +01:00
|
|
|
|
# Build/set the initial configuration.
|
|
|
|
|
if (defined $systemPath) {
|
|
|
|
|
system("nix-env", "-p", "$profileDir/system", "--set", $systemPath) == 0
|
|
|
|
|
or die "$0: failed to set initial container configuration\n";
|
|
|
|
|
} else {
|
|
|
|
|
mkpath("$root/etc/nixos", 0, 0755);
|
|
|
|
|
|
2016-05-31 17:29:54 +01:00
|
|
|
|
my $nixenvF = $nixosPath // "<nixpkgs/nixos>";
|
2014-08-19 14:47:51 +01:00
|
|
|
|
my $nixosConfigFile = "$root/etc/nixos/configuration.nix";
|
|
|
|
|
writeNixOSConfig $nixosConfigFile;
|
|
|
|
|
|
|
|
|
|
system("nix-env", "-p", "$profileDir/system",
|
2016-05-31 17:29:54 +01:00
|
|
|
|
"-I", "nixos-config=$nixosConfigFile", "-f", "$nixenvF",
|
2014-08-19 14:47:51 +01:00
|
|
|
|
"--set", "-A", "system") == 0
|
|
|
|
|
or die "$0: failed to build initial container configuration\n";
|
|
|
|
|
}
|
2014-03-31 18:21:36 +01:00
|
|
|
|
|
|
|
|
|
print "$containerName\n" if $ensureUniqueName;
|
|
|
|
|
exit 0;
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-10 10:32:50 +01:00
|
|
|
|
my $root = "/var/lib/containers/$containerName";
|
|
|
|
|
my $profileDir = "/nix/var/nix/profiles/per-container/$containerName";
|
2014-08-15 00:19:17 +01:00
|
|
|
|
my $gcRootsDir = "/nix/var/nix/gcroots/per-container/$containerName";
|
2014-03-31 18:21:36 +01:00
|
|
|
|
my $confFile = "/etc/containers/$containerName.conf";
|
2014-08-15 02:35:55 +01:00
|
|
|
|
if (!-e $confFile) {
|
2014-08-18 12:46:54 +01:00
|
|
|
|
if ($action eq "destroy") {
|
|
|
|
|
exit 0;
|
2014-08-19 15:57:02 +01:00
|
|
|
|
} elsif ($action eq "status") {
|
2014-08-18 12:46:54 +01:00
|
|
|
|
print "gone\n";
|
|
|
|
|
}
|
2014-08-15 02:35:55 +01:00
|
|
|
|
die "$0: container ‘$containerName’ does not exist\n" ;
|
|
|
|
|
}
|
2014-03-31 18:21:36 +01:00
|
|
|
|
|
2014-04-10 10:32:50 +01:00
|
|
|
|
sub isContainerRunning {
|
|
|
|
|
my $status = `systemctl show 'container\@$containerName'`;
|
|
|
|
|
return $status =~ /ActiveState=active/;
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-03 17:54:19 +01:00
|
|
|
|
sub terminateContainer {
|
|
|
|
|
system("machinectl", "terminate", $containerName) == 0
|
|
|
|
|
or die "$0: failed to terminate container\n";
|
2016-07-20 19:43:34 +01:00
|
|
|
|
}
|
|
|
|
|
|
2014-03-31 18:21:36 +01:00
|
|
|
|
sub stopContainer {
|
|
|
|
|
system("systemctl", "stop", "container\@$containerName") == 0
|
|
|
|
|
or die "$0: failed to stop container\n";
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-11 17:03:45 +01:00
|
|
|
|
# Remove a directory while recursively unmounting all mounted filesystems within
|
|
|
|
|
# that directory and unmounting/removing that directory afterwards as well.
|
|
|
|
|
#
|
|
|
|
|
# NOTE: If the specified path is a mountpoint, its contents will be removed,
|
|
|
|
|
# only mountpoints underneath that path will be unmounted properly.
|
|
|
|
|
sub safeRemoveTree {
|
|
|
|
|
my ($path) = @_;
|
|
|
|
|
system("find", $path, "-mindepth", "1", "-xdev",
|
|
|
|
|
"(", "-type", "d", "-exec", "mountpoint", "-q", "{}", ";", ")",
|
|
|
|
|
"-exec", "umount", "-fR", "{}", "+");
|
|
|
|
|
system("rm", "--one-file-system", "-rf", $path);
|
|
|
|
|
if (-e $path) {
|
|
|
|
|
system("umount", "-fR", $path);
|
|
|
|
|
system("rm", "--one-file-system", "-rf", $path);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-31 18:21:36 +01:00
|
|
|
|
if ($action eq "destroy") {
|
2014-04-03 15:25:21 +01:00
|
|
|
|
die "$0: cannot destroy declarative container (remove it from your configuration.nix instead)\n"
|
|
|
|
|
unless POSIX::access($confFile, &POSIX::W_OK);
|
|
|
|
|
|
2016-08-03 17:54:19 +01:00
|
|
|
|
terminateContainer if (isContainerRunning);
|
2014-03-31 18:21:36 +01:00
|
|
|
|
|
2014-09-11 17:03:45 +01:00
|
|
|
|
safeRemoveTree($profileDir) if -e $profileDir;
|
|
|
|
|
safeRemoveTree($gcRootsDir) if -e $gcRootsDir;
|
|
|
|
|
safeRemoveTree($root) if -e $root;
|
2014-03-31 18:21:36 +01:00
|
|
|
|
unlink($confFile) or die;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
elsif ($action eq "start") {
|
|
|
|
|
system("systemctl", "start", "container\@$containerName") == 0
|
|
|
|
|
or die "$0: failed to start container\n";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
elsif ($action eq "stop") {
|
|
|
|
|
stopContainer;
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-03 17:54:19 +01:00
|
|
|
|
elsif ($action eq "terminate") {
|
|
|
|
|
terminateContainer;
|
2016-07-20 19:43:34 +01:00
|
|
|
|
}
|
|
|
|
|
|
2014-08-18 12:46:54 +01:00
|
|
|
|
elsif ($action eq "status") {
|
|
|
|
|
print isContainerRunning() ? "up" : "down", "\n";
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-10 10:32:50 +01:00
|
|
|
|
elsif ($action eq "update") {
|
|
|
|
|
my $nixosConfigFile = "$root/etc/nixos/configuration.nix";
|
|
|
|
|
|
|
|
|
|
# FIXME: may want to be more careful about clobbering the existing
|
|
|
|
|
# configuration.nix.
|
2016-06-24 15:02:26 +01:00
|
|
|
|
if ((defined $extraConfig && $extraConfig ne "") ||
|
|
|
|
|
(defined $configFile && $configFile ne "")) {
|
|
|
|
|
writeNixOSConfig $nixosConfigFile;
|
|
|
|
|
}
|
2014-04-10 10:32:50 +01:00
|
|
|
|
|
|
|
|
|
system("nix-env", "-p", "$profileDir/system",
|
2016-06-24 15:02:26 +01:00
|
|
|
|
"-I", "nixos-config=$nixosConfigFile", "-f", "<nixpkgs/nixos>",
|
2014-04-10 10:32:50 +01:00
|
|
|
|
"--set", "-A", "system") == 0
|
|
|
|
|
or die "$0: failed to build container configuration\n";
|
|
|
|
|
|
|
|
|
|
if (isContainerRunning) {
|
|
|
|
|
print STDERR "reloading container...\n";
|
|
|
|
|
system("systemctl", "reload", "container\@$containerName") == 0
|
|
|
|
|
or die "$0: failed to reload container\n";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-31 18:21:36 +01:00
|
|
|
|
elsif ($action eq "login") {
|
2014-04-18 19:47:31 +01:00
|
|
|
|
exec("machinectl", "login", "--", $containerName);
|
2014-03-31 18:21:36 +01:00
|
|
|
|
}
|
|
|
|
|
|
2014-04-10 12:12:34 +01:00
|
|
|
|
elsif ($action eq "root-login") {
|
2016-09-23 23:40:41 +01:00
|
|
|
|
exec("machinectl", "shell", $containerName, "/bin/sh", "-l");
|
2014-04-10 12:12:34 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
elsif ($action eq "run") {
|
|
|
|
|
shift @ARGV; shift @ARGV;
|
2014-08-27 20:12:18 +01:00
|
|
|
|
# Escape command.
|
|
|
|
|
my $s = join(' ', map { s/'/'\\''/g; "'$_'" } @ARGV);
|
2016-09-23 23:40:41 +01:00
|
|
|
|
exec("machinectl", "--quiet", "shell", $containerName, "/bin/sh", "-l", "-c", $s);
|
2014-03-31 18:21:36 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
elsif ($action eq "show-ip") {
|
|
|
|
|
my $s = read_file($confFile) or die;
|
|
|
|
|
$s =~ /^LOCAL_ADDRESS=([0-9\.]+)$/m or die "$0: cannot get IP address\n";
|
|
|
|
|
print "$1\n";
|
|
|
|
|
}
|
|
|
|
|
|
2014-08-19 15:57:02 +01:00
|
|
|
|
elsif ($action eq "show-host-key") {
|
2015-09-08 14:57:04 +01:00
|
|
|
|
my $fn = "$root/etc/ssh/ssh_host_ed25519_key.pub";
|
|
|
|
|
$fn = "$root/etc/ssh/ssh_host_ecdsa_key.pub" unless -e $fn;
|
2014-08-19 15:57:02 +01:00
|
|
|
|
exit 1 if ! -f $fn;
|
|
|
|
|
print read_file($fn);
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-31 18:21:36 +01:00
|
|
|
|
else {
|
|
|
|
|
die "$0: unknown action ‘$action’\n";
|
|
|
|
|
}
|