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" |
| 28 | #include "update_engine/common/subprocess.h" |
| 29 | #include "update_engine/common/utils.h" |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 30 | |
| 31 | namespace chromeos_update_engine { |
| 32 | |
| 33 | using std::string; |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 34 | using std::vector; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 35 | |
| 36 | namespace { |
Alex Deymo | 032e772 | 2014-03-25 17:53:56 -0700 | [diff] [blame] | 37 | // The absolute path to the post install command. |
Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 38 | const char kPostinstallScript[] = "/postinst"; |
Alex Deymo | 032e772 | 2014-03-25 17:53:56 -0700 | [diff] [blame] | 39 | |
| 40 | // Path to the binary file used by kPostinstallScript. Used to get and log the |
| 41 | // file format of the binary to debug issues when the ELF format on the update |
| 42 | // doesn't match the one on the current system. This path is not executed. |
| 43 | const char kDebugPostinstallBinaryPath[] = "/usr/bin/cros_installer"; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | void PostinstallRunnerAction::PerformAction() { |
| 47 | CHECK(HasInputObject()); |
Chris Sosa | d317e40 | 2013-06-12 13:47:09 -0700 | [diff] [blame] | 48 | install_plan_ = GetInputObject(); |
Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 49 | |
Chris Sosa | d317e40 | 2013-06-12 13:47:09 -0700 | [diff] [blame] | 50 | if (install_plan_.powerwash_required) { |
Gilad Arnold | 30dedd8 | 2013-07-03 06:19:09 -0700 | [diff] [blame] | 51 | if (utils::CreatePowerwashMarkerFile(powerwash_marker_file_)) { |
Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 52 | powerwash_marker_created_ = true; |
| 53 | } else { |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 54 | return CompletePostinstall(ErrorCode::kPostinstallPowerwashError); |
Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 55 | } |
| 56 | } |
| 57 | |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 58 | PerformPartitionPostinstall(); |
| 59 | } |
| 60 | |
| 61 | void PostinstallRunnerAction::PerformPartitionPostinstall() { |
| 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. |
| 85 | TEST_AND_RETURN( |
| 86 | utils::MakeTempDirectory("au_postint_mount.XXXXXX", &temp_rootfs_dir_)); |
| 87 | |
| 88 | if (!utils::MountFilesystem(mountable_device, temp_rootfs_dir_, MS_RDONLY)) { |
| 89 | return CompletePartitionPostinstall( |
| 90 | 1, "Error mounting the device " + mountable_device); |
| 91 | } |
| 92 | |
| 93 | LOG(INFO) << "Performing postinst (" << kPostinstallScript |
| 94 | << ") installed on device " << partition.target_path |
| 95 | << " and mountable device " << mountable_device; |
| 96 | |
Alex Deymo | 032e772 | 2014-03-25 17:53:56 -0700 | [diff] [blame] | 97 | // Logs the file format of the postinstall script we are about to run. This |
| 98 | // will help debug when the postinstall script doesn't match the architecture |
| 99 | // of our build. |
| 100 | LOG(INFO) << "Format file for new " << kPostinstallScript << " is: " |
| 101 | << utils::GetFileFormat(temp_rootfs_dir_ + kPostinstallScript); |
| 102 | LOG(INFO) << "Format file for new " << kDebugPostinstallBinaryPath << " is: " |
| 103 | << utils::GetFileFormat( |
| 104 | temp_rootfs_dir_ + kDebugPostinstallBinaryPath); |
| 105 | |
Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 106 | // Runs the postinstall script asynchronously to free up the main loop while |
| 107 | // it's running. |
| 108 | vector<string> command; |
Chris Sosa | 000ecb3 | 2014-04-23 12:21:18 -0700 | [diff] [blame] | 109 | if (!install_plan_.download_url.empty()) { |
| 110 | command.push_back(temp_rootfs_dir_ + kPostinstallScript); |
| 111 | } else { |
| 112 | // TODO(sosa): crbug.com/366207. |
| 113 | // If we're doing a rollback, just run our own postinstall. |
| 114 | command.push_back(kPostinstallScript); |
| 115 | } |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 116 | command.push_back(partition.target_path); |
| 117 | if (!Subprocess::Get().Exec( |
| 118 | command, |
| 119 | base::Bind( |
| 120 | &PostinstallRunnerAction::CompletePartitionPostinstall, |
| 121 | base::Unretained(this)))) { |
| 122 | CompletePartitionPostinstall(1, "Postinstall didn't launch"); |
Darin Petkov | 58dd134 | 2011-05-06 12:05:13 -0700 | [diff] [blame] | 123 | } |
Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 124 | } |
| 125 | |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 126 | void PostinstallRunnerAction::CompletePartitionPostinstall( |
| 127 | int return_code, |
| 128 | const string& output) { |
| 129 | utils::UnmountFilesystem(temp_rootfs_dir_); |
| 130 | if (!base::DeleteFile(base::FilePath(temp_rootfs_dir_), false)) { |
| 131 | PLOG(WARNING) << "Not removing mountpoint " << temp_rootfs_dir_; |
| 132 | } |
| 133 | temp_rootfs_dir_.clear(); |
Alex Deymo | 31d95ac | 2015-09-17 11:56:18 -0700 | [diff] [blame] | 134 | |
Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 135 | if (return_code != 0) { |
| 136 | LOG(ERROR) << "Postinst command failed with code: " << return_code; |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 137 | ErrorCode error_code = ErrorCode::kPostinstallRunnerError; |
Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 138 | |
Andrew de los Reyes | fe57d54 | 2011-06-07 09:00:36 -0700 | [diff] [blame] | 139 | if (return_code == 3) { |
Andrew de los Reyes | c1d5c93 | 2011-04-20 17:15:47 -0700 | [diff] [blame] | 140 | // This special return code means that we tried to update firmware, |
| 141 | // but couldn't because we booted from FW B, and we need to reboot |
| 142 | // to get back to FW A. |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 143 | error_code = ErrorCode::kPostinstallBootedFromFirmwareB; |
Andrew de los Reyes | c1d5c93 | 2011-04-20 17:15:47 -0700 | [diff] [blame] | 144 | } |
Don Garrett | 81018e0 | 2013-07-30 18:46:31 -0700 | [diff] [blame] | 145 | |
| 146 | if (return_code == 4) { |
| 147 | // This special return code means that we tried to update firmware, |
| 148 | // but couldn't because we booted from FW B, and we need to reboot |
| 149 | // to get back to FW A. |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 150 | error_code = ErrorCode::kPostinstallFirmwareRONotUpdatable; |
Don Garrett | 81018e0 | 2013-07-30 18:46:31 -0700 | [diff] [blame] | 151 | } |
Alex Deymo | e5e5fe9 | 2015-10-05 09:28:19 -0700 | [diff] [blame] | 152 | return CompletePostinstall(error_code); |
| 153 | } |
| 154 | current_partition_++; |
| 155 | PerformPartitionPostinstall(); |
| 156 | } |
| 157 | |
| 158 | void PostinstallRunnerAction::CompletePostinstall(ErrorCode error_code) { |
| 159 | // We only attempt to mark the new slot as active if all the postinstall |
| 160 | // steps succeeded. |
| 161 | if (error_code == ErrorCode::kSuccess && |
| 162 | !system_state_->boot_control()->SetActiveBootSlot( |
| 163 | install_plan_.target_slot)) { |
| 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 |