blob: 7fa70096e5d0be1cefe9774dd8d92da05cbd93a0 [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
Alex Deymod15c5462016-03-09 18:11:12 -080019#include <signal.h>
adlr@google.com3defe6a2009-12-04 20:57:17 +000020#include <stdlib.h>
Alex Vakulenko44cab302014-07-23 13:12:15 -070021#include <sys/mount.h>
Alex Deymod15c5462016-03-09 18:11:12 -080022#include <sys/types.h>
Andrew de los Reyesf9714432010-05-04 10:21:23 -070023#include <vector>
Jay Srinivasan1c0fe792013-03-28 16:45:25 -070024
Alex Deymoe5e5fe92015-10-05 09:28:19 -070025#include <base/files/file_path.h>
26#include <base/files/file_util.h>
Alex Deymod15c5462016-03-09 18:11:12 -080027#include <base/logging.h>
Alex Deymo390efed2016-02-18 11:00:40 -080028#include <base/strings/string_util.h>
Alex Deymo461b2592015-07-24 20:10:52 -070029
Alex Deymo39910dc2015-11-09 17:04:30 -080030#include "update_engine/common/action_processor.h"
Alex Deymob15a0b82015-11-25 20:30:40 -030031#include "update_engine/common/boot_control_interface.h"
Alex Deymo14dbd332016-03-01 18:55:54 -080032#include "update_engine/common/platform_constants.h"
Alex Deymo39910dc2015-11-09 17:04:30 -080033#include "update_engine/common/subprocess.h"
34#include "update_engine/common/utils.h"
adlr@google.com3defe6a2009-12-04 20:57:17 +000035
36namespace chromeos_update_engine {
37
38using std::string;
Andrew de los Reyesf9714432010-05-04 10:21:23 -070039using std::vector;
adlr@google.com3defe6a2009-12-04 20:57:17 +000040
adlr@google.com3defe6a2009-12-04 20:57:17 +000041void PostinstallRunnerAction::PerformAction() {
42 CHECK(HasInputObject());
Chris Sosad317e402013-06-12 13:47:09 -070043 install_plan_ = GetInputObject();
Darin Petkov6f03a3b2010-11-10 14:27:14 -080044
Chris Sosad317e402013-06-12 13:47:09 -070045 if (install_plan_.powerwash_required) {
Gilad Arnold30dedd82013-07-03 06:19:09 -070046 if (utils::CreatePowerwashMarkerFile(powerwash_marker_file_)) {
Jay Srinivasan1c0fe792013-03-28 16:45:25 -070047 powerwash_marker_created_ = true;
48 } else {
Alex Deymoe5e5fe92015-10-05 09:28:19 -070049 return CompletePostinstall(ErrorCode::kPostinstallPowerwashError);
Jay Srinivasan1c0fe792013-03-28 16:45:25 -070050 }
51 }
52
Alex Deymoe5e5fe92015-10-05 09:28:19 -070053 PerformPartitionPostinstall();
54}
55
56void PostinstallRunnerAction::PerformPartitionPostinstall() {
Alex Deymo390efed2016-02-18 11:00:40 -080057 if (install_plan_.download_url.empty()) {
58 LOG(INFO) << "Skipping post-install during rollback";
59 return CompletePostinstall(ErrorCode::kSuccess);
60 }
61
Alex Deymoe5e5fe92015-10-05 09:28:19 -070062 // 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 Deymo390efed2016-02-18 11:00:40 -080085#ifdef __ANDROID__
86 fs_mount_dir_ = "/postinstall";
87#else // __ANDROID__
Alex Deymoe5e5fe92015-10-05 09:28:19 -070088 TEST_AND_RETURN(
Alex Deymo390efed2016-02-18 11:00:40 -080089 utils::MakeTempDirectory("au_postint_mount.XXXXXX", &fs_mount_dir_));
90#endif // __ANDROID__
Alex Deymoe5e5fe92015-10-05 09:28:19 -070091
Alex Deymocbc22742016-03-04 17:53:02 -080092 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 Deymo390efed2016-02-18 11:00:40 -0800102 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 Deymo14dbd332016-03-01 18:55:54 -0800112 partition.filesystem_type,
113 constants::kPostinstallMountOptions)) {
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700114 return CompletePartitionPostinstall(
115 1, "Error mounting the device " + mountable_device);
116 }
117
Alex Deymo390efed2016-02-18 11:00:40 -0800118 LOG(INFO) << "Performing postinst (" << partition.postinstall_path << " at "
119 << abs_path << ") installed on device " << partition.target_path
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700120 << " and mountable device " << mountable_device;
121
Alex Deymo032e7722014-03-25 17:53:56 -0700122 // 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 Deymo390efed2016-02-18 11:00:40 -0800125 LOG(INFO) << "Format file for new " << partition.postinstall_path
126 << " is: " << utils::GetFileFormat(abs_path);
Alex Deymo032e7722014-03-25 17:53:56 -0700127
Darin Petkov6f03a3b2010-11-10 14:27:14 -0800128 // Runs the postinstall script asynchronously to free up the main loop while
129 // it's running.
Alex Deymo390efed2016-02-18 11:00:40 -0800130 vector<string> command = {abs_path, partition.target_path};
Alex Deymod15c5462016-03-09 18:11:12 -0800131 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 Deymoe5e5fe92015-10-05 09:28:19 -0700139 CompletePartitionPostinstall(1, "Postinstall didn't launch");
Darin Petkov6f03a3b2010-11-10 14:27:14 -0800140}
141
Alex Deymod15c5462016-03-09 18:11:12 -0800142void PostinstallRunnerAction::CleanupMount() {
Alex Deymo390efed2016-02-18 11:00:40 -0800143 utils::UnmountFilesystem(fs_mount_dir_);
Alex Deymod15c5462016-03-09 18:11:12 -0800144#ifndef __ANDROID__
Alex Deymo390efed2016-02-18 11:00:40 -0800145 if (!base::DeleteFile(base::FilePath(fs_mount_dir_), false)) {
146 PLOG(WARNING) << "Not removing temporary mountpoint " << fs_mount_dir_;
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700147 }
Alex Deymod15c5462016-03-09 18:11:12 -0800148#endif // !__ANDROID__
Alex Deymo390efed2016-02-18 11:00:40 -0800149 fs_mount_dir_.clear();
Alex Deymod15c5462016-03-09 18:11:12 -0800150}
151
152void PostinstallRunnerAction::CompletePartitionPostinstall(
153 int return_code, const string& output) {
154 current_command_ = 0;
155 CleanupMount();
Alex Deymo31d95ac2015-09-17 11:56:18 -0700156
Darin Petkov6f03a3b2010-11-10 14:27:14 -0800157 if (return_code != 0) {
158 LOG(ERROR) << "Postinst command failed with code: " << return_code;
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700159 ErrorCode error_code = ErrorCode::kPostinstallRunnerError;
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700160
Andrew de los Reyesfe57d542011-06-07 09:00:36 -0700161 if (return_code == 3) {
Andrew de los Reyesc1d5c932011-04-20 17:15:47 -0700162 // 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 Deymoe5e5fe92015-10-05 09:28:19 -0700165 error_code = ErrorCode::kPostinstallBootedFromFirmwareB;
Andrew de los Reyesc1d5c932011-04-20 17:15:47 -0700166 }
Don Garrett81018e02013-07-30 18:46:31 -0700167
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 Deymoe5e5fe92015-10-05 09:28:19 -0700172 error_code = ErrorCode::kPostinstallFirmwareRONotUpdatable;
Don Garrett81018e02013-07-30 18:46:31 -0700173 }
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700174 return CompletePostinstall(error_code);
175 }
176 current_partition_++;
177 PerformPartitionPostinstall();
178}
179
180void 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 Deymob15a0b82015-11-25 20:30:40 -0300184 !boot_control_->SetActiveBootSlot(install_plan_.target_slot)) {
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700185 error_code = ErrorCode::kPostinstallRunnerError;
186 }
187
188 ScopedActionCompleter completer(processor_, this);
Alex Deymocbc22742016-03-04 17:53:02 -0800189 completer.set_code(error_code);
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700190
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 Garrett81018e02013-07-30 18:46:31 -0700197
Darin Petkov6f03a3b2010-11-10 14:27:14 -0800198 return;
199 }
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700200
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700201 LOG(INFO) << "All post-install commands succeeded";
Chris Sosad317e402013-06-12 13:47:09 -0700202 if (HasOutputPipe()) {
203 SetOutputObject(install_plan_);
204 }
adlr@google.com3defe6a2009-12-04 20:57:17 +0000205}
206
Alex Deymod15c5462016-03-09 18:11:12 -0800207void 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
215void 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
223void 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.com3defe6a2009-12-04 20:57:17 +0000233} // namespace chromeos_update_engine