Yifan Hong | dad0af8 | 2020-02-19 17:19:49 -0800 | [diff] [blame] | 1 | // |
| 2 | // Copyright (C) 2020 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 | // |
Amin Hassani | ec7bc11 | 2020-10-29 16:47:58 -0700 | [diff] [blame] | 16 | #include "update_engine/aosp/cleanup_previous_update_action.h" |
Yifan Hong | dad0af8 | 2020-02-19 17:19:49 -0800 | [diff] [blame] | 17 | |
Yifan Hong | d976cc5 | 2020-02-25 14:51:42 -0800 | [diff] [blame] | 18 | #include <chrono> // NOLINT(build/c++11) -- for merge times |
Yifan Hong | dad0af8 | 2020-02-19 17:19:49 -0800 | [diff] [blame] | 19 | #include <functional> |
| 20 | #include <string> |
Yifan Hong | 2403171 | 2020-03-19 19:25:38 -0700 | [diff] [blame] | 21 | #include <type_traits> |
Yifan Hong | dad0af8 | 2020-02-19 17:19:49 -0800 | [diff] [blame] | 22 | |
| 23 | #include <android-base/properties.h> |
| 24 | #include <base/bind.h> |
| 25 | |
Yifan Hong | d976cc5 | 2020-02-25 14:51:42 -0800 | [diff] [blame] | 26 | #ifndef __ANDROID_RECOVERY__ |
| 27 | #include <statslog.h> |
| 28 | #endif |
| 29 | |
Yifan Hong | dad0af8 | 2020-02-19 17:19:49 -0800 | [diff] [blame] | 30 | #include "update_engine/common/utils.h" |
| 31 | #include "update_engine/payload_consumer/delta_performer.h" |
| 32 | |
Howard Chen | 224aed9 | 2020-04-17 11:22:13 +0800 | [diff] [blame] | 33 | using android::base::GetBoolProperty; |
Yifan Hong | f9cb449 | 2020-04-15 13:00:20 -0700 | [diff] [blame] | 34 | using android::snapshot::ISnapshotManager; |
Yifan Hong | d976cc5 | 2020-02-25 14:51:42 -0800 | [diff] [blame] | 35 | using android::snapshot::SnapshotMergeStats; |
Yifan Hong | dad0af8 | 2020-02-19 17:19:49 -0800 | [diff] [blame] | 36 | using android::snapshot::UpdateState; |
| 37 | using brillo::MessageLoop; |
| 38 | |
| 39 | constexpr char kBootCompletedProp[] = "sys.boot_completed"; |
| 40 | // Interval to check sys.boot_completed. |
| 41 | constexpr auto kCheckBootCompletedInterval = base::TimeDelta::FromSeconds(2); |
| 42 | // Interval to check IBootControl::isSlotMarkedSuccessful |
| 43 | constexpr auto kCheckSlotMarkedSuccessfulInterval = |
| 44 | base::TimeDelta::FromSeconds(2); |
| 45 | // Interval to call SnapshotManager::ProcessUpdateState |
| 46 | constexpr auto kWaitForMergeInterval = base::TimeDelta::FromSeconds(2); |
| 47 | |
Yifan Hong | 5cd63fa | 2020-03-16 12:31:16 -0700 | [diff] [blame] | 48 | #ifdef __ANDROID_RECOVERY__ |
| 49 | static constexpr bool kIsRecovery = true; |
| 50 | #else |
| 51 | static constexpr bool kIsRecovery = false; |
| 52 | #endif |
| 53 | |
Yifan Hong | dad0af8 | 2020-02-19 17:19:49 -0800 | [diff] [blame] | 54 | namespace chromeos_update_engine { |
| 55 | |
| 56 | CleanupPreviousUpdateAction::CleanupPreviousUpdateAction( |
| 57 | PrefsInterface* prefs, |
| 58 | BootControlInterface* boot_control, |
Yifan Hong | f9cb449 | 2020-04-15 13:00:20 -0700 | [diff] [blame] | 59 | android::snapshot::ISnapshotManager* snapshot, |
Yifan Hong | dad0af8 | 2020-02-19 17:19:49 -0800 | [diff] [blame] | 60 | CleanupPreviousUpdateActionDelegateInterface* delegate) |
| 61 | : prefs_(prefs), |
| 62 | boot_control_(boot_control), |
| 63 | snapshot_(snapshot), |
| 64 | delegate_(delegate), |
| 65 | running_(false), |
| 66 | cancel_failed_(false), |
Yifan Hong | d976cc5 | 2020-02-25 14:51:42 -0800 | [diff] [blame] | 67 | last_percentage_(0), |
Yifan Hong | f9cb449 | 2020-04-15 13:00:20 -0700 | [diff] [blame] | 68 | merge_stats_(nullptr) {} |
Yifan Hong | dad0af8 | 2020-02-19 17:19:49 -0800 | [diff] [blame] | 69 | |
Yifan Hong | d1d52a0 | 2020-09-25 15:09:07 -0700 | [diff] [blame] | 70 | CleanupPreviousUpdateAction::~CleanupPreviousUpdateAction() { |
| 71 | StopActionInternal(); |
| 72 | } |
| 73 | |
Yifan Hong | dad0af8 | 2020-02-19 17:19:49 -0800 | [diff] [blame] | 74 | void CleanupPreviousUpdateAction::PerformAction() { |
Yifan Hong | d506dee | 2020-09-25 15:08:19 -0700 | [diff] [blame] | 75 | StartActionInternal(); |
Yifan Hong | dad0af8 | 2020-02-19 17:19:49 -0800 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | void CleanupPreviousUpdateAction::TerminateProcessing() { |
Yifan Hong | d506dee | 2020-09-25 15:08:19 -0700 | [diff] [blame] | 79 | StopActionInternal(); |
Yifan Hong | dad0af8 | 2020-02-19 17:19:49 -0800 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | void CleanupPreviousUpdateAction::ResumeAction() { |
Yifan Hong | dad0af8 | 2020-02-19 17:19:49 -0800 | [diff] [blame] | 83 | StartActionInternal(); |
| 84 | } |
| 85 | |
| 86 | void CleanupPreviousUpdateAction::SuspendAction() { |
Yifan Hong | d506dee | 2020-09-25 15:08:19 -0700 | [diff] [blame] | 87 | StopActionInternal(); |
Yifan Hong | dad0af8 | 2020-02-19 17:19:49 -0800 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | void CleanupPreviousUpdateAction::ActionCompleted(ErrorCode error_code) { |
Yifan Hong | d506dee | 2020-09-25 15:08:19 -0700 | [diff] [blame] | 91 | StopActionInternal(); |
Yifan Hong | d976cc5 | 2020-02-25 14:51:42 -0800 | [diff] [blame] | 92 | ReportMergeStats(); |
Yifan Hong | 5cd63fa | 2020-03-16 12:31:16 -0700 | [diff] [blame] | 93 | metadata_device_ = nullptr; |
Yifan Hong | dad0af8 | 2020-02-19 17:19:49 -0800 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | std::string CleanupPreviousUpdateAction::Type() const { |
| 97 | return StaticType(); |
| 98 | } |
| 99 | |
| 100 | std::string CleanupPreviousUpdateAction::StaticType() { |
| 101 | return "CleanupPreviousUpdateAction"; |
| 102 | } |
| 103 | |
Yifan Hong | d1d52a0 | 2020-09-25 15:09:07 -0700 | [diff] [blame] | 104 | // This function is called at the beginning of all delayed functions. By |
| 105 | // resetting |scheduled_task_|, the delayed function acknowledges that the task |
| 106 | // has already been executed, therefore there's no need to cancel it in the |
| 107 | // future. This avoids StopActionInternal() from resetting task IDs in an |
| 108 | // unexpected way because task IDs could be reused. |
| 109 | void CleanupPreviousUpdateAction::AcknowledgeTaskExecuted() { |
| 110 | if (scheduled_task_ != MessageLoop::kTaskIdNull) { |
| 111 | LOG(INFO) << "Executing task " << scheduled_task_; |
| 112 | } |
| 113 | scheduled_task_ = MessageLoop::kTaskIdNull; |
| 114 | } |
| 115 | |
| 116 | // Check that scheduled_task_ is a valid task ID. Otherwise, terminate the |
| 117 | // action. |
| 118 | void CleanupPreviousUpdateAction::CheckTaskScheduled(std::string_view name) { |
| 119 | if (scheduled_task_ == MessageLoop::kTaskIdNull) { |
| 120 | LOG(ERROR) << "Unable to schedule " << name; |
| 121 | processor_->ActionComplete(this, ErrorCode::kError); |
| 122 | } else { |
| 123 | LOG(INFO) << "CleanupPreviousUpdateAction scheduled task ID " |
| 124 | << scheduled_task_ << " for " << name; |
| 125 | } |
| 126 | } |
| 127 | |
Yifan Hong | d506dee | 2020-09-25 15:08:19 -0700 | [diff] [blame] | 128 | void CleanupPreviousUpdateAction::StopActionInternal() { |
| 129 | LOG(INFO) << "Stopping/suspending/completing CleanupPreviousUpdateAction"; |
| 130 | running_ = false; |
Yifan Hong | d1d52a0 | 2020-09-25 15:09:07 -0700 | [diff] [blame] | 131 | |
| 132 | if (scheduled_task_ != MessageLoop::kTaskIdNull) { |
| 133 | if (MessageLoop::current()->CancelTask(scheduled_task_)) { |
| 134 | LOG(INFO) << "CleanupPreviousUpdateAction cancelled pending task ID " |
| 135 | << scheduled_task_; |
| 136 | } else { |
| 137 | LOG(ERROR) << "CleanupPreviousUpdateAction unable to cancel task ID " |
| 138 | << scheduled_task_; |
| 139 | } |
| 140 | } |
| 141 | scheduled_task_ = MessageLoop::kTaskIdNull; |
Yifan Hong | d506dee | 2020-09-25 15:08:19 -0700 | [diff] [blame] | 142 | } |
| 143 | |
Yifan Hong | dad0af8 | 2020-02-19 17:19:49 -0800 | [diff] [blame] | 144 | void CleanupPreviousUpdateAction::StartActionInternal() { |
Yifan Hong | d506dee | 2020-09-25 15:08:19 -0700 | [diff] [blame] | 145 | CHECK(prefs_); |
| 146 | CHECK(boot_control_); |
| 147 | |
| 148 | LOG(INFO) << "Starting/resuming CleanupPreviousUpdateAction"; |
| 149 | running_ = true; |
Yifan Hong | dad0af8 | 2020-02-19 17:19:49 -0800 | [diff] [blame] | 150 | // Do nothing on non-VAB device. |
| 151 | if (!boot_control_->GetDynamicPartitionControl() |
| 152 | ->GetVirtualAbFeatureFlag() |
| 153 | .IsEnabled()) { |
| 154 | processor_->ActionComplete(this, ErrorCode::kSuccess); |
| 155 | return; |
| 156 | } |
Yifan Hong | f9cb449 | 2020-04-15 13:00:20 -0700 | [diff] [blame] | 157 | // SnapshotManager must be available on VAB devices. |
| 158 | CHECK(snapshot_ != nullptr); |
| 159 | merge_stats_ = snapshot_->GetSnapshotMergeStatsInstance(); |
| 160 | CHECK(merge_stats_ != nullptr); |
Yifan Hong | dad0af8 | 2020-02-19 17:19:49 -0800 | [diff] [blame] | 161 | WaitBootCompletedOrSchedule(); |
| 162 | } |
| 163 | |
| 164 | void CleanupPreviousUpdateAction::ScheduleWaitBootCompleted() { |
| 165 | TEST_AND_RETURN(running_); |
Yifan Hong | d1d52a0 | 2020-09-25 15:09:07 -0700 | [diff] [blame] | 166 | scheduled_task_ = MessageLoop::current()->PostDelayedTask( |
Yifan Hong | dad0af8 | 2020-02-19 17:19:49 -0800 | [diff] [blame] | 167 | FROM_HERE, |
| 168 | base::Bind(&CleanupPreviousUpdateAction::WaitBootCompletedOrSchedule, |
| 169 | base::Unretained(this)), |
| 170 | kCheckBootCompletedInterval); |
Yifan Hong | d1d52a0 | 2020-09-25 15:09:07 -0700 | [diff] [blame] | 171 | CheckTaskScheduled("WaitBootCompleted"); |
Yifan Hong | dad0af8 | 2020-02-19 17:19:49 -0800 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | void CleanupPreviousUpdateAction::WaitBootCompletedOrSchedule() { |
Yifan Hong | d1d52a0 | 2020-09-25 15:09:07 -0700 | [diff] [blame] | 175 | AcknowledgeTaskExecuted(); |
Yifan Hong | dad0af8 | 2020-02-19 17:19:49 -0800 | [diff] [blame] | 176 | TEST_AND_RETURN(running_); |
Yifan Hong | 5cd63fa | 2020-03-16 12:31:16 -0700 | [diff] [blame] | 177 | if (!kIsRecovery && |
| 178 | !android::base::GetBoolProperty(kBootCompletedProp, false)) { |
Yifan Hong | dad0af8 | 2020-02-19 17:19:49 -0800 | [diff] [blame] | 179 | // repeat |
| 180 | ScheduleWaitBootCompleted(); |
| 181 | return; |
| 182 | } |
| 183 | |
| 184 | LOG(INFO) << "Boot completed, waiting on markBootSuccessful()"; |
| 185 | CheckSlotMarkedSuccessfulOrSchedule(); |
| 186 | } |
| 187 | |
| 188 | void CleanupPreviousUpdateAction::ScheduleWaitMarkBootSuccessful() { |
| 189 | TEST_AND_RETURN(running_); |
Yifan Hong | d1d52a0 | 2020-09-25 15:09:07 -0700 | [diff] [blame] | 190 | scheduled_task_ = MessageLoop::current()->PostDelayedTask( |
Yifan Hong | dad0af8 | 2020-02-19 17:19:49 -0800 | [diff] [blame] | 191 | FROM_HERE, |
| 192 | base::Bind( |
| 193 | &CleanupPreviousUpdateAction::CheckSlotMarkedSuccessfulOrSchedule, |
| 194 | base::Unretained(this)), |
| 195 | kCheckSlotMarkedSuccessfulInterval); |
Yifan Hong | d1d52a0 | 2020-09-25 15:09:07 -0700 | [diff] [blame] | 196 | CheckTaskScheduled("WaitMarkBootSuccessful"); |
Yifan Hong | dad0af8 | 2020-02-19 17:19:49 -0800 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | void CleanupPreviousUpdateAction::CheckSlotMarkedSuccessfulOrSchedule() { |
Yifan Hong | d1d52a0 | 2020-09-25 15:09:07 -0700 | [diff] [blame] | 200 | AcknowledgeTaskExecuted(); |
Yifan Hong | dad0af8 | 2020-02-19 17:19:49 -0800 | [diff] [blame] | 201 | TEST_AND_RETURN(running_); |
Yifan Hong | 5cd63fa | 2020-03-16 12:31:16 -0700 | [diff] [blame] | 202 | if (!kIsRecovery && |
| 203 | !boot_control_->IsSlotMarkedSuccessful(boot_control_->GetCurrentSlot())) { |
Yifan Hong | dad0af8 | 2020-02-19 17:19:49 -0800 | [diff] [blame] | 204 | ScheduleWaitMarkBootSuccessful(); |
| 205 | } |
Yifan Hong | 5cd63fa | 2020-03-16 12:31:16 -0700 | [diff] [blame] | 206 | |
| 207 | if (metadata_device_ == nullptr) { |
| 208 | metadata_device_ = snapshot_->EnsureMetadataMounted(); |
| 209 | } |
| 210 | |
| 211 | if (metadata_device_ == nullptr) { |
| 212 | LOG(ERROR) << "Failed to mount /metadata."; |
Yifan Hong | 4d7c5eb | 2020-04-03 11:31:50 -0700 | [diff] [blame] | 213 | // If metadata is erased but not formatted, it is possible to not mount |
| 214 | // it in recovery. It is safe to skip CleanupPreviousUpdateAction. |
| 215 | processor_->ActionComplete( |
| 216 | this, kIsRecovery ? ErrorCode::kSuccess : ErrorCode::kError); |
Yifan Hong | 5cd63fa | 2020-03-16 12:31:16 -0700 | [diff] [blame] | 217 | return; |
| 218 | } |
| 219 | |
Yifan Hong | 2403171 | 2020-03-19 19:25:38 -0700 | [diff] [blame] | 220 | if (kIsRecovery) { |
| 221 | auto snapshots_created = |
| 222 | snapshot_->RecoveryCreateSnapshotDevices(metadata_device_); |
| 223 | switch (snapshots_created) { |
| 224 | case android::snapshot::CreateResult::CREATED: { |
| 225 | // If previous update has not finished merging, snapshots exists and are |
| 226 | // created here so that ProcessUpdateState can proceed. |
| 227 | LOG(INFO) << "Snapshot devices are created"; |
| 228 | break; |
| 229 | } |
| 230 | case android::snapshot::CreateResult::NOT_CREATED: { |
| 231 | // If there is no previous update, no snapshot devices are created and |
| 232 | // ProcessUpdateState will return immediately. Hence, NOT_CREATED is not |
| 233 | // considered an error. |
| 234 | LOG(INFO) << "Snapshot devices are not created"; |
| 235 | break; |
| 236 | } |
| 237 | case android::snapshot::CreateResult::ERROR: |
| 238 | default: { |
| 239 | LOG(ERROR) |
| 240 | << "Failed to create snapshot devices (CreateResult = " |
| 241 | << static_cast< |
| 242 | std::underlying_type_t<android::snapshot::CreateResult>>( |
| 243 | snapshots_created); |
| 244 | processor_->ActionComplete(this, ErrorCode::kError); |
| 245 | return; |
| 246 | } |
| 247 | } |
| 248 | } |
| 249 | |
Yifan Hong | d976cc5 | 2020-02-25 14:51:42 -0800 | [diff] [blame] | 250 | if (!merge_stats_->Start()) { |
| 251 | // Not an error because CleanupPreviousUpdateAction may be paused and |
| 252 | // resumed while kernel continues merging snapshots in the background. |
| 253 | LOG(WARNING) << "SnapshotMergeStats::Start failed."; |
| 254 | } |
Yifan Hong | dad0af8 | 2020-02-19 17:19:49 -0800 | [diff] [blame] | 255 | LOG(INFO) << "Waiting for any previous merge request to complete. " |
| 256 | << "This can take up to several minutes."; |
| 257 | WaitForMergeOrSchedule(); |
| 258 | } |
| 259 | |
| 260 | void CleanupPreviousUpdateAction::ScheduleWaitForMerge() { |
| 261 | TEST_AND_RETURN(running_); |
Yifan Hong | d1d52a0 | 2020-09-25 15:09:07 -0700 | [diff] [blame] | 262 | scheduled_task_ = MessageLoop::current()->PostDelayedTask( |
Yifan Hong | dad0af8 | 2020-02-19 17:19:49 -0800 | [diff] [blame] | 263 | FROM_HERE, |
| 264 | base::Bind(&CleanupPreviousUpdateAction::WaitForMergeOrSchedule, |
| 265 | base::Unretained(this)), |
| 266 | kWaitForMergeInterval); |
Yifan Hong | d1d52a0 | 2020-09-25 15:09:07 -0700 | [diff] [blame] | 267 | CheckTaskScheduled("WaitForMerge"); |
Yifan Hong | dad0af8 | 2020-02-19 17:19:49 -0800 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | void CleanupPreviousUpdateAction::WaitForMergeOrSchedule() { |
Yifan Hong | d1d52a0 | 2020-09-25 15:09:07 -0700 | [diff] [blame] | 271 | AcknowledgeTaskExecuted(); |
Yifan Hong | dad0af8 | 2020-02-19 17:19:49 -0800 | [diff] [blame] | 272 | TEST_AND_RETURN(running_); |
David Anderson | 9661fb3 | 2021-02-04 17:58:51 -0800 | [diff] [blame] | 273 | auto update_uses_compression = snapshot_->UpdateUsesCompression(); |
Yifan Hong | dad0af8 | 2020-02-19 17:19:49 -0800 | [diff] [blame] | 274 | auto state = snapshot_->ProcessUpdateState( |
| 275 | std::bind(&CleanupPreviousUpdateAction::OnMergePercentageUpdate, this), |
| 276 | std::bind(&CleanupPreviousUpdateAction::BeforeCancel, this)); |
David Anderson | 9661fb3 | 2021-02-04 17:58:51 -0800 | [diff] [blame] | 277 | merge_stats_->set_state(state, update_uses_compression); |
Yifan Hong | dad0af8 | 2020-02-19 17:19:49 -0800 | [diff] [blame] | 278 | |
Yifan Hong | dad0af8 | 2020-02-19 17:19:49 -0800 | [diff] [blame] | 279 | switch (state) { |
| 280 | case UpdateState::None: { |
| 281 | LOG(INFO) << "Can't find any snapshot to merge."; |
Yifan Hong | 16b594a | 2020-03-05 21:02:36 -0800 | [diff] [blame] | 282 | ErrorCode error_code = ErrorCode::kSuccess; |
| 283 | if (!snapshot_->CancelUpdate()) { |
| 284 | error_code = ErrorCode::kError; |
| 285 | LOG(INFO) << "Failed to call SnapshotManager::CancelUpdate()."; |
| 286 | } |
| 287 | processor_->ActionComplete(this, error_code); |
Yifan Hong | dad0af8 | 2020-02-19 17:19:49 -0800 | [diff] [blame] | 288 | return; |
| 289 | } |
| 290 | |
| 291 | case UpdateState::Initiated: { |
| 292 | LOG(ERROR) << "Previous update has not been completed, not cleaning up"; |
| 293 | processor_->ActionComplete(this, ErrorCode::kSuccess); |
| 294 | return; |
| 295 | } |
| 296 | |
| 297 | case UpdateState::Unverified: { |
| 298 | InitiateMergeAndWait(); |
| 299 | return; |
| 300 | } |
| 301 | |
| 302 | case UpdateState::Merging: { |
| 303 | ScheduleWaitForMerge(); |
| 304 | return; |
| 305 | } |
| 306 | |
| 307 | case UpdateState::MergeNeedsReboot: { |
| 308 | LOG(ERROR) << "Need reboot to finish merging."; |
| 309 | processor_->ActionComplete(this, ErrorCode::kError); |
| 310 | return; |
| 311 | } |
| 312 | |
| 313 | case UpdateState::MergeCompleted: { |
| 314 | LOG(INFO) << "Merge finished with state MergeCompleted."; |
| 315 | processor_->ActionComplete(this, ErrorCode::kSuccess); |
| 316 | return; |
| 317 | } |
| 318 | |
| 319 | case UpdateState::MergeFailed: { |
| 320 | LOG(ERROR) << "Merge failed. Device may be corrupted."; |
| 321 | processor_->ActionComplete(this, ErrorCode::kDeviceCorrupted); |
| 322 | return; |
| 323 | } |
| 324 | |
| 325 | case UpdateState::Cancelled: { |
| 326 | // DeltaPerformer::ResetUpdateProgress failed, hence snapshots are |
| 327 | // not deleted to avoid inconsistency. |
| 328 | // Nothing can be done here; just try next time. |
| 329 | ErrorCode error_code = |
| 330 | cancel_failed_ ? ErrorCode::kError : ErrorCode::kSuccess; |
| 331 | processor_->ActionComplete(this, error_code); |
| 332 | return; |
| 333 | } |
| 334 | |
| 335 | default: { |
| 336 | // Protobuf has some reserved enum values, so a default case is needed. |
| 337 | LOG(FATAL) << "SnapshotManager::ProcessUpdateState returns " |
| 338 | << static_cast<int32_t>(state); |
| 339 | } |
| 340 | } |
| 341 | } |
| 342 | |
| 343 | bool CleanupPreviousUpdateAction::OnMergePercentageUpdate() { |
| 344 | double percentage = 0.0; |
| 345 | snapshot_->GetUpdateState(&percentage); |
| 346 | if (delegate_) { |
| 347 | // libsnapshot uses [0, 100] percentage but update_engine uses [0, 1]. |
| 348 | delegate_->OnCleanupProgressUpdate(percentage / 100); |
| 349 | } |
| 350 | |
| 351 | // Log if percentage increments by at least 1. |
| 352 | if (last_percentage_ < static_cast<unsigned int>(percentage)) { |
| 353 | last_percentage_ = percentage; |
| 354 | LOG(INFO) << "Waiting for merge to complete: " << last_percentage_ << "%."; |
| 355 | } |
| 356 | |
| 357 | // Do not continue to wait for merge. Instead, let ProcessUpdateState |
| 358 | // return Merging directly so that we can ScheduleWaitForMerge() in |
| 359 | // MessageLoop. |
| 360 | return false; |
| 361 | } |
| 362 | |
| 363 | bool CleanupPreviousUpdateAction::BeforeCancel() { |
| 364 | if (DeltaPerformer::ResetUpdateProgress( |
| 365 | prefs_, |
| 366 | false /* quick */, |
| 367 | false /* skip dynamic partitions metadata*/)) { |
| 368 | return true; |
| 369 | } |
| 370 | |
| 371 | // ResetUpdateProgress might not work on stub prefs. Do additional checks. |
| 372 | LOG(WARNING) << "ProcessUpdateState returns Cancelled but cleanup failed."; |
| 373 | |
| 374 | std::string val; |
| 375 | ignore_result(prefs_->GetString(kPrefsDynamicPartitionMetadataUpdated, &val)); |
| 376 | if (val.empty()) { |
| 377 | LOG(INFO) << kPrefsDynamicPartitionMetadataUpdated |
| 378 | << " is empty, assuming successful cleanup"; |
| 379 | return true; |
| 380 | } |
| 381 | LOG(WARNING) |
| 382 | << kPrefsDynamicPartitionMetadataUpdated << " is " << val |
| 383 | << ", not deleting snapshots even though UpdateState is Cancelled."; |
| 384 | cancel_failed_ = true; |
| 385 | return false; |
| 386 | } |
| 387 | |
| 388 | void CleanupPreviousUpdateAction::InitiateMergeAndWait() { |
| 389 | TEST_AND_RETURN(running_); |
| 390 | LOG(INFO) << "Attempting to initiate merge."; |
Howard Chen | 224aed9 | 2020-04-17 11:22:13 +0800 | [diff] [blame] | 391 | // suspend the VAB merge when running a DSU |
| 392 | if (GetBoolProperty("ro.gsid.image_running", false)) { |
| 393 | LOG(WARNING) << "Suspend the VAB merge when running a DSU."; |
| 394 | processor_->ActionComplete(this, ErrorCode::kError); |
| 395 | return; |
| 396 | } |
Yifan Hong | dad0af8 | 2020-02-19 17:19:49 -0800 | [diff] [blame] | 397 | |
David Anderson | 8bda821 | 2021-03-03 18:33:38 -0800 | [diff] [blame^] | 398 | snapshot_->UpdateCowStats(merge_stats_); |
| 399 | |
| 400 | if (snapshot_->InitiateMerge()) { |
Yifan Hong | dad0af8 | 2020-02-19 17:19:49 -0800 | [diff] [blame] | 401 | WaitForMergeOrSchedule(); |
| 402 | return; |
| 403 | } |
| 404 | |
| 405 | LOG(WARNING) << "InitiateMerge failed."; |
| 406 | auto state = snapshot_->GetUpdateState(); |
David Anderson | 9661fb3 | 2021-02-04 17:58:51 -0800 | [diff] [blame] | 407 | merge_stats_->set_state(state, snapshot_->UpdateUsesCompression()); |
Yifan Hong | dad0af8 | 2020-02-19 17:19:49 -0800 | [diff] [blame] | 408 | if (state == UpdateState::Unverified) { |
| 409 | // We are stuck at unverified state. This can happen if the update has |
| 410 | // been applied, but it has not even been attempted yet (in libsnapshot, |
| 411 | // rollback indicator does not exist); for example, if update_engine |
| 412 | // restarts before the device reboots, then this state may be reached. |
| 413 | // Nothing should be done here. |
| 414 | LOG(WARNING) << "InitiateMerge leaves the device at " |
| 415 | << "UpdateState::Unverified. (Did update_engine " |
| 416 | << "restarted?)"; |
| 417 | processor_->ActionComplete(this, ErrorCode::kSuccess); |
| 418 | return; |
| 419 | } |
| 420 | |
| 421 | // State does seems to be advanced. |
| 422 | // It is possibly racy. For example, on a userdebug build, the user may |
| 423 | // manually initiate a merge with snapshotctl between last time |
| 424 | // update_engine checks UpdateState. Hence, just call |
| 425 | // WaitForMergeOrSchedule one more time. |
| 426 | LOG(WARNING) << "IniitateMerge failed but GetUpdateState returned " |
| 427 | << android::snapshot::UpdateState_Name(state) |
| 428 | << ", try to wait for merge again."; |
| 429 | WaitForMergeOrSchedule(); |
| 430 | return; |
| 431 | } |
| 432 | |
Yifan Hong | d976cc5 | 2020-02-25 14:51:42 -0800 | [diff] [blame] | 433 | void CleanupPreviousUpdateAction::ReportMergeStats() { |
| 434 | auto result = merge_stats_->Finish(); |
| 435 | if (result == nullptr) { |
| 436 | LOG(WARNING) << "Not reporting merge stats because " |
| 437 | "SnapshotMergeStats::Finish failed."; |
| 438 | return; |
| 439 | } |
| 440 | |
| 441 | #ifdef __ANDROID_RECOVERY__ |
| 442 | LOG(INFO) << "Skip reporting merge stats in recovery."; |
| 443 | #else |
| 444 | const auto& report = result->report(); |
| 445 | |
| 446 | if (report.state() == UpdateState::None || |
| 447 | report.state() == UpdateState::Initiated || |
| 448 | report.state() == UpdateState::Unverified) { |
| 449 | LOG(INFO) << "Not reporting merge stats because state is " |
| 450 | << android::snapshot::UpdateState_Name(report.state()); |
| 451 | return; |
| 452 | } |
| 453 | |
| 454 | auto passed_ms = std::chrono::duration_cast<std::chrono::milliseconds>( |
| 455 | result->merge_time()); |
Alessio Balsini | 4ed0512 | 2020-05-26 22:17:03 +0100 | [diff] [blame] | 456 | |
| 457 | bool vab_retrofit = boot_control_->GetDynamicPartitionControl() |
| 458 | ->GetVirtualAbFeatureFlag() |
| 459 | .IsRetrofit(); |
Yifan Hong | bab11c5 | 2021-02-03 15:05:05 -0800 | [diff] [blame] | 460 | bool vab_compression_enabled = boot_control_->GetDynamicPartitionControl() |
| 461 | ->GetVirtualAbCompressionFeatureFlag() |
| 462 | .IsEnabled(); |
| 463 | // The snapshot has been merged, so we can no longer call |
| 464 | // DynamicPartitionControlInterface::UpdateUsesSnapshotCompression. |
| 465 | // However, we have saved the flag in the snapshot report. |
| 466 | bool vab_compression_used = report.compression_enabled(); |
Alessio Balsini | 4ed0512 | 2020-05-26 22:17:03 +0100 | [diff] [blame] | 467 | |
Yifan Hong | d976cc5 | 2020-02-25 14:51:42 -0800 | [diff] [blame] | 468 | LOG(INFO) << "Reporting merge stats: " |
| 469 | << android::snapshot::UpdateState_Name(report.state()) << " in " |
| 470 | << passed_ms.count() << "ms (resumed " << report.resume_count() |
Alessio Balsini | 4ed0512 | 2020-05-26 22:17:03 +0100 | [diff] [blame] | 471 | << " times), using " << report.cow_file_size() |
| 472 | << " bytes of COW image."; |
Yifan Hong | be59a00 | 2020-03-02 15:45:14 -0800 | [diff] [blame] | 473 | android::util::stats_write(android::util::SNAPSHOT_MERGE_REPORTED, |
| 474 | static_cast<int32_t>(report.state()), |
| 475 | static_cast<int64_t>(passed_ms.count()), |
Alessio Balsini | 4ed0512 | 2020-05-26 22:17:03 +0100 | [diff] [blame] | 476 | static_cast<int32_t>(report.resume_count()), |
| 477 | vab_retrofit, |
Yifan Hong | bab11c5 | 2021-02-03 15:05:05 -0800 | [diff] [blame] | 478 | static_cast<int64_t>(report.cow_file_size()), |
| 479 | vab_compression_enabled, |
| 480 | vab_compression_used); |
Yifan Hong | d976cc5 | 2020-02-25 14:51:42 -0800 | [diff] [blame] | 481 | #endif |
| 482 | } |
| 483 | |
Yifan Hong | dad0af8 | 2020-02-19 17:19:49 -0800 | [diff] [blame] | 484 | } // namespace chromeos_update_engine |