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