2019-08-28 01:37:41 +00:00
|
|
|
// 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
|
|
|
|
|
2019-07-26 00:52:08 +00:00
|
|
|
#include <functional>
|
2020-02-03 22:51:06 +00:00
|
|
|
#include <iostream>
|
2019-08-28 01:37:41 +00:00
|
|
|
#include <string>
|
|
|
|
|
|
|
|
#include <android-base/macros.h>
|
2019-10-18 01:25:04 +00:00
|
|
|
#include <fstab/fstab.h>
|
2019-08-28 01:37:41 +00:00
|
|
|
#include <libdm/dm.h>
|
|
|
|
#include <libfiemap/image_manager.h>
|
2019-07-26 00:52:08 +00:00
|
|
|
#include <liblp/builder.h>
|
|
|
|
#include <libsnapshot/snapshot.h>
|
2019-09-13 21:49:53 +00:00
|
|
|
#include <update_engine/update_metadata.pb.h>
|
2019-08-28 01:37:41 +00:00
|
|
|
|
2019-10-18 01:25:04 +00:00
|
|
|
#include <libsnapshot/auto_device.h>
|
2020-01-03 22:36:35 +00:00
|
|
|
#include <libsnapshot/snapshot.h>
|
2019-10-18 01:25:04 +00:00
|
|
|
|
2019-08-28 01:37:41 +00:00
|
|
|
namespace android {
|
|
|
|
namespace snapshot {
|
|
|
|
|
2019-10-14 19:56:58 +00:00
|
|
|
// Unit is sectors, this is a 4K chunk.
|
|
|
|
static constexpr uint32_t kSnapshotChunkSize = 8;
|
|
|
|
|
2019-08-28 01:37:41 +00:00
|
|
|
// A list of devices we created along the way.
|
|
|
|
// - Whenever a device is created that is subject to GC'ed at the end of
|
|
|
|
// this function, add it to this list.
|
|
|
|
// - If any error has occurred, the list is destroyed, and all these devices
|
|
|
|
// are cleaned up.
|
|
|
|
// - Upon success, Release() should be called so that the created devices
|
|
|
|
// are kept.
|
|
|
|
struct AutoDeviceList {
|
|
|
|
~AutoDeviceList();
|
|
|
|
template <typename T, typename... Args>
|
|
|
|
void EmplaceBack(Args&&... args) {
|
|
|
|
devices_.emplace_back(std::make_unique<T>(std::forward<Args>(args)...));
|
|
|
|
}
|
|
|
|
void Release();
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::vector<std::unique_ptr<AutoDevice>> devices_;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Automatically unmap a device upon deletion.
|
|
|
|
struct AutoUnmapDevice : AutoDevice {
|
|
|
|
// On destruct, delete |name| from device mapper.
|
|
|
|
AutoUnmapDevice(android::dm::DeviceMapper* dm, const std::string& name)
|
|
|
|
: AutoDevice(name), dm_(dm) {}
|
|
|
|
AutoUnmapDevice(AutoUnmapDevice&& other) = default;
|
|
|
|
~AutoUnmapDevice();
|
|
|
|
|
|
|
|
private:
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(AutoUnmapDevice);
|
|
|
|
android::dm::DeviceMapper* dm_ = nullptr;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Automatically unmap an image upon deletion.
|
|
|
|
struct AutoUnmapImage : AutoDevice {
|
|
|
|
// On destruct, delete |name| from image manager.
|
|
|
|
AutoUnmapImage(android::fiemap::IImageManager* images, const std::string& name)
|
|
|
|
: AutoDevice(name), images_(images) {}
|
|
|
|
AutoUnmapImage(AutoUnmapImage&& other) = default;
|
|
|
|
~AutoUnmapImage();
|
|
|
|
|
|
|
|
private:
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(AutoUnmapImage);
|
|
|
|
android::fiemap::IImageManager* images_ = nullptr;
|
|
|
|
};
|
|
|
|
|
2019-07-26 00:52:08 +00:00
|
|
|
// Automatically deletes a snapshot. |name| should be the name of the partition, e.g. "system_a".
|
|
|
|
// Client is responsible for maintaining the lifetime of |manager| and |lock|.
|
|
|
|
struct AutoDeleteSnapshot : AutoDevice {
|
|
|
|
AutoDeleteSnapshot(SnapshotManager* manager, SnapshotManager::LockedFile* lock,
|
|
|
|
const std::string& name)
|
|
|
|
: AutoDevice(name), manager_(manager), lock_(lock) {}
|
|
|
|
AutoDeleteSnapshot(AutoDeleteSnapshot&& other);
|
|
|
|
~AutoDeleteSnapshot();
|
|
|
|
|
|
|
|
private:
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(AutoDeleteSnapshot);
|
|
|
|
SnapshotManager* manager_ = nullptr;
|
|
|
|
SnapshotManager::LockedFile* lock_ = nullptr;
|
|
|
|
};
|
|
|
|
|
2019-10-18 01:25:04 +00:00
|
|
|
struct AutoUnmountDevice : AutoDevice {
|
|
|
|
// Empty object that does nothing.
|
|
|
|
AutoUnmountDevice() : AutoDevice("") {}
|
|
|
|
static std::unique_ptr<AutoUnmountDevice> New(const std::string& path);
|
|
|
|
~AutoUnmountDevice();
|
|
|
|
|
|
|
|
private:
|
|
|
|
AutoUnmountDevice(const std::string& path, android::fs_mgr::Fstab&& fstab)
|
|
|
|
: AutoDevice(path), fstab_(std::move(fstab)) {}
|
|
|
|
android::fs_mgr::Fstab fstab_;
|
|
|
|
};
|
|
|
|
|
2019-07-26 00:52:08 +00:00
|
|
|
// Return a list of partitions in |builder| with the name ending in |suffix|.
|
|
|
|
std::vector<android::fs_mgr::Partition*> ListPartitionsWithSuffix(
|
|
|
|
android::fs_mgr::MetadataBuilder* builder, const std::string& suffix);
|
|
|
|
|
2019-08-27 21:25:31 +00:00
|
|
|
// Initialize a device before using it as the COW device for a dm-snapshot device.
|
2020-01-16 23:10:35 +00:00
|
|
|
Return InitializeCow(const std::string& device);
|
2019-08-27 21:25:31 +00:00
|
|
|
|
2019-11-27 00:20:17 +00:00
|
|
|
// "Atomically" write string to file. This is done by a series of actions:
|
|
|
|
// 1. Write to path + ".tmp"
|
|
|
|
// 2. Move temporary file to path using rename()
|
|
|
|
// Note that rename() is an atomic operation. This function may not work properly if there
|
|
|
|
// is an open fd to |path|, because that fd has an old view of the file.
|
|
|
|
bool WriteStringToFileAtomic(const std::string& content, const std::string& path);
|
|
|
|
|
2020-02-03 22:51:06 +00:00
|
|
|
// Writes current time to a given stream.
|
|
|
|
struct Now {};
|
|
|
|
std::ostream& operator<<(std::ostream& os, const Now&);
|
|
|
|
|
2019-08-28 01:37:41 +00:00
|
|
|
} // namespace snapshot
|
|
|
|
} // namespace android
|