Skip to content

add overlaybd-resize tool for userspace ext4 resize on overlaybd images#408

Merged
BigVan merged 1 commit into
containerd:mainfrom
OsentryO:feat/overlaybd-resize
Jun 24, 2026
Merged

add overlaybd-resize tool for userspace ext4 resize on overlaybd images#408
BigVan merged 1 commit into
containerd:mainfrom
OsentryO:feat/overlaybd-resize

Conversation

@OsentryO

@OsentryO OsentryO commented Jun 18, 2026

Copy link
Copy Markdown
Contributor

Summary

Add overlaybd-resize tool that performs ext4 filesystem resize (shrink/expand) on overlaybd images in userspace, without kernel block device.

Changes

Usage

overlaybd-resize --config /path/to/config.v1.json --size 8 [--service_config_path PATH] [--verbose]

Size is specified in GB (e.g., 8 for 8GB, 500 for 500GB). Only integer values are accepted.

Requirements:

  • Config must have an existing upper layer (data + index files)
  • Upper layer files must be accessible (read/write)

Design

Operates on existing writable layer (upper required):

  • Expand: ftruncate data file to target size → update index header (if non-sparse) → update data header → open image → resize_extfs
  • Shrink: open image → resize_extfs → ftruncate data file → update headers

Key features:

  • Inlined ImageService calls to avoid exit(-1) in create_overlaybd, enabling proper error handling and rollback
  • Header update order: ftruncate → index → data (data written last as commit point for retry safety)
  • Sparse layer detection: automatically skips index header update for sparse layers (tiny/empty index files)

Dependencies

Testing

  • 10 unit tests covering: basic shrink/expand, relocation, too-small target, non-aligned groups, empty disk, multiple resizes
  • Verified on real overlaybd images: wordpress (no lazyinit), swebench (lazyinit), stable-diffusion (30 layers)
    • Shrink 256G→8G: 0.2-0.8s
    • Expand 256G→500G: 0.6-0.7s

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds a new userspace CLI (overlaybd-resize) to resize ext4 filesystems inside overlaybd images without requiring a kernel block device, along with unit tests and build system integration for the required Photon/e2fsprogs components.

Changes:

  • Adds src/tools/overlaybd-resize.cpp implementing shrink/expand flows for images with or without an existing upper layer.
  • Adds src/test/resize_test.cpp with unit tests covering resize scenarios and data integrity checks.
  • Integrates resize support into the build via CMake updates (Photon/e2fsprogs fetching and new tool/test targets).

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 8 comments.

Show a summary per file
File Description
src/tools/overlaybd-resize.cpp New CLI tool implementing ext4 resize on overlaybd images in userspace.
src/tools/CMakeLists.txt Adds build/install target for overlaybd-resize gated by PHOTON_ENABLE_RESIZE.
src/test/resize_test.cpp New gtest-based unit tests for shrink/expand and edge cases.
src/test/CMakeLists.txt Adds resize_test target and registers it with CTest (gated).
CMake/Findphoton.cmake Updates Photon fetch revision and enables resize-related build flags.
CMake/Finde2fs.cmake Updates e2fsprogs fetch revision and exposes resize build directories for object linking.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/tools/overlaybd-resize.cpp Outdated
Comment on lines +39 to +46
uint64_t multiplier = 1024ULL * 1024; // default MB
if (end && (*end == 'G' || *end == 'g'))
multiplier = 1024ULL * 1024 * 1024;
else if (end && (*end == 'T' || *end == 't'))
multiplier = 1024ULL * 1024 * 1024 * 1024;
else if (end && (*end == 'M' || *end == 'm'))
multiplier = 1024ULL * 1024;

Comment thread src/tools/overlaybd-resize.cpp Outdated
Comment thread src/tools/overlaybd-resize.cpp Outdated
Comment on lines +276 to +285
ImageService *imgservice = nullptr;
photon::fs::IFile *imgfile = nullptr;
create_overlaybd(service_config_path, config_path, imgservice, imgfile);
if (!imgfile) {
fprintf(stderr, "failed to open overlaybd image\n");
if (is_expand) {
update_vsize(existing_data_path, (uint64_t)current_vsize);
}
return 1;
}
Comment thread src/tools/overlaybd-resize.cpp Outdated
Comment on lines +354 to +364
// Step 3: open image via ImageService
ImageService *imgservice = nullptr;
photon::fs::IFile *imgfile = nullptr;
create_overlaybd(service_config_path, tmp_config, imgservice, imgfile);
if (!imgfile) {
fprintf(stderr, "failed to open overlaybd image\n");
unlink(tmp_config.c_str());
unlink(data_path.c_str());
unlink(index_path.c_str());
return 1;
}
Comment thread src/tools/overlaybd-resize.cpp Outdated
Comment on lines +107 to +123
static std::string parse_upper_data_path(const std::string &config_path) {
FILE *fp = fopen(config_path.c_str(), "r");
if (!fp) return "";
char readbuf[65536];
rapidjson::FileReadStream is(fp, readbuf, sizeof(readbuf));
rapidjson::Document doc;
doc.ParseStream(is);
fclose(fp);

if (doc.HasParseError()) return "";
if (!doc.HasMember("upper")) return "";
if (!doc["upper"].IsObject()) return "";
if (!doc["upper"].HasMember("data")) return "";
if (!doc["upper"]["data"].IsString()) return "";

return doc["upper"]["data"].GetString();
}
Comment thread src/test/resize_test.cpp Outdated
Comment on lines +34 to +53
static const char *TEST_IMG = "/tmp/resize_test.img";

static photon::fs::IFile *create_image(uint64_t size_mb) {
auto file = photon::fs::open_localfile_adaptor(
TEST_IMG, O_RDWR | O_CREAT | O_TRUNC, 0644, 0);
if (!file) return nullptr;
if (file->ftruncate(size_mb * MB) < 0) {
delete file;
return nullptr;
}
if (photon::fs::make_extfs(file) < 0) {
delete file;
return nullptr;
}
return file;
}

static void cleanup() {
::unlink(TEST_IMG);
}
Comment thread CMake/Findphoton.cmake
Comment thread src/tools/overlaybd-resize.cpp Outdated
Comment on lines +139 to +141
// virtual_size is at offset 48 in HeaderTrailer (after magic0[8] + magic1[16] + size[4] + flags[4] + index_offset[8] + index_size[8])
uint64_t *vsize_ptr = (uint64_t *)(buf + 48);
*vsize_ptr = new_vsize;
Comment thread src/tools/overlaybd-resize.cpp Outdated

CLI::App app{"overlaybd-resize: resize ext4 filesystem on overlaybd image in userspace"};
app.add_option("--config", config_path,
"overlaybd image config (config.v1.json from rpull)")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove'rpull'

Comment thread src/tools/overlaybd-resize.cpp Outdated
app.add_option("--size", size_str,
"target size (e.g. 8G, 500G, 8192M)")
->required();
app.add_option("--data_dir", data_dir,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove this...
do resize on the default 'upper' in config.v1.json

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 6 out of 6 changed files in this pull request and generated 6 comments.

Comment thread src/tools/overlaybd-resize.cpp Outdated
Comment thread src/tools/overlaybd-resize.cpp Outdated
Comment thread src/tools/overlaybd-resize.cpp Outdated
Comment thread src/tools/CMakeLists.txt Outdated
Comment on lines +34 to +35
if (PHOTON_ENABLE_RESIZE)
set_source_files_properties(
Comment thread src/test/CMakeLists.txt Outdated
Comment on lines +53 to +54
if (PHOTON_ENABLE_RESIZE)
set_source_files_properties(
Comment thread CMake/Findphoton.cmake

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 6 out of 6 changed files in this pull request and generated 6 comments.

Comment thread src/tools/CMakeLists.txt Outdated
Comment on lines +67 to +69
if (PHOTON_ENABLE_RESIZE)
install(TARGETS overlaybd-resize DESTINATION /opt/overlaybd/bin)
endif()
Comment thread CMake/Finde2fs.cmake Outdated
Comment on lines +14 to +15
set(E2FS_RESIZE_DIR ${e2fsprogs_SOURCE_DIR}/build/resize CACHE STRING "path to e2fsprogs resize build dir")
set(E2FS_INSTALL_LIB_DIR ${e2fsprogs_SOURCE_DIR}/build/v1.47.0-opt/lib CACHE STRING "path to e2fsprogs install-libs output")
Comment thread src/tools/overlaybd-resize.cpp Outdated
Comment on lines +172 to +176
if (!parse_upper_paths(config_path, data_path, index_path) || data_path.empty()) {
fprintf(stderr, "error: config has no upper writable layer. "
"overlaybd-resize requires an existing upper layer.\n");
return 1;
}
Comment thread src/tools/overlaybd-resize.cpp Outdated
Comment on lines +45 to +49
auto f = open_localfile_adaptor(data_path.c_str(), O_RDONLY, 0, 0);
if (!f) {
fprintf(stderr, "failed to open data file for reading vsize: %s\n", data_path.c_str());
return -1;
}
Comment thread src/tools/overlaybd-resize.cpp Outdated
Comment on lines +87 to +91
auto f = open_localfile_adaptor(path.c_str(), O_RDWR, 0, 0);
if (!f) {
fprintf(stderr, "failed to open file for vsize update: %s\n", path.c_str());
return -1;
}
Comment thread src/tools/overlaybd-resize.cpp Outdated
Comment on lines +227 to +230
struct stat st;
imgfile->fstat(&st);
printf("Image vsize: %lld MB, target: %llu MB\n",
(long long)st.st_size / 1024 / 1024, target_size / 1024 / 1024);
@OsentryO OsentryO force-pushed the feat/overlaybd-resize branch 2 times, most recently from 0f4b3ff to 73489e0 Compare June 23, 2026 13:13
@OsentryO OsentryO requested a review from Copilot June 23, 2026 13:16

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 9 out of 9 changed files in this pull request and generated 4 comments.

@@ -0,0 +1,102 @@
#include <photon/common/alog.h>
Comment thread src/image_file.cpp
Comment on lines +626 to +630
auto rw = (LSMT::IFileRW *)m_file;
struct stat st;
m_file->fstat(&st);
uint64_t current = (uint64_t)st.st_size;

Comment thread src/image_file.cpp
Comment on lines +659 to +665
if (target_size < current) {
// Shrink: update vsize after filesystem is shrunk
if (rw->update_vsize(target_size) != 0) {
LOG_ERROR("filesystem resized but failed to update vsize. Re-run to retry.");
return -1;
}
}
Comment thread src/CMakeLists.txt Outdated
Comment on lines +40 to +44
${E2FS_RESIZE_DIR}/resize2fs.o
${E2FS_RESIZE_DIR}/extent.o
${E2FS_RESIZE_DIR}/resource_track.o
${E2FS_LIBRARIES}
com_err

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 9 out of 9 changed files in this pull request and generated 4 comments.

Comment thread CMake/Findphoton.cmake
Comment on lines 3 to +5
set(PHOTON_ENABLE_EXTFS ON)
set(PHOTON_ENABLE_RESIZE ON)
add_definitions(-DPHOTON_ENABLE_RESIZE)
}
DEFER({ delete imgservice; });

auto imgfile = (ImageFile *)imgservice->create_image_file(config_path.c_str(), "");
Comment thread src/tools/CMakeLists.txt
Comment on lines +33 to +39
# overlaybd-resize: userspace ext4 resize for overlaybd images
if (NOT ORIGIN_EXT2FS)
add_executable(overlaybd-resize overlaybd-resize.cpp)
target_include_directories(overlaybd-resize PUBLIC ${PHOTON_INCLUDE_DIR})
target_link_libraries(overlaybd-resize photon_static overlaybd_image_lib)
set_target_properties(overlaybd-resize PROPERTIES INSTALL_RPATH "/opt/overlaybd/lib")
endif()
Comment thread src/tools/CMakeLists.txt
Comment on lines +52 to +54
if (NOT ORIGIN_EXT2FS)
install(TARGETS overlaybd-resize DESTINATION /opt/overlaybd/bin)
endif()
@BigVan BigVan merged commit e2637b0 into containerd:main Jun 24, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants