Alex Deymo | aea4c1c | 2015-08-19 20:24:43 -0700 | [diff] [blame] | 1 | // |
| 2 | // Copyright (C) 2011 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 | // |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 16 | |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 17 | #include "update_engine/payload_consumer/postinstall_runner_action.h" |
Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 18 | |
Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 19 | #include <fcntl.h> |
Alex Deymo | d15c546 | 2016-03-09 18:11:12 -0800 | [diff] [blame] | 20 | #include <signal.h> |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 21 | #include <stdlib.h> |
Luca Stefani | d1ddfee | 2019-01-03 21:20:42 +0100 | [diff] [blame^] | 22 | #include <selinux/selinux.h> |
Alex Vakulenko | 44cab30 | 2014-07-23 13:12:15 -0700 | [diff] [blame] | 23 | #include <sys/mount.h> |
Alex Deymo | d15c546 | 2016-03-09 18:11:12 -0800 | [diff] [blame] | 24 | #include <sys/types.h> |
Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 25 | #include <unistd.h> |
Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 26 | |
Alex Deymo | 44b3567 | 2016-04-05 17:57:48 -0700 | [diff] [blame] | 27 | #include <cmath> |
Kelvin Zhang | ddc2580 | 2021-12-30 13:05:27 -0800 | [diff] [blame] | 28 | #include <fstream> |
| 29 | #include <string> |
Alex Deymo | 44b3567 | 2016-04-05 17:57:48 -0700 | [diff] [blame] | 30 | |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 31 | #include <base/files/file_path.h> |
| 32 | #include <base/files/file_util.h> |
Alex Deymo | d15c546 | 2016-03-09 18:11:12 -0800 | [diff] [blame] | 33 | #include <base/logging.h> |
hscham | 00b6aa2 | 2020-02-20 12:32:06 +0900 | [diff] [blame] | 34 | #include <base/stl_util.h> |
Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 35 | #include <base/strings/string_split.h> |
Alex Deymo | 390efed | 2016-02-18 11:00:40 -0800 | [diff] [blame] | 36 | #include <base/strings/string_util.h> |
Alex Deymo | 461b259 | 2015-07-24 20:10:52 -0700 | [diff] [blame] | 37 | |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 38 | #include "update_engine/common/action_processor.h" |
Alex Deymo | b15a0b8 | 2015-11-25 20:30:40 -0300 | [diff] [blame] | 39 | #include "update_engine/common/boot_control_interface.h" |
Kelvin Zhang | 99cbbe7 | 2024-01-18 14:50:01 -0800 | [diff] [blame] | 40 | #include "update_engine/common/error_code_utils.h" |
| 41 | #include "update_engine/common/platform_constants.h" |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 42 | #include "update_engine/common/subprocess.h" |
| 43 | #include "update_engine/common/utils.h" |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 44 | |
Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 45 | namespace { |
| 46 | |
| 47 | // The file descriptor number from the postinstall program's perspective where |
| 48 | // it can report status updates. This can be any number greater than 2 (stderr), |
| 49 | // but must be kept in sync with the "bin/postinst_progress" defined in the |
| 50 | // sample_images.sh file. |
| 51 | const int kPostinstallStatusFd = 3; |
| 52 | |
Kelvin Zhang | ddc2580 | 2021-12-30 13:05:27 -0800 | [diff] [blame] | 53 | static constexpr bool Contains(std::string_view haystack, |
| 54 | std::string_view needle) { |
| 55 | return haystack.find(needle) != std::string::npos; |
| 56 | } |
| 57 | |
| 58 | static void LogBuildInfoForPartition(std::string_view mount_point) { |
| 59 | static constexpr std::array<std::string_view, 3> kBuildPropFiles{ |
| 60 | "build.prop", "etc/build.prop", "system/build.prop"}; |
| 61 | for (const auto& file : kBuildPropFiles) { |
| 62 | auto path = std::string(mount_point); |
| 63 | if (path.back() != '/') { |
| 64 | path.push_back('/'); |
| 65 | } |
| 66 | path += file; |
| 67 | LOG(INFO) << "Trying to read " << path; |
| 68 | std::ifstream infile(path); |
| 69 | std::string line; |
| 70 | while (std::getline(infile, line)) { |
| 71 | if (Contains(line, "ro.build")) { |
| 72 | LOG(INFO) << line; |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | |
Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 78 | } // namespace |
| 79 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 80 | namespace chromeos_update_engine { |
| 81 | |
| 82 | using std::string; |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 83 | using std::vector; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 84 | |
Kelvin Zhang | e9def4e | 2020-12-02 14:04:09 -0500 | [diff] [blame] | 85 | PostinstallRunnerAction::PostinstallRunnerAction( |
| 86 | BootControlInterface* boot_control, HardwareInterface* hardware) |
| 87 | : boot_control_(boot_control), hardware_(hardware) { |
| 88 | #ifdef __ANDROID__ |
| 89 | fs_mount_dir_ = "/postinstall"; |
| 90 | #else // __ANDROID__ |
| 91 | base::FilePath temp_dir; |
| 92 | TEST_AND_RETURN(base::CreateNewTempDirectory("au_postint_mount", &temp_dir)); |
| 93 | fs_mount_dir_ = temp_dir.value(); |
| 94 | #endif // __ANDROID__ |
Kelvin Zhang | 2379fa9 | 2020-12-09 14:39:04 -0500 | [diff] [blame] | 95 | CHECK(!fs_mount_dir_.empty()); |
Kelvin Zhang | 1df000a | 2022-02-09 16:00:17 -0800 | [diff] [blame] | 96 | EnsureUnmounted(); |
Kelvin Zhang | 2379fa9 | 2020-12-09 14:39:04 -0500 | [diff] [blame] | 97 | LOG(INFO) << "postinstall mount point: " << fs_mount_dir_; |
Kelvin Zhang | e9def4e | 2020-12-02 14:04:09 -0500 | [diff] [blame] | 98 | } |
| 99 | |
Kelvin Zhang | 1df000a | 2022-02-09 16:00:17 -0800 | [diff] [blame] | 100 | void PostinstallRunnerAction::EnsureUnmounted() { |
| 101 | if (utils::IsMountpoint(fs_mount_dir_)) { |
| 102 | LOG(INFO) << "Found previously mounted filesystem at " << fs_mount_dir_; |
| 103 | utils::UnmountFilesystem(fs_mount_dir_); |
| 104 | } |
| 105 | } |
| 106 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 107 | void PostinstallRunnerAction::PerformAction() { |
| 108 | CHECK(HasInputObject()); |
Kelvin Zhang | 8b1e0dc | 2020-10-26 12:27:53 -0400 | [diff] [blame] | 109 | CHECK(boot_control_); |
Chris Sosa | d317e40 | 2013-06-12 13:47:09 -0700 | [diff] [blame] | 110 | install_plan_ = GetInputObject(); |
Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 111 | |
Kelvin Zhang | 8b1e0dc | 2020-10-26 12:27:53 -0400 | [diff] [blame] | 112 | auto dynamic_control = boot_control_->GetDynamicPartitionControl(); |
| 113 | CHECK(dynamic_control); |
| 114 | |
Kelvin Zhang | 19acf4f | 2024-01-08 21:18:28 +0000 | [diff] [blame] | 115 | // Mount snapshot partitions for Virtual AB Compression Compression. |
Kelvin Zhang | 0618835 | 2021-02-10 13:21:47 -0500 | [diff] [blame] | 116 | if (dynamic_control->UpdateUsesSnapshotCompression()) { |
Kelvin Zhang | 8b1e0dc | 2020-10-26 12:27:53 -0400 | [diff] [blame] | 117 | // Before calling MapAllPartitions to map snapshot devices, all CowWriters |
| 118 | // must be closed, and MapAllPartitions() should be called. |
Kelvin Zhang | 263a540 | 2022-12-08 23:03:32 -0800 | [diff] [blame] | 119 | if (!install_plan_.partitions.empty()) { |
Kelvin Zhang | 263a540 | 2022-12-08 23:03:32 -0800 | [diff] [blame] | 120 | if (!dynamic_control->MapAllPartitions()) { |
| 121 | return CompletePostinstall(ErrorCode::kPostInstallMountError); |
| 122 | } |
Kelvin Zhang | 8b1e0dc | 2020-10-26 12:27:53 -0400 | [diff] [blame] | 123 | } |
| 124 | } |
| 125 | |
Zentaro Kavanagh | 28def4f | 2019-01-15 17:15:01 -0800 | [diff] [blame] | 126 | // We always powerwash when rolling back, however policy can determine |
| 127 | // if this is a full/normal powerwash, or a special rollback powerwash |
| 128 | // that retains a small amount of system state such as enrollment and |
| 129 | // network configuration. In both cases all user accounts are deleted. |
Marton Hunyady | 199152d | 2018-05-07 19:08:48 +0200 | [diff] [blame] | 130 | if (install_plan_.powerwash_required || install_plan_.is_rollback) { |
Miriam Polzer | aff7200 | 2020-08-27 08:20:39 +0200 | [diff] [blame] | 131 | if (hardware_->SchedulePowerwash( |
| 132 | install_plan_.rollback_data_save_requested)) { |
Alex Deymo | fb905d9 | 2016-06-03 19:26:58 -0700 | [diff] [blame] | 133 | powerwash_scheduled_ = true; |
Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 134 | } else { |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 135 | return CompletePostinstall(ErrorCode::kPostinstallPowerwashError); |
Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 136 | } |
| 137 | } |
| 138 | |
Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 139 | // Initialize all the partition weights. |
| 140 | partition_weight_.resize(install_plan_.partitions.size()); |
| 141 | total_weight_ = 0; |
| 142 | for (size_t i = 0; i < install_plan_.partitions.size(); ++i) { |
Tianjie Xu | 087de9d | 2019-11-01 17:11:22 -0700 | [diff] [blame] | 143 | auto& partition = install_plan_.partitions[i]; |
| 144 | if (!install_plan_.run_post_install && partition.postinstall_optional) { |
| 145 | partition.run_postinstall = false; |
| 146 | LOG(INFO) << "Skipping optional post-install for partition " |
| 147 | << partition.name << " according to install plan."; |
| 148 | } |
| 149 | |
Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 150 | // TODO(deymo): This code sets the weight to all the postinstall commands, |
| 151 | // but we could remember how long they took in the past and use those |
| 152 | // values. |
Tianjie Xu | 087de9d | 2019-11-01 17:11:22 -0700 | [diff] [blame] | 153 | partition_weight_[i] = partition.run_postinstall; |
Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 154 | total_weight_ += partition_weight_[i]; |
| 155 | } |
| 156 | accumulated_weight_ = 0; |
| 157 | ReportProgress(0); |
| 158 | |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 159 | PerformPartitionPostinstall(); |
| 160 | } |
| 161 | |
Kelvin Zhang | 06e654a | 2021-09-10 13:21:00 -0700 | [diff] [blame] | 162 | bool PostinstallRunnerAction::MountPartition( |
| 163 | const InstallPlan::Partition& partition) noexcept { |
| 164 | // Perform post-install for the current_partition_ partition. At this point we |
| 165 | // need to call CompletePartitionPostinstall to complete the operation and |
| 166 | // cleanup. |
| 167 | const auto mountable_device = partition.readonly_target_path; |
| 168 | if (!utils::FileExists(mountable_device.c_str())) { |
| 169 | LOG(ERROR) << "Mountable device " << mountable_device << " for partition " |
| 170 | << partition.name << " does not exist"; |
| 171 | return false; |
| 172 | } |
| 173 | |
| 174 | if (!utils::FileExists(fs_mount_dir_.c_str())) { |
| 175 | LOG(ERROR) << "Mount point " << fs_mount_dir_ |
| 176 | << " does not exist, mount call will fail"; |
| 177 | return false; |
| 178 | } |
| 179 | // Double check that the fs_mount_dir is not busy with a previous mounted |
| 180 | // filesystem from a previous crashed postinstall step. |
Kelvin Zhang | 1df000a | 2022-02-09 16:00:17 -0800 | [diff] [blame] | 181 | EnsureUnmounted(); |
Kelvin Zhang | 06e654a | 2021-09-10 13:21:00 -0700 | [diff] [blame] | 182 | |
| 183 | #ifdef __ANDROID__ |
Dan Pasanen | a747c63 | 2017-05-10 16:29:35 -0500 | [diff] [blame] | 184 | // Check the currently installed /system partition to see if it's ever |
| 185 | // been mounted R/W. If it has, we'll run backuptool scripts for it |
| 186 | // since we can safely assume something on the partition has been |
| 187 | // changed and we won't be breaking verity (since it's already been |
| 188 | // broken). If it hasn't ever been mounted R/W, we can assume that |
| 189 | // the rom that the user is upgrading to will have everything they |
| 190 | // need and no addon.d scripts will need to be run to retain stuff |
| 191 | // after the upgrade. |
| 192 | // |
| 193 | // Use the following disk layout info to make the determination |
| 194 | // https://ext4.wiki.kernel.org/index.php/Ext4_Disk_Layout |
| 195 | // Super block starts from block 0, offset 0x400 |
| 196 | // 0x2C: len32 Mount time |
| 197 | // 0x30: len32 Write time |
| 198 | // 0x34: len16 Number of mounts since the last fsck |
| 199 | // 0x38: len16 Magic signature 0xEF53 |
| 200 | |
| 201 | string source_path; |
| 202 | |
| 203 | if (install_plan_.source_slot != BootControlInterface::kInvalidSlot) { |
| 204 | boot_control_->GetPartitionDevice(partition.name, install_plan_.source_slot, &source_path); |
| 205 | } |
| 206 | |
| 207 | uint16_t mount_count = 0; |
| 208 | |
| 209 | if (!source_path.empty()) { |
| 210 | brillo::Blob chunk; |
| 211 | |
| 212 | utils::ReadFileChunk(source_path, 0x400 + 0x34, sizeof(uint16_t), &chunk); |
| 213 | mount_count = *reinterpret_cast<uint16_t*>(chunk.data()); |
| 214 | } |
| 215 | |
| 216 | LOG(INFO) << source_path << " has been mounted R/W " << mount_count << " times."; |
| 217 | |
| 218 | if (mount_count > 0) { |
Christian Hoffmann | 76d57b9 | 2022-01-23 12:04:42 +0100 | [diff] [blame] | 219 | if (!utils::SetBlockDeviceReadOnly(mountable_device, false)) { |
| 220 | LOG(ERROR) << "Error marking the device " << mountable_device << " writeable."; |
| 221 | return false; |
| 222 | } |
Dan Pasanen | a747c63 | 2017-05-10 16:29:35 -0500 | [diff] [blame] | 223 | // Mount the target partition R/W |
| 224 | LOG(INFO) << "Running backuptool scripts"; |
| 225 | utils::MountFilesystem(mountable_device, fs_mount_dir_, MS_NOATIME | MS_NODEV | MS_NODIRATIME, |
| 226 | partition.filesystem_type, "seclabel"); |
| 227 | |
Luca Stefani | d1ddfee | 2019-01-03 21:20:42 +0100 | [diff] [blame^] | 228 | // Switch to a permissive domain |
| 229 | if (setexeccon("u:r:backuptool:s0")) { |
| 230 | LOG(ERROR) << "Failed to set backuptool context"; |
| 231 | return false; |
| 232 | } |
| 233 | |
Dan Pasanen | a747c63 | 2017-05-10 16:29:35 -0500 | [diff] [blame] | 234 | // Run backuptool script |
| 235 | int ret = system("/postinstall/system/bin/backuptool_postinstall.sh"); |
| 236 | if (ret == -1 || WEXITSTATUS(ret) != 0) { |
| 237 | LOG(ERROR) << "Backuptool postinstall step failed. ret=" << ret; |
| 238 | } |
Luca Stefani | d1ddfee | 2019-01-03 21:20:42 +0100 | [diff] [blame^] | 239 | |
| 240 | // Switch back to update_engine domain |
| 241 | if (setexeccon(nullptr)) { |
| 242 | LOG(ERROR) << "Failed to set update_engine context"; |
| 243 | return false; |
| 244 | } |
Dan Pasanen | a747c63 | 2017-05-10 16:29:35 -0500 | [diff] [blame] | 245 | } else { |
| 246 | LOG(INFO) << "Skipping backuptool scripts"; |
| 247 | } |
| 248 | |
| 249 | utils::UnmountFilesystem(fs_mount_dir_); |
| 250 | |
Kelvin Zhang | 06e654a | 2021-09-10 13:21:00 -0700 | [diff] [blame] | 251 | // In Chromium OS, the postinstall step is allowed to write to the block |
| 252 | // device on the target image, so we don't mark it as read-only and should |
| 253 | // be read-write since we just wrote to it during the update. |
| 254 | |
| 255 | // Mark the block device as read-only before mounting for post-install. |
| 256 | if (!utils::SetBlockDeviceReadOnly(mountable_device, true)) { |
| 257 | return false; |
| 258 | } |
| 259 | #endif // __ANDROID__ |
| 260 | |
| 261 | if (!utils::MountFilesystem( |
| 262 | mountable_device, |
| 263 | fs_mount_dir_, |
| 264 | MS_RDONLY, |
| 265 | partition.filesystem_type, |
| 266 | hardware_->GetPartitionMountOptions(partition.name))) { |
| 267 | return false; |
| 268 | } |
| 269 | return true; |
| 270 | } |
| 271 | |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 272 | void PostinstallRunnerAction::PerformPartitionPostinstall() { |
Alex Deymo | 390efed | 2016-02-18 11:00:40 -0800 | [diff] [blame] | 273 | if (install_plan_.download_url.empty()) { |
Kelvin Zhang | 24287c3 | 2023-03-09 10:13:26 -0800 | [diff] [blame] | 274 | LOG(INFO) << "Skipping post-install"; |
Alex Deymo | 390efed | 2016-02-18 11:00:40 -0800 | [diff] [blame] | 275 | return CompletePostinstall(ErrorCode::kSuccess); |
| 276 | } |
| 277 | |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 278 | // Skip all the partitions that don't have a post-install step. |
| 279 | while (current_partition_ < install_plan_.partitions.size() && |
| 280 | !install_plan_.partitions[current_partition_].run_postinstall) { |
| 281 | VLOG(1) << "Skipping post-install on partition " |
| 282 | << install_plan_.partitions[current_partition_].name; |
Kelvin Zhang | 06e654a | 2021-09-10 13:21:00 -0700 | [diff] [blame] | 283 | // Attempt to mount a device if it has postinstall script configured, even |
| 284 | // if we want to skip running postinstall script. |
| 285 | // This is because we've seen bugs like b/198787355 which is only triggered |
| 286 | // when you attempt to mount a device. If device fails to mount, it will |
| 287 | // likely fail to mount during boot anyway, so it's better to catch any |
| 288 | // issues earlier. |
| 289 | // It's possible that some of the partitions aren't mountable, but these |
| 290 | // partitions shouldn't have postinstall configured. Therefore we guard this |
| 291 | // logic with |postinstall_path.empty()|. |
| 292 | const auto& partition = install_plan_.partitions[current_partition_]; |
| 293 | if (!partition.postinstall_path.empty()) { |
| 294 | const auto mountable_device = partition.readonly_target_path; |
| 295 | if (!MountPartition(partition)) { |
| 296 | return CompletePostinstall(ErrorCode::kPostInstallMountError); |
| 297 | } |
Kelvin Zhang | ddc2580 | 2021-12-30 13:05:27 -0800 | [diff] [blame] | 298 | LogBuildInfoForPartition(fs_mount_dir_); |
Kelvin Zhang | 06e654a | 2021-09-10 13:21:00 -0700 | [diff] [blame] | 299 | if (!utils::UnmountFilesystem(fs_mount_dir_)) { |
| 300 | return CompletePartitionPostinstall( |
| 301 | 1, "Error unmounting the device " + mountable_device); |
| 302 | } |
| 303 | } |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 304 | current_partition_++; |
| 305 | } |
| 306 | if (current_partition_ == install_plan_.partitions.size()) |
| 307 | return CompletePostinstall(ErrorCode::kSuccess); |
| 308 | |
| 309 | const InstallPlan::Partition& partition = |
| 310 | install_plan_.partitions[current_partition_]; |
| 311 | |
Kelvin Zhang | a9b5d8c | 2021-05-05 09:17:46 -0400 | [diff] [blame] | 312 | const string mountable_device = partition.readonly_target_path; |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 313 | // Perform post-install for the current_partition_ partition. At this point we |
| 314 | // need to call CompletePartitionPostinstall to complete the operation and |
| 315 | // cleanup. |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 316 | |
Kelvin Zhang | 06e654a | 2021-09-10 13:21:00 -0700 | [diff] [blame] | 317 | if (!MountPartition(partition)) { |
| 318 | CompletePostinstall(ErrorCode::kPostInstallMountError); |
| 319 | return; |
Kelvin Zhang | 4ce0110 | 2020-11-16 09:32:08 -0500 | [diff] [blame] | 320 | } |
Kelvin Zhang | ddc2580 | 2021-12-30 13:05:27 -0800 | [diff] [blame] | 321 | LogBuildInfoForPartition(fs_mount_dir_); |
Alex Deymo | cbc2274 | 2016-03-04 17:53:02 -0800 | [diff] [blame] | 322 | base::FilePath postinstall_path(partition.postinstall_path); |
| 323 | if (postinstall_path.IsAbsolute()) { |
| 324 | LOG(ERROR) << "Invalid absolute path passed to postinstall, use a relative" |
| 325 | "path instead: " |
| 326 | << partition.postinstall_path; |
| 327 | return CompletePostinstall(ErrorCode::kPostinstallRunnerError); |
| 328 | } |
| 329 | |
| 330 | string abs_path = |
| 331 | base::FilePath(fs_mount_dir_).Append(postinstall_path).value(); |
Alex Deymo | 390efed | 2016-02-18 11:00:40 -0800 | [diff] [blame] | 332 | if (!base::StartsWith( |
| 333 | abs_path, fs_mount_dir_, base::CompareCase::SENSITIVE)) { |
| 334 | LOG(ERROR) << "Invalid relative postinstall path: " |
| 335 | << partition.postinstall_path; |
| 336 | return CompletePostinstall(ErrorCode::kPostinstallRunnerError); |
| 337 | } |
| 338 | |
Alex Deymo | 390efed | 2016-02-18 11:00:40 -0800 | [diff] [blame] | 339 | LOG(INFO) << "Performing postinst (" << partition.postinstall_path << " at " |
Kelvin Zhang | be1c180 | 2021-06-21 10:03:36 -0400 | [diff] [blame] | 340 | << abs_path << ") installed on mountable device " |
| 341 | << mountable_device; |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 342 | |
Alex Deymo | 032e772 | 2014-03-25 17:53:56 -0700 | [diff] [blame] | 343 | // Logs the file format of the postinstall script we are about to run. This |
| 344 | // will help debug when the postinstall script doesn't match the architecture |
| 345 | // of our build. |
Alex Deymo | 390efed | 2016-02-18 11:00:40 -0800 | [diff] [blame] | 346 | LOG(INFO) << "Format file for new " << partition.postinstall_path |
| 347 | << " is: " << utils::GetFileFormat(abs_path); |
Alex Deymo | 032e772 | 2014-03-25 17:53:56 -0700 | [diff] [blame] | 348 | |
Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 349 | // Runs the postinstall script asynchronously to free up the main loop while |
| 350 | // it's running. |
Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 351 | vector<string> command = {abs_path}; |
| 352 | #ifdef __ANDROID__ |
| 353 | // In Brillo and Android, we pass the slot number and status fd. |
| 354 | command.push_back(std::to_string(install_plan_.target_slot)); |
| 355 | command.push_back(std::to_string(kPostinstallStatusFd)); |
| 356 | #else |
| 357 | // Chrome OS postinstall expects the target rootfs as the first parameter. |
| 358 | command.push_back(partition.target_path); |
| 359 | #endif // __ANDROID__ |
| 360 | |
| 361 | current_command_ = Subprocess::Get().ExecFlags( |
Alex Deymo | d15c546 | 2016-03-09 18:11:12 -0800 | [diff] [blame] | 362 | command, |
Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 363 | Subprocess::kRedirectStderrToStdout, |
| 364 | {kPostinstallStatusFd}, |
Alex Deymo | d15c546 | 2016-03-09 18:11:12 -0800 | [diff] [blame] | 365 | base::Bind(&PostinstallRunnerAction::CompletePartitionPostinstall, |
| 366 | base::Unretained(this))); |
| 367 | // Subprocess::Exec should never return a negative process id. |
| 368 | CHECK_GE(current_command_, 0); |
| 369 | |
Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 370 | if (!current_command_) { |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 371 | CompletePartitionPostinstall(1, "Postinstall didn't launch"); |
Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 372 | return; |
| 373 | } |
| 374 | |
| 375 | // Monitor the status file descriptor. |
| 376 | progress_fd_ = |
| 377 | Subprocess::Get().GetPipeFd(current_command_, kPostinstallStatusFd); |
| 378 | int fd_flags = fcntl(progress_fd_, F_GETFL, 0) | O_NONBLOCK; |
| 379 | if (HANDLE_EINTR(fcntl(progress_fd_, F_SETFL, fd_flags)) < 0) { |
| 380 | PLOG(ERROR) << "Unable to set non-blocking I/O mode on fd " << progress_fd_; |
| 381 | } |
| 382 | |
Hidehiko Abe | 493fecb | 2019-07-10 23:30:50 +0900 | [diff] [blame] | 383 | progress_controller_ = base::FileDescriptorWatcher::WatchReadable( |
Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 384 | progress_fd_, |
Hidehiko Abe | 493fecb | 2019-07-10 23:30:50 +0900 | [diff] [blame] | 385 | base::BindRepeating(&PostinstallRunnerAction::OnProgressFdReady, |
| 386 | base::Unretained(this))); |
Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 387 | } |
| 388 | |
Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 389 | void PostinstallRunnerAction::OnProgressFdReady() { |
| 390 | char buf[1024]; |
| 391 | size_t bytes_read; |
| 392 | do { |
| 393 | bytes_read = 0; |
| 394 | bool eof; |
| 395 | bool ok = |
hscham | 00b6aa2 | 2020-02-20 12:32:06 +0900 | [diff] [blame] | 396 | utils::ReadAll(progress_fd_, buf, base::size(buf), &bytes_read, &eof); |
Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 397 | progress_buffer_.append(buf, bytes_read); |
| 398 | // Process every line. |
| 399 | vector<string> lines = base::SplitString( |
| 400 | progress_buffer_, "\n", base::KEEP_WHITESPACE, base::SPLIT_WANT_ALL); |
| 401 | if (!lines.empty()) { |
| 402 | progress_buffer_ = lines.back(); |
| 403 | lines.pop_back(); |
| 404 | for (const auto& line : lines) { |
| 405 | ProcessProgressLine(line); |
| 406 | } |
| 407 | } |
| 408 | if (!ok || eof) { |
| 409 | // There was either an error or an EOF condition, so we are done watching |
| 410 | // the file descriptor. |
Hidehiko Abe | 493fecb | 2019-07-10 23:30:50 +0900 | [diff] [blame] | 411 | progress_controller_.reset(); |
Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 412 | return; |
| 413 | } |
| 414 | } while (bytes_read); |
| 415 | } |
| 416 | |
| 417 | bool PostinstallRunnerAction::ProcessProgressLine(const string& line) { |
| 418 | double frac = 0; |
Alex Deymo | a2ea1c2 | 2016-08-24 17:26:19 -0700 | [diff] [blame] | 419 | if (sscanf(line.c_str(), "global_progress %lf", &frac) == 1 && |
| 420 | !std::isnan(frac)) { |
Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 421 | ReportProgress(frac); |
| 422 | return true; |
| 423 | } |
| 424 | |
| 425 | return false; |
| 426 | } |
| 427 | |
| 428 | void PostinstallRunnerAction::ReportProgress(double frac) { |
| 429 | if (!delegate_) |
| 430 | return; |
Yoshitaka Ishida | 128936f | 2018-02-16 18:20:07 +0900 | [diff] [blame] | 431 | if (current_partition_ >= partition_weight_.size() || total_weight_ == 0) { |
Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 432 | delegate_->ProgressUpdate(1.); |
| 433 | return; |
| 434 | } |
Alex Deymo | 44b3567 | 2016-04-05 17:57:48 -0700 | [diff] [blame] | 435 | if (!std::isfinite(frac) || frac < 0) |
Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 436 | frac = 0; |
| 437 | if (frac > 1) |
| 438 | frac = 1; |
| 439 | double postinst_action_progress = |
| 440 | (accumulated_weight_ + partition_weight_[current_partition_] * frac) / |
| 441 | total_weight_; |
| 442 | delegate_->ProgressUpdate(postinst_action_progress); |
| 443 | } |
| 444 | |
| 445 | void PostinstallRunnerAction::Cleanup() { |
Alex Deymo | 390efed | 2016-02-18 11:00:40 -0800 | [diff] [blame] | 446 | utils::UnmountFilesystem(fs_mount_dir_); |
Alex Deymo | d15c546 | 2016-03-09 18:11:12 -0800 | [diff] [blame] | 447 | #ifndef __ANDROID__ |
Kelvin Zhang | 4aeaa12 | 2020-12-04 13:28:47 -0500 | [diff] [blame] | 448 | #if BASE_VER < 800000 |
| 449 | if (!base::DeleteFile(base::FilePath(fs_mount_dir_), true)) { |
| 450 | #else |
hscham | 043355b | 2020-11-17 16:50:10 +0900 | [diff] [blame] | 451 | if (!base::DeleteFile(base::FilePath(fs_mount_dir_))) { |
Kelvin Zhang | 4aeaa12 | 2020-12-04 13:28:47 -0500 | [diff] [blame] | 452 | #endif |
Alex Deymo | 390efed | 2016-02-18 11:00:40 -0800 | [diff] [blame] | 453 | PLOG(WARNING) << "Not removing temporary mountpoint " << fs_mount_dir_; |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 454 | } |
Kelvin Zhang | 4aeaa12 | 2020-12-04 13:28:47 -0500 | [diff] [blame] | 455 | #endif |
Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 456 | |
| 457 | progress_fd_ = -1; |
Hidehiko Abe | 493fecb | 2019-07-10 23:30:50 +0900 | [diff] [blame] | 458 | progress_controller_.reset(); |
Tianjie | 55abd3c | 2020-06-19 00:22:59 -0700 | [diff] [blame] | 459 | |
Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 460 | progress_buffer_.clear(); |
Alex Deymo | d15c546 | 2016-03-09 18:11:12 -0800 | [diff] [blame] | 461 | } |
| 462 | |
| 463 | void PostinstallRunnerAction::CompletePartitionPostinstall( |
| 464 | int return_code, const string& output) { |
| 465 | current_command_ = 0; |
Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 466 | Cleanup(); |
Alex Deymo | 31d95ac | 2015-09-17 11:56:18 -0700 | [diff] [blame] | 467 | |
Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 468 | if (return_code != 0) { |
| 469 | LOG(ERROR) << "Postinst command failed with code: " << return_code; |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 470 | ErrorCode error_code = ErrorCode::kPostinstallRunnerError; |
Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 471 | |
Andrew de los Reyes | fe57d54 | 2011-06-07 09:00:36 -0700 | [diff] [blame] | 472 | if (return_code == 3) { |
Andrew de los Reyes | c1d5c93 | 2011-04-20 17:15:47 -0700 | [diff] [blame] | 473 | // This special return code means that we tried to update firmware, |
| 474 | // but couldn't because we booted from FW B, and we need to reboot |
| 475 | // to get back to FW A. |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 476 | error_code = ErrorCode::kPostinstallBootedFromFirmwareB; |
Andrew de los Reyes | c1d5c93 | 2011-04-20 17:15:47 -0700 | [diff] [blame] | 477 | } |
Don Garrett | 81018e0 | 2013-07-30 18:46:31 -0700 | [diff] [blame] | 478 | |
| 479 | if (return_code == 4) { |
| 480 | // This special return code means that we tried to update firmware, |
| 481 | // but couldn't because we booted from FW B, and we need to reboot |
| 482 | // to get back to FW A. |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 483 | error_code = ErrorCode::kPostinstallFirmwareRONotUpdatable; |
Don Garrett | 81018e0 | 2013-07-30 18:46:31 -0700 | [diff] [blame] | 484 | } |
Alex Deymo | 5b91c6b | 2016-08-04 20:33:36 -0700 | [diff] [blame] | 485 | |
| 486 | // If postinstall script for this partition is optional we can ignore the |
| 487 | // result. |
| 488 | if (install_plan_.partitions[current_partition_].postinstall_optional) { |
| 489 | LOG(INFO) << "Ignoring postinstall failure since it is optional"; |
| 490 | } else { |
| 491 | return CompletePostinstall(error_code); |
| 492 | } |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 493 | } |
Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 494 | accumulated_weight_ += partition_weight_[current_partition_]; |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 495 | current_partition_++; |
Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 496 | ReportProgress(0); |
| 497 | |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 498 | PerformPartitionPostinstall(); |
| 499 | } |
| 500 | |
Kelvin Zhang | 19acf4f | 2024-01-08 21:18:28 +0000 | [diff] [blame] | 501 | PostinstallRunnerAction::~PostinstallRunnerAction() { |
| 502 | if (!install_plan_.partitions.empty()) { |
| 503 | auto dynamic_control = boot_control_->GetDynamicPartitionControl(); |
| 504 | CHECK(dynamic_control); |
| 505 | dynamic_control->UnmapAllPartitions(); |
| 506 | LOG(INFO) << "Unmapped all partitions."; |
| 507 | } |
| 508 | } |
| 509 | |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 510 | void PostinstallRunnerAction::CompletePostinstall(ErrorCode error_code) { |
| 511 | // We only attempt to mark the new slot as active if all the postinstall |
| 512 | // steps succeeded. |
Kelvin Zhang | 19acf4f | 2024-01-08 21:18:28 +0000 | [diff] [blame] | 513 | DEFER { |
| 514 | if (error_code != ErrorCode::kSuccess && |
| 515 | error_code != ErrorCode::kUpdatedButNotActive) { |
Kelvin Zhang | 99cbbe7 | 2024-01-18 14:50:01 -0800 | [diff] [blame] | 516 | LOG(ERROR) << "Postinstall action failed. " |
| 517 | << utils::ErrorCodeToString(error_code); |
Kelvin Zhang | 19acf4f | 2024-01-08 21:18:28 +0000 | [diff] [blame] | 518 | |
| 519 | // Undo any changes done to trigger Powerwash. |
| 520 | if (powerwash_scheduled_) |
| 521 | hardware_->CancelPowerwash(); |
| 522 | } |
| 523 | processor_->ActionComplete(this, error_code); |
| 524 | }; |
Sen Jiang | 02c4942 | 2017-10-31 15:14:11 -0700 | [diff] [blame] | 525 | if (error_code == ErrorCode::kSuccess) { |
| 526 | if (install_plan_.switch_slot_on_reboot) { |
Kelvin Zhang | 99cbbe7 | 2024-01-18 14:50:01 -0800 | [diff] [blame] | 527 | if constexpr (!constants::kIsRecovery) { |
| 528 | if (!boot_control_->GetDynamicPartitionControl()->MapAllPartitions()) { |
| 529 | LOG(WARNING) |
| 530 | << "Failed to map all partitions before marking snapshot as " |
| 531 | "ready for slot switch. Subsequent FinishUpdate() call may or " |
| 532 | "may not work"; |
| 533 | } |
Kelvin Zhang | 19acf4f | 2024-01-08 21:18:28 +0000 | [diff] [blame] | 534 | } |
| 535 | if (!boot_control_->GetDynamicPartitionControl()->FinishUpdate( |
| 536 | install_plan_.powerwash_required) || |
| 537 | !boot_control_->SetActiveBootSlot(install_plan_.target_slot)) { |
Sen Jiang | 02c4942 | 2017-10-31 15:14:11 -0700 | [diff] [blame] | 538 | error_code = ErrorCode::kPostinstallRunnerError; |
Tianjie Xu | d6aa91f | 2019-11-14 11:55:10 -0800 | [diff] [blame] | 539 | } else { |
| 540 | // Schedules warm reset on next reboot, ignores the error. |
| 541 | hardware_->SetWarmReset(true); |
Tianjie | 838793d | 2021-01-14 22:05:13 -0800 | [diff] [blame] | 542 | // Sets the vbmeta digest for the other slot to boot into. |
| 543 | hardware_->SetVbmetaDigestForInactiveSlot(false); |
Sen Jiang | 02c4942 | 2017-10-31 15:14:11 -0700 | [diff] [blame] | 544 | } |
| 545 | } else { |
| 546 | error_code = ErrorCode::kUpdatedButNotActive; |
| 547 | } |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 548 | } |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 549 | |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 550 | LOG(INFO) << "All post-install commands succeeded"; |
Chris Sosa | d317e40 | 2013-06-12 13:47:09 -0700 | [diff] [blame] | 551 | if (HasOutputPipe()) { |
| 552 | SetOutputObject(install_plan_); |
| 553 | } |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 554 | } |
| 555 | |
Alex Deymo | d15c546 | 2016-03-09 18:11:12 -0800 | [diff] [blame] | 556 | void PostinstallRunnerAction::SuspendAction() { |
| 557 | if (!current_command_) |
| 558 | return; |
| 559 | if (kill(current_command_, SIGSTOP) != 0) { |
| 560 | PLOG(ERROR) << "Couldn't pause child process " << current_command_; |
Ben Chan | 7f4bc3f | 2017-01-10 15:32:11 -0800 | [diff] [blame] | 561 | } else { |
| 562 | is_current_command_suspended_ = true; |
Alex Deymo | d15c546 | 2016-03-09 18:11:12 -0800 | [diff] [blame] | 563 | } |
| 564 | } |
| 565 | |
| 566 | void PostinstallRunnerAction::ResumeAction() { |
| 567 | if (!current_command_) |
| 568 | return; |
| 569 | if (kill(current_command_, SIGCONT) != 0) { |
| 570 | PLOG(ERROR) << "Couldn't resume child process " << current_command_; |
Ben Chan | 7f4bc3f | 2017-01-10 15:32:11 -0800 | [diff] [blame] | 571 | } else { |
| 572 | is_current_command_suspended_ = false; |
Alex Deymo | d15c546 | 2016-03-09 18:11:12 -0800 | [diff] [blame] | 573 | } |
| 574 | } |
| 575 | |
| 576 | void PostinstallRunnerAction::TerminateProcessing() { |
| 577 | if (!current_command_) |
| 578 | return; |
| 579 | // Calling KillExec() will discard the callback we registered and therefore |
| 580 | // the unretained reference to this object. |
| 581 | Subprocess::Get().KillExec(current_command_); |
Ben Chan | 7f4bc3f | 2017-01-10 15:32:11 -0800 | [diff] [blame] | 582 | |
| 583 | // If the command has been suspended, resume it after KillExec() so that the |
| 584 | // process can process the SIGTERM sent by KillExec(). |
| 585 | if (is_current_command_suspended_) { |
| 586 | ResumeAction(); |
| 587 | } |
| 588 | |
Alex Deymo | d15c546 | 2016-03-09 18:11:12 -0800 | [diff] [blame] | 589 | current_command_ = 0; |
Alex Deymo | 0d29854 | 2016-03-30 18:31:49 -0700 | [diff] [blame] | 590 | Cleanup(); |
Alex Deymo | d15c546 | 2016-03-09 18:11:12 -0800 | [diff] [blame] | 591 | } |
| 592 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 593 | } // namespace chromeos_update_engine |