blob: 33bbf5b1ec97c6b5c8e0405dce4026f1f51e4bd1 [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"
28#include "update_engine/common/subprocess.h"
29#include "update_engine/common/utils.h"
adlr@google.com3defe6a2009-12-04 20:57:17 +000030
31namespace chromeos_update_engine {
32
33using std::string;
Andrew de los Reyesf9714432010-05-04 10:21:23 -070034using std::vector;
adlr@google.com3defe6a2009-12-04 20:57:17 +000035
36namespace {
Alex Deymo032e7722014-03-25 17:53:56 -070037// The absolute path to the post install command.
Darin Petkov6f03a3b2010-11-10 14:27:14 -080038const char kPostinstallScript[] = "/postinst";
Alex Deymo032e7722014-03-25 17:53:56 -070039
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.
43const char kDebugPostinstallBinaryPath[] = "/usr/bin/cros_installer";
adlr@google.com3defe6a2009-12-04 20:57:17 +000044}
45
46void PostinstallRunnerAction::PerformAction() {
47 CHECK(HasInputObject());
Chris Sosad317e402013-06-12 13:47:09 -070048 install_plan_ = GetInputObject();
Darin Petkov6f03a3b2010-11-10 14:27:14 -080049
Chris Sosad317e402013-06-12 13:47:09 -070050 if (install_plan_.powerwash_required) {
Gilad Arnold30dedd82013-07-03 06:19:09 -070051 if (utils::CreatePowerwashMarkerFile(powerwash_marker_file_)) {
Jay Srinivasan1c0fe792013-03-28 16:45:25 -070052 powerwash_marker_created_ = true;
53 } else {
Alex Deymoe5e5fe92015-10-05 09:28:19 -070054 return CompletePostinstall(ErrorCode::kPostinstallPowerwashError);
Jay Srinivasan1c0fe792013-03-28 16:45:25 -070055 }
56 }
57
Alex Deymoe5e5fe92015-10-05 09:28:19 -070058 PerformPartitionPostinstall();
59}
60
61void 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 Deymo032e7722014-03-25 17:53:56 -070097 // 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 Petkov6f03a3b2010-11-10 14:27:14 -0800106 // Runs the postinstall script asynchronously to free up the main loop while
107 // it's running.
108 vector<string> command;
Chris Sosa000ecb32014-04-23 12:21:18 -0700109 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 Deymoe5e5fe92015-10-05 09:28:19 -0700116 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 Petkov58dd1342011-05-06 12:05:13 -0700123 }
Darin Petkov6f03a3b2010-11-10 14:27:14 -0800124}
125
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700126void 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 Deymo31d95ac2015-09-17 11:56:18 -0700134
Darin Petkov6f03a3b2010-11-10 14:27:14 -0800135 if (return_code != 0) {
136 LOG(ERROR) << "Postinst command failed with code: " << return_code;
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700137 ErrorCode error_code = ErrorCode::kPostinstallRunnerError;
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700138
Andrew de los Reyesfe57d542011-06-07 09:00:36 -0700139 if (return_code == 3) {
Andrew de los Reyesc1d5c932011-04-20 17:15:47 -0700140 // 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 Deymoe5e5fe92015-10-05 09:28:19 -0700143 error_code = ErrorCode::kPostinstallBootedFromFirmwareB;
Andrew de los Reyesc1d5c932011-04-20 17:15:47 -0700144 }
Don Garrett81018e02013-07-30 18:46:31 -0700145
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 Deymoe5e5fe92015-10-05 09:28:19 -0700150 error_code = ErrorCode::kPostinstallFirmwareRONotUpdatable;
Don Garrett81018e02013-07-30 18:46:31 -0700151 }
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700152 return CompletePostinstall(error_code);
153 }
154 current_partition_++;
155 PerformPartitionPostinstall();
156}
157
158void 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 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