blob: 9ebef53e4888ddacb237b894a9f1f053023bf378 [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 Deymo390efed2016-02-18 11:00:40 -080026#include <base/strings/string_util.h>
Alex Deymo461b2592015-07-24 20:10:52 -070027
Alex Deymo39910dc2015-11-09 17:04:30 -080028#include "update_engine/common/action_processor.h"
Alex Deymob15a0b82015-11-25 20:30:40 -030029#include "update_engine/common/boot_control_interface.h"
Alex Deymo14dbd332016-03-01 18:55:54 -080030#include "update_engine/common/platform_constants.h"
Alex Deymo39910dc2015-11-09 17:04:30 -080031#include "update_engine/common/subprocess.h"
32#include "update_engine/common/utils.h"
adlr@google.com3defe6a2009-12-04 20:57:17 +000033
34namespace chromeos_update_engine {
35
36using std::string;
Andrew de los Reyesf9714432010-05-04 10:21:23 -070037using std::vector;
adlr@google.com3defe6a2009-12-04 20:57:17 +000038
adlr@google.com3defe6a2009-12-04 20:57:17 +000039void PostinstallRunnerAction::PerformAction() {
40 CHECK(HasInputObject());
Chris Sosad317e402013-06-12 13:47:09 -070041 install_plan_ = GetInputObject();
Darin Petkov6f03a3b2010-11-10 14:27:14 -080042
Chris Sosad317e402013-06-12 13:47:09 -070043 if (install_plan_.powerwash_required) {
Gilad Arnold30dedd82013-07-03 06:19:09 -070044 if (utils::CreatePowerwashMarkerFile(powerwash_marker_file_)) {
Jay Srinivasan1c0fe792013-03-28 16:45:25 -070045 powerwash_marker_created_ = true;
46 } else {
Alex Deymoe5e5fe92015-10-05 09:28:19 -070047 return CompletePostinstall(ErrorCode::kPostinstallPowerwashError);
Jay Srinivasan1c0fe792013-03-28 16:45:25 -070048 }
49 }
50
Alex Deymoe5e5fe92015-10-05 09:28:19 -070051 PerformPartitionPostinstall();
52}
53
54void PostinstallRunnerAction::PerformPartitionPostinstall() {
Alex Deymo390efed2016-02-18 11:00:40 -080055 if (install_plan_.download_url.empty()) {
56 LOG(INFO) << "Skipping post-install during rollback";
57 return CompletePostinstall(ErrorCode::kSuccess);
58 }
59
Alex Deymoe5e5fe92015-10-05 09:28:19 -070060 // Skip all the partitions that don't have a post-install step.
61 while (current_partition_ < install_plan_.partitions.size() &&
62 !install_plan_.partitions[current_partition_].run_postinstall) {
63 VLOG(1) << "Skipping post-install on partition "
64 << install_plan_.partitions[current_partition_].name;
65 current_partition_++;
66 }
67 if (current_partition_ == install_plan_.partitions.size())
68 return CompletePostinstall(ErrorCode::kSuccess);
69
70 const InstallPlan::Partition& partition =
71 install_plan_.partitions[current_partition_];
72
73 const string mountable_device =
74 utils::MakePartitionNameForMount(partition.target_path);
75 if (mountable_device.empty()) {
76 LOG(ERROR) << "Cannot make mountable device from " << partition.target_path;
77 return CompletePostinstall(ErrorCode::kPostinstallRunnerError);
78 }
79
80 // Perform post-install for the current_partition_ partition. At this point we
81 // need to call CompletePartitionPostinstall to complete the operation and
82 // cleanup.
Alex Deymo390efed2016-02-18 11:00:40 -080083#ifdef __ANDROID__
84 fs_mount_dir_ = "/postinstall";
85#else // __ANDROID__
Alex Deymoe5e5fe92015-10-05 09:28:19 -070086 TEST_AND_RETURN(
Alex Deymo390efed2016-02-18 11:00:40 -080087 utils::MakeTempDirectory("au_postint_mount.XXXXXX", &fs_mount_dir_));
88#endif // __ANDROID__
Alex Deymoe5e5fe92015-10-05 09:28:19 -070089
Alex Deymocbc22742016-03-04 17:53:02 -080090 base::FilePath postinstall_path(partition.postinstall_path);
91 if (postinstall_path.IsAbsolute()) {
92 LOG(ERROR) << "Invalid absolute path passed to postinstall, use a relative"
93 "path instead: "
94 << partition.postinstall_path;
95 return CompletePostinstall(ErrorCode::kPostinstallRunnerError);
96 }
97
98 string abs_path =
99 base::FilePath(fs_mount_dir_).Append(postinstall_path).value();
Alex Deymo390efed2016-02-18 11:00:40 -0800100 if (!base::StartsWith(
101 abs_path, fs_mount_dir_, base::CompareCase::SENSITIVE)) {
102 LOG(ERROR) << "Invalid relative postinstall path: "
103 << partition.postinstall_path;
104 return CompletePostinstall(ErrorCode::kPostinstallRunnerError);
105 }
106
107 if (!utils::MountFilesystem(mountable_device,
108 fs_mount_dir_,
109 MS_RDONLY,
Alex Deymo14dbd332016-03-01 18:55:54 -0800110 partition.filesystem_type,
111 constants::kPostinstallMountOptions)) {
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700112 return CompletePartitionPostinstall(
113 1, "Error mounting the device " + mountable_device);
114 }
115
Alex Deymo390efed2016-02-18 11:00:40 -0800116 LOG(INFO) << "Performing postinst (" << partition.postinstall_path << " at "
117 << abs_path << ") installed on device " << partition.target_path
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700118 << " and mountable device " << mountable_device;
119
Alex Deymo032e7722014-03-25 17:53:56 -0700120 // Logs the file format of the postinstall script we are about to run. This
121 // will help debug when the postinstall script doesn't match the architecture
122 // of our build.
Alex Deymo390efed2016-02-18 11:00:40 -0800123 LOG(INFO) << "Format file for new " << partition.postinstall_path
124 << " is: " << utils::GetFileFormat(abs_path);
Alex Deymo032e7722014-03-25 17:53:56 -0700125
Darin Petkov6f03a3b2010-11-10 14:27:14 -0800126 // Runs the postinstall script asynchronously to free up the main loop while
127 // it's running.
Alex Deymo390efed2016-02-18 11:00:40 -0800128 vector<string> command = {abs_path, partition.target_path};
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700129 if (!Subprocess::Get().Exec(
130 command,
131 base::Bind(
132 &PostinstallRunnerAction::CompletePartitionPostinstall,
133 base::Unretained(this)))) {
134 CompletePartitionPostinstall(1, "Postinstall didn't launch");
Darin Petkov58dd1342011-05-06 12:05:13 -0700135 }
Darin Petkov6f03a3b2010-11-10 14:27:14 -0800136}
137
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700138void PostinstallRunnerAction::CompletePartitionPostinstall(
139 int return_code,
140 const string& output) {
Alex Deymo390efed2016-02-18 11:00:40 -0800141 utils::UnmountFilesystem(fs_mount_dir_);
142#ifndef ANDROID
143 if (!base::DeleteFile(base::FilePath(fs_mount_dir_), false)) {
144 PLOG(WARNING) << "Not removing temporary mountpoint " << fs_mount_dir_;
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700145 }
Alex Deymo390efed2016-02-18 11:00:40 -0800146#endif // !ANDROID
147 fs_mount_dir_.clear();
Alex Deymo31d95ac2015-09-17 11:56:18 -0700148
Darin Petkov6f03a3b2010-11-10 14:27:14 -0800149 if (return_code != 0) {
150 LOG(ERROR) << "Postinst command failed with code: " << return_code;
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700151 ErrorCode error_code = ErrorCode::kPostinstallRunnerError;
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700152
Andrew de los Reyesfe57d542011-06-07 09:00:36 -0700153 if (return_code == 3) {
Andrew de los Reyesc1d5c932011-04-20 17:15:47 -0700154 // This special return code means that we tried to update firmware,
155 // but couldn't because we booted from FW B, and we need to reboot
156 // to get back to FW A.
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700157 error_code = ErrorCode::kPostinstallBootedFromFirmwareB;
Andrew de los Reyesc1d5c932011-04-20 17:15:47 -0700158 }
Don Garrett81018e02013-07-30 18:46:31 -0700159
160 if (return_code == 4) {
161 // This special return code means that we tried to update firmware,
162 // but couldn't because we booted from FW B, and we need to reboot
163 // to get back to FW A.
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700164 error_code = ErrorCode::kPostinstallFirmwareRONotUpdatable;
Don Garrett81018e02013-07-30 18:46:31 -0700165 }
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700166 return CompletePostinstall(error_code);
167 }
168 current_partition_++;
169 PerformPartitionPostinstall();
170}
171
172void PostinstallRunnerAction::CompletePostinstall(ErrorCode error_code) {
173 // We only attempt to mark the new slot as active if all the postinstall
174 // steps succeeded.
175 if (error_code == ErrorCode::kSuccess &&
Alex Deymob15a0b82015-11-25 20:30:40 -0300176 !boot_control_->SetActiveBootSlot(install_plan_.target_slot)) {
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700177 error_code = ErrorCode::kPostinstallRunnerError;
178 }
179
180 ScopedActionCompleter completer(processor_, this);
Alex Deymocbc22742016-03-04 17:53:02 -0800181 completer.set_code(error_code);
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700182
183 if (error_code != ErrorCode::kSuccess) {
184 LOG(ERROR) << "Postinstall action failed.";
185
186 // Undo any changes done to trigger Powerwash using clobber-state.
187 if (powerwash_marker_created_)
188 utils::DeletePowerwashMarkerFile(powerwash_marker_file_);
Don Garrett81018e02013-07-30 18:46:31 -0700189
Darin Petkov6f03a3b2010-11-10 14:27:14 -0800190 return;
191 }
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700192
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700193 LOG(INFO) << "All post-install commands succeeded";
Chris Sosad317e402013-06-12 13:47:09 -0700194 if (HasOutputPipe()) {
195 SetOutputObject(install_plan_);
196 }
adlr@google.com3defe6a2009-12-04 20:57:17 +0000197}
198
adlr@google.com3defe6a2009-12-04 20:57:17 +0000199} // namespace chromeos_update_engine