fix(ec2): remove the volume helper container on shutdown (#5551)

* fix(ec2): remove the volume helper container on shutdown

The privileged helper that manages loop devices for EBS volumes was
started on the first volume operation and never stopped or removed, so
it kept running after Floci exited and stayed in the container list.

Ec2VolumeBlockDeviceManager now implements ContainerTeardown. On
shutdown and on a state reset it looks the helper up by name, detaches
the loop devices behind the backing files and removes the container. It
never creates a helper only to remove it, and it also collects one left
behind by an earlier process. Backing files stay in storage and the next
volume operation recreates the helper.

Signed-off-by: Hector Ventura <hectorvent@gmail.com>

* fix(ec2): leave no volume helper after a reset and report failed loop detaches

A state reset removed the helper in the container teardown and then
started it again to delete the backing files, so the reset ended with a
running helper. clear() now removes the helper once the files are gone.

The script that releases loop devices before the helper is removed
discarded every losetup failure, so the warning that follows could only
fire when the exec itself failed. It now records each loop device that
would not detach and exits non-zero, while still trying every device and
still removing the helper.

Signed-off-by: Hector Ventura <hectorvent@gmail.com>

---------

Signed-off-by: Hector Ventura <hectorvent@gmail.com>
This commit is contained in:
Hector Ventura authored and GitHub committed 2026-10-12 23:36:15 -04:00
1 parent 8a87e062c8
commit ec5692dfa2
4 files changed
+180 -4

No files matched your search

@@ -4,6 +4,7 @@ import com.github.dockerjava.api.DockerClient;
import com.github.dockerjava.api.command.InspectContainerResponse;
import com.github.dockerjava.api.exception.NotFoundException;
import io.github.hectorvent.floci.config.EmulatorConfig;
import io.github.hectorvent.floci.core.common.ContainerTeardown;
import io.github.hectorvent.floci.core.common.Resettable;
import io.github.hectorvent.floci.core.common.docker.ContainerBuilder;
import io.github.hectorvent.floci.core.common.docker.ContainerExec;
@@ -39,7 +40,7 @@ import java.util.regex.Pattern;
* created inside the target container at the requested path.
*/
@ApplicationScoped
public class Ec2VolumeBlockDeviceManager implements Resettable {
public class Ec2VolumeBlockDeviceManager implements ContainerTeardown, Resettable {
private static final Logger LOG = Logger.getLogger(Ec2VolumeBlockDeviceManager.class);
private static final int DEFAULT_TIMEOUT_SECONDS = 30;
@@ -462,6 +463,52 @@ public class Ec2VolumeBlockDeviceManager implements Resettable {
}
}
/**
* Removes the helper container on shutdown and on a state reset. The helper is looked up by
* name and never created here, so one left behind by an earlier process is collected too.
* Backing files stay in storage, and the next volume operation recreates the helper.
*/
@Override
public synchronized void stopManagedContainers() {
if (!isAvailable()) {
return;
}
String helperName = ContainerStorageHelper.resourceName(config, "ec2", null, "volume-helper");
InspectContainerResponse helper;
try {
helper = dockerClient.inspectContainerCmd(helperName).exec();
} catch (NotFoundException e) {
LOG.debugv("No EC2 volume helper container {0} to remove", helperName);
return;
} catch (Exception e) {
LOG.warnv("Failed to inspect EC2 volume helper container {0}: {1}", helperName, e.getMessage());
return;
}
if (Boolean.TRUE.equals(helper.getState().getRunning())) {
// One pass per file: a loop device still held open by an instance container is
// detached lazily and stays listed, so retrying it until it disappears would spin.
// Every loop is tried, and one that would not detach fails the script so it is reported.
String script = SHELL_LOOP_FINDER
+ "failed=0\n"
+ "for f in /volumes/*.raw; do\n"
+ " [ -f \"$f\" ] || continue\n"
+ " for loop in $(find_loops \"$f\"); do\n"
+ " losetup -d \"$loop\" 2>/dev/null || { echo \"could not detach $loop from $f\"; failed=1; }\n"
+ " done\n"
+ "done\n"
+ "exit $failed";
ContainerExec.Result released = execInContainer(helper.getId(),
new String[]{"sh", "-c", script}, DEFAULT_TIMEOUT_SECONDS);
if (released.exitCode() != 0) {
LOG.warnv("Failed to release loop devices before removing EC2 volume helper container {0}: {1}",
helperName, released.summary());
}
}
// The helper only sleeps and would ignore SIGTERM as PID 1, so do not wait on a graceful stop.
lifecycleManager.stopAndRemove(helper.getId(), null, 0);
activeLoopDevices.clear();
}
private ContainerExec.Result execInContainer(String containerId, String[] cmd, int timeoutSeconds) {
try {
return ContainerExec.runMerged(dockerClient, containerId, cmd, timeoutSeconds);
@@ -489,6 +536,9 @@ public class Ec2VolumeBlockDeviceManager implements Resettable {
+ " rm -f \"$f\"\n"
+ "done";
execInContainer(helperId, new String[]{"sh", "-c", script}, DEFAULT_TIMEOUT_SECONDS);
// The helper was only needed to delete the files, so a reset ends as a shutdown does:
// without one.
lifecycleManager.stopAndRemove(helperId, null, 0);
}
activeLoopDevices.clear();
}