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 --------------------------- diff --git a/perl/lib/NeedRestart/CONT/LXC.pm b/perl/lib/NeedRestart/CONT/LXC.pm index c3b091d..283d628 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; } @@ -72,10 +81,11 @@ 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(/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}); @@ -91,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 { - ($_ => [qw(lxc restart), $_]); + ($_ => lxd_restart_with_project($self->{lxd_bin}, $_)); } keys %{ $self->{lxd} }); }