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 | d15c546 | 2016-03-09 18:11:12 -0800 | [diff] [blame] | 19 | #include <signal.h> |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 20 | #include <stdlib.h> |
Alex Vakulenko | 44cab30 | 2014-07-23 13:12:15 -0700 | [diff] [blame] | 21 | #include <sys/mount.h> |
Alex Deymo | d15c546 | 2016-03-09 18:11:12 -0800 | [diff] [blame] | 22 | #include <sys/types.h> |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 23 | #include <vector> |
Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 24 | |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 25 | #include <base/files/file_path.h> |
| 26 | #include <base/files/file_util.h> |
Alex Deymo | d15c546 | 2016-03-09 18:11:12 -0800 | [diff] [blame] | 27 | #include <base/logging.h> |
Alex Deymo | 390efed | 2016-02-18 11:00:40 -0800 | [diff] [blame] | 28 | #include <base/strings/string_util.h> |
Alex Deymo | 461b259 | 2015-07-24 20:10:52 -0700 | [diff] [blame] | 29 | |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 30 | #include "update_engine/common/action_processor.h" |
Alex Deymo | b15a0b8 | 2015-11-25 20:30:40 -0300 | [diff] [blame] | 31 | #include "update_engine/common/boot_control_interface.h" |
Alex Deymo | 14dbd33 | 2016-03-01 18:55:54 -0800 | [diff] [blame] | 32 | #include "update_engine/common/platform_constants.h" |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 33 | #include "update_engine/common/subprocess.h" |
| 34 | #include "update_engine/common/utils.h" |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 35 | |
| 36 | namespace chromeos_update_engine { |
| 37 | |
| 38 | using std::string; |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 39 | using std::vector; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 40 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 41 | void PostinstallRunnerAction::PerformAction() { |
| 42 | CHECK(HasInputObject()); |
Chris Sosa | d317e40 | 2013-06-12 13:47:09 -0700 | [diff] [blame] | 43 | install_plan_ = GetInputObject(); |
Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 44 | |
Chris Sosa | d317e40 | 2013-06-12 13:47:09 -0700 | [diff] [blame] | 45 | if (install_plan_.powerwash_required) { |
Gilad Arnold | 30dedd8 | 2013-07-03 06:19:09 -0700 | [diff] [blame] | 46 | if (utils::CreatePowerwashMarkerFile(powerwash_marker_file_)) { |
Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 47 | powerwash_marker_created_ = true; |
| 48 | } else { |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 49 | return CompletePostinstall(ErrorCode::kPostinstallPowerwashError); |
Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 50 | } |
| 51 | } |
| 52 | |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 53 | PerformPartitionPostinstall(); |
| 54 | } |
| 55 | |
| 56 | void PostinstallRunnerAction::PerformPartitionPostinstall() { |
Alex Deymo | 390efed | 2016-02-18 11:00:40 -0800 | [diff] [blame] | 57 | if (install_plan_.download_url.empty()) { |
| 58 | LOG(INFO) << "Skipping post-install during rollback"; |
| 59 | return CompletePostinstall(ErrorCode::kSuccess); |
| 60 | } |
| 61 | |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 62 | // Skip all the partitions that don't have a post-install step. |
| 63 | while (current_partition_ < install_plan_.partitions.size() && |
| 64 | !install_plan_.partitions[current_partition_].run_postinstall) { |
| 65 | VLOG(1) << "Skipping post-install on partition " |
| 66 | << install_plan_.partitions[current_partition_].name; |
| 67 | current_partition_++; |
| 68 | } |
| 69 | if (current_partition_ == install_plan_.partitions.size()) |
| 70 | return CompletePostinstall(ErrorCode::kSuccess); |
| 71 | |
| 72 | const InstallPlan::Partition& partition = |
| 73 | install_plan_.partitions[current_partition_]; |
| 74 | |
| 75 | const string mountable_device = |
| 76 | utils::MakePartitionNameForMount(partition.target_path); |
| 77 | if (mountable_device.empty()) { |
| 78 | LOG(ERROR) << "Cannot make mountable device from " << partition.target_path; |
| 79 | return CompletePostinstall(ErrorCode::kPostinstallRunnerError); |
| 80 | } |
| 81 | |
| 82 | // Perform post-install for the current_partition_ partition. At this point we |
| 83 | // need to call CompletePartitionPostinstall to complete the operation and |
| 84 | // cleanup. |
Alex Deymo | 390efed | 2016-02-18 11:00:40 -0800 | [diff] [blame] | 85 | #ifdef __ANDROID__ |
| 86 | fs_mount_dir_ = "/postinstall"; |
| 87 | #else // __ANDROID__ |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 88 | TEST_AND_RETURN( |
Alex Deymo | 390efed | 2016-02-18 11:00:40 -0800 | [diff] [blame] | 89 | utils::MakeTempDirectory("au_postint_mount.XXXXXX", &fs_mount_dir_)); |
| 90 | #endif // __ANDROID__ |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 91 | |
Alex Deymo | cbc2274 | 2016-03-04 17:53:02 -0800 | [diff] [blame] | 92 | base::FilePath postinstall_path(partition.postinstall_path); |
| 93 | if (postinstall_path.IsAbsolute()) { |
| 94 | LOG(ERROR) << "Invalid absolute path passed to postinstall, use a relative" |
| 95 | "path instead: " |
| 96 | << partition.postinstall_path; |
| 97 | return CompletePostinstall(ErrorCode::kPostinstallRunnerError); |
| 98 | } |
| 99 | |
| 100 | string abs_path = |
| 101 | base::FilePath(fs_mount_dir_).Append(postinstall_path).value(); |
Alex Deymo | 390efed | 2016-02-18 11:00:40 -0800 | [diff] [blame] | 102 | if (!base::StartsWith( |
| 103 | abs_path, fs_mount_dir_, base::CompareCase::SENSITIVE)) { |
| 104 | LOG(ERROR) << "Invalid relative postinstall path: " |
| 105 | << partition.postinstall_path; |
| 106 | return CompletePostinstall(ErrorCode::kPostinstallRunnerError); |
| 107 | } |
| 108 | |
| 109 | if (!utils::MountFilesystem(mountable_device, |
| 110 | fs_mount_dir_, |
| 111 | MS_RDONLY, |
Alex Deymo | 14dbd33 | 2016-03-01 18:55:54 -0800 | [diff] [blame] | 112 | partition.filesystem_type, |
| 113 | constants::kPostinstallMountOptions)) { |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 114 | return CompletePartitionPostinstall( |
| 115 | 1, "Error mounting the device " + mountable_device); |
| 116 | } |
| 117 | |
Alex Deymo | 390efed | 2016-02-18 11:00:40 -0800 | [diff] [blame] | 118 | LOG(INFO) << "Performing postinst (" << partition.postinstall_path << " at " |
| 119 | << abs_path << ") installed on device " << partition.target_path |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 120 | << " and mountable device " << mountable_device; |
| 121 | |
Alex Deymo | 032e772 | 2014-03-25 17:53:56 -0700 | [diff] [blame] | 122 | // Logs the file format of the postinstall script we are about to run. This |
| 123 | // will help debug when the postinstall script doesn't match the architecture |
| 124 | // of our build. |
Alex Deymo | 390efed | 2016-02-18 11:00:40 -0800 | [diff] [blame] | 125 | LOG(INFO) << "Format file for new " << partition.postinstall_path |
| 126 | << " is: " << utils::GetFileFormat(abs_path); |
Alex Deymo | 032e772 | 2014-03-25 17:53:56 -0700 | [diff] [blame] | 127 | |
Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 128 | // Runs the postinstall script asynchronously to free up the main loop while |
| 129 | // it's running. |
Alex Deymo | 390efed | 2016-02-18 11:00:40 -0800 | [diff] [blame] | 130 | vector<string> command = {abs_path, partition.target_path}; |
Alex Deymo | d15c546 | 2016-03-09 18:11:12 -0800 | [diff] [blame] | 131 | current_command_ = Subprocess::Get().Exec( |
| 132 | command, |
| 133 | base::Bind(&PostinstallRunnerAction::CompletePartitionPostinstall, |
| 134 | base::Unretained(this))); |
| 135 | // Subprocess::Exec should never return a negative process id. |
| 136 | CHECK_GE(current_command_, 0); |
| 137 | |
| 138 | if (!current_command_) |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 139 | CompletePartitionPostinstall(1, "Postinstall didn't launch"); |
Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 140 | } |
| 141 | |
Alex Deymo | d15c546 | 2016-03-09 18:11:12 -0800 | [diff] [blame] | 142 | void PostinstallRunnerAction::CleanupMount() { |
Alex Deymo | 390efed | 2016-02-18 11:00:40 -0800 | [diff] [blame] | 143 | utils::UnmountFilesystem(fs_mount_dir_); |
Alex Deymo | d15c546 | 2016-03-09 18:11:12 -0800 | [diff] [blame] | 144 | #ifndef __ANDROID__ |
Alex Deymo | 390efed | 2016-02-18 11:00:40 -0800 | [diff] [blame] | 145 | if (!base::DeleteFile(base::FilePath(fs_mount_dir_), false)) { |
| 146 | PLOG(WARNING) << "Not removing temporary mountpoint " << fs_mount_dir_; |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 147 | } |
Alex Deymo | d15c546 | 2016-03-09 18:11:12 -0800 | [diff] [blame] | 148 | #endif // !__ANDROID__ |
Alex Deymo | 390efed | 2016-02-18 11:00:40 -0800 | [diff] [blame] | 149 | fs_mount_dir_.clear(); |
Alex Deymo | d15c546 | 2016-03-09 18:11:12 -0800 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | void PostinstallRunnerAction::CompletePartitionPostinstall( |
| 153 | int return_code, const string& output) { |
| 154 | current_command_ = 0; |
| 155 | CleanupMount(); |
Alex Deymo | 31d95ac | 2015-09-17 11:56:18 -0700 | [diff] [blame] | 156 | |
Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 157 | if (return_code != 0) { |
| 158 | LOG(ERROR) << "Postinst command failed with code: " << return_code; |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 159 | ErrorCode error_code = ErrorCode::kPostinstallRunnerError; |
Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 160 | |
Andrew de los Reyes | fe57d54 | 2011-06-07 09:00:36 -0700 | [diff] [blame] | 161 | if (return_code == 3) { |
Andrew de los Reyes | c1d5c93 | 2011-04-20 17:15:47 -0700 | [diff] [blame] | 162 | // This special return code means that we tried to update firmware, |
| 163 | // but couldn't because we booted from FW B, and we need to reboot |
| 164 | // to get back to FW A. |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 165 | error_code = ErrorCode::kPostinstallBootedFromFirmwareB; |
Andrew de los Reyes | c1d5c93 | 2011-04-20 17:15:47 -0700 | [diff] [blame] | 166 | } |
Don Garrett | 81018e0 | 2013-07-30 18:46:31 -0700 | [diff] [blame] | 167 | |
| 168 | if (return_code == 4) { |
| 169 | // This special return code means that we tried to update firmware, |
| 170 | // but couldn't because we booted from FW B, and we need to reboot |
| 171 | // to get back to FW A. |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 172 | error_code = ErrorCode::kPostinstallFirmwareRONotUpdatable; |
Don Garrett | 81018e0 | 2013-07-30 18:46:31 -0700 | [diff] [blame] | 173 | } |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 174 | return CompletePostinstall(error_code); |
| 175 | } |
| 176 | current_partition_++; |
| 177 | PerformPartitionPostinstall(); |
| 178 | } |
| 179 | |
| 180 | void PostinstallRunnerAction::CompletePostinstall(ErrorCode error_code) { |
| 181 | // We only attempt to mark the new slot as active if all the postinstall |
| 182 | // steps succeeded. |
| 183 | if (error_code == ErrorCode::kSuccess && |
Alex Deymo | b15a0b8 | 2015-11-25 20:30:40 -0300 | [diff] [blame] | 184 | !boot_control_->SetActiveBootSlot(install_plan_.target_slot)) { |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 185 | error_code = ErrorCode::kPostinstallRunnerError; |
| 186 | } |
| 187 | |
| 188 | ScopedActionCompleter completer(processor_, this); |
Alex Deymo | cbc2274 | 2016-03-04 17:53:02 -0800 | [diff] [blame] | 189 | completer.set_code(error_code); |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 190 | |
| 191 | if (error_code != ErrorCode::kSuccess) { |
| 192 | LOG(ERROR) << "Postinstall action failed."; |
| 193 | |
| 194 | // Undo any changes done to trigger Powerwash using clobber-state. |
| 195 | if (powerwash_marker_created_) |
| 196 | utils::DeletePowerwashMarkerFile(powerwash_marker_file_); |
Don Garrett | 81018e0 | 2013-07-30 18:46:31 -0700 | [diff] [blame] | 197 | |
Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 198 | return; |
| 199 | } |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 200 | |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 201 | LOG(INFO) << "All post-install commands succeeded"; |
Chris Sosa | d317e40 | 2013-06-12 13:47:09 -0700 | [diff] [blame] | 202 | if (HasOutputPipe()) { |
| 203 | SetOutputObject(install_plan_); |
| 204 | } |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 205 | } |
| 206 | |
Alex Deymo | d15c546 | 2016-03-09 18:11:12 -0800 | [diff] [blame] | 207 | void PostinstallRunnerAction::SuspendAction() { |
| 208 | if (!current_command_) |
| 209 | return; |
| 210 | if (kill(current_command_, SIGSTOP) != 0) { |
| 211 | PLOG(ERROR) << "Couldn't pause child process " << current_command_; |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | void PostinstallRunnerAction::ResumeAction() { |
| 216 | if (!current_command_) |
| 217 | return; |
| 218 | if (kill(current_command_, SIGCONT) != 0) { |
| 219 | PLOG(ERROR) << "Couldn't resume child process " << current_command_; |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | void PostinstallRunnerAction::TerminateProcessing() { |
| 224 | if (!current_command_) |
| 225 | return; |
| 226 | // Calling KillExec() will discard the callback we registered and therefore |
| 227 | // the unretained reference to this object. |
| 228 | Subprocess::Get().KillExec(current_command_); |
| 229 | current_command_ = 0; |
| 230 | CleanupMount(); |
| 231 | } |
| 232 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 233 | } // namespace chromeos_update_engine |