Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes for LXC / LXD container support #188

Merged
merged 4 commits into from
Sep 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.Cont.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
---------------------------
Expand Down
28 changes: 24 additions & 4 deletions perl/lib/NeedRestart/CONT/LXC.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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});

Expand All @@ -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} });
}

Expand Down