Skip to content

armgit / floci

Public Imported from GitHub
0

Light, fluffy, and always free - The AWS Local Emulator alternative

feat(ec2): resize volume backing files and loop devices on ModifyVolume (#4666)

Simone Primarosad1a9b50cf5bf
diff --git a/docs/services/ec2.md b/docs/services/ec2.md
index b8c75ee84c..c1f1513909 100644
--- a/docs/services/ec2.md
+++ b/docs/services/ec2.md
@@ -704,7 +704,7 @@ When `floci.services.ec2.volume-block-devices` is enabled (the default), attache
- **Restart reconciliation**: When an instance container stops and starts, or when Floci restarts, attached volume device nodes are automatically restored inside the target container. Backing raw files remain preserved across reboots in persistent storage.
- **Graceful degradation**: If Docker is unavailable, the target instance container is not running, or the container is not privileged, volume attachments degrade gracefully to metadata-only tracking without failing the API call.
- **Platform requirements**: Requires a Linux Docker environment (native Linux Docker daemon, or Colima / Docker Desktop with a Linux virtual machine) and privileged instance containers.
-- **Unmodeled aspects**: Multi-attach is tracked at metadata level only. Automated filesystem formatting (volumes start unformatted like real EBS block devices), volume encryption at the block layer, and live resizing of underlying raw backing files are not modeled. `ModifyVolume` updates recorded metadata and reports completion without resizing the backing raw image.
+- **Unmodeled aspects**: Multi-attach is tracked at metadata level only. Automated filesystem formatting (volumes start unformatted like real EBS block devices) and volume encryption at the block layer are not modeled. Root volume backing is logical (container rootfs); `ModifyVolume` updates recorded root volume metadata without modifying container storage, while EBS data volume size increases expand the underlying raw backing file and refresh any attached loop device. Volume type, IOPS, and throughput changes remain recorded metadata without altering device characteristics.
Validation matches AWS behavior:
- Unknown volumes are rejected with `InvalidVolume.NotFound`.
diff --git a/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2Service.java b/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2Service.java
index 8b4dedd4b9..fa266716bf 100644
--- a/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2Service.java
+++ b/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2Service.java
@@ -119,8 +119,11 @@ import java.util.Set;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
+import java.util.concurrent.atomic.AtomicInteger;
+import java.util.concurrent.locks.ReentrantLock;
import java.util.function.BiPredicate;
import java.util.function.Function;
+import java.util.function.Supplier;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@@ -316,6 +319,18 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider {
instances.put(key(instance.getRegion(), instance.getInstanceId()), instance);
}
+ void deleteInstanceForTest(String region, String instanceId) {
+ instances.delete(key(region, instanceId));
+ }
+
+ void putVolumeForTest(Volume volume) {
+ volumes.put(key(volume.getRegion(), volume.getVolumeId()), volume);
+ }
+
+ void deleteVolumeForTest(String region, String volumeId) {
+ volumes.delete(key(region, volumeId));
+ }
+
// Public, no request context - for callers (and tests) that construct this service directly
// without CDI. Caller-identity resolution falls back to the configured default account.
public Ec2Service(EmulatorConfig config, Ec2ContainerManager containerManager,
@@ -8843,122 +8858,222 @@ public class Ec2Service implements ContainerTeardown, ResourceProvider {
}
public void deleteVolume(String region, String volumeId) {
- Volume volume = volumes.get(key(region, volumeId)).orElseThrow(() ->
- new AwsException("InvalidVolume.NotFound",
- "The volume '" + volumeId + "' does not exist.", 400));
- if ("in-use".equals(volume.getState()) || !volume.getAttachments().isEmpty()) {
- throw new AwsException("VolumeInUse",
- "Volume " + volumeId + " is currently attached to an instance", 400);
+ if (volumeId == null || volumeId.isBlank()) {
+ throw new AwsException("MissingParameter", "The parameter VolumeId is missing", 400);
}
- if (volumeBlockDeviceManager != null && volumeBlockDeviceManager.isAvailable()) {
- if (!volumeBlockDeviceManager.deleteVolume(volumeId)) {
- throw new AwsException("InternalError",
- "Failed to delete backing storage for volume " + volumeId, 500);
+ withVolumeLock(volumeId, () -> {
+ ensureDefaultResources(region);
+ Volume volume = volumes.get(key(region, volumeId)).orElseThrow(() ->
+ new AwsException("InvalidVolume.NotFound",
+ "The volume '" + volumeId + "' does not exist.", 400));
+ if ("in-use".equals(volume.getState()) || !volume.getAttachments().isEmpty()) {
+ throw new AwsException("VolumeInUse",
+ "Volume " + volumeId + " is currently attached to an instance", 400);
+ }
+ if (volumeBlockDeviceManager != null && volumeBlockDeviceManager.isAvailable()) {
+ if (!volumeBlockDeviceManager.deleteVolume(volumeId)) {
+ throw new AwsException("InternalError",
+ "Failed to delete backing storage for volume " + volumeId, 500);
+ }
}
- }
- volumes.delete(key(region, volumeId));
+ volumes.delete(key(region, volumeId));
+ });
}
- public VolumeModification modifyVolume(String region, String volumeId, Integer size,
- String volumeType, Integer iops, Integer throughput,
- Boolean multiAttachEnabled, boolean dryRun) {
- if (volumeId == null || volumeId.isBlank()) {
- throw new AwsException("MissingParameter", "The parameter VolumeId is missing", 400);
- }
- ensureDefaultResources(region);
- Volume volume = getRequiredVolume(region, volumeId);
+ private static final class RefCountedLock {
+ private final ReentrantLock lock = new ReentrantLock();
+ private final AtomicInteger refCount = new AtomicInteger(1);
+ }
- if (size != null && size <= 0) {
- throw new AwsException("InvalidParameterValue",
- "Value (" + size + ") for parameter size is invalid.", 400);
+ private final ConcurrentHashMap<String, RefCountedLock> volumeLocks = new ConcurrentHashMap<>();
+
+ private <T> T withVolumeLock(String volumeId, Supplier<T> action) {
+ if (volumeId == null) {
+ return action.get();
}
- if (size != null && size < volume.getSize()) {
- throw new AwsException("InvalidParameterValue",
- "New size cannot be smaller than existing size", 400);
+ RefCountedLock refLock = volumeLocks.compute(volumeId, (k, v) -> {
+ if (v == null) {
+ return new RefCountedLock();
+ }
+ v.refCount.incrementAndGet();
+ return v;
+ });
+ refLock.lock.lock();
+ try {
+ return action.get();
+ } finally {
+ refLock.lock.unlock();
+ volumeLocks.computeIfPresent(volumeId, (k, v) -> {
+ if (v.refCount.decrementAndGet() <= 0) {
+ return null;
+ }
+ return v;
+ });
}
+ }
- String targetVolumeType = volumeType != null ? volumeType : volume.getVolumeType();
- if (volumeType != null && !VALID_VOLUME_TYPES.contains(volumeType)) {
- throw new AwsException("InvalidParameterValue",
- "Value (" + volumeType + ") for parameter volumeType is invalid. Unknown volume type.", 400);
- }
+ private void withVolumeLock(String volumeId, Runnable action) {
+ withVolumeLock(volumeId, () -> {
+ action.run();
+ return null;
+ });
+ }
- if (throughput != null && !"gp3".equals(targetVolumeType)) {
- throw new AwsException("InvalidParameterCombination",
- "The parameter Throughput is not supported for " + targetVolumeType + " volumes", 400);
+ private static boolean isSameDevice(String d1, String d2) {
+ if (d1 == null && d2 == null) {
+ return true;
}
-
- if (iops != null && !"gp3".equals(targetVolumeType) && !"io1".equals(targetVolumeType) && !"io2".equals(targetVolumeType)) {
- throw new AwsException("InvalidParameterCombination",
- "The parameter iops is not supported for " + targetVolumeType + " volumes", 400);
+ if (d1 == null || d2 == null) {
+ return false;
}
+ String n1 = d1.startsWith("/") ? d1 : "/dev/" + d1;
+ String n2 = d2.startsWith("/") ? d2 : "/dev/" + d2;
+ return n1.equals(n2);
+ }
- if (multiAttachEnabled != null && multiAttachEnabled && !"io1".equals(targetVolumeType) && !"io2".equals(targetVolumeType)) {
- throw new AwsException("InvalidParameterCombination",
- "The parameter MultiAttachEnabled is not supported for " + targetVolumeType + " volumes", 400);
+ private boolean isRootVolume(String region, Volume volume) {
+ if (volume == null || volume.getAttachments().isEmpty()) {
+ return false;
+ }
+ for (VolumeAttachment att : volume.getAttachments()) {
+ if (att.getInstanceId() == null) {
+ continue;
+ }
+ Instance inst = findAnyInstance(key(region, att.getInstanceId()))
+ .or(() -> findExternalInstance(callerAccountId(), region, att.getInstanceId()))
+ .orElse(null);
+ if (inst == null) {
+ for (Instance candidate : instances.scan(k -> true)) {
+ if (att.getInstanceId().equals(candidate.getInstanceId()) && region.equals(candidate.getRegion())) {
+ inst = candidate;
+ break;
+ }
+ }
+ }
+ if (inst != null && volume.getVolumeId().equals(inst.getRootVolumeId())
+ && isSameDevice(att.getDevice(), inst.getRootDeviceName())) {
+ return true;
+ }
}
+ return false;
+ }
- if (dryRun) {
- throw new AwsException("DryRunOperation", "Request would have succeeded, but DryRun flag is set.", 412);
+ public VolumeModification modifyVolume(String region, String volumeId, Integer size,
+ String volumeType, Integer iops, Integer throughput,
+ Boolean multiAttachEnabled, boolean dryRun) {
+ if (volumeId == null || volumeId.isBlank()) {
+ throw new AwsException("MissingParameter", "The parameter VolumeId is missing", 400);
}
+ return withVolumeLock(volumeId, () -> {
+ ensureDefaultResources(region);
+ Volume volume = getRequiredVolume(region, volumeId);
- Integer originalSize = volume.getSize();
- String originalVolumeType = volume.getVolumeType();
- Integer originalIops = volume.getIops() > 0 ? volume.getIops() : null;
- Integer originalThroughput = volume.getThroughput();
- Boolean originalMultiAttachEnabled = volume.getMultiAttachEnabled();
+ if (size != null && size <= 0) {
+ throw new AwsException("InvalidParameterValue",
+ "Value (" + size + ") for parameter size is invalid.", 400);
+ }
+ if (size != null && size < volume.getSize()) {
+ throw new AwsException("InvalidParameterValue",
+ "New size cannot be smaller than existing size", 400);
+ }
- int targetSize = size != null ? size : volume.getSize();
- Integer targetIops = iops != null ? iops : (volume.getIops() > 0 ? volume.getIops() : null);
- Integer targetThroughput = throughput != null ? throughput : volume.getThroughput();
- Boolean targetMultiAttach = multiAttachEnabled != null ? multiAttachEnabled : volume.getMultiAttachEnabled();
+ String targetVolumeType = volumeType != null ? volumeType : volume.getVolumeType();
+ if (volumeType != null && !VALID_VOLUME_TYPES.contains(volumeType)) {
+ throw new AwsException("InvalidParameterValue",
+ "Value (" + volumeType + ") for parameter volumeType is invalid. Unknown volume type.", 400);
+ }
- if ("gp3".equals(targetVolumeType)) {
- if (targetIops == null) {
- targetIops = 3000;
+ if (throughput != null && !"gp3".equals(targetVolumeType)) {
+ throw new AwsException("InvalidParameterCombination",
+ "The parameter Throughput is not supported for " + targetVolumeType + " volumes", 400);
}
- if (targetThroughput == null) {
- targetThroughput = 125;
+
+ if (iops != null && !"gp3".equals(targetVolumeType) && !"io1".equals(targetVolumeType) && !"io2".equals(targetVolumeType)) {
+ throw new AwsException("InvalidParameterCombination",
+ "The parameter iops is not supported for " + targetVolumeType + " volumes", 400);
}
- } else if ("io1".equals(targetVolumeType) || "io2".equals(targetVolumeType)) {
- if (targetIops == null) {
- targetIops = 3000;
+
+ if (multiAttachEnabled != null && multiAttachEnabled && !"io1".equals(targetVolumeType) && !"io2".equals(targetVolumeType)) {
+ throw new AwsException("InvalidParameterCombination",
+ "The parameter MultiAttachEnabled is not supported for " + targetVolumeType + " volumes", 400);
}
- targetThroughput = null;
- } else {
- targetIops = null;
- targetThroughput = null;
- targetMultiAttach = false;
- }
- volume.setSize(targetSize);
- volume.setVolumeType(targetVolumeType);
- volume.setIops(targetIops != null ? targetIops : 0);
- volume.setThroughput(targetThroughput);
- volume.setMultiAttachEnabled(targetMultiAttach);
- volumes.put(key(region, volumeId), volume);
+ if (dryRun) {
+ throw new AwsException("DryRunOperation", "Request would have succeeded, but DryRun flag is set.", 412);
+ }
+
+ Integer originalSize = volume.getSize();
+ String originalVolumeType = volume.getVolumeType();
+ Integer originalIops = volume.getIops() > 0 ? volume.getIops() : null;
+ Integer originalThroughput = volume.getThroughput();
+ Boolean originalMultiAttachEnabled = volume.getMultiAttachEnabled();
+
+ int targetSize = size != null ? size : volume.getSize();
+ Integer targetIops = iops != null ? iops : (volume.getIops() > 0 ? volume.getIops() : null);
+ Integer targetThroughput = throughput != null ? throughput : volume.getThroughput();
+ Boolean targetMultiAttach = multiAttachEnabled != null ? multiAttachEnabled : volume.getMultiAttachEnabled();
- Instant now = Instant.now();
- VolumeModification mod = new VolumeModification();
- mod.setVolumeId(volumeId);
- mod.setModificationState("completed");
- mod.setTargetSize(targetSize);
- mod.setTargetVolumeType(targetVolumeType);
- mod.setTargetIops(targetIops);
- mod.setTargetThroughput(targetThroughput);
- mod.setTargetMultiAttachEnabled(targetMultiAttach);
- mod.setOriginalSize(originalSize);
- mod.setOriginalVolumeType(originalVolumeType);
- mod.setOriginalIops(originalIops);
- mod.setOriginalThroughput(originalThroughput);
- mod.setOriginalMultiAttachEnabled(originalMultiAttachEnabled);
- mod.setProgress(100L);
- mod.setStartTime(now);
- mod.setEndTime(now);
- mod.setRegion(region);
-
- volumeModifications.put(key(region, volumeId), mod);
- return mod;
+ if ("gp3".equals(targetVolumeType)) {
+ if (targetIops == null) {
+ targetIops = 3000;
+ }
+ if (targetThroughput == null) {
+ targetThroughput = 125;
+ }
+ } else if ("io1".equals(targetVolumeType) || "io2".equals(targetVolumeType)) {
+ if (targetIops == null) {
+ targetIops = 3000;
+ }
+ targetThroughput = null;
+ } else {
+ targetIops = null;
+ targetThroughput = null;
+ targetMultiAttach = false;
+ }
+
+ boolean resizeSuccess = true;
+ if (volumeBlockDeviceManager != null && volumeBlockDeviceManager.isAvailable()
+ && targetSize > originalSize && !isRootVolume(region, volume)) {
+ resizeSuccess = volumeBlockDeviceManager.resizeVolume(volumeId, targetSize);
+ }
+
+ Instant now = Instant.now();
+ VolumeModification mod = new VolumeModification();
+ mod.setVolumeId(volumeId);
+ mod.setTargetSize(targetSize);
+ mod.setTargetVolumeType(targetVolumeType);
+ mod.setTargetIops(targetIops);
+ mod.setTargetThroughput(targetThroughput);
+ mod.setTargetMultiAttachEnabled(targetMultiAttach);
+ mod.setOriginalSize(originalSize);
+ mod.setOriginalVolumeType(originalVolumeType);
+ mod.setOriginalIops(originalIops);
+ mod.setOriginalThroughput(originalThroughput);
+ mod.setOriginalMultiAttachEnabled(originalMultiAttachEnabled);
+ mod.setStartTime(now);
+ mod.setEndTime(now);
+ mod.setRegion(region);
+
+ if (!resizeSuccess) {
+ mod.setModificationState("failed");
+ mod.setStatusMessage("Failed to resize volume backing file or loop device");
+ mod.setProgress(0L);
+ volumeModifications.put(key(region, volumeId), mod);
+ return mod;
+ }
+
+ volume.setSize(targetSize);
+ volume.setVolumeType(targetVolumeType);
+ volume.setIops(targetIops != null ? targetIops : 0);
+ volume.setThroughput(targetThroughput);
+ volume.setMultiAttachEnabled(targetMultiAttach);
+ volumes.put(key(region, volumeId), volume);
+
+ mod.setModificationState("completed");
+ mod.setProgress(100L);
+ volumeModifications.put(key(region, volumeId), mod);
+ return mod;
+ });
}
public List<VolumeModification> describeVolumesModifications(String region, List<String> volumeIds,
diff --git a/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2VolumeBlockDeviceManager.java b/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2VolumeBlockDeviceManager.java
index 1e9eee1a87..abce261231 100644
--- a/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2VolumeBlockDeviceManager.java
+++ b/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2VolumeBlockDeviceManager.java
@@ -24,7 +24,10 @@ import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.atomic.AtomicInteger;
+import java.util.concurrent.locks.ReentrantLock;
import java.util.function.Function;
+import java.util.function.Supplier;
import java.util.regex.Pattern;
/**
@@ -135,28 +138,126 @@ public class Ec2VolumeBlockDeviceManager implements Resettable {
* Deletes the volume's backing file and releases any associated loop device.
*/
public boolean deleteVolume(String volumeId) {
- if (!isAvailable()) {
+ if (!isAvailable() || volumeId == null) {
return false;
}
- String helperId = ensureHelperContainer();
- if (helperId == null) {
- LOG.warnv("EC2 volume helper container unavailable; backing file for volume {0} could not be deleted",
- volumeId);
+ return withVolumeLock(volumeId, () -> {
+ String helperId = ensureHelperContainer();
+ if (helperId == null) {
+ LOG.warnv("EC2 volume helper container unavailable; backing file for volume {0} could not be deleted",
+ volumeId);
+ return false;
+ }
+ String rawFile = "/volumes/" + volumeId + ".raw";
+ String script = SHELL_LOOP_FINDER
+ + "raw=\"$1\"\n"
+ + "while true; do\n"
+ + " loop=$(find_loops \"$raw\" | head -n1)\n"
+ + " [ -n \"$loop\" ] || break\n"
+ + " losetup -d \"$loop\" 2>/dev/null || break\n"
+ + "done\n"
+ + "rm -f \"$raw\"";
+ ContainerExec.Result result = execInContainer(helperId,
+ new String[]{"sh", "-c", script, "delete", rawFile}, DEFAULT_TIMEOUT_SECONDS);
+ activeLoopDevices.remove(volumeId);
+ return result.exitCode() == 0;
+ });
+ }
+
+ private static final class RefCountedLock {
+ private final ReentrantLock lock = new ReentrantLock();
+ private final AtomicInteger refCount = new AtomicInteger(1);
+ }
+
+ private final ConcurrentHashMap<String, RefCountedLock> volumeLocks = new ConcurrentHashMap<>();
+
+ private <T> T withVolumeLock(String volumeId, Supplier<T> action) {
+ if (volumeId == null) {
+ return action.get();
+ }
+ RefCountedLock refLock = volumeLocks.compute(volumeId, (k, v) -> {
+ if (v == null) {
+ return new RefCountedLock();
+ }
+ v.refCount.incrementAndGet();
+ return v;
+ });
+ refLock.lock.lock();
+ try {
+ return action.get();
+ } finally {
+ refLock.lock.unlock();
+ volumeLocks.computeIfPresent(volumeId, (k, v) -> {
+ if (v.refCount.decrementAndGet() <= 0) {
+ return null;
+ }
+ return v;
+ });
+ }
+ }
+
+ /**
+ * Resizes the volume's raw backing file and refreshes any associated loop device.
+ */
+ public boolean resizeVolume(String volumeId, int sizeGib) {
+ if (!isAvailable() || volumeId == null) {
return false;
}
- String rawFile = "/volumes/" + volumeId + ".raw";
- String script = SHELL_LOOP_FINDER
- + "raw=\"$1\"\n"
- + "while true; do\n"
- + " loop=$(find_loops \"$raw\" | head -n1)\n"
- + " [ -n \"$loop\" ] || break\n"
- + " losetup -d \"$loop\" 2>/dev/null || break\n"
- + "done\n"
- + "rm -f \"$raw\"";
- ContainerExec.Result result = execInContainer(helperId,
- new String[]{"sh", "-c", script, "delete", rawFile}, DEFAULT_TIMEOUT_SECONDS);
- activeLoopDevices.remove(volumeId);
- return result.exitCode() == 0;
+ return withVolumeLock(volumeId, () -> {
+ String helperId = ensureHelperContainer();
+ if (helperId == null) {
+ LOG.warnv("EC2 volume helper container unavailable; backing file for volume {0} could not be resized",
+ volumeId);
+ return false;
+ }
+ int effectiveSize = sizeGib > 0 ? sizeGib : 8;
+ String rawFile = "/volumes/" + volumeId + ".raw";
+ String script = SHELL_LOOP_FINDER
+ + "raw=\"$1\"\n"
+ + "size=\"$2\"\n"
+ + "cur_bytes=$(stat -c %s \"$raw\" 2>/dev/null || echo 0)\n"
+ + "target_bytes=$((size * 1024 * 1024 * 1024))\n"
+ + "truncated=0\n"
+ + "if [ \"$cur_bytes\" -lt \"$target_bytes\" ]; then\n"
+ + " truncate -s \"${size}G\" \"$raw\" || exit 1\n"
+ + " truncated=1\n"
+ + "fi\n"
+ + "for loop in $(find_loops \"$raw\"); do\n"
+ + " minor=$(echo \"$loop\" | sed 's/[^0-9]*//g')\n"
+ + " if [ -n \"$minor\" ] && [ ! -b \"$loop\" ]; then\n"
+ + " mknod \"$loop\" b 7 \"$minor\" 2>/dev/null || true\n"
+ + " fi\n"
+ + " if ! losetup -c \"$loop\"; then\n"
+ + " if [ \"$truncated\" -eq 1 ]; then\n"
+ + " if [ \"$cur_bytes\" -gt 0 ]; then\n"
+ + " truncate -s \"$cur_bytes\" \"$raw\" 2>/dev/null || true\n"
+ + " else\n"
+ + " rm -f \"$raw\" 2>/dev/null || true\n"
+ + " fi\n"
+ + " for rloop in $(find_loops \"$raw\"); do\n"
+ + " losetup -c \"$rloop\" 2>/dev/null || true\n"
+ + " done\n"
+ + " fi\n"
+ + " exit 2\n"
+ + " fi\n"
+ + "done";
+ ContainerExec.Result result = execInContainer(helperId,
+ new String[]{"sh", "-c", script, "resize", rawFile, String.valueOf(effectiveSize)},
+ DEFAULT_TIMEOUT_SECONDS);
+ if (result.exitCode() == 1) {
+ LOG.warnv("Failed to resize backing file for volume {0}: {1}", volumeId, result.summary());
+ return false;
+ }
+ if (result.exitCode() == 2) {
+ LOG.warnv("Failed to refresh loop device for volume {0}: {1}", volumeId, result.summary());
+ return false;
+ }
+ if (result.exitCode() != 0) {
+ LOG.warnv("Failed to resize volume {0}: {1}", volumeId, result.summary());
+ return false;
+ }
+ return true;
+ });
}
/**
diff --git a/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2VolumeBlockDeviceDockerIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2VolumeBlockDeviceDockerIntegrationTest.java
index 14e58f23e1..3162a463c0 100644
--- a/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2VolumeBlockDeviceDockerIntegrationTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2VolumeBlockDeviceDockerIntegrationTest.java
@@ -13,6 +13,7 @@ import io.github.hectorvent.floci.services.ec2.model.InstanceState;
import io.github.hectorvent.floci.services.ec2.model.Placement;
import io.github.hectorvent.floci.services.ec2.model.Volume;
import io.github.hectorvent.floci.services.ec2.model.VolumeAttachment;
+import io.github.hectorvent.floci.services.ec2.model.VolumeModification;
import io.quarkus.test.junit.QuarkusTest;
import io.quarkus.test.junit.QuarkusTestProfile;
import io.quarkus.test.junit.TestProfile;
@@ -23,6 +24,7 @@ import org.junit.jupiter.api.Assumptions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
+import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@@ -30,6 +32,7 @@ import java.util.Objects;
import java.util.Optional;
import java.util.UUID;
+import static org.awaitility.Awaitility.await;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
@@ -71,6 +74,8 @@ class Ec2VolumeBlockDeviceDockerIntegrationTest {
private final List<String> containersToClean = new ArrayList<>();
private final List<String> volumesToClean = new ArrayList<>();
+ private final List<String> syntheticRootVolumesToClean = new ArrayList<>();
+ private final List<String> instancesToClean = new ArrayList<>();
@BeforeEach
void checkPrerequisites() {
@@ -102,6 +107,25 @@ class Ec2VolumeBlockDeviceDockerIntegrationTest {
}
}
volumesToClean.clear();
+
+ for (String volId : syntheticRootVolumesToClean) {
+ try {
+ ec2Service.deleteVolumeForTest("us-east-1", volId);
+ } catch (Exception e) {
+ LOG.debugv("Ignoring delete synthetic root volume error during test cleanup: {0}", e.getMessage());
+ }
+ }
+ syntheticRootVolumesToClean.clear();
+
+ for (String iid : instancesToClean) {
+ try {
+ ec2Service.deleteInstanceForTest("us-east-1", iid);
+ } catch (Exception e) {
+ LOG.debugv("Ignoring delete test instance error during test cleanup: {0}", e.getMessage());
+ }
+ }
+ instancesToClean.clear();
+
ec2Service.setClusterNodeInstanceProvider(null);
for (String cid : containersToClean) {
@@ -146,13 +170,33 @@ class Ec2VolumeBlockDeviceDockerIntegrationTest {
String instanceId = "i-" + suffix;
Instance instance = new Instance();
instance.setInstanceId(instanceId);
+ String rootVolId = "vol-root-" + suffix;
instance.setDockerContainerId(containerId);
instance.setRegion("us-east-1");
+ instance.setRootVolumeId(rootVolId);
+ instance.setRootDeviceName("/dev/xvda");
Placement placement = new Placement();
placement.setAvailabilityZone("us-east-1a");
instance.setPlacement(placement);
instance.setState(InstanceState.running());
+ Volume rootVol = new Volume();
+ rootVol.setVolumeId(rootVolId);
+ rootVol.setVolumeType("gp3");
+ rootVol.setSize(8);
+ rootVol.setState("in-use");
+ rootVol.setRegion("us-east-1");
+ rootVol.setAvailabilityZone("us-east-1a");
+ VolumeAttachment rootAtt = new VolumeAttachment();
+ rootAtt.setVolumeId(rootVolId);
+ rootAtt.setInstanceId(instanceId);
+ rootAtt.setDevice("/dev/xvda");
+ rootAtt.setState("attached");
+ rootVol.getAttachments().add(rootAtt);
+ ec2Service.putVolumeForTest(rootVol);
+ syntheticRootVolumesToClean.add(rootVolId);
+ instancesToClean.add(instanceId);
+
// Register directly into service storage for hermetic test execution
ec2Service.putInstanceForTest(instance);
return instance;
@@ -393,6 +437,142 @@ class Ec2VolumeBlockDeviceDockerIntegrationTest {
assertTrue(ex.getMessage().contains("already in use by volume"));
}
+ @Test
+ void modifyVolumeAttachedExpandsBackingFileLoopDeviceAndAllowsWritingBeyondOriginalCapacity() {
+ String suffix = UUID.randomUUID().toString().replace("-", "").substring(0, 10);
+ Instance instance = createRunningTestInstance(suffix);
+
+ Volume volume = ec2Service.createVolume("us-east-1", "us-east-1a", "gp3", 1, false, 3000, 125, null, null);
+ volumesToClean.add(volume.getVolumeId());
+
+ VolumeAttachment attachment = ec2Service.attachVolume("us-east-1", volume.getVolumeId(), instance.getInstanceId(), "/dev/xvdf");
+ assertNotNull(attachment);
+
+ String targetContainerId = instance.getDockerContainerId();
+
+ // 1. Initial size is 1 GiB (1073741824 bytes)
+ ContainerExec.Result checkInitialSize = execIn(targetContainerId, "blockdev --getsize64 /dev/xvdf");
+ assertEquals(0, checkInitialSize.exitCode());
+ assertEquals("1073741824", checkInitialSize.stdout().trim());
+
+ // 2. Modify volume to 2 GiB
+ VolumeModification mod = ec2Service.modifyVolume("us-east-1", volume.getVolumeId(), 2, null, null, null, null, false);
+ assertNotNull(mod);
+ assertEquals("completed", mod.getModificationState());
+ assertEquals(2, mod.getTargetSize());
+
+ // 3. Backing file size on helper is 2 GiB (2147483648 bytes)
+ String helperName = ContainerStorageHelper.resourceName(config, "ec2", null, "volume-helper");
+ ContainerExec.Result checkFileSize = execIn(helperName, "stat -c %s /volumes/" + volume.getVolumeId() + ".raw");
+ assertEquals(0, checkFileSize.exitCode());
+ assertEquals("2147483648", checkFileSize.stdout().trim());
+
+ // 4. Loop device capacity inside instance container refreshed to 2 GiB
+ ContainerExec.Result checkResizedSize = execIn(targetContainerId, "blockdev --getsize64 /dev/xvdf");
+ assertEquals(0, checkResizedSize.exitCode());
+ assertEquals("2147483648", checkResizedSize.stdout().trim());
+
+ // 5. Writing beyond the original 1 GiB capacity (at 1.5 GiB offset) succeeds
+ ContainerExec.Result writeBeyond = execIn(targetContainerId,
+ "dd if=/dev/zero of=/dev/xvdf bs=1M count=10 seek=1500 conv=notrunc");
+ assertEquals(0, writeBeyond.exitCode(), "Writing beyond original capacity should succeed: " + writeBeyond.summary());
+ }
+
+ @Test
+ void modifyVolumeUnattachedGrowsBackingFile() {
+ Volume volume = ec2Service.createVolume("us-east-1", "us-east-1a", "gp3", 1, false, 3000, 125, null, null);
+ volumesToClean.add(volume.getVolumeId());
+
+ String helperName = ContainerStorageHelper.resourceName(config, "ec2", null, "volume-helper");
+ ContainerExec.Result checkInitialSize = execIn(helperName, "stat -c %s /volumes/" + volume.getVolumeId() + ".raw");
+ assertEquals(0, checkInitialSize.exitCode());
+ assertEquals("1073741824", checkInitialSize.stdout().trim());
+
+ VolumeModification mod = ec2Service.modifyVolume("us-east-1", volume.getVolumeId(), 3, null, null, null, null, false);
+ assertNotNull(mod);
+ assertEquals("completed", mod.getModificationState());
+
+ ContainerExec.Result checkResizedSize = execIn(helperName, "stat -c %s /volumes/" + volume.getVolumeId() + ".raw");
+ assertEquals(0, checkResizedSize.exitCode());
+ assertEquals("3221225472", checkResizedSize.stdout().trim());
+ }
+
+ @Test
+ void modifyVolumeRejectsSizeDecreaseLeavingBackingFileUntouched() {
+ Volume volume = ec2Service.createVolume("us-east-1", "us-east-1a", "gp3", 5, false, 3000, 125, null, null);
+ volumesToClean.add(volume.getVolumeId());
+
+ String helperName = ContainerStorageHelper.resourceName(config, "ec2", null, "volume-helper");
+ ContainerExec.Result checkInitialSize = execIn(helperName, "stat -c %s /volumes/" + volume.getVolumeId() + ".raw");
+ assertEquals(0, checkInitialSize.exitCode());
+ assertEquals("5368709120", checkInitialSize.stdout().trim());
+
+ AwsException ex = assertThrows(AwsException.class, () ->
+ ec2Service.modifyVolume("us-east-1", volume.getVolumeId(), 3, null, null, null, null, false));
+ assertEquals("InvalidParameterValue", ex.getErrorCode());
+
+ ContainerExec.Result checkAfterSize = execIn(helperName, "stat -c %s /volumes/" + volume.getVolumeId() + ".raw");
+ assertEquals(0, checkAfterSize.exitCode());
+ assertEquals("5368709120", checkAfterSize.stdout().trim());
+ }
+
+ @Test
+ void modifyVolumeRootVolumeUpdatesMetadataWithoutCreatingBackingFile() {
+ Instance instance = createRunningTestInstance("root-resize");
+ String rootVolId = instance.getRootVolumeId();
+ assertNotNull(rootVolId);
+
+ VolumeModification mod = ec2Service.modifyVolume("us-east-1", rootVolId, 16, null, null, null, null, false);
+ assertNotNull(mod);
+ assertEquals("completed", mod.getModificationState());
+ assertEquals(16, mod.getTargetSize());
+
+ String helperName = ContainerStorageHelper.resourceName(config, "ec2", null, "volume-helper");
+ ContainerExec.Result checkFile = execIn(helperName, "test -f /volumes/" + rootVolId + ".raw");
+ assertTrue(checkFile.exitCode() != 0);
+ }
+
+ @Test
+ void modifyVolumeDataVolumeAttachedAtRootDeviceResizesBackingFile() {
+ Instance instance = createRunningTestInstance("data-at-root");
+ String rootVolId = instance.getRootVolumeId();
+ assertNotNull(rootVolId);
+
+ ec2Service.stopInstances("us-east-1", List.of(instance.getInstanceId()));
+ await().atMost(Duration.ofSeconds(35)).until(() ->
+ "stopped".equals(ec2Service.describeInstances("us-east-1", List.of(instance.getInstanceId()), null)
+ .getFirst().getInstances().getFirst().getState().getName()));
+ ec2Service.detachVolume("us-east-1", rootVolId, instance.getInstanceId(), instance.getRootDeviceName(), true);
+
+ // Detached root volume is no longer attached as an instance root volume; resizing it creates
+ // and expands its backing raw file so it can be reattached as a data volume.
+ VolumeModification rootMod = ec2Service.modifyVolume("us-east-1", rootVolId, 12, null, null, null, null, false);
+ assertNotNull(rootMod);
+ assertEquals("completed", rootMod.getModificationState());
+ assertEquals(12, rootMod.getTargetSize());
+
+ String helperName = ContainerStorageHelper.resourceName(config, "ec2", null, "volume-helper");
+ ContainerExec.Result checkRootFile = execIn(helperName, "stat -c %s /volumes/" + rootVolId + ".raw");
+ assertEquals(0, checkRootFile.exitCode());
+ assertEquals("12884901888", checkRootFile.stdout().trim());
+ volumesToClean.add(rootVolId);
+ syntheticRootVolumesToClean.remove(rootVolId);
+
+ Volume dataVol = ec2Service.createVolume("us-east-1", "us-east-1a", "gp3", 5, false, 3000, 125, null, null);
+ volumesToClean.add(dataVol.getVolumeId());
+
+ ec2Service.attachVolume("us-east-1", dataVol.getVolumeId(), instance.getInstanceId(), instance.getRootDeviceName());
+
+ VolumeModification mod = ec2Service.modifyVolume("us-east-1", dataVol.getVolumeId(), 10, null, null, null, null, false);
+ assertNotNull(mod);
+ assertEquals("completed", mod.getModificationState());
+ assertEquals(10, mod.getTargetSize());
+
+ ContainerExec.Result checkFile = execIn(helperName, "stat -c %s /volumes/" + dataVol.getVolumeId() + ".raw");
+ assertEquals(0, checkFile.exitCode());
+ assertEquals("10737418240", checkFile.stdout().trim());
+ }
+
private ContainerExec.Result execIn(String containerIdOrName, String command) {
try {
return ContainerExec.runMerged(dockerClient, containerIdOrName, new String[]{"sh", "-c", command}, 30);
diff --git a/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2VolumeBlockDeviceManagerTest.java b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2VolumeBlockDeviceManagerTest.java
index 56ca8e50eb..62953ea9de 100644
--- a/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2VolumeBlockDeviceManagerTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2VolumeBlockDeviceManagerTest.java
@@ -13,6 +13,8 @@ import java.util.List;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
import static org.mockito.Mockito.mock;
@@ -60,4 +62,66 @@ class Ec2VolumeBlockDeviceManagerTest {
assertDoesNotThrow(() -> manager.createVolume("vol-1", 8));
}
+
+ @Test
+ void resizeVolumeSizesBackingFileAndRefreshesLoopDevicesInsideHelperContainer() {
+ List<List<String>> commands = ContainerExecStubs.completeEveryExec(dockerClient, HELPER_ID, 0, "", "");
+
+ boolean ok = manager.resizeVolume("vol-1", 16);
+
+ assertTrue(ok);
+ assertEquals(1, commands.size());
+ List<String> cmd = commands.getFirst();
+ assertEquals("sh", cmd.get(0));
+ assertEquals("-c", cmd.get(1));
+ assertEquals("resize", cmd.get(3));
+ assertEquals("/volumes/vol-1.raw", cmd.get(4));
+ assertEquals("16", cmd.get(5));
+ assertTrue(cmd.get(2).contains("truncate -s \"${size}G\" \"$raw\""));
+ assertTrue(cmd.get(2).contains("losetup -c \"$loop\""));
+ assertTrue(cmd.get(2).contains("cur_bytes=$(stat -c %s \"$raw\" 2>/dev/null || echo 0)"));
+ assertTrue(cmd.get(2).contains("if [ \"$cur_bytes\" -lt \"$target_bytes\" ]; then"));
+ assertTrue(cmd.get(2).contains("truncate -s \"$cur_bytes\" \"$raw\""));
+ assertTrue(cmd.get(2).contains("for rloop in $(find_loops \"$raw\"); do"));
+ }
+
+ @Test
+ void resizeVolumeDockerFailureWhileRunningIsNotThrown() {
+ when(dockerClient.execCreateCmd(HELPER_ID)).thenThrow(new IllegalStateException("daemon went away"));
+
+ assertDoesNotThrow(() -> {
+ boolean ok = manager.resizeVolume("vol-1", 16);
+ assertFalse(ok);
+ });
+ }
+
+ @Test
+ void resizeVolumeTruncateFailureLogsAndReturnsFalseWithoutThrowing() {
+ ContainerExecStubs.completeEveryExec(dockerClient, HELPER_ID, 1, "", "truncate: out of space");
+
+ assertDoesNotThrow(() -> {
+ boolean ok = manager.resizeVolume("vol-1", 16);
+ assertFalse(ok);
+ });
+ }
+
+ @Test
+ void resizeVolumeRefreshFailureLogsAndReturnsFalseWithoutThrowing() {
+ ContainerExecStubs.completeEveryExec(dockerClient, HELPER_ID, 2, "", "losetup: invalid option -- c");
+
+ assertDoesNotThrow(() -> {
+ boolean ok = manager.resizeVolume("vol-1", 16);
+ assertFalse(ok);
+ });
+ }
+
+ @Test
+ void resizeVolumeReturnsFalseWhenNotAvailable() {
+ EmulatorConfig config = mock(EmulatorConfig.class, RETURNS_DEEP_STUBS);
+ when(config.services().ec2().volumeBlockDevices()).thenReturn(false);
+ Ec2VolumeBlockDeviceManager disabledManager = new Ec2VolumeBlockDeviceManager(dockerClient,
+ mock(ContainerBuilder.class), mock(ContainerLifecycleManager.class), config);
+
+ assertFalse(disabledManager.resizeVolume("vol-1", 16));
+ }
}