diff options
author | Scott R. Parish <srparish@gmail.com> | 2016-07-20 11:43:34 -0700 |
---|---|---|
committer | Scott R. Parish <srparish@gmail.com> | 2016-07-21 14:58:05 -0700 |
commit | 60c6c7bc9a0d564cf86af4b1711b33db48cf0d07 (patch) | |
tree | 76bbf5c44b6468bcea9c97c7a14b154becf89e26 /pkgs/tools/virtualization/nixos-container | |
parent | 659a3bc89bb4b54b9a47c5a0a241ed2835c35c6b (diff) |
nixos-container: add 'kill' command, 'destroy' to use 'kill'
Using 'machinectl kill' is much faster then gracefully stopping the
container.
In the case of 'destroy', since we're destroying it anyway, there's no
reason to do a graceful shutdown.
Diffstat (limited to 'pkgs/tools/virtualization/nixos-container')
-rwxr-xr-x | pkgs/tools/virtualization/nixos-container/nixos-container.pl | 20 |
1 files changed, 18 insertions, 2 deletions
diff --git a/pkgs/tools/virtualization/nixos-container/nixos-container.pl b/pkgs/tools/virtualization/nixos-container/nixos-container.pl index eda57a9751eb..a4b6f11654e8 100755 --- a/pkgs/tools/virtualization/nixos-container/nixos-container.pl +++ b/pkgs/tools/virtualization/nixos-container/nixos-container.pl @@ -22,6 +22,7 @@ Usage: nixos-container list nixos-container destroy <container-name> nixos-container start <container-name> nixos-container stop <container-name> + nixos-container kill <container-name> [--signal <signal-specifier>] nixos-container status <container-name> nixos-container update <container-name> [--config <string>] nixos-container login <container-name> @@ -37,13 +38,15 @@ my $systemPath; my $ensureUniqueName = 0; my $autoStart = 0; my $extraConfig; +my $signal; GetOptions( "help" => sub { showHelp() }, "ensure-unique-name" => \$ensureUniqueName, "auto-start" => \$autoStart, "system-path=s" => \$systemPath, - "config=s" => \$extraConfig + "config=s" => \$extraConfig, + "signal=s" => \$signal ) or exit 1; my $action = $ARGV[0] or die "$0: no action specified\n"; @@ -186,6 +189,14 @@ sub isContainerRunning { return $status =~ /ActiveState=active/; } +sub killContainer { + my @args = (); + push(@args, ("--signal", $signal)) if ($signal ne ""); + + system("machinectl", "kill", $containerName, @args) == 0 + or die "$0: failed to kill container\n"; +} + sub stopContainer { system("systemctl", "stop", "container\@$containerName") == 0 or die "$0: failed to stop container\n"; @@ -228,7 +239,8 @@ if ($action eq "destroy") { die "$0: cannot destroy declarative container (remove it from your configuration.nix instead)\n" unless POSIX::access($confFile, &POSIX::W_OK); - stopContainer if isContainerRunning; + $signal = "SIGKILL"; + killContainer if (isContainerRunning); safeRemoveTree($profileDir) if -e $profileDir; safeRemoveTree($gcRootsDir) if -e $gcRootsDir; @@ -245,6 +257,10 @@ elsif ($action eq "stop") { stopContainer; } +elsif ($action eq "kill") { + killContainer; +} + elsif ($action eq "status") { print isContainerRunning() ? "up" : "down", "\n"; } |