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