blob: 84ca398c2d21d3f2940b6ca9ecd9f71cafd75c47 [file] [log] [blame]
Alex Deymoaea4c1c2015-08-19 20:24:43 -07001//
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.com3defe6a2009-12-04 20:57:17 +000016
Alex Deymo39910dc2015-11-09 17:04:30 -080017#include "update_engine/payload_consumer/postinstall_runner_action.h"
Jay Srinivasan1c0fe792013-03-28 16:45:25 -070018
adlr@google.com3defe6a2009-12-04 20:57:17 +000019#include <stdlib.h>
Alex Vakulenko44cab302014-07-23 13:12:15 -070020#include <sys/mount.h>
Andrew de los Reyesf9714432010-05-04 10:21:23 -070021#include <vector>
Jay Srinivasan1c0fe792013-03-28 16:45:25 -070022
Alex Deymo461b2592015-07-24 20:10:52 -070023#include <base/bind.h>
Alex Deymoe5e5fe92015-10-05 09:28:19 -070024#include <base/files/file_path.h>
25#include <base/files/file_util.h>
Alex Deymo461b2592015-07-24 20:10:52 -070026
Alex Deymo39910dc2015-11-09 17:04:30 -080027#include "update_engine/common/action_processor.h"
Alex Deymob15a0b82015-11-25 20:30:40 -030028#include "update_engine/common/boot_control_interface.h"
Alex Deymo39910dc2015-11-09 17:04:30 -080029#include "update_engine/common/subprocess.h"
30#include "update_engine/common/utils.h"
adlr@google.com3defe6a2009-12-04 20:57:17 +000031
32namespace chromeos_update_engine {
33
34using std::string;
Andrew de los Reyesf9714432010-05-04 10:21:23 -070035using std::vector;
adlr@google.com3defe6a2009-12-04 20:57:17 +000036
37namespace {
Alex Deymo032e7722014-03-25 17:53:56 -070038// The absolute path to the post install command.
Darin Petkov6f03a3b2010-11-10 14:27:14 -080039const char kPostinstallScript[] = "/postinst";
Alex Deymo032e7722014-03-25 17:53:56 -070040
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.
44const char kDebugPostinstallBinaryPath[] = "/usr/bin/cros_installer";
adlr@google.com3defe6a2009-12-04 20:57:17 +000045}
46
47void PostinstallRunnerAction::PerformAction() {
48 CHECK(HasInputObject());
Chris Sosad317e402013-06-12 13:47:09 -070049 install_plan_ = GetInputObject();
Darin Petkov6f03a3b2010-11-10 14:27:14 -080050
Chris Sosad317e402013-06-12 13:47:09 -070051 if (install_plan_.powerwash_required) {
Gilad Arnold30dedd82013-07-03 06:19:09 -070052 if (utils::CreatePowerwashMarkerFile(powerwash_marker_file_)) {
Jay Srinivasan1c0fe792013-03-28 16:45:25 -070053 powerwash_marker_created_ = true;
54 } else {
Alex Deymoe5e5fe92015-10-05 09:28:19 -070055 return CompletePostinstall(ErrorCode::kPostinstallPowerwashError);
Jay Srinivasan1c0fe792013-03-28 16:45:25 -070056 }
57 }
58
Alex Deymoe5e5fe92015-10-05 09:28:19 -070059 PerformPartitionPostinstall();
60}
61
62void 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 Deymo032e7722014-03-25 17:53:56 -070098 // 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 Petkov6f03a3b2010-11-10 14:27:14 -0800107 // Runs the postinstall script asynchronously to free up the main loop while
108 // it's running.
109 vector<string> command;
Chris Sosa000ecb32014-04-23 12:21:18 -0700110 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 Deymoe5e5fe92015-10-05 09:28:19 -0700117 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 Petkov58dd1342011-05-06 12:05:13 -0700124 }
Darin Petkov6f03a3b2010-11-10 14:27:14 -0800125}
126
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700127void 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 Deymo31d95ac2015-09-17 11:56:18 -0700135
Darin Petkov6f03a3b2010-11-10 14:27:14 -0800136 if (return_code != 0) {
137 LOG(ERROR) << "Postinst command failed with code: " << return_code;
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700138 ErrorCode error_code = ErrorCode::kPostinstallRunnerError;
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700139
Andrew de los Reyesfe57d542011-06-07 09:00:36 -0700140 if (return_code == 3) {
Andrew de los Reyesc1d5c932011-04-20 17:15:47 -0700141 // 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 Deymoe5e5fe92015-10-05 09:28:19 -0700144 error_code = ErrorCode::kPostinstallBootedFromFirmwareB;
Andrew de los Reyesc1d5c932011-04-20 17:15:47 -0700145 }
Don Garrett81018e02013-07-30 18:46:31 -0700146
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 Deymoe5e5fe92015-10-05 09:28:19 -0700151 error_code = ErrorCode::kPostinstallFirmwareRONotUpdatable;
Don Garrett81018e02013-07-30 18:46:31 -0700152 }
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700153 return CompletePostinstall(error_code);
154 }
155 current_partition_++;
156 PerformPartitionPostinstall();
157}
158
159void 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 Deymob15a0b82015-11-25 20:30:40 -0300163 !boot_control_->SetActiveBootSlot(install_plan_.target_slot)) {
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700164 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 Garrett81018e02013-07-30 18:46:31 -0700175
Darin Petkov6f03a3b2010-11-10 14:27:14 -0800176 return;
177 }
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700178
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700179 LOG(INFO) << "All post-install commands succeeded";
Chris Sosad317e402013-06-12 13:47:09 -0700180 if (HasOutputPipe()) {
181 SetOutputObject(install_plan_);
182 }
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700183
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700184 completer.set_code(ErrorCode::kSuccess);
adlr@google.com3defe6a2009-12-04 20:57:17 +0000185}
186
adlr@google.com3defe6a2009-12-04 20:57:17 +0000187} // namespace chromeos_update_engine