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 | |
Howard Chen | 4663de6 | 2019-11-05 20:46:20 +0800 | [diff] [blame] | 17 | #include "partition_installer.h" |
David Anderson | b2988ab | 2019-04-16 17:14:09 -0700 | [diff] [blame] | 18 | |
| 19 | #include <sys/statvfs.h> |
| 20 | |
| 21 | #include <android-base/file.h> |
| 22 | #include <android-base/logging.h> |
| 23 | #include <android-base/unique_fd.h> |
David Anderson | 8bdf625 | 2019-06-11 16:43:24 -0700 | [diff] [blame] | 24 | #include <ext4_utils/ext4_utils.h> |
Yi-Yo Chiang | 81af6b2 | 2021-11-03 20:26:32 +0800 | [diff] [blame] | 25 | #include <fs_mgr.h> |
David Anderson | b2988ab | 2019-04-16 17:14:09 -0700 | [diff] [blame] | 26 | #include <fs_mgr_dm_linear.h> |
| 27 | #include <libdm/dm.h> |
| 28 | #include <libgsi/libgsi.h> |
Yi-Yo Chiang | 81af6b2 | 2021-11-03 20:26:32 +0800 | [diff] [blame] | 29 | #include <liblp/partition_opener.h> |
David Anderson | b2988ab | 2019-04-16 17:14:09 -0700 | [diff] [blame] | 30 | |
| 31 | #include "file_paths.h" |
| 32 | #include "gsi_service.h" |
| 33 | #include "libgsi_private.h" |
| 34 | |
| 35 | namespace android { |
| 36 | namespace gsi { |
| 37 | |
| 38 | using namespace std::literals; |
| 39 | using namespace android::dm; |
David Anderson | 9ca7728 | 2019-07-15 23:56:13 +0000 | [diff] [blame] | 40 | using namespace android::fiemap; |
David Anderson | b2988ab | 2019-04-16 17:14:09 -0700 | [diff] [blame] | 41 | using namespace android::fs_mgr; |
| 42 | using android::base::unique_fd; |
| 43 | |
Howard Chen | 4663de6 | 2019-11-05 20:46:20 +0800 | [diff] [blame] | 44 | PartitionInstaller::PartitionInstaller(GsiService* service, const std::string& install_dir, |
Howard Chen | ee5c2b1 | 2019-11-08 11:57:47 +0800 | [diff] [blame] | 45 | const std::string& name, const std::string& active_dsu, |
| 46 | int64_t size, bool read_only) |
| 47 | : service_(service), |
| 48 | install_dir_(install_dir), |
| 49 | name_(name), |
| 50 | active_dsu_(active_dsu), |
| 51 | size_(size), |
| 52 | readOnly_(read_only) { |
| 53 | images_ = ImageManager::Open(MetadataDir(active_dsu), install_dir_); |
David Anderson | b2988ab | 2019-04-16 17:14:09 -0700 | [diff] [blame] | 54 | } |
| 55 | |
Howard Chen | 4663de6 | 2019-11-05 20:46:20 +0800 | [diff] [blame] | 56 | PartitionInstaller::~PartitionInstaller() { |
Yo Chiang | f194dce | 2020-08-24 17:21:10 +0800 | [diff] [blame] | 57 | if (FinishInstall() != IGsiService::INSTALL_OK) { |
Yo Chiang | 281584b | 2020-08-24 16:50:20 +0800 | [diff] [blame] | 58 | LOG(ERROR) << "Installation failed: install_dir=" << install_dir_ |
| 59 | << ", dsu_slot=" << active_dsu_ << ", partition_name=" << name_; |
David Anderson | b2988ab | 2019-04-16 17:14:09 -0700 | [diff] [blame] | 60 | } |
Howard Chen | 5676d96 | 2019-08-05 16:21:00 +0800 | [diff] [blame] | 61 | if (IsAshmemMapped()) { |
| 62 | UnmapAshmem(); |
| 63 | } |
David Anderson | b2988ab | 2019-04-16 17:14:09 -0700 | [diff] [blame] | 64 | } |
| 65 | |
Yo Chiang | f194dce | 2020-08-24 17:21:10 +0800 | [diff] [blame] | 66 | int PartitionInstaller::FinishInstall() { |
| 67 | if (finished_) { |
| 68 | return finished_status_; |
David Anderson | b2988ab | 2019-04-16 17:14:09 -0700 | [diff] [blame] | 69 | } |
Yo Chiang | f194dce | 2020-08-24 17:21:10 +0800 | [diff] [blame] | 70 | finished_ = true; |
| 71 | finished_status_ = CheckInstallState(); |
| 72 | system_device_ = nullptr; |
| 73 | if (finished_status_ != IGsiService::INSTALL_OK) { |
| 74 | auto file = GetBackingFile(name_); |
| 75 | LOG(ERROR) << "Installation failed, clean up: " << file; |
| 76 | if (images_->IsImageMapped(file)) { |
| 77 | LOG(ERROR) << "unmap " << file; |
| 78 | images_->UnmapImageDevice(file); |
| 79 | } |
| 80 | images_->DeleteBackingImage(file); |
David Anderson | b2988ab | 2019-04-16 17:14:09 -0700 | [diff] [blame] | 81 | } |
Yo Chiang | f194dce | 2020-08-24 17:21:10 +0800 | [diff] [blame] | 82 | return finished_status_; |
David Anderson | b2988ab | 2019-04-16 17:14:09 -0700 | [diff] [blame] | 83 | } |
| 84 | |
Howard Chen | 4663de6 | 2019-11-05 20:46:20 +0800 | [diff] [blame] | 85 | int PartitionInstaller::StartInstall() { |
David Anderson | b2988ab | 2019-04-16 17:14:09 -0700 | [diff] [blame] | 86 | if (int status = PerformSanityChecks()) { |
| 87 | return status; |
| 88 | } |
Howard Chen | 18109b1 | 2019-08-13 17:00:44 +0800 | [diff] [blame] | 89 | if (int status = Preallocate()) { |
David Anderson | b2988ab | 2019-04-16 17:14:09 -0700 | [diff] [blame] | 90 | return status; |
| 91 | } |
Howard Chen | 18109b1 | 2019-08-13 17:00:44 +0800 | [diff] [blame] | 92 | if (!readOnly_) { |
| 93 | if (!Format()) { |
| 94 | return IGsiService::INSTALL_ERROR_GENERIC; |
| 95 | } |
Howard Chen | 18109b1 | 2019-08-13 17:00:44 +0800 | [diff] [blame] | 96 | } else { |
| 97 | // Map ${name}_gsi so we can write to it. |
| 98 | system_device_ = OpenPartition(GetBackingFile(name_)); |
| 99 | if (!system_device_) { |
| 100 | return IGsiService::INSTALL_ERROR_GENERIC; |
| 101 | } |
David Anderson | b2988ab | 2019-04-16 17:14:09 -0700 | [diff] [blame] | 102 | |
Howard Chen | 18109b1 | 2019-08-13 17:00:44 +0800 | [diff] [blame] | 103 | // Clear the progress indicator. |
| 104 | service_->UpdateProgress(IGsiService::STATUS_NO_OPERATION, 0); |
David Anderson | b2988ab | 2019-04-16 17:14:09 -0700 | [diff] [blame] | 105 | } |
David Anderson | b2988ab | 2019-04-16 17:14:09 -0700 | [diff] [blame] | 106 | return IGsiService::INSTALL_OK; |
| 107 | } |
| 108 | |
Howard Chen | 4663de6 | 2019-11-05 20:46:20 +0800 | [diff] [blame] | 109 | int PartitionInstaller::PerformSanityChecks() { |
David Anderson | 64b53fb | 2019-07-01 19:05:35 -0700 | [diff] [blame] | 110 | if (!images_) { |
| 111 | LOG(ERROR) << "unable to create image manager"; |
| 112 | return IGsiService::INSTALL_ERROR_GENERIC; |
| 113 | } |
Howard Chen | 18109b1 | 2019-08-13 17:00:44 +0800 | [diff] [blame] | 114 | if (size_ < 0) { |
| 115 | LOG(ERROR) << "image size " << size_ << " is negative"; |
David Anderson | b2988ab | 2019-04-16 17:14:09 -0700 | [diff] [blame] | 116 | return IGsiService::INSTALL_ERROR_GENERIC; |
| 117 | } |
| 118 | if (android::gsi::IsGsiRunning()) { |
| 119 | LOG(ERROR) << "cannot install gsi inside a live gsi"; |
| 120 | return IGsiService::INSTALL_ERROR_GENERIC; |
| 121 | } |
| 122 | |
| 123 | struct statvfs sb; |
| 124 | if (statvfs(install_dir_.c_str(), &sb)) { |
| 125 | PLOG(ERROR) << "failed to read file system stats"; |
| 126 | return IGsiService::INSTALL_ERROR_GENERIC; |
| 127 | } |
| 128 | |
| 129 | // This is the same as android::vold::GetFreebytes() but we also |
| 130 | // need the total file system size so we open code it here. |
Yi-Yo Chiang | 23a996f | 2021-02-12 01:57:44 +0800 | [diff] [blame] | 131 | uint64_t free_space = static_cast<uint64_t>(sb.f_bavail) * sb.f_frsize; |
Howard Chen | 18109b1 | 2019-08-13 17:00:44 +0800 | [diff] [blame] | 132 | if (free_space <= (size_)) { |
David Anderson | b2988ab | 2019-04-16 17:14:09 -0700 | [diff] [blame] | 133 | LOG(ERROR) << "not enough free space (only " << free_space << " bytes available)"; |
| 134 | return IGsiService::INSTALL_ERROR_NO_SPACE; |
| 135 | } |
Yi-Yo Chiang | 81af6b2 | 2021-11-03 20:26:32 +0800 | [diff] [blame] | 136 | |
| 137 | const auto free_space_threshold = GetMinimumFreeSpaceThreshold(install_dir_); |
| 138 | if (!free_space_threshold.has_value()) { |
| 139 | return IGsiService::INSTALL_ERROR_GENERIC; |
| 140 | } |
| 141 | if (free_space < size_ + *free_space_threshold) { |
| 142 | LOG(ERROR) << "post-installation free space (" << free_space << " - " << size_ |
| 143 | << ") would be below the minimum threshold of " << *free_space_threshold; |
David Anderson | b2988ab | 2019-04-16 17:14:09 -0700 | [diff] [blame] | 144 | return IGsiService::INSTALL_ERROR_FILE_SYSTEM_CLUTTERED; |
| 145 | } |
| 146 | return IGsiService::INSTALL_OK; |
| 147 | } |
| 148 | |
Howard Chen | 4663de6 | 2019-11-05 20:46:20 +0800 | [diff] [blame] | 149 | int PartitionInstaller::Preallocate() { |
Howard Chen | 18109b1 | 2019-08-13 17:00:44 +0800 | [diff] [blame] | 150 | std::string file = GetBackingFile(name_); |
Howard Chen | 4663de6 | 2019-11-05 20:46:20 +0800 | [diff] [blame] | 151 | if (!images_->UnmapImageIfExists(file)) { |
| 152 | LOG(ERROR) << "failed to UnmapImageIfExists " << file; |
| 153 | return IGsiService::INSTALL_ERROR_GENERIC; |
David Anderson | b2988ab | 2019-04-16 17:14:09 -0700 | [diff] [blame] | 154 | } |
Howard Chen | 4663de6 | 2019-11-05 20:46:20 +0800 | [diff] [blame] | 155 | // always delete the old one when it presents in case there might a partition |
| 156 | // with same name but different size. |
| 157 | if (images_->BackingImageExists(file)) { |
| 158 | if (!images_->DeleteBackingImage(file)) { |
| 159 | LOG(ERROR) << "failed to DeleteBackingImage " << file; |
Howard Chen | 18109b1 | 2019-08-13 17:00:44 +0800 | [diff] [blame] | 160 | return IGsiService::INSTALL_ERROR_GENERIC; |
| 161 | } |
David Anderson | b2988ab | 2019-04-16 17:14:09 -0700 | [diff] [blame] | 162 | } |
Howard Chen | 4663de6 | 2019-11-05 20:46:20 +0800 | [diff] [blame] | 163 | service_->StartAsyncOperation("create " + name_, size_); |
| 164 | if (!CreateImage(file, size_)) { |
| 165 | LOG(ERROR) << "Could not create userdata image"; |
| 166 | return IGsiService::INSTALL_ERROR_GENERIC; |
| 167 | } |
David Anderson | b2988ab | 2019-04-16 17:14:09 -0700 | [diff] [blame] | 168 | service_->UpdateProgress(IGsiService::STATUS_COMPLETE, 0); |
| 169 | return IGsiService::INSTALL_OK; |
| 170 | } |
| 171 | |
Howard Chen | 4663de6 | 2019-11-05 20:46:20 +0800 | [diff] [blame] | 172 | bool PartitionInstaller::CreateImage(const std::string& name, uint64_t size) { |
David Anderson | 64b53fb | 2019-07-01 19:05:35 -0700 | [diff] [blame] | 173 | auto progress = [this](uint64_t bytes, uint64_t /* total */) -> bool { |
| 174 | service_->UpdateProgress(IGsiService::STATUS_WORKING, bytes); |
| 175 | if (service_->should_abort()) return false; |
| 176 | return true; |
| 177 | }; |
David Anderson | 1fdec26 | 2019-07-24 14:03:49 -0700 | [diff] [blame] | 178 | int flags = ImageManager::CREATE_IMAGE_DEFAULT; |
Howard Chen | 18109b1 | 2019-08-13 17:00:44 +0800 | [diff] [blame] | 179 | if (readOnly_) { |
David Anderson | 1fdec26 | 2019-07-24 14:03:49 -0700 | [diff] [blame] | 180 | flags |= ImageManager::CREATE_IMAGE_READONLY; |
| 181 | } |
| 182 | return images_->CreateBackingImage(name, size, flags, std::move(progress)); |
David Anderson | b2988ab | 2019-04-16 17:14:09 -0700 | [diff] [blame] | 183 | } |
| 184 | |
Howard Chen | 4663de6 | 2019-11-05 20:46:20 +0800 | [diff] [blame] | 185 | std::unique_ptr<MappedDevice> PartitionInstaller::OpenPartition(const std::string& name) { |
Howard Chen | 73595fe | 2019-11-05 14:53:22 +0800 | [diff] [blame] | 186 | return MappedDevice::Open(images_.get(), 10s, name); |
David Anderson | b2988ab | 2019-04-16 17:14:09 -0700 | [diff] [blame] | 187 | } |
| 188 | |
Howard Chen | 4663de6 | 2019-11-05 20:46:20 +0800 | [diff] [blame] | 189 | bool PartitionInstaller::CommitGsiChunk(int stream_fd, int64_t bytes) { |
Howard Chen | 18109b1 | 2019-08-13 17:00:44 +0800 | [diff] [blame] | 190 | service_->StartAsyncOperation("write " + name_, size_); |
David Anderson | b2988ab | 2019-04-16 17:14:09 -0700 | [diff] [blame] | 191 | |
| 192 | if (bytes < 0) { |
| 193 | LOG(ERROR) << "chunk size " << bytes << " is negative"; |
| 194 | return false; |
| 195 | } |
| 196 | |
David Anderson | 64b53fb | 2019-07-01 19:05:35 -0700 | [diff] [blame] | 197 | static const size_t kBlockSize = 4096; |
| 198 | auto buffer = std::make_unique<char[]>(kBlockSize); |
David Anderson | b2988ab | 2019-04-16 17:14:09 -0700 | [diff] [blame] | 199 | |
| 200 | int progress = -1; |
| 201 | uint64_t remaining = bytes; |
| 202 | while (remaining) { |
David Anderson | 64b53fb | 2019-07-01 19:05:35 -0700 | [diff] [blame] | 203 | size_t max_to_read = std::min(static_cast<uint64_t>(kBlockSize), remaining); |
David Anderson | b2988ab | 2019-04-16 17:14:09 -0700 | [diff] [blame] | 204 | ssize_t rv = TEMP_FAILURE_RETRY(read(stream_fd, buffer.get(), max_to_read)); |
| 205 | if (rv < 0) { |
| 206 | PLOG(ERROR) << "read gsi chunk"; |
| 207 | return false; |
| 208 | } |
| 209 | if (rv == 0) { |
| 210 | LOG(ERROR) << "no bytes left in stream"; |
| 211 | return false; |
| 212 | } |
| 213 | if (!CommitGsiChunk(buffer.get(), rv)) { |
| 214 | return false; |
| 215 | } |
| 216 | CHECK(static_cast<uint64_t>(rv) <= remaining); |
| 217 | remaining -= rv; |
| 218 | |
| 219 | // Only update the progress when the % (or permille, in this case) |
| 220 | // significantly changes. |
Howard Chen | 18109b1 | 2019-08-13 17:00:44 +0800 | [diff] [blame] | 221 | int new_progress = ((size_ - remaining) * 1000) / size_; |
David Anderson | b2988ab | 2019-04-16 17:14:09 -0700 | [diff] [blame] | 222 | if (new_progress != progress) { |
Howard Chen | 18109b1 | 2019-08-13 17:00:44 +0800 | [diff] [blame] | 223 | service_->UpdateProgress(IGsiService::STATUS_WORKING, size_ - remaining); |
David Anderson | b2988ab | 2019-04-16 17:14:09 -0700 | [diff] [blame] | 224 | } |
| 225 | } |
| 226 | |
Howard Chen | 18109b1 | 2019-08-13 17:00:44 +0800 | [diff] [blame] | 227 | service_->UpdateProgress(IGsiService::STATUS_COMPLETE, size_); |
David Anderson | b2988ab | 2019-04-16 17:14:09 -0700 | [diff] [blame] | 228 | return true; |
| 229 | } |
| 230 | |
Howard Chen | 4663de6 | 2019-11-05 20:46:20 +0800 | [diff] [blame] | 231 | bool PartitionInstaller::IsFinishedWriting() { |
Howard Chen | 18109b1 | 2019-08-13 17:00:44 +0800 | [diff] [blame] | 232 | return gsi_bytes_written_ == size_; |
Howard Chen | 5676d96 | 2019-08-05 16:21:00 +0800 | [diff] [blame] | 233 | } |
| 234 | |
Howard Chen | 4663de6 | 2019-11-05 20:46:20 +0800 | [diff] [blame] | 235 | bool PartitionInstaller::IsAshmemMapped() { |
Howard Chen | 5676d96 | 2019-08-05 16:21:00 +0800 | [diff] [blame] | 236 | return ashmem_data_ != MAP_FAILED; |
| 237 | } |
| 238 | |
Howard Chen | 4663de6 | 2019-11-05 20:46:20 +0800 | [diff] [blame] | 239 | bool PartitionInstaller::CommitGsiChunk(const void* data, size_t bytes) { |
Howard Chen | 18109b1 | 2019-08-13 17:00:44 +0800 | [diff] [blame] | 240 | if (static_cast<uint64_t>(bytes) > size_ - gsi_bytes_written_) { |
David Anderson | b2988ab | 2019-04-16 17:14:09 -0700 | [diff] [blame] | 241 | // We cannot write past the end of the image file. |
Howard Chen | 18109b1 | 2019-08-13 17:00:44 +0800 | [diff] [blame] | 242 | LOG(ERROR) << "chunk size " << bytes << " exceeds remaining image size (" << size_ |
David Anderson | b2988ab | 2019-04-16 17:14:09 -0700 | [diff] [blame] | 243 | << " expected, " << gsi_bytes_written_ << " written)"; |
| 244 | return false; |
| 245 | } |
| 246 | if (service_->should_abort()) { |
| 247 | return false; |
| 248 | } |
David Anderson | 64b53fb | 2019-07-01 19:05:35 -0700 | [diff] [blame] | 249 | if (!android::base::WriteFully(system_device_->fd(), data, bytes)) { |
David Anderson | b2988ab | 2019-04-16 17:14:09 -0700 | [diff] [blame] | 250 | PLOG(ERROR) << "write failed"; |
| 251 | return false; |
| 252 | } |
| 253 | gsi_bytes_written_ += bytes; |
| 254 | return true; |
| 255 | } |
| 256 | |
Yo Chiang | 53bed1c | 2020-01-01 16:25:19 +0800 | [diff] [blame] | 257 | int PartitionInstaller::GetPartitionFd() { |
| 258 | return system_device_->fd(); |
| 259 | } |
| 260 | |
Howard Chen | 4663de6 | 2019-11-05 20:46:20 +0800 | [diff] [blame] | 261 | bool PartitionInstaller::MapAshmem(int fd, size_t size) { |
Howard Chen | 5676d96 | 2019-08-05 16:21:00 +0800 | [diff] [blame] | 262 | ashmem_size_ = size; |
| 263 | ashmem_data_ = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); |
| 264 | return ashmem_data_ != MAP_FAILED; |
| 265 | } |
| 266 | |
Howard Chen | 4663de6 | 2019-11-05 20:46:20 +0800 | [diff] [blame] | 267 | void PartitionInstaller::UnmapAshmem() { |
Howard Chen | 5676d96 | 2019-08-05 16:21:00 +0800 | [diff] [blame] | 268 | if (munmap(ashmem_data_, ashmem_size_) != 0) { |
| 269 | PLOG(ERROR) << "cannot munmap"; |
| 270 | return; |
| 271 | } |
| 272 | ashmem_data_ = MAP_FAILED; |
| 273 | ashmem_size_ = -1; |
| 274 | } |
| 275 | |
Howard Chen | 4663de6 | 2019-11-05 20:46:20 +0800 | [diff] [blame] | 276 | bool PartitionInstaller::CommitGsiChunk(size_t bytes) { |
Howard Chen | 5676d96 | 2019-08-05 16:21:00 +0800 | [diff] [blame] | 277 | if (!IsAshmemMapped()) { |
| 278 | PLOG(ERROR) << "ashmem is not mapped"; |
| 279 | return false; |
| 280 | } |
| 281 | bool success = CommitGsiChunk(ashmem_data_, bytes); |
| 282 | if (success && IsFinishedWriting()) { |
| 283 | UnmapAshmem(); |
| 284 | } |
| 285 | return success; |
| 286 | } |
| 287 | |
Howard Chen | 4663de6 | 2019-11-05 20:46:20 +0800 | [diff] [blame] | 288 | const std::string PartitionInstaller::GetBackingFile(std::string name) { |
Howard Chen | 18109b1 | 2019-08-13 17:00:44 +0800 | [diff] [blame] | 289 | return name + "_gsi"; |
| 290 | } |
| 291 | |
Howard Chen | 4663de6 | 2019-11-05 20:46:20 +0800 | [diff] [blame] | 292 | bool PartitionInstaller::Format() { |
Howard Chen | 18109b1 | 2019-08-13 17:00:44 +0800 | [diff] [blame] | 293 | auto file = GetBackingFile(name_); |
| 294 | auto device = OpenPartition(file); |
David Anderson | 64b53fb | 2019-07-01 19:05:35 -0700 | [diff] [blame] | 295 | if (!device) { |
David Anderson | b2988ab | 2019-04-16 17:14:09 -0700 | [diff] [blame] | 296 | return false; |
| 297 | } |
| 298 | |
| 299 | // libcutils checks the first 4K, no matter the block size. |
| 300 | std::string zeroes(4096, 0); |
David Anderson | 64b53fb | 2019-07-01 19:05:35 -0700 | [diff] [blame] | 301 | if (!android::base::WriteFully(device->fd(), zeroes.data(), zeroes.size())) { |
Howard Chen | 18109b1 | 2019-08-13 17:00:44 +0800 | [diff] [blame] | 302 | PLOG(ERROR) << "write " << file; |
David Anderson | b2988ab | 2019-04-16 17:14:09 -0700 | [diff] [blame] | 303 | return false; |
| 304 | } |
| 305 | return true; |
| 306 | } |
| 307 | |
Yo Chiang | 5369220 | 2020-08-24 17:05:00 +0800 | [diff] [blame] | 308 | int PartitionInstaller::CheckInstallState() { |
| 309 | if (readOnly_ && !IsFinishedWriting()) { |
David Anderson | b2988ab | 2019-04-16 17:14:09 -0700 | [diff] [blame] | 310 | // We cannot boot if the image is incomplete. |
Howard Chen | 18109b1 | 2019-08-13 17:00:44 +0800 | [diff] [blame] | 311 | LOG(ERROR) << "image incomplete; expected " << size_ << " bytes, waiting for " |
| 312 | << (size_ - gsi_bytes_written_) << " bytes"; |
David Anderson | b2988ab | 2019-04-16 17:14:09 -0700 | [diff] [blame] | 313 | return IGsiService::INSTALL_ERROR_GENERIC; |
| 314 | } |
Yo Chiang | 5369220 | 2020-08-24 17:05:00 +0800 | [diff] [blame] | 315 | if (system_device_ != nullptr && fsync(GetPartitionFd())) { |
| 316 | PLOG(ERROR) << "fsync failed for " << GetBackingFile(name_); |
David Anderson | b2988ab | 2019-04-16 17:14:09 -0700 | [diff] [blame] | 317 | return IGsiService::INSTALL_ERROR_GENERIC; |
| 318 | } |
David Anderson | b2988ab | 2019-04-16 17:14:09 -0700 | [diff] [blame] | 319 | // If files moved (are no longer pinned), the metadata file will be invalid. |
David Anderson | 64b53fb | 2019-07-01 19:05:35 -0700 | [diff] [blame] | 320 | // This check can be removed once b/133967059 is fixed. |
| 321 | if (!images_->Validate()) { |
| 322 | return IGsiService::INSTALL_ERROR_GENERIC; |
David Anderson | b2988ab | 2019-04-16 17:14:09 -0700 | [diff] [blame] | 323 | } |
David Anderson | b2988ab | 2019-04-16 17:14:09 -0700 | [diff] [blame] | 324 | return IGsiService::INSTALL_OK; |
| 325 | } |
| 326 | |
Howard Chen | ee5c2b1 | 2019-11-08 11:57:47 +0800 | [diff] [blame] | 327 | int PartitionInstaller::WipeWritable(const std::string& active_dsu, const std::string& install_dir, |
| 328 | const std::string& name) { |
| 329 | auto image = ImageManager::Open(MetadataDir(active_dsu), install_dir); |
Howard Chen | 73595fe | 2019-11-05 14:53:22 +0800 | [diff] [blame] | 330 | // The device object has to be destroyed before the image object |
| 331 | auto device = MappedDevice::Open(image.get(), 10s, name); |
David Anderson | 64b53fb | 2019-07-01 19:05:35 -0700 | [diff] [blame] | 332 | if (!device) { |
David Anderson | 8bdf625 | 2019-06-11 16:43:24 -0700 | [diff] [blame] | 333 | return IGsiService::INSTALL_ERROR_GENERIC; |
| 334 | } |
| 335 | |
| 336 | // Wipe the first 1MiB of the device, ensuring both the first block and |
| 337 | // the superblock are destroyed. |
| 338 | static constexpr uint64_t kEraseSize = 1024 * 1024; |
| 339 | |
| 340 | std::string zeroes(4096, 0); |
David Anderson | 64b53fb | 2019-07-01 19:05:35 -0700 | [diff] [blame] | 341 | uint64_t erase_size = std::min(kEraseSize, get_block_device_size(device->fd())); |
David Anderson | 8bdf625 | 2019-06-11 16:43:24 -0700 | [diff] [blame] | 342 | for (uint64_t i = 0; i < erase_size; i += zeroes.size()) { |
David Anderson | 64b53fb | 2019-07-01 19:05:35 -0700 | [diff] [blame] | 343 | if (!android::base::WriteFully(device->fd(), zeroes.data(), zeroes.size())) { |
Howard Chen | 46cc752 | 2020-03-03 13:28:37 +0800 | [diff] [blame] | 344 | PLOG(ERROR) << "write " << name; |
David Anderson | 8bdf625 | 2019-06-11 16:43:24 -0700 | [diff] [blame] | 345 | return IGsiService::INSTALL_ERROR_GENERIC; |
| 346 | } |
| 347 | } |
David Anderson | b2988ab | 2019-04-16 17:14:09 -0700 | [diff] [blame] | 348 | return IGsiService::INSTALL_OK; |
| 349 | } |
| 350 | |
Yi-Yo Chiang | 81af6b2 | 2021-11-03 20:26:32 +0800 | [diff] [blame] | 351 | std::optional<uint64_t> PartitionInstaller::GetMinimumFreeSpaceThreshold( |
| 352 | const std::string& install_dir) { |
| 353 | // No need to retain any space if we were not installing to the internal storage. |
| 354 | if (!android::base::StartsWith(install_dir, "/data"s)) { |
| 355 | return 0; |
| 356 | } |
| 357 | // Dynamic Partitions device must have a "super" block device. |
| 358 | BlockDeviceInfo info; |
| 359 | PartitionOpener opener; |
| 360 | if (!opener.GetInfo(fs_mgr_get_super_partition_name(), &info)) { |
| 361 | // We shouldn't reach here, but handle it just in case. |
| 362 | LOG(ERROR) << "could not get block device info of super"; |
| 363 | return std::nullopt; |
| 364 | } |
| 365 | // Reserve |super partition| of storage space so we don't disable VAB. |
| 366 | return info.size; |
| 367 | } |
| 368 | |
David Anderson | b2988ab | 2019-04-16 17:14:09 -0700 | [diff] [blame] | 369 | } // namespace gsi |
| 370 | } // namespace android |