blob: d57ef4e8cf7dab592035363c18b8801195177954 [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 Deymo390efed2016-02-18 11:00:40 -080090 string abs_path = base::FilePath(fs_mount_dir_)
91 .AppendASCII(partition.postinstall_path)
92 .value();
93 if (!base::StartsWith(
94 abs_path, fs_mount_dir_, base::CompareCase::SENSITIVE)) {
95 LOG(ERROR) << "Invalid relative postinstall path: "
96 << partition.postinstall_path;
97 return CompletePostinstall(ErrorCode::kPostinstallRunnerError);
98 }
99
100 if (!utils::MountFilesystem(mountable_device,
101 fs_mount_dir_,
102 MS_RDONLY,
Alex Deymo14dbd332016-03-01 18:55:54 -0800103 partition.filesystem_type,
104 constants::kPostinstallMountOptions)) {
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700105 return CompletePartitionPostinstall(
106 1, "Error mounting the device " + mountable_device);
107 }
108
Alex Deymo390efed2016-02-18 11:00:40 -0800109 LOG(INFO) << "Performing postinst (" << partition.postinstall_path << " at "
110 << abs_path << ") installed on device " << partition.target_path
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700111 << " and mountable device " << mountable_device;
112
Alex Deymo032e7722014-03-25 17:53:56 -0700113 // Logs the file format of the postinstall script we are about to run. This
114 // will help debug when the postinstall script doesn't match the architecture
115 // of our build.
Alex Deymo390efed2016-02-18 11:00:40 -0800116 LOG(INFO) << "Format file for new " << partition.postinstall_path
117 << " is: " << utils::GetFileFormat(abs_path);
Alex Deymo032e7722014-03-25 17:53:56 -0700118
Darin Petkov6f03a3b2010-11-10 14:27:14 -0800119 // Runs the postinstall script asynchronously to free up the main loop while
120 // it's running.
Alex Deymo390efed2016-02-18 11:00:40 -0800121 vector<string> command = {abs_path, partition.target_path};
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700122 if (!Subprocess::Get().Exec(
123 command,
124 base::Bind(
125 &PostinstallRunnerAction::CompletePartitionPostinstall,
126 base::Unretained(this)))) {
127 CompletePartitionPostinstall(1, "Postinstall didn't launch");
Darin Petkov58dd1342011-05-06 12:05:13 -0700128 }
Darin Petkov6f03a3b2010-11-10 14:27:14 -0800129}
130
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700131void PostinstallRunnerAction::CompletePartitionPostinstall(
132 int return_code,
133 const string& output) {
Alex Deymo390efed2016-02-18 11:00:40 -0800134 utils::UnmountFilesystem(fs_mount_dir_);
135#ifndef ANDROID
136 if (!base::DeleteFile(base::FilePath(fs_mount_dir_), false)) {
137 PLOG(WARNING) << "Not removing temporary mountpoint " << fs_mount_dir_;
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700138 }
Alex Deymo390efed2016-02-18 11:00:40 -0800139#endif // !ANDROID
140 fs_mount_dir_.clear();
Alex Deymo31d95ac2015-09-17 11:56:18 -0700141
Darin Petkov6f03a3b2010-11-10 14:27:14 -0800142 if (return_code != 0) {
143 LOG(ERROR) << "Postinst command failed with code: " << return_code;
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700144 ErrorCode error_code = ErrorCode::kPostinstallRunnerError;
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700145
Andrew de los Reyesfe57d542011-06-07 09:00:36 -0700146 if (return_code == 3) {
Andrew de los Reyesc1d5c932011-04-20 17:15:47 -0700147 // 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::kPostinstallBootedFromFirmwareB;
Andrew de los Reyesc1d5c932011-04-20 17:15:47 -0700151 }
Don Garrett81018e02013-07-30 18:46:31 -0700152
153 if (return_code == 4) {
154 // 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::kPostinstallFirmwareRONotUpdatable;
Don Garrett81018e02013-07-30 18:46:31 -0700158 }
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700159 return CompletePostinstall(error_code);
160 }
161 current_partition_++;
162 PerformPartitionPostinstall();
163}
164
165void PostinstallRunnerAction::CompletePostinstall(ErrorCode error_code) {
166 // We only attempt to mark the new slot as active if all the postinstall
167 // steps succeeded.
168 if (error_code == ErrorCode::kSuccess &&
Alex Deymob15a0b82015-11-25 20:30:40 -0300169 !boot_control_->SetActiveBootSlot(install_plan_.target_slot)) {
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700170 error_code = ErrorCode::kPostinstallRunnerError;
171 }
172
173 ScopedActionCompleter completer(processor_, this);
174
175 if (error_code != ErrorCode::kSuccess) {
176 LOG(ERROR) << "Postinstall action failed.";
177
178 // Undo any changes done to trigger Powerwash using clobber-state.
179 if (powerwash_marker_created_)
180 utils::DeletePowerwashMarkerFile(powerwash_marker_file_);
Don Garrett81018e02013-07-30 18:46:31 -0700181
Darin Petkov6f03a3b2010-11-10 14:27:14 -0800182 return;
183 }
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700184
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700185 LOG(INFO) << "All post-install commands succeeded";
Chris Sosad317e402013-06-12 13:47:09 -0700186 if (HasOutputPipe()) {
187 SetOutputObject(install_plan_);
188 }
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700189
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700190 completer.set_code(ErrorCode::kSuccess);
adlr@google.com3defe6a2009-12-04 20:57:17 +0000191}
192
adlr@google.com3defe6a2009-12-04 20:57:17 +0000193} // namespace chromeos_update_engine