blob: 5450169205508a0daa80d6a687e723e1333ebcd9 [file] [log] [blame]
David Andersonb2988ab2019-04-16 17:14:09 -07001/*
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 Chen4663de62019-11-05 20:46:20 +080017#include "partition_installer.h"
David Andersonb2988ab2019-04-16 17:14:09 -070018
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 Anderson8bdf6252019-06-11 16:43:24 -070024#include <ext4_utils/ext4_utils.h>
Yi-Yo Chiang81af6b22021-11-03 20:26:32 +080025#include <fs_mgr.h>
David Andersonb2988ab2019-04-16 17:14:09 -070026#include <fs_mgr_dm_linear.h>
27#include <libdm/dm.h>
28#include <libgsi/libgsi.h>
Yi-Yo Chiang81af6b22021-11-03 20:26:32 +080029#include <liblp/partition_opener.h>
David Andersonb2988ab2019-04-16 17:14:09 -070030
31#include "file_paths.h"
32#include "gsi_service.h"
33#include "libgsi_private.h"
34
35namespace android {
36namespace gsi {
37
38using namespace std::literals;
39using namespace android::dm;
David Anderson9ca77282019-07-15 23:56:13 +000040using namespace android::fiemap;
David Andersonb2988ab2019-04-16 17:14:09 -070041using namespace android::fs_mgr;
42using android::base::unique_fd;
43
Howard Chen4663de62019-11-05 20:46:20 +080044PartitionInstaller::PartitionInstaller(GsiService* service, const std::string& install_dir,
Howard Chenee5c2b12019-11-08 11:57:47 +080045 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 Andersonb2988ab2019-04-16 17:14:09 -070054}
55
Howard Chen4663de62019-11-05 20:46:20 +080056PartitionInstaller::~PartitionInstaller() {
Yo Chiangf194dce2020-08-24 17:21:10 +080057 if (FinishInstall() != IGsiService::INSTALL_OK) {
Yo Chiang281584b2020-08-24 16:50:20 +080058 LOG(ERROR) << "Installation failed: install_dir=" << install_dir_
59 << ", dsu_slot=" << active_dsu_ << ", partition_name=" << name_;
David Andersonb2988ab2019-04-16 17:14:09 -070060 }
Howard Chen5676d962019-08-05 16:21:00 +080061 if (IsAshmemMapped()) {
62 UnmapAshmem();
63 }
David Andersonb2988ab2019-04-16 17:14:09 -070064}
65
Yo Chiangf194dce2020-08-24 17:21:10 +080066int PartitionInstaller::FinishInstall() {
67 if (finished_) {
68 return finished_status_;
David Andersonb2988ab2019-04-16 17:14:09 -070069 }
Yo Chiangf194dce2020-08-24 17:21:10 +080070 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 Andersonb2988ab2019-04-16 17:14:09 -070081 }
Yo Chiangf194dce2020-08-24 17:21:10 +080082 return finished_status_;
David Andersonb2988ab2019-04-16 17:14:09 -070083}
84
Howard Chen4663de62019-11-05 20:46:20 +080085int PartitionInstaller::StartInstall() {
David Andersonb2988ab2019-04-16 17:14:09 -070086 if (int status = PerformSanityChecks()) {
87 return status;
88 }
Howard Chen18109b12019-08-13 17:00:44 +080089 if (int status = Preallocate()) {
David Andersonb2988ab2019-04-16 17:14:09 -070090 return status;
91 }
Howard Chen18109b12019-08-13 17:00:44 +080092 if (!readOnly_) {
93 if (!Format()) {
94 return IGsiService::INSTALL_ERROR_GENERIC;
95 }
Howard Chen18109b12019-08-13 17:00:44 +080096 } 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 Andersonb2988ab2019-04-16 17:14:09 -0700102
Howard Chen18109b12019-08-13 17:00:44 +0800103 // Clear the progress indicator.
104 service_->UpdateProgress(IGsiService::STATUS_NO_OPERATION, 0);
David Andersonb2988ab2019-04-16 17:14:09 -0700105 }
David Andersonb2988ab2019-04-16 17:14:09 -0700106 return IGsiService::INSTALL_OK;
107}
108
Howard Chen4663de62019-11-05 20:46:20 +0800109int PartitionInstaller::PerformSanityChecks() {
David Anderson64b53fb2019-07-01 19:05:35 -0700110 if (!images_) {
111 LOG(ERROR) << "unable to create image manager";
112 return IGsiService::INSTALL_ERROR_GENERIC;
113 }
Howard Chen18109b12019-08-13 17:00:44 +0800114 if (size_ < 0) {
115 LOG(ERROR) << "image size " << size_ << " is negative";
David Andersonb2988ab2019-04-16 17:14:09 -0700116 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 Chiang23a996f2021-02-12 01:57:44 +0800131 uint64_t free_space = static_cast<uint64_t>(sb.f_bavail) * sb.f_frsize;
Howard Chen18109b12019-08-13 17:00:44 +0800132 if (free_space <= (size_)) {
David Andersonb2988ab2019-04-16 17:14:09 -0700133 LOG(ERROR) << "not enough free space (only " << free_space << " bytes available)";
134 return IGsiService::INSTALL_ERROR_NO_SPACE;
135 }
Yi-Yo Chiang81af6b22021-11-03 20:26:32 +0800136
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 Andersonb2988ab2019-04-16 17:14:09 -0700144 return IGsiService::INSTALL_ERROR_FILE_SYSTEM_CLUTTERED;
145 }
146 return IGsiService::INSTALL_OK;
147}
148
Howard Chen4663de62019-11-05 20:46:20 +0800149int PartitionInstaller::Preallocate() {
Howard Chen18109b12019-08-13 17:00:44 +0800150 std::string file = GetBackingFile(name_);
Howard Chen4663de62019-11-05 20:46:20 +0800151 if (!images_->UnmapImageIfExists(file)) {
152 LOG(ERROR) << "failed to UnmapImageIfExists " << file;
153 return IGsiService::INSTALL_ERROR_GENERIC;
David Andersonb2988ab2019-04-16 17:14:09 -0700154 }
Howard Chen4663de62019-11-05 20:46:20 +0800155 // 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 Chen18109b12019-08-13 17:00:44 +0800160 return IGsiService::INSTALL_ERROR_GENERIC;
161 }
David Andersonb2988ab2019-04-16 17:14:09 -0700162 }
Howard Chen4663de62019-11-05 20:46:20 +0800163 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 Andersonb2988ab2019-04-16 17:14:09 -0700168 service_->UpdateProgress(IGsiService::STATUS_COMPLETE, 0);
169 return IGsiService::INSTALL_OK;
170}
171
Howard Chen4663de62019-11-05 20:46:20 +0800172bool PartitionInstaller::CreateImage(const std::string& name, uint64_t size) {
David Anderson64b53fb2019-07-01 19:05:35 -0700173 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 Anderson1fdec262019-07-24 14:03:49 -0700178 int flags = ImageManager::CREATE_IMAGE_DEFAULT;
Howard Chen18109b12019-08-13 17:00:44 +0800179 if (readOnly_) {
David Anderson1fdec262019-07-24 14:03:49 -0700180 flags |= ImageManager::CREATE_IMAGE_READONLY;
181 }
182 return images_->CreateBackingImage(name, size, flags, std::move(progress));
David Andersonb2988ab2019-04-16 17:14:09 -0700183}
184
Howard Chen4663de62019-11-05 20:46:20 +0800185std::unique_ptr<MappedDevice> PartitionInstaller::OpenPartition(const std::string& name) {
Howard Chen73595fe2019-11-05 14:53:22 +0800186 return MappedDevice::Open(images_.get(), 10s, name);
David Andersonb2988ab2019-04-16 17:14:09 -0700187}
188
Howard Chen4663de62019-11-05 20:46:20 +0800189bool PartitionInstaller::CommitGsiChunk(int stream_fd, int64_t bytes) {
Howard Chen18109b12019-08-13 17:00:44 +0800190 service_->StartAsyncOperation("write " + name_, size_);
David Andersonb2988ab2019-04-16 17:14:09 -0700191
192 if (bytes < 0) {
193 LOG(ERROR) << "chunk size " << bytes << " is negative";
194 return false;
195 }
196
David Anderson64b53fb2019-07-01 19:05:35 -0700197 static const size_t kBlockSize = 4096;
198 auto buffer = std::make_unique<char[]>(kBlockSize);
David Andersonb2988ab2019-04-16 17:14:09 -0700199
200 int progress = -1;
201 uint64_t remaining = bytes;
202 while (remaining) {
David Anderson64b53fb2019-07-01 19:05:35 -0700203 size_t max_to_read = std::min(static_cast<uint64_t>(kBlockSize), remaining);
David Andersonb2988ab2019-04-16 17:14:09 -0700204 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 Chen18109b12019-08-13 17:00:44 +0800221 int new_progress = ((size_ - remaining) * 1000) / size_;
David Andersonb2988ab2019-04-16 17:14:09 -0700222 if (new_progress != progress) {
Howard Chen18109b12019-08-13 17:00:44 +0800223 service_->UpdateProgress(IGsiService::STATUS_WORKING, size_ - remaining);
David Andersonb2988ab2019-04-16 17:14:09 -0700224 }
225 }
226
Howard Chen18109b12019-08-13 17:00:44 +0800227 service_->UpdateProgress(IGsiService::STATUS_COMPLETE, size_);
David Andersonb2988ab2019-04-16 17:14:09 -0700228 return true;
229}
230
Howard Chen4663de62019-11-05 20:46:20 +0800231bool PartitionInstaller::IsFinishedWriting() {
Howard Chen18109b12019-08-13 17:00:44 +0800232 return gsi_bytes_written_ == size_;
Howard Chen5676d962019-08-05 16:21:00 +0800233}
234
Howard Chen4663de62019-11-05 20:46:20 +0800235bool PartitionInstaller::IsAshmemMapped() {
Howard Chen5676d962019-08-05 16:21:00 +0800236 return ashmem_data_ != MAP_FAILED;
237}
238
Howard Chen4663de62019-11-05 20:46:20 +0800239bool PartitionInstaller::CommitGsiChunk(const void* data, size_t bytes) {
Howard Chen18109b12019-08-13 17:00:44 +0800240 if (static_cast<uint64_t>(bytes) > size_ - gsi_bytes_written_) {
David Andersonb2988ab2019-04-16 17:14:09 -0700241 // We cannot write past the end of the image file.
Howard Chen18109b12019-08-13 17:00:44 +0800242 LOG(ERROR) << "chunk size " << bytes << " exceeds remaining image size (" << size_
David Andersonb2988ab2019-04-16 17:14:09 -0700243 << " expected, " << gsi_bytes_written_ << " written)";
244 return false;
245 }
246 if (service_->should_abort()) {
247 return false;
248 }
David Anderson64b53fb2019-07-01 19:05:35 -0700249 if (!android::base::WriteFully(system_device_->fd(), data, bytes)) {
David Andersonb2988ab2019-04-16 17:14:09 -0700250 PLOG(ERROR) << "write failed";
251 return false;
252 }
253 gsi_bytes_written_ += bytes;
254 return true;
255}
256
Yo Chiang53bed1c2020-01-01 16:25:19 +0800257int PartitionInstaller::GetPartitionFd() {
258 return system_device_->fd();
259}
260
Howard Chen4663de62019-11-05 20:46:20 +0800261bool PartitionInstaller::MapAshmem(int fd, size_t size) {
Howard Chen5676d962019-08-05 16:21:00 +0800262 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 Chen4663de62019-11-05 20:46:20 +0800267void PartitionInstaller::UnmapAshmem() {
Howard Chen5676d962019-08-05 16:21:00 +0800268 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 Chen4663de62019-11-05 20:46:20 +0800276bool PartitionInstaller::CommitGsiChunk(size_t bytes) {
Howard Chen5676d962019-08-05 16:21:00 +0800277 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 Chen4663de62019-11-05 20:46:20 +0800288const std::string PartitionInstaller::GetBackingFile(std::string name) {
Howard Chen18109b12019-08-13 17:00:44 +0800289 return name + "_gsi";
290}
291
Howard Chen4663de62019-11-05 20:46:20 +0800292bool PartitionInstaller::Format() {
Howard Chen18109b12019-08-13 17:00:44 +0800293 auto file = GetBackingFile(name_);
294 auto device = OpenPartition(file);
David Anderson64b53fb2019-07-01 19:05:35 -0700295 if (!device) {
David Andersonb2988ab2019-04-16 17:14:09 -0700296 return false;
297 }
298
299 // libcutils checks the first 4K, no matter the block size.
300 std::string zeroes(4096, 0);
David Anderson64b53fb2019-07-01 19:05:35 -0700301 if (!android::base::WriteFully(device->fd(), zeroes.data(), zeroes.size())) {
Howard Chen18109b12019-08-13 17:00:44 +0800302 PLOG(ERROR) << "write " << file;
David Andersonb2988ab2019-04-16 17:14:09 -0700303 return false;
304 }
305 return true;
306}
307
Yo Chiang53692202020-08-24 17:05:00 +0800308int PartitionInstaller::CheckInstallState() {
309 if (readOnly_ && !IsFinishedWriting()) {
David Andersonb2988ab2019-04-16 17:14:09 -0700310 // We cannot boot if the image is incomplete.
Howard Chen18109b12019-08-13 17:00:44 +0800311 LOG(ERROR) << "image incomplete; expected " << size_ << " bytes, waiting for "
312 << (size_ - gsi_bytes_written_) << " bytes";
David Andersonb2988ab2019-04-16 17:14:09 -0700313 return IGsiService::INSTALL_ERROR_GENERIC;
314 }
Yo Chiang53692202020-08-24 17:05:00 +0800315 if (system_device_ != nullptr && fsync(GetPartitionFd())) {
316 PLOG(ERROR) << "fsync failed for " << GetBackingFile(name_);
David Andersonb2988ab2019-04-16 17:14:09 -0700317 return IGsiService::INSTALL_ERROR_GENERIC;
318 }
David Andersonb2988ab2019-04-16 17:14:09 -0700319 // If files moved (are no longer pinned), the metadata file will be invalid.
David Anderson64b53fb2019-07-01 19:05:35 -0700320 // This check can be removed once b/133967059 is fixed.
321 if (!images_->Validate()) {
322 return IGsiService::INSTALL_ERROR_GENERIC;
David Andersonb2988ab2019-04-16 17:14:09 -0700323 }
David Andersonb2988ab2019-04-16 17:14:09 -0700324 return IGsiService::INSTALL_OK;
325}
326
Howard Chenee5c2b12019-11-08 11:57:47 +0800327int 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 Chen73595fe2019-11-05 14:53:22 +0800330 // The device object has to be destroyed before the image object
331 auto device = MappedDevice::Open(image.get(), 10s, name);
David Anderson64b53fb2019-07-01 19:05:35 -0700332 if (!device) {
David Anderson8bdf6252019-06-11 16:43:24 -0700333 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 Anderson64b53fb2019-07-01 19:05:35 -0700341 uint64_t erase_size = std::min(kEraseSize, get_block_device_size(device->fd()));
David Anderson8bdf6252019-06-11 16:43:24 -0700342 for (uint64_t i = 0; i < erase_size; i += zeroes.size()) {
David Anderson64b53fb2019-07-01 19:05:35 -0700343 if (!android::base::WriteFully(device->fd(), zeroes.data(), zeroes.size())) {
Howard Chen46cc7522020-03-03 13:28:37 +0800344 PLOG(ERROR) << "write " << name;
David Anderson8bdf6252019-06-11 16:43:24 -0700345 return IGsiService::INSTALL_ERROR_GENERIC;
346 }
347 }
David Andersonb2988ab2019-04-16 17:14:09 -0700348 return IGsiService::INSTALL_OK;
349}
350
Yi-Yo Chiang81af6b22021-11-03 20:26:32 +0800351std::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 Andersonb2988ab2019-04-16 17:14:09 -0700369} // namespace gsi
370} // namespace android