David Anderson | b2988ab | 2019-04-16 17:14:09 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | #pragma once |
| 17 | |
| 18 | #include <stdint.h> |
| 19 | |
| 20 | #include <memory> |
| 21 | #include <string> |
| 22 | |
| 23 | #include <android/gsi/IGsiService.h> |
| 24 | #include <libfiemap_writer/split_fiemap_writer.h> |
| 25 | #include <liblp/builder.h> |
| 26 | |
| 27 | namespace android { |
| 28 | namespace gsi { |
| 29 | |
| 30 | class GsiService; |
| 31 | |
| 32 | class GsiInstaller final { |
| 33 | public: |
| 34 | // Constructor for a new GSI installation. |
| 35 | GsiInstaller(GsiService* service, const GsiInstallParams& params); |
| 36 | // Constructor for re-enabling a previous GSI installation. |
| 37 | GsiInstaller(GsiService* service, const std::string& install_dir); |
| 38 | ~GsiInstaller(); |
| 39 | |
| 40 | // Methods for a clean GSI install. |
| 41 | int StartInstall(); |
| 42 | bool CommitGsiChunk(int stream_fd, int64_t bytes); |
| 43 | bool CommitGsiChunk(const void* data, size_t bytes); |
| 44 | int SetGsiBootable(bool one_shot); |
| 45 | |
David Anderson | 8bdf625 | 2019-06-11 16:43:24 -0700 | [diff] [blame^] | 46 | // Methods for interacting with an existing install. |
David Anderson | b2988ab | 2019-04-16 17:14:09 -0700 | [diff] [blame] | 47 | int ReenableGsi(bool one_shot); |
David Anderson | 8bdf625 | 2019-06-11 16:43:24 -0700 | [diff] [blame^] | 48 | int WipeUserdata(); |
David Anderson | b2988ab | 2019-04-16 17:14:09 -0700 | [diff] [blame] | 49 | |
| 50 | // Clean up install state if gsid crashed and restarted. |
| 51 | static void PostInstallCleanup(); |
| 52 | |
| 53 | // This helper class will redirect writes to either a SplitFiemap or |
| 54 | // device-mapper. |
| 55 | class WriteHelper { |
| 56 | public: |
| 57 | virtual ~WriteHelper(){}; |
| 58 | virtual bool Write(const void* data, uint64_t bytes) = 0; |
| 59 | virtual bool Flush() = 0; |
David Anderson | 8bdf625 | 2019-06-11 16:43:24 -0700 | [diff] [blame^] | 60 | virtual uint64_t Size() = 0; |
David Anderson | b2988ab | 2019-04-16 17:14:09 -0700 | [diff] [blame] | 61 | |
| 62 | WriteHelper() = default; |
| 63 | WriteHelper(const WriteHelper&) = delete; |
| 64 | WriteHelper& operator=(const WriteHelper&) = delete; |
| 65 | WriteHelper& operator=(WriteHelper&&) = delete; |
| 66 | WriteHelper(WriteHelper&&) = delete; |
| 67 | }; |
| 68 | |
| 69 | const std::string& install_dir() const { return install_dir_; } |
| 70 | uint64_t userdata_size() const { return userdata_size_; } |
| 71 | |
| 72 | private: |
| 73 | using MetadataBuilder = android::fs_mgr::MetadataBuilder; |
| 74 | using LpMetadata = android::fs_mgr::LpMetadata; |
| 75 | using SplitFiemap = android::fiemap_writer::SplitFiemap; |
| 76 | |
| 77 | // The image file may be larger than the requested size, due to alignment, |
| 78 | // so we must track the requested size as well. |
| 79 | struct Image { |
| 80 | std::unique_ptr<SplitFiemap> writer; |
| 81 | uint64_t actual_size; |
| 82 | }; |
| 83 | |
| 84 | int PerformSanityChecks(); |
| 85 | int PreallocateFiles(); |
| 86 | int PreallocateUserdata(); |
| 87 | int PreallocateSystem(); |
| 88 | int DetermineReadWriteMethod(); |
| 89 | bool FormatUserdata(); |
| 90 | bool AddPartitionFiemap(MetadataBuilder* builder, android::fs_mgr::Partition* partition, |
| 91 | const Image& image, const std::string& block_device); |
| 92 | std::unique_ptr<LpMetadata> CreateMetadata(); |
| 93 | std::unique_ptr<SplitFiemap> CreateFiemapWriter(const std::string& path, uint64_t size, |
| 94 | int* error); |
| 95 | std::unique_ptr<WriteHelper> OpenPartition(const std::string& name); |
| 96 | int GetExistingImage(const LpMetadata& metadata, const std::string& name, Image* image); |
David Anderson | 8bdf625 | 2019-06-11 16:43:24 -0700 | [diff] [blame^] | 97 | int RebuildInstallState(); |
David Anderson | b2988ab | 2019-04-16 17:14:09 -0700 | [diff] [blame] | 98 | bool CreateInstallStatusFile(); |
| 99 | bool CreateMetadataFile(); |
| 100 | bool SetBootMode(bool one_shot); |
| 101 | std::string GetImagePath(const std::string& name); |
| 102 | |
| 103 | GsiService* service_; |
| 104 | |
| 105 | std::string install_dir_; |
| 106 | std::string userdata_gsi_path_; |
| 107 | std::string system_gsi_path_; |
| 108 | uint64_t userdata_block_size_ = 0; |
| 109 | uint64_t system_block_size_ = 0; |
| 110 | uint64_t gsi_size_ = 0; |
| 111 | uint64_t userdata_size_ = 0; |
| 112 | bool can_use_devicemapper_ = false; |
| 113 | bool wipe_userdata_ = false; |
| 114 | bool wipe_userdata_on_failure_ = false; |
| 115 | // Remaining data we're waiting to receive for the GSI image. |
| 116 | uint64_t gsi_bytes_written_ = 0; |
| 117 | bool succeeded_ = false; |
| 118 | |
| 119 | std::unique_ptr<WriteHelper> system_writer_; |
| 120 | |
| 121 | // This is used to track which GSI partitions have been created. |
| 122 | std::map<std::string, Image> partitions_; |
| 123 | std::unique_ptr<LpMetadata> metadata_; |
| 124 | }; |
| 125 | |
| 126 | } // namespace gsi |
| 127 | } // namespace android |