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