From 63cf8b28eb9083d2ef33dd3fa631e34415f5c342 Mon Sep 17 00:00:00 2001 From: James TD Smith Date: Tue, 22 Oct 2019 15:15:03 +0100 Subject: [PATCH 1/4] Support LXD installed through snap This is the recommended way to install LXD and puts the container dirs in /var/snap/lxd/common/lxd/containers instead of /var/lib/lxd/containers. --- perl/lib/NeedRestart/CONT/LXC.pm | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/perl/lib/NeedRestart/CONT/LXC.pm b/perl/lib/NeedRestart/CONT/LXC.pm index c3b091d..2a7dd6c 100644 --- a/perl/lib/NeedRestart/CONT/LXC.pm +++ b/perl/lib/NeedRestart/CONT/LXC.pm @@ -45,7 +45,16 @@ sub new { $self->{lxc} = {}; $self->{lxd} = {}; - $self->{has_lxd} = -x q(/usr/bin/lxc); + if (-d q(/snap/lxd)) { + $self->{has_lxd} = 1; + $self->{lxd_bin} = q(/snap/bin/lxc); + $self->{lxd_container_path} = q(/var/snap/lxd/common/lxd/containers); + print STDERR "$LOGPREF LXD installed via snap\n" if($self->{debug}); + } else { + $self->{has_lxd} = -x q(/usr/bin/lxc); + $self->{lxd_bin} = q(/usr/bin/lxc); + $self->{lxd_container_path} = q(/var/lib/lxd/containers); + } return bless $self, $class; } @@ -75,7 +84,8 @@ sub check { return unless($cg =~ /^\d+:[^:]+:\/lxc\/([^\/\n]+)($|\/)/m); my $name = $1; - my $type = ($self->{has_lxd} && -d qq(/var/lib/lxd/containers/$name) ? 'LXD' : 'LXC'); + my $type = ($self->{has_lxd} && -d qq($self->{lxd_container_path}/$name) ? 'LXD' : 'LXC'); + unless($norestart) { print STDERR "$LOGPREF #$pid is part of $type container '$name' and should be restarted\n" if($self->{debug}); @@ -94,7 +104,7 @@ sub get { return (map { ($_ => [qw(lxc-stop --reboot --name), $_]); } keys %{ $self->{lxc} }), (map { - ($_ => [qw(lxc restart), $_]); + ($_ => [ $self->{lxd_bin}, "restart", $_]); } keys %{ $self->{lxd} }); } From 1f1bd6d4c539ec70ecfdb7efe69c37386475a71e Mon Sep 17 00:00:00 2001 From: James TD Smith Date: Tue, 22 Oct 2019 15:16:32 +0100 Subject: [PATCH 2/4] Handle differences in cgroup names for LXD containers LXC containers on newer versions of LXC are under the lxc.payload cgroup, update the cgroup regex to match this too. --- perl/lib/NeedRestart/CONT/LXC.pm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/perl/lib/NeedRestart/CONT/LXC.pm b/perl/lib/NeedRestart/CONT/LXC.pm index 2a7dd6c..53a4dda 100644 --- a/perl/lib/NeedRestart/CONT/LXC.pm +++ b/perl/lib/NeedRestart/CONT/LXC.pm @@ -81,7 +81,7 @@ sub check { } # look for LXC cgroups - return unless($cg =~ /^\d+:[^:]+:\/lxc\/([^\/\n]+)($|\/)/m); + return unless($cg =~ /^\d+:[^:]+:\/lxc(?:.payload)?\/([^\/\n]+)($|\/)/m); my $name = $1; my $type = ($self->{has_lxd} && -d qq($self->{lxd_container_path}/$name) ? 'LXD' : 'LXC'); From 569d8d5fda5a4723a94be6b474eccd6c24ec5474 Mon Sep 17 00:00:00 2001 From: James TD Smith Date: Tue, 22 Oct 2019 15:36:17 +0100 Subject: [PATCH 3/4] Fix restarting for containers in LXD projects Check if containers are in a project and add the appropriate parameter to the restart command. --- perl/lib/NeedRestart/CONT/LXC.pm | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/perl/lib/NeedRestart/CONT/LXC.pm b/perl/lib/NeedRestart/CONT/LXC.pm index 53a4dda..283d628 100644 --- a/perl/lib/NeedRestart/CONT/LXC.pm +++ b/perl/lib/NeedRestart/CONT/LXC.pm @@ -101,10 +101,20 @@ sub check { sub get { my $self = shift; + sub lxd_restart_with_project { + my ($bin, $container) = @_; + my @parts = split(/_/, $container); + if ($#parts == 1) { + return [ $bin, 'restart', qq(--project=$parts[0]), $parts[1] ]; + } else { + [ $bin, 'restart', $container ] + } + } + return (map { ($_ => [qw(lxc-stop --reboot --name), $_]); } keys %{ $self->{lxc} }), (map { - ($_ => [ $self->{lxd_bin}, "restart", $_]); + ($_ => lxd_restart_with_project($self->{lxd_bin}, $_)); } keys %{ $self->{lxd} }); } From 58d0d1609ac0c8e0588850ec416727f1365b438e Mon Sep 17 00:00:00 2001 From: James TD Smith Date: Tue, 22 Oct 2019 15:47:09 +0100 Subject: [PATCH 4/4] Update documentation for LXC/LXD support --- README.Cont.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.Cont.md b/README.Cont.md index e2c06ec..dbc39df 100644 --- a/README.Cont.md +++ b/README.Cont.md @@ -21,11 +21,13 @@ within docker containers by design. NeedRestart::CONT::LXC ---------------------- -Recognized by: cgroup path (`/lxc/*`) +Recognized by: cgroup path (`/lxc/*` || `/lxc.payload/*`) For each container which should be restarted needrestart calls `lxc-stop --reboot --name $NAME`. +This package also supports LXD containers, which are restarted by `lxc restart +$NAME` or `lxc restart --project=$PROJECT $NAME` for containers in projects. NeedRestart::CONT::machined ---------------------------