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