Merge changes from topic "libsnapshot_api_update_engine"

* changes:
  libsnapshot: CreateUpdateSnapshot accepts DeltaArchiveManifest
  libsnapshot: Add snapshot metadata updater.
This commit is contained in:
Yifan Hong 2019-09-17 23:52:19 +00:00 committed by Gerrit Code Review
commit 7509fca99a
13 changed files with 888 additions and 141 deletions

View File

@ -32,6 +32,7 @@ cc_defaults {
"libfs_mgr",
"libfstab",
"liblp",
"update_metadata-protos",
],
whole_static_libs: [
"libext2_uuid",
@ -41,6 +42,9 @@ cc_defaults {
header_libs: [
"libfiemap_headers",
],
export_static_lib_headers: [
"update_metadata-protos",
],
export_header_lib_headers: [
"libfiemap_headers",
],
@ -51,6 +55,7 @@ filegroup {
name: "libsnapshot_sources",
srcs: [
"snapshot.cpp",
"snapshot_metadata_updater.cpp",
"partition_cow_creator.cpp",
"utility.cpp",
],
@ -87,10 +92,12 @@ cc_test {
srcs: [
"snapshot_test.cpp",
"partition_cow_creator_test.cpp",
"snapshot_metadata_updater_test.cpp",
"test_helpers.cpp",
],
shared_libs: [
"libbinder",
"libprotobuf-cpp-lite",
"libutils",
],
static_libs: [

View File

@ -29,6 +29,7 @@
#include <libfiemap/image_manager.h>
#include <liblp/builder.h>
#include <liblp/liblp.h>
#include <update_engine/update_metadata.pb.h>
#ifndef FRIEND_TEST
#define FRIEND_TEST(test_set_name, individual_test) \
@ -89,6 +90,7 @@ class SnapshotManager final {
using IPartitionOpener = android::fs_mgr::IPartitionOpener;
using LpMetadata = android::fs_mgr::LpMetadata;
using MetadataBuilder = android::fs_mgr::MetadataBuilder;
using DeltaArchiveManifest = chromeos_update_engine::DeltaArchiveManifest;
public:
// Dependency injection for testing.
@ -98,6 +100,7 @@ class SnapshotManager final {
virtual std::string GetGsidDir() const = 0;
virtual std::string GetMetadataDir() const = 0;
virtual std::string GetSlotSuffix() const = 0;
virtual std::string GetOtherSlotSuffix() const = 0;
virtual std::string GetSuperDevice(uint32_t slot) const = 0;
virtual const IPartitionOpener& GetPartitionOpener() const = 0;
virtual bool IsOverlayfsSetup() const = 0;
@ -169,9 +172,7 @@ class SnapshotManager final {
// Create necessary COW device / files for OTA clients. New logical partitions will be added to
// group "cow" in target_metadata. Regions of partitions of current_metadata will be
// "write-protected" and snapshotted.
bool CreateUpdateSnapshots(MetadataBuilder* target_metadata, const std::string& target_suffix,
MetadataBuilder* current_metadata, const std::string& current_suffix,
const std::map<std::string, uint64_t>& cow_sizes);
bool CreateUpdateSnapshots(const DeltaArchiveManifest& manifest);
// Map a snapshotted partition for OTA clients to write to. Write-protected regions are
// determined previously in CreateSnapshots.

View File

@ -25,6 +25,9 @@ using android::fs_mgr::Extent;
using android::fs_mgr::Interval;
using android::fs_mgr::kDefaultBlockSize;
using android::fs_mgr::Partition;
using chromeos_update_engine::InstallOperation;
template <typename T>
using RepeatedPtrField = google::protobuf::RepeatedPtrField<T>;
namespace android {
namespace snapshot {
@ -117,9 +120,15 @@ std::optional<uint64_t> PartitionCowCreator::GetSnapshotSize() {
return snapshot_size;
}
std::optional<PartitionCowCreator::Return> PartitionCowCreator::Run() {
std::optional<uint64_t> PartitionCowCreator::GetCowSize(uint64_t snapshot_size) {
// TODO: Use |operations|. to determine a minimum COW size.
// kCowEstimateFactor is good for prototyping but we can't use that in production.
static constexpr double kCowEstimateFactor = 1.05;
auto cow_size = RoundUp(snapshot_size * kCowEstimateFactor, kDefaultBlockSize);
return cow_size;
}
std::optional<PartitionCowCreator::Return> PartitionCowCreator::Run() {
CHECK(current_metadata->GetBlockDevicePartitionName(0) == LP_METADATA_DEFAULT_PARTITION_NAME &&
target_metadata->GetBlockDevicePartitionName(0) == LP_METADATA_DEFAULT_PARTITION_NAME);
@ -135,13 +144,8 @@ std::optional<PartitionCowCreator::Return> PartitionCowCreator::Run() {
ret.snapshot_status.snapshot_size = *snapshot_size;
// TODO: always read from cow_size when the COW size is written in
// update package. kCowEstimateFactor is good for prototyping but
// we can't use that in production.
if (!cow_size.has_value()) {
cow_size =
RoundUp(ret.snapshot_status.snapshot_size * kCowEstimateFactor, kDefaultBlockSize);
}
auto cow_size = GetCowSize(*snapshot_size);
if (!cow_size.has_value()) return std::nullopt;
// Compute regions that are free in both current and target metadata. These are the regions
// we can use for COW partition.

View File

@ -32,20 +32,23 @@ struct PartitionCowCreator {
using Interval = android::fs_mgr::Interval;
using MetadataBuilder = android::fs_mgr::MetadataBuilder;
using Partition = android::fs_mgr::Partition;
using InstallOperation = chromeos_update_engine::InstallOperation;
template <typename T>
using RepeatedPtrField = google::protobuf::RepeatedPtrField<T>;
// The metadata that will be written to target metadata slot.
MetadataBuilder* target_metadata;
MetadataBuilder* target_metadata = nullptr;
// The suffix of the target slot.
std::string target_suffix;
// The partition in target_metadata that needs to be snapshotted.
Partition* target_partition;
Partition* target_partition = nullptr;
// The metadata at the current slot (that would be used if the device boots
// normally). This is used to determine which extents are being used.
MetadataBuilder* current_metadata;
MetadataBuilder* current_metadata = nullptr;
// The suffix of the current slot.
std::string current_suffix;
// The COW size given by client code.
std::optional<uint64_t> cow_size;
// List of operations to be applied on the partition.
const RepeatedPtrField<InstallOperation>* operations = nullptr;
struct Return {
SnapshotManager::SnapshotStatus snapshot_status;
@ -57,6 +60,7 @@ struct PartitionCowCreator {
private:
bool HasExtent(Partition* p, Extent* e);
std::optional<uint64_t> GetSnapshotSize();
std::optional<uint64_t> GetCowSize(uint64_t snapshot_size);
};
} // namespace snapshot

View File

@ -76,14 +76,11 @@ TEST(PartitionCowCreator, IntersectSelf) {
.target_suffix = "_b",
.target_partition = system_b,
.current_metadata = builder_a.get(),
.current_suffix = "_a",
.cow_size = 20 * 1024};
.current_suffix = "_a"};
auto ret = creator.Run();
ASSERT_TRUE(ret.has_value());
ASSERT_EQ(40 * 1024, ret->snapshot_status.device_size);
ASSERT_EQ(40 * 1024, ret->snapshot_status.snapshot_size);
ASSERT_EQ(20 * 1024,
ret->snapshot_status.cow_file_size + ret->snapshot_status.cow_partition_size);
}
} // namespace snapshot

View File

@ -39,6 +39,7 @@
#include <liblp/liblp.h>
#include "partition_cow_creator.h"
#include "snapshot_metadata_updater.h"
#include "utility.h"
namespace android {
@ -61,6 +62,10 @@ using android::fs_mgr::GetPartitionName;
using android::fs_mgr::LpMetadata;
using android::fs_mgr::MetadataBuilder;
using android::fs_mgr::SlotNumberForSlotSuffix;
using chromeos_update_engine::DeltaArchiveManifest;
using chromeos_update_engine::InstallOperation;
template <typename T>
using RepeatedPtrField = google::protobuf::RepeatedPtrField<T>;
using std::chrono::duration_cast;
using namespace std::chrono_literals;
using namespace std::string_literals;
@ -74,6 +79,7 @@ class DeviceInfo final : public SnapshotManager::IDeviceInfo {
std::string GetGsidDir() const override { return "ota"s; }
std::string GetMetadataDir() const override { return "/metadata/ota"s; }
std::string GetSlotSuffix() const override { return fs_mgr_get_slot_suffix(); }
std::string GetOtherSlotSuffix() const override { return fs_mgr_get_other_slot_suffix(); }
const android::fs_mgr::IPartitionOpener& GetPartitionOpener() const { return opener_; }
std::string GetSuperDevice(uint32_t slot) const override {
return fs_mgr_get_super_partition_name(slot);
@ -1713,26 +1719,44 @@ bool SnapshotManager::ForceLocalImageManager() {
return true;
}
bool SnapshotManager::CreateUpdateSnapshots(MetadataBuilder* target_metadata,
const std::string& target_suffix,
MetadataBuilder* current_metadata,
const std::string& current_suffix,
const std::map<std::string, uint64_t>& cow_sizes) {
bool SnapshotManager::CreateUpdateSnapshots(const DeltaArchiveManifest& manifest) {
auto lock = LockExclusive();
if (!lock) return false;
// Add _{target_suffix} to COW size map.
std::map<std::string, uint64_t> suffixed_cow_sizes;
for (const auto& [name, size] : cow_sizes) {
suffixed_cow_sizes[name + target_suffix] = size;
const auto& opener = device_->GetPartitionOpener();
auto current_suffix = device_->GetSlotSuffix();
uint32_t current_slot = SlotNumberForSlotSuffix(current_suffix);
auto target_suffix = device_->GetOtherSlotSuffix();
uint32_t target_slot = SlotNumberForSlotSuffix(target_suffix);
auto current_super = device_->GetSuperDevice(current_slot);
auto current_metadata = MetadataBuilder::New(opener, current_super, current_slot);
auto target_metadata =
MetadataBuilder::NewForUpdate(opener, current_super, current_slot, target_slot);
SnapshotMetadataUpdater metadata_updater(target_metadata.get(), target_slot, manifest);
if (!metadata_updater.Update()) {
LOG(ERROR) << "Cannot calculate new metadata.";
return false;
}
target_metadata->RemoveGroupAndPartitions(kCowGroupName);
if (!target_metadata->AddGroup(kCowGroupName, 0)) {
LOG(ERROR) << "Cannot add group " << kCowGroupName;
return false;
}
std::map<std::string, const RepeatedPtrField<InstallOperation>*> install_operation_map;
for (const auto& partition_update : manifest.partitions()) {
auto suffixed_name = partition_update.partition_name() + target_suffix;
auto&& [it, inserted] = install_operation_map.emplace(std::move(suffixed_name),
&partition_update.operations());
if (!inserted) {
LOG(ERROR) << "Duplicated partition " << partition_update.partition_name()
<< " in update manifest.";
return false;
}
}
// TODO(b/134949511): remove this check. Right now, with overlayfs mounted, the scratch
// partition takes up a big chunk of space in super, causing COW images to be created on
// retrofit Virtual A/B devices.
@ -1757,18 +1781,16 @@ bool SnapshotManager::CreateUpdateSnapshots(MetadataBuilder* target_metadata,
// these devices.
AutoDeviceList created_devices;
for (auto* target_partition : ListPartitionsWithSuffix(target_metadata, target_suffix)) {
std::optional<uint64_t> cow_size = std::nullopt;
auto it = suffixed_cow_sizes.find(target_partition->name());
if (it != suffixed_cow_sizes.end()) {
cow_size = it->second;
LOG(INFO) << "Using provided COW size " << *cow_size << " for partition "
<< target_partition->name();
for (auto* target_partition : ListPartitionsWithSuffix(target_metadata.get(), target_suffix)) {
const RepeatedPtrField<InstallOperation>* operations = nullptr;
auto operations_it = install_operation_map.find(target_partition->name());
if (operations_it != install_operation_map.end()) {
operations = operations_it->second;
}
// Compute the device sizes for the partition.
PartitionCowCreator cow_creator{target_metadata, target_suffix, target_partition,
current_metadata, current_suffix, cow_size};
PartitionCowCreator cow_creator{target_metadata.get(), target_suffix, target_partition,
current_metadata.get(), current_suffix, operations};
auto cow_creator_ret = cow_creator.Run();
if (!cow_creator_ret.has_value()) {
return false;
@ -1846,13 +1868,17 @@ bool SnapshotManager::CreateUpdateSnapshots(MetadataBuilder* target_metadata,
auto& dm = DeviceMapper::Instance();
auto exported_target_metadata = target_metadata->Export();
if (exported_target_metadata == nullptr) {
LOG(ERROR) << "Cannot export target metadata";
return false;
}
CreateLogicalPartitionParams cow_params{
.block_device = LP_METADATA_DEFAULT_PARTITION_NAME,
.metadata = exported_target_metadata.get(),
.timeout_ms = std::chrono::milliseconds::max(),
.partition_opener = &device_->GetPartitionOpener(),
};
for (auto* target_partition : ListPartitionsWithSuffix(target_metadata, target_suffix)) {
for (auto* target_partition : ListPartitionsWithSuffix(target_metadata.get(), target_suffix)) {
AutoDeviceList created_devices_for_cow;
if (!UnmapPartitionWithSnapshot(lock.get(), target_partition->name())) {
@ -1884,6 +1910,12 @@ bool SnapshotManager::CreateUpdateSnapshots(MetadataBuilder* target_metadata,
// Let destructor of created_devices_for_cow to unmap the COW devices.
};
if (!UpdatePartitionTable(opener, device_->GetSuperDevice(target_slot),
*exported_target_metadata, target_slot)) {
LOG(ERROR) << "Cannot write target metadata";
return false;
}
created_devices.Release();
LOG(INFO) << "Successfully created all snapshots for target slot " << target_suffix;

View File

@ -0,0 +1,273 @@
//
// Copyright (C) 2019 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#include "snapshot_metadata_updater.h"
#include <algorithm>
#include <map>
#include <optional>
#include <set>
#include <string>
#include <string_view>
#include <vector>
#include <android-base/logging.h>
#include <android-base/strings.h>
#include <fs_mgr.h>
#include <libsnapshot/snapshot.h>
using android::fs_mgr::MetadataBuilder;
using android::fs_mgr::Partition;
using android::fs_mgr::SlotSuffixForSlotNumber;
using chromeos_update_engine::DeltaArchiveManifest;
namespace android {
namespace snapshot {
SnapshotMetadataUpdater::SnapshotMetadataUpdater(MetadataBuilder* builder, uint32_t target_slot,
const DeltaArchiveManifest& manifest)
: builder_(builder), target_suffix_(SlotSuffixForSlotNumber(target_slot)) {
if (!manifest.has_dynamic_partition_metadata()) {
return;
}
// Key: partition name ("system"). Value: group name ("group").
// No suffix.
std::map<std::string_view, std::string_view> partition_group_map;
const auto& metadata_groups = manifest.dynamic_partition_metadata().groups();
groups_.reserve(metadata_groups.size());
for (const auto& group : metadata_groups) {
groups_.emplace_back(Group{group.name() + target_suffix_, &group});
for (const auto& partition_name : group.partition_names()) {
partition_group_map[partition_name] = group.name();
}
}
for (const auto& p : manifest.partitions()) {
auto it = partition_group_map.find(p.partition_name());
if (it != partition_group_map.end()) {
partitions_.emplace_back(Partition{p.partition_name() + target_suffix_,
std::string(it->second) + target_suffix_, &p});
}
}
}
bool SnapshotMetadataUpdater::ShrinkPartitions() const {
for (const auto& partition_update : partitions_) {
auto* existing_partition = builder_->FindPartition(partition_update.name);
if (existing_partition == nullptr) {
continue;
}
auto new_size = partition_update->new_partition_info().size();
if (existing_partition->size() <= new_size) {
continue;
}
if (!builder_->ResizePartition(existing_partition, new_size)) {
return false;
}
}
return true;
}
bool SnapshotMetadataUpdater::DeletePartitions() const {
std::vector<std::string> partitions_to_delete;
// Don't delete partitions in groups where the group name doesn't have target_suffix,
// e.g. default.
for (auto* existing_partition : ListPartitionsWithSuffix(builder_, target_suffix_)) {
auto iter = std::find_if(partitions_.begin(), partitions_.end(),
[existing_partition](auto&& partition_update) {
return partition_update.name == existing_partition->name();
});
// Update package metadata doesn't have this partition. Prepare to delete it.
// Not deleting from builder_ yet because it may break ListPartitionsWithSuffix if it were
// to return an iterable view of builder_.
if (iter == partitions_.end()) {
partitions_to_delete.push_back(existing_partition->name());
}
}
for (const auto& partition_name : partitions_to_delete) {
builder_->RemovePartition(partition_name);
}
return true;
}
bool SnapshotMetadataUpdater::MovePartitionsToDefault() const {
for (const auto& partition_update : partitions_) {
auto* existing_partition = builder_->FindPartition(partition_update.name);
if (existing_partition == nullptr) {
continue;
}
if (existing_partition->group_name() == partition_update.group_name) {
continue;
}
// Move to "default" group (which doesn't have maximum size constraint)
// temporarily.
if (!builder_->ChangePartitionGroup(existing_partition, android::fs_mgr::kDefaultGroup)) {
return false;
}
}
return true;
}
bool SnapshotMetadataUpdater::ShrinkGroups() const {
for (const auto& group_update : groups_) {
auto* existing_group = builder_->FindGroup(group_update.name);
if (existing_group == nullptr) {
continue;
}
if (existing_group->maximum_size() <= group_update->size()) {
continue;
}
if (!builder_->ChangeGroupSize(existing_group->name(), group_update->size())) {
return false;
}
}
return true;
}
bool SnapshotMetadataUpdater::DeleteGroups() const {
std::vector<std::string> existing_groups = builder_->ListGroups();
for (const auto& existing_group_name : existing_groups) {
// Don't delete groups without target suffix, e.g. default.
if (!android::base::EndsWith(existing_group_name, target_suffix_)) {
continue;
}
auto iter = std::find_if(groups_.begin(), groups_.end(),
[&existing_group_name](auto&& group_update) {
return group_update.name == existing_group_name;
});
// Update package metadata has this group as well, so not deleting it.
if (iter != groups_.end()) {
continue;
}
// Update package metadata doesn't have this group. Before deleting it, sanity check that it
// doesn't have any partitions left. Update metadata shouldn't assign any partitions to this
// group, so all partitions that originally belong to this group should be moved by
// MovePartitionsToDefault at this point.
auto existing_partitions_in_group = builder_->ListPartitionsInGroup(existing_group_name);
if (!existing_partitions_in_group.empty()) {
std::vector<std::string> partition_names_in_group;
std::transform(existing_partitions_in_group.begin(), existing_partitions_in_group.end(),
std::back_inserter(partition_names_in_group),
[](auto* p) { return p->name(); });
LOG(ERROR)
<< "Group " << existing_group_name
<< " cannot be deleted because the following partitions are left unassigned: ["
<< android::base::Join(partition_names_in_group, ",") << "]";
return false;
}
builder_->RemoveGroupAndPartitions(existing_group_name);
}
return true;
}
bool SnapshotMetadataUpdater::AddGroups() const {
for (const auto& group_update : groups_) {
if (builder_->FindGroup(group_update.name) == nullptr) {
if (!builder_->AddGroup(group_update.name, group_update->size())) {
return false;
}
}
}
return true;
}
bool SnapshotMetadataUpdater::GrowGroups() const {
for (const auto& group_update : groups_) {
auto* existing_group = builder_->FindGroup(group_update.name);
if (existing_group == nullptr) {
continue;
}
if (existing_group->maximum_size() >= group_update->size()) {
continue;
}
if (!builder_->ChangeGroupSize(existing_group->name(), group_update->size())) {
return false;
}
}
return true;
}
bool SnapshotMetadataUpdater::AddPartitions() const {
for (const auto& partition_update : partitions_) {
if (builder_->FindPartition(partition_update.name) == nullptr) {
auto* p =
builder_->AddPartition(partition_update.name, partition_update.group_name,
LP_PARTITION_ATTR_READONLY | LP_PARTITION_ATTR_UPDATED);
if (p == nullptr) {
return false;
}
}
}
// Will be resized in GrowPartitions.
return true;
}
bool SnapshotMetadataUpdater::GrowPartitions() const {
for (const auto& partition_update : partitions_) {
auto* existing_partition = builder_->FindPartition(partition_update.name);
if (existing_partition == nullptr) {
continue;
}
auto new_size = partition_update->new_partition_info().size();
if (existing_partition->size() >= new_size) {
continue;
}
if (!builder_->ResizePartition(existing_partition, new_size)) {
return false;
}
}
return true;
}
bool SnapshotMetadataUpdater::MovePartitionsToCorrectGroup() const {
for (const auto& partition_update : partitions_) {
auto* existing_partition = builder_->FindPartition(partition_update.name);
if (existing_partition == nullptr) {
continue;
}
if (existing_partition->group_name() == partition_update.group_name) {
continue;
}
if (!builder_->ChangePartitionGroup(existing_partition, partition_update.group_name)) {
return false;
}
}
return true;
}
bool SnapshotMetadataUpdater::Update() const {
// Remove extents used by COW devices by removing the COW group completely.
builder_->RemoveGroupAndPartitions(android::snapshot::kCowGroupName);
// The order of these operations are important so that we
// always have enough space to grow or add new partitions / groups.
// clang-format off
return ShrinkPartitions() &&
DeletePartitions() &&
MovePartitionsToDefault() &&
ShrinkGroups() &&
DeleteGroups() &&
AddGroups() &&
GrowGroups() &&
AddPartitions() &&
GrowPartitions() &&
MovePartitionsToCorrectGroup();
// clang-format on
}
} // namespace snapshot
} // namespace android

View File

@ -0,0 +1,85 @@
//
// Copyright (C) 2019 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#pragma once
#include <stdint.h>
#include <string>
#include <vector>
#include <liblp/builder.h>
#include <update_engine/update_metadata.pb.h>
#include "utility.h"
namespace android {
namespace snapshot {
// Helper class that modifies a super partition metadata for an update for
// Virtual A/B devices.
class SnapshotMetadataUpdater {
using DeltaArchiveManifest = chromeos_update_engine::DeltaArchiveManifest;
using DynamicPartitionMetadata = chromeos_update_engine::DynamicPartitionMetadata;
using DynamicPartitionGroup = chromeos_update_engine::DynamicPartitionGroup;
using PartitionUpdate = chromeos_update_engine::PartitionUpdate;
public:
// Caller is responsible for ensuring the lifetime of manifest to be longer
// than SnapshotMetadataUpdater.
SnapshotMetadataUpdater(android::fs_mgr::MetadataBuilder* builder, uint32_t target_slot,
const DeltaArchiveManifest& manifest);
bool Update() const;
private:
bool RenameGroupSuffix() const;
bool ShrinkPartitions() const;
bool DeletePartitions() const;
bool MovePartitionsToDefault() const;
bool ShrinkGroups() const;
bool DeleteGroups() const;
bool AddGroups() const;
bool GrowGroups() const;
bool AddPartitions() const;
bool GrowPartitions() const;
bool MovePartitionsToCorrectGroup() const;
// Wraps a DynamicPartitionGroup with a slot-suffixed name. Always use
// .name instead of ->name() because .name has the slot suffix (e.g.
// .name is "group_b" and ->name() is "group".)
struct Group {
std::string name;
const DynamicPartitionGroup* group;
const DynamicPartitionGroup* operator->() const { return group; }
};
// Wraps a PartitionUpdate with a slot-suffixed name / group name. Always use
// .name instead of ->partition_name() because .name has the slot suffix (e.g.
// .name is "system_b" and ->partition_name() is "system".)
struct Partition {
std::string name;
std::string group_name;
const PartitionUpdate* partition;
const PartitionUpdate* operator->() const { return partition; }
};
android::fs_mgr::MetadataBuilder* const builder_;
const std::string target_suffix_;
std::vector<Group> groups_;
std::vector<Partition> partitions_;
};
} // namespace snapshot
} // namespace android

View File

@ -0,0 +1,328 @@
//
// Copyright (C) 2019 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#include "snapshot_metadata_updater.h"
#include <memory>
#include <string>
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <liblp/builder.h>
#include <storage_literals/storage_literals.h>
#include "test_helpers.h"
using namespace android::storage_literals;
using android::fs_mgr::LpMetadata;
using android::fs_mgr::MetadataBuilder;
using android::fs_mgr::SlotSuffixForSlotNumber;
using chromeos_update_engine::DeltaArchiveManifest;
using chromeos_update_engine::DynamicPartitionGroup;
using chromeos_update_engine::PartitionUpdate;
using testing::AssertionFailure;
using testing::AssertionResult;
using testing::AssertionSuccess;
namespace android {
namespace snapshot {
class SnapshotMetadataUpdaterTest : public ::testing::TestWithParam<uint32_t> {
public:
void SetUp() override {
target_slot_ = GetParam();
target_suffix_ = SlotSuffixForSlotNumber(target_slot_);
builder_ = MetadataBuilder::New(4_GiB + 1_MiB, 4_KiB, 2);
group_ = manifest_.mutable_dynamic_partition_metadata()->add_groups();
group_->set_name("group");
group_->set_size(4_GiB);
group_->add_partition_names("system");
group_->add_partition_names("vendor");
system_ = manifest_.add_partitions();
system_->set_partition_name("system");
SetSize(system_, 2_GiB);
vendor_ = manifest_.add_partitions();
vendor_->set_partition_name("vendor");
SetSize(vendor_, 1_GiB);
ASSERT_TRUE(FillFakeMetadata(builder_.get(), manifest_, target_suffix_));
}
// Append suffix to name.
std::string T(std::string_view name) { return std::string(name) + target_suffix_; }
AssertionResult UpdateAndExport() {
SnapshotMetadataUpdater updater(builder_.get(), target_slot_, manifest_);
if (!updater.Update()) {
return AssertionFailure() << "Update failed.";
}
exported_ = builder_->Export();
if (exported_ == nullptr) {
return AssertionFailure() << "Export failed.";
}
return AssertionSuccess();
}
// Check that in |builder_|, partition |name| + |target_suffix_| has the given |size|.
AssertionResult CheckSize(std::string_view name, uint64_t size) {
auto p = builder_->FindPartition(T(name));
if (p == nullptr) {
return AssertionFailure() << "Cannot find partition " << T(name);
}
if (p->size() != size) {
return AssertionFailure() << "Partition " << T(name) << " should be " << size
<< " bytes, but is " << p->size() << " bytes.";
}
return AssertionSuccess() << "Partition" << T(name) << " is " << size << " bytes.";
}
// Check that in |builder_|, group |name| + |target_suffix_| has the given |size|.
AssertionResult CheckGroupSize(std::string_view name, uint64_t size) {
auto g = builder_->FindGroup(T(name));
if (g == nullptr) {
return AssertionFailure() << "Cannot find group " << T(name);
}
if (g->maximum_size() != size) {
return AssertionFailure() << "Group " << T(name) << " should be " << size
<< " bytes, but is " << g->maximum_size() << " bytes.";
}
return AssertionSuccess() << "Group" << T(name) << " is " << size << " bytes.";
}
// Check that in |builder_|, partition |partition_name| + |target_suffix_| is in group
// |group_name| + |target_suffix_|;
AssertionResult CheckGroupName(std::string_view partition_name, std::string_view group_name) {
auto p = builder_->FindPartition(T(partition_name));
if (p == nullptr) {
return AssertionFailure() << "Cannot find partition " << T(partition_name);
}
if (p->group_name() != T(group_name)) {
return AssertionFailure() << "Partition " << T(partition_name) << " should be in "
<< T(group_name) << ", but is in " << p->group_name() << ".";
}
return AssertionSuccess() << "Partition" << T(partition_name) << " is in " << T(group_name)
<< ".";
}
std::unique_ptr<MetadataBuilder> builder_;
uint32_t target_slot_;
std::string target_suffix_;
DeltaArchiveManifest manifest_;
std::unique_ptr<LpMetadata> exported_;
DynamicPartitionGroup* group_ = nullptr;
PartitionUpdate* system_ = nullptr;
PartitionUpdate* vendor_ = nullptr;
};
TEST_P(SnapshotMetadataUpdaterTest, NoChange) {
EXPECT_TRUE(UpdateAndExport());
EXPECT_TRUE(CheckGroupSize("group", 4_GiB));
EXPECT_TRUE(CheckSize("system", 2_GiB));
EXPECT_TRUE(CheckGroupName("system", "group"));
EXPECT_TRUE(CheckSize("vendor", 1_GiB));
EXPECT_TRUE(CheckGroupName("vendor", "group"));
}
TEST_P(SnapshotMetadataUpdaterTest, GrowWithinBounds) {
SetSize(system_, 2_GiB + 512_MiB);
SetSize(vendor_, 1_GiB + 512_MiB);
ASSERT_TRUE(UpdateAndExport());
EXPECT_TRUE(CheckSize("system", 2_GiB + 512_MiB));
EXPECT_TRUE(CheckSize("vendor", 1_GiB + 512_MiB));
}
TEST_P(SnapshotMetadataUpdaterTest, GrowOverSuper) {
SetSize(system_, 3_GiB);
SetSize(vendor_, 1_GiB + 512_MiB);
EXPECT_FALSE(UpdateAndExport());
}
TEST_P(SnapshotMetadataUpdaterTest, GrowOverGroup) {
SetSize(system_, 3_GiB);
SetSize(vendor_, 1_GiB + 4_KiB);
EXPECT_FALSE(UpdateAndExport());
}
TEST_P(SnapshotMetadataUpdaterTest, Add) {
group_->add_partition_names("product");
auto product = manifest_.add_partitions();
product->set_partition_name("product");
SetSize(product, 1_GiB);
EXPECT_TRUE(UpdateAndExport());
EXPECT_TRUE(CheckSize("system", 2_GiB));
EXPECT_TRUE(CheckSize("vendor", 1_GiB));
EXPECT_TRUE(CheckSize("product", 1_GiB));
}
TEST_P(SnapshotMetadataUpdaterTest, AddTooBig) {
group_->add_partition_names("product");
auto product = manifest_.add_partitions();
product->set_partition_name("product");
SetSize(product, 1_GiB + 4_KiB);
EXPECT_FALSE(UpdateAndExport());
}
TEST_P(SnapshotMetadataUpdaterTest, ShrinkAll) {
SetSize(system_, 1_GiB);
SetSize(vendor_, 512_MiB);
ASSERT_TRUE(UpdateAndExport());
EXPECT_TRUE(CheckSize("system", 1_GiB));
EXPECT_TRUE(CheckSize("vendor", 512_MiB));
}
TEST_P(SnapshotMetadataUpdaterTest, ShrinkAndGrow) {
SetSize(system_, 3_GiB + 512_MiB);
SetSize(vendor_, 512_MiB);
ASSERT_TRUE(UpdateAndExport());
EXPECT_TRUE(CheckSize("system", 3_GiB + 512_MiB));
EXPECT_TRUE(CheckSize("vendor", 512_MiB));
}
TEST_P(SnapshotMetadataUpdaterTest, ShrinkAndAdd) {
SetSize(system_, 2_GiB);
SetSize(vendor_, 512_MiB);
group_->add_partition_names("product");
auto product = manifest_.add_partitions();
product->set_partition_name("product");
SetSize(product, 1_GiB + 512_MiB);
ASSERT_TRUE(UpdateAndExport());
EXPECT_TRUE(CheckSize("system", 2_GiB));
EXPECT_TRUE(CheckSize("vendor", 512_MiB));
EXPECT_TRUE(CheckSize("product", 1_GiB + 512_MiB));
}
TEST_P(SnapshotMetadataUpdaterTest, Delete) {
group_->mutable_partition_names()->RemoveLast();
// No need to delete it from manifest.partitions as SnapshotMetadataUpdater
// should ignore them (treat them as static partitions).
EXPECT_TRUE(UpdateAndExport());
EXPECT_TRUE(CheckSize("system", 2_GiB));
EXPECT_EQ(nullptr, builder_->FindPartition(T("vendor")));
}
TEST_P(SnapshotMetadataUpdaterTest, DeleteAndGrow) {
group_->mutable_partition_names()->RemoveLast();
SetSize(system_, 4_GiB);
EXPECT_TRUE(UpdateAndExport());
EXPECT_TRUE(CheckSize("system", 4_GiB));
}
TEST_P(SnapshotMetadataUpdaterTest, DeleteAndAdd) {
group_->mutable_partition_names()->RemoveLast();
group_->add_partition_names("product");
auto product = manifest_.add_partitions();
product->set_partition_name("product");
SetSize(product, 2_GiB);
EXPECT_TRUE(UpdateAndExport());
EXPECT_TRUE(CheckSize("system", 2_GiB));
EXPECT_EQ(nullptr, builder_->FindPartition(T("vendor")));
EXPECT_TRUE(CheckSize("product", 2_GiB));
}
TEST_P(SnapshotMetadataUpdaterTest, GrowGroup) {
group_->set_size(4_GiB + 512_KiB);
SetSize(system_, 2_GiB + 256_KiB);
SetSize(vendor_, 2_GiB + 256_KiB);
EXPECT_TRUE(UpdateAndExport());
EXPECT_TRUE(CheckSize("system", 2_GiB + 256_KiB));
EXPECT_TRUE(CheckSize("vendor", 2_GiB + 256_KiB));
}
TEST_P(SnapshotMetadataUpdaterTest, ShrinkGroup) {
group_->set_size(1_GiB);
SetSize(system_, 512_MiB);
SetSize(vendor_, 512_MiB);
EXPECT_TRUE(UpdateAndExport());
EXPECT_TRUE(CheckSize("system", 512_MiB));
EXPECT_TRUE(CheckSize("vendor", 512_MiB));
}
TEST_P(SnapshotMetadataUpdaterTest, MoveToNewGroup) {
group_->mutable_partition_names()->RemoveLast();
group_->set_size(2_GiB);
auto another_group = manifest_.mutable_dynamic_partition_metadata()->add_groups();
another_group->set_name("another_group");
another_group->set_size(2_GiB);
another_group->add_partition_names("vendor");
SetSize(vendor_, 2_GiB);
EXPECT_TRUE(UpdateAndExport());
EXPECT_TRUE(CheckGroupSize("group", 2_GiB));
EXPECT_TRUE(CheckGroupSize("another_group", 2_GiB));
EXPECT_TRUE(CheckSize("system", 2_GiB));
EXPECT_TRUE(CheckGroupName("system", "group"));
EXPECT_TRUE(CheckSize("vendor", 2_GiB));
EXPECT_TRUE(CheckGroupName("vendor", "another_group"));
}
TEST_P(SnapshotMetadataUpdaterTest, DeleteAndAddGroup) {
manifest_.mutable_dynamic_partition_metadata()->mutable_groups()->RemoveLast();
group_ = nullptr;
auto another_group = manifest_.mutable_dynamic_partition_metadata()->add_groups();
another_group->set_name("another_group");
another_group->set_size(4_GiB);
another_group->add_partition_names("system");
another_group->add_partition_names("vendor");
another_group->add_partition_names("product");
auto product = manifest_.add_partitions();
product->set_partition_name("product");
SetSize(product, 1_GiB);
EXPECT_TRUE(UpdateAndExport());
EXPECT_EQ(nullptr, builder_->FindGroup(T("group")));
EXPECT_TRUE(CheckGroupSize("another_group", 4_GiB));
EXPECT_TRUE(CheckSize("system", 2_GiB));
EXPECT_TRUE(CheckGroupName("system", "another_group"));
EXPECT_TRUE(CheckSize("vendor", 1_GiB));
EXPECT_TRUE(CheckGroupName("vendor", "another_group"));
EXPECT_TRUE(CheckSize("product", 1_GiB));
EXPECT_TRUE(CheckGroupName("product", "another_group"));
}
INSTANTIATE_TEST_SUITE_P(, SnapshotMetadataUpdaterTest, testing::Values(0, 1));
} // namespace snapshot
} // namespace android

View File

@ -50,6 +50,8 @@ using android::fs_mgr::DestroyLogicalPartition;
using android::fs_mgr::GetPartitionGroupName;
using android::fs_mgr::GetPartitionName;
using android::fs_mgr::MetadataBuilder;
using chromeos_update_engine::DeltaArchiveManifest;
using chromeos_update_engine::PartitionUpdate;
using namespace ::testing;
using namespace android::fs_mgr::testing;
using namespace android::storage_literals;
@ -62,7 +64,8 @@ std::unique_ptr<SnapshotManager> sm;
TestDeviceInfo* test_device = nullptr;
std::string fake_super;
static constexpr uint64_t kSuperSize = 16_MiB;
static constexpr uint64_t kSuperSize = 16_MiB + 4_KiB;
static constexpr uint64_t kGroupSize = 16_MiB;
class SnapshotTest : public ::testing::Test {
public:
@ -636,28 +639,39 @@ class SnapshotUpdateTest : public SnapshotTest {
ON_CALL(*GetMockedPropertyFetcher(), GetBoolProperty("ro.virtual_ab.enabled", _))
.WillByDefault(Return(true));
opener = std::make_unique<TestPartitionOpener>(fake_super);
opener_ = std::make_unique<TestPartitionOpener>(fake_super);
// Initialize source partition metadata. sys_b is similar to system_other.
// Create a fake update package metadata.
// Not using full name "system", "vendor", "product" because these names collide with the
// mapped partitions on the running device.
src = MetadataBuilder::New(*opener, "super", 0);
ASSERT_NE(nullptr, src);
auto partition = src->AddPartition("sys_a", 0);
// Each test modifies manifest_ slightly to indicate changes to the partition layout.
auto group = manifest_.mutable_dynamic_partition_metadata()->add_groups();
group->set_name("group");
group->set_size(kGroupSize);
group->add_partition_names("sys");
group->add_partition_names("vnd");
group->add_partition_names("prd");
sys_ = manifest_.add_partitions();
sys_->set_partition_name("sys");
SetSize(sys_, 3_MiB);
vnd_ = manifest_.add_partitions();
vnd_->set_partition_name("vnd");
SetSize(vnd_, 3_MiB);
prd_ = manifest_.add_partitions();
prd_->set_partition_name("prd");
SetSize(prd_, 3_MiB);
// Initialize source partition metadata using |manifest_|.
src_ = MetadataBuilder::New(*opener_, "super", 0);
ASSERT_TRUE(FillFakeMetadata(src_.get(), manifest_, "_a"));
ASSERT_NE(nullptr, src_);
// Add sys_b which is like system_other.
auto partition = src_->AddPartition("sys_b", 0);
ASSERT_NE(nullptr, partition);
ASSERT_TRUE(src->ResizePartition(partition, 3_MiB));
partition = src->AddPartition("vnd_a", 0);
ASSERT_NE(nullptr, partition);
ASSERT_TRUE(src->ResizePartition(partition, 3_MiB));
partition = src->AddPartition("prd_a", 0);
ASSERT_NE(nullptr, partition);
ASSERT_TRUE(src->ResizePartition(partition, 3_MiB));
partition = src->AddPartition("sys_b", 0);
ASSERT_NE(nullptr, partition);
ASSERT_TRUE(src->ResizePartition(partition, 1_MiB));
auto metadata = src->Export();
ASSERT_TRUE(src_->ResizePartition(partition, 1_MiB));
auto metadata = src_->Export();
ASSERT_NE(nullptr, metadata);
ASSERT_TRUE(UpdatePartitionTable(*opener, "super", *metadata.get(), 0));
ASSERT_TRUE(UpdatePartitionTable(*opener_, "super", *metadata.get(), 0));
// Map source partitions. Additionally, map sys_b to simulate system_other after flashing.
std::string path;
@ -668,7 +682,7 @@ class SnapshotUpdateTest : public SnapshotTest {
.metadata_slot = 0,
.partition_name = name,
.timeout_ms = 1s,
.partition_opener = opener.get(),
.partition_opener = opener_.get(),
},
&path));
ASSERT_TRUE(WriteRandomData(path));
@ -692,22 +706,6 @@ class SnapshotUpdateTest : public SnapshotTest {
EXPECT_TRUE(UnmapAll());
}
static AssertionResult ResizePartitions(
MetadataBuilder* builder,
const std::vector<std::pair<std::string, uint64_t>>& partition_sizes) {
for (auto&& [name, size] : partition_sizes) {
auto partition = builder->FindPartition(name);
if (!partition) {
return AssertionFailure() << "Cannot find partition in metadata " << name;
}
if (!builder->ResizePartition(partition, size)) {
return AssertionFailure()
<< "Cannot resize partition " << name << " to " << size << " bytes";
}
}
return AssertionSuccess();
}
AssertionResult IsPartitionUnchanged(const std::string& name) {
std::string path;
if (!dm_.GetDmDevicePathByName(name, &path)) {
@ -748,9 +746,14 @@ class SnapshotUpdateTest : public SnapshotTest {
return AssertionSuccess();
}
std::unique_ptr<TestPartitionOpener> opener;
std::unique_ptr<MetadataBuilder> src;
std::unique_ptr<TestPartitionOpener> opener_;
DeltaArchiveManifest manifest_;
std::unique_ptr<MetadataBuilder> src_;
std::map<std::string, std::string> hashes_;
PartitionUpdate* sys_ = nullptr;
PartitionUpdate* vnd_ = nullptr;
PartitionUpdate* prd_ = nullptr;
};
// Test full update flow executed by update_engine. Some partitions uses super empty space,
@ -767,29 +770,19 @@ TEST_F(SnapshotUpdateTest, FullUpdateFlow) {
ASSERT_TRUE(sm->UnmapUpdateSnapshot(name));
}
// OTA client adjusts the partition sizes before giving it to CreateUpdateSnapshots.
auto tgt = MetadataBuilder::NewForUpdate(*opener, "super", 0, 1);
ASSERT_NE(nullptr, tgt);
// clang-format off
ASSERT_TRUE(ResizePartitions(tgt.get(), {
{"sys_b", 4_MiB}, // grows
{"vnd_b", 4_MiB}, // grows
{"prd_b", 4_MiB}, // grows
}));
// clang-format on
// Grow all partitions.
SetSize(sys_, 4_MiB);
SetSize(vnd_, 4_MiB);
SetSize(prd_, 4_MiB);
ASSERT_TRUE(sm->CreateUpdateSnapshots(tgt.get(), "_b", src.get(), "_a", {}));
ASSERT_TRUE(sm->CreateUpdateSnapshots(manifest_));
// Test that partitions prioritize using space in super.
auto tgt = MetadataBuilder::New(*opener_, "super", 1);
ASSERT_NE(nullptr, tgt->FindPartition("sys_b-cow"));
ASSERT_NE(nullptr, tgt->FindPartition("vnd_b-cow"));
ASSERT_EQ(nullptr, tgt->FindPartition("prd_b-cow"));
// The metadata slot 1 is now updated.
auto metadata = tgt->Export();
ASSERT_NE(nullptr, metadata);
ASSERT_TRUE(UpdatePartitionTable(*opener, "super", *metadata.get(), 1));
// Write some data to target partitions.
for (const auto& name : {"sys_b", "vnd_b", "prd_b"}) {
std::string path;
@ -799,7 +792,7 @@ TEST_F(SnapshotUpdateTest, FullUpdateFlow) {
.metadata_slot = 1,
.partition_name = name,
.timeout_ms = 10s,
.partition_opener = opener.get(),
.partition_opener = opener_.get(),
},
&path))
<< name;
@ -845,49 +838,31 @@ TEST_F(SnapshotUpdateTest, FullUpdateFlow) {
// Test that if new system partitions uses empty space in super, that region is not snapshotted.
TEST_F(SnapshotUpdateTest, DirectWriteEmptySpace) {
auto tgt = MetadataBuilder::NewForUpdate(*opener, "super", 0, 1);
ASSERT_NE(nullptr, tgt);
// clang-format off
ASSERT_TRUE(ResizePartitions(tgt.get(), {
{"sys_b", 4_MiB}, // grows
// vnd_b and prd_b are unchanged
}));
// clang-format on
ASSERT_TRUE(sm->CreateUpdateSnapshots(tgt.get(), "_b", src.get(), "_a", {}));
SetSize(sys_, 4_MiB);
// vnd_b and prd_b are unchanged.
ASSERT_TRUE(sm->CreateUpdateSnapshots(manifest_));
ASSERT_EQ(3_MiB, GetSnapshotSize("sys_b").value_or(0));
}
// Test that if new system partitions uses space of old vendor partition, that region is
// snapshotted.
TEST_F(SnapshotUpdateTest, SnapshotOldPartitions) {
auto tgt = MetadataBuilder::NewForUpdate(*opener, "super", 0, 1);
ASSERT_NE(nullptr, tgt);
// clang-format off
ASSERT_TRUE(ResizePartitions(tgt.get(), {
{"vnd_b", 2_MiB}, // shrinks
{"sys_b", 4_MiB}, // grows
// prd_b is unchanged
}));
// clang-format on
ASSERT_TRUE(sm->CreateUpdateSnapshots(tgt.get(), "_b", src.get(), "_a", {}));
SetSize(sys_, 4_MiB); // grows
SetSize(vnd_, 2_MiB); // shrinks
// prd_b is unchanged
ASSERT_TRUE(sm->CreateUpdateSnapshots(manifest_));
ASSERT_EQ(4_MiB, GetSnapshotSize("sys_b").value_or(0));
}
// Test that even if there seem to be empty space in target metadata, COW partition won't take
// it because they are used by old partitions.
TEST_F(SnapshotUpdateTest, CowPartitionDoNotTakeOldPartitions) {
auto tgt = MetadataBuilder::NewForUpdate(*opener, "super", 0, 1);
ASSERT_NE(nullptr, tgt);
// clang-format off
ASSERT_TRUE(ResizePartitions(tgt.get(), {
{"sys_b", 2_MiB}, // shrinks
// vnd_b and prd_b are unchanged
}));
// clang-format on
ASSERT_TRUE(sm->CreateUpdateSnapshots(tgt.get(), "_b", src.get(), "_a", {}));
SetSize(sys_, 2_MiB); // shrinks
// vnd_b and prd_b are unchanged.
ASSERT_TRUE(sm->CreateUpdateSnapshots(manifest_));
auto tgt = MetadataBuilder::New(*opener_, "super", 1);
ASSERT_NE(nullptr, tgt);
auto metadata = tgt->Export();
ASSERT_NE(nullptr, metadata);
std::vector<std::string> written;
@ -903,7 +878,7 @@ TEST_F(SnapshotUpdateTest, CowPartitionDoNotTakeOldPartitions) {
.metadata = metadata.get(),
.partition = &p,
.timeout_ms = 1s,
.partition_opener = opener.get(),
.partition_opener = opener_.get(),
},
&path));
ASSERT_TRUE(WriteRandomData(path));
@ -934,14 +909,8 @@ TEST_F(SnapshotUpdateTest, SnapshotStatusFileWithoutCow) {
// Redo the update.
ASSERT_TRUE(sm->BeginUpdate());
ASSERT_TRUE(sm->UnmapUpdateSnapshot("sys_b"));
auto tgt = MetadataBuilder::NewForUpdate(*opener, "super", 0, 1);
ASSERT_NE(nullptr, tgt);
ASSERT_TRUE(sm->CreateUpdateSnapshots(tgt.get(), "_b", src.get(), "_a", {}));
// The metadata slot 1 is now updated.
auto metadata = tgt->Export();
ASSERT_NE(nullptr, metadata);
ASSERT_TRUE(UpdatePartitionTable(*opener, "super", *metadata.get(), 1));
ASSERT_TRUE(sm->CreateUpdateSnapshots(manifest_));
// Check that target partitions can be mapped.
for (const auto& name : {"sys_b", "vnd_b", "prd_b"}) {
@ -952,7 +921,7 @@ TEST_F(SnapshotUpdateTest, SnapshotStatusFileWithoutCow) {
.metadata_slot = 1,
.partition_name = name,
.timeout_ms = 10s,
.partition_opener = opener.get(),
.partition_opener = opener_.get(),
},
&path))
<< name;
@ -964,14 +933,8 @@ TEST_F(SnapshotUpdateTest, TestRollback) {
// Execute the update.
ASSERT_TRUE(sm->BeginUpdate());
ASSERT_TRUE(sm->UnmapUpdateSnapshot("sys_b"));
auto tgt = MetadataBuilder::NewForUpdate(*opener, "super", 0, 1);
ASSERT_NE(nullptr, tgt);
ASSERT_TRUE(sm->CreateUpdateSnapshots(tgt.get(), "_b", src.get(), "_a", {}));
// The metadata slot 1 is now updated.
auto metadata = tgt->Export();
ASSERT_NE(nullptr, metadata);
ASSERT_TRUE(UpdatePartitionTable(*opener, "super", *metadata.get(), 1));
ASSERT_TRUE(sm->CreateUpdateSnapshots(manifest_));
// Write some data to target partitions.
for (const auto& name : {"sys_b", "vnd_b", "prd_b"}) {
@ -982,7 +945,7 @@ TEST_F(SnapshotUpdateTest, TestRollback) {
.metadata_slot = 1,
.partition_name = name,
.timeout_ms = 10s,
.partition_opener = opener.get(),
.partition_opener = opener_.get(),
},
&path))
<< name;

View File

@ -27,6 +27,8 @@ using android::base::ReadFully;
using android::base::unique_fd;
using android::base::WriteFully;
using android::fiemap::IImageManager;
using testing::AssertionFailure;
using testing::AssertionSuccess;
void DeleteBackingImage(IImageManager* manager, const std::string& name) {
if (manager->IsImageMapped(name)) {
@ -110,5 +112,40 @@ std::optional<std::string> GetHash(const std::string& path) {
return ToHexString(out, sizeof(out));
}
AssertionResult FillFakeMetadata(MetadataBuilder* builder, const DeltaArchiveManifest& manifest,
const std::string& suffix) {
for (const auto& group : manifest.dynamic_partition_metadata().groups()) {
if (!builder->AddGroup(group.name() + suffix, group.size())) {
return AssertionFailure()
<< "Cannot add group " << group.name() << " with size " << group.size();
}
for (const auto& partition_name : group.partition_names()) {
auto p = builder->AddPartition(partition_name + suffix, group.name() + suffix,
0 /* attr */);
if (!p) {
return AssertionFailure() << "Cannot add partition " << partition_name + suffix
<< " to group " << group.name() << suffix;
}
}
}
for (const auto& partition : manifest.partitions()) {
auto p = builder->FindPartition(partition.partition_name() + suffix);
if (!p) {
return AssertionFailure() << "Cannot resize partition " << partition.partition_name()
<< suffix << "; it is not found.";
}
if (!builder->ResizePartition(p, partition.new_partition_info().size())) {
return AssertionFailure()
<< "Cannot resize partition " << partition.partition_name() << suffix
<< " to size " << partition.new_partition_info().size();
}
}
return AssertionSuccess();
}
void SetSize(PartitionUpdate* partition_update, uint64_t size) {
partition_update->mutable_new_partition_info()->set_size(size);
}
} // namespace snapshot
} // namespace android

View File

@ -17,13 +17,20 @@
#include <optional>
#include <string>
#include <gtest/gtest.h>
#include <libfiemap/image_manager.h>
#include <liblp/partition_opener.h>
#include <libsnapshot/snapshot.h>
#include <update_engine/update_metadata.pb.h>
namespace android {
namespace snapshot {
using android::fs_mgr::MetadataBuilder;
using chromeos_update_engine::DeltaArchiveManifest;
using chromeos_update_engine::PartitionUpdate;
using testing::AssertionResult;
using namespace std::string_literals;
// Redirect requests for "super" to our fake super partition.
@ -48,6 +55,7 @@ class TestDeviceInfo : public SnapshotManager::IDeviceInfo {
std::string GetGsidDir() const override { return "ota/test"s; }
std::string GetMetadataDir() const override { return "/metadata/ota/test"s; }
std::string GetSlotSuffix() const override { return slot_suffix_; }
std::string GetOtherSlotSuffix() const override { return slot_suffix_ == "_a" ? "_b" : "_a"; }
std::string GetSuperDevice([[maybe_unused]] uint32_t slot) const override { return "super"; }
const android::fs_mgr::IPartitionOpener& GetPartitionOpener() const override {
return *opener_.get();
@ -72,5 +80,12 @@ bool WriteRandomData(const std::string& device);
std::optional<std::string> GetHash(const std::string& path);
// Add partitions and groups described by |manifest|.
AssertionResult FillFakeMetadata(MetadataBuilder* builder, const DeltaArchiveManifest& manifest,
const std::string& suffix);
// In the update package metadata, set a partition with the given size.
void SetSize(PartitionUpdate* partition_update, uint64_t size);
} // namespace snapshot
} // namespace android

View File

@ -22,6 +22,7 @@
#include <libfiemap/image_manager.h>
#include <liblp/builder.h>
#include <libsnapshot/snapshot.h>
#include <update_engine/update_metadata.pb.h>
namespace android {
namespace snapshot {