From 402241328c83cb63c01ae1c46c9921a7c84d1533 Mon Sep 17 00:00:00 2001 From: 3405691582 Date: Fri, 18 Sep 2026 18:39:17 +0000 Subject: [PATCH 1/2] Refactor disk space check. No functional change. --- .../assemble_cvd/create_dynamic_disk_files.cc | 63 ++++++++++--------- 1 file changed, 34 insertions(+), 29 deletions(-) diff --git a/base/cvd/cuttlefish/host/commands/assemble_cvd/create_dynamic_disk_files.cc b/base/cvd/cuttlefish/host/commands/assemble_cvd/create_dynamic_disk_files.cc index d33c17d02cd..919ab1bfe17 100644 --- a/base/cvd/cuttlefish/host/commands/assemble_cvd/create_dynamic_disk_files.cc +++ b/base/cvd/cuttlefish/host/commands/assemble_cvd/create_dynamic_disk_files.cc @@ -83,6 +83,39 @@ uint64_t AvailableSpaceAtPath(const std::string& path) { return static_cast(vfs.f_frsize) * vfs.f_bavail; } +Result CheckDataImageSpace( + const CuttlefishConfig::InstanceSpecific& instance) { + // Check if filling in the sparse image would run out of disk space. + std::string data_image = instance.data_image(); + auto existing_sizes = SparseFileSizes(data_image); + if (existing_sizes.sparse_size == 0 && existing_sizes.disk_size == 0) { + data_image = instance.new_data_image(); + existing_sizes = SparseFileSizes(data_image); + CF_EXPECT(existing_sizes.sparse_size > 0 || existing_sizes.disk_size > 0, + "Unable to determine size of \"" << data_image + << "\". Does this file exist?"); + } + if (existing_sizes.sparse_size > 0 || existing_sizes.disk_size > 0) { + auto available_space = AvailableSpaceAtPath(data_image); + if (available_space < + existing_sizes.sparse_size - existing_sizes.disk_size) { + // TODO(schuffelen): Duplicate this check in run_cvd when it can run on + // a separate machine + return CF_ERR("Not enough space remaining in fs containing \"" + << data_image << "\", wanted " + << (existing_sizes.sparse_size - existing_sizes.disk_size) + << ", got " << available_space); + } else { + VLOG(0) << "Available space: " << available_space; + VLOG(0) << "Sparse size of \"" << data_image + << "\": " << existing_sizes.sparse_size; + VLOG(0) << "Disk size of \"" << data_image + << "\": " << existing_sizes.disk_size; + } + } + return {}; +} + Result FindImgZip(const FetcherConfig& fetcher_config, std::string_view system_image_dir) { for (const auto& [member_name, member] : fetcher_config.get_cvd_files()) { @@ -139,35 +172,7 @@ Result CreateDynamicDiskFiles( CF_EXPECT(InitializeSdCard(config, instance)); CF_EXPECT(InitializeDataImage(instance)); CF_EXPECT(InitializePflash(instance)); - - // Check if filling in the sparse image would run out of disk space. - std::string data_image = instance.data_image(); - auto existing_sizes = SparseFileSizes(data_image); - if (existing_sizes.sparse_size == 0 && existing_sizes.disk_size == 0) { - data_image = instance.new_data_image(); - existing_sizes = SparseFileSizes(data_image); - CF_EXPECT(existing_sizes.sparse_size > 0 || existing_sizes.disk_size > 0, - "Unable to determine size of \"" - << data_image << "\". Does this file exist?"); - } - if (existing_sizes.sparse_size > 0 || existing_sizes.disk_size > 0) { - auto available_space = AvailableSpaceAtPath(data_image); - if (available_space < - existing_sizes.sparse_size - existing_sizes.disk_size) { - // TODO(schuffelen): Duplicate this check in run_cvd when it can run on - // a separate machine - return CF_ERR("Not enough space remaining in fs containing \"" - << data_image << "\", wanted " - << (existing_sizes.sparse_size - existing_sizes.disk_size) - << ", got " << available_space); - } else { - VLOG(0) << "Available space: " << available_space; - VLOG(0) << "Sparse size of \"" << data_image - << "\": " << existing_sizes.sparse_size; - VLOG(0) << "Disk size of \"" << data_image - << "\": " << existing_sizes.disk_size; - } - } + CF_EXPECT(CheckDataImageSpace(instance)); CF_EXPECT_LE(instance_index, image_files.size()); const std::vector>& instance_image_files = From 6697a9496dd24d4884740252df5579536da96736 Mon Sep 17 00:00:00 2001 From: 3405691582 Date: Fri, 18 Sep 2026 18:40:14 +0000 Subject: [PATCH 2/2] Skip free space check when overlays are used. Currently, after fetch, disk images are filesystem-sparse, so we check free space available to guard against the case where regular instance usage depletes free space that was not actually reserved, potentially leaving the disk images in an incomplete state. This check is problematic however when overlay images are used. As written, the check is for free space on the filesystem containing the base images, not the overlay images. Worse, since the overlay images need to potentially capture all disk mutations, the free space required needs to potentially balloon to the entire disk size, which is a more difficult limit to mandate. While the same errant state when disk space is depleted may manifest, potential corruption is limited to the overlay only, so instead, just skip the check entirely when overlays are used. --- base/cvd/cuttlefish/host/commands/assemble_cvd/BUILD.bazel | 1 + .../host/commands/assemble_cvd/create_dynamic_disk_files.cc | 5 +++++ 2 files changed, 6 insertions(+) diff --git a/base/cvd/cuttlefish/host/commands/assemble_cvd/BUILD.bazel b/base/cvd/cuttlefish/host/commands/assemble_cvd/BUILD.bazel index d610d2a5422..38d141d5191 100644 --- a/base/cvd/cuttlefish/host/commands/assemble_cvd/BUILD.bazel +++ b/base/cvd/cuttlefish/host/commands/assemble_cvd/BUILD.bazel @@ -208,6 +208,7 @@ cf_cc_library( deps = [ "//cuttlefish/common/libs/utils:files", "//cuttlefish/files:file_exists", + "//cuttlefish/host/commands/assemble_cvd:assemble_cvd_flags", "//cuttlefish/host/commands/assemble_cvd:boot_config", "//cuttlefish/host/commands/assemble_cvd:boot_image_utils", "//cuttlefish/host/commands/assemble_cvd:disk_builder", diff --git a/base/cvd/cuttlefish/host/commands/assemble_cvd/create_dynamic_disk_files.cc b/base/cvd/cuttlefish/host/commands/assemble_cvd/create_dynamic_disk_files.cc index 919ab1bfe17..50364bbbf7e 100644 --- a/base/cvd/cuttlefish/host/commands/assemble_cvd/create_dynamic_disk_files.cc +++ b/base/cvd/cuttlefish/host/commands/assemble_cvd/create_dynamic_disk_files.cc @@ -34,6 +34,7 @@ #include "cuttlefish/common/libs/utils/files.h" #include "cuttlefish/files/file_exists.h" #include "cuttlefish/host/commands/assemble_cvd/android_build/android_builds.h" +#include "cuttlefish/host/commands/assemble_cvd/assemble_cvd_flags.h" #include "cuttlefish/host/commands/assemble_cvd/boot_config.h" #include "cuttlefish/host/commands/assemble_cvd/boot_image_utils.h" #include "cuttlefish/host/commands/assemble_cvd/disk/access_kregistry.h" @@ -85,6 +86,10 @@ uint64_t AvailableSpaceAtPath(const std::string& path) { Result CheckDataImageSpace( const CuttlefishConfig::InstanceSpecific& instance) { + if (FLAGS_use_overlay) { + return {}; + } + // Check if filling in the sparse image would run out of disk space. std::string data_image = instance.data_image(); auto existing_sizes = SparseFileSizes(data_image);